2017-06-16 22:31:20 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* `encode.c' - b64
|
|
|
|
*
|
|
|
|
* copyright (c) 2014 joseph werle
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2020-03-21 21:00:02 +01:00
|
|
|
#include <b64.h>
|
2017-06-16 22:31:20 +02:00
|
|
|
|
|
|
|
char *
|
|
|
|
b64_encode (const unsigned char *src, size_t len) {
|
|
|
|
return b64_encode_ex(src, len, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
b64_encode_ex (const unsigned char *src, size_t len, size_t *encsize) {
|
|
|
|
int i = 0;
|
|
|
|
int j = 0;
|
|
|
|
char *enc = NULL;
|
|
|
|
size_t size = 0;
|
|
|
|
unsigned char buf[4];
|
|
|
|
unsigned char tmp[3];
|
|
|
|
|
|
|
|
// alloc
|
|
|
|
enc = (char *) b64_malloc(1);
|
|
|
|
if (NULL == enc) { return NULL; }
|
|
|
|
|
|
|
|
// parse until end of source
|
|
|
|
while (len--) {
|
|
|
|
// read up to 3 bytes at a time into `tmp'
|
|
|
|
tmp[i++] = *(src++);
|
|
|
|
|
|
|
|
// if 3 bytes read then encode into `buf'
|
|
|
|
if (3 == i) {
|
|
|
|
buf[0] = (tmp[0] & 0xfc) >> 2;
|
|
|
|
buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
|
|
|
|
buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
|
|
|
|
buf[3] = tmp[2] & 0x3f;
|
|
|
|
|
|
|
|
// allocate 4 new byts for `enc` and
|
|
|
|
// then translate each encoded buffer
|
|
|
|
// part by index from the base 64 index table
|
|
|
|
// into `enc' unsigned char array
|
|
|
|
enc = (char *) b64_realloc(enc, size + 4);
|
|
|
|
for (i = 0; i < 4; ++i) {
|
|
|
|
enc[size++] = b64_table[buf[i]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset index
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// remainder
|
|
|
|
if (i > 0) {
|
|
|
|
// fill `tmp' with `\0' at most 3 times
|
|
|
|
for (j = i; j < 3; ++j) {
|
|
|
|
tmp[j] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform same codec as above
|
|
|
|
buf[0] = (tmp[0] & 0xfc) >> 2;
|
|
|
|
buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4);
|
|
|
|
buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6);
|
|
|
|
buf[3] = tmp[2] & 0x3f;
|
|
|
|
|
|
|
|
// perform same write to `enc` with new allocation
|
|
|
|
for (j = 0; (j < i + 1); ++j) {
|
|
|
|
enc = (char *) b64_realloc(enc, size + 1);
|
|
|
|
enc[size++] = b64_table[buf[j]];
|
|
|
|
}
|
|
|
|
|
|
|
|
// while there is still a remainder
|
|
|
|
// append `=' to `enc'
|
|
|
|
while ((i++ < 3)) {
|
|
|
|
enc = (char *) b64_realloc(enc, size + 1);
|
|
|
|
enc[size++] = '=';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we have enough space to add '\0' character at end.
|
|
|
|
enc = (char *) b64_realloc(enc, size + 1);
|
|
|
|
enc[size] = '\0';
|
|
|
|
|
|
|
|
// Return back the size of encoded string if demanded.
|
|
|
|
if (encsize != NULL) {
|
|
|
|
*encsize = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return enc;
|
|
|
|
}
|