Skip to content

Commit

Permalink
FEAT: simplified enbase-64 padding code
Browse files Browse the repository at this point in the history
  • Loading branch information
Oldes committed May 20, 2023
1 parent ddcedfe commit df09076
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions src/core/f-enbase.c
Original file line number Diff line number Diff line change
Expand Up @@ -946,7 +946,7 @@ static REBU64 base36_powers[BASE36_LENGTH] = {
{
REBYTE *p;
REBYTE *src;
REBINT x, loop;
REBINT x, loop, pad;

if(len > VAL_LEN(value)) len = VAL_LEN(value);
src = VAL_BIN_DATA(value);
Expand All @@ -971,26 +971,16 @@ static REBU64 base36_powers[BASE36_LENGTH] = {
if ((x+3) % 48 == 0 && brk)
*p++ = LF;
}

if ((len % 3) != 0) {
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++;
pad = len % 3;
if (pad != 0) {
*p++ = table[src[x] >> 2];
if (pad >= 1)
*p++ = table[((src[x] & 0x3) << 4) + (pad == 1 ? 0 : src[x + 1] >> 4)];
if (pad == 2)
*p++ = table[(src[x + 1] & 0xF) << 2];
if (!urlSafe) {
// add padding
while (pad++ < 3) { *p = '='; p++; }
}
}

Expand Down

0 comments on commit df09076

Please sign in to comment.