Skip to content

Commit

Permalink
FIX: do not pad output when used enbase/url
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed May 20, 2023
1 parent 8657fb9 commit ddcedfe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
28 changes: 19 additions & 9 deletions src/core/f-enbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -973,15 +973,25 @@ static REBU64 base36_powers[BASE36_LENGTH] = {
}

if ((len % 3) != 0) {
p[2] = p[3] = '=';
*p++ = table[src[x] >> 2];
if ((len - x) >= 1)
*p++ = table[((src[x] & 0x3) << 4) + ((len - x) == 1 ? 0 : src[x + 1] >> 4)];
else p++;
if ((len - x) == 2)
*p++ = table[(src[x + 1] & 0xF) << 2];
else p++;
p++;
if (urlSafe) {
// no padding
*p++ = table[src[x] >> 2];
if ((len - x) >= 1)
*p++ = table[((src[x] & 0x3) << 4) + ((len - x) == 1 ? 0 : src[x + 1] >> 4)];
if ((len - x) == 2)
*p++ = table[(src[x + 1] & 0xF) << 2];
}
else {
p[2] = p[3] = '=';
*p++ = table[src[x] >> 2];
if ((len - x) >= 1)
*p++ = table[((src[x] & 0x3) << 4) + ((len - x) == 1 ? 0 : src[x + 1] >> 4)];
else p++;
if ((len - x) == 2)
*p++ = table[(src[x + 1] & 0xF) << 2];
else p++;
p++;
}
}

//if (*(p-1) != LF && x > 49 && brk) *p++ = LF; // adds LF before closing bracket
Expand Down
15 changes: 13 additions & 2 deletions src/tests/units/enbase-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,25 @@ AAACAAIAAAMDCCGTADs=}
} 64]
--test-- "debase 64 url 3"
key1: "qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA=="
key2: "qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA"
bin: try [debase/url key1 64]
--assert true? all [binary? bin key1 = enbase/url bin 64]
--assert true? all [binary? bin key2 = enbase/url bin 64]
;debase is working also when input is missing the padding
key2: "qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA"
--assert bin = try [debase/url key2 64]

===end-group===


===start-group=== "enbase 64 - safe URL variant"
--test "enbase/url"
;@@ https://github.com/Oldes/Rebol-issues/issues/2548
--assert "YQ==" == enbase "a" 64
--assert "YWE=" == enbase "aa" 64
--assert "YQ" == enbase/url "a" 64
--assert "YWE" == enbase/url "aa" 64
===end-group===


===start-group=== "debase 16"

--test-- "debase 16 1"
Expand Down

0 comments on commit ddcedfe

Please sign in to comment.