#include <string.h>
#include <isc/base64.h>
#include <isc/buffer.h>
#include <isc/region.h>
#include <isc/util.h>
#define RETERR(x) do { \
isc_result_t _r = (x); \
if (_r != ISC_R_SUCCESS) \
return (_r); \
} while (0)
static const char base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
isc_result_t
isc_base64_totext(isc_region_t *source, int wordlength,
const char *wordbreak, isc_buffer_t *target)
{
char buf[5];
unsigned int loops = 0;
if (wordlength < 4)
wordlength = 4;
memset(buf, 0, sizeof(buf));
while (source->length > 2) {
buf[0] = base64[(source->base[0]>>2)&0x3f];
buf[1] = base64[((source->base[0]<<4)&0x30)|
((source->base[1]>>4)&0x0f)];
buf[2] = base64[((source->base[1]<<2)&0x3c)|
((source->base[2]>>6)&0x03)];
buf[3] = base64[source->base[2]&0x3f];
RETERR(isc_str_tobuffer(buf, target));
isc_region_consume(source, 3);
loops++;
if (source->length != 0 &&
(int)((loops + 1) * 4) >= wordlength)
{
loops = 0;
RETERR(isc_str_tobuffer(wordbreak, target));
}
}
if (source->length == 2) {
buf[0] = base64[(source->base[0]>>2)&0x3f];
buf[1] = base64[((source->base[0]<<4)&0x30)|
((source->base[1]>>4)&0x0f)];
buf[2] = base64[((source->base[1]<<2)&0x3c)];
buf[3] = '=';
RETERR(isc_str_tobuffer(buf, target));
isc_region_consume(source, 2);
} else if (source->length == 1) {
buf[0] = base64[(source->base[0]>>2)&0x3f];
buf[1] = base64[((source->base[0]<<4)&0x30)];
buf[2] = buf[3] = '=';
RETERR(isc_str_tobuffer(buf, target));
isc_region_consume(source, 1);
}
return (ISC_R_SUCCESS);
}
typedef struct {
int length;
isc_buffer_t *target;
int digits;
int seen_end;
int val[4];
} base64_decode_ctx_t;
static inline void
base64_decode_init(base64_decode_ctx_t *ctx, int length, isc_buffer_t *target)
{
ctx->digits = 0;
ctx->seen_end = 0;
ctx->length = length;
ctx->target = target;
}
static inline isc_result_t
base64_decode_char(base64_decode_ctx_t *ctx, int c) {
const char *s;
if (ctx->seen_end)
return (ISC_R_BADBASE64);
if ((s = strchr(base64, c)) == NULL)
return (ISC_R_BADBASE64);
ctx->val[ctx->digits++] = (int)(s - base64);
if (ctx->digits == 4) {
int n;
unsigned char buf[3];
if (ctx->val[0] == 64 || ctx->val[1] == 64)
return (ISC_R_BADBASE64);
if (ctx->val[2] == 64 && ctx->val[3] != 64)
return (ISC_R_BADBASE64);
if (ctx->val[2] == 64 && (ctx->val[1] & 0xf) != 0)
return (ISC_R_BADBASE64);
if (ctx->val[3] == 64 && (ctx->val[2] & 0x3) != 0)
return (ISC_R_BADBASE64);
n = (ctx->val[2] == 64) ? 1 :
(ctx->val[3] == 64) ? 2 : 3;
if (n != 3) {
ctx->seen_end = 1;
if (ctx->val[2] == 64)
ctx->val[2] = 0;
if (ctx->val[3] == 64)
ctx->val[3] = 0;
}
buf[0] = (ctx->val[0]<<2)|(ctx->val[1]>>4);
buf[1] = (ctx->val[1]<<4)|(ctx->val[2]>>2);
buf[2] = (ctx->val[2]<<6)|(ctx->val[3]);
RETERR(isc_mem_tobuffer(ctx->target, buf, n));
if (ctx->length >= 0) {
if (n > ctx->length)
return (ISC_R_BADBASE64);
else
ctx->length -= n;
}
ctx->digits = 0;
}
return (ISC_R_SUCCESS);
}
static inline isc_result_t
base64_decode_finish(base64_decode_ctx_t *ctx) {
if (ctx->length > 0)
return (ISC_R_UNEXPECTEDEND);
if (ctx->digits != 0)
return (ISC_R_BADBASE64);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_base64_decodestring(const char *cstr, isc_buffer_t *target) {
base64_decode_ctx_t ctx;
base64_decode_init(&ctx, -1, target);
for (;;) {
int c = *cstr++;
if (c == '\0')
break;
if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
continue;
RETERR(base64_decode_char(&ctx, c));
}
RETERR(base64_decode_finish(&ctx));
return (ISC_R_SUCCESS);
}