use crate::error::{check, Result};
use crate::ffi;
use crate::ffi_safe::call_for_optional_string;
use crate::ops::{ArmorType, Input, Output};
use std::ffi::CString;
use std::ptr;
pub fn enarmor(input: &Input, output: &mut Output, ty: Option<ArmorType>) -> Result<()> {
let cstr = ty.map(|t| CString::new(t.as_str()).unwrap());
let ptr = cstr.as_ref().map(|c| c.as_ptr()).unwrap_or(ptr::null());
unsafe { check(ffi::rnp_enarmor(input.as_ptr(), output.as_ptr(), ptr)) }
}
pub fn dearmor(input: &Input, output: &mut Output) -> Result<()> {
unsafe { check(ffi::rnp_dearmor(input.as_ptr(), output.as_ptr())) }
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ContentType {
Message,
PublicKey,
SecretKey,
Signature,
Cleartext,
Unknown,
}
impl ContentType {
fn from_cstr(s: &str) -> Self {
match s {
"message" => ContentType::Message,
"public key" => ContentType::PublicKey,
"secret key" => ContentType::SecretKey,
"signature" => ContentType::Signature,
"cleartext signed message" | "cleartext" => ContentType::Cleartext,
_ => ContentType::Unknown,
}
}
}
pub fn guess_contents(input: &Input) -> Result<ContentType> {
let s = call_for_optional_string(|out| unsafe {
ffi::rnp_guess_contents(input.as_ptr(), out)
})?;
Ok(ContentType::from_cstr(s.as_deref().unwrap_or("")))
}
pub fn armor_bytes(bytes: &[u8], ty: ArmorType) -> Result<Vec<u8>> {
let input = Input::from_memory(bytes)?;
let mut output = Output::to_memory()?;
enarmor(&input, &mut output, Some(ty))?;
output.into_bytes()
}
pub fn dearmor_bytes(bytes: &[u8]) -> Result<Vec<u8>> {
let input = Input::from_memory(bytes)?;
let mut output = Output::to_memory()?;
dearmor(&input, &mut output)?;
output.into_bytes()
}
#[allow(unused_imports)]
use crate::error as _error_reexport;