Add Base64 encoding routines

This commit is contained in:
Julien BLACHE 2010-04-12 18:22:38 +02:00
parent 1849601d56
commit 7a02215d0d
2 changed files with 70 additions and 0 deletions

View File

@ -308,6 +308,73 @@ b64_decode(const char *b64)
return str; return str;
} }
static const char b64_encode_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static void
b64_encode_block(uint8_t *in, char *out, int len)
{
out[0] = b64_encode_table[in[0] >> 2];
out[2] = out[3] = '=';
if (len == 1)
out[1] = b64_encode_table[((in[0] & 0x03) << 4)];
else
{
out[1] = b64_encode_table[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
if (len == 2)
out[2] = b64_encode_table[((in[1] & 0x0f) << 2)];
else
{
out[2] = b64_encode_table[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
out[3] = b64_encode_table[in[2] & 0x3f];
}
}
}
static void
b64_encode_full_block(uint8_t *in, char *out)
{
out[0] = b64_encode_table[in[0] >> 2];
out[1] = b64_encode_table[((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)];
out[2] = b64_encode_table[((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)];
out[3] = b64_encode_table[in[2] & 0x3f];
}
char *
b64_encode(uint8_t *in, size_t len)
{
char *encoded;
char *out;
/* 3 in chars -> 4 out chars */
encoded = (char *)malloc(len + (len / 3) + 4 + 1);
if (!encoded)
return NULL;
out = encoded;
while (len >= 3)
{
b64_encode_full_block(in, out);
len -= 3;
in += 3;
out += 4;
}
if (len > 0)
{
b64_encode_block(in, out, len);
out += 4;
}
out[0] = '\0';
return encoded;
}
/* /*
* MurmurHash2, 64-bit versions, by Austin Appleby * MurmurHash2, 64-bit versions, by Austin Appleby
* *

View File

@ -25,6 +25,9 @@ djb_hash(void *data, size_t len);
char * char *
b64_decode(const char *b64); b64_decode(const char *b64);
char *
b64_encode(uint8_t *in, size_t len);
uint64_t uint64_t
murmur_hash64(const void *key, int len, uint32_t seed); murmur_hash64(const void *key, int len, uint32_t seed);