From 2b0c87e4bc8d446b8ca26d393b9e1128d0f619a2 Mon Sep 17 00:00:00 2001 From: grjte Date: Fri, 18 Oct 2024 09:54:24 +0100 Subject: [PATCH] feat: add support for base64url decoding --- src/decoder.nr | 270 +++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.nr | 1 + 2 files changed, 271 insertions(+) diff --git a/src/decoder.nr b/src/decoder.nr index 314fb93..f690ba8 100644 --- a/src/decoder.nr +++ b/src/decoder.nr @@ -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 { @@ -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] } diff --git a/src/lib.nr b/src/lib.nr index d9b2d0e..6e2c8c2 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -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 {