encoded-words 0.2.0

Encoded Words for usage in MIME headers
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
//! Routines for manipulating RFC2047 encoded words.
//!
//! An ecoded word looks like this: `=?charset[*lang]?cte?encoded_string?=`.

use regex::bytes::{Captures, NoExpand, Regex};
use thiserror::Error;

use crate::charset::Charset;
use crate::defects::Defect;

// -- Quoted Printable

// regex based decoder.

lazy_static::lazy_static! {
    static ref Q_BYTE_RE_1: Regex = Regex::new(r"(_)").unwrap();
    static ref Q_BYTE_RE_2: Regex = Regex::new(r"=([a-fA-F0-9]{2})").unwrap();
}

fn decode_q<T: AsRef<[u8]>>(encoded: T) -> Vec<u8> {
    let one = Q_BYTE_RE_1.replace_all(encoded.as_ref(), NoExpand(b" "));
    Q_BYTE_RE_2
        .replace_all(one.as_ref(), |caps: &Captures| {
            hex::decode(caps[1].as_ref()).expect("invalid regex capture")
        })
        .to_vec()
}

fn write_q_byte<T: std::fmt::Write>(mut writer: T, byte: u8) -> std::fmt::Result {
    match byte {
        b' ' => writer.write_char('_'),
        b'-' | b'!' | b'*' | b'+' | b'/' | b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' => {
            writer.write_char(byte as char)
        }
        _ => write!(writer, "={:02X}", byte),
    }
}

fn encode_q<T: AsRef<[u8]>>(bstring: T) -> String {
    let mut out = String::with_capacity(bstring.as_ref().len());

    for byte in bstring.as_ref() {
        write_q_byte(&mut out, *byte).expect("String writes always succeed");
    }

    out
}

fn len_q<T: AsRef<[u8]>>(bstring: T) -> usize {
    bstring.as_ref().iter().copied().map(len_q_byte).sum()
}

fn len_q_byte(byte: u8) -> usize {
    match byte {
        b' ' => 1,
        b'-' | b'!' | b'*' | b'+' | b'/' | b'0'..=b'9' | b'a'..=b'z' | b'A'..=b'Z' => 1,
        _ => 3,
    }
}

// -- Base64

fn decode_b<T: AsRef<[u8]>>(encoded: T) -> (Vec<u8>, Vec<Defect>) {
    let mut defects = Vec::new();

    let config =
        base64::Config::new(base64::CharacterSet::Standard, true).decode_allow_trailing_bits(true);
    // First try the good case.
    match base64::decode_config(&encoded, config) {
        Ok(decoded) => {
            let pad_err = encoded.as_ref().len() % 4;
            if pad_err > 0 {
                defects.push(Defect::InvalidBase64Padding);
            }

            (decoded, defects)
        }
        Err(err) => match err {
            base64::DecodeError::InvalidByte(_offset, byte) => {
                defects.push(Defect::InvalidBase64Characters { byte });

                // filter out invalid characters
                let encoded: Vec<u8> = encoded
                    .as_ref()
                    .iter()
                    .copied()
                    .filter(|b| match b {
                        0..=42 => false,
                        43 => true,
                        44..=46 => false,
                        47..=57 => true,
                        58..=64 => false,
                        65..=90 => true,
                        91..=96 => false,
                        97..=122 => true,
                        _ => false,
                    })
                    .collect();

                if encoded.len() % 4 > 0 {
                    defects.push(Defect::InvalidBase64Padding);
                }

                match base64::decode_config(&encoded, config) {
                    Ok(decoded) => (decoded, defects),
                    Err(_err) => {
                        // giving up
                        (encoded.to_vec(), defects)
                    }
                }
            }
            base64::DecodeError::InvalidLastSymbol(_offset, _byte) => {
                unreachable!("config disables this error");
            }
            base64::DecodeError::InvalidLength => {
                // Nothing we can do
                defects.push(Defect::InvalidBase64Length);
                (encoded.as_ref().to_vec(), defects)
            }
        },
    }
}

fn encode_b<T: AsRef<[u8]>>(bstring: T) -> String {
    base64::encode(&bstring)
}

fn len_b<T: AsRef<[u8]>>(bstring: T) -> usize {
    let len = bstring.as_ref().len();
    let groups_of_3 = len / 3;
    let leftover = len % 3;

    // 4 bytes out for each 3 bytes (or nonzero fraction thereof) in.
    let padding_len = if leftover > 0 { 4 } else { 0 };
    groups_of_3 * 4 + padding_len
}

/// The result from decoding an encoded word.
#[derive(Debug, Clone, PartialEq)]
pub struct DecodingResult {
    pub decoded: String,
    pub charset: Charset,
    pub lang: String,
    pub defects: Vec<Defect>,
}

#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub enum DecodingError {
    #[error("Malformed input")]
    MalformedInput,
    #[error("Unknown charset {}", charset)]
    UnknownCharset { charset: String },
}

/// Decode encoded word and return (string, charset, lang, defects) tuple.
///
/// An RFC 2047/2243 encoded word has the form: `=?charset*lang?cte?encoded_string?=`
///
/// where '*lang' may be omitted but the other parts may not be.
///
/// This function expects exactly such a string (that is, it does not check the
/// syntax and may raise errors if the string is not well formed), and returns
/// the encoded_string decoded first from its Content Transfer Encoding and
/// then from the resulting bytes into unicode using the specified charset.  If
/// the cte-decoded string does not successfully decode using the specified
/// character set, a defect is added to the defects list and the unknown octets
/// are replaced by the unicode 'unknown' character \\uFDFF.
///
/// The specified charset and language are returned.  The default for language,
/// which is rarely if ever encountered, is the empty string.
pub fn decode<T: AsRef<str>>(ew: T) -> Result<DecodingResult, DecodingError> {
    let mut split = ew.as_ref().split('?');
    let _ = split.next().ok_or_else(|| DecodingError::MalformedInput)?;
    let charset = split.next().ok_or_else(|| DecodingError::MalformedInput)?;
    let cte = split.next().ok_or_else(|| DecodingError::MalformedInput)?;
    let cte_string = split.next().ok_or_else(|| DecodingError::MalformedInput)?;

    let (charset, lang) = if let Some(index) = charset.find('*') {
        let (charset, lang) = charset.split_at(index);
        (charset, &lang[1..])
    } else {
        (charset, "")
    };

    let mut defects = Vec::new();

    let charset = if charset == "latin-1" {
        // For some resason latin-1 is not repored
        Charset::for_label(b"latin1").unwrap()
    } else {
        match Charset::for_label(charset.as_bytes()) {
            Some(c) => c,
            None => {
                if charset != "unknown-8bit" {
                    defects.push(Defect::InvalidCharset {
                        charset: charset.into(),
                    })
                }
                Charset::Ascii
            }
        }
    };

    let cte = cte.to_lowercase();

    // Recover the original bytes and do CTE decoding.
    let (bstring, has_invalid_ascii) = Charset::Ascii.encode(cte_string);
    if has_invalid_ascii {
        defects.push(Defect::UndecodableBytes);
    }
    let (bstring, new_defects) = match cte.as_str() {
        "q" => (decode_q(bstring), Vec::new()),
        "b" => decode_b(bstring),
        _ => return Err(DecodingError::MalformedInput),
    };
    defects.extend_from_slice(&new_defects);

    // Turn the CTE decoded bytes into unicode.
    let (decoded, has_invalid_bytes) = charset.decode_without_bom_handling(&bstring);

    if has_invalid_bytes {
        defects.push(Defect::UndecodableBytes);
    }

    Ok(DecodingResult {
        decoded: decoded.into(),
        charset,
        lang: lang.into(),
        defects,
    })
}

/// Flags for types of header encodings
pub enum EncodingFlag {
    /// Quoted printable encoding.
    QuotedPrintable,
    /// Base64 encoding.
    Base64,
    /// The shorter of `QuotedPrintable` or `Base64`, but only for headers.
    Shortest,
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum Encoding {
    QuotedPrintable,
    Base64,
}

impl Encoding {
    pub fn decode<T: AsRef<[u8]>>(self, ew: T) -> (Vec<u8>, Vec<Defect>) {
        match self {
            Encoding::QuotedPrintable => (decode_q(ew), Vec::new()),
            Encoding::Base64 => decode_b(ew),
        }
    }

    pub fn encode<T: AsRef<[u8]>>(self, bstring: T) -> String {
        match self {
            Encoding::QuotedPrintable => encode_q(bstring),
            Encoding::Base64 => encode_b(bstring),
        }
    }
    pub fn char(self) -> char {
        match self {
            Encoding::QuotedPrintable => 'q',
            Encoding::Base64 => 'b',
        }
    }
}

/// Encode string using the CTE encoding that produces the shorter result.
///
/// Produces an RFC 2047/2243 encoded word of the form: `=?charset*lang?cte?encoded_string?=`
///
/// where '*lang' is omitted unless the 'lang' parameter is given a value.
/// Optional argument charset (defaults to utf-8) specifies the charset to use
/// to encode the string to binary before CTE encoding it.  Optional argument
/// 'encoding' is the cte specifier for the encoding that should be used ('q'
/// or 'b'); if it is None (the default) the encoding which produces the
/// shortest encoded sequence is used, except that 'q' is preferred if it is up
/// to five characters longer.  Optional argument 'lang' (default '') gives the
/// RFC 2243 language string to specify in the encoded word.
pub fn encode<T: AsRef<str>>(
    ew: T,
    charset: Option<Charset>,
    encoding_flag: EncodingFlag,
    lang: Option<&str>,
) -> String {
    // TODO: is using Charset the right option here? Need to handle utf-7 somehow.
    let charset = charset.unwrap_or_else(|| Charset::Encoding(encoding_rs::UTF_8));
    let (bstring, _) = charset.encode(ew.as_ref());

    let encoding = match encoding_flag {
        EncodingFlag::Base64 => Encoding::Base64,
        EncodingFlag::QuotedPrintable => Encoding::QuotedPrintable,
        EncodingFlag::Shortest => {
            let q_len = len_q(&bstring);
            let b_len = len_b(&bstring);

            // Bias toward q. 5 is arbitrary.
            if q_len as isize - (b_len as isize) < 5 {
                Encoding::QuotedPrintable
            } else {
                Encoding::Base64
            }
        }
    };

    let encoded = encoding.encode(&bstring);
    if let Some(lang) = lang {
        format!(
            "=?{}*{}?{}?{}?=",
            charset.name().to_lowercase(),
            lang,
            encoding.char(),
            encoded
        )
    } else {
        format!(
            "=?{}?{}?{}?=",
            charset.name().to_lowercase(),
            encoding.char(),
            encoded
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_decode_q_no_encoded() {
        assert_eq!(&decode_q(b"foobar"), b"foobar");
    }

    #[test]
    fn test_decode_q_spaces() {
        assert_eq!(&decode_q(b"foo=20bar=20"), b"foo bar ");
        assert_eq!(&decode_q(b"foo_bar_"), b"foo bar ");
    }

    #[test]
    fn test_decode_q_encoded() {
        assert_eq!(&decode_q(b"foo=20=20=21=2Cbar"), b"foo  !,bar");
    }

    #[test]
    fn test_decode_b_simple() {
        assert_eq!(decode_b(b"Zm9v"), (b"foo".to_vec(), Vec::new()));
    }

    #[test]
    fn test_decode_b_missing_padding() {
        // 1 missing padding character
        assert_eq!(
            decode_b(b"dmk"),
            (b"vi".to_vec(), vec![Defect::InvalidBase64Padding])
        );
        // 2 missing padding characters
        assert_eq!(
            decode_b(b"dg"),
            (b"v".to_vec(), vec![Defect::InvalidBase64Padding])
        );
    }

    #[test]
    fn test_decode_b_invalid_character() {
        assert_eq!(
            decode_b(b"dm\x01k==="),
            (
                b"vi".to_vec(),
                vec![
                    Defect::InvalidBase64Characters { byte: b'\x01' },
                    Defect::InvalidBase64Padding
                ]
            )
        );
    }

    #[test]
    fn test_decode_b_invalid_character_and_bad_padding() {
        assert_eq!(
            decode_b(b"dm\x01k"),
            (
                b"vi".to_vec(),
                vec![
                    Defect::InvalidBase64Characters { byte: b'\x01' },
                    Defect::InvalidBase64Padding
                ]
            )
        );
    }

    #[test]
    fn test_decode_b_invalid_length() {
        assert_eq!(
            decode_b(b"abcde"),
            (b"abcde".to_vec(), vec![Defect::InvalidBase64Length])
        );
    }

    #[test]
    fn test_decode_wrong_format_input() {
        assert_eq!(decode("=?badone?="), Err(DecodingError::MalformedInput));
        assert_eq!(decode("=?"), Err(DecodingError::MalformedInput));
        assert_eq!(decode(""), Err(DecodingError::MalformedInput));
        assert_eq!(
            decode("=?utf-9?X?somevalue?="),
            Err(DecodingError::MalformedInput)
        );
    }

    #[test]
    fn test_decode_simple_q() {
        assert_eq!(
            decode("=?us-ascii?q?foo?=").unwrap(),
            DecodingResult {
                decoded: "foo".into(),
                charset: Charset::Ascii,
                lang: "".into(),
                defects: Vec::new(),
            }
        );
    }

    #[test]
    fn test_decode_simple_b() {
        assert_eq!(
            decode("=?us-ascii?b?dmk=?=").unwrap(),
            DecodingResult {
                decoded: "vi".into(),
                charset: Charset::Ascii,
                lang: "".into(),
                defects: Vec::new(),
            }
        );
    }

    #[test]
    fn test_decode_case_ignored_q() {
        assert_eq!(
            decode("=?us-ascii?Q?foo?=").unwrap(),
            DecodingResult {
                decoded: "foo".into(),
                charset: Charset::Ascii,
                lang: "".into(),
                defects: Vec::new(),
            }
        );
    }

    #[test]
    fn test_decode_case_ignored_b() {
        assert_eq!(
            decode("=?us-ascii?B?dmk=?=").unwrap(),
            DecodingResult {
                decoded: "vi".into(),
                charset: Charset::Ascii,
                lang: "".into(),
                defects: Vec::new(),
            }
        );
    }

    #[test]
    fn test_decode_non_trivial_q() {
        assert_eq!(
            decode("=?latin-1?q?=20F=fcr=20Elise=20?=").unwrap(),
            DecodingResult {
                decoded: " Für Elise ".into(),
                charset: Charset::for_label(b"latin1").unwrap(),
                lang: "".into(),
                defects: Vec::new(),
            }
        );
    }

    #[test]
    fn test_decode_escaped_bytes_preserved_q() {
        assert_eq!(
            decode("=?us-ascii?q?=20\u{AC}foo?=").unwrap(),
            DecodingResult {
                decoded: " \u{AC}foo".into(),
                charset: Charset::Ascii,
                lang: "".into(),
                defects: vec![/*Defect::UndecodableBytes*/],
            }
        );
    }

    #[test]
    fn test_decode_undecodable_bytes_ignored_with_defect_b() {
        assert_eq!(
            decode("=?us-ascii?b?dm\u{AC}k?=").unwrap(),
            DecodingResult {
                decoded: "vi".into(),
                charset: Charset::Ascii,
                lang: "".into(),
                defects: vec![
                    Defect::InvalidBase64Characters { byte: 172 },
                    Defect::InvalidBase64Padding
                ],
            }
        );
    }

    #[test]
    fn test_decode_invalid_bytes_ignored_with_defect_b() {
        assert_eq!(
            decode("=?us-ascii?b?dm\x01k===?=").unwrap(),
            DecodingResult {
                decoded: "vi".into(),
                charset: Charset::Ascii,
                lang: "".into(),
                defects: vec![
                    Defect::InvalidBase64Characters { byte: 1 },
                    Defect::InvalidBase64Padding
                ],
            }
        );
    }

    #[test]
    fn test_decode_padding_defect_b() {
        assert_eq!(
            decode("=?us-ascii?b?dmk?=").unwrap(),
            DecodingResult {
                decoded: "vi".into(),
                charset: Charset::Ascii,
                lang: "".into(),
                defects: vec![Defect::InvalidBase64Padding],
            }
        );
    }

    #[test]
    fn test_decode_nonnull_lang() {
        assert_eq!(
            decode("=?us-ascii*jive?q?test?=").unwrap(),
            DecodingResult {
                decoded: "test".into(),
                charset: Charset::Ascii,
                lang: "jive".into(),
                defects: vec![],
            }
        );
    }

    #[test]
    fn test_decode_unknown_8bit_charset() {
        assert_eq!(
            decode("=?unknown-8bit?q?foo=ACbar?=").unwrap(),
            DecodingResult {
                decoded: "foo\u{ac}bar".into(),
                charset: Charset::Unknown8Bit,
                lang: "".into(),
                defects: vec![],
            }
        );
    }

    #[test]
    fn test_decode_unknown_charset() {
        assert_eq!(
            decode("=?foobar?q?foo=ACbar?=").unwrap(),
            DecodingResult {
                decoded: "foo\u{ac}bar".into(),
                charset: Charset::Ascii,
                lang: "".into(),
                defects: vec![Defect::InvalidCharset {
                    charset: "foobar".into()
                }],
            }
        );
    }

    #[test]
    fn test_decode_nonascii_q() {
        assert_eq!(
            decode("=?utf-8?q?=C3=89ric?=").unwrap(),
            DecodingResult {
                decoded: "Éric".into(),
                charset: Charset::for_label(b"utf-8").unwrap(),
                lang: "".into(),
                defects: vec![],
            }
        );
    }

    #[test]
    fn test_encode_q_all_safe() {
        assert_eq!(&encode_q(b"foobar"), "foobar");
    }

    #[test]
    fn test_encode_q_spaces() {
        assert_eq!(&encode_q(b"foo bar "), "foo_bar_");
    }

    #[test]
    fn test_encode_q_encodables() {
        assert_eq!(&encode_q(b"foo  ,,bar"), "foo__=2C=2Cbar");
        assert_eq!(len_q(b"foo  ,,bar"), b"foo__=2C=2Cbar".len());
    }

    #[test]
    fn test_encode_b_simple() {
        assert_eq!(&encode_b(b"foo"), "Zm9v");
        assert_eq!(len_b(b"foo"), b"Zm9v".len());
    }

    #[test]
    fn test_encode_b_padding() {
        assert_eq!(&encode_b(b"vi"), "dmk=");
        assert_eq!(len_b(b"vi"), b"dmk=".len());
    }

    #[test]
    fn test_encode_simple_q() {
        assert_eq!(
            &encode(
                "foo",
                Some(encoding_rs::UTF_8.into()),
                EncodingFlag::QuotedPrintable,
                None,
            ),
            "=?utf-8?q?foo?="
        );
    }

    #[test]
    fn test_encode_simple_b() {
        assert_eq!(
            &encode(
                "foo",
                Some(encoding_rs::UTF_8.into()),
                EncodingFlag::Base64,
                None,
            ),
            "=?utf-8?b?Zm9v?="
        );
    }

    #[test]
    fn test_encode_auto_q() {
        assert_eq!(
            &encode(
                "foo",
                Some(encoding_rs::UTF_8.into()),
                EncodingFlag::Shortest,
                None,
            ),
            "=?utf-8?q?foo?="
        );
    }

    #[test]
    fn test_encode_auto_q_if_short_mostly_safe() {
        assert_eq!(
            &encode(
                "vi.",
                Some(encoding_rs::UTF_8.into()),
                EncodingFlag::Shortest,
                None,
            ),
            "=?utf-8?q?vi=2E?="
        );
    }

    #[test]
    fn test_encode_auto_b_if_enough_unsafe() {
        assert_eq!(
            &encode(
                ".....",
                Some(encoding_rs::UTF_8.into()),
                EncodingFlag::Shortest,
                None,
            ),
            "=?utf-8?b?Li4uLi4=?="
        );
    }

    #[test]
    fn test_encode_auto_b_if_long_unsafe() {
        assert_eq!(
            &encode(
                "vi.vi.vi.vi.vi.",
                Some(encoding_rs::UTF_8.into()),
                EncodingFlag::Shortest,
                None,
            ),
            "=?utf-8?b?dmkudmkudmkudmkudmku?="
        );
    }

    #[test]
    fn test_encode_auto_q_if_mostly_safe() {
        assert_eq!(
            &encode(
                "vi vi vi.vi ",
                Some(encoding_rs::UTF_8.into()),
                EncodingFlag::Shortest,
                None,
            ),
            "=?utf-8?q?vi_vi_vi=2Evi_?="
        );
    }

    #[test]
    fn test_encode_utf8_default() {
        assert_eq!(
            &encode("foo", None, EncodingFlag::Shortest, None,),
            "=?utf-8?q?foo?="
        );
    }

    #[test]
    fn test_encode_lang() {
        assert_eq!(
            &encode("foo", None, EncodingFlag::Shortest, Some("jive")),
            "=?utf-8*jive?q?foo?="
        );
    }
}