1258945Sroberto/*
2258945Sroberto * Copyright (C) 2008, 2009  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto *
4258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
5258945Sroberto * purpose with or without fee is hereby granted, provided that the above
6258945Sroberto * copyright notice and this permission notice appear in all copies.
7258945Sroberto *
8258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
15258945Sroberto */
16258945Sroberto
17280849Scy/* $Id: base32.c,v 1.6 2009/10/21 01:22:29 each Exp $ */
18258945Sroberto
19258945Sroberto/*! \file */
20258945Sroberto
21258945Sroberto#include <config.h>
22258945Sroberto
23258945Sroberto#include <isc/base32.h>
24258945Sroberto#include <isc/buffer.h>
25258945Sroberto#include <isc/lex.h>
26258945Sroberto#include <isc/region.h>
27258945Sroberto#include <isc/string.h>
28258945Sroberto#include <isc/util.h>
29258945Sroberto
30258945Sroberto#define RETERR(x) do { \
31258945Sroberto	isc_result_t _r = (x); \
32258945Sroberto	if (_r != ISC_R_SUCCESS) \
33258945Sroberto		return (_r); \
34258945Sroberto	} while (0)
35258945Sroberto
36258945Sroberto
37258945Sroberto/*@{*/
38258945Sroberto/*!
39258945Sroberto * These static functions are also present in lib/dns/rdata.c.  I'm not
40258945Sroberto * sure where they should go. -- bwelling
41258945Sroberto */
42258945Srobertostatic isc_result_t
43258945Srobertostr_totext(const char *source, isc_buffer_t *target);
44258945Sroberto
45258945Srobertostatic isc_result_t
46258945Srobertomem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
47258945Sroberto
48258945Sroberto/*@}*/
49258945Sroberto
50258945Srobertostatic const char base32[] =
51258945Sroberto	 "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=abcdefghijklmnopqrstuvwxyz234567";
52258945Srobertostatic const char base32hex[] =
53258945Sroberto	"0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
54258945Sroberto
55258945Srobertostatic isc_result_t
56258945Srobertobase32_totext(isc_region_t *source, int wordlength, const char *wordbreak,
57258945Sroberto	      isc_buffer_t *target, const char base[])
58258945Sroberto{
59258945Sroberto	char buf[9];
60258945Sroberto	unsigned int loops = 0;
61258945Sroberto
62258945Sroberto	if (wordlength >= 0 && wordlength < 8)
63258945Sroberto		wordlength = 8;
64258945Sroberto
65258945Sroberto	memset(buf, 0, sizeof(buf));
66258945Sroberto	while (source->length > 0) {
67258945Sroberto		buf[0] = base[((source->base[0]>>3)&0x1f)];	/* 5 + */
68258945Sroberto		if (source->length == 1) {
69258945Sroberto			buf[1] = base[(source->base[0]<<2)&0x1c];
70258945Sroberto			buf[2] = buf[3] = buf[4] = '=';
71258945Sroberto			buf[5] = buf[6] = buf[7] = '=';
72258945Sroberto			RETERR(str_totext(buf, target));
73258945Sroberto			break;
74258945Sroberto		}
75258945Sroberto		buf[1] = base[((source->base[0]<<2)&0x1c)|	/* 3 = 8 */
76258945Sroberto			      ((source->base[1]>>6)&0x03)];	/* 2 + */
77258945Sroberto		buf[2] = base[((source->base[1]>>1)&0x1f)];	/* 5 + */
78258945Sroberto		if (source->length == 2) {
79258945Sroberto			buf[3] = base[(source->base[1]<<4)&0x10];
80258945Sroberto			buf[4] = buf[5] = buf[6] = buf[7] = '=';
81258945Sroberto			RETERR(str_totext(buf, target));
82258945Sroberto			break;
83258945Sroberto		}
84258945Sroberto		buf[3] = base[((source->base[1]<<4)&0x10)|	/* 1 = 8 */
85258945Sroberto			      ((source->base[2]>>4)&0x0f)];	/* 4 + */
86258945Sroberto		if (source->length == 3) {
87258945Sroberto			buf[4] = base[(source->base[2]<<1)&0x1e];
88258945Sroberto			buf[5] = buf[6] = buf[7] = '=';
89258945Sroberto			RETERR(str_totext(buf, target));
90258945Sroberto			break;
91258945Sroberto		}
92258945Sroberto		buf[4] = base[((source->base[2]<<1)&0x1e)|	/* 4 = 8 */
93258945Sroberto			      ((source->base[3]>>7)&0x01)];	/* 1 + */
94258945Sroberto		buf[5] = base[((source->base[3]>>2)&0x1f)];	/* 5 + */
95258945Sroberto		if (source->length == 4) {
96258945Sroberto			buf[6] = base[(source->base[3]<<3)&0x18];
97258945Sroberto			buf[7] = '=';
98258945Sroberto			RETERR(str_totext(buf, target));
99258945Sroberto			break;
100258945Sroberto		}
101258945Sroberto		buf[6] = base[((source->base[3]<<3)&0x18)|	/* 2 = 8 */
102258945Sroberto			      ((source->base[4]>>5)&0x07)];	/* 3 + */
103258945Sroberto		buf[7] = base[source->base[4]&0x1f];		/* 5 = 8 */
104258945Sroberto		RETERR(str_totext(buf, target));
105258945Sroberto		isc_region_consume(source, 5);
106258945Sroberto
107258945Sroberto		loops++;
108258945Sroberto		if (source->length != 0 && wordlength >= 0 &&
109258945Sroberto		    (int)((loops + 1) * 8) >= wordlength)
110258945Sroberto		{
111258945Sroberto			loops = 0;
112258945Sroberto			RETERR(str_totext(wordbreak, target));
113258945Sroberto		}
114258945Sroberto	}
115280849Scy	if (source->length > 0)
116280849Scy		isc_region_consume(source, source->length);
117258945Sroberto	return (ISC_R_SUCCESS);
118258945Sroberto}
119258945Sroberto
120258945Srobertoisc_result_t
121258945Srobertoisc_base32_totext(isc_region_t *source, int wordlength,
122258945Sroberto		  const char *wordbreak, isc_buffer_t *target)
123258945Sroberto{
124258945Sroberto	return (base32_totext(source, wordlength, wordbreak, target, base32));
125258945Sroberto}
126258945Sroberto
127258945Srobertoisc_result_t
128258945Srobertoisc_base32hex_totext(isc_region_t *source, int wordlength,
129258945Sroberto		     const char *wordbreak, isc_buffer_t *target)
130258945Sroberto{
131258945Sroberto	return (base32_totext(source, wordlength, wordbreak, target,
132258945Sroberto			      base32hex));
133258945Sroberto}
134258945Sroberto
135258945Sroberto/*%
136258945Sroberto * State of a base32 decoding process in progress.
137258945Sroberto */
138258945Srobertotypedef struct {
139258945Sroberto	int length;		/*%< Desired length of binary data or -1 */
140258945Sroberto	isc_buffer_t *target;	/*%< Buffer for resulting binary data */
141258945Sroberto	int digits;		/*%< Number of buffered base32 digits */
142258945Sroberto	isc_boolean_t seen_end;	/*%< True if "=" end marker seen */
143258945Sroberto	int val[8];
144258945Sroberto	const char *base;	/*%< Which encoding we are using */
145258945Sroberto	int seen_32;		/*%< Number of significant bytes if non zero */
146258945Sroberto} base32_decode_ctx_t;
147258945Sroberto
148258945Srobertostatic inline void
149258945Srobertobase32_decode_init(base32_decode_ctx_t *ctx, int length,
150258945Sroberto		   const char base[], isc_buffer_t *target)
151258945Sroberto{
152258945Sroberto	ctx->digits = 0;
153258945Sroberto	ctx->seen_end = ISC_FALSE;
154258945Sroberto	ctx->seen_32 = 0;
155258945Sroberto	ctx->length = length;
156258945Sroberto	ctx->target = target;
157258945Sroberto	ctx->base = base;
158258945Sroberto}
159258945Sroberto
160258945Srobertostatic inline isc_result_t
161258945Srobertobase32_decode_char(base32_decode_ctx_t *ctx, int c) {
162258945Sroberto	char *s;
163258945Sroberto	unsigned int last;
164258945Sroberto
165258945Sroberto	if (ctx->seen_end)
166258945Sroberto		return (ISC_R_BADBASE32);
167258945Sroberto	if ((s = strchr(ctx->base, c)) == NULL)
168258945Sroberto		return (ISC_R_BADBASE32);
169258945Sroberto	last = s - ctx->base;
170258945Sroberto	/*
171258945Sroberto	 * Handle lower case.
172258945Sroberto	 */
173258945Sroberto	if (last > 32)
174258945Sroberto		last -= 33;
175258945Sroberto	/*
176258945Sroberto	 * Check that padding is contiguous.
177258945Sroberto	 */
178258945Sroberto	if (last != 32 && ctx->seen_32 != 0)
179258945Sroberto		return (ISC_R_BADBASE32);
180258945Sroberto	/*
181258945Sroberto	 * Check that padding starts at the right place and that
182258945Sroberto	 * bits that should be zero are.
183258945Sroberto	 * Record how many significant bytes in answer (seen_32).
184258945Sroberto	 */
185258945Sroberto	if (last == 32 && ctx->seen_32 == 0)
186258945Sroberto		switch (ctx->digits) {
187258945Sroberto		case 0:
188258945Sroberto		case 1:
189258945Sroberto			return (ISC_R_BADBASE32);
190258945Sroberto		case 2:
191258945Sroberto			if ((ctx->val[1]&0x03) != 0)
192258945Sroberto				return (ISC_R_BADBASE32);
193258945Sroberto			ctx->seen_32 = 1;
194258945Sroberto			break;
195258945Sroberto		case 3:
196258945Sroberto			return (ISC_R_BADBASE32);
197258945Sroberto		case 4:
198258945Sroberto			if ((ctx->val[3]&0x0f) != 0)
199258945Sroberto				return (ISC_R_BADBASE32);
200258945Sroberto			ctx->seen_32 = 3;
201258945Sroberto			break;
202258945Sroberto		case 5:
203258945Sroberto			if ((ctx->val[4]&0x01) != 0)
204258945Sroberto				return (ISC_R_BADBASE32);
205258945Sroberto			ctx->seen_32 = 3;
206258945Sroberto			break;
207258945Sroberto		case 6:
208258945Sroberto			return (ISC_R_BADBASE32);
209258945Sroberto		case 7:
210258945Sroberto			if ((ctx->val[6]&0x07) != 0)
211258945Sroberto				return (ISC_R_BADBASE32);
212258945Sroberto			ctx->seen_32 = 4;
213258945Sroberto			break;
214258945Sroberto		}
215258945Sroberto	/*
216258945Sroberto	 * Zero fill pad values.
217258945Sroberto	 */
218258945Sroberto	ctx->val[ctx->digits++] = (last == 32) ? 0 : last;
219258945Sroberto
220258945Sroberto	if (ctx->digits == 8) {
221258945Sroberto		int n = 5;
222258945Sroberto		unsigned char buf[5];
223258945Sroberto
224258945Sroberto		if (ctx->seen_32 != 0) {
225258945Sroberto			ctx->seen_end = ISC_TRUE;
226258945Sroberto			n = ctx->seen_32;
227258945Sroberto		}
228258945Sroberto		buf[0] = (ctx->val[0]<<3)|(ctx->val[1]>>2);
229258945Sroberto		buf[1] = (ctx->val[1]<<6)|(ctx->val[2]<<1)|(ctx->val[3]>>4);
230258945Sroberto		buf[2] = (ctx->val[3]<<4)|(ctx->val[4]>>1);
231258945Sroberto		buf[3] = (ctx->val[4]<<7)|(ctx->val[5]<<2)|(ctx->val[6]>>3);
232258945Sroberto		buf[4] = (ctx->val[6]<<5)|(ctx->val[7]);
233258945Sroberto		RETERR(mem_tobuffer(ctx->target, buf, n));
234258945Sroberto		if (ctx->length >= 0) {
235258945Sroberto			if (n > ctx->length)
236258945Sroberto				return (ISC_R_BADBASE32);
237258945Sroberto			else
238258945Sroberto				ctx->length -= n;
239258945Sroberto		}
240258945Sroberto		ctx->digits = 0;
241258945Sroberto	}
242258945Sroberto	return (ISC_R_SUCCESS);
243258945Sroberto}
244258945Sroberto
245258945Srobertostatic inline isc_result_t
246258945Srobertobase32_decode_finish(base32_decode_ctx_t *ctx) {
247258945Sroberto	if (ctx->length > 0)
248258945Sroberto		return (ISC_R_UNEXPECTEDEND);
249258945Sroberto	if (ctx->digits != 0)
250258945Sroberto		return (ISC_R_BADBASE32);
251258945Sroberto	return (ISC_R_SUCCESS);
252258945Sroberto}
253258945Sroberto
254258945Srobertostatic isc_result_t
255258945Srobertobase32_tobuffer(isc_lex_t *lexer, const char base[], isc_buffer_t *target,
256258945Sroberto		int length)
257258945Sroberto{
258258945Sroberto	base32_decode_ctx_t ctx;
259258945Sroberto	isc_textregion_t *tr;
260258945Sroberto	isc_token_t token;
261258945Sroberto	isc_boolean_t eol;
262258945Sroberto
263258945Sroberto	base32_decode_init(&ctx, length, base, target);
264258945Sroberto
265258945Sroberto	while (!ctx.seen_end && (ctx.length != 0)) {
266258945Sroberto		unsigned int i;
267258945Sroberto
268258945Sroberto		if (length > 0)
269258945Sroberto			eol = ISC_FALSE;
270258945Sroberto		else
271258945Sroberto			eol = ISC_TRUE;
272258945Sroberto		RETERR(isc_lex_getmastertoken(lexer, &token,
273258945Sroberto					      isc_tokentype_string, eol));
274258945Sroberto		if (token.type != isc_tokentype_string)
275258945Sroberto			break;
276258945Sroberto		tr = &token.value.as_textregion;
277258945Sroberto		for (i = 0; i < tr->length; i++)
278258945Sroberto			RETERR(base32_decode_char(&ctx, tr->base[i]));
279258945Sroberto	}
280258945Sroberto	if (ctx.length < 0 && !ctx.seen_end)
281258945Sroberto		isc_lex_ungettoken(lexer, &token);
282258945Sroberto	RETERR(base32_decode_finish(&ctx));
283258945Sroberto	return (ISC_R_SUCCESS);
284258945Sroberto}
285258945Sroberto
286258945Srobertoisc_result_t
287258945Srobertoisc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
288258945Sroberto	return (base32_tobuffer(lexer, base32, target, length));
289258945Sroberto}
290258945Sroberto
291258945Srobertoisc_result_t
292258945Srobertoisc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
293258945Sroberto	return (base32_tobuffer(lexer, base32hex, target, length));
294258945Sroberto}
295258945Sroberto
296258945Srobertostatic isc_result_t
297258945Srobertobase32_decodestring(const char *cstr, const char base[], isc_buffer_t *target) {
298258945Sroberto	base32_decode_ctx_t ctx;
299258945Sroberto
300258945Sroberto	base32_decode_init(&ctx, -1, base, target);
301258945Sroberto	for (;;) {
302258945Sroberto		int c = *cstr++;
303258945Sroberto		if (c == '\0')
304258945Sroberto			break;
305258945Sroberto		if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
306258945Sroberto			continue;
307258945Sroberto		RETERR(base32_decode_char(&ctx, c));
308258945Sroberto	}
309258945Sroberto	RETERR(base32_decode_finish(&ctx));
310258945Sroberto	return (ISC_R_SUCCESS);
311258945Sroberto}
312258945Sroberto
313258945Srobertoisc_result_t
314258945Srobertoisc_base32_decodestring(const char *cstr, isc_buffer_t *target) {
315258945Sroberto	return (base32_decodestring(cstr, base32, target));
316258945Sroberto}
317258945Sroberto
318258945Srobertoisc_result_t
319258945Srobertoisc_base32hex_decodestring(const char *cstr, isc_buffer_t *target) {
320258945Sroberto	return (base32_decodestring(cstr, base32hex, target));
321258945Sroberto}
322258945Sroberto
323258945Srobertostatic isc_result_t
324258945Srobertobase32_decoderegion(isc_region_t *source, const char base[], isc_buffer_t *target) {
325258945Sroberto	base32_decode_ctx_t ctx;
326258945Sroberto
327258945Sroberto	base32_decode_init(&ctx, -1, base, target);
328258945Sroberto	while (source->length != 0) {
329258945Sroberto		int c = *source->base;
330258945Sroberto		RETERR(base32_decode_char(&ctx, c));
331258945Sroberto		isc_region_consume(source, 1);
332258945Sroberto	}
333258945Sroberto	RETERR(base32_decode_finish(&ctx));
334258945Sroberto	return (ISC_R_SUCCESS);
335258945Sroberto}
336258945Sroberto
337258945Srobertoisc_result_t
338258945Srobertoisc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target) {
339258945Sroberto	return (base32_decoderegion(source, base32, target));
340258945Sroberto}
341258945Sroberto
342258945Srobertoisc_result_t
343258945Srobertoisc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target) {
344258945Sroberto	return (base32_decoderegion(source, base32hex, target));
345258945Sroberto}
346258945Sroberto
347258945Srobertostatic isc_result_t
348258945Srobertostr_totext(const char *source, isc_buffer_t *target) {
349258945Sroberto	unsigned int l;
350258945Sroberto	isc_region_t region;
351258945Sroberto
352258945Sroberto	isc_buffer_availableregion(target, &region);
353258945Sroberto	l = strlen(source);
354258945Sroberto
355258945Sroberto	if (l > region.length)
356258945Sroberto		return (ISC_R_NOSPACE);
357258945Sroberto
358258945Sroberto	memcpy(region.base, source, l);
359258945Sroberto	isc_buffer_add(target, l);
360258945Sroberto	return (ISC_R_SUCCESS);
361258945Sroberto}
362258945Sroberto
363258945Srobertostatic isc_result_t
364258945Srobertomem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
365258945Sroberto	isc_region_t tr;
366258945Sroberto
367258945Sroberto	isc_buffer_availableregion(target, &tr);
368258945Sroberto	if (length > tr.length)
369258945Sroberto		return (ISC_R_NOSPACE);
370258945Sroberto	memcpy(tr.base, base, length);
371258945Sroberto	isc_buffer_add(target, length);
372258945Sroberto	return (ISC_R_SUCCESS);
373258945Sroberto}
374