Skip to content

Commit ddcedfe

Browse files
committed
FIX: do not pad output when used enbase/url
resolves: Oldes/Rebol-issues#2548
1 parent 8657fb9 commit ddcedfe

File tree

2 files changed

+32
-11
lines changed

2 files changed

+32
-11
lines changed

src/core/f-enbase.c

+19-9
Original file line numberDiff line numberDiff line change
@@ -973,15 +973,25 @@ static REBU64 base36_powers[BASE36_LENGTH] = {
973973
}
974974

975975
if ((len % 3) != 0) {
976-
p[2] = p[3] = '=';
977-
*p++ = table[src[x] >> 2];
978-
if ((len - x) >= 1)
979-
*p++ = table[((src[x] & 0x3) << 4) + ((len - x) == 1 ? 0 : src[x + 1] >> 4)];
980-
else p++;
981-
if ((len - x) == 2)
982-
*p++ = table[(src[x + 1] & 0xF) << 2];
983-
else p++;
984-
p++;
976+
if (urlSafe) {
977+
// no padding
978+
*p++ = table[src[x] >> 2];
979+
if ((len - x) >= 1)
980+
*p++ = table[((src[x] & 0x3) << 4) + ((len - x) == 1 ? 0 : src[x + 1] >> 4)];
981+
if ((len - x) == 2)
982+
*p++ = table[(src[x + 1] & 0xF) << 2];
983+
}
984+
else {
985+
p[2] = p[3] = '=';
986+
*p++ = table[src[x] >> 2];
987+
if ((len - x) >= 1)
988+
*p++ = table[((src[x] & 0x3) << 4) + ((len - x) == 1 ? 0 : src[x + 1] >> 4)];
989+
else p++;
990+
if ((len - x) == 2)
991+
*p++ = table[(src[x + 1] & 0xF) << 2];
992+
else p++;
993+
p++;
994+
}
985995
}
986996

987997
//if (*(p-1) != LF && x > 49 && brk) *p++ = LF; // adds LF before closing bracket

src/tests/units/enbase-test.r3

+13-2
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,25 @@ AAACAAIAAAMDCCGTADs=}
107107
} 64]
108108
--test-- "debase 64 url 3"
109109
key1: "qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA=="
110+
key2: "qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA"
110111
bin: try [debase/url key1 64]
111-
--assert true? all [binary? bin key1 = enbase/url bin 64]
112+
--assert true? all [binary? bin key2 = enbase/url bin 64]
112113
;debase is working also when input is missing the padding
113-
key2: "qL8R4QIcQ_ZsRqOAbeRfcZhilN_MksRtDaErMA"
114114
--assert bin = try [debase/url key2 64]
115115

116116
===end-group===
117117

118+
119+
===start-group=== "enbase 64 - safe URL variant"
120+
--test "enbase/url"
121+
;@@ https://github.com/Oldes/Rebol-issues/issues/2548
122+
--assert "YQ==" == enbase "a" 64
123+
--assert "YWE=" == enbase "aa" 64
124+
--assert "YQ" == enbase/url "a" 64
125+
--assert "YWE" == enbase/url "aa" 64
126+
===end-group===
127+
128+
118129
===start-group=== "debase 16"
119130

120131
--test-- "debase 16 1"

0 commit comments

Comments
 (0)