1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/lib/webfuse_provider/impl/util/base64.c

184 lines
6.0 KiB
C
Raw Normal View History

2020-06-21 19:18:43 +00:00
#include "webfuse_provider/impl/util/base64.h"
2019-12-01 16:17:21 +00:00
static const uint8_t wfp_impl_base64_decode_table[256] = {
2019-12-01 16:17:21 +00:00
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 0
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 1
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 62, 0x80, 0x80, 0x80, 63, // 2
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0x80, 0x80, 0x80, 0, 0x80, 0x80, // 3
0x80, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 4
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0x80, 0x80, 0x80, 0x80, 0x80, // 5
0x80, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 6
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 0x80, 0x80, 0x80, 0x80, 0x80, // 7
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 8
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // 9
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // A
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // B
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // C
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // D
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // E
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, // F
};
size_t wfp_impl_base64_encoded_size(size_t length)
2019-12-01 16:17:21 +00:00
{
return ((length + 2) / 3) * 4;
}
size_t wfp_impl_base64_encode(
2019-12-01 16:17:21 +00:00
uint8_t const * data,
size_t length,
char * buffer,
size_t buffer_size)
{
// 0 1 2 3 4 5 6
// 0123456789012345678901234567890123456789012345678901234567890123
static char const table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t const length_needed = wfp_impl_base64_encoded_size(length);
2019-12-01 16:17:21 +00:00
if (buffer_size < length_needed)
{
return 0;
}
size_t pos = 0;
size_t out_pos = 0;
for(; (length - pos) >= 3; pos += 3)
{
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) | (data[pos + 2] >> 6) ];
buffer[out_pos++] = table[ data[pos + 2] & 0x3f ];
}
switch((length - pos))
{
case 1:
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos] & 0x03) << 4) ];
buffer[out_pos++] = '=';
buffer[out_pos++] = '=';
break;
case 2:
buffer[out_pos++] = table[ data[pos] >> 2 ];
buffer[out_pos++] = table[ ((data[pos ] & 0x03) << 4) | (data[pos + 1] >> 4) ];
buffer[out_pos++] = table[ ((data[pos + 1] & 0x0f) << 2) ];
buffer[out_pos++] = '=';
break;
default:
break;
}
if (buffer_size > out_pos)
{
buffer[out_pos] = '\0';
}
return out_pos;
}
size_t wfp_impl_base64_decoded_size(char const * data, size_t length)
2019-12-01 16:17:21 +00:00
{
size_t result = 0;
if ((length > 0) && ((length % 4) == 0))
{
result = (length / 4) * 3;
if ('=' == data[length - 1])
{
result--;
if ('=' == data[length - 2])
{
result--;
}
}
}
return result;
}
size_t wfp_impl_base64_decode(
2019-12-01 16:17:21 +00:00
char const * data,
size_t length,
uint8_t * buffer,
size_t buffer_size)
{
uint8_t const * table = wfp_impl_base64_decode_table;
size_t needed_size = wfp_impl_base64_decoded_size(data, length);
2019-12-01 16:17:21 +00:00
if ((0 == needed_size) || (buffer_size < needed_size))
{
return 0;
}
size_t out_pos = 0;
size_t pos = 0;
for(; pos < length - 4; pos += 4)
{
uint8_t a = table[ (unsigned char) data[pos ] ];
uint8_t b = table[ (unsigned char) data[pos + 1] ];
uint8_t c = table[ (unsigned char) data[pos + 2] ];
uint8_t d = table[ (unsigned char) data[pos + 3] ];
buffer[out_pos++] = (a << 2) | (b >> 4);
buffer[out_pos++] = (b << 4) | (c >> 2);
buffer[out_pos++] = (c << 6) | d;
}
// decode last block
{
uint8_t a = table[ (unsigned char) data[pos ] ];
uint8_t b = table[ (unsigned char) data[pos + 1] ];
uint8_t c = table[ (unsigned char) data[pos + 2] ];
uint8_t d = table[ (unsigned char) data[pos + 3] ];
buffer[out_pos++] = (a << 2) | (b >> 4);
if ('=' != data[pos + 2])
{
buffer[out_pos++] = (b << 4) | (c >> 2);
if ('=' != data[pos + 3])
{
buffer[out_pos++] = (c << 6) | d;
}
}
}
return out_pos;
}
extern bool wfp_impl_base64_isvalid(char const * data, size_t length)
2019-12-01 16:17:21 +00:00
{
uint8_t const * table = wfp_impl_base64_decode_table;
2019-12-01 16:17:21 +00:00
if ((length == 0) || ((length % 4) != 0))
{
return false;
}
size_t pos = 0;
for(; pos < (length - 2); pos++)
{
unsigned char c = (unsigned char) data[pos];
if (('=' == c) || (0x80 == table[c]))
{
return false;
}
}
if (('=' == data[pos]) && ('=' != data[pos + 1]))
{
return false;
}
for(;pos < length; pos++)
{
char c = data[pos];
if (0x80 == table[ (unsigned char) c])
{
return false;
}
}
return true;
}