Charsets: skip superseded ISO-2022-JP designations (fixes stalwartlabs/encodify#3)

This commit is contained in:
Stalwart Git Sync
2026-11-29 16:29:26 +01:00
parent edb426b1a3
commit b596735bf1
3 changed files with 100 additions and 1 deletions
+1
View File
@@ -6,6 +6,7 @@ All notable changes to this project will be documented in this file. This projec
### Fixed
- `html_to_text` and HTML previews ran words together across block elements, so `<div>One</div><div>Two</div>` gave `OneTwo`. Block elements such as `<div>`, `<li>`, `<tr>` and `<h1>` now end the line and table cells are separated by a space, while a `<br>` or `<p>` inside `<template>`, `<style>` or `<script>` no longer adds a line break (#172).
- ISO-2022-JP text with two escape sequences in a row and no characters between them, such as `ESC ( B ESC $ B`, decoded with a U+FFFD. The first, superseded escape sequence is now skipped, so the text decodes cleanly in headers, encoded words and bodies (encodify#3).
## [1.0.2] - 2026-11-23
+37
View File
@@ -699,6 +699,43 @@ mod tests {
}
}
#[cfg(feature = "full_encoding")]
#[test]
fn iso_2022_jp_drops_superseded_designations() {
let charset = Charset::Iso2022Jp;
for (bytes, expected) in [
(&b"\x1b$B$\"\x1b(B\x1b$B$$\x1b(B"[..], "\u{3042}\u{3044}"),
(b"\x1b$B\x1b(BA\x1b$B$$\x1b(B", "A\u{3044}"),
(b"\x1b(B\x1b$B\x1b(J\x1b$B$$\x1b(B", "\u{3044}"),
(b"\x1b(B\x1b(B", ""),
(b"\x1b$B$\"\x1b(B\x1b$X", "\u{3042}\u{fffd}$X"),
(b"a\x1b(B", "a"),
(b"\x1b(Ba", "a"),
] {
assert_eq!(charset.decode(bytes), expected, "{bytes:?}");
assert_eq!(charset.decode_prefix(bytes), expected, "{bytes:?}");
let (text, malformed) = charset.decode_checked(bytes);
assert_eq!(text, expected, "{bytes:?}");
assert_eq!(malformed, expected.contains('\u{fffd}'), "{bytes:?}");
let mut out = String::from(">");
charset.decode_append(bytes, &mut out);
assert_eq!(out.strip_prefix('>'), Some(expected), "{bytes:?}");
}
for (header, expected) in [
(
&b"=?ISO-2022-JP?B?GyRCJCIbKEIbJEIkJBsoQg==?="[..],
"\u{3042}\u{3044}",
),
(b"=?ISO-2022-JP?B?GyRCGyhCQRskQiQkGyhC?=", "A\u{3044}"),
] {
assert_eq!(
encodify::rfc2047::decode_text(header, decode_append),
expected,
"{header:?}"
);
}
}
#[test]
fn decoder_charset() {
for input in ["gbk", "extended_unix_code_packed_format_for_japanese"] {
+62 -1
View File
@@ -6,6 +6,9 @@
use std::borrow::Cow;
#[cfg(feature = "full_encoding")]
use memchr::memchr_iter;
#[cfg(feature = "full_encoding")]
use encoding_rs::{
BIG5, CoderResult, Decoder, EUC_JP, EUC_KR, Encoding, GB18030, GBK, IBM866, ISO_2022_JP,
@@ -29,6 +32,10 @@ pub(super) enum MultiByte {
#[cfg(feature = "full_encoding")]
const MIN_ROOM: usize = 16;
#[cfg(feature = "full_encoding")]
const ESC: u8 = 0x1b;
#[cfg(feature = "full_encoding")]
const DESIGNATION_LEN: usize = 3;
#[cfg(feature = "full_encoding")]
impl MultiByte {
@@ -48,11 +55,26 @@ impl MultiByte {
}
}
fn without_superseded_designations(self, bytes: &[u8]) -> Option<Vec<u8>> {
if self != MultiByte::Iso2022Jp {
return None;
}
let first = memchr_iter(ESC, bytes).find(|&at| superseded_designation_at(bytes, at))?;
Some(drop_superseded_designations(bytes, first))
}
pub(super) fn decode(self, bytes: &[u8]) -> Cow<'_, str> {
if let Some(bytes) = self.without_superseded_designations(bytes) {
return Cow::Owned(self.encoding().decode(&bytes).0.into_owned());
}
self.encoding().decode(bytes).0
}
pub(super) fn decode_checked(self, bytes: &[u8]) -> (Cow<'_, str>, bool) {
if let Some(bytes) = self.without_superseded_designations(bytes) {
let (text, _, malformed) = self.encoding().decode(&bytes);
return (Cow::Owned(text.into_owned()), malformed);
}
let (text, _, malformed) = self.encoding().decode(bytes);
(text, malformed)
}
@@ -63,7 +85,12 @@ impl MultiByte {
{
out.push_str(ascii);
} else {
decode_with(self.encoding().new_decoder(), bytes, out);
let normalized = self.without_superseded_designations(bytes);
decode_with(
self.encoding().new_decoder(),
normalized.as_deref().unwrap_or(bytes),
out,
);
}
}
@@ -73,6 +100,8 @@ impl MultiByte {
{
return Cow::Borrowed(ascii);
}
let normalized = self.without_superseded_designations(bytes);
let bytes = normalized.as_deref().unwrap_or(bytes);
let mut decoder = self.encoding().new_decoder();
let mut out = String::with_capacity(
decoder
@@ -98,6 +127,38 @@ impl MultiByte {
}
}
#[cfg(feature = "full_encoding")]
fn superseded_designation_at(bytes: &[u8], at: usize) -> bool {
let is_designation = |bytes: &[u8]| {
matches!(
bytes,
[ESC, b'(', b'B' | b'J' | b'I', ..] | [ESC, b'$', b'@' | b'B', ..]
)
};
bytes
.get(at..)
.and_then(|rest| rest.split_at_checked(DESIGNATION_LEN))
.is_some_and(|(designation, next)| is_designation(designation) && is_designation(next))
}
#[cfg(feature = "full_encoding")]
#[cold]
fn drop_superseded_designations(bytes: &[u8], first: usize) -> Vec<u8> {
let mut normalized = Vec::with_capacity(bytes.len());
normalized.extend_from_slice(bytes.get(..first).unwrap_or_default());
let resume = first + DESIGNATION_LEN;
let mut copied = resume;
for at in memchr_iter(ESC, bytes.get(resume..).unwrap_or_default()) {
let at = resume + at;
if superseded_designation_at(bytes, at) {
normalized.extend_from_slice(bytes.get(copied..at).unwrap_or_default());
copied = at + DESIGNATION_LEN;
}
}
normalized.extend_from_slice(bytes.get(copied..).unwrap_or_default());
normalized
}
#[cfg(feature = "full_encoding")]
pub(super) fn decode_with(mut decoder: Decoder, mut bytes: &[u8], out: &mut String) {
loop {