melib: fix non-unicode encode_header() char boundary issue

master
Manos Pitsidianakis 2020-06-04 17:54:38 +03:00
parent 84976b1dc9
commit 8c1c628c2c
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
1 changed files with 12 additions and 12 deletions

View File

@ -88,8 +88,7 @@ pub fn encode_header(value: &str) -> String {
{ {
/* TODO: test this. If it works as fine as the one above, there's no need to keep the above /* TODO: test this. If it works as fine as the one above, there's no need to keep the above
* implementation.*/ * implementation.*/
let mut idx = 0; for (i, g) in value.char_indices() {
for g in value.chars() {
match (g.is_ascii(), is_current_window_ascii) { match (g.is_ascii(), is_current_window_ascii) {
(true, true) => { (true, true) => {
ret.push(g); ret.push(g);
@ -99,23 +98,23 @@ pub fn encode_header(value: &str) -> String {
* *
* Whitespaces inside encoded tokens must be greedily taken, * Whitespaces inside encoded tokens must be greedily taken,
* instead of splitting each non-ascii word into separate encoded tokens. */ * instead of splitting each non-ascii word into separate encoded tokens. */
if !g.is_whitespace() { if !g.is_whitespace() && value.is_char_boundary(i) {
ret.push_str(&format!( ret.push_str(&format!(
"=?UTF-8?B?{}?=", "=?UTF-8?B?{}?=",
BASE64_MIME BASE64_MIME
.encode(value[current_window_start..idx].as_bytes()) .encode(value[current_window_start..i].as_bytes())
.trim() .trim()
)); ));
if idx != value.len() - 1 { if i != value.len() - 1 {
ret.push(' '); ret.push(' ');
} }
is_current_window_ascii = true; is_current_window_ascii = true;
current_window_start = idx; current_window_start = i;
ret.push(g); ret.push(g);
} }
} }
(false, true) => { (false, true) => {
current_window_start = idx; current_window_start = i;
is_current_window_ascii = false; is_current_window_ascii = false;
} }
/* RFC2047 recommends: /* RFC2047 recommends:
@ -124,21 +123,22 @@ pub fn encode_header(value: &str) -> String {
* characters.' * characters.'
* This is a rough compliance. * This is a rough compliance.
*/ */
(false, false) if (((4 * (idx - current_window_start) / 3) + 3) & !3) > 33 => { (false, false)
if value.is_char_boundary(i) && value[current_window_start..i].len() > 76 =>
{
ret.push_str(&format!( ret.push_str(&format!(
"=?UTF-8?B?{}?=", "=?UTF-8?B?{}?=",
BASE64_MIME BASE64_MIME
.encode(value[current_window_start..idx].as_bytes()) .encode(value[current_window_start..i].as_bytes())
.trim() .trim()
)); ));
if idx != value.len() - 1 { if i != value.len() - 1 {
ret.push(' '); ret.push(' ');
} }
current_window_start = idx; current_window_start = i;
} }
(false, false) => {} (false, false) => {}
} }
idx += std::mem::size_of::<char>();
} }
} }
/* If the last part of the header value is encoded, it won't be pushed inside the previous for /* If the last part of the header value is encoded, it won't be pushed inside the previous for