base32.c 10.6 KB
Newer Older
Mark Andrews's avatar
Mark Andrews committed
1
/*
2
 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
Mark Andrews's avatar
Mark Andrews committed
3
 *
4 5 6
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 8 9
 *
 * See the COPYRIGHT file distributed with this work for additional
 * information regarding copyright ownership.
Mark Andrews's avatar
Mark Andrews committed
10 11 12 13 14 15 16
 */


/*! \file */

#include <config.h>

17 18
#include <stdbool.h>

Mark Andrews's avatar
Mark Andrews committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <isc/base32.h>
#include <isc/buffer.h>
#include <isc/lex.h>
#include <isc/region.h>
#include <isc/string.h>
#include <isc/util.h>

#define RETERR(x) do { \
	isc_result_t _r = (x); \
	if (_r != ISC_R_SUCCESS) \
		return (_r); \
	} while (0)


/*@{*/
/*!
 * These static functions are also present in lib/dns/rdata.c.  I'm not
 * sure where they should go. -- bwelling
 */
static isc_result_t
str_totext(const char *source, isc_buffer_t *target);

static isc_result_t
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);

/*@}*/

static const char base32[] =
	 "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=abcdefghijklmnopqrstuvwxyz234567";
static const char base32hex[] =
	"0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";

static isc_result_t
base32_totext(isc_region_t *source, int wordlength, const char *wordbreak,
53
	      isc_buffer_t *target, const char base[], char pad)
Mark Andrews's avatar
Mark Andrews committed
54 55 56 57 58 59 60 61 62 63
{
	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)];	/* 5 + */
Automatic Updater's avatar
Automatic Updater committed
64
		if (source->length == 1) {
Mark Andrews's avatar
Mark Andrews committed
65
			buf[1] = base[(source->base[0]<<2)&0x1c];
66 67
			buf[2] = buf[3] = buf[4] = pad;
			buf[5] = buf[6] = buf[7] = pad;
Mark Andrews's avatar
Mark Andrews committed
68 69 70 71 72 73 74 75
			RETERR(str_totext(buf, target));
			break;
		}
		buf[1] = base[((source->base[0]<<2)&0x1c)|	/* 3 = 8 */
			      ((source->base[1]>>6)&0x03)];	/* 2 + */
		buf[2] = base[((source->base[1]>>1)&0x1f)];	/* 5 + */
		if (source->length == 2) {
			buf[3] = base[(source->base[1]<<4)&0x10];
76
			buf[4] = buf[5] = buf[6] = buf[7] = pad;
Mark Andrews's avatar
Mark Andrews committed
77 78 79 80 81 82 83
			RETERR(str_totext(buf, target));
			break;
		}
		buf[3] = base[((source->base[1]<<4)&0x10)|	/* 1 = 8 */
			      ((source->base[2]>>4)&0x0f)];	/* 4 + */
		if (source->length == 3) {
			buf[4] = base[(source->base[2]<<1)&0x1e];
84
			buf[5] = buf[6] = buf[7] = pad;
Mark Andrews's avatar
Mark Andrews committed
85 86 87 88 89 90 91 92
			RETERR(str_totext(buf, target));
			break;
		}
		buf[4] = base[((source->base[2]<<1)&0x1e)|	/* 4 = 8 */
			      ((source->base[3]>>7)&0x01)];	/* 1 + */
		buf[5] = base[((source->base[3]>>2)&0x1f)];	/* 5 + */
		if (source->length == 4) {
			buf[6] = base[(source->base[3]<<3)&0x18];
93
			buf[7] = pad;
Mark Andrews's avatar
Mark Andrews committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
			RETERR(str_totext(buf, target));
			break;
		}
		buf[6] = base[((source->base[3]<<3)&0x18)|	/* 2 = 8 */
			      ((source->base[4]>>5)&0x07)];	/* 3 + */
		buf[7] = base[source->base[4]&0x1f];		/* 5 = 8 */
		RETERR(str_totext(buf, target));
		isc_region_consume(source, 5);

		loops++;
		if (source->length != 0 && wordlength >= 0 &&
		    (int)((loops + 1) * 8) >= wordlength)
		{
			loops = 0;
			RETERR(str_totext(wordbreak, target));
		}
	}
111 112
	if (source->length > 0)
		isc_region_consume(source, source->length);
Mark Andrews's avatar
Mark Andrews committed
113 114 115 116 117 118 119
	return (ISC_R_SUCCESS);
}

isc_result_t
isc_base32_totext(isc_region_t *source, int wordlength,
		  const char *wordbreak, isc_buffer_t *target)
{
120 121
	return (base32_totext(source, wordlength, wordbreak, target,
			      base32, '='));
Mark Andrews's avatar
Mark Andrews committed
122 123 124 125 126 127 128
}

isc_result_t
isc_base32hex_totext(isc_region_t *source, int wordlength,
		     const char *wordbreak, isc_buffer_t *target)
{
	return (base32_totext(source, wordlength, wordbreak, target,
129 130 131 132 133 134 135 136 137
			      base32hex, '='));
}

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));
Mark Andrews's avatar
Mark Andrews committed
138 139 140 141 142 143 144 145 146
}

/*%
 * State of a base32 decoding process in progress.
 */
typedef struct {
	int length;		/*%< Desired length of binary data or -1 */
	isc_buffer_t *target;	/*%< Buffer for resulting binary data */
	int digits;		/*%< Number of buffered base32 digits */
147
	bool seen_end;	/*%< True if "=" end marker seen */
Mark Andrews's avatar
Mark Andrews committed
148 149 150
	int val[8];
	const char *base;	/*%< Which encoding we are using */
	int seen_32;		/*%< Number of significant bytes if non zero */
151
	bool pad;	/*%< Expect padding */
Mark Andrews's avatar
Mark Andrews committed
152 153 154
} base32_decode_ctx_t;

static inline void
155
base32_decode_init(base32_decode_ctx_t *ctx, int length, const char base[],
156
		   bool pad, isc_buffer_t *target)
Mark Andrews's avatar
Mark Andrews committed
157 158
{
	ctx->digits = 0;
159
	ctx->seen_end = false;
Mark Andrews's avatar
Mark Andrews committed
160 161 162 163
	ctx->seen_32 = 0;
	ctx->length = length;
	ctx->target = target;
	ctx->base = base;
164
	ctx->pad = pad;
Mark Andrews's avatar
Mark Andrews committed
165 166 167 168
}

static inline isc_result_t
base32_decode_char(base32_decode_ctx_t *ctx, int c) {
169
	const char *s;
Mark Andrews's avatar
Mark Andrews committed
170 171 172 173 174 175
	unsigned int last;

	if (ctx->seen_end)
		return (ISC_R_BADBASE32);
	if ((s = strchr(ctx->base, c)) == NULL)
		return (ISC_R_BADBASE32);
176
	last = (unsigned int)(s - ctx->base);
177

Mark Andrews's avatar
Mark Andrews committed
178 179 180 181 182
	/*
	 * Handle lower case.
	 */
	if (last > 32)
		last -= 33;
183

Mark Andrews's avatar
Mark Andrews committed
184
	/*
Francis Dupont's avatar
Francis Dupont committed
185
	 * Check that padding is contiguous.
Mark Andrews's avatar
Mark Andrews committed
186 187 188
	 */
	if (last != 32 && ctx->seen_32 != 0)
		return (ISC_R_BADBASE32);
189 190 191 192 193 194 195

	/*
	 * If padding is not permitted flag padding as a error.
	 */
	if (last == 32 && !ctx->pad)
		return (ISC_R_BADBASE32);

Mark Andrews's avatar
Mark Andrews committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	/*
	 * Check that padding starts at the right place and that
	 * bits that should be zero are.
	 * Record how many significant bytes in answer (seen_32).
	 */
	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;
		}
231

Mark Andrews's avatar
Mark Andrews committed
232 233 234 235 236 237 238 239 240 241
	/*
	 * Zero fill pad values.
	 */
	ctx->val[ctx->digits++] = (last == 32) ? 0 : last;

	if (ctx->digits == 8) {
		int n = 5;
		unsigned char buf[5];

		if (ctx->seen_32 != 0) {
242
			ctx->seen_end = true;
Mark Andrews's avatar
Mark Andrews committed
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
			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(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) {
264

Mark Andrews's avatar
Mark Andrews committed
265 266
	if (ctx->length > 0)
		return (ISC_R_UNEXPECTEDEND);
267 268 269 270
	/*
	 * Add missing padding if required.
	 */
	if (!ctx->pad && ctx->digits != 0) {
271
		ctx->pad = true;
272 273 274 275
		do {
			RETERR(base32_decode_char(ctx, '='));
		} while (ctx->digits != 0);
	}
Mark Andrews's avatar
Mark Andrews committed
276 277 278 279 280 281
	if (ctx->digits != 0)
		return (ISC_R_BADBASE32);
	return (ISC_R_SUCCESS);
}

static isc_result_t
282
base32_tobuffer(isc_lex_t *lexer, const char base[], bool pad,
283
		isc_buffer_t *target, int length)
Automatic Updater's avatar
Automatic Updater committed
284
{
285
	unsigned int before, after;
Mark Andrews's avatar
Mark Andrews committed
286 287 288
	base32_decode_ctx_t ctx;
	isc_textregion_t *tr;
	isc_token_t token;
289
	bool eol;
Mark Andrews's avatar
Mark Andrews committed
290

291 292
	REQUIRE(length >= -2);

293
	base32_decode_init(&ctx, length, base, pad, target);
Mark Andrews's avatar
Mark Andrews committed
294

295
	before = isc_buffer_usedlength(target);
Mark Andrews's avatar
Mark Andrews committed
296 297 298
	while (!ctx.seen_end && (ctx.length != 0)) {
		unsigned int i;

299
		if (length > 0) {
300
			eol = false;
301
		} else {
302
			eol = true;
303
		}
Mark Andrews's avatar
Mark Andrews committed
304 305
		RETERR(isc_lex_getmastertoken(lexer, &token,
					      isc_tokentype_string, eol));
306
		if (token.type != isc_tokentype_string) {
Mark Andrews's avatar
Mark Andrews committed
307
			break;
308
		}
Mark Andrews's avatar
Mark Andrews committed
309
		tr = &token.value.as_textregion;
310
		for (i = 0; i < tr->length; i++) {
Mark Andrews's avatar
Mark Andrews committed
311
			RETERR(base32_decode_char(&ctx, tr->base[i]));
312
		}
Mark Andrews's avatar
Mark Andrews committed
313
	}
314 315
	after = isc_buffer_usedlength(target);
	if (ctx.length < 0 && !ctx.seen_end) {
Mark Andrews's avatar
Mark Andrews committed
316
		isc_lex_ungettoken(lexer, &token);
317
	}
Mark Andrews's avatar
Mark Andrews committed
318
	RETERR(base32_decode_finish(&ctx));
319 320 321
	if (length == -2 && before == after) {
		return (ISC_R_UNEXPECTEDEND);
	}
Mark Andrews's avatar
Mark Andrews committed
322 323 324 325 326
	return (ISC_R_SUCCESS);
}

isc_result_t
isc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
327
	return (base32_tobuffer(lexer, base32, true, target, length));
Mark Andrews's avatar
Mark Andrews committed
328 329 330 331
}

isc_result_t
isc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
332
	return (base32_tobuffer(lexer, base32hex, true, target, length));
333 334 335 336
}

isc_result_t
isc_base32hexnp_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
337
	return (base32_tobuffer(lexer, base32hex, false, target, length));
Mark Andrews's avatar
Mark Andrews committed
338 339 340
}

static isc_result_t
341
base32_decodestring(const char *cstr, const char base[], bool pad,
342 343
		    isc_buffer_t *target)
{
Mark Andrews's avatar
Mark Andrews committed
344 345
	base32_decode_ctx_t ctx;

346
	base32_decode_init(&ctx, -1, base, pad, target);
Mark Andrews's avatar
Mark Andrews committed
347 348 349 350 351 352 353 354
	for (;;) {
		int c = *cstr++;
		if (c == '\0')
			break;
		if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
			continue;
		RETERR(base32_decode_char(&ctx, c));
	}
Automatic Updater's avatar
Automatic Updater committed
355
	RETERR(base32_decode_finish(&ctx));
Mark Andrews's avatar
Mark Andrews committed
356 357 358 359 360
	return (ISC_R_SUCCESS);
}

isc_result_t
isc_base32_decodestring(const char *cstr, isc_buffer_t *target) {
361
	return (base32_decodestring(cstr, base32, true, target));
Mark Andrews's avatar
Mark Andrews committed
362 363 364 365
}

isc_result_t
isc_base32hex_decodestring(const char *cstr, isc_buffer_t *target) {
366
	return (base32_decodestring(cstr, base32hex, true, target));
367 368 369 370
}

isc_result_t
isc_base32hexnp_decodestring(const char *cstr, isc_buffer_t *target) {
371
	return (base32_decodestring(cstr, base32hex, false, target));
Mark Andrews's avatar
Mark Andrews committed
372 373 374
}

static isc_result_t
375
base32_decoderegion(isc_region_t *source, const char base[],
376
		    bool pad, isc_buffer_t *target)
377
{
Mark Andrews's avatar
Mark Andrews committed
378 379
	base32_decode_ctx_t ctx;

380
	base32_decode_init(&ctx, -1, base, pad, target);
Mark Andrews's avatar
Mark Andrews committed
381 382 383 384 385
	while (source->length != 0) {
		int c = *source->base;
		RETERR(base32_decode_char(&ctx, c));
		isc_region_consume(source, 1);
	}
Automatic Updater's avatar
Automatic Updater committed
386
	RETERR(base32_decode_finish(&ctx));
Mark Andrews's avatar
Mark Andrews committed
387 388 389 390 391
	return (ISC_R_SUCCESS);
}

isc_result_t
isc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target) {
392
	return (base32_decoderegion(source, base32, true, target));
Mark Andrews's avatar
Mark Andrews committed
393 394 395 396
}

isc_result_t
isc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target) {
397
	return (base32_decoderegion(source, base32hex, true, target));
398 399 400 401
}

isc_result_t
isc_base32hexnp_decoderegion(isc_region_t *source, isc_buffer_t *target) {
402
	return (base32_decoderegion(source, base32hex, false, target));
Mark Andrews's avatar
Mark Andrews committed
403 404 405 406 407 408 409 410 411 412 413 414 415
}

static isc_result_t
str_totext(const char *source, isc_buffer_t *target) {
	unsigned int l;
	isc_region_t region;

	isc_buffer_availableregion(target, &region);
	l = strlen(source);

	if (l > region.length)
		return (ISC_R_NOSPACE);

416
	memmove(region.base, source, l);
Mark Andrews's avatar
Mark Andrews committed
417 418 419 420 421 422 423 424 425 426 427
	isc_buffer_add(target, l);
	return (ISC_R_SUCCESS);
}

static isc_result_t
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
	isc_region_t tr;

	isc_buffer_availableregion(target, &tr);
	if (length > tr.length)
		return (ISC_R_NOSPACE);
428
	memmove(tr.base, base, length);
Mark Andrews's avatar
Mark Andrews committed
429 430 431
	isc_buffer_add(target, length);
	return (ISC_R_SUCCESS);
}