encodify 1.0.2

Fast Base64, Base32, Quoted-Printable, RFC 2047, UTF-7 and PEM encoding and decoding
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
/*
 * SPDX-FileCopyrightText: 2026 Stalwart Labs LLC <hello@stalw.art>
 *
 * SPDX-License-Identifier: Apache-2.0 OR MIT
 */

//! Hexadecimal escapes shared by quoted-printable (`=XX`), percent-encoding
//! (`%XX`) and xtext (`+XX`).
//!
//! [`decode_pair`] is the primitive every decoder in the crate uses. The
//! [`HexEscape`] engines apply a whole scheme: bytes outside the scheme's
//! safe set are written as a marker followed by two uppercase hexadecimal
//! digits, and decoding turns every marker and the two digits after it back
//! into a byte.
//!
//! ```
//! use encodify::hex;
//! use std::borrow::Cow;
//!
//! # fn main() -> Result<(), encodify::Error> {
//! assert_eq!(hex::PERCENT.encode("naïve.txt"), "na%C3%AFve.txt");
//! assert_eq!(hex::PERCENT.decode("na%c3%afve.txt")?, "naïve.txt".as_bytes());
//!
//! assert_eq!(
//!     hex::XTEXT.encode("rfc822;a+b@example.org"),
//!     "rfc822;a+2Bb@example.org"
//! );
//! assert!(matches!(
//!     hex::XTEXT.decode("rfc822;user@example.org")?,
//!     Cow::Borrowed(_)
//! ));
//! # Ok(())
//! # }
//! ```

#![allow(unsafe_code)]

use crate::{Buffer, Error};
use std::{borrow::Cow, mem::MaybeUninit};

const INVALID: u8 = 0xff;

static HEX: [u8; 256] = {
    let mut table = [INVALID; 256];
    let mut byte = 0;
    while byte < 256 {
        table[byte] = match byte as u8 {
            digit @ b'0'..=b'9' => digit - b'0',
            upper @ b'A'..=b'F' => upper - b'A' + 10,
            lower @ b'a'..=b'f' => lower - b'a' + 10,
            _ => INVALID,
        };
        byte += 1;
    }
    table
};

/// Decodes two hexadecimal digits (either case) into a byte.
#[inline(always)]
pub const fn decode_pair(high: u8, low: u8) -> Option<u8> {
    let high = HEX[high as usize];
    let low = HEX[low as usize];
    if (high | low) & 0xf0 == 0 {
        Some((high << 4) | low)
    } else {
        None
    }
}

const UPPER: &[u8; 16] = b"0123456789ABCDEF";
const ENTRY_LEN: usize = 3;
const LEN_SHIFT: usize = 8 * ENTRY_LEN;
const ESCAPED: usize = 3;
pub(crate) const BLOCK: usize = 16;
pub(crate) const BLOCK_ROOM: usize = ESCAPED * BLOCK + 1;
#[cfg(encodify_simd)]
pub(crate) const SPARSE_ESCAPES: usize = 4;
#[cfg(encodify_simd)]
pub(crate) const SPARSE_LEN: usize = BLOCK + (ESCAPED - 1) * SPARSE_ESCAPES;

pub(crate) struct EscapeTable {
    entries: [[u8; 4]; 256],
}

impl EscapeTable {
    pub(crate) const fn escaped(marker: u8, byte: u8) -> [u8; 4] {
        [
            marker,
            UPPER[(byte >> 4) as usize],
            UPPER[(byte & 0x0f) as usize],
            ESCAPED as u8,
        ]
    }

    pub(crate) const fn new(marker: u8, safe: &[bool; 256]) -> Self {
        let mut entries = [[0u8; 4]; 256];
        let mut byte = 0;
        while byte < 256 {
            entries[byte] = if safe[byte] {
                [byte as u8, 0, 0, 1]
            } else {
                Self::escaped(marker, byte as u8)
            };
            byte += 1;
        }
        EscapeTable { entries }
    }

    #[inline(always)]
    const fn is_continuation(byte: u8) -> bool {
        (byte as i8) < -0x40
    }

    pub(crate) const fn with(mut self, byte: u8, literal: u8) -> Self {
        self.entries[byte as usize] = [literal, 0, 0, 1];
        self
    }

    #[inline(always)]
    pub(crate) fn word(&self, byte: u8) -> u32 {
        u32::from_le_bytes(self.entries[byte as usize])
    }

    pub(crate) const fn byte_len(&self, byte: u8) -> usize {
        self.entries[byte as usize][ENTRY_LEN] as usize
    }

    pub(crate) const fn strs(&'static self) -> [&'static str; 256] {
        let mut strs = [""; 256];
        let mut byte = 0;
        while byte < 256 {
            let entry = &self.entries[byte];
            let (text, _) = entry.as_slice().split_at(entry[ENTRY_LEN] as usize);
            strs[byte] = match std::str::from_utf8(text) {
                Ok(text) => text,
                Err(_) => "",
            };
            byte += 1;
        }
        strs
    }

    #[inline(always)]
    pub(crate) fn encode_block(
        &self,
        block: &[u8; BLOCK],
        window: &mut [MaybeUninit<u8>; BLOCK_ROOM],
    ) -> usize {
        block.iter().fold(0, |written, &byte| {
            let word = self.word(byte);
            if let Some(slot) = window
                .get_mut(written..)
                .and_then(|rest| rest.first_chunk_mut::<4>())
            {
                *slot = word.to_le_bytes().map(MaybeUninit::new);
            }
            written + (word >> LEN_SHIFT) as usize
        })
    }

    #[inline(always)]
    pub(crate) fn encode_blocks(
        &self,
        input: &[u8],
        dst: &mut [MaybeUninit<u8>],
        budget: usize,
    ) -> (usize, usize) {
        let (blocks, _) = input.as_chunks::<BLOCK>();
        let mut read = 0;
        let mut written = 0;
        for block in blocks {
            let Some(window) = dst
                .get_mut(written..)
                .and_then(|rest| rest.first_chunk_mut::<BLOCK_ROOM>())
            else {
                break;
            };
            let len = self.encode_block(block, window);
            if len > budget - written {
                break;
            }
            written += len;
            read += BLOCK;
        }
        (read, written)
    }

    #[inline(always)]
    pub(crate) fn encode_into(&self, input: &[u8], dst: &mut [MaybeUninit<u8>]) -> usize {
        let (read, written) = self.encode_blocks(input, dst, usize::MAX);
        written
            + self.encode_tail(
                input.get(read..).unwrap_or_default(),
                dst.get_mut(written..).unwrap_or_default(),
            )
    }

    #[inline(always)]
    fn encode_tail(&self, input: &[u8], dst: &mut [MaybeUninit<u8>]) -> usize {
        let written = input.iter().fold(0, |written, &byte| {
            let word = self.word(byte);
            let len = (word >> LEN_SHIFT) as usize;
            Self::put(word, len, dst.get_mut(written..).unwrap_or_default());
            written + len
        });
        debug_assert!(written <= dst.len());
        written
    }

    #[inline(always)]
    pub(crate) fn encode_within(
        &self,
        input: &[u8],
        start: (usize, usize),
        budget: usize,
        chars: bool,
        dst: &mut [MaybeUninit<u8>],
    ) -> (usize, usize) {
        let (mut read, mut written) = start;
        let budget = budget.min(dst.len());
        let mut mark = match chars {
            true => self.char_start(input, start),
            false => start,
        };
        for &byte in input.get(read..).unwrap_or_default() {
            if chars && !Self::is_continuation(byte) {
                mark = (read, written);
            }
            let word = self.word(byte);
            let len = (word >> LEN_SHIFT) as usize;
            if len > budget - written {
                return match chars {
                    true => mark,
                    false => (read, written),
                };
            }
            Self::put(word, len, dst.get_mut(written..).unwrap_or_default());
            read += 1;
            written += len;
        }
        (read, written)
    }

    fn char_start(&self, input: &[u8], (read, written): (usize, usize)) -> (usize, usize) {
        if !input
            .get(read)
            .is_some_and(|&byte| Self::is_continuation(byte))
        {
            return (read, written);
        }
        let before = input.get(..read).unwrap_or_default();
        let lead = before
            .iter()
            .rposition(|&byte| !Self::is_continuation(byte))
            .unwrap_or_default();
        let split = before.get(lead..).unwrap_or_default();
        (lead, written.saturating_sub(self.encoded_len(split)))
    }

    #[inline(always)]
    fn put(word: u32, len: usize, dst: &mut [MaybeUninit<u8>]) {
        match dst.first_chunk_mut::<4>() {
            Some(slot) => *slot = word.to_le_bytes().map(MaybeUninit::new),
            None => {
                for (slot, byte) in dst.iter_mut().zip(word.to_le_bytes().into_iter().take(len)) {
                    slot.write(byte);
                }
            }
        }
    }

    #[inline(always)]
    pub(crate) fn encoded_len(&self, input: &[u8]) -> usize {
        input.iter().map(|&byte| self.byte_len(byte)).sum()
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Scheme {
    Percent,
    Xtext,
}

impl Scheme {
    const fn marker(self) -> u8 {
        match self {
            Scheme::Percent => b'%',
            Scheme::Xtext => b'+',
        }
    }

    const fn safe_set(self) -> [bool; 256] {
        let mut set = [false; 256];
        let mut byte = 0;
        while byte < 128 {
            let ch = byte as u8;
            set[byte] = match self {
                Scheme::Percent => {
                    ch.is_ascii_alphanumeric()
                        || matches!(
                            ch,
                            b'!' | b'#'
                                | b'$'
                                | b'&'
                                | b'+'
                                | b'-'
                                | b'.'
                                | b'^'
                                | b'_'
                                | b'`'
                                | b'{'
                                | b'|'
                                | b'}'
                                | b'~'
                        )
                }
                Scheme::Xtext => matches!(ch, b'!'..=b'~') && ch != b'+' && ch != b'=',
            };
            byte += 1;
        }
        set
    }

    const fn table(self) -> EscapeTable {
        EscapeTable::new(self.marker(), &self.safe_set())
    }
}

const PERCENT_ESCAPES: EscapeTable = Scheme::Percent.table();
const XTEXT_ESCAPES: EscapeTable = Scheme::Xtext.table();
static PERCENT_TABLE: EscapeTable = PERCENT_ESCAPES;
static XTEXT_TABLE: EscapeTable = XTEXT_ESCAPES;
static PERCENT_STRS: [&str; 256] = PERCENT_TABLE.strs();
static XTEXT_STRS: [&str; 256] = XTEXT_TABLE.strs();

/// A hexadecimal escaping scheme: a marker byte followed by two hexadecimal
/// digits stands for any byte outside the scheme's safe set.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HexEscape {
    scheme: Scheme,
}

/// Percent-encoding (`%XX`). Encoding keeps the RFC 2231 `attribute-char`
/// set (letters, digits and ``! # $ & + - . ^ _ ` { | } ~``) and escapes
/// every other byte, as RFC 2231 extended parameter values require. It is
/// not a URI component encoder: RFC 3986 reserves some of those characters.
/// Decoding accepts any literal byte, so URIs and `mailto:` URLs decode too;
/// `+` is not a space.
pub const PERCENT: HexEscape = HexEscape {
    scheme: Scheme::Percent,
};

/// SMTP xtext (RFC 3461, `+XX`), used by the `ENVID` and `ORCPT`
/// parameters. Encoding keeps the printable ASCII characters other than `+`
/// and `=`.
pub const XTEXT: HexEscape = HexEscape {
    scheme: Scheme::Xtext,
};

impl HexEscape {
    /// The byte that introduces an escape.
    pub const fn marker(&self) -> u8 {
        self.scheme.marker()
    }

    #[inline(always)]
    fn table(&self) -> &'static EscapeTable {
        match self.scheme {
            Scheme::Percent => &PERCENT_TABLE,
            Scheme::Xtext => &XTEXT_TABLE,
        }
    }

    /// Length of the encoding of `byte`: 1 or 3.
    pub const fn encoded_byte_len(&self, byte: u8) -> usize {
        match self.scheme {
            Scheme::Percent => PERCENT_ESCAPES.byte_len(byte),
            Scheme::Xtext => XTEXT_ESCAPES.byte_len(byte),
        }
    }

    /// The encoding of `byte`: the byte itself or its escape.
    pub fn encode_byte(&self, byte: u8) -> &'static str {
        match self.scheme {
            Scheme::Percent => PERCENT_STRS[byte as usize],
            Scheme::Xtext => XTEXT_STRS[byte as usize],
        }
    }

    /// Length of the encoding of `input`.
    pub fn encoded_len(&self, input: impl AsRef<[u8]>) -> usize {
        self.table().encoded_len(input.as_ref())
    }

    /// Encodes `input` into a new `String`.
    pub fn encode(&self, input: impl AsRef<[u8]>) -> String {
        let input = input.as_ref();
        let mut out = String::with_capacity(input.len().saturating_mul(ESCAPED).saturating_add(1));
        self.encode_append(input, &mut out);
        out
    }

    /// Appends the encoding of `input` to a `Vec<u8>` or `String` and returns
    /// the number of bytes appended.
    pub fn encode_append(&self, input: impl AsRef<[u8]>, out: &mut impl Buffer) -> usize {
        let input = input.as_ref();
        // SAFETY: at most 3 bytes per input byte fit in the region, `encode_into`
        // returns the count it wrote, and every committed byte is a safe-set
        // ASCII byte, the marker or an uppercase hex digit.
        unsafe {
            out.append_ascii(
                input.len().saturating_mul(ESCAPED).saturating_add(1),
                |dst| self.encode_into(input, dst),
            )
        }
    }

    fn encode_into(&self, input: &[u8], dst: &mut [MaybeUninit<u8>]) -> usize {
        self.table().encode_into(input, dst)
    }

    /// Decodes `input`, borrowing it when it contains no escape. Hex digits
    /// may be in either case; a marker that is not followed by two of them
    /// is an error.
    pub fn decode<'x>(
        &self,
        input: &'x (impl AsRef<[u8]> + ?Sized),
    ) -> Result<Cow<'x, [u8]>, Error> {
        let input = input.as_ref();
        if !input.contains(&self.marker()) {
            return Ok(Cow::Borrowed(input));
        }
        let mut out = Vec::with_capacity(input.len());
        self.decode_into(input, &mut out)?;
        Ok(Cow::Owned(out))
    }

    /// Appends the decoding of `input` to `out` and returns the number of
    /// bytes appended. On error `out` is left unchanged.
    pub fn decode_append(
        &self,
        input: impl AsRef<[u8]>,
        out: &mut Vec<u8>,
    ) -> Result<usize, Error> {
        let start = out.len();
        let input = input.as_ref();
        out.reserve(input.len());
        match self.decode_into(input, out) {
            Ok(()) => Ok(out.len() - start),
            Err(err) => {
                out.truncate(start);
                Err(err)
            }
        }
    }

    fn decode_into(&self, input: &[u8], out: &mut Vec<u8>) -> Result<(), Error> {
        let marker = self.marker();
        let mut read = 0;
        loop {
            let literal = input
                .get(read..)
                .unwrap_or_default()
                .split(|&byte| byte == marker)
                .next()
                .unwrap_or_default();
            out.extend_from_slice(literal);
            read += literal.len();
            loop {
                let byte = match input.get(read..).unwrap_or_default() {
                    [] => return Ok(()),
                    [_, high, low, ..] => decode_pair(*high, *low)
                        .ok_or_else(|| Self::invalid_escape(read, *high, *low))?,
                    [_, high] if !high.is_ascii_hexdigit() => {
                        return Err(Error::InvalidByte {
                            offset: read + 1,
                            byte: *high,
                        });
                    }
                    _ => {
                        return Err(Error::Truncated {
                            offset: input.len(),
                        });
                    }
                };
                out.push(byte);
                read += ESCAPED;
                if input.get(read) != Some(&marker) {
                    break;
                }
            }
        }
    }

    const fn invalid_escape(marker_at: usize, high: u8, low: u8) -> Error {
        match high.is_ascii_hexdigit() {
            false => Error::InvalidByte {
                offset: marker_at + 1,
                byte: high,
            },
            true => Error::InvalidByte {
                offset: marker_at + 2,
                byte: low,
            },
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{PERCENT, XTEXT, decode_pair};
    use crate::{
        Error,
        qp::tests::reference::{Digits, Failure, Hex},
        test_rng::{XorShift, scaled},
    };
    use std::borrow::Cow;

    #[test]
    fn decodes_every_pair() {
        for byte in 0..=u8::MAX {
            let upper = format!("{byte:02X}");
            let lower = format!("{byte:02x}");
            for text in [upper, lower] {
                let [high, low] = text.as_bytes() else {
                    panic!("two digits");
                };
                assert_eq!(decode_pair(*high, *low), Some(byte));
            }
        }
        assert_eq!(decode_pair(b'G', b'0'), None);
        assert_eq!(decode_pair(b'0', b' '), None);
        for high in 0..=u8::MAX {
            for low in 0..=u8::MAX {
                assert_eq!(decode_pair(high, low), Digits::pair(high, low));
            }
        }
    }

    #[test]
    fn encodes_every_byte_like_the_reference() {
        for (engine, safe) in [
            (PERCENT, Hex::is_attribute_char as fn(u8) -> bool),
            (XTEXT, Hex::is_xchar),
        ] {
            let all: Vec<u8> = (0..=u8::MAX).collect();
            let expected = Hex::encode(&all, engine.marker(), safe);
            assert_eq!(engine.encode(&all).as_bytes(), expected);
            assert_eq!(engine.encoded_len(&all), expected.len());
            for byte in 0..=u8::MAX {
                let single = Hex::encode(&[byte], engine.marker(), safe);
                assert_eq!(engine.encode_byte(byte).as_bytes(), single);
                assert_eq!(engine.encoded_byte_len(byte), single.len());
            }
        }
    }

    #[test]
    fn matches_the_reference_on_random_input() {
        let mut rng = XorShift::new(0x4e58);
        let alphabet = b"%+=09afAFgG \x00\xff;.";
        for round in 0..scaled(20_000) {
            let len = rng.below(if round % 16 == 0 { 300 } else { 24 });
            let input: Vec<u8> = match round % 2 {
                0 => (0..len).map(|_| *rng.pick(alphabet)).collect(),
                _ => rng.bytes(len),
            };
            for engine in [PERCENT, XTEXT] {
                let encoded = engine.encode(&input);
                assert_eq!(engine.decode(&encoded).as_deref(), Ok(input.as_slice()));
                let expected = Hex::decode(&input, engine.marker());
                let actual = engine.decode(&input);
                assert_eq!(
                    actual.as_deref().map_err(|&err| Failure::from(err)),
                    expected.as_deref().map_err(|&err| err),
                    "{:?}",
                    String::from_utf8_lossy(&input)
                );
                let mut appended = b"prefix".to_vec();
                match engine.decode_append(&input, &mut appended) {
                    Ok(written) => {
                        assert_eq!(appended.get(6..), expected.as_deref().ok());
                        assert_eq!(appended.len(), 6 + written);
                    }
                    Err(_) => assert_eq!(appended, b"prefix"),
                }
            }
        }
    }

    #[test]
    fn borrows_when_nothing_is_escaped() {
        let input = "rfc822;user@example.org";
        assert!(matches!(XTEXT.decode(input), Ok(Cow::Borrowed(text)) if text == input.as_bytes()));
        assert!(matches!(
            PERCENT.decode("plain"),
            Ok(Cow::Borrowed(b"plain"))
        ));
        assert!(matches!(XTEXT.decode("a+2Bb"), Ok(Cow::Owned(text)) if text == b"a+b"));
    }

    #[test]
    fn reports_malformed_escapes() {
        for (input, error) in [
            ("%", Error::Truncated { offset: 1 }),
            ("ab%4", Error::Truncated { offset: 4 }),
            (
                "%G1",
                Error::InvalidByte {
                    offset: 1,
                    byte: b'G',
                },
            ),
            (
                "%4G",
                Error::InvalidByte {
                    offset: 2,
                    byte: b'G',
                },
            ),
            (
                "%%41",
                Error::InvalidByte {
                    offset: 1,
                    byte: b'%',
                },
            ),
            (
                "%4%41",
                Error::InvalidByte {
                    offset: 2,
                    byte: b'%',
                },
            ),
            ("x%41%2", Error::Truncated { offset: 6 }),
        ] {
            assert_eq!(PERCENT.decode(input), Err(error), "{input}");
        }
        assert_eq!(XTEXT.decode("a+2"), Err(Error::Truncated { offset: 3 }));
    }

    #[test]
    fn rfc_examples() {
        assert_eq!(
            PERCENT
                .decode("This%20is%20%2A%2A%2Afun%2A%2A%2A")
                .as_deref(),
            Ok(&b"This is ***fun***"[..])
        );
        assert_eq!(
            PERCENT.encode("This is ***fun***"),
            "This%20is%20%2A%2A%2Afun%2A%2A%2A"
        );
        assert_eq!(XTEXT.encode("Joe+Smith=x"), "Joe+2BSmith+3Dx");
        assert_eq!(
            XTEXT.decode("Joe+2BSmith+3dx").as_deref(),
            Ok(&b"Joe+Smith=x"[..])
        );
    }
}