mirror of
https://github.com/chatmail/core.git
synced 2026-05-03 13:26:28 +03:00
Use rust strings instead of MMapString in src/dc_strencode.rs
Since Rust strings operations are assumed to never fail, this commit removes a lot of checking, whether appending to mmapstring fails. Now it is Rust runtime burden.
This commit is contained in:
committed by
holger krekel
parent
b73d6377fc
commit
e7565e1a2a
@@ -5,12 +5,20 @@ use std::ptr;
|
|||||||
use charset::Charset;
|
use charset::Charset;
|
||||||
use libc::{free, strlen};
|
use libc::{free, strlen};
|
||||||
use mmime::mailmime::decode::mailmime_encoded_phrase_parse;
|
use mmime::mailmime::decode::mailmime_encoded_phrase_parse;
|
||||||
use mmime::mmapstring::*;
|
|
||||||
use mmime::other::*;
|
use mmime::other::*;
|
||||||
use percent_encoding::{percent_decode, utf8_percent_encode, AsciiSet, CONTROLS};
|
use percent_encoding::{percent_decode, utf8_percent_encode, AsciiSet, CONTROLS};
|
||||||
|
|
||||||
use crate::dc_tools::*;
|
use crate::dc_tools::*;
|
||||||
|
|
||||||
|
/// Append valid utf-8 string {buf} with size {len} to Rust string {s}.
|
||||||
|
///
|
||||||
|
/// {buf} is not expected to be null-terminated.
|
||||||
|
unsafe fn append_buf(s: &mut String, buf: *const libc::c_char, len: usize) {
|
||||||
|
let slice = std::slice::from_raw_parts(buf.cast(), len);
|
||||||
|
let string = std::str::from_utf8(slice).expect("append_buf: invalid utf8");
|
||||||
|
s.push_str(string);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encode non-ascii-strings as `=?UTF-8?Q?Bj=c3=b6rn_Petersen?=`.
|
* Encode non-ascii-strings as `=?UTF-8?Q?Bj=c3=b6rn_Petersen?=`.
|
||||||
* Belongs to RFC 2047: https://tools.ietf.org/html/rfc2047
|
* Belongs to RFC 2047: https://tools.ietf.org/html/rfc2047
|
||||||
@@ -32,15 +40,10 @@ pub unsafe fn dc_encode_header_words(to_encode_r: impl AsRef<str>) -> String {
|
|||||||
let mut ok_to_continue = true;
|
let mut ok_to_continue = true;
|
||||||
let mut ret_str: *mut libc::c_char = ptr::null_mut();
|
let mut ret_str: *mut libc::c_char = ptr::null_mut();
|
||||||
let mut cur: *const libc::c_char = to_encode.as_ptr();
|
let mut cur: *const libc::c_char = to_encode.as_ptr();
|
||||||
let mmapstr: *mut MMAPString = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char);
|
let mut mmapstr = "".into();
|
||||||
if mmapstr.is_null() {
|
|
||||||
ok_to_continue = false;
|
|
||||||
}
|
|
||||||
loop {
|
loop {
|
||||||
if !ok_to_continue {
|
if !ok_to_continue {
|
||||||
if !mmapstr.is_null() {
|
|
||||||
mmap_string_free(mmapstr);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
if *cur as libc::c_int != '\u{0}' as i32 {
|
if *cur as libc::c_int != '\u{0}' as i32 {
|
||||||
@@ -64,53 +67,24 @@ pub unsafe fn dc_encode_header_words(to_encode_r: impl AsRef<str>) -> String {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if 0 != quote_words {
|
if 0 != quote_words {
|
||||||
if !quote_word(
|
quote_word(&mut mmapstr, begin, end.wrapping_offset_from(begin) as _);
|
||||||
mmapstr,
|
|
||||||
begin,
|
|
||||||
end.wrapping_offset_from(begin) as libc::size_t,
|
|
||||||
) {
|
|
||||||
ok_to_continue = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if *end as libc::c_int == ' ' as i32 || *end as libc::c_int == '\t' as i32 {
|
if *end as libc::c_int == ' ' as i32 || *end as libc::c_int == '\t' as i32 {
|
||||||
if mmap_string_append_c(mmapstr, *end).is_null() {
|
mmapstr.push(*end as u8 as char);
|
||||||
ok_to_continue = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
end = end.offset(1isize)
|
end = end.offset(1isize)
|
||||||
}
|
}
|
||||||
if *end as libc::c_int != '\u{0}' as i32 {
|
if *end as libc::c_int != '\u{0}' as i32 {
|
||||||
if mmap_string_append_len(
|
append_buf(&mut mmapstr, end, cur.wrapping_offset_from(end) as _);
|
||||||
mmapstr,
|
|
||||||
end,
|
|
||||||
cur.wrapping_offset_from(end) as libc::size_t,
|
|
||||||
)
|
|
||||||
.is_null()
|
|
||||||
{
|
|
||||||
ok_to_continue = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if mmap_string_append_len(
|
} else {
|
||||||
mmapstr,
|
append_buf(&mut mmapstr, begin, cur.wrapping_offset_from(begin) as _);
|
||||||
begin,
|
|
||||||
cur.wrapping_offset_from(begin) as libc::size_t,
|
|
||||||
)
|
|
||||||
.is_null()
|
|
||||||
{
|
|
||||||
ok_to_continue = false;
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
if !(*cur as libc::c_int == ' ' as i32 || *cur as libc::c_int == '\t' as i32) {
|
if !(*cur as libc::c_int == ' ' as i32 || *cur as libc::c_int == '\t' as i32) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if mmap_string_append_c(mmapstr, *cur).is_null() {
|
mmapstr.push(*cur as u8 as char);
|
||||||
ok_to_continue = false;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
cur = cur.offset(1isize);
|
cur = cur.offset(1isize);
|
||||||
} else {
|
} else {
|
||||||
ret_str = strdup((*mmapstr).str_0);
|
ret_str = mmapstr.strdup();
|
||||||
ok_to_continue = false;
|
ok_to_continue = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -121,18 +95,12 @@ pub unsafe fn dc_encode_header_words(to_encode_r: impl AsRef<str>) -> String {
|
|||||||
s
|
s
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn quote_word(
|
unsafe fn quote_word(mmapstr: &mut String, word: *const libc::c_char, size: libc::size_t) {
|
||||||
mmapstr: *mut MMAPString,
|
|
||||||
word: *const libc::c_char,
|
|
||||||
size: libc::size_t,
|
|
||||||
) -> bool {
|
|
||||||
let mut cur: *const libc::c_char;
|
let mut cur: *const libc::c_char;
|
||||||
let mut i = 0;
|
let mut i = 0;
|
||||||
let mut hex: [libc::c_char; 4] = [0; 4];
|
let mut hex: [libc::c_char; 4] = [0; 4];
|
||||||
// let mut col: libc::c_int = 0i32;
|
// let mut col: libc::c_int = 0i32;
|
||||||
if mmap_string_append(mmapstr, b"=?utf-8?Q?\x00".as_ptr().cast()).is_null() {
|
mmapstr.push_str("=?utf-8?Q?");
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
// col = (*mmapstr).len as libc::c_int;
|
// col = (*mmapstr).len as libc::c_int;
|
||||||
cur = word;
|
cur = word;
|
||||||
@@ -149,28 +117,20 @@ unsafe fn quote_word(
|
|||||||
}
|
}
|
||||||
if do_quote_char {
|
if do_quote_char {
|
||||||
print_hex(hex.as_mut_ptr(), cur);
|
print_hex(hex.as_mut_ptr(), cur);
|
||||||
if mmap_string_append(mmapstr, hex.as_mut_ptr()).is_null() {
|
mmapstr.push_str(as_str(hex.as_ptr()));
|
||||||
return false;
|
|
||||||
}
|
|
||||||
// col += 3i32
|
// col += 3i32
|
||||||
} else {
|
} else {
|
||||||
if *cur as libc::c_int == ' ' as i32 {
|
if *cur as libc::c_int == ' ' as i32 {
|
||||||
if mmap_string_append_c(mmapstr, '_' as i32 as libc::c_char).is_null() {
|
mmapstr.push('_');
|
||||||
return false;
|
} else {
|
||||||
}
|
mmapstr.push(*cur as u8 as char);
|
||||||
} else if mmap_string_append_c(mmapstr, *cur).is_null() {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
// col += 3i32
|
// col += 3i32
|
||||||
}
|
}
|
||||||
cur = cur.offset(1isize);
|
cur = cur.offset(1isize);
|
||||||
i = i.wrapping_add(1)
|
i = i.wrapping_add(1)
|
||||||
}
|
}
|
||||||
if mmap_string_append(mmapstr, b"?=\x00" as *const u8 as *const libc::c_char).is_null() {
|
mmapstr.push_str("?=");
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn get_word(
|
unsafe fn get_word(
|
||||||
|
|||||||
Reference in New Issue
Block a user