From 450f3880c6781753adbb7fa9b53ecf087a0f4c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20Rubinos=20Rodr=C3=ADguez?= Date: Tue, 28 May 2024 12:36:14 +0200 Subject: [PATCH] fix: remove padding, $= is not url safe. (#6) --- src/nuid_base64.erl | 55 ++++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/src/nuid_base64.erl b/src/nuid_base64.erl index 8b60d96..bba2e38 100644 --- a/src/nuid_base64.erl +++ b/src/nuid_base64.erl @@ -30,6 +30,7 @@ %% -type base64_alphabet() :: $A..$Z | $a..$z | $0..$9 | $- | $_. %% Section 4, `urlsafe' for RFC 4648 Section 5. %% It's been reordered to be lexicographically sortable. +%% Padding is the dot. %% The following type is a subtype of string() for return values %% of encoding functions. @@ -61,12 +62,12 @@ encode_binary(<>, A) -> encode_binary(<>, A) -> E1 = b64e(B1), E2 = b64e(B2 bsl 4), - <>; + <>; encode_binary(<>, A) -> E1 = b64e(B1), E2 = b64e(B2), E3 = b64e(B3 bsl 2), - <>. + <>. %% mime_decode strips away all characters not Base64 before %% converting, whereas decode crashes if an illegal character is found @@ -106,23 +107,14 @@ decode_binary(<>, A, B1) -> decode_binary(<>, A, B1, B2) -> B3 = b64d(C3), decode_binary(Cs, A, B1, B2, B3); -decode_binary(<<_Cs/bits>>, _A, _B1, _B2) -> - missing_padding_error(). +decode_binary(<<>>, A, B1, B2) -> + <>. decode_binary(<>, A, B1, B2, B3) -> B4 = b64d(C4), decode_binary(Cs, <>); -decode_binary(<<>>, _A, _B1, _B2, _B3) -> - missing_padding_error(). - -%%%======================================================================== -%%% Error handling functions -%%%======================================================================== - -% always inlined for useful stacktraces when called in tail position --compile({inline, missing_padding_error/0}). -missing_padding_error() -> - error(missing_padding, none, [{error_info, #{}}]). +decode_binary(<<>>, A, B1, B2, B3) -> + <>. %%%======================================================================== @@ -219,3 +211,36 @@ b64e(X) -> $z } ). + +%%----------------------------------------------------------------------- +%% Code to generate decode table +%%----------------------------------------------------------------------- +%% code({value, {Pos, _Value}}) -> +%% Pos; +%% code(_) -> +%% bad. +%% +%% alphabet_pos([], _Pos, Acc) -> +%% lists:reverse(Acc); +%% alphabet_pos([Char | Rest], Pos, Acc) -> +%% alphabet_pos(Rest, Pos + 1, [{Pos, Char} | Acc]). +%% +%% decode_tuple(AlphabetPos) -> +%% Seq = lists:seq(1, 256), +%% decode_tuple(AlphabetPos, Seq, []). +%% +%% +%% decode_tuple(_AlphabetPos, [], Acc) -> +%% list_to_tuple(lists:reverse(Acc)); +%% decode_tuple(AlphabetPos, [Char | Rest], Acc) -> +%% Value = code(lists:keysearch(Char, 2, AlphabetPos)), +%% decode_tuple(AlphabetPos, Rest, [Value | Acc]). +%% +%% decode_table() -> +%% Alphabet = [$-, $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, +%% $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], +%% AlphabetPos = alphabet_pos(Alphabet, 0, []), +%% decode_tuple(AlphabetPos).