base64ct/alphabet/
standard.rs

1//! Standard Base64 encoding.
2
3use super::{Alphabet, DecodeStep, EncodeStep};
4
5/// Standard Base64 encoding with `=` padding.
6///
7/// ```text
8/// [A-Z]      [a-z]      [0-9]      +     /
9/// 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
10/// ```
11#[derive(Copy, Clone, Debug, Eq, PartialEq)]
12pub struct Base64;
13
14impl Alphabet for Base64 {
15    const BASE: u8 = b'A';
16    const DECODER: &'static [DecodeStep] = DECODER;
17    const ENCODER: &'static [EncodeStep] = ENCODER;
18    const PADDED: bool = true;
19    type Unpadded = Base64Unpadded;
20}
21
22/// Standard Base64 encoding *without* padding.
23///
24/// ```text
25/// [A-Z]      [a-z]      [0-9]      +     /
26/// 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
27/// ```
28#[derive(Copy, Clone, Debug, Eq, PartialEq)]
29pub struct Base64Unpadded;
30
31impl Alphabet for Base64Unpadded {
32    const BASE: u8 = b'A';
33    const DECODER: &'static [DecodeStep] = DECODER;
34    const ENCODER: &'static [EncodeStep] = ENCODER;
35    const PADDED: bool = false;
36    type Unpadded = Self;
37}
38
39/// Standard Base64 decoder
40const DECODER: &[DecodeStep] = &[
41    DecodeStep::Range(b'A'..=b'Z', -64),
42    DecodeStep::Range(b'a'..=b'z', -70),
43    DecodeStep::Range(b'0'..=b'9', 5),
44    DecodeStep::Eq(b'+', 63),
45    DecodeStep::Eq(b'/', 64),
46];
47
48/// Standard Base64 encoder
49const ENCODER: &[EncodeStep] = &[
50    EncodeStep::Diff(25, 6),
51    EncodeStep::Diff(51, -75),
52    EncodeStep::Diff(61, -(b'+' as i16 - 0x1c)),
53    EncodeStep::Diff(62, b'/' as i16 - b'+' as i16 - 1),
54];