dc_mimeparser_parse: accept input as slice, not as pointer + length

This commit is contained in:
Dmitry Bogatov
2019-08-26 16:58:31 +00:00
committed by Floris Bruynooghe
parent d3048aa06f
commit 21976b14a6
3 changed files with 7 additions and 12 deletions

View File

@@ -123,18 +123,14 @@ unsafe fn dc_mimepart_unref(mut mimepart: dc_mimepart_t) {
mimepart.msg_raw = 0 as *mut libc::c_char; mimepart.msg_raw = 0 as *mut libc::c_char;
} }
pub unsafe fn dc_mimeparser_parse( pub unsafe fn dc_mimeparser_parse(mimeparser: &mut dc_mimeparser_t, body: &[u8]) {
mimeparser: &mut dc_mimeparser_t,
body_not_terminated: *const libc::c_char,
body_bytes: size_t,
) {
let r: libc::c_int; let r: libc::c_int;
let mut index: size_t = 0i32 as size_t; let mut index: size_t = 0i32 as size_t;
let optional_field: *mut mailimf_optional_field; let optional_field: *mut mailimf_optional_field;
dc_mimeparser_empty(mimeparser); dc_mimeparser_empty(mimeparser);
r = mailmime_parse( r = mailmime_parse(
body_not_terminated, body.as_ptr() as *const libc::c_char,
body_bytes, body.len(),
&mut index, &mut index,
&mut mimeparser.mimeroot, &mut mimeparser.mimeroot,
); );

View File

@@ -57,7 +57,8 @@ pub unsafe fn dc_receive_imf(
// somewhen, I did not found out anything that speaks against this approach yet) // somewhen, I did not found out anything that speaks against this approach yet)
let mut mime_parser = dc_mimeparser_new(context); let mut mime_parser = dc_mimeparser_new(context);
dc_mimeparser_parse(&mut mime_parser, imf_raw_not_terminated, imf_raw_bytes); let body = std::slice::from_raw_parts(imf_raw_not_terminated as *const u8, imf_raw_bytes);
dc_mimeparser_parse(&mut mime_parser, body);
if mime_parser.header.is_empty() { if mime_parser.header.is_empty() {
// Error - even adding an empty record won't help as we do not know the message ID // Error - even adding an empty record won't help as we do not know the message ID

View File

@@ -659,11 +659,9 @@ fn test_dc_mimeparser_with_context() {
let context = create_test_context(); let context = create_test_context();
let mut mimeparser = dc_mimeparser_new(&context.ctx); let mut mimeparser = dc_mimeparser_new(&context.ctx);
let raw: *const libc::c_char = let raw = b"Content-Type: multipart/mixed; boundary=\"==break==\";\nSubject: outer-subject\nX-Special-A: special-a\nFoo: Bar\nChat-Version: 0.0\n\n--==break==\nContent-Type: text/plain; protected-headers=\"v1\";\nSubject: inner-subject\nX-Special-B: special-b\nFoo: Xy\nChat-Version: 1.0\n\ntest1\n\n--==break==--\n\n\x00";
b"Content-Type: multipart/mixed; boundary=\"==break==\";\nSubject: outer-subject\nX-Special-A: special-a\nFoo: Bar\nChat-Version: 0.0\n\n--==break==\nContent-Type: text/plain; protected-headers=\"v1\";\nSubject: inner-subject\nX-Special-B: special-b\nFoo: Xy\nChat-Version: 1.0\n\ntest1\n\n--==break==--\n\n\x00"
as *const u8 as *const libc::c_char;
dc_mimeparser_parse(&mut mimeparser, raw, strlen(raw)); dc_mimeparser_parse(&mut mimeparser, &raw[..]);
assert_eq!( assert_eq!(
as_str(mimeparser.subject as *const libc::c_char), as_str(mimeparser.subject as *const libc::c_char),
"inner-subject", "inner-subject",