Make key::to_asc return String

This commit is contained in:
Alexander Krotov
2019-07-30 02:20:11 +03:00
parent 9314a5a8fd
commit 3ace4fcc2f
2 changed files with 12 additions and 19 deletions

View File

@@ -242,16 +242,16 @@ pub unsafe extern "C" fn dc_render_setup_file(
None
};
if let Some(payload_key_asc) = curr_private_key.map(|k| k.to_asc_c(headers)) {
if let Some(payload_key_asc) = curr_private_key.map(|k| k.to_asc(headers)) {
let payload_key_asc_c = CString::new(payload_key_asc).unwrap();
if let Some(encr) = dc_pgp_symm_encrypt(
passphrase,
payload_key_asc as *const libc::c_void,
strlen(payload_key_asc),
payload_key_asc_c.as_ptr() as *const libc::c_void,
payload_key_asc_c.as_bytes().len(),
) {
let encr_string_c = CString::new(encr).unwrap();
let mut encr_string = strdup(encr_string_c.as_ptr());
free(payload_key_asc as *mut libc::c_void);
let replacement: *mut libc::c_char =
dc_mprintf(b"-----BEGIN PGP MESSAGE-----\r\nPassphrase-Format: numeric9x4\r\nPassphrase-Begin: %s\x00"
as *const u8 as *const libc::c_char,

View File

@@ -216,22 +216,16 @@ impl Key {
}
}
/// Each header line must be terminated by `\r\n`, the result must be freed.
pub fn to_asc_c(&self, header: Option<(&str, &str)>) -> *mut libc::c_char {
/// Each header line must be terminated by `\r\n`
pub fn to_asc(&self, header: Option<(&str, &str)>) -> String {
let headers = header.map(|(key, value)| {
let mut m = BTreeMap::new();
m.insert(key.to_string(), value.to_string());
m
});
let buf = self
.to_armored_string(headers.as_ref())
.expect("failed to serialize key");
let buf_c = CString::new(buf).unwrap();
// need to use strdup to allocate the result with malloc
// so it can be `free`d later.
unsafe { strdup(buf_c.as_ptr()) }
self.to_armored_string(headers.as_ref())
.expect("failed to serialize key")
}
pub fn write_asc_to_file(&self, file: *const libc::c_char, context: &Context) -> bool {
@@ -239,15 +233,16 @@ impl Key {
return false;
}
let file_content = self.to_asc_c(None);
let file_content = self.to_asc(None);
let file_content_c = CString::new(file_content).unwrap();
let success = if 0
== unsafe {
dc_write_file(
context,
file,
file_content as *const libc::c_void,
strlen(file_content),
file_content_c.as_ptr() as *const libc::c_void,
file_content_c.as_bytes().len(),
)
} {
error!(context, 0, "Cannot write key to {}", to_string(file));
@@ -256,8 +251,6 @@ impl Key {
true
};
unsafe { free(file_content as *mut libc::c_void) };
success
}