From e7fb02a84a24f5430199249934a3dc37b10a4d39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zolt=C3=A1n=20K=C3=A9ri?= Date: Wed, 25 Dec 2024 16:15:41 +0100 Subject: [PATCH] encoding/base32: Add custom validation support Add support for custom alphabet validation through an optional validation function parameter. The default validation follows RFC 4648 base32 alphabet rules (A-Z, 2-7). This properly supports the documented ability to use custom alphabets. --- core/encoding/base32/base32.odin | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/core/encoding/base32/base32.odin b/core/encoding/base32/base32.odin index 68b8b7a5e..7c70b7e9a 100644 --- a/core/encoding/base32/base32.odin +++ b/core/encoding/base32/base32.odin @@ -7,18 +7,25 @@ import "core:bytes" // A secondary param can be used to supply a custom alphabet to // @link(encode) and a matching decoding table to @link(decode). // If none is supplied it just uses the standard Base32 alphabet. -// Incase your specific version does not use padding, you may +// In case your specific version does not use padding, you may // truncate it from the encoded output. // Error represents errors that can occur during base32 decoding operations. // See RFC 4648 sections 3.2, 4 and 6. Error :: enum { None, - Invalid_Character, // Input contains characters outside of base32 alphabet (A-Z, 2-7) + Invalid_Character, // Input contains characters outside the specified alphabet Invalid_Length, // Input length is not valid for base32 (must be a multiple of 8 with proper padding) Malformed_Input, // Input has improper structure (wrong padding position or incomplete groups) } +Validate_Proc :: #type proc(c: byte) -> bool + +@private +_validate_default :: proc(c: byte) -> bool { + return (c >= 'A' && c <= 'Z') || (c >= '2' && c <= '7') +} + ENC_TABLE := [32]byte { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', @@ -105,7 +112,11 @@ _encode :: proc(out, data: []byte, ENC_TBL := ENC_TABLE, allocator := context.al } @(optimization_mode="favor_size") -decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocator) -> (out: []byte, err: Error) { +decode :: proc( + data: string, + DEC_TBL := DEC_TABLE, + validate: Validate_Proc = _validate_default, + allocator := context.allocator) -> (out: []byte, err: Error) { if len(data) == 0 { return nil, .None } @@ -115,13 +126,13 @@ decode :: proc(data: string, DEC_TBL := DEC_TABLE, allocator := context.allocato return nil, .Invalid_Length } - // Validate characters - only A-Z and 2-7 allowed before padding + // Validate characters using provided validation function for i := 0; i < len(data); i += 1 { c := data[i] if c == byte(PADDING) { break } - if !((c >= 'A' && c <= 'Z') || (c >= '2' && c <= '7')) { + if !validate(c) { return nil, .Invalid_Character } }