Leniently decode quoted-printable bodies with invalid = escapes (closes #144)

This commit is contained in:
Maurus Decimus
2026-06-21 09:07:56 +02:00
parent 0128bc1da0
commit 6da7920ab6
24 changed files with 6693 additions and 37 deletions
+13 -4
View File
@@ -157,8 +157,10 @@ impl<'x> MessageStream<'x> {
if hex1 != -1 {
state = QuotedPrintableState::Hex1;
} else if !ch.is_ascii_whitespace() {
self.restore();
return (usize::MAX, b""[..].into());
state = QuotedPrintableState::None;
buf.push(b'=');
buf.push(ch);
ws_count = 0;
}
}
QuotedPrintableState::Hex1 => {
@@ -169,8 +171,10 @@ impl<'x> MessageStream<'x> {
buf.push(((hex1 as u8) << 4) | hex2 as u8);
ws_count = 0;
} else {
self.restore();
return (usize::MAX, b""[..].into());
buf.push(b'=');
buf.push(last_ch);
buf.push(ch);
ws_count = 0;
}
}
},
@@ -346,6 +350,11 @@ mod tests {
#[test]
fn decode_quoted_printable_mime() {
for (encoded_str, expected_result) in [
(
"<meta content=\"text/html; charset=utf-8\"> h=C3=B6\n--boundary",
"<meta content=\"text/html; charset=utf-8\"> hö",
),
("first=AZ second\n--boundary", "first=AZ second"),
(
"=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry\n--boundary",
"— Antoine de Saint-Exupéry",
-1
View File
@@ -243,7 +243,6 @@ impl MessageParser {
// Attempt to recover contents of an invalid message
let mut is_encoding_problem = offset_end == usize::MAX;
eprintln!("DBG part={} ct={:?} mime_type={:?} is_text={} is_inline={} enc_problem={} offset_end={}", state.parts, content_type.map(|c| format!("{}/{}", c.ctype(), c.subtype().unwrap_or(""))), mime_type, is_text, is_inline, is_encoding_problem, offset_end as i64);
if is_encoding_problem {
encoding = Encoding::None;
if mime_type != MimeType::TextPlain {