Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/src/mime.rs

27.4 KiB, 39 runs

created by r1870400018:74762, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! MIME text decoding: RFC 2047 encoded-words in header values, quoted-printable and the
2//! whitespace-wrapped base64 of a MIME part, the leaf parts of a message walked by boundary,
3//! and the charsets a mail body arrives in, as UTF-8.
4//!
5//! Moved from `fe2o3_mail::message` on 2026-10-26 so that `fe2o3_net`'s IMAP SEARCH, which
6//! `fe2o3_mail` depends on, matches header values decoded, and the sans-io message reader,
7//! which builds without `fe2o3_net`, reads them through the same code.
8//!
9//! 2026-10-26 (F2b): the part walker joined it, for SEARCH BODY and TEXT and the message
10//! reader alike. It works on bytes, not text, since a Latin-1 body is not UTF-8 and would lose
11//! its accents to a lossy read before its charset was known. ISO-8859-1, ISO-8859-15 and
12//! Windows-1252 are transcoded; any other charset is read as UTF-8 with invalid sequences
13//! replaced, which keeps the ASCII every charset shares.
14
15use crate::base64;
16
17
18/// Decode RFC 2047 encoded-words (`=?charset?B?...?=` or `?Q?`) in a header value.
19///
20/// Adjacent encoded-words separated only by whitespace are joined with none, which is what
21/// the standard says to do so a name split across two words reads as one.
22pub fn decode_words(s: &str) -> String {
23 let bytes = s.as_bytes();
24 let mut out = String::with_capacity(s.len());
25 let mut i = 0;
26 // Whether the previous token emitted was itself an encoded-word, so the whitespace
27 // before the next one can be dropped where that next one is also encoded.
28 let mut last_was_word = false;
29 while i < bytes.len() {
30 if bytes[i] == b'=' && i + 1 < bytes.len() && bytes[i + 1] == b'?' {
31 if let Some((decoded, next)) = decode_one_word(s, i) {
32 // Drop the run of whitespace this word was separated from the previous
33 // encoded-word by.
34 if last_was_word {
35 while out.ends_with(' ') || out.ends_with('\t')
36 || out.ends_with('\r') || out.ends_with('\n')
37 {
38 out.pop();
39 }
40 }
41 out.push_str(&decoded);
42 i = next;
43 last_was_word = true;
44 continue;
45 }
46 }
47 // Any ordinary character. A non-whitespace one means the run of encoded-words has
48 // ended, so a later word no longer joins to an earlier one.
49 let ch = s[i..].chars().next().unwrap_or('\u{fffd}');
50 if !ch.is_whitespace() {
51 last_was_word = false;
52 }
53 out.push(ch);
54 i += ch.len_utf8();
55 }
56 out
57}
58
59/// Decode the one encoded-word beginning at `start`, returning it and the offset just past
60/// it, or `None` when the bytes there are not a well-formed encoded-word.
61fn decode_one_word(s: &str, start: usize) -> Option<(String, usize)> {
62 // `=?charset?enc?text?=`
63 let rest = &s[start + 2..];
64 let q1 = match rest.find('?') {
65 Some(n) => n,
66 None => return None,
67 };
68 let charset = &rest[..q1];
69 let after_cs = &rest[q1 + 1..];
70 let q2 = match after_cs.find('?') {
71 Some(n) => n,
72 None => return None,
73 };
74 let enc = &after_cs[..q2];
75 let after_enc = &after_cs[q2 + 1..];
76 let q3 = match after_enc.find("?=") {
77 Some(n) => n,
78 None => return None,
79 };
80 let text = &after_enc[..q3];
81 if charset.is_empty() || enc.len() != 1 {
82 return None;
83 }
84 let end = start + 2 + q1 + 1 + q2 + 1 + q3 + 2;
85 let raw = match enc.as_bytes()[0].to_ascii_lowercase() {
86 b'b' => decode_base64_lenient(text),
87 b'q' => decode_q(text),
88 _ => return None,
89 };
90 // RFC 2231 lets a language follow the charset: `iso-8859-1*fr`.
91 let cs = charset.split('*').next().unwrap_or(charset);
92 Some((to_utf8(&raw, Some(cs)), end))
93}
94
95/// Decode a `Q` encoded-word body: `_` is a space, `=HH` is a byte, everything else stands.
96fn decode_q(s: &str) -> Vec<u8> {
97 let b = s.as_bytes();
98 let mut out = Vec::with_capacity(b.len());
99 let mut i = 0;
100 while i < b.len() {
101 match b[i] {
102 b'_' => { out.push(b' '); i += 1; },
103 b'=' if i + 2 < b.len() => {
104 match hex2(b[i + 1], b[i + 2]) {
105 Some(v) => { out.push(v); i += 3; },
106 None => { out.push(b'='); i += 1; },
107 }
108 },
109 other => { out.push(other); i += 1; },
110 }
111 }
112 out
113}
114
115/// Decode a quoted-printable body to bytes: `=\n` soft breaks vanish, `=HH` is one byte.
116pub fn decode_qp<B: AsRef<[u8]> + ?Sized>(s: &B) -> Vec<u8> {
117 let b = s.as_ref();
118 let mut out = Vec::with_capacity(b.len());
119 let mut i = 0;
120 while i < b.len() {
121 if b[i] == b'=' {
122 // A soft line break: `=` at end of line.
123 if i + 1 < b.len() && b[i + 1] == b'\n' { i += 2; continue; }
124 if i + 2 < b.len() && b[i + 1] == b'\r' && b[i + 2] == b'\n' { i += 3; continue; }
125 if i + 2 < b.len() {
126 if let Some(v) = hex2(b[i + 1], b[i + 2]) {
127 out.push(v);
128 i += 3;
129 continue;
130 }
131 }
132 }
133 out.push(b[i]);
134 i += 1;
135 }
136 out
137}
138
139/// Base64, tolerant of the line breaks and whitespace a MIME body wraps it in.
140pub fn decode_base64_lenient<B: AsRef<[u8]> + ?Sized>(s: &B) -> Vec<u8> {
141 let s = s.as_ref();
142 let cleaned: String = s.iter()
143 .filter(|b| !b.is_ascii_whitespace())
144 .map(|b| *b as char)
145 .collect();
146 if cleaned.is_empty() {
147 return Vec::new();
148 }
149 match base64::decode(&cleaned) {
150 Ok(v) => v,
151 // A body that will not decode is handed back as its own bytes rather than lost: a
152 // reader seeing the encoded text is better served than one seeing nothing.
153 Err(_) => s.to_vec(),
154 }
155}
156
157/// Two hex digits to a byte, or `None` when either is not hex.
158fn hex2(a: u8, b: u8) -> Option<u8> {
159 match ((a as char).to_digit(16), (b as char).to_digit(16)) {
160 (Some(hi), Some(lo)) => Some((hi * 16 + lo) as u8),
161 _ => None,
162 }
163}
164
165
166// ┌───────────────────────────────────────────────────────────────────────────┐
167// │ PARTS │
168// └───────────────────────────────────────────────────────────────────────────┘
169
170const MAX_DEPTH: usize = 16; // nesting followed, against a hostile message
171
172/// One entity of a MIME message, a part or the message itself, with its headers read and its
173/// body still in its transfer encoding.
174#[derive(Clone, Debug)]
175pub struct Part<'a> {
176 pub ctype: String, // the Content-Type value, `text/plain` when absent
177 pub encoding: String, // Content-Transfer-Encoding, lower case, empty when absent
178 pub disposition: String, // Content-Disposition, as given
179 pub head: &'a [u8], // the header block
180 pub body: &'a [u8],
181 pub structure: Structure<'a>,
182}
183
184/// What an entity holds, by RFC 2046: content, a multipart's parts, or an encapsulated
185/// message, which is a message in its own right and not part of its parent's text.
186#[derive(Clone, Debug)]
187pub enum Structure<'a> {
188 Leaf,
189 Multipart(Vec<Part<'a>>),
190 Message(Box<Part<'a>>),
191}
192
193impl<'a> Part<'a> {
194 /// The media type alone, lower case: `text/plain`.
195 pub fn media_type(&self) -> String {
196 media_type(&self.ctype)
197 }
198
199 pub fn is_text(&self) -> bool {
200 self.media_type().starts_with("text/")
201 }
202
203 /// Is this an encapsulated message (`message/rfc822`, or RFC 6532's `message/global`),
204 /// whether or not its body could be read as one?
205 pub fn is_message(&self) -> bool {
206 matches!(self.media_type().as_str(), "message/rfc822" | "message/global")
207 }
208
209 /// The first value of a header field of this entity, `name` in lower case.
210 pub fn header(&self, name: &str) -> Option<String> {
211 field(&header_fields(self.head), name)
212 }
213
214 /// The name of the file the part carries, from Content-Type's `name` or
215 /// Content-Disposition's `filename`, encoded-words decoded.
216 pub fn filename(&self) -> Option<String> {
217 param(&self.ctype, "name")
218 .or_else(|| param(&self.disposition, "filename"))
219 .map(|n| decode_words(n.trim()))
220 .filter(|n| !n.is_empty())
221 }
222
223 /// Is the part disposed as an `attachment`, or named as a file with a disposition other
224 /// than `inline`?
225 fn is_disposed_attachment(&self) -> bool {
226 let d = self.disposition.trim_start().to_ascii_lowercase();
227 d.starts_with("attachment") || (!d.is_empty() && !d.starts_with("inline") && self.filename().is_some())
228 }
229
230 /// Is this what a reader means by an attachment: a part with a file name, one disposed
231 /// as `attachment`, or an encapsulated message?
232 pub fn is_attachment(&self) -> bool {
233 self.filename().is_some()
234 || self.is_message()
235 || self.disposition.trim_start().to_ascii_lowercase().starts_with("attachment")
236 }
237
238 /// The name a reader is shown for this part as an attachment: its file name, else for an
239 /// encapsulated message its Subject, else `forwarded message` or `attachment`.
240 pub fn attachment_name(&self) -> String {
241 if let Some(n) = self.filename() { return n; }
242 if !self.is_message() { return String::from("attachment"); }
243 let subject = match &self.structure {
244 Structure::Message(inner) => inner.header("subject"),
245 // Encoded against RFC 2046 §5.2.1, so read once decoded.
246 _ => {
247 let bytes = match self.encoding.as_str() {
248 "base64" => decode_base64_lenient(self.body),
249 "quoted-printable" => decode_qp(self.body),
250 _ => self.body.to_vec(),
251 };
252 field(&headers(&bytes), "subject")
253 }
254 };
255 subject.map(|s| decode_words(s.trim()))
256 .filter(|s| !s.is_empty())
257 .unwrap_or_else(|| String::from("forwarded message"))
258 }
259
260 /// The body out of its transfer encoding and its charset, as UTF-8.
261 pub fn text(&self) -> String {
262 decode_body(self.body, &self.encoding, param(&self.ctype, "charset").as_deref())
263 }
264
265 /// The body out of its transfer encoding, as the bytes it carries: an attachment's file.
266 pub fn bytes(&self) -> Vec<u8> {
267 decode_transfer(self.body, &self.encoding)
268 }
269
270 /// The Content-ID (RFC 2392) a `cid:` URL names this part by, without its angle brackets.
271 pub fn content_id(&self) -> Option<String> {
272 self.header("content-id")
273 .map(|id| {
274 let t = id.trim();
275 t.strip_prefix('<').and_then(|t| t.strip_suffix('>')).unwrap_or(t).trim().to_string()
276 })
277 .filter(|id| !id.is_empty())
278 }
279
280 /// The `text/plain` and `text/html` leaves a reader is shown as this entity's body, in
281 /// order, by RFC 2046: of a `multipart/alternative` the plain version among its own parts,
282 /// else the last that shows anything; of a `multipart/related` its root (RFC 2387); of any
283 /// other multipart each part in turn. An attachment, and anything inside an encapsulated
284 /// message, is never shown, nor is a leaf with no text.
285 pub fn display_parts(&self) -> Vec<&Part<'a>> {
286 let mut out = Vec::new();
287 self.display_into(&mut out, false);
288 out
289 }
290
291 /// The leaves a viewer that renders HTML shows, as [`Part::display_parts`] except that of
292 /// a `multipart/alternative` it takes the last version that shows anything, the richest
293 /// by RFC 2046 §5.1.4.
294 pub fn rich_parts(&self) -> Vec<&Part<'a>> {
295 let mut out = Vec::new();
296 self.display_into(&mut out, true);
297 out
298 }
299
300 fn display_into<'s>(&'s self, out: &mut Vec<&'s Part<'a>>, rich: bool) {
301 match &self.structure {
302 Structure::Leaf => {
303 let media = self.media_type();
304 // A multipart left a leaf, with no boundary or too deep, is shown as the text
305 // it is rather than not at all.
306 let shown = media == "text/plain" || media == "text/html" || media.starts_with("multipart/");
307 // A file name alone does not make a text part an attachment to the reader:
308 // with no disposition it is shown inline, as other readers do (RFC 2183 §2).
309 if shown
310 && !self.is_disposed_attachment()
311 && !self.text().trim().is_empty()
312 {
313 out.push(self);
314 }
315 }
316 Structure::Message(_) => (),
317 Structure::Multipart(kids) => match self.media_type().as_str() {
318 "multipart/alternative" => {
319 let parts = |k: &'s Part<'a>| {
320 let mut v = Vec::new();
321 k.display_into(&mut v, rich);
322 v
323 };
324 let plain = if rich { None } else {
325 kids.iter()
326 .filter(|k| matches!(k.structure, Structure::Leaf) && k.media_type() == "text/plain")
327 .map(parts)
328 .find(|v| !v.is_empty())
329 };
330 let chosen = plain.or_else(|| kids.iter().rev()
331 .map(parts)
332 .find(|v| !v.is_empty()));
333 if let Some(v) = chosen { out.extend(v); }
334 }
335 "multipart/related" => {
336 let start = param(&self.ctype, "start");
337 let root = start.and_then(|s| kids.iter().find(|k|
338 k.header("content-id").map(|id| id.trim() == s.trim()).unwrap_or(false)))
339 .or_else(|| kids.first());
340 if let Some(r) = root { r.display_into(out, rich); }
341 }
342 _ => for k in kids { k.display_into(out, rich); },
343 },
344 }
345 }
346}
347
348/// The MIME structure of `raw`, a whole message: each multipart split by its boundary and each
349/// encapsulated message read as a message, to a depth of sixteen, below which an entity is a
350/// leaf. The preamble and epilogue of a multipart are not parts.
351pub fn entity(raw: &[u8]) -> Part<'_> {
352 read_entity(raw, 0)
353}
354
355fn read_entity(raw: &[u8], depth: usize) -> Part<'_> {
356 let (head, body) = split_entity(raw);
357 let hs = header_fields(head);
358 let ctype = match field(&hs, "content-type") {
359 Some(c) if !c.trim().is_empty() => c,
360 _ => String::from("text/plain"),
361 };
362 let encoding = field(&hs, "content-transfer-encoding").unwrap_or_default().trim().to_ascii_lowercase();
363 let media = media_type(&ctype);
364 let mut structure = Structure::Leaf;
365 if depth < MAX_DEPTH {
366 if media.starts_with("multipart/") {
367 if let Some(b) = param(&ctype, "boundary") {
368 structure = Structure::Multipart(multipart_bodies(body, b.as_bytes()).into_iter()
369 .map(|p| read_entity(p, depth + 1))
370 .collect());
371 }
372 } else if (media == "message/rfc822" || media == "message/global")
373 // An encoded message (RFC 2046 §5.2.1 forbids it) stays a leaf: its bytes would
374 // have to be decoded into a buffer the parts could not borrow from.
375 && matches!(encoding.as_str(), "" | "7bit" | "8bit" | "binary")
376 {
377 structure = Structure::Message(Box::new(read_entity(body, depth + 1)));
378 }
379 }
380 Part {
381 ctype,
382 encoding,
383 disposition: field(&hs, "content-disposition").unwrap_or_default(),
384 head,
385 body,
386 structure,
387 }
388}
389
390/// Every leaf of `raw`, a whole message, in the order it holds them, inside encapsulated
391/// messages too, as SEARCH reads them. A message that is not multipart is its own one leaf.
392pub fn leaf_parts(raw: &[u8]) -> Vec<Part<'_>> {
393 let mut out = Vec::new();
394 flatten(entity(raw), &mut out);
395 out
396}
397
398fn flatten<'a>(mut p: Part<'a>, out: &mut Vec<Part<'a>>) {
399 match std::mem::replace(&mut p.structure, Structure::Leaf) {
400 Structure::Leaf => out.push(p),
401 Structure::Multipart(kids) => for k in kids { flatten(k, out); },
402 Structure::Message(inner) => flatten(*inner, out),
403 }
404}
405
406/// The bodies of a multipart's parts, each without the line break that belongs to the
407/// delimiter after it (RFC 2046 §5.1.1). A multipart cut short before its closing delimiter
408/// keeps what it has.
409fn multipart_bodies<'a>(body: &'a [u8], boundary: &[u8]) -> Vec<&'a [u8]> {
410 let mut parts = Vec::new();
411 let mut start: Option<usize> = None;
412 let mut pos = 0;
413 for line in body.split_inclusive(|b| *b == b'\n') {
414 let rest = line.strip_prefix(b"--").and_then(|r| r.strip_prefix(boundary));
415 if let Some(rest) = rest {
416 let closing = rest.starts_with(b"--");
417 // Transport padding may follow the delimiter; anything else makes it a line of
418 // the body that only begins like one.
419 if closing || rest.iter().all(|b| b.is_ascii_whitespace()) {
420 if let Some(s) = start {
421 let content = &body[s..pos];
422 let content = match content.strip_suffix(b"\n") {
423 Some(c) => c.strip_suffix(b"\r").unwrap_or(c),
424 None => content,
425 };
426 parts.push(content);
427 }
428 if closing { return parts; }
429 start = Some(pos + line.len());
430 }
431 }
432 pos += line.len();
433 }
434 if let Some(s) = start {
435 parts.push(&body[s..]);
436 }
437 parts
438}
439
440/// An entity's header block and body, split at the first blank line. One that opens with a
441/// blank line has no headers, and one with no blank line is all header.
442pub fn split_entity(raw: &[u8]) -> (&[u8], &[u8]) {
443 if let Some(rest) = raw.strip_prefix(b"\r\n") { return (&[], rest); }
444 if let Some(rest) = raw.strip_prefix(b"\n") { return (&[], rest); }
445 let mut i = 0;
446 while i + 1 < raw.len() {
447 if raw[i] == b'\n' {
448 if raw[i + 1] == b'\n' {
449 return (&raw[..i], &raw[i + 2..]);
450 }
451 if raw[i + 1] == b'\r' && raw.get(i + 2) == Some(&b'\n') {
452 return (&raw[..i], &raw[i + 3..]);
453 }
454 }
455 i += 1;
456 }
457 (raw, &[])
458}
459
460/// The header fields of a message or part, unfolded, as `(lower-case name, value)` in order.
461///
462/// The name is lower-cased because a field is looked up by a caller who cannot know whether
463/// the sender wrote `Message-ID` or `Message-Id`; the value keeps its case. Raw 8-bit bytes
464/// in a value are read as UTF-8 (RFC 6532).
465pub fn headers(raw: &[u8]) -> Vec<(String, String)> {
466 header_fields(split_entity(raw).0)
467}
468
469fn header_fields(block: &[u8]) -> Vec<(String, String)> {
470 let text = String::from_utf8_lossy(block);
471 let mut out: Vec<(String, String)> = Vec::new();
472 for line in text.split('\n') {
473 let line = line.strip_suffix('\r').unwrap_or(line);
474 // A line that begins with a space or tab continues the field above it.
475 if line.starts_with(' ') || line.starts_with('\t') {
476 if let Some(last) = out.last_mut() {
477 last.1.push(' ');
478 last.1.push_str(line.trim());
479 }
480 continue;
481 }
482 if let Some((n, v)) = line.split_once(':') {
483 let name = n.trim().to_ascii_lowercase();
484 if !name.is_empty() {
485 out.push((name, v.trim().to_string()));
486 }
487 }
488 }
489 out
490}
491
492/// The first value of the field `name` (lower case) in `hs`.
493pub fn field(hs: &[(String, String)], name: &str) -> Option<String> {
494 hs.iter().find(|(n, _)| n == name).map(|(_, v)| v.clone())
495}
496
497fn media_type(ctype: &str) -> String {
498 ctype.split(';').next().unwrap_or("").trim().to_ascii_lowercase()
499}
500
501/// The value of the parameter `name` of a header field, matched in any case: a token, or a
502/// quoted string with its backslash escapes (RFC 2045 §5.1), or an RFC 2231 value, extended
503/// (`name*=charset'lang'%xx`) or continued (`name*0`, `name*1*`, ...), which is preferred to a
504/// plain one of the same name.
505pub fn param(header: &str, name: &str) -> Option<String> {
506 let ps = params(header);
507 let name = name.to_ascii_lowercase();
508 let ext = format!("{}*", name);
509 // RFC 2231 sections: (index, extended, value).
510 let mut secs: Vec<(usize, bool, &str)> = Vec::new();
511 for (k, v) in &ps {
512 if *k == ext {
513 secs.push((0, true, v));
514 } else if let Some(rest) = k.strip_prefix(&ext) {
515 let (n, extended) = match rest.strip_suffix('*') {
516 Some(n) => (n, true),
517 None => (rest, false),
518 };
519 if let Ok(i) = n.parse::<usize>() {
520 secs.push((i, extended, v));
521 }
522 }
523 }
524 let v = if secs.is_empty() {
525 ps.into_iter().find(|(k, _)| *k == name).map(|(_, v)| v)
526 } else {
527 secs.sort_by_key(|s| s.0);
528 let mut bytes = Vec::new();
529 let mut charset: Option<&str> = None;
530 for (i, (_, extended, v)) in secs.iter().enumerate() {
531 if !*extended {
532 bytes.extend_from_slice(v.as_bytes());
533 continue;
534 }
535 let mut v: &str = v;
536 // The first section names the charset and language: `utf-8'en'`.
537 if i == 0 {
538 let mut it = v.splitn(3, '\'');
539 if let (Some(cs), Some(_), Some(rest)) = (it.next(), it.next(), it.next()) {
540 if !cs.is_empty() { charset = Some(cs); }
541 v = rest;
542 }
543 }
544 bytes.extend(percent_decode(v));
545 }
546 Some(to_utf8(&bytes, charset))
547 };
548 v.filter(|v| !v.is_empty())
549}
550
551/// The parameters of a header field, after its first `;`, as `(lower-case name, value)` in
552/// order, a quoted value unquoted. A quote left open runs to the end.
553pub fn params(header: &str) -> Vec<(String, String)> {
554 let mut out = Vec::new();
555 let mut chars = header.chars().peekable();
556 // The field's own value, before the first `;`.
557 while let Some(c) = chars.next() {
558 if c == ';' { break; }
559 }
560 loop {
561 let mut name = String::new();
562 let mut has_value = false;
563 while let Some(c) = chars.next() {
564 match c {
565 ';' => break,
566 '=' => { has_value = true; break; }
567 _ => name.push(c),
568 }
569 }
570 let name = name.trim().to_ascii_lowercase();
571 if !has_value {
572 if chars.peek().is_none() { return out; }
573 continue;
574 }
575 while chars.peek().map(|c| c.is_whitespace()).unwrap_or(false) { chars.next(); }
576 let mut value = String::new();
577 if chars.peek() == Some(&'"') {
578 chars.next();
579 while let Some(c) = chars.next() {
580 match c {
581 '"' => break,
582 '\\' => if let Some(q) = chars.next() { value.push(q); },
583 _ => value.push(c),
584 }
585 }
586 // Anything after the closing quote, up to the next `;`, is not part of it.
587 while let Some(c) = chars.next() {
588 if c == ';' { break; }
589 }
590 } else {
591 while let Some(c) = chars.next() {
592 if c == ';' { break; }
593 value.push(c);
594 }
595 value = value.trim().to_string();
596 }
597 if !name.is_empty() {
598 out.push((name, value));
599 }
600 if chars.peek().is_none() { return out; }
601 }
602}
603
604/// `%xx` escapes to bytes; a `%` not followed by two hex digits stands for itself.
605fn percent_decode(s: &str) -> Vec<u8> {
606 let b = s.as_bytes();
607 let mut out = Vec::with_capacity(b.len());
608 let mut i = 0;
609 while i < b.len() {
610 if b[i] == b'%' && i + 2 < b.len() {
611 if let Some(x) = hex2(b[i + 1], b[i + 2]) {
612 out.push(x);
613 i += 3;
614 continue;
615 }
616 }
617 out.push(b[i]);
618 i += 1;
619 }
620 out
621}
622
623/// A body out of its Content-Transfer-Encoding and its charset, as UTF-8.
624pub fn decode_body(body: &[u8], encoding: &str, charset: Option<&str>) -> String {
625 to_utf8(&decode_transfer(body, encoding), charset)
626}
627
628/// A body out of its Content-Transfer-Encoding: base64 and quoted-printable decoded, any other
629/// (7bit, 8bit, binary, or none) as it is.
630pub fn decode_transfer(body: &[u8], encoding: &str) -> Vec<u8> {
631 match encoding.trim().to_ascii_lowercase().as_str() {
632 "base64" => decode_base64_lenient(body),
633 "quoted-printable" => decode_qp(body),
634 _ => body.to_vec(),
635 }
636}
637
638/// Bytes in `charset` as UTF-8. ISO-8859-1, ISO-8859-15 and Windows-1252 are transcoded,
639/// unless the bytes are already valid UTF-8; any other charset, or none, is read as UTF-8 with
640/// invalid sequences replaced.
641pub fn to_utf8(bytes: &[u8], charset: Option<&str>) -> String {
642 let cs = charset.map(|c| c.trim().trim_matches('"').to_ascii_lowercase()).unwrap_or_default();
643 // A body labelled with a single-byte charset that is valid UTF-8 with non-ASCII in it was
644 // written in UTF-8 and mislabelled: real Latin-1 text almost never forms valid UTF-8
645 // sequences (`é` and the like), so reading it by its label would only make mojibake.
646 if !bytes.is_ascii() {
647 if let Ok(s) = std::str::from_utf8(bytes) {
648 return s.to_string();
649 }
650 }
651 match cs.as_str() {
652 "iso-8859-1" | "iso8859-1" | "iso_8859-1" | "latin1" | "latin-1" | "l1"
653 => bytes.iter().map(|b| *b as char).collect(),
654 "iso-8859-15" | "iso8859-15" | "iso_8859-15" | "latin-9" | "latin9"
655 => bytes.iter().map(|b| latin9(*b)).collect(),
656 "windows-1252" | "cp1252" | "x-cp1252"
657 => bytes.iter().map(|b| cp1252(*b)).collect(),
658 _ => String::from_utf8_lossy(bytes).into_owned(),
659 }
660}
661
662/// ISO-8859-15: Latin-1 with eight places changed.
663fn latin9(b: u8) -> char {
664 match b {
665 0xA4 => '\u{20AC}',
666 0xA6 => '\u{0160}',
667 0xA8 => '\u{0161}',
668 0xB4 => '\u{017D}',
669 0xB8 => '\u{017E}',
670 0xBC => '\u{0152}',
671 0xBD => '\u{0153}',
672 0xBE => '\u{0178}',
673 _ => b as char,
674 }
675}
676
677/// Windows-1252: Latin-1 with 0x80 to 0x9F given printable characters; the five it leaves
678/// undefined stand as the C1 controls, as browsers read them.
679fn cp1252(b: u8) -> char {
680 const HIGH: [char; 32] = [
681 '\u{20AC}', '\u{0081}', '\u{201A}', '\u{0192}', '\u{201E}', '\u{2026}', '\u{2020}', '\u{2021}',
682 '\u{02C6}', '\u{2030}', '\u{0160}', '\u{2039}', '\u{0152}', '\u{008D}', '\u{017D}', '\u{008F}',
683 '\u{0090}', '\u{2018}', '\u{2019}', '\u{201C}', '\u{201D}', '\u{2022}', '\u{2013}', '\u{2014}',
684 '\u{02DC}', '\u{2122}', '\u{0161}', '\u{203A}', '\u{0153}', '\u{009D}', '\u{017E}', '\u{0178}',
685 ];
686 match b {
687 0x80..=0x9F => HIGH[(b - 0x80) as usize],
688 _ => b as char,
689 }
690}