base64ct/alphabet/
url.rs

1//! URL-safe Base64 encoding.
2
3use super::{Alphabet, DecodeStep, EncodeStep};
4
5/// URL-safe Base64 encoding with `=` padding.
6///
7/// ```text
8/// [A-Z]      [a-z]      [0-9]      -     _
9/// 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2d, 0x5f
10/// ```
11#[derive(Copy, Clone, Debug, Eq, PartialEq)]
12pub struct Base64Url;
13
14impl Alphabet for Base64Url {
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 = Base64UrlUnpadded;
20}
21
22/// URL-safe Base64 encoding *without* padding.
23///
24/// ```text
25/// [A-Z]      [a-z]      [0-9]      -     _
26/// 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2d, 0x5f
27/// ```
28#[derive(Copy, Clone, Debug, Eq, PartialEq)]
29pub struct Base64UrlUnpadded;
30
31impl Alphabet for Base64UrlUnpadded {
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/// URL-safe 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/// URL-safe Base64 encoder
49const ENCODER: &[EncodeStep] = &[
50    EncodeStep::Diff(25, 6),
51    EncodeStep::Diff(51, -75),
52    EncodeStep::Diff(61, -(b'-' as i16 - 0x20)),
53    EncodeStep::Diff(62, b'_' as i16 - b'-' as i16 - 1),
54];