Skip to content

Commit

Permalink
feat: add support for base64url decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
grjte committed Oct 29, 2024
1 parent 67877dc commit 2b0c87e
Show file tree
Hide file tree
Showing 2 changed files with 271 additions and 0 deletions.
270 changes: 270 additions & 0 deletions src/decoder.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use super::defaults::BASE64_PADDING_CHAR;

pub global STANDARD = Base64DecodeBE::new(true);
pub global STANDARD_NO_PAD = Base64DecodeBE::new(false);
pub global URL_SAFE = Base64DecodeBE::base64url(true);
pub global URL_SAFE_NO_PAD = Base64DecodeBE::base64url(false);

global INVALID_VALUE: u8 = 255;
struct Base64DecodeBE {
Expand Down Expand Up @@ -278,6 +280,274 @@ impl Base64DecodeBE {
}
}

// Creates a new decoder that uses the URL and Filename Safe Alphabet specified in RFC 4648
// https://datatracker.ietf.org/doc/html/rfc4648#section-5
fn base64url(pad: bool) -> Self {
Base64DecodeBE {
table: [
// 0-44 (no mapping)
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
62, // 45 (-)
INVALID_VALUE,
INVALID_VALUE, // 46-47 (no mapping)
52,
53,
54,
55,
56,
57,
58,
59,
60,
61, // 48-57 (0-9)
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE, // 58-64 (no mapping)
0,
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, // 65-90 (A-Z)
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE, // 91-94 (no mapping)
63, // 95 (_)
INVALID_VALUE, // 96 (no mapping)
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, // 97-122 (a-z)
// 123-255
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
INVALID_VALUE,
],
pad,
}
}

fn get(self, idx: Field) -> u8 {
self.table[idx]
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub use encoder::{
mod decoder;
pub use decoder::{
STANDARD as BASE64_DECODER_STANDARD, STANDARD_NO_PAD as BASE64_DECODER_STANDARD_NO_PAD,
URL_SAFE as BASE64_DECODER_URL_SAFE, URL_SAFE_NO_PAD as BASE64_DECODER_URL_SAFE_NO_PAD,
};

pub(crate) mod defaults {
Expand Down

0 comments on commit 2b0c87e

Please sign in to comment.