lighttpd 1.4.x https://www.lighttpd.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

210 lines
7.4 KiB

#include "first.h"
#include "base64.h"
/* reverse mapping:
* >= 0: base64 value
* -1: invalid character
* -2: skip character (whitespace/control)
* -3: padding
*/
/* BASE64_STANDARD: "A-Z a-z 0-9 + /" maps to 0-63, pad with "=" */
static const char base64_standard_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
static const signed char base64_standard_reverse_table[] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x00 - 0x0F */
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x10 - 0x1F */
-2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, /* 0x20 - 0x2F */
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -3, -1, -1, /* 0x30 - 0x3F */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 - 0x4F */
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, /* 0x50 - 0x5F */
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 - 0x6F */
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70 - 0x7F */
};
/* BASE64_URL: "A-Z a-z 0-9 - _" maps to 0-63, pad with "." */
static const char base64_url_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_.";
static const signed char base64_url_reverse_table[] = {
/* 0 1 2 3 4 5 6 7 8 9 A B C D E F */
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x00 - 0x0F */
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, /* 0x10 - 0x1F */
-2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -3, -1, /* 0x20 - 0x2F */
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, /* 0x30 - 0x3F */
-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, /* 0x40 - 0x4F */
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63, /* 0x50 - 0x5F */
-1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, /* 0x60 - 0x6F */
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, /* 0x70 - 0x7F */
};
__attribute_const__
static char li_base64_padding_char (const base64_charset charset) {
switch (charset) {
default:
case BASE64_STANDARD:
return '='; /* base64_standard_table[64]; */
case BASE64_URL:
return '.'; /* base64_url_table[64]; */
}
}
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 */
/* 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 */
}
}
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;
}
}
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;
}
size_t li_to_base64_no_padding(char* out, size_t out_length, const unsigned char* in, size_t in_length, base64_charset charset) {
size_t i;
size_t out_pos = 0;
const char* const base64_table = (charset)
? base64_url_table /* BASE64_URL */
: base64_standard_table; /* BASE64_STANDARD */
/* check overflows */
/* 1073741823 is max num full_tuples to avoid overflow in uint32_t;
* and (1 GB - 1) is ridiculously large for base64-encoded data */
force_assert(in_length <= 3221225469); /* (3221225469+2) / 3 * 4 < UINT32_MAX */
force_assert((in_length+2)/3*4 <= out_length);
for (i = 2; i < in_length; i += 3) {
unsigned int v = (in[i-2] << 16) | (in[i-1] << 8) | in[i];
out[out_pos+3] = base64_table[v & 0x3f]; v >>= 6;
out[out_pos+2] = base64_table[v & 0x3f]; v >>= 6;
out[out_pos+1] = base64_table[v & 0x3f]; v >>= 6;
out[out_pos+0] = base64_table[v & 0x3f];
out_pos += 4;
}
switch (in_length - (i-2)) {
case 0:
default:
break;
case 1:
{
/* pretend in[i-1] = in[i] = 0, don't write last two (out_pos+3, out_pos+2) characters */
unsigned int v = (in[i-2] << 4);
out[out_pos+1] = base64_table[v & 0x3f]; v >>= 6;
out[out_pos+0] = base64_table[v & 0x3f];
out_pos += 2;
}
break;
case 2:
{
/* pretend in[i] = 0, don't write last (out_pos+3) character */
unsigned int v = (in[i-2] << 10) | (in[i-1] << 2);
out[out_pos+2] = base64_table[v & 0x3f]; v >>= 6;
out[out_pos+1] = base64_table[v & 0x3f]; v >>= 6;
out[out_pos+0] = base64_table[v & 0x3f];
out_pos += 3;
}
break;
}
return out_pos;
}
size_t li_to_base64(char* out, size_t out_length, const unsigned char* in, size_t in_length, base64_charset charset) {
size_t out_pos =
li_to_base64_no_padding(out, out_length, in, in_length, charset);
const size_t in_tuple_remainder = in_length % 3;
const size_t padding_length = in_tuple_remainder ? 3 - in_tuple_remainder : 0;
if (padding_length) { /* [0,1,2] */
const char padding = li_base64_padding_char(charset);
force_assert(padding_length <= out_length - out_pos);
if (padding_length > 1)
out[out_pos++] = padding;
out[out_pos++] = padding;
}
return out_pos;
}
char* buffer_append_base64_encode_opt(buffer *out, const unsigned char* in, size_t in_length, base64_charset charset, int pad) {
const size_t reserve = (in_length+2)/3*4;
char * const result = buffer_string_prepare_append(out, reserve);
const size_t out_pos = (pad)
? li_to_base64(result, reserve, in, in_length, charset)
: li_to_base64_no_padding(result, reserve, in, in_length, charset);
buffer_commit(out, out_pos);
return result;
}
unsigned char* buffer_append_base64_decode(buffer *out, const char* in, size_t in_length, base64_charset charset) {
const size_t reserve = 3*(in_length/4) + 3;
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);
buffer_commit(out, out_pos);
return (out_pos || !in_length) ? result : NULL;
}