Implement safe version of quote_word

This commit is contained in:
Dmitry Bogatov
2019-09-22 21:34:41 +00:00
committed by holger krekel
parent e17c671b7c
commit e523ebe3c1

View File

@@ -1,3 +1,4 @@
use phf::phf_set;
use std::borrow::Cow; use std::borrow::Cow;
use std::ffi::CString; use std::ffi::CString;
use std::ptr; use std::ptr;
@@ -95,42 +96,29 @@ pub unsafe fn dc_encode_header_words(to_encode_r: impl AsRef<str>) -> String {
s s
} }
unsafe fn quote_word(dest: &mut String, word: *const libc::c_char, size: libc::size_t) { unsafe fn quote_word(dest: &mut String, word: *const libc::c_char, size: usize) {
let mut cur: *const libc::c_char; let slice = std::slice::from_raw_parts(word.cast(), size);
let mut i = 0; dest.push_str(&quote_word_safe(slice))
let mut hex: [libc::c_char; 4] = [0; 4]; }
// let mut col: libc::c_int = 0i32;
dest.push_str("=?utf-8?Q?");
// col = (*dest).len as libc::c_int; fn quote_word_safe(word: &[u8]) -> String {
cur = word; static ENCODED: phf::Set<u8> = phf_set! {
while i < size { b',' , b':' , b'!' , b'"' , b'#' , b'$' , b'@' , b'[' , b'\\' , b']',
let mut do_quote_char = false; b'^' , b'`' , b'{' , b'|', b'}' , b'~' , b'=' , b'?' , b'_' ,
match *cur as u8 as char { };
',' | ':' | '!' | '"' | '#' | '$' | '@' | '[' | '\\' | ']' | '^' | '`' | '{' | '|' let mut result: String = "=?utf-8?Q?".into();
| '}' | '~' | '=' | '?' | '_' => do_quote_char = true, for byte in word {
_ => { let byte = *byte;
if *cur as u8 >= 128 { if byte >= 128 || ENCODED.contains(&byte) {
do_quote_char = true; result.push_str(&format!("={:2X}", byte))
} } else if byte == b' ' {
} result.push('_');
}
if do_quote_char {
print_hex(hex.as_mut_ptr(), cur);
dest.push_str(as_str(hex.as_ptr()));
// col += 3i32
} else { } else {
if *cur as libc::c_int == ' ' as i32 { result.push(byte as _);
dest.push('_');
} else {
dest.push(*cur as u8 as char);
}
// col += 3i32
} }
cur = cur.offset(1isize);
i = i.wrapping_add(1)
} }
dest.push_str("?="); result.push_str("?=");
result
} }
unsafe fn get_word( unsafe fn get_word(