From 454f75cb2288e171cd4d40879938ea970e3b9019 Mon Sep 17 00:00:00 2001 From: rykad <> Date: Sat, 17 Jan 2026 22:26:25 +0000 Subject: [PATCH] core:encoding/base64: add support for url variant --- core/encoding/base64/base64.odin | 49 ++++++++++++++++++++++++++ tests/core/encoding/base64/base64.odin | 17 ++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/core/encoding/base64/base64.odin b/core/encoding/base64/base64.odin index 032716bde..0677fdfb7 100644 --- a/core/encoding/base64/base64.odin +++ b/core/encoding/base64/base64.odin @@ -24,6 +24,18 @@ ENC_TABLE := [64]byte { '4', '5', '6', '7', '8', '9', '+', '/', } +// Encoding table for Base64url variant +ENC_URL_TABLE := [64]byte { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '-', '_', +} + PADDING :: '=' DEC_TABLE := [256]u8 { @@ -61,6 +73,43 @@ DEC_TABLE := [256]u8 { 0, 0, 0, 0, 0, 0, 0, 0, } +// Decoding table for Base64url variant +DEC_URL_TABLE := [256]u8 { + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 62, 0, 0, + 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, 0, 0, 0, 0, 0, 0, + 0, 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, 0, 0, 0, 0, 63, + 0, 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, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, +} + + encode :: proc(data: []byte, ENC_TBL := ENC_TABLE, allocator := context.allocator) -> (encoded: string, err: mem.Allocator_Error) #optional_allocator_error { out_length := encoded_len(data) if out_length == 0 { diff --git a/tests/core/encoding/base64/base64.odin b/tests/core/encoding/base64/base64.odin index ed1bee8af..93b3afb59 100644 --- a/tests/core/encoding/base64/base64.odin +++ b/tests/core/encoding/base64/base64.odin @@ -50,4 +50,19 @@ test_roundtrip :: proc(t: ^testing.T) { for v, i in decoded { testing.expect_value(t, v, values[i]) } -} \ No newline at end of file +} + +@(test) +test_base64url :: proc(t: ^testing.T) { + plain := ">>>" + url := "Pj4-" + + encoded := base64.encode(transmute([]byte)plain, base64.ENC_URL_TABLE) + defer delete(encoded) + testing.expect_value(t, encoded, url) + + decoded := string(base64.decode(url, base64.DEC_URL_TABLE)) + defer delete(decoded) + testing.expect_value(t, decoded, plain) + +}