diff --git a/src/dc_saxparser.rs b/src/dc_saxparser.rs index e8aa7569b..f9a1840cb 100644 --- a/src/dc_saxparser.rs +++ b/src/dc_saxparser.rs @@ -205,7 +205,7 @@ pub unsafe fn dc_saxparser_parse( if p != beg_tag_name_0 { let mut after_tag_name: *mut libc::c_char = p; let mut attr_index: libc::c_int = 0i32; - while 0 != isspace(*p as libc::c_int) { + while 0 != libc::isspace(*p as libc::c_int) { p = p.offset(1isize) } while 0 != *p as libc::c_int @@ -301,7 +301,7 @@ pub unsafe fn dc_saxparser_parse( attr_index += 2i32 } } - while 0 != isspace(*p as libc::c_int) { + while 0 != libc::isspace(*p as libc::c_int) { p = p.offset(1isize) } } @@ -444,7 +444,7 @@ unsafe fn xml_decode(mut s: *mut libc::c_char, mut type_0: libc::c_char) -> *mut loop { while 0 != *s as libc::c_int && *s as libc::c_int != '&' as i32 - && 0 == isspace(*s as libc::c_int) + && 0 == libc::isspace(*s as libc::c_int) { s = s.offset(1isize) } @@ -539,7 +539,7 @@ unsafe fn xml_decode(mut s: *mut libc::c_char, mut type_0: libc::c_char) -> *mut } else { s = s.offset(1isize) } - } else if type_0 as libc::c_int == ' ' as i32 && 0 != isspace(*s as libc::c_int) { + } else if type_0 as libc::c_int == ' ' as i32 && 0 != libc::isspace(*s as libc::c_int) { let fresh6 = s; s = s.offset(1); *fresh6 = ' ' as i32 as libc::c_char diff --git a/src/dc_strencode.rs b/src/dc_strencode.rs index 2bee4ebc8..d1edf2e9f 100644 --- a/src/dc_strencode.rs +++ b/src/dc_strencode.rs @@ -22,14 +22,6 @@ pub fn isalnum(mut _c: libc::c_int) -> libc::c_int { } } -#[cfg(test)] -#[test] -fn test_isalnum() { - assert_eq!(isalnum(0), 0); - assert_eq!(isalnum('5' as libc::c_int), 1); - assert_eq!(isalnum('Q' as libc::c_int), 1); -} - #[inline] pub fn isdigit(mut _c: libc::c_int) -> libc::c_int { if _c < std::u8::MAX as libc::c_int { @@ -715,3 +707,15 @@ pub unsafe fn dc_decode_ext_header(mut to_decode: *const libc::c_char) -> *mut l dc_strdup(to_decode) }; } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_isalnum() { + assert_eq!(isalnum(0), 0); + assert_eq!(isalnum('5' as libc::c_int), 1); + assert_eq!(isalnum('Q' as libc::c_int), 1); + } +} diff --git a/src/dc_tools.rs b/src/dc_tools.rs index 8597e7c0d..60729efd7 100644 --- a/src/dc_tools.rs +++ b/src/dc_tools.rs @@ -110,6 +110,7 @@ pub unsafe fn dc_str_replace( } return replacements; } + pub unsafe fn dc_ftoa(mut f: libc::c_double) -> *mut libc::c_char { // hack around printf(%f) that may return `,` as decimal point on mac let mut test: *mut libc::c_char = @@ -124,13 +125,14 @@ pub unsafe fn dc_ftoa(mut f: libc::c_double) -> *mut libc::c_char { free(test as *mut libc::c_void); return str; } + pub unsafe fn dc_ltrim(mut buf: *mut libc::c_char) { let mut len: size_t = 0i32 as size_t; let mut cur: *const libc::c_uchar = 0 as *const libc::c_uchar; if !buf.is_null() && 0 != *buf as libc::c_int { len = strlen(buf); cur = buf as *const libc::c_uchar; - while 0 != *cur as libc::c_int && 0 != isspace(*cur as libc::c_int) { + while 0 != *cur as libc::c_int && 0 != libc::isspace(*cur as libc::c_int) { cur = cur.offset(1isize); len = len.wrapping_sub(1) } @@ -143,6 +145,7 @@ pub unsafe fn dc_ltrim(mut buf: *mut libc::c_char) { } }; } + pub unsafe fn dc_rtrim(mut buf: *mut libc::c_char) { let mut len: size_t = 0i32 as size_t; let mut cur: *mut libc::c_uchar = 0 as *mut libc::c_uchar; @@ -151,12 +154,12 @@ pub unsafe fn dc_rtrim(mut buf: *mut libc::c_char) { cur = (buf as *mut libc::c_uchar) .offset(len as isize) .offset(-1isize); - while cur != buf as *mut libc::c_uchar && 0 != isspace(*cur as libc::c_int) { + while cur != buf as *mut libc::c_uchar && 0 != libc::isspace(*cur as libc::c_int) { cur = cur.offset(-1isize); len = len.wrapping_sub(1) } *cur.offset( - (if 0 != isspace(*cur as libc::c_int) { + (if 0 != libc::isspace(*cur as libc::c_int) { 0i32 } else { 1i32 @@ -164,10 +167,12 @@ pub unsafe fn dc_rtrim(mut buf: *mut libc::c_char) { ) = '\u{0}' as i32 as libc::c_uchar }; } + pub unsafe fn dc_trim(mut buf: *mut libc::c_char) { dc_ltrim(buf); dc_rtrim(buf); } + /* the result must be free()'d */ pub unsafe fn dc_strlower(mut in_0: *const libc::c_char) -> *mut libc::c_char { let mut out: *mut libc::c_char = dc_strdup(in_0); @@ -1652,3 +1657,60 @@ pub unsafe fn dc_make_rel_and_copy( free(filename as *mut libc::c_void); return success; } + +#[cfg(test)] +mod tests { + use super::*; + use std::ffi::CStr; + + #[test] + fn test_dc_ltrim() { + unsafe { + let mut html: *const libc::c_char = + b"\r\r\nline1
\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char; + let mut out: *mut libc::c_char = 0 as *mut libc::c_char; + out = strndup(html, strlen(html) as libc::c_ulong); + + dc_ltrim(out); + + assert_eq!( + CStr::from_ptr(out as *const libc::c_char).to_str().unwrap(), + "line1
\r\n\r\n\r\rline2\n\r" + ); + } + } + + #[test] + fn test_dc_rtrim() { + unsafe { + let mut html: *const libc::c_char = + b"\r\r\nline1
\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char; + let mut out: *mut libc::c_char = 0 as *mut libc::c_char; + out = strndup(html, strlen(html) as libc::c_ulong); + + dc_rtrim(out); + + assert_eq!( + CStr::from_ptr(out as *const libc::c_char).to_str().unwrap(), + "\r\r\nline1
\r\n\r\n\r\rline2" + ); + } + } + + #[test] + fn test_dc_trim() { + unsafe { + let mut html: *const libc::c_char = + b"\r\r\nline1
\r\n\r\n\r\rline2\n\r\x00" as *const u8 as *const libc::c_char; + let mut out: *mut libc::c_char = 0 as *mut libc::c_char; + out = strndup(html, strlen(html) as libc::c_ulong); + + dc_trim(out); + + assert_eq!( + CStr::from_ptr(out as *const libc::c_char).to_str().unwrap(), + "line1
\r\n\r\n\r\rline2" + ); + } + } +} diff --git a/src/types.rs b/src/types.rs index a4b8b08f2..2b5b139f5 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1205,15 +1205,6 @@ pub unsafe fn isascii(mut _c: libc::c_int) -> libc::c_int { return (_c & !0x7fi32 == 0i32) as libc::c_int; } -#[inline] -pub unsafe fn isspace(mut _c: libc::c_int) -> libc::c_int { - if _c < std::u8::MAX as libc::c_int { - ((_c as u8 as char) == ' ') as libc::c_int - } else { - 0 - } -} - #[inline] pub unsafe fn tolower(mut _c: libc::c_int) -> libc::c_int { return __tolower(_c); diff --git a/tests/stress.rs b/tests/stress.rs index 0c1cda2f8..5e3af825a 100644 --- a/tests/stress.rs +++ b/tests/stress.rs @@ -63,14 +63,14 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { let mut plain: *mut libc::c_char = dc_simplify_simplify(simplify, html, strlen(html) as libc::c_int, 1i32, 0i32); - // FIXME - // assert_eq!( - // strcmp(plain, b"line1\nline2\x00" as *const u8 as *const libc::c_char), - // 0, - // "{:?}", - // std::ffi::CStr::from_ptr(plain), - // ); - // free(plain as *mut libc::c_void); + assert_eq!( + CStr::from_ptr(plain as *const libc::c_char) + .to_str() + .unwrap(), + "line1\nline2", + ); + free(plain as *mut libc::c_void); + html = b"text(b"stress_functions\x00")) - // .as_ptr(), - // b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char, - // 706i32, - // b"strcmp(p1->packed, \"a=foo\\nd=4\")==0\x00" as *const u8 as *const libc::c_char, - // ); - // } else { - // }; + + assert_eq!( + CStr::from_ptr((*p1).packed as *const libc::c_char) + .to_str() + .unwrap(), + "a=foo\nd=4", + ); + dc_param_set(p1, 'a' as i32, 0 as *const libc::c_char); dc_param_set(p1, 'd' as i32, 0 as *const libc::c_char); - // FIXME - // if 0 != !(strcmp((*p1).packed, b"\x00" as *const u8 as *const libc::c_char) == 0i32) - // as libc::c_int as libc::c_long - // { - // __assert_rtn( - // (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) - // .as_ptr(), - // b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char, - // 710i32, - // b"strcmp(p1->packed, \"\")==0\x00" as *const u8 as *const libc::c_char, - // ); - // } else { - // }; + assert_eq!( + CStr::from_ptr((*p1).packed as *const libc::c_char) + .to_str() + .unwrap(), + "", + ); + dc_param_unref(p1); + let mut keys: *mut libc::c_char = dc_get_config( context, b"sys.config_keys\x00" as *const u8 as *const libc::c_char, @@ -3457,21 +3445,17 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { ); } else { }; - // FIXME - // if 0 != !(!base64.is_null() - // && strcmp(base64, b"data\x00" as *const u8 as *const libc::c_char) == 0i32) - // as libc::c_int as libc::c_long - // { - // __assert_rtn( - // (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) - // .as_ptr(), - // b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char, - // 823i32, - // b"base64 && strcmp(base64, \"data\") == 0\x00" as *const u8 as *const libc::c_char, - // ); - // } else { - // }; + + assert!(!base64.is_null()); + assert_eq!( + CStr::from_ptr(base64 as *const libc::c_char) + .to_str() + .unwrap(), + "data", + ); + free(buf_0 as *mut libc::c_void); + buf_0 = strdup(b"-----BEGIN PGP MESSAGE-----\n\ndat1\n-----END PGP MESSAGE-----\n-----BEGIN PGP MESSAGE-----\n\ndat2\n-----END PGP MESSAGE-----\x00" as *const u8 as *const libc::c_char); @@ -3509,21 +3493,16 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; - // FIXME - // if 0 != !(!base64.is_null() - // && strcmp(base64, b"dat1\x00" as *const u8 as *const libc::c_char) == 0i32) - // as libc::c_int as libc::c_long - // { - // __assert_rtn( - // (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) - // .as_ptr(), - // b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char, - // 830i32, - // b"base64 && strcmp(base64, \"dat1\") == 0\x00" as *const u8 as *const libc::c_char, - // ); - // } else { - // }; + assert!(!base64.is_null()); + assert_eq!( + CStr::from_ptr(base64 as *const libc::c_char) + .to_str() + .unwrap(), + "dat1", + ); + free(buf_0 as *mut libc::c_void); + buf_0 = strdup( b"foo \n -----BEGIN PGP MESSAGE----- \n base64-123 \n -----END PGP MESSAGE-----\x00" as *const u8 as *const libc::c_char, @@ -3572,23 +3551,16 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; - // FIXME - // if 0 != !(!base64.is_null() - // && strcmp( - // base64, - // b"base64-123\x00" as *const u8 as *const libc::c_char, - // ) == 0i32) as libc::c_int as libc::c_long - // { - // __assert_rtn( - // (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) - // .as_ptr(), - // b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char, - // 838i32, - // b"base64 && strcmp(base64, \"base64-123\")==0\x00" as *const u8 as *const libc::c_char, - // ); - // } else { - // }; + assert!(!base64.is_null()); + assert_eq!( + CStr::from_ptr(base64 as *const libc::c_char) + .to_str() + .unwrap(), + "base64-123", + ); + free(buf_0 as *mut libc::c_void); + buf_0 = strdup(b"foo-----BEGIN PGP MESSAGE-----\x00" as *const u8 as *const libc::c_char); ok = dc_split_armored_data( buf_0, @@ -3661,24 +3633,16 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; - // FIXME - // if 0 != !(!base64.is_null() - // && strcmp( - // base64, - // b"base64-567 \n abc\x00" as *const u8 as *const libc::c_char, - // ) == 0i32) as libc::c_int as libc::c_long - // { - // __assert_rtn( - // (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) - // .as_ptr(), - // b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char, - // 851i32, - // b"base64 && strcmp(base64, \"base64-567 \\n abc\")==0\x00" as *const u8 - // as *const libc::c_char, - // ); - // } else { - // }; + assert!(!base64.is_null()); + assert_eq!( + CStr::from_ptr(base64 as *const libc::c_char) + .to_str() + .unwrap(), + "base64-567 \n abc", + ); + free(buf_0 as *mut libc::c_void); + buf_0 = strdup(b"-----BEGIN PGP PRIVATE KEY BLOCK-----\n Autocrypt-Prefer-Encrypt : mutual \n\nbase64\n-----END PGP PRIVATE KEY BLOCK-----\x00" as *const u8 as *const libc::c_char); @@ -3732,21 +3696,16 @@ unsafe extern "C" fn stress_functions(context: &dc_context_t) { } else { }; - // FIXME - // if 0 != !(!base64.is_null() - // && strcmp(base64, b"base64\x00" as *const u8 as *const libc::c_char) == 0i32) - // as libc::c_int as libc::c_long - // { - // __assert_rtn( - // (*::std::mem::transmute::<&[u8; 17], &[libc::c_char; 17]>(b"stress_functions\x00")) - // .as_ptr(), - // b"../cmdline/stress.c\x00" as *const u8 as *const libc::c_char, - // 859i32, - // b"base64 && strcmp(base64, \"base64\")==0\x00" as *const u8 as *const libc::c_char, - // ); - // } else { - // }; + assert!(!base64.is_null()); + assert_eq!( + CStr::from_ptr(base64 as *const libc::c_char) + .to_str() + .unwrap(), + "base64", + ); + free(buf_0 as *mut libc::c_void); + let mut norm: *mut libc::c_char = dc_normalize_setup_code( context, b"123422343234423452346234723482349234\x00" as *const u8 as *const libc::c_char,