2 * Copyright (C) 2008, 2009, 2013-2015 Internet Systems Consortium, Inc. ("ISC")
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
17 /* $Id: base32.c,v 1.6.698.1 2012/02/15 05:00:16 marka Exp $ */
23 #include <isc/base32.h>
24 #include <isc/buffer.h>
26 #include <isc/region.h>
27 #include <isc/string.h>
30 #define RETERR(x) do { \
31 isc_result_t _r = (x); \
32 if (_r != ISC_R_SUCCESS) \
39 * These static functions are also present in lib/dns/rdata.c. I'm not
40 * sure where they should go. -- bwelling
43 str_totext(const char *source, isc_buffer_t *target);
46 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
50 static const char base32[] =
51 "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=abcdefghijklmnopqrstuvwxyz234567";
52 static const char base32hex[] =
53 "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
56 base32_totext(isc_region_t *source, int wordlength, const char *wordbreak,
57 isc_buffer_t *target, const char base[], char pad)
60 unsigned int loops = 0;
62 if (wordlength >= 0 && wordlength < 8)
65 memset(buf, 0, sizeof(buf));
66 while (source->length > 0) {
67 buf[0] = base[((source->base[0]>>3)&0x1f)]; /* 5 + */
68 if (source->length == 1) {
69 buf[1] = base[(source->base[0]<<2)&0x1c];
70 buf[2] = buf[3] = buf[4] = pad;
71 buf[5] = buf[6] = buf[7] = pad;
72 RETERR(str_totext(buf, target));
75 buf[1] = base[((source->base[0]<<2)&0x1c)| /* 3 = 8 */
76 ((source->base[1]>>6)&0x03)]; /* 2 + */
77 buf[2] = base[((source->base[1]>>1)&0x1f)]; /* 5 + */
78 if (source->length == 2) {
79 buf[3] = base[(source->base[1]<<4)&0x10];
80 buf[4] = buf[5] = buf[6] = buf[7] = pad;
81 RETERR(str_totext(buf, target));
84 buf[3] = base[((source->base[1]<<4)&0x10)| /* 1 = 8 */
85 ((source->base[2]>>4)&0x0f)]; /* 4 + */
86 if (source->length == 3) {
87 buf[4] = base[(source->base[2]<<1)&0x1e];
88 buf[5] = buf[6] = buf[7] = pad;
89 RETERR(str_totext(buf, target));
92 buf[4] = base[((source->base[2]<<1)&0x1e)| /* 4 = 8 */
93 ((source->base[3]>>7)&0x01)]; /* 1 + */
94 buf[5] = base[((source->base[3]>>2)&0x1f)]; /* 5 + */
95 if (source->length == 4) {
96 buf[6] = base[(source->base[3]<<3)&0x18];
98 RETERR(str_totext(buf, target));
101 buf[6] = base[((source->base[3]<<3)&0x18)| /* 2 = 8 */
102 ((source->base[4]>>5)&0x07)]; /* 3 + */
103 buf[7] = base[source->base[4]&0x1f]; /* 5 = 8 */
104 RETERR(str_totext(buf, target));
105 isc_region_consume(source, 5);
108 if (source->length != 0 && wordlength >= 0 &&
109 (int)((loops + 1) * 8) >= wordlength)
112 RETERR(str_totext(wordbreak, target));
115 if (source->length > 0)
116 isc_region_consume(source, source->length);
117 return (ISC_R_SUCCESS);
121 isc_base32_totext(isc_region_t *source, int wordlength,
122 const char *wordbreak, isc_buffer_t *target)
124 return (base32_totext(source, wordlength, wordbreak, target,
129 isc_base32hex_totext(isc_region_t *source, int wordlength,
130 const char *wordbreak, isc_buffer_t *target)
132 return (base32_totext(source, wordlength, wordbreak, target,
137 isc_base32hexnp_totext(isc_region_t *source, int wordlength,
138 const char *wordbreak, isc_buffer_t *target)
140 return (base32_totext(source, wordlength, wordbreak, target,
145 * State of a base32 decoding process in progress.
148 int length; /*%< Desired length of binary data or -1 */
149 isc_buffer_t *target; /*%< Buffer for resulting binary data */
150 int digits; /*%< Number of buffered base32 digits */
151 isc_boolean_t seen_end; /*%< True if "=" end marker seen */
153 const char *base; /*%< Which encoding we are using */
154 int seen_32; /*%< Number of significant bytes if non zero */
155 isc_boolean_t pad; /*%< Expect padding */
156 } base32_decode_ctx_t;
159 base32_decode_init(base32_decode_ctx_t *ctx, int length, const char base[],
160 isc_boolean_t pad, isc_buffer_t *target)
163 ctx->seen_end = ISC_FALSE;
165 ctx->length = length;
166 ctx->target = target;
171 static inline isc_result_t
172 base32_decode_char(base32_decode_ctx_t *ctx, int c) {
177 return (ISC_R_BADBASE32);
178 if ((s = strchr(ctx->base, c)) == NULL)
179 return (ISC_R_BADBASE32);
180 last = (unsigned int)(s - ctx->base);
189 * Check that padding is contiguous.
191 if (last != 32 && ctx->seen_32 != 0)
192 return (ISC_R_BADBASE32);
195 * If padding is not permitted flag padding as a error.
197 if (last == 32 && !ctx->pad)
198 return (ISC_R_BADBASE32);
201 * Check that padding starts at the right place and that
202 * bits that should be zero are.
203 * Record how many significant bytes in answer (seen_32).
205 if (last == 32 && ctx->seen_32 == 0)
206 switch (ctx->digits) {
209 return (ISC_R_BADBASE32);
211 if ((ctx->val[1]&0x03) != 0)
212 return (ISC_R_BADBASE32);
216 return (ISC_R_BADBASE32);
218 if ((ctx->val[3]&0x0f) != 0)
219 return (ISC_R_BADBASE32);
223 if ((ctx->val[4]&0x01) != 0)
224 return (ISC_R_BADBASE32);
228 return (ISC_R_BADBASE32);
230 if ((ctx->val[6]&0x07) != 0)
231 return (ISC_R_BADBASE32);
237 * Zero fill pad values.
239 ctx->val[ctx->digits++] = (last == 32) ? 0 : last;
241 if (ctx->digits == 8) {
243 unsigned char buf[5];
245 if (ctx->seen_32 != 0) {
246 ctx->seen_end = ISC_TRUE;
249 buf[0] = (ctx->val[0]<<3)|(ctx->val[1]>>2);
250 buf[1] = (ctx->val[1]<<6)|(ctx->val[2]<<1)|(ctx->val[3]>>4);
251 buf[2] = (ctx->val[3]<<4)|(ctx->val[4]>>1);
252 buf[3] = (ctx->val[4]<<7)|(ctx->val[5]<<2)|(ctx->val[6]>>3);
253 buf[4] = (ctx->val[6]<<5)|(ctx->val[7]);
254 RETERR(mem_tobuffer(ctx->target, buf, n));
255 if (ctx->length >= 0) {
257 return (ISC_R_BADBASE32);
263 return (ISC_R_SUCCESS);
266 static inline isc_result_t
267 base32_decode_finish(base32_decode_ctx_t *ctx) {
270 return (ISC_R_UNEXPECTEDEND);
272 * Add missing padding if required.
274 if (!ctx->pad && ctx->digits != 0) {
277 RETERR(base32_decode_char(ctx, '='));
278 } while (ctx->digits != 0);
280 if (ctx->digits != 0)
281 return (ISC_R_BADBASE32);
282 return (ISC_R_SUCCESS);
286 base32_tobuffer(isc_lex_t *lexer, const char base[], isc_boolean_t pad,
287 isc_buffer_t *target, int length)
289 base32_decode_ctx_t ctx;
290 isc_textregion_t *tr;
294 base32_decode_init(&ctx, length, base, pad, target);
296 while (!ctx.seen_end && (ctx.length != 0)) {
303 RETERR(isc_lex_getmastertoken(lexer, &token,
304 isc_tokentype_string, eol));
305 if (token.type != isc_tokentype_string)
307 tr = &token.value.as_textregion;
308 for (i = 0; i < tr->length; i++)
309 RETERR(base32_decode_char(&ctx, tr->base[i]));
311 if (ctx.length < 0 && !ctx.seen_end)
312 isc_lex_ungettoken(lexer, &token);
313 RETERR(base32_decode_finish(&ctx));
314 return (ISC_R_SUCCESS);
318 isc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
319 return (base32_tobuffer(lexer, base32, ISC_TRUE, target, length));
323 isc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
324 return (base32_tobuffer(lexer, base32hex, ISC_TRUE, target, length));
328 isc_base32hexnp_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
329 return (base32_tobuffer(lexer, base32hex, ISC_FALSE, target, length));
333 base32_decodestring(const char *cstr, const char base[], isc_boolean_t pad,
334 isc_buffer_t *target)
336 base32_decode_ctx_t ctx;
338 base32_decode_init(&ctx, -1, base, pad, target);
343 if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
345 RETERR(base32_decode_char(&ctx, c));
347 RETERR(base32_decode_finish(&ctx));
348 return (ISC_R_SUCCESS);
352 isc_base32_decodestring(const char *cstr, isc_buffer_t *target) {
353 return (base32_decodestring(cstr, base32, ISC_TRUE, target));
357 isc_base32hex_decodestring(const char *cstr, isc_buffer_t *target) {
358 return (base32_decodestring(cstr, base32hex, ISC_TRUE, target));
362 isc_base32hexnp_decodestring(const char *cstr, isc_buffer_t *target) {
363 return (base32_decodestring(cstr, base32hex, ISC_FALSE, target));
367 base32_decoderegion(isc_region_t *source, const char base[],
368 isc_boolean_t pad, isc_buffer_t *target)
370 base32_decode_ctx_t ctx;
372 base32_decode_init(&ctx, -1, base, pad, target);
373 while (source->length != 0) {
374 int c = *source->base;
375 RETERR(base32_decode_char(&ctx, c));
376 isc_region_consume(source, 1);
378 RETERR(base32_decode_finish(&ctx));
379 return (ISC_R_SUCCESS);
383 isc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target) {
384 return (base32_decoderegion(source, base32, ISC_TRUE, target));
388 isc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target) {
389 return (base32_decoderegion(source, base32hex, ISC_TRUE, target));
393 isc_base32hexnp_decoderegion(isc_region_t *source, isc_buffer_t *target) {
394 return (base32_decoderegion(source, base32hex, ISC_FALSE, target));
398 str_totext(const char *source, isc_buffer_t *target) {
402 isc_buffer_availableregion(target, ®ion);
405 if (l > region.length)
406 return (ISC_R_NOSPACE);
408 memmove(region.base, source, l);
409 isc_buffer_add(target, l);
410 return (ISC_R_SUCCESS);
414 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
417 isc_buffer_availableregion(target, &tr);
418 if (length > tr.length)
419 return (ISC_R_NOSPACE);
420 memmove(tr.base, base, length);
421 isc_buffer_add(target, length);
422 return (ISC_R_SUCCESS);