base32.c revision 36e5ac00333d89001f0c518a7d381d16c38d0402
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews/*
431a83fb29482c5170b3e4026e59bb14849a6707Tinderbox User * Copyright (C) 2008, 2009, 2013, 2014 Internet Systems Consortium, Inc. ("ISC")
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews *
f052a01ff268cdbbf3e6f600b06d02ed8573badfAutomatic Updater * Permission to use, copy, modify, and/or distribute this software for any
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * purpose with or without fee is hereby granted, provided that the above
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * copyright notice and this permission notice appear in all copies.
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews *
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * PERFORMANCE OF THIS SOFTWARE.
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews/* $Id: base32.c,v 1.6.698.1 2012/02/15 05:00:16 marka Exp $ */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews/*! \file */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews#include <config.h>
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews#include <isc/base32.h>
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews#include <isc/buffer.h>
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews#include <isc/lex.h>
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews#include <isc/region.h>
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews#include <isc/string.h>
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews#include <isc/util.h>
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews#define RETERR(x) do { \
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_result_t _r = (x); \
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (_r != ISC_R_SUCCESS) \
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (_r); \
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews } while (0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews/*@{*/
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews/*!
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * These static functions are also present in lib/dns/rdata.c. I'm not
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * sure where they should go. -- bwelling
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic isc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstr_totext(const char *source, isc_buffer_t *target);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic isc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsmem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews/*@}*/
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic const char base32[] =
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=abcdefghijklmnopqrstuvwxyz234567";
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic const char base32hex[] =
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic isc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsbase32_totext(isc_region_t *source, int wordlength, const char *wordbreak,
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews isc_buffer_t *target, const char base[], char pad)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews{
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews char buf[9];
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews unsigned int loops = 0;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (wordlength >= 0 && wordlength < 8)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews wordlength = 8;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews memset(buf, 0, sizeof(buf));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews while (source->length > 0) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[0] = base[((source->base[0]>>3)&0x1f)]; /* 5 + */
f052a01ff268cdbbf3e6f600b06d02ed8573badfAutomatic Updater if (source->length == 1) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[1] = base[(source->base[0]<<2)&0x1c];
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews buf[2] = buf[3] = buf[4] = pad;
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews buf[5] = buf[6] = buf[7] = pad;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(str_totext(buf, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[1] = base[((source->base[0]<<2)&0x1c)| /* 3 = 8 */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ((source->base[1]>>6)&0x03)]; /* 2 + */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[2] = base[((source->base[1]>>1)&0x1f)]; /* 5 + */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (source->length == 2) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[3] = base[(source->base[1]<<4)&0x10];
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews buf[4] = buf[5] = buf[6] = buf[7] = pad;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(str_totext(buf, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[3] = base[((source->base[1]<<4)&0x10)| /* 1 = 8 */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ((source->base[2]>>4)&0x0f)]; /* 4 + */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (source->length == 3) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[4] = base[(source->base[2]<<1)&0x1e];
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews buf[5] = buf[6] = buf[7] = pad;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(str_totext(buf, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[4] = base[((source->base[2]<<1)&0x1e)| /* 4 = 8 */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ((source->base[3]>>7)&0x01)]; /* 1 + */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[5] = base[((source->base[3]>>2)&0x1f)]; /* 5 + */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (source->length == 4) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[6] = base[(source->base[3]<<3)&0x18];
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews buf[7] = pad;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(str_totext(buf, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[6] = base[((source->base[3]<<3)&0x18)| /* 2 = 8 */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ((source->base[4]>>5)&0x07)]; /* 3 + */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[7] = base[source->base[4]&0x1f]; /* 5 = 8 */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(str_totext(buf, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_region_consume(source, 5);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews loops++;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (source->length != 0 && wordlength >= 0 &&
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews (int)((loops + 1) * 8) >= wordlength)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews loops = 0;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(str_totext(wordbreak, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
8ec993c774d7c996cb266d0ff5f1282fa3940f09Evan Hunt if (source->length > 0)
8ec993c774d7c996cb266d0ff5f1282fa3940f09Evan Hunt isc_region_consume(source, source->length);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_SUCCESS);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_base32_totext(isc_region_t *source, int wordlength,
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews const char *wordbreak, isc_buffer_t *target)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews{
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_totext(source, wordlength, wordbreak, target,
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews base32, '='));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_base32hex_totext(isc_region_t *source, int wordlength,
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews const char *wordbreak, isc_buffer_t *target)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews{
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (base32_totext(source, wordlength, wordbreak, target,
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews base32hex, '='));
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews}
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsisc_result_t
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsisc_base32hexnp_totext(isc_region_t *source, int wordlength,
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews const char *wordbreak, isc_buffer_t *target)
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews{
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_totext(source, wordlength, wordbreak, target,
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews base32hex, 0));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews/*%
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * State of a base32 decoding process in progress.
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewstypedef struct {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews int length; /*%< Desired length of binary data or -1 */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_buffer_t *target; /*%< Buffer for resulting binary data */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews int digits; /*%< Number of buffered base32 digits */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_boolean_t seen_end; /*%< True if "=" end marker seen */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews int val[8];
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews const char *base; /*%< Which encoding we are using */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews int seen_32; /*%< Number of significant bytes if non zero */
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews isc_boolean_t pad; /*%< Expect padding */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews} base32_decode_ctx_t;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic inline void
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsbase32_decode_init(base32_decode_ctx_t *ctx, int length, const char base[],
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews isc_boolean_t pad, isc_buffer_t *target)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews{
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->digits = 0;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->seen_end = ISC_FALSE;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->seen_32 = 0;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->length = length;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->target = target;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->base = base;
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews ctx->pad = pad;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic inline isc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsbase32_decode_char(base32_decode_ctx_t *ctx, int c) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews char *s;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews unsigned int last;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (ctx->seen_end)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if ((s = strchr(ctx->base, c)) == NULL)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
c3c8823fed039b3a2b8e5ca8bc2f3301d1dd840eMark Andrews last = (unsigned int)(s - ctx->base);
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews /*
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * Handle lower case.
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (last > 32)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews last -= 33;
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews /*
1016f2638f8a34a802ce41043d5369553ac14246Francis Dupont * Check that padding is contiguous.
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (last != 32 && ctx->seen_32 != 0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews /*
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews * If padding is not permitted flag padding as a error.
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews */
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews if (last == 32 && !ctx->pad)
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (ISC_R_BADBASE32);
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews /*
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * Check that padding starts at the right place and that
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * bits that should be zero are.
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * Record how many significant bytes in answer (seen_32).
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (last == 32 && ctx->seen_32 == 0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews switch (ctx->digits) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews case 0:
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews case 1:
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews case 2:
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if ((ctx->val[1]&0x03) != 0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->seen_32 = 1;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews case 3:
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews case 4:
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if ((ctx->val[3]&0x0f) != 0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->seen_32 = 3;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews case 5:
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if ((ctx->val[4]&0x01) != 0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->seen_32 = 3;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews case 6:
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews case 7:
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if ((ctx->val[6]&0x07) != 0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->seen_32 = 4;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews /*
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews * Zero fill pad values.
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews */
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->val[ctx->digits++] = (last == 32) ? 0 : last;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (ctx->digits == 8) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews int n = 5;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews unsigned char buf[5];
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (ctx->seen_32 != 0) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->seen_end = ISC_TRUE;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews n = ctx->seen_32;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[0] = (ctx->val[0]<<3)|(ctx->val[1]>>2);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[1] = (ctx->val[1]<<6)|(ctx->val[2]<<1)|(ctx->val[3]>>4);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[2] = (ctx->val[3]<<4)|(ctx->val[4]>>1);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[3] = (ctx->val[4]<<7)|(ctx->val[5]<<2)|(ctx->val[6]>>3);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews buf[4] = (ctx->val[6]<<5)|(ctx->val[7]);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(mem_tobuffer(ctx->target, buf, n));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (ctx->length >= 0) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (n > ctx->length)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews else
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->length -= n;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews ctx->digits = 0;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_SUCCESS);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic inline isc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsbase32_decode_finish(base32_decode_ctx_t *ctx) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (ctx->length > 0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_UNEXPECTEDEND);
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews /*
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews * Add missing padding if required.
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews */
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews if (!ctx->pad && ctx->digits != 0) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews ctx->pad = ISC_TRUE;
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews do {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews RETERR(base32_decode_char(ctx, '='));
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews } while (ctx->digits != 0);
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (ctx->digits != 0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_BADBASE32);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_SUCCESS);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic isc_result_t
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsbase32_tobuffer(isc_lex_t *lexer, const char base[], isc_boolean_t pad,
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews isc_buffer_t *target, int length)
f052a01ff268cdbbf3e6f600b06d02ed8573badfAutomatic Updater{
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews base32_decode_ctx_t ctx;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_textregion_t *tr;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_token_t token;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_boolean_t eol;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews base32_decode_init(&ctx, length, base, pad, target);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews while (!ctx.seen_end && (ctx.length != 0)) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews unsigned int i;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (length > 0)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews eol = ISC_FALSE;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews else
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews eol = ISC_TRUE;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(isc_lex_getmastertoken(lexer, &token,
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_tokentype_string, eol));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (token.type != isc_tokentype_string)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews tr = &token.value.as_textregion;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews for (i = 0; i < tr->length; i++)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(base32_decode_char(&ctx, tr->base[i]));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (ctx.length < 0 && !ctx.seen_end)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_lex_ungettoken(lexer, &token);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(base32_decode_finish(&ctx));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_SUCCESS);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_tobuffer(lexer, base32, ISC_TRUE, target, length));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_tobuffer(lexer, base32hex, ISC_TRUE, target, length));
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews}
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsisc_result_t
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsisc_base32hexnp_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_tobuffer(lexer, base32hex, ISC_FALSE, target, length));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic isc_result_t
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsbase32_decodestring(const char *cstr, const char base[], isc_boolean_t pad,
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews isc_buffer_t *target)
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews{
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews base32_decode_ctx_t ctx;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews base32_decode_init(&ctx, -1, base, pad, target);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews for (;;) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews int c = *cstr++;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (c == '\0')
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews break;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews continue;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(base32_decode_char(&ctx, c));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
f052a01ff268cdbbf3e6f600b06d02ed8573badfAutomatic Updater RETERR(base32_decode_finish(&ctx));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_SUCCESS);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_base32_decodestring(const char *cstr, isc_buffer_t *target) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_decodestring(cstr, base32, ISC_TRUE, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_base32hex_decodestring(const char *cstr, isc_buffer_t *target) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_decodestring(cstr, base32hex, ISC_TRUE, target));
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews}
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsisc_result_t
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsisc_base32hexnp_decodestring(const char *cstr, isc_buffer_t *target) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_decodestring(cstr, base32hex, ISC_FALSE, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic isc_result_t
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsbase32_decoderegion(isc_region_t *source, const char base[],
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews isc_boolean_t pad, isc_buffer_t *target)
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews{
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews base32_decode_ctx_t ctx;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews base32_decode_init(&ctx, -1, base, pad, target);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews while (source->length != 0) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews int c = *source->base;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews RETERR(base32_decode_char(&ctx, c));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_region_consume(source, 1);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews }
f052a01ff268cdbbf3e6f600b06d02ed8573badfAutomatic Updater RETERR(base32_decode_finish(&ctx));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_SUCCESS);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_decoderegion(source, base32, ISC_TRUE, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsisc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_decoderegion(source, base32hex, ISC_TRUE, target));
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews}
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsisc_result_t
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrewsisc_base32hexnp_decoderegion(isc_region_t *source, isc_buffer_t *target) {
36e5ac00333d89001f0c518a7d381d16c38d0402Mark Andrews return (base32_decoderegion(source, base32hex, ISC_FALSE, target));
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic isc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstr_totext(const char *source, isc_buffer_t *target) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews unsigned int l;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_region_t region;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_buffer_availableregion(target, &region);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews l = strlen(source);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (l > region.length)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_NOSPACE);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(region.base, source, l);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_buffer_add(target, l);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_SUCCESS);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsstatic isc_result_t
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrewsmem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_region_t tr;
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_buffer_availableregion(target, &tr);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews if (length > tr.length)
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_NOSPACE);
e851ea826066ac5a5b01c2c23218faa0273a12e8Evan Hunt memmove(tr.base, base, length);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews isc_buffer_add(target, length);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews return (ISC_R_SUCCESS);
77abeb5330ed5f87eb406c64a738c9edb403a171Mark Andrews}