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
/*
 * Copyright Stalwart Labs Ltd. See the COPYING
 * file at the top-level directory of this distribution.
 *
 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
 * option. This file may not be copied, modified, or distributed
 * except according to those terms.
 */

use std::borrow::Cow;

use crate::parsers::MessageStream;

#[derive(PartialEq, Debug)]
enum QuotedPrintableState {
    None,
    Eq,
    Hex1,
}

pub fn quoted_printable_decode(bytes: &[u8]) -> Option<Vec<u8>> {
    let mut buf = Vec::with_capacity(bytes.len());

    let mut state = QuotedPrintableState::None;
    let mut hex1 = 0;

    for &ch in bytes {
        match ch {
            b'=' => {
                if let QuotedPrintableState::None = state {
                    state = QuotedPrintableState::Eq
                } else {
                    return None;
                }
            }
            b'\n' => {
                if QuotedPrintableState::Eq == state {
                    state = QuotedPrintableState::None;
                } else {
                    buf.push(b'\n');
                }
            }
            b'\r' => (),
            _ => match state {
                QuotedPrintableState::None => {
                    buf.push(ch);
                }
                QuotedPrintableState::Eq => {
                    hex1 = {
                        #[cfg(feature = "ludicrous_mode")]
                        unsafe {
                            *HEX_MAP.get_unchecked(ch as usize)
                        }
                        #[cfg(not(feature = "ludicrous_mode"))]
                        HEX_MAP[ch as usize]
                    };

                    if hex1 != -1 {
                        state = QuotedPrintableState::Hex1;
                    } else {
                        return None;
                    }
                }
                QuotedPrintableState::Hex1 => {
                    #[cfg(feature = "ludicrous_mode")]
                    let hex2 = unsafe { *HEX_MAP.get_unchecked(ch as usize) };
                    #[cfg(not(feature = "ludicrous_mode"))]
                    let hex2 = HEX_MAP[ch as usize];

                    state = QuotedPrintableState::None;
                    if hex2 != -1 {
                        buf.push(((hex1 as u8) << 4) | hex2 as u8);
                    } else {
                        return None;
                    }
                }
            },
        }
    }

    buf.into()
}

#[inline(always)]
pub fn quoted_printable_decode_char(hex1: u8, hex2: u8) -> Option<u8> {
    #[cfg(feature = "ludicrous_mode")]
    {
        let hex1 = unsafe { *HEX_MAP.get_unchecked(hex1 as usize) };
        let hex2 = unsafe { *HEX_MAP.get_unchecked(hex2 as usize) };
        if hex1 != -1 && hex2 != -1 {
            (((hex1 as u8) << 4) | hex2 as u8).into()
        } else {
            None
        }
    }
    #[cfg(not(feature = "ludicrous_mode"))]
    {
        let hex1 = HEX_MAP[hex1 as usize];
        let hex2 = HEX_MAP[hex2 as usize];
        if hex1 != -1 && hex2 != -1 {
            (((hex1 as u8) << 4) | hex2 as u8).into()
        } else {
            None
        }
    }
}

impl<'x> MessageStream<'x> {
    pub fn decode_quoted_printable_mime(&mut self, boundary: &[u8]) -> (usize, Cow<'x, [u8]>) {
        let mut buf = Vec::with_capacity(128);

        let mut state = QuotedPrintableState::None;
        let mut hex1 = 0;
        let mut last_ch = 0;
        let mut before_last_ch = 0;
        let mut ws_count = 0;
        let mut end_pos = self.offset();
        let mut crlf = b"\n".as_ref();

        self.checkpoint();

        while let Some(&ch) = self.next() {
            match ch {
                b'=' => {
                    if let QuotedPrintableState::None = state {
                        state = QuotedPrintableState::Eq
                    } else {
                        self.restore();
                        return (usize::MAX, b""[..].into());
                    }
                }
                b'\n' => {
                    end_pos = if last_ch == b'\r' {
                        self.offset() - 2
                    } else {
                        self.offset() - 1
                    };
                    if QuotedPrintableState::Eq == state {
                        state = QuotedPrintableState::None;
                    } else {
                        if ws_count > 0 {
                            buf.truncate(buf.len() - ws_count);
                        }
                        buf.extend_from_slice(crlf);
                    }
                    ws_count = 0;
                }
                b'\r' => {
                    crlf = b"\r\n".as_ref();
                }
                b'-' if !boundary.is_empty() && last_ch == b'-' && self.try_skip(boundary) => {
                    if before_last_ch == b'\n' {
                        buf.truncate(buf.len() - (crlf.len() + 1));
                    } else {
                        buf.truncate(buf.len() - 1);
                        end_pos = self.offset() - boundary.len() - 2;
                    }

                    return (end_pos, buf.into());
                }
                _ => match state {
                    QuotedPrintableState::None => {
                        if ch.is_ascii_whitespace() {
                            ws_count += 1;
                        } else {
                            ws_count = 0;
                        }
                        buf.push(ch);
                    }
                    QuotedPrintableState::Eq => {
                        hex1 = {
                            #[cfg(feature = "ludicrous_mode")]
                            unsafe {
                                *HEX_MAP.get_unchecked(ch as usize)
                            }
                            #[cfg(not(feature = "ludicrous_mode"))]
                            HEX_MAP[ch as usize]
                        };
                        if hex1 != -1 {
                            state = QuotedPrintableState::Hex1;
                        } else if !ch.is_ascii_whitespace() {
                            self.restore();
                            return (usize::MAX, b""[..].into());
                        }
                    }
                    QuotedPrintableState::Hex1 => {
                        #[cfg(feature = "ludicrous_mode")]
                        let hex2 = unsafe { *HEX_MAP.get_unchecked(ch as usize) };
                        #[cfg(not(feature = "ludicrous_mode"))]
                        let hex2 = HEX_MAP[ch as usize];

                        state = QuotedPrintableState::None;
                        if hex2 != -1 {
                            buf.push(((hex1 as u8) << 4) | hex2 as u8);
                            ws_count = 0;
                        } else {
                            self.restore();
                            return (usize::MAX, b""[..].into());
                        }
                    }
                },
            }

            before_last_ch = last_ch;
            last_ch = ch;
        }

        (
            if boundary.is_empty() {
                self.offset()
            } else {
                self.restore();
                usize::MAX
            },
            buf.into(),
        )
    }

    pub fn decode_quoted_printable_word(&mut self) -> Option<Vec<u8>> {
        let mut buf = Vec::with_capacity(64);

        let mut state = QuotedPrintableState::None;
        let mut hex1 = 0;

        while let Some(&ch) = self.next() {
            match ch {
                b'=' => {
                    if let QuotedPrintableState::None = state {
                        state = QuotedPrintableState::Eq
                    } else {
                        break;
                    }
                }
                b'?' => {
                    if let Some(b'=') = self.peek() {
                        self.next();
                        return buf.into();
                    } else {
                        buf.push(b'?');
                    }
                }
                b'\n' => {
                    if let Some(b' ' | b'\t') = self.peek() {
                        loop {
                            self.next();
                            if !self.peek_next_is_space() {
                                break;
                            }
                        }
                    } else {
                        break;
                    }
                }
                b'_' => {
                    buf.push(b' ');
                }
                b'\r' => (),
                _ => match state {
                    QuotedPrintableState::None => {
                        buf.push(ch);
                    }
                    QuotedPrintableState::Eq => {
                        hex1 = {
                            #[cfg(feature = "ludicrous_mode")]
                            unsafe {
                                *HEX_MAP.get_unchecked(ch as usize)
                            }
                            #[cfg(not(feature = "ludicrous_mode"))]
                            HEX_MAP[ch as usize]
                        };
                        if hex1 != -1 {
                            state = QuotedPrintableState::Hex1;
                        } else {
                            // Failed
                            break;
                        }
                    }
                    QuotedPrintableState::Hex1 => {
                        #[cfg(feature = "ludicrous_mode")]
                        let hex2 = unsafe { *HEX_MAP.get_unchecked(ch as usize) };
                        #[cfg(not(feature = "ludicrous_mode"))]
                        let hex2 = HEX_MAP[ch as usize];

                        state = QuotedPrintableState::None;
                        if hex2 != -1 {
                            buf.push(((hex1 as u8) << 4) | hex2 as u8);
                        } else {
                            // Failed
                            break;
                        }
                    }
                },
            }
        }

        None
    }
}
#[cfg(test)]
mod tests {
    use crate::parsers::MessageStream;

    #[test]
    fn decode_quoted_printable() {
        for (encoded_str, expected_result) in [
            (
                concat!(
                    "J'interdis aux marchands de vanter trop leurs marchandises. ",
                    "Car ils se font=\nvite p=C3=A9dagogues et t'enseignent comme but ce ",
                    "qui n'est par essence qu=\n'un moyen, et te trompant ainsi sur la route ",
                    "=C3=A0 suivre les voil=C3=\n=A0 bient=C3=B4t qui te d=C3=A9gradent, car ",
                    "si leur musique est vulgaire il=\ns te fabriquent pour te la vendre une ",
                    "=C3=A2me vulgaire.\n=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry, ",
                    "Citadelle (1948)"
                ),
                concat!(
                    "J'interdis aux marchands de vanter trop leurs marchandises. ",
                    "Car ils se fontvite pédagogues et t'enseignent comme but ce qui ",
                    "n'est par essence qu'un moyen, et te trompant ainsi sur la route ",
                    "à suivre les voilà bientôt qui te dégradent, car si leur musique ",
                    "est vulgaire ils te fabriquent pour te la vendre une âme vulgaire.\n",
                    "— Antoine de Saint-Exupéry, Citadelle (1948)"
                ),
            ),
            (
                "=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry",
                "— Antoine de Saint-Exupéry",
            ),
            (
                concat!(
                    "Die Hasen klagten einst uber ihre Lage; \"wir ",
                    "leben\", sprach ein=\r\n Redner, \"in steter Furcht vor Menschen",
                    " und Tieren, eine Beute der Hunde,=\r\n der\n"
                ),
                concat!(
                    "Die Hasen klagten einst uber ihre Lage; \"wir leben\", ",
                    "sprach ein Redner, \"in steter Furcht vor Menschen und ",
                    "Tieren, eine Beute der Hunde, der\n"
                ),
            ),
            ("\n\n", "\n\n"),
        ] {
            assert_eq!(
                super::quoted_printable_decode(encoded_str.as_bytes()).unwrap_or_default(),
                expected_result.as_bytes(),
                "Failed for {encoded_str:?}",
            );
        }
    }

    #[test]
    fn decode_quoted_printable_mime() {
        for (encoded_str, expected_result) in [
            (
                "=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry\n--boundary",
                "— Antoine de Saint-Exupéry",
            ),
            (
                "=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry\n--\n--boundary",
                "— Antoine de Saint-Exupéry\n--",
            ),
            (
                "=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry=\n--\n--boundary",
                "— Antoine de Saint-Exupéry--",
            ),
            (
                concat!(
                    "J'interdis aux marchands de vanter trop leurs marchandises. ",
                    "Car ils se font=\nvite p=C3=A9dagogues et t'enseignent comme but ce ",
                    "qui n'est par essence qu=\n'un moyen, et te trompant ainsi sur la route ",
                    "=C3=A0 suivre les voil=C3=\n=A0 bient=C3=B4t qui te d=C3=A9gradent, car ",
                    "si leur musique est vulgaire il=\ns te fabriquent pour te la vendre une ",
                    "=C3=A2me vulgaire.\n=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry, ",
                    "Citadelle (1948)\r\n--boundary--"
                ),
                concat!(
                    "J'interdis aux marchands de vanter trop leurs marchandises. ",
                    "Car ils se fontvite pédagogues et t'enseignent comme but ce qui ",
                    "n'est par essence qu'un moyen, et te trompant ainsi sur la route ",
                    "à suivre les voilà bientôt qui te dégradent, car si leur musique ",
                    "est vulgaire ils te fabriquent pour te la vendre une âme vulgaire.\n",
                    "— Antoine de Saint-Exupéry, Citadelle (1948)"
                ),
            ),
            (
                "=E2=80=94=E2=80=89Antoine de Saint-Exup=C3=A9ry\n--\n--boundary",
                "— Antoine de Saint-Exupéry\n--",
            ),
            (
                concat!(
                    "Die Hasen klagten einst uber ihre Lage; \"wir ",
                    "leben\", sprach ein=\r\n Redner, \"in steter Furcht vor Menschen",
                    " und Tieren, eine Beute der Hunde,=\r\n der\r\n\r\n--boundary \n"
                ),
                concat!(
                    "Die Hasen klagten einst uber ihre Lage; \"wir leben\", ",
                    "sprach ein Redner, \"in steter Furcht vor Menschen und ",
                    "Tieren, eine Beute der Hunde, der\r\n"
                ),
            ),
            (
                concat!(
                    "hello  \r\nbar=\r\n\r\nfoo\t=\r\nbar\r\nfoo\t \t= \r\n=62\r\nfoo = ",
                    "\t\r\nbar\r\nfoo =\r\n=62\r\nfoo  \r\nbar=\r\n\r\nfoo_bar\r\n\r\n--boundary"
                ),
                "hello\r\nbar\r\nfoo\tbar\r\nfoo\t \tb\r\nfoo bar\r\nfoo b\r\nfoo\r\nbar\r\nfoo_bar\r\n",
            ),
        ] {
            let mut s = MessageStream::new(encoded_str.as_bytes());
            let (bytes_read, result) = s.decode_quoted_printable_mime(b"boundary");
            assert_ne!(bytes_read, usize::MAX);
            assert_eq!(
                std::str::from_utf8(result.as_ref()).unwrap(),
                expected_result,
                "Failed for {encoded_str:?}",
            );
        }
    }

    #[test]
    fn decode_quoted_printable_word() {
        for (encoded_str, expected_result) in [
            ("this=20is=20some=20text?=", "this is some text"),
            ("this=20is=20\n  some=20text?=", "this is some text"),
            ("this is some text?=", "this is some text"),
            ("Keith_Moore?=", "Keith Moore"),
            ("=2=123?=", ""),
            ("= 20?=", ""),
            ("=====?=", ""),
            ("=20=20=XX?=", ""),
            ("=AX?=", ""),
            ("=\n=\n==?=", ""),
            ("=\r=1z?=", ""),
            ("=|?=", ""),
            ("????????=", "???????"),
            ("\n\n", ""),
        ] {
            let mut s = MessageStream::new(encoded_str.as_bytes());

            assert_eq!(
                s.decode_quoted_printable_word().unwrap_or_default(),
                expected_result.as_bytes(),
                "Failed for {encoded_str:?}",
            );
        }
    }
}

/*
 * Adapted from Daniel Lemire's source:
 * https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2019/04/17/hexparse.cpp
 *
 */

pub static HEX_MAP: &[i8] = &[
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 10,
    11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
];