#include <isc/base32.h>
#include <isc/buffer.h>
#include <isc/region.h>
#include <string.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 base32hex[] =
"0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
static isc_result_t
base32_totext(isc_region_t *source, int wordlength, const char *wordbreak,
isc_buffer_t *target, const char base[], char pad)
{
char buf[9];
unsigned int loops = 0;
if (wordlength >= 0 && wordlength < 8)
wordlength = 8;
memset(buf, 0, sizeof(buf));
while (source->length > 0) {
buf[0] = base[((source->base[0]>>3)&0x1f)];
if (source->length == 1) {
buf[1] = base[(source->base[0]<<2)&0x1c];
buf[2] = buf[3] = buf[4] = pad;
buf[5] = buf[6] = buf[7] = pad;
RETERR(isc_str_tobuffer(buf, target));
break;
}
buf[1] = base[((source->base[0]<<2)&0x1c)|
((source->base[1]>>6)&0x03)];
buf[2] = base[((source->base[1]>>1)&0x1f)];
if (source->length == 2) {
buf[3] = base[(source->base[1]<<4)&0x10];
buf[4] = buf[5] = buf[6] = buf[7] = pad;
RETERR(isc_str_tobuffer(buf, target));
break;
}
buf[3] = base[((source->base[1]<<4)&0x10)|
((source->base[2]>>4)&0x0f)];
if (source->length == 3) {
buf[4] = base[(source->base[2]<<1)&0x1e];
buf[5] = buf[6] = buf[7] = pad;
RETERR(isc_str_tobuffer(buf, target));
break;
}
buf[4] = base[((source->base[2]<<1)&0x1e)|
((source->base[3]>>7)&0x01)];
buf[5] = base[((source->base[3]>>2)&0x1f)];
if (source->length == 4) {
buf[6] = base[(source->base[3]<<3)&0x18];
buf[7] = pad;
RETERR(isc_str_tobuffer(buf, target));
break;
}
buf[6] = base[((source->base[3]<<3)&0x18)|
((source->base[4]>>5)&0x07)];
buf[7] = base[source->base[4]&0x1f];
RETERR(isc_str_tobuffer(buf, target));
isc_region_consume(source, 5);
loops++;
if (source->length != 0 && wordlength >= 0 &&
(int)((loops + 1) * 8) >= wordlength)
{
loops = 0;
RETERR(isc_str_tobuffer(wordbreak, target));
}
}
if (source->length > 0)
isc_region_consume(source, source->length);
return (ISC_R_SUCCESS);
}
isc_result_t
isc_base32hexnp_totext(isc_region_t *source, int wordlength,
const char *wordbreak, isc_buffer_t *target)
{
return (base32_totext(source, wordlength, wordbreak, target,
base32hex, 0));
}
typedef struct {
int length;
isc_buffer_t *target;
int digits;
int seen_end;
int val[8];
const char *base;
int seen_32;
int pad;
} base32_decode_ctx_t;
static inline void
base32_decode_init(base32_decode_ctx_t *ctx, int length, const char base[],
int pad, isc_buffer_t *target)
{
ctx->digits = 0;
ctx->seen_end = 0;
ctx->seen_32 = 0;
ctx->length = length;
ctx->target = target;
ctx->base = base;
ctx->pad = pad;
}
static inline isc_result_t
base32_decode_char(base32_decode_ctx_t *ctx, int c) {
const char *s;
unsigned int last;
if (ctx->seen_end)
return (ISC_R_BADBASE32);
if ((s = strchr(ctx->base, c)) == NULL)
return (ISC_R_BADBASE32);
last = (unsigned int)(s - ctx->base);
if (last > 32)
last -= 33;
if (last != 32 && ctx->seen_32 != 0)
return (ISC_R_BADBASE32);
if (last == 32 && !ctx->pad)
return (ISC_R_BADBASE32);
if (last == 32 && ctx->seen_32 == 0)
switch (ctx->digits) {
case 0:
case 1:
return (ISC_R_BADBASE32);
case 2:
if ((ctx->val[1]&0x03) != 0)
return (ISC_R_BADBASE32);
ctx->seen_32 = 1;
break;
case 3:
return (ISC_R_BADBASE32);
case 4:
if ((ctx->val[3]&0x0f) != 0)
return (ISC_R_BADBASE32);
ctx->seen_32 = 3;
break;
case 5:
if ((ctx->val[4]&0x01) != 0)
return (ISC_R_BADBASE32);
ctx->seen_32 = 3;
break;
case 6:
return (ISC_R_BADBASE32);
case 7:
if ((ctx->val[6]&0x07) != 0)
return (ISC_R_BADBASE32);
ctx->seen_32 = 4;
break;
}
ctx->val[ctx->digits++] = (last == 32) ? 0 : last;
if (ctx->digits == 8) {
int n = 5;
unsigned char buf[5];
if (ctx->seen_32 != 0) {
ctx->seen_end = 1;
n = ctx->seen_32;
}
buf[0] = (ctx->val[0]<<3)|(ctx->val[1]>>2);
buf[1] = (ctx->val[1]<<6)|(ctx->val[2]<<1)|(ctx->val[3]>>4);
buf[2] = (ctx->val[3]<<4)|(ctx->val[4]>>1);
buf[3] = (ctx->val[4]<<7)|(ctx->val[5]<<2)|(ctx->val[6]>>3);
buf[4] = (ctx->val[6]<<5)|(ctx->val[7]);
RETERR(isc_mem_tobuffer(ctx->target, buf, n));
if (ctx->length >= 0) {
if (n > ctx->length)
return (ISC_R_BADBASE32);
else
ctx->length -= n;
}
ctx->digits = 0;
}
return (ISC_R_SUCCESS);
}
static inline isc_result_t
base32_decode_finish(base32_decode_ctx_t *ctx) {
if (ctx->length > 0)
return (ISC_R_UNEXPECTEDEND);
if (!ctx->pad && ctx->digits != 0) {
ctx->pad = 1;
do {
RETERR(base32_decode_char(ctx, '='));
} while (ctx->digits != 0);
}
if (ctx->digits != 0)
return (ISC_R_BADBASE32);
return (ISC_R_SUCCESS);
}
static isc_result_t
base32_decoderegion(isc_region_t *source, const char base[],
int pad, isc_buffer_t *target)
{
base32_decode_ctx_t ctx;
base32_decode_init(&ctx, -1, base, pad, target);
while (source->length != 0) {
int c = *source->base;
RETERR(base32_decode_char(&ctx, c));
isc_region_consume(source, 1);
}
RETERR(base32_decode_finish(&ctx));
return (ISC_R_SUCCESS);
}
isc_result_t
isc_base32hexnp_decoderegion(isc_region_t *source, isc_buffer_t *target) {
return (base32_decoderegion(source, base32hex, 0, target));
}