[core] li_base64_dec() on 4 bytes at a time

This commit is contained in:
Glenn Strauss 2021-05-19 02:16:48 -04:00
parent 0dfe9f7b76
commit 3301fe0455
1 changed files with 45 additions and 65 deletions

View File

@ -48,73 +48,53 @@ static char li_base64_padding_char (const base64_charset charset) {
}
}
static size_t li_base64_decode(unsigned char * const result, const size_t out_length, const char * const in, const size_t in_length, const base64_charset charset) {
size_t out_pos = 0; /* current output character (position) that is decoded. can contain partial result */
unsigned int group = 0; /* how many base64 digits in the current group were decoded already. each group has up to 4 digits */
size_t i;
const signed char * const base64_reverse_table = (charset)
? base64_url_reverse_table /* BASE64_URL */
: base64_standard_reverse_table; /* BASE64_STANDARD */
static size_t li_base64_dec(unsigned char * const result, const size_t out_length, const char * const in, const size_t in_length, const base64_charset charset) {
size_t i;
const signed char * const base64_reverse_table = (charset)
? base64_url_reverse_table /* BASE64_URL */
: base64_standard_reverse_table; /* BASE64_STANDARD */
/* run through the whole string, converting as we go */
for (i = 0; i < in_length; i++) {
const int c = ((unsigned char *)in)[i];
const int ch = (c < 128) ? base64_reverse_table[c] : -1;
if (__builtin_expect( (ch < 0), 0)) {
if (-3 == ch) {
/* pad character; can only come after 2 base64 digits in a group */
if (group < 2) return 0;
break;
} else if (-2 == ch) {
continue; /* skip character */
} else {
if (c == '\0') break;
return 0; /* invalid character, abort */
}
}
int_fast32_t ch = 0;
int_fast32_t out4 = 0;
size_t out_pos = 0;
for (i = 0; i < in_length; i++) {
const uint_fast32_t c = ((unsigned char *)in)[i];
ch = (c < 128) ? base64_reverse_table[c] : -1;
if (__builtin_expect( (ch < 0), 0)) {
/* skip formatted base64; skip whitespace ('\r' '\n' '\t' ' ')*/
if (-2 == ch) /*(loose check; skip ' ', all ctrls not \127 or \0)*/
continue; /* skip character */
break;
}
switch(group) {
case 0:
result[out_pos] = ch << 2;
group = 1;
break;
case 1:
result[out_pos++] |= ch >> 4;
result[out_pos] = (ch & 0x0f) << 4;
group = 2;
break;
case 2:
result[out_pos++] |= ch >>2;
result[out_pos] = (ch & 0x03) << 6;
group = 3;
break;
case 3:
result[out_pos++] |= ch;
group = 0;
break;
}
}
out4 = (out4 << 6) | ch;
if ((i & 3) == 3) {
result[out_pos] = (out4 >> 16) & 0xFF;
result[out_pos+1] = (out4 >> 8) & 0xFF;
result[out_pos+2] = (out4 ) & 0xFF;
out_pos += 3;
out4 = 0;
}
}
switch(group) {
case 0:
/* ended on boundary */
break;
case 1:
/* need at least 2 base64 digits per group */
return 0;
case 2:
/* have 2 base64 digits in last group => one real octet, two zeroes padded */
case 3:
/* have 3 base64 digits in last group => two real octects, one zero padded */
/* for both cases the current index already is on the first zero padded octet
* - check it really is zero (overlapping bits) */
if (0 != result[out_pos]) return 0;
break;
}
force_assert(out_pos <= out_length);
return out_pos;
/* permit base64 string ending with pad chars (ch == -3); not checking
* for one or two pad chars + optional whitespace reaches in_length) */
/* permit base64 string truncated before in_length (in[i] == '\0') */
switch (i == in_length || ch == -3 || in[i] != '\0' ? (i & 3) : 1) {
case 3:
result[out_pos++] = (out4 >> 10);
out4 <<= 2;
__attribute_fallthrough__
case 2:
result[out_pos++] = (out4 >> 4) & 0xFF;
__attribute_fallthrough__
case 0:
force_assert(out_pos <= out_length);
return out_pos;
case 1: /* pad char or str end can only come after 2+ base64 chars */
default:
return 0; /* invalid character, abort */
}
}
size_t li_base64_enc(char * const restrict out, const size_t out_length, const unsigned char * const restrict in, const size_t in_length, const base64_charset charset, const int pad) {
@ -197,7 +177,7 @@ unsigned char* buffer_append_base64_decode(buffer *out, const char* in, size_t i
unsigned char * const result = (unsigned char *)
buffer_string_prepare_append(out, reserve);
const size_t out_pos =
li_base64_decode(result, reserve, in, in_length, charset);
li_base64_dec(result, reserve, in, in_length, charset);
buffer_commit(out, out_pos);