diff --git a/Cargo.toml b/Cargo.toml index b89ebbfc8..f21c5a2ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ failure_derive = "0.1.5" # TODO: make optional rustyline = "4.1.0" lazy_static = "1.3.0" +regex = "1.1.6" [dev-dependencies] tempfile = "3.0" diff --git a/misc.c b/misc.c index 83a825a80..78b34a474 100644 --- a/misc.c +++ b/misc.c @@ -50,96 +50,3 @@ char* dc_mprintf(const char* format, ...) va_end(argp_copy); return buf; } - - -/** - * Add a string to the end of the current string in a string-builder-object. - * The internal buffer is reallocated as needed. - * If reallocation fails, the program halts. - * - * @param strbuilder The object to initialze. Must be initialized with - * dc_strbuilder_init(). - * @param text Null-terminated string to add to the end of the string-builder-string. - * @return Returns a pointer to the copy of the given text. - * The returned pointer is a pointer inside dc_strbuilder_t::buf and MUST NOT - * be freed. If the string-builder was empty before, the returned - * pointer is equal to dc_strbuilder_t::buf. - * If the given text is NULL, NULL is returned and the string-builder-object is not modified. - */ -static char* internal_dc_strbuilder_cat(dc_strbuilder_t* strbuilder, const char* text) -{ - // this function MUST NOT call logging functions as it is used to output the log - if (strbuilder==NULL || text==NULL) { - return NULL; - } - - int len = strlen(text); - - if (len > strbuilder->free) { - int add_bytes = DC_MAX(len, strbuilder->allocated); - int old_offset = (int)(strbuilder->eos - strbuilder->buf); - - strbuilder->allocated = strbuilder->allocated + add_bytes; - strbuilder->buf = realloc(strbuilder->buf, strbuilder->allocated+add_bytes); - - if (strbuilder->buf==NULL) { - exit(39); - } - - strbuilder->free = strbuilder->free + add_bytes; - strbuilder->eos = strbuilder->buf + old_offset; - } - - char* ret = strbuilder->eos; - - strcpy(strbuilder->eos, text); - strbuilder->eos += len; - strbuilder->free -= len; - - return ret; -} - -/** - * Add a formatted string to a string-builder-object. - * This function is similar to dc_strbuilder_cat() but allows the same - * formatting options as eg. printf() - * - * @param strbuilder The object to initialze. Must be initialized with - * dc_strbuilder_init(). - * @param format The formatting string to add to the string-builder-object. - * This parameter may be followed by data to be inserted into the - * formatting string, see eg. printf() - * @return None. - */ -void dc_strbuilder_catf(dc_strbuilder_t* strbuilder, const char* format, ...) -{ - char testbuf[1]; - char* buf = NULL; - int char_cnt_without_zero = 0; - - va_list argp; - va_list argp_copy; - va_start(argp, format); - va_copy(argp_copy, argp); - - char_cnt_without_zero = vsnprintf(testbuf, 0, format, argp); - va_end(argp); - if (char_cnt_without_zero < 0) { - va_end(argp_copy); - internal_dc_strbuilder_cat(strbuilder, "ErrFmt"); - return; - } - - buf = malloc(char_cnt_without_zero+2 /* +1 would be enough, however, protect against off-by-one-errors */); - if (buf==NULL) { - va_end(argp_copy); - internal_dc_strbuilder_cat(strbuilder, "ErrMem"); - return; - } - - vsnprintf(buf, char_cnt_without_zero+1, format, argp_copy); - va_end(argp_copy); - - internal_dc_strbuilder_cat(strbuilder, buf); - free(buf); -} diff --git a/misc.h b/misc.h index e8595255c..bf406f418 100644 --- a/misc.h +++ b/misc.h @@ -1,15 +1 @@ -#define DC_MAX(X, Y) (((X) > (Y))? (X) : (Y)) - -typedef struct _dc_strbuilder dc_strbuilder_t; - -struct _dc_strbuilder -{ - char* buf; - int allocated; - int free; - char* eos; -}; - - char* dc_mprintf (const char* format, ...); /* The result must be free()'d. */ -void dc_strbuilder_catf (dc_strbuilder_t*, const char* format, ...); diff --git a/src/context.rs b/src/context.rs index d7957b295..2079c7912 100644 --- a/src/context.rs +++ b/src/context.rs @@ -14,7 +14,6 @@ use crate::dc_msg::*; use crate::dc_receive_imf::*; use crate::dc_sqlite3::*; use crate::dc_stock::*; -use crate::dc_strbuilder::*; use crate::dc_tools::*; use crate::imap::*; use crate::key::*; @@ -571,23 +570,16 @@ unsafe fn get_sys_config_str(key: *const libc::c_char) -> *mut libc::c_char { } unsafe fn get_config_keys_str() -> *mut libc::c_char { - let mut ret = dc_strbuilder_t { - buf: std::ptr::null_mut(), - allocated: 0, - free: 0, - eos: std::ptr::null_mut(), - }; - dc_strbuilder_init(&mut ret, 0); - + let mut ret = String::new(); let mut i = 0; while i < (::std::mem::size_of::<[*const libc::c_char; 33]>()) .wrapping_div(::std::mem::size_of::<*mut libc::c_char>()) { - if strlen(ret.buf) > 0 { - dc_strbuilder_cat(&mut ret, b" \x00" as *const u8 as *const libc::c_char); + if !ret.is_empty() { + ret += " "; } - dc_strbuilder_cat(&mut ret, config_keys[i as usize]); + ret += &to_string(config_keys[i as usize]); i += 1 } @@ -596,14 +588,14 @@ unsafe fn get_config_keys_str() -> *mut libc::c_char { < (::std::mem::size_of::<[*const libc::c_char; 3]>()) .wrapping_div(::std::mem::size_of::<*mut libc::c_char>()) { - if strlen(ret.buf) > 0 { - dc_strbuilder_cat(&mut ret, b" \x00" as *const u8 as *const libc::c_char); + if !ret.is_empty() { + ret += " "; } - dc_strbuilder_cat(&mut ret, sys_config_keys[i as usize]); + ret += &to_string(sys_config_keys[i as usize]); i += 1 } - ret.buf + strdup(to_cstring(ret).as_ptr()) } pub unsafe fn dc_get_info(context: &Context) -> *mut libc::c_char { diff --git a/src/dc_contact.rs b/src/dc_contact.rs index 323dbf416..5dfd9746a 100644 --- a/src/dc_contact.rs +++ b/src/dc_contact.rs @@ -8,7 +8,6 @@ use crate::dc_log::*; use crate::dc_loginparam::*; use crate::dc_sqlite3::*; use crate::dc_stock::*; -use crate::dc_strbuilder::*; use crate::dc_tools::*; use crate::key::*; use crate::peerstate::*; @@ -759,7 +758,7 @@ pub unsafe fn dc_get_contact_encrinfo( context: &Context, contact_id: uint32_t, ) -> *mut libc::c_char { - let mut ret: dc_strbuilder_t; + let mut ret = String::new(); let loginparam: *mut dc_loginparam_t = dc_loginparam_new(); let contact: *mut dc_contact_t = dc_contact_new(context); @@ -768,13 +767,6 @@ pub unsafe fn dc_get_contact_encrinfo( let mut fingerprint_other_unverified: *mut libc::c_char = 0 as *mut libc::c_char; let mut p: *mut libc::c_char; - ret = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut ret, 0i32); if !(!dc_contact_load_from_db(contact, &context.sql.clone().read().unwrap(), contact_id)) { let peerstate = Peerstate::from_addr( context, @@ -803,7 +795,7 @@ pub unsafe fn dc_get_contact_encrinfo( 25i32 }, ); - dc_strbuilder_cat(&mut ret, p); + ret += as_str(p); free(p as *mut libc::c_void); if self_key.is_none() { dc_ensure_secret_key_exists(context); @@ -813,11 +805,9 @@ pub unsafe fn dc_get_contact_encrinfo( &context.sql.clone().read().unwrap(), ); } - dc_strbuilder_cat(&mut ret, b" \x00" as *const u8 as *const libc::c_char); p = dc_stock_str(context, 30i32); - dc_strbuilder_cat(&mut ret, p); + ret += &format!(" {}:", as_str(p)); free(p as *mut libc::c_void); - dc_strbuilder_cat(&mut ret, b":\x00" as *const u8 as *const libc::c_char); fingerprint_self = self_key .map(|k| k.formatted_fingerprint_c()) @@ -835,34 +825,26 @@ pub unsafe fn dc_get_contact_encrinfo( { cat_fingerprint( &mut ret, - (*loginparam).addr, + to_string((*loginparam).addr), fingerprint_self, 0 as *const libc::c_char, ); - let c_addr = to_cstring(peerstate.addr.as_ref().unwrap()); cat_fingerprint( &mut ret, - c_addr.as_ptr(), + peerstate.addr.as_ref().unwrap(), fingerprint_other_verified, fingerprint_other_unverified, ); } else { - let c_addr = peerstate.addr.as_ref().map(to_cstring).unwrap_or_default(); - let addr_ptr = if peerstate.addr.is_some() { - c_addr.as_ptr() - } else { - std::ptr::null() - }; - cat_fingerprint( &mut ret, - addr_ptr, + peerstate.addr.as_ref().unwrap(), fingerprint_other_verified, fingerprint_other_unverified, ); cat_fingerprint( &mut ret, - (*loginparam).addr, + to_string((*loginparam).addr), fingerprint_self, 0 as *const libc::c_char, ); @@ -871,11 +853,11 @@ pub unsafe fn dc_get_contact_encrinfo( && 0 == (*loginparam).server_flags & 0x40000i32 { p = dc_stock_str(context, 27i32); - dc_strbuilder_cat(&mut ret, p); + ret += as_str(p); free(p as *mut libc::c_void); } else { p = dc_stock_str(context, 28i32); - dc_strbuilder_cat(&mut ret, p); + ret += as_str(p); free(p as *mut libc::c_void); } } @@ -887,26 +869,24 @@ pub unsafe fn dc_get_contact_encrinfo( free(fingerprint_other_verified as *mut libc::c_void); free(fingerprint_other_unverified as *mut libc::c_void); - ret.buf + strdup(to_cstring(ret).as_ptr()) } unsafe fn cat_fingerprint( - ret: *mut dc_strbuilder_t, - addr: *const libc::c_char, + ret: &mut String, + addr: impl AsRef, fingerprint_verified: *const libc::c_char, fingerprint_unverified: *const libc::c_char, ) { - dc_strbuilder_cat(ret, b"\n\n\x00" as *const u8 as *const libc::c_char); - dc_strbuilder_cat(ret, addr); - dc_strbuilder_cat(ret, b":\n\x00" as *const u8 as *const libc::c_char); - dc_strbuilder_cat( - ret, + *ret += &format!( + "\n\n{}:\n{}", + addr.as_ref(), if !fingerprint_verified.is_null() && 0 != *fingerprint_verified.offset(0isize) as libc::c_int { - fingerprint_verified + as_str(fingerprint_verified) } else { - fingerprint_unverified + as_str(fingerprint_unverified) }, ); if !fingerprint_verified.is_null() @@ -915,14 +895,12 @@ unsafe fn cat_fingerprint( && 0 != *fingerprint_unverified.offset(0isize) as libc::c_int && strcmp(fingerprint_verified, fingerprint_unverified) != 0i32 { - dc_strbuilder_cat(ret, b"\n\n\x00" as *const u8 as *const libc::c_char); - dc_strbuilder_cat(ret, addr); - dc_strbuilder_cat( - ret, - b" (alternative):\n\x00" as *const u8 as *const libc::c_char, + *ret += &format!( + "\n\n{} (alternative):\n{}", + addr.as_ref(), + as_str(fingerprint_unverified) ); - dc_strbuilder_cat(ret, fingerprint_unverified); - }; + } } pub unsafe fn dc_delete_contact(context: &Context, contact_id: uint32_t) -> bool { diff --git a/src/dc_dehtml.rs b/src/dc_dehtml.rs index 9196e76ef..805ea5c6b 100644 --- a/src/dc_dehtml.rs +++ b/src/dc_dehtml.rs @@ -1,204 +1,151 @@ +use lazy_static::lazy_static; + use crate::dc_saxparser::*; -use crate::dc_strbuilder::*; use crate::dc_tools::*; use crate::x::*; -#[derive(Copy, Clone)] -#[repr(C)] -pub struct dehtml_t { - pub strbuilder: dc_strbuilder_t, - pub add_text: libc::c_int, - pub last_href: *mut libc::c_char, +lazy_static! { + static ref LINE_RE: regex::Regex = regex::Regex::new(r"(\r?\n)+").unwrap(); } -/* ** library-internal *********************************************************/ -/* dc_dehtml() returns way too many lineends; however, an optimisation on this issue is not needed as the lineends are typically remove in further processing by the caller */ +struct Dehtml { + strbuilder: String, + add_text: AddText, + last_href: *mut libc::c_char, +} + +#[derive(Debug, PartialEq)] +enum AddText { + No, + YesRemoveLineEnds, + YesPreserveLineEnds, +} + +// dc_dehtml() returns way too many lineends; however, an optimisation on this issue is not needed as +// the lineends are typically remove in further processing by the caller pub unsafe fn dc_dehtml(buf_terminated: *mut libc::c_char) -> *mut libc::c_char { dc_trim(buf_terminated); if *buf_terminated.offset(0isize) as libc::c_int == 0i32 { return dc_strdup(b"\x00" as *const u8 as *const libc::c_char); - } else { - let mut dehtml: dehtml_t = dehtml_t { - strbuilder: dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }, - add_text: 0, - last_href: 0 as *mut libc::c_char, - }; - let mut saxparser: dc_saxparser_t = dc_saxparser_t { - starttag_cb: None, - endtag_cb: None, - text_cb: None, - userdata: 0 as *mut libc::c_void, - }; - memset( - &mut dehtml as *mut dehtml_t as *mut libc::c_void, - 0, - ::std::mem::size_of::(), - ); - dehtml.add_text = 1i32; - dc_strbuilder_init( - &mut dehtml.strbuilder, - strlen(buf_terminated) as libc::c_int, - ); - dc_saxparser_init( - &mut saxparser, - &mut dehtml as *mut dehtml_t as *mut libc::c_void, - ); - dc_saxparser_set_tag_handler( - &mut saxparser, - Some(dehtml_starttag_cb), - Some(dehtml_endtag_cb), - ); - dc_saxparser_set_text_handler(&mut saxparser, Some(dehtml_text_cb)); - dc_saxparser_parse(&mut saxparser, buf_terminated); - free(dehtml.last_href as *mut libc::c_void); - return dehtml.strbuilder.buf; + } + + let mut dehtml = Dehtml { + strbuilder: String::with_capacity(strlen(buf_terminated)), + add_text: AddText::YesRemoveLineEnds, + last_href: 0 as *mut libc::c_char, }; + let mut saxparser = dc_saxparser_t { + starttag_cb: None, + endtag_cb: None, + text_cb: None, + userdata: 0 as *mut libc::c_void, + }; + dc_saxparser_init( + &mut saxparser, + &mut dehtml as *mut Dehtml as *mut libc::c_void, + ); + dc_saxparser_set_tag_handler( + &mut saxparser, + Some(dehtml_starttag_cb), + Some(dehtml_endtag_cb), + ); + dc_saxparser_set_text_handler(&mut saxparser, Some(dehtml_text_cb)); + dc_saxparser_parse(&mut saxparser, buf_terminated); + free(dehtml.last_href as *mut libc::c_void); + + strdup(to_cstring(dehtml.strbuilder).as_ptr()) } + unsafe fn dehtml_text_cb( userdata: *mut libc::c_void, text: *const libc::c_char, _len: libc::c_int, ) { - let dehtml: *mut dehtml_t = userdata as *mut dehtml_t; - if (*dehtml).add_text != 0i32 { - let last_added: *mut libc::c_char = dc_strbuilder_cat(&mut (*dehtml).strbuilder, text); - if (*dehtml).add_text == 1i32 { - let mut p: *mut libc::c_uchar = last_added as *mut libc::c_uchar; - while 0 != *p { - if *p as libc::c_int == '\n' as i32 { - let mut last_is_lineend: libc::c_int = 1i32; - let mut p2: *const libc::c_uchar = p.offset(-1isize); - while p2 >= (*dehtml).strbuilder.buf as *const libc::c_uchar { - if *p2 as libc::c_int == '\r' as i32 { - p2 = p2.offset(-1isize) - } else { - if *p2 as libc::c_int == '\n' as i32 { - break; - } - last_is_lineend = 0i32; - break; - } - } - *p = (if 0 != last_is_lineend { - '\r' as i32 - } else { - ' ' as i32 - }) as libc::c_uchar - } - p = p.offset(1isize) + let dehtml = &mut *(userdata as *mut Dehtml); + + if dehtml.add_text == AddText::YesPreserveLineEnds + || dehtml.add_text == AddText::YesRemoveLineEnds + { + let last_added = std::ffi::CStr::from_ptr(text).to_string_lossy(); + + if dehtml.add_text == AddText::YesRemoveLineEnds { + dehtml.strbuilder += LINE_RE.replace_all(last_added.as_ref(), "\r").as_ref(); + } else { + dehtml.strbuilder += last_added.as_ref(); + } + } +} + +unsafe fn dehtml_endtag_cb(userdata: *mut libc::c_void, tag: *const libc::c_char) { + let mut dehtml = &mut *(userdata as *mut Dehtml); + let tag = std::ffi::CStr::from_ptr(tag).to_string_lossy(); + + match tag.as_ref() { + "p" | "div" | "table" | "td" | "style" | "script" | "title" | "pre" => { + dehtml.strbuilder += "\n\n"; + dehtml.add_text = AddText::YesRemoveLineEnds; + } + "a" => { + if !dehtml.last_href.is_null() { + dehtml.strbuilder += "]("; + dehtml.strbuilder += std::ffi::CStr::from_ptr((*dehtml).last_href) + .to_string_lossy() + .as_ref(); + dehtml.strbuilder += ")"; + free(dehtml.last_href as *mut libc::c_void); + dehtml.last_href = 0 as *mut libc::c_char; } } - }; -} -unsafe fn dehtml_endtag_cb(userdata: *mut libc::c_void, tag: *const libc::c_char) { - let mut dehtml: *mut dehtml_t = userdata as *mut dehtml_t; - if strcmp(tag, b"p\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"div\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"table\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"td\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"style\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"script\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"title\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"pre\x00" as *const u8 as *const libc::c_char) == 0i32 - { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"\n\n\x00" as *const u8 as *const libc::c_char, - ); - (*dehtml).add_text = 1i32 - } else if strcmp(tag, b"a\x00" as *const u8 as *const libc::c_char) == 0i32 { - if !(*dehtml).last_href.is_null() { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"](\x00" as *const u8 as *const libc::c_char, - ); - dc_strbuilder_cat(&mut (*dehtml).strbuilder, (*dehtml).last_href); - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b")\x00" as *const u8 as *const libc::c_char, - ); - free((*dehtml).last_href as *mut libc::c_void); - (*dehtml).last_href = 0 as *mut libc::c_char + "b" | "strong" => { + dehtml.strbuilder += "*"; } - } else if strcmp(tag, b"b\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"strong\x00" as *const u8 as *const libc::c_char) == 0i32 - { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"*\x00" as *const u8 as *const libc::c_char, - ); - } else if strcmp(tag, b"i\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"em\x00" as *const u8 as *const libc::c_char) == 0i32 - { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"_\x00" as *const u8 as *const libc::c_char, - ); - }; + "i" | "em" => { + dehtml.strbuilder += "_"; + } + _ => {} + } } + unsafe fn dehtml_starttag_cb( userdata: *mut libc::c_void, tag: *const libc::c_char, attr: *mut *mut libc::c_char, ) { - let mut dehtml: *mut dehtml_t = userdata as *mut dehtml_t; - if strcmp(tag, b"p\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"div\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"table\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"td\x00" as *const u8 as *const libc::c_char) == 0i32 - { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"\n\n\x00" as *const u8 as *const libc::c_char, - ); - (*dehtml).add_text = 1i32 - } else if strcmp(tag, b"br\x00" as *const u8 as *const libc::c_char) == 0i32 { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"\n\x00" as *const u8 as *const libc::c_char, - ); - (*dehtml).add_text = 1i32 - } else if strcmp(tag, b"style\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"script\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"title\x00" as *const u8 as *const libc::c_char) == 0i32 - { - (*dehtml).add_text = 0i32 - } else if strcmp(tag, b"pre\x00" as *const u8 as *const libc::c_char) == 0i32 { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"\n\n\x00" as *const u8 as *const libc::c_char, - ); - (*dehtml).add_text = 2i32 - } else if strcmp(tag, b"a\x00" as *const u8 as *const libc::c_char) == 0i32 { - free((*dehtml).last_href as *mut libc::c_void); - (*dehtml).last_href = dc_strdup_keep_null(dc_attr_find( - attr, - b"href\x00" as *const u8 as *const libc::c_char, - )); - if !(*dehtml).last_href.is_null() { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"[\x00" as *const u8 as *const libc::c_char, - ); + let mut dehtml = &mut *(userdata as *mut Dehtml); + let tag = std::ffi::CStr::from_ptr(tag).to_string_lossy(); + + match tag.as_ref() { + "p" | "div" | "table" | "td" => { + dehtml.strbuilder += "\n\n"; + dehtml.add_text = AddText::YesRemoveLineEnds; } - } else if strcmp(tag, b"b\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"strong\x00" as *const u8 as *const libc::c_char) == 0i32 - { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"*\x00" as *const u8 as *const libc::c_char, - ); - } else if strcmp(tag, b"i\x00" as *const u8 as *const libc::c_char) == 0i32 - || strcmp(tag, b"em\x00" as *const u8 as *const libc::c_char) == 0i32 - { - dc_strbuilder_cat( - &mut (*dehtml).strbuilder, - b"_\x00" as *const u8 as *const libc::c_char, - ); - }; + "br" => { + dehtml.strbuilder += "\n"; + dehtml.add_text = AddText::YesRemoveLineEnds; + } + "style" | "script" | "title" => { + dehtml.add_text = AddText::No; + } + "pre" => { + dehtml.strbuilder += "\n\n"; + dehtml.add_text = AddText::YesPreserveLineEnds; + } + "a" => { + free(dehtml.last_href as *mut libc::c_void); + dehtml.last_href = dc_strdup_keep_null(dc_attr_find( + attr, + b"href\x00" as *const u8 as *const libc::c_char, + )); + if !dehtml.last_href.is_null() { + dehtml.strbuilder += "["; + } + } + "b" | "strong" => { + dehtml.strbuilder += "*"; + } + "i" | "em" => { + dehtml.strbuilder += "_"; + } + _ => {} + } } diff --git a/src/dc_imex.rs b/src/dc_imex.rs index 31b44cda8..68af12dc4 100644 --- a/src/dc_imex.rs +++ b/src/dc_imex.rs @@ -16,7 +16,6 @@ use crate::dc_msg::*; use crate::dc_param::*; use crate::dc_sqlite3::*; use crate::dc_stock::*; -use crate::dc_strbuilder::*; use crate::dc_tools::*; use crate::key::*; use crate::pgp::*; @@ -328,38 +327,25 @@ pub unsafe extern "C" fn dc_render_setup_file( pub unsafe fn dc_create_setup_code(_context: &Context) -> *mut libc::c_char { let mut random_val: uint16_t; - let mut i: libc::c_int; - let mut ret: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut ret, 0i32); - i = 0i32; let mut rng = thread_rng(); - while i < 9i32 { + let mut ret = String::new(); + + for i in 0..9 { loop { random_val = rng.gen(); - if !(random_val as libc::c_int > 60000i32) { + if !(random_val as libc::c_int > 60000) { break; } } - random_val = (random_val as libc::c_int % 10000i32) as uint16_t; - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b"%s%04i\x00" as *const u8 as *const libc::c_char, - if 0 != i { - b"-\x00" as *const u8 as *const libc::c_char - } else { - b"\x00" as *const u8 as *const libc::c_char - }, + random_val = (random_val as libc::c_int % 10000) as uint16_t; + ret += &format!( + "{}{:04}", + if 0 != i { "-" } else { "" }, random_val as libc::c_int, ); - i += 1 } - ret.buf + strdup(to_cstring(ret).as_ptr()) } // TODO should return bool /rtn @@ -643,39 +629,29 @@ pub unsafe fn dc_normalize_setup_code( if in_0.is_null() { return 0 as *mut libc::c_char; } - let mut out: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut out, 0i32); - let mut outlen: libc::c_int; + let mut out = String::new(); + let mut outlen; let mut p1: *const libc::c_char = in_0; while 0 != *p1 { if *p1 as libc::c_int >= '0' as i32 && *p1 as libc::c_int <= '9' as i32 { - dc_strbuilder_catf( - &mut out as *mut dc_strbuilder_t, - b"%c\x00" as *const u8 as *const libc::c_char, - *p1 as libc::c_int, - ); - outlen = strlen(out.buf) as libc::c_int; - if outlen == 4i32 - || outlen == 9i32 - || outlen == 14i32 - || outlen == 19i32 - || outlen == 24i32 - || outlen == 29i32 - || outlen == 34i32 - || outlen == 39i32 + out += &format!("{}", *p1 as i32 as u8 as char); + outlen = out.len(); + if outlen == 4 + || outlen == 9 + || outlen == 14 + || outlen == 19 + || outlen == 24 + || outlen == 29 + || outlen == 34 + || outlen == 39 { - dc_strbuilder_cat(&mut out, b"-\x00" as *const u8 as *const libc::c_char); + out += "-"; } } - p1 = p1.offset(1isize) + p1 = p1.offset(1); } - out.buf + strdup(to_cstring(out).as_ptr()) } pub unsafe fn dc_job_do_DC_JOB_IMEX_IMAP(context: &Context, job: *mut dc_job_t) { diff --git a/src/dc_location.rs b/src/dc_location.rs index 9de309aa8..cf09d40af 100644 --- a/src/dc_location.rs +++ b/src/dc_location.rs @@ -9,7 +9,6 @@ use crate::dc_param::*; use crate::dc_saxparser::*; use crate::dc_sqlite3::*; use crate::dc_stock::*; -use crate::dc_strbuilder::*; use crate::dc_tools::*; use crate::types::*; use crate::x::*; @@ -327,13 +326,7 @@ pub unsafe fn dc_get_location_kml( let locations_send_until: i64; let locations_last_sent: i64; let mut location_count: libc::c_int = 0i32; - let mut ret: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut ret, 1000i32); + let mut ret = String::new(); self_addr = dc_sqlite3_get_config( context, @@ -354,11 +347,12 @@ pub unsafe fn dc_get_location_kml( locations_last_sent = sqlite3_column_int64(stmt, 2i32) as i64; sqlite3_finalize(stmt); stmt = 0 as *mut sqlite3_stmt; + if !(locations_send_begin == 0 || now > locations_send_until) { - dc_strbuilder_catf(&mut ret as *mut dc_strbuilder_t, - b"\n\n\n\x00" - as *const u8 as *const libc::c_char, - self_addr); + ret += &format!( + "\n\n\n", + to_string(self_addr), + ); stmt = dc_sqlite3_prepare( context, &context.sql.clone().read().unwrap(), @@ -378,45 +372,38 @@ pub unsafe fn dc_get_location_kml( sqlite3_bind_int(stmt, 4i32, 1i32); while sqlite3_step(stmt) == 100i32 { let location_id: uint32_t = sqlite3_column_int(stmt, 0i32) as uint32_t; - let latitude: *mut libc::c_char = dc_ftoa(sqlite3_column_double(stmt, 1i32)); - let longitude: *mut libc::c_char = dc_ftoa(sqlite3_column_double(stmt, 2i32)); - let accuracy: *mut libc::c_char = dc_ftoa(sqlite3_column_double(stmt, 3i32)); - let timestamp: *mut libc::c_char = - get_kml_timestamp(sqlite3_column_int64(stmt, 4i32) as i64); - dc_strbuilder_catf(&mut ret as *mut dc_strbuilder_t, - b"%s%s,%s\n\x00" - as *const u8 as - *const libc::c_char, timestamp, - accuracy, longitude, latitude); + let latitude = sqlite3_column_double(stmt, 1i32); + let longitude = sqlite3_column_double(stmt, 2i32); + let accuracy = sqlite3_column_double(stmt, 3i32); + let timestamp = get_kml_timestamp(sqlite3_column_int64(stmt, 4i32) as i64); + ret += &format!( + "{}{},{}\n\x00", + as_str(timestamp), + accuracy, + longitude, + latitude + ); location_count += 1; if !last_added_location_id.is_null() { *last_added_location_id = location_id } - free(latitude as *mut libc::c_void); - free(longitude as *mut libc::c_void); - free(accuracy as *mut libc::c_void); free(timestamp as *mut libc::c_void); } - if !(location_count == 0i32) { - dc_strbuilder_cat( - &mut ret, - b"\n\x00" as *const u8 as *const libc::c_char, - ); - success = 1i32 + if !(location_count == 0) { + ret += "\n"; + success = 1; } } } sqlite3_finalize(stmt); free(self_addr as *mut libc::c_void); - if 0 == success { - free(ret.buf as *mut libc::c_void); - } - return if 0 != success { - ret.buf + + if 0 != success { + strdup(to_cstring(ret).as_ptr()) } else { 0 as *mut libc::c_char - }; + } } /******************************************************************************* diff --git a/src/dc_loginparam.rs b/src/dc_loginparam.rs index 3f18a23ec..2e41981c5 100644 --- a/src/dc_loginparam.rs +++ b/src/dc_loginparam.rs @@ -1,6 +1,5 @@ use crate::context::Context; use crate::dc_sqlite3::*; -use crate::dc_strbuilder::*; use crate::dc_tools::*; use crate::types::*; use crate::x::*; @@ -274,92 +273,51 @@ pub unsafe fn dc_loginparam_get_readable(loginparam: *const dc_loginparam_t) -> ret } -unsafe fn get_readable_flags(flags: libc::c_int) -> *mut libc::c_char { - let mut strbuilder: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut strbuilder, 0i32); - let mut bit: libc::c_int = 0i32; - while bit <= 30i32 { - if 0 != flags & 1i32 << bit { - let mut flag_added: libc::c_int = 0i32; - if 1i32 << bit == 0x2i32 { - dc_strbuilder_cat( - &mut strbuilder, - b"OAUTH2 \x00" as *const u8 as *const libc::c_char, - ); - flag_added = 1i32 +fn get_readable_flags(flags: libc::c_int) -> *mut libc::c_char { + let mut res = String::new(); + for bit in 0..31 { + if 0 != flags & 1 << bit { + let mut flag_added: libc::c_int = 0; + if 1 << bit == 0x2 { + res += "OAUTH2 "; + flag_added = 1; } - if 1i32 << bit == 0x4i32 { - dc_strbuilder_cat( - &mut strbuilder, - b"AUTH_NORMAL \x00" as *const u8 as *const libc::c_char, - ); - flag_added = 1i32 + if 1 << bit == 0x4 { + res += "AUTH_NORMAL "; + flag_added = 1; } - if 1i32 << bit == 0x100i32 { - dc_strbuilder_cat( - &mut strbuilder, - b"IMAP_STARTTLS \x00" as *const u8 as *const libc::c_char, - ); - flag_added = 1i32 + if 1 << bit == 0x100 { + res += "IMAP_STARTTLS "; + flag_added = 1; } - if 1i32 << bit == 0x200i32 { - dc_strbuilder_cat( - &mut strbuilder, - b"IMAP_SSL \x00" as *const u8 as *const libc::c_char, - ); - flag_added = 1i32 + if 1 << bit == 0x200 { + res += "IMAP_SSL "; + flag_added = 1; } - if 1i32 << bit == 0x400i32 { - dc_strbuilder_cat( - &mut strbuilder, - b"IMAP_PLAIN \x00" as *const u8 as *const libc::c_char, - ); - flag_added = 1i32 + if 1 << bit == 0x400 { + res += "IMAP_PLAIN "; + flag_added = 1; } - if 1i32 << bit == 0x10000i32 { - dc_strbuilder_cat( - &mut strbuilder, - b"SMTP_STARTTLS \x00" as *const u8 as *const libc::c_char, - ); - flag_added = 1i32 + if 1 << bit == 0x10000 { + res += "SMTP_STARTTLS "; + flag_added = 1 } - if 1i32 << bit == 0x20000i32 { - dc_strbuilder_cat( - &mut strbuilder, - b"SMTP_SSL \x00" as *const u8 as *const libc::c_char, - ); - flag_added = 1i32 + if 1 << bit == 0x20000 { + res += "SMTP_SSL "; + flag_added = 1 } - if 1i32 << bit == 0x40000i32 { - dc_strbuilder_cat( - &mut strbuilder, - b"SMTP_PLAIN \x00" as *const u8 as *const libc::c_char, - ); - flag_added = 1i32 + if 1 << bit == 0x40000 { + res += "SMTP_PLAIN "; + flag_added = 1 } if 0 == flag_added { - let temp: *mut libc::c_char = dc_mprintf( - b"0x%x \x00" as *const u8 as *const libc::c_char, - 1i32 << bit, - ); - dc_strbuilder_cat(&mut strbuilder, temp); - free(temp as *mut libc::c_void); + res += &format!("{:#0x}", 1 << bit); } } - bit += 1 } - if *strbuilder.buf.offset(0isize) as libc::c_int == 0i32 { - dc_strbuilder_cat( - &mut strbuilder, - b"0\x00" as *const u8 as *const libc::c_char, - ); + if res.is_empty() { + res += "0"; } - dc_trim(strbuilder.buf); - strbuilder.buf + unsafe { strdup(to_cstring(res).as_ptr()) } } diff --git a/src/dc_mimeparser.rs b/src/dc_mimeparser.rs index 4d2d06913..818a7641c 100644 --- a/src/dc_mimeparser.rs +++ b/src/dc_mimeparser.rs @@ -19,7 +19,6 @@ use crate::dc_log::*; use crate::dc_param::*; use crate::dc_simplify::*; use crate::dc_stock::*; -use crate::dc_strbuilder::*; use crate::dc_strencode::*; use crate::dc_tools::*; use crate::types::*; @@ -1291,13 +1290,7 @@ unsafe fn dc_mimeparser_add_single_part_if_known( `Content-Disposition: ... filename*=...` or `Content-Disposition: ... filename*0*=... filename*1*=... filename*2*=...` or `Content-Disposition: ... filename=...` */ - let mut filename_parts: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut filename_parts, 0i32); + let mut filename_parts = String::new(); let mut cur1: *mut clistiter = (*(*(*mime).mm_mime_fields).fld_list).first; while !cur1.is_null() { let field: *mut mailmime_field = (if !cur1.is_null() { @@ -1338,8 +1331,7 @@ unsafe fn dc_mimeparser_add_single_part_if_known( 9, ) == 0i32 { - dc_strbuilder_cat( - &mut filename_parts, + filename_parts += &to_string( (*(*dsp_param).pa_data.pa_parameter).pa_value, ); } else if (*dsp_param).pa_type @@ -1366,13 +1358,13 @@ unsafe fn dc_mimeparser_add_single_part_if_known( } } } - if 0 != strlen(filename_parts.buf) { + if !filename_parts.is_empty() { free(desired_filename as *mut libc::c_void); - desired_filename = dc_decode_ext_header(filename_parts.buf) + desired_filename = + dc_decode_ext_header(to_cstring(filename_parts).as_ptr()); } - free(filename_parts.buf as *mut libc::c_void); if desired_filename.is_null() { - let param: *mut mailmime_parameter = mailmime_find_ct_parameter( + let param = mailmime_find_ct_parameter( mime, b"name\x00" as *const u8 as *const libc::c_char, ); diff --git a/src/dc_msg.rs b/src/dc_msg.rs index f8f45d427..1600781b5 100644 --- a/src/dc_msg.rs +++ b/src/dc_msg.rs @@ -9,7 +9,6 @@ use crate::dc_lot::*; use crate::dc_param::*; use crate::dc_sqlite3::*; use crate::dc_stock::*; -use crate::dc_strbuilder::*; use crate::dc_tools::*; use crate::pgp::*; use crate::types::*; @@ -55,13 +54,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: uint32_t) -> *mut libc: let contact_from: *mut dc_contact_t = dc_contact_new(context); let mut rawtxt: *mut libc::c_char = 0 as *mut libc::c_char; let mut p: *mut libc::c_char; - let mut ret: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut ret, 0i32); + let mut ret = String::new(); dc_msg_load_from_db(msg, context, msg_id); dc_contact_load_from_db( @@ -74,47 +67,35 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: uint32_t) -> *mut libc: &context.sql.clone().read().unwrap(), b"SELECT txt_raw FROM msgs WHERE id=?;\x00" as *const u8 as *const libc::c_char, ); - sqlite3_bind_int(stmt, 1i32, msg_id as libc::c_int); - if sqlite3_step(stmt) != 100i32 { - p = dc_mprintf( - b"Cannot load message #%i.\x00" as *const u8 as *const libc::c_char, - msg_id as libc::c_int, - ); - dc_strbuilder_cat(&mut ret, p); - free(p as *mut libc::c_void); + sqlite3_bind_int(stmt, 1, msg_id as libc::c_int); + if sqlite3_step(stmt) != 100 { + ret += &format!("Cannot load message #{}.", msg_id as usize); } else { - rawtxt = dc_strdup(sqlite3_column_text(stmt, 0i32) as *mut libc::c_char); + rawtxt = dc_strdup(sqlite3_column_text(stmt, 0) as *mut libc::c_char); sqlite3_finalize(stmt); stmt = 0 as *mut sqlite3_stmt; dc_trim(rawtxt); - dc_truncate_str(rawtxt, 100000i32); - dc_strbuilder_cat(&mut ret, b"Sent: \x00" as *const u8 as *const libc::c_char); + dc_truncate_str(rawtxt, 100000); p = dc_timestamp_to_str(dc_msg_get_timestamp(msg)); - dc_strbuilder_cat(&mut ret, p); + ret += &format!("Sent: {}", as_str(p)); + free(p as *mut libc::c_void); p = dc_contact_get_name_n_addr(contact_from); - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b" by %s\x00" as *const u8 as *const libc::c_char, - p, - ); + ret += &format!(" by {}", to_string(p)); + free(p as *mut libc::c_void); - dc_strbuilder_cat(&mut ret, b"\n\x00" as *const u8 as *const libc::c_char); - if (*msg).from_id != 1i32 as libc::c_uint { - dc_strbuilder_cat( - &mut ret, - b"Received: \x00" as *const u8 as *const libc::c_char, - ); + ret += "\n"; + if (*msg).from_id != 1 as libc::c_uint { p = dc_timestamp_to_str(if 0 != (*msg).timestamp_rcvd { (*msg).timestamp_rcvd } else { (*msg).timestamp_sort }); - dc_strbuilder_cat(&mut ret, p); + ret += &format!("Received: {}", as_str(p)); free(p as *mut libc::c_void); - dc_strbuilder_cat(&mut ret, b"\n\x00" as *const u8 as *const libc::c_char); + ret += "\n"; } - if !((*msg).from_id == 2i32 as libc::c_uint || (*msg).to_id == 2i32 as libc::c_uint) { + if !((*msg).from_id == 2 as libc::c_uint || (*msg).to_id == 2 as libc::c_uint) { // device-internal message, no further details needed stmt = dc_sqlite3_prepare( context, @@ -122,155 +103,108 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: uint32_t) -> *mut libc: b"SELECT contact_id, timestamp_sent FROM msgs_mdns WHERE msg_id=?;\x00" as *const u8 as *const libc::c_char, ); - sqlite3_bind_int(stmt, 1i32, msg_id as libc::c_int); - while sqlite3_step(stmt) == 100i32 { - dc_strbuilder_cat(&mut ret, b"Read: \x00" as *const u8 as *const libc::c_char); - p = dc_timestamp_to_str(sqlite3_column_int64(stmt, 1i32) as i64); - dc_strbuilder_cat(&mut ret, p); + sqlite3_bind_int(stmt, 1, msg_id as libc::c_int); + while sqlite3_step(stmt) == 100 { + p = dc_timestamp_to_str(sqlite3_column_int64(stmt, 1) as i64); + ret += &format!("Read: {}", as_str(p)); free(p as *mut libc::c_void); - dc_strbuilder_cat(&mut ret, b" by \x00" as *const u8 as *const libc::c_char); - let contact: *mut dc_contact_t = dc_contact_new(context); + let contact = dc_contact_new(context); dc_contact_load_from_db( contact, &context.sql.clone().read().unwrap(), - sqlite3_column_int64(stmt, 0i32) as uint32_t, + sqlite3_column_int64(stmt, 0) as uint32_t, ); p = dc_contact_get_name_n_addr(contact); - dc_strbuilder_cat(&mut ret, p); + ret += &format!(" by {}", as_str(p)); free(p as *mut libc::c_void); dc_contact_unref(contact); - dc_strbuilder_cat(&mut ret, b"\n\x00" as *const u8 as *const libc::c_char); + ret += "\n"; } sqlite3_finalize(stmt); stmt = 0 as *mut sqlite3_stmt; + ret += "State: "; match (*msg).state { - 10 => p = dc_strdup(b"Fresh\x00" as *const u8 as *const libc::c_char), - 13 => p = dc_strdup(b"Noticed\x00" as *const u8 as *const libc::c_char), - 16 => p = dc_strdup(b"Seen\x00" as *const u8 as *const libc::c_char), - 26 => p = dc_strdup(b"Delivered\x00" as *const u8 as *const libc::c_char), - 24 => p = dc_strdup(b"Failed\x00" as *const u8 as *const libc::c_char), - 28 => p = dc_strdup(b"Read\x00" as *const u8 as *const libc::c_char), - 20 => p = dc_strdup(b"Pending\x00" as *const u8 as *const libc::c_char), - 18 => p = dc_strdup(b"Preparing\x00" as *const u8 as *const libc::c_char), - _ => p = dc_mprintf(b"%i\x00" as *const u8 as *const libc::c_char, (*msg).state), + 10 => ret += "Fresh", + 13 => ret += "Noticed", + 16 => ret += "Seen", + 26 => ret += "Delivered", + 24 => ret += "Failed", + 28 => ret += "Read", + 20 => ret += "Pending", + 18 => ret += "Preparing", + _ => ret += &format!("{}", (*msg).state), } - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b"State: %s\x00" as *const u8 as *const libc::c_char, - p, - ); - free(p as *mut libc::c_void); + if dc_msg_has_location(msg) { - dc_strbuilder_cat( - &mut ret, - b", Location sent\x00" as *const u8 as *const libc::c_char, - ); + ret += ", Location sent"; } p = 0 as *mut libc::c_char; - e2ee_errors = dc_param_get_int((*msg).param, 'e' as i32, 0i32); + e2ee_errors = dc_param_get_int((*msg).param, 'e' as i32, 0); if 0 != e2ee_errors { - if 0 != e2ee_errors & 0x2i32 { + if 0 != e2ee_errors & 0x2 { p = dc_strdup( b"Encrypted, no valid signature\x00" as *const u8 as *const libc::c_char, ) } - } else if 0 != dc_param_get_int((*msg).param, 'c' as i32, 0i32) { + } else if 0 != dc_param_get_int((*msg).param, 'c' as i32, 0) { p = dc_strdup(b"Encrypted\x00" as *const u8 as *const libc::c_char) } if !p.is_null() { - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b", %s\x00" as *const u8 as *const libc::c_char, - p, - ); + ret += &format!(", {}", as_str(p)); free(p as *mut libc::c_void); } - dc_strbuilder_cat(&mut ret, b"\n\x00" as *const u8 as *const libc::c_char); + ret += "\n"; p = dc_param_get((*msg).param, 'L' as i32, 0 as *const libc::c_char); if !p.is_null() { - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b"Error: %s\n\x00" as *const u8 as *const libc::c_char, - p, - ); + ret += &format!("Error: {}", as_str(p)); free(p as *mut libc::c_void); } p = dc_msg_get_file(msg); if !p.is_null() && 0 != *p.offset(0isize) as libc::c_int { - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b"\nFile: %s, %i bytes\n\x00" as *const u8 as *const libc::c_char, - p, + ret += &format!( + "\nFile: {}, {}, bytes\n", + as_str(p), dc_get_filebytes(context, p) as libc::c_int, ); } free(p as *mut libc::c_void); - if (*msg).type_0 != 10i32 { + if (*msg).type_0 != 10 { + ret += "Type: "; match (*msg).type_0 { - 40 => p = dc_strdup(b"Audio\x00" as *const u8 as *const libc::c_char), - 60 => p = dc_strdup(b"File\x00" as *const u8 as *const libc::c_char), - 21 => p = dc_strdup(b"GIF\x00" as *const u8 as *const libc::c_char), - 20 => p = dc_strdup(b"Image\x00" as *const u8 as *const libc::c_char), - 50 => p = dc_strdup(b"Video\x00" as *const u8 as *const libc::c_char), - 41 => p = dc_strdup(b"Voice\x00" as *const u8 as *const libc::c_char), - _ => { - p = dc_mprintf(b"%i\x00" as *const u8 as *const libc::c_char, (*msg).type_0) - } + 40 => ret += "Audio", + 60 => ret += "File", + 21 => ret += "GIF", + 20 => ret += "Image", + 50 => ret += "Video", + 41 => ret += "Voice", + _ => ret += &format!("{}", (*msg).type_0), } - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b"Type: %s\n\x00" as *const u8 as *const libc::c_char, - p, - ); - free(p as *mut libc::c_void); + ret += "\n"; p = dc_msg_get_filemime(msg); - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b"Mimetype: %s\n\x00" as *const u8 as *const libc::c_char, - p, - ); + ret += &format!("Mimetype: {}\n", as_str(p)); free(p as *mut libc::c_void); } - w = dc_param_get_int((*msg).param, 'w' as i32, 0i32); - h = dc_param_get_int((*msg).param, 'h' as i32, 0i32); - if w != 0i32 || h != 0i32 { - p = dc_mprintf( - b"Dimension: %i x %i\n\x00" as *const u8 as *const libc::c_char, - w, - h, - ); - dc_strbuilder_cat(&mut ret, p); - free(p as *mut libc::c_void); + w = dc_param_get_int((*msg).param, 'w' as i32, 0); + h = dc_param_get_int((*msg).param, 'h' as i32, 0); + if w != 0 || h != 0 { + ret += &format!("Dimension: {} x {}\n", w, h,); } - duration = dc_param_get_int((*msg).param, 'd' as i32, 0i32); - if duration != 0i32 { - p = dc_mprintf( - b"Duration: %i ms\n\x00" as *const u8 as *const libc::c_char, - duration, - ); - dc_strbuilder_cat(&mut ret, p); - free(p as *mut libc::c_void); + duration = dc_param_get_int((*msg).param, 'd' as i32, 0); + if duration != 0 { + ret += &format!("Duration: {} ms\n", duration,); } - if !rawtxt.is_null() && 0 != *rawtxt.offset(0isize) as libc::c_int { - dc_strbuilder_cat(&mut ret, b"\n\x00" as *const u8 as *const libc::c_char); - dc_strbuilder_cat(&mut ret, rawtxt); - dc_strbuilder_cat(&mut ret, b"\n\x00" as *const u8 as *const libc::c_char); + if !rawtxt.is_null() && 0 != *rawtxt.offset(0) as libc::c_int { + ret += &format!("\n{}\n", as_str(rawtxt)); } - if !(*msg).rfc724_mid.is_null() && 0 != *(*msg).rfc724_mid.offset(0isize) as libc::c_int - { - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b"\nMessage-ID: %s\x00" as *const u8 as *const libc::c_char, - (*msg).rfc724_mid, - ); + if !(*msg).rfc724_mid.is_null() && 0 != *(*msg).rfc724_mid.offset(0) as libc::c_int { + ret += &format!("\nMessage-ID: {}", (*msg).rfc724_mid as libc::c_int); } if !(*msg).server_folder.is_null() - && 0 != *(*msg).server_folder.offset(0isize) as libc::c_int + && 0 != *(*msg).server_folder.offset(0) as libc::c_int { - dc_strbuilder_catf( - &mut ret as *mut dc_strbuilder_t, - b"\nLast seen as: %s/%i\x00" as *const u8 as *const libc::c_char, - (*msg).server_folder, + ret += &format!( + "\nLast seen as: {}/{}", + to_string((*msg).server_folder), (*msg).server_uid as libc::c_int, ); } @@ -282,7 +216,7 @@ pub unsafe fn dc_get_msg_info(context: &Context, msg_id: uint32_t) -> *mut libc: dc_contact_unref(contact_from); free(rawtxt as *mut libc::c_void); - ret.buf + strdup(to_cstring(ret).as_ptr()) } pub unsafe fn dc_msg_new_untyped<'a>(context: &'a Context) -> *mut dc_msg_t<'a> { @@ -1620,12 +1554,7 @@ mod tests { &mut mime_0, ); assert_eq!(type_0, DC_MSG_AUDIO as libc::c_int); - assert_eq!( - CStr::from_ptr(mime_0 as *const libc::c_char) - .to_str() - .unwrap(), - "audio/mpeg" - ); + assert_eq!(as_str(mime_0 as *const libc::c_char), "audio/mpeg"); free(mime_0 as *mut libc::c_void); dc_msg_guess_msgtype_from_suffix( @@ -1634,12 +1563,7 @@ mod tests { &mut mime_0, ); assert_eq!(type_0, DC_MSG_AUDIO as libc::c_int); - assert_eq!( - CStr::from_ptr(mime_0 as *const libc::c_char) - .to_str() - .unwrap(), - "audio/aac" - ); + assert_eq!(as_str(mime_0 as *const libc::c_char), "audio/aac"); free(mime_0 as *mut libc::c_void); dc_msg_guess_msgtype_from_suffix( @@ -1648,12 +1572,7 @@ mod tests { &mut mime_0, ); assert_eq!(type_0, DC_MSG_VIDEO as libc::c_int); - assert_eq!( - CStr::from_ptr(mime_0 as *const libc::c_char) - .to_str() - .unwrap(), - "video/mp4" - ); + assert_eq!(as_str(mime_0 as *const libc::c_char), "video/mp4"); free(mime_0 as *mut libc::c_void); dc_msg_guess_msgtype_from_suffix( diff --git a/src/dc_receive_imf.rs b/src/dc_receive_imf.rs index 7c25f7211..42eb51091 100644 --- a/src/dc_receive_imf.rs +++ b/src/dc_receive_imf.rs @@ -22,7 +22,6 @@ use crate::dc_param::*; use crate::dc_securejoin::*; use crate::dc_sqlite3::*; use crate::dc_stock::*; -use crate::dc_strbuilder::*; use crate::dc_strencode::*; use crate::dc_tools::*; use crate::peerstate::*; @@ -1692,15 +1691,9 @@ unsafe fn create_adhoc_grp_id(context: &Context, member_ids: *mut dc_array_t) -> let stmt: *mut sqlite3_stmt; let q3: *mut libc::c_char; let mut addr: *mut libc::c_char; - let mut i: libc::c_int; let iCnt: libc::c_int; - let mut member_cs: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut member_cs, 0i32); + let mut member_cs = String::new(); + q3 = sqlite3_mprintf( b"SELECT addr FROM contacts WHERE id IN(%s) AND id!=1\x00" as *const u8 as *const libc::c_char, @@ -1722,35 +1715,26 @@ unsafe fn create_adhoc_grp_id(context: &Context, member_ids: *mut dc_array_t) -> } dc_array_sort_strings(member_addrs); iCnt = dc_array_get_cnt(member_addrs) as libc::c_int; - i = 0i32; - while i < iCnt { + + for i in 0..iCnt { if 0 != i { - dc_strbuilder_cat(&mut member_cs, b",\x00" as *const u8 as *const libc::c_char); + member_cs += ","; } - dc_strbuilder_cat( - &mut member_cs, - dc_array_get_ptr(member_addrs, i as size_t) as *const libc::c_char, - ); - i += 1 + member_cs += &to_string(dc_array_get_ptr(member_addrs, i as size_t) as *const libc::c_char); } - let ret = hex_hash(member_cs.buf as *const _, strlen(member_cs.buf)); + let ret = hex_hash(&member_cs); dc_array_free_ptr(member_addrs); dc_array_unref(member_addrs); free(member_ids_str as *mut libc::c_void); sqlite3_finalize(stmt); sqlite3_free(q3 as *mut libc::c_void); - free(member_cs.buf as *mut libc::c_void); ret as *mut _ } -fn hex_hash(bytes_ptr: *const u8, bytes_len: libc::size_t) -> *const libc::c_char { - if bytes_ptr.is_null() || bytes_len == 0 { - return std::ptr::null(); - } - - let bytes = unsafe { std::slice::from_raw_parts(bytes_ptr, bytes_len) }; +fn hex_hash(s: impl AsRef) -> *const libc::c_char { + let bytes = s.as_ref().as_bytes(); let result = Sha256::digest(bytes); let result_hex = hex::encode(&result[..8]); let result_cstring = to_cstring(result_hex); @@ -2302,9 +2286,9 @@ mod tests { #[test] fn test_hex_hash() { - let data = b"hello world\x00" as *const u8 as *const libc::c_char; + let data = "hello world"; - let res_c = hex_hash(data as *const _, unsafe { strlen(data) }); + let res_c = hex_hash(data); let res = to_string(res_c); assert_eq!(res, "b94d27b9934d3e08"); } diff --git a/src/dc_simplify.rs b/src/dc_simplify.rs index 9ecc2329e..dcf3aecd2 100644 --- a/src/dc_simplify.rs +++ b/src/dc_simplify.rs @@ -1,5 +1,4 @@ use crate::dc_dehtml::*; -use crate::dc_strbuilder::*; use crate::dc_tools::*; use crate::types::*; use crate::x::*; @@ -204,15 +203,9 @@ unsafe fn dc_simplify_simplify_plain_text( } } /* re-create buffer from the remaining lines */ - let mut ret: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut ret, strlen(buf_terminated) as libc::c_int); + let mut ret = String::new(); if 0 != (*simplify).is_cut_at_begin { - dc_strbuilder_cat(&mut ret, b"[...] \x00" as *const u8 as *const libc::c_char); + ret += "[...]"; } /* we write empty lines only in case and non-empty line follows */ let mut pending_linebreaks: libc::c_int = 0i32; @@ -228,11 +221,11 @@ unsafe fn dc_simplify_simplify_plain_text( pending_linebreaks = 2i32 } while 0 != pending_linebreaks { - dc_strbuilder_cat(&mut ret, b"\n\x00" as *const u8 as *const libc::c_char); + ret += "\n"; pending_linebreaks -= 1 } } - dc_strbuilder_cat(&mut ret, line); + ret += &to_string(line); content_lines_added += 1; pending_linebreaks = 1i32 } @@ -241,11 +234,11 @@ unsafe fn dc_simplify_simplify_plain_text( if 0 != (*simplify).is_cut_at_end && (0 == (*simplify).is_cut_at_begin || 0 != content_lines_added) { - dc_strbuilder_cat(&mut ret, b" [...]\x00" as *const u8 as *const libc::c_char); + ret += " [...]"; } dc_free_splitted_lines(lines); - ret.buf + strdup(to_cstring(ret).as_ptr()) } /** diff --git a/src/dc_strbuilder.rs b/src/dc_strbuilder.rs deleted file mode 100644 index 9881cd371..000000000 --- a/src/dc_strbuilder.rs +++ /dev/null @@ -1,62 +0,0 @@ -use crate::x::*; - -#[derive(Copy, Clone)] -#[repr(C)] -pub struct dc_strbuilder_t { - pub buf: *mut libc::c_char, - pub allocated: libc::c_int, - pub free: libc::c_int, - pub eos: *mut libc::c_char, -} - -pub unsafe fn dc_strbuilder_init(mut strbuilder: *mut dc_strbuilder_t, init_bytes: libc::c_int) { - if strbuilder.is_null() { - return; - } - (*strbuilder).allocated = if init_bytes > 128i32 { - init_bytes - } else { - 128i32 - }; - (*strbuilder).buf = malloc((*strbuilder).allocated as usize) as *mut libc::c_char; - assert!(!(*strbuilder).buf.is_null()); - *(*strbuilder).buf.offset(0isize) = 0i32 as libc::c_char; - (*strbuilder).free = (*strbuilder).allocated - 1i32; - (*strbuilder).eos = (*strbuilder).buf; -} -pub unsafe fn dc_strbuilder_cat( - mut strbuilder: *mut dc_strbuilder_t, - text: *const libc::c_char, -) -> *mut libc::c_char { - if strbuilder.is_null() || text.is_null() { - return 0 as *mut libc::c_char; - } - let len: libc::c_int = strlen(text) as libc::c_int; - if len > (*strbuilder).free { - let add_bytes: libc::c_int = if len > (*strbuilder).allocated { - len - } else { - (*strbuilder).allocated - }; - let old_offset: libc::c_int = - (*strbuilder).eos.wrapping_offset_from((*strbuilder).buf) as libc::c_int; - (*strbuilder).allocated = (*strbuilder).allocated + add_bytes; - (*strbuilder).buf = realloc( - (*strbuilder).buf as *mut libc::c_void, - ((*strbuilder).allocated + add_bytes) as usize, - ) as *mut libc::c_char; - assert!(!(*strbuilder).buf.is_null()); - (*strbuilder).free = (*strbuilder).free + add_bytes; - (*strbuilder).eos = (*strbuilder).buf.offset(old_offset as isize) - } - let ret: *mut libc::c_char = (*strbuilder).eos; - strcpy((*strbuilder).eos, text); - (*strbuilder).eos = (*strbuilder).eos.offset(len as isize); - (*strbuilder).free -= len; - return ret; -} -pub unsafe fn dc_strbuilder_empty(mut strbuilder: *mut dc_strbuilder_t) { - *(*strbuilder).buf.offset(0isize) = 0i32 as libc::c_char; - (*strbuilder).free = (*strbuilder).allocated - 1i32; - (*strbuilder).eos = (*strbuilder).buf; -} diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 3ca2c18e1..48fb75aa3 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -8,7 +8,6 @@ use rand::{thread_rng, Rng}; use crate::context::Context; use crate::dc_array::*; use crate::dc_log::*; -use crate::dc_strbuilder::*; use crate::types::*; use crate::x::*; @@ -510,26 +509,22 @@ pub unsafe fn dc_str_from_clist( list: *const clist, delimiter: *const libc::c_char, ) -> *mut libc::c_char { - let mut str: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut str, 256i32); + let mut res = String::new(); + if !list.is_null() { let mut cur: *mut clistiter = (*list).first; while !cur.is_null() { - let rfc724_mid: *const libc::c_char = (if !cur.is_null() { + let rfc724_mid = (if !cur.is_null() { (*cur).data } else { 0 as *mut libc::c_void }) as *const libc::c_char; + if !rfc724_mid.is_null() { - if 0 != *str.buf.offset(0isize) as libc::c_int && !delimiter.is_null() { - dc_strbuilder_cat(&mut str, delimiter); + if !res.is_empty() && !delimiter.is_null() { + res += as_str(delimiter); } - dc_strbuilder_cat(&mut str, rfc724_mid); + res += as_str(rfc724_mid); } cur = if !cur.is_null() { (*cur).next @@ -539,7 +534,7 @@ pub unsafe fn dc_str_from_clist( } } - str.buf + strdup(to_cstring(res).as_ptr()) } pub unsafe fn dc_str_to_clist( @@ -1123,11 +1118,7 @@ pub unsafe fn dc_file_exist(context: &Context, pathNfilename: *const libc::c_cha } let exist = { - let p = std::path::Path::new( - std::ffi::CStr::from_ptr(pathNfilename_abs) - .to_str() - .unwrap(), - ); + let p = std::path::Path::new(as_str(pathNfilename_abs)); p.exists() }; @@ -1648,6 +1639,11 @@ mod tests { } } + #[test] + fn test_rust_ftoa() { + assert_eq!("1.22", format!("{}", 1.22)); + } + #[test] fn test_dc_str_replace() { unsafe { diff --git a/src/lib.rs b/src/lib.rs index 06e2bd3d3..2d520873b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,7 +57,6 @@ pub mod dc_securejoin; pub mod dc_simplify; pub mod dc_sqlite3; pub mod dc_stock; -pub mod dc_strbuilder; pub mod dc_strencode; pub mod dc_token; pub mod dc_tools; diff --git a/src/x.rs b/src/x.rs index 0f619b652..9a0f188ee 100644 --- a/src/x.rs +++ b/src/x.rs @@ -1,4 +1,3 @@ -use crate::dc_strbuilder::dc_strbuilder_t; use crate::types::*; pub use libc::{ @@ -54,8 +53,6 @@ extern "C" { ) -> libc::c_int; // -- DC Methods - - pub fn dc_strbuilder_catf(_: *mut dc_strbuilder_t, format: *const libc::c_char, _: ...); pub fn dc_mprintf(format: *const libc::c_char, _: ...) -> *mut libc::c_char; } diff --git a/tests/stress.rs b/tests/stress.rs index 850b49e46..cb245e21e 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -17,7 +17,6 @@ use deltachat::dc_mimeparser::*; use deltachat::dc_qr::*; use deltachat::dc_saxparser::*; use deltachat::dc_securejoin::*; -use deltachat::dc_strbuilder::*; use deltachat::dc_tools::*; use deltachat::key::*; use deltachat::keyring::*; @@ -245,141 +244,45 @@ unsafe fn stress_functions(context: &Context) { free(fn0 as *mut libc::c_void); free(fn1 as *mut libc::c_void); } - let mut keys: *mut libc::c_char = dc_get_config( + let keys = dc_get_config( context, b"sys.config_keys\x00" as *const u8 as *const libc::c_char, ); assert!(!keys.is_null()); assert_ne!(0, *keys.offset(0isize) as libc::c_int); - let mut sb: dc_strbuilder_t = dc_strbuilder_t { - buf: 0 as *mut libc::c_char, - allocated: 0, - free: 0, - eos: 0 as *mut libc::c_char, - }; - dc_strbuilder_init(&mut sb, 200i32); - dc_strbuilder_catf( - &mut sb as *mut dc_strbuilder_t, - b" %s \x00" as *const u8 as *const libc::c_char, - keys, - ); - free(keys as *mut libc::c_void); - keys = sb.buf; - assert!(strstr( - keys, - b" probably_never_a_key \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr(keys, b" addr \x00" as *const u8 as *const libc::c_char).is_null()); - assert!(!strstr( - keys, - b" mail_server \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr(keys, b" mail_user \x00" as *const u8 as *const libc::c_char).is_null()); - assert!(!strstr(keys, b" mail_pw \x00" as *const u8 as *const libc::c_char).is_null()); - assert!(!strstr(keys, b" mail_port \x00" as *const u8 as *const libc::c_char).is_null()); - assert!(!strstr( - keys, - b" send_server \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr(keys, b" send_user \x00" as *const u8 as *const libc::c_char).is_null()); - assert!(!strstr(keys, b" send_pw \x00" as *const u8 as *const libc::c_char).is_null()); - assert!(!strstr(keys, b" send_port \x00" as *const u8 as *const libc::c_char).is_null()); - assert!(!strstr( - keys, - b" server_flags \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" imap_folder \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" displayname \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" selfstatus \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" selfavatar \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" e2ee_enabled \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" mdns_enabled \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" save_mime_headers \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_addr \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_mail_server \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_mail_user \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_mail_pw \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_mail_port \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_send_server \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_send_user \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_send_pw \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_send_port \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); - assert!(!strstr( - keys, - b" configured_server_flags \x00" as *const u8 as *const libc::c_char, - ) - .is_null()); + let res = format!(" {} ", as_str(keys)); free(keys as *mut libc::c_void); + assert!(!res.contains(" probably_never_a_key ")); + assert!(res.contains(" addr ")); + assert!(res.contains(" mail_server ")); + assert!(res.contains(" mail_user ")); + assert!(res.contains(" mail_pw ")); + assert!(res.contains(" mail_port ")); + assert!(res.contains(" send_server ")); + assert!(res.contains(" send_user ")); + assert!(res.contains(" send_pw ")); + assert!(res.contains(" send_port ")); + assert!(res.contains(" server_flags ")); + assert!(res.contains(" imap_folder ")); + assert!(res.contains(" displayname ")); + assert!(res.contains(" selfstatus ")); + assert!(res.contains(" selfavatar ")); + assert!(res.contains(" e2ee_enabled ")); + assert!(res.contains(" mdns_enabled ")); + assert!(res.contains(" save_mime_headers ")); + assert!(res.contains(" configured_addr ")); + assert!(res.contains(" configured_mail_server ")); + assert!(res.contains(" configured_mail_user ")); + assert!(res.contains(" configured_mail_pw ")); + assert!(res.contains(" configured_mail_port ")); + assert!(res.contains(" configured_send_server ")); + assert!(res.contains(" configured_send_user ")); + assert!(res.contains(" configured_send_pw ")); + assert!(res.contains(" configured_send_port ")); + assert!(res.contains(" configured_server_flags ")); + let mut ok: libc::c_int; let mut buf_0: *mut libc::c_char; let mut headerline: *const libc::c_char = 0 as *const libc::c_char; @@ -408,12 +311,7 @@ unsafe fn stress_functions(context: &Context) { ); assert!(!base64.is_null()); - assert_eq!( - CStr::from_ptr(base64 as *const libc::c_char) - .to_str() - .unwrap(), - "data", - ); + assert_eq!(as_str(base64 as *const libc::c_char), "data",); free(buf_0 as *mut libc::c_void); @@ -439,12 +337,7 @@ unsafe fn stress_functions(context: &Context) { ); assert!(!base64.is_null()); - assert_eq!( - CStr::from_ptr(base64 as *const libc::c_char) - .to_str() - .unwrap(), - "dat1", - ); + assert_eq!(as_str(base64 as *const libc::c_char), "dat1",); free(buf_0 as *mut libc::c_void); @@ -472,12 +365,7 @@ unsafe fn stress_functions(context: &Context) { assert!(setupcodebegin.is_null()); assert!(!base64.is_null()); - assert_eq!( - CStr::from_ptr(base64 as *const libc::c_char) - .to_str() - .unwrap(), - "base64-123", - ); + assert_eq!(as_str(base64 as *const libc::c_char), "base64-123",); free(buf_0 as *mut libc::c_void); @@ -522,12 +410,7 @@ unsafe fn stress_functions(context: &Context) { ); assert!(!base64.is_null()); - assert_eq!( - CStr::from_ptr(base64 as *const libc::c_char) - .to_str() - .unwrap(), - "base64-567 \n abc", - ); + assert_eq!(as_str(base64 as *const libc::c_char), "base64-567 \n abc",); free(buf_0 as *mut libc::c_void); @@ -561,12 +444,7 @@ unsafe fn stress_functions(context: &Context) { ); assert!(!base64.is_null()); - assert_eq!( - CStr::from_ptr(base64 as *const libc::c_char) - .to_str() - .unwrap(), - "base64", - ); + assert_eq!(as_str(base64 as *const libc::c_char), "base64",); free(buf_0 as *mut libc::c_void); @@ -853,10 +731,7 @@ fn test_encryption_decryption() { ) .unwrap(); - assert_eq!( - std::str::from_utf8(&plain).unwrap(), - CStr::from_ptr(original_text).to_str().unwrap() - ); + assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),); assert_eq!(valid_signatures.len(), 1); valid_signatures.clear(); @@ -870,10 +745,7 @@ fn test_encryption_decryption() { Some(&mut valid_signatures), ) .unwrap(); - assert_eq!( - std::str::from_utf8(&plain).unwrap(), - CStr::from_ptr(original_text).to_str().unwrap() - ); + assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),); assert_eq!(valid_signatures.len(), 0); valid_signatures.clear(); @@ -886,10 +758,7 @@ fn test_encryption_decryption() { Some(&mut valid_signatures), ) .unwrap(); - assert_eq!( - std::str::from_utf8(&plain).unwrap(), - CStr::from_ptr(original_text).to_str().unwrap() - ); + assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),); assert_eq!(valid_signatures.len(), 0); valid_signatures.clear(); @@ -904,10 +773,7 @@ fn test_encryption_decryption() { Some(&mut valid_signatures), ) .unwrap(); - assert_eq!( - std::str::from_utf8(&plain).unwrap(), - CStr::from_ptr(original_text).to_str().unwrap() - ); + assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),); assert_eq!(valid_signatures.len(), 1); valid_signatures.clear(); @@ -920,10 +786,7 @@ fn test_encryption_decryption() { Some(&mut valid_signatures), ) .unwrap(); - assert_eq!( - std::str::from_utf8(&plain).unwrap(), - CStr::from_ptr(original_text).to_str().unwrap() - ); + assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),); valid_signatures.clear(); @@ -940,10 +803,7 @@ fn test_encryption_decryption() { None, ) .unwrap(); - assert_eq!( - std::str::from_utf8(&plain).unwrap(), - CStr::from_ptr(original_text).to_str().unwrap() - ); + assert_eq!(std::str::from_utf8(&plain).unwrap(), as_str(original_text),); } } @@ -970,9 +830,7 @@ unsafe fn create_test_context() -> TestContext { dc_open(&mut ctx, dbfile.as_ptr(), std::ptr::null()), 1, "Failed to open {}", - CStr::from_ptr(dbfile.as_ptr() as *const libc::c_char) - .to_str() - .unwrap() + as_str(dbfile.as_ptr() as *const libc::c_char) ); TestContext { ctx: ctx, dir: dir } @@ -991,9 +849,7 @@ fn test_dc_kml_parse() { assert!(!(*kml).addr.is_null()); assert_eq!( - CStr::from_ptr((*kml).addr as *const libc::c_char) - .to_str() - .unwrap(), + as_str((*kml).addr as *const libc::c_char), "user@example.org", ); @@ -1032,9 +888,7 @@ fn test_dc_mimeparser_with_context() { dc_mimeparser_parse(&mut mimeparser, raw, strlen(raw)); assert_eq!( - CStr::from_ptr(mimeparser.subject as *const libc::c_char) - .to_str() - .unwrap(), + as_str(mimeparser.subject as *const libc::c_char), "inner-subject", ); @@ -1042,34 +896,19 @@ fn test_dc_mimeparser_with_context() { &mimeparser, b"X-Special-A\x00" as *const u8 as *const libc::c_char, ); - assert_eq!( - CStr::from_ptr((*of).fld_value as *const libc::c_char) - .to_str() - .unwrap(), - "special-a", - ); + assert_eq!(as_str((*of).fld_value as *const libc::c_char), "special-a",); of = dc_mimeparser_lookup_optional_field( &mimeparser, b"Foo\x00" as *const u8 as *const libc::c_char, ); - assert_eq!( - CStr::from_ptr((*of).fld_value as *const libc::c_char) - .to_str() - .unwrap(), - "Bar", - ); + assert_eq!(as_str((*of).fld_value as *const libc::c_char), "Bar",); of = dc_mimeparser_lookup_optional_field( &mimeparser, b"Chat-Version\x00" as *const u8 as *const libc::c_char, ); - assert_eq!( - CStr::from_ptr((*of).fld_value as *const libc::c_char) - .to_str() - .unwrap(), - "1.0", - ); + assert_eq!(as_str((*of).fld_value as *const libc::c_char), "1.0",); assert_eq!(carray_count(mimeparser.parts), 1); dc_mimeparser_unref(&mut mimeparser);