4 * Copyright (C) 2008, 2009 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
19 /* Id: base32.c,v 1.3.116.2 2009/01/18 23:47:41 tbox Exp */
25 #include <isc/base32.h>
26 #include <isc/buffer.h>
28 #include <isc/region.h>
29 #include <isc/string.h>
32 #define RETERR(x) do { \
33 isc_result_t _r = (x); \
34 if (_r != ISC_R_SUCCESS) \
41 * These static functions are also present in lib/dns/rdata.c. I'm not
42 * sure where they should go. -- bwelling
45 str_totext(const char *source
, isc_buffer_t
*target
);
48 mem_tobuffer(isc_buffer_t
*target
, void *base
, unsigned int length
);
52 static const char base32
[] =
53 "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=abcdefghijklmnopqrstuvwxyz234567";
54 static const char base32hex
[] =
55 "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
58 base32_totext(isc_region_t
*source
, int wordlength
, const char *wordbreak
,
59 isc_buffer_t
*target
, const char base
[])
62 unsigned int loops
= 0;
64 if (wordlength
>= 0 && wordlength
< 8)
67 memset(buf
, 0, sizeof(buf
));
68 while (source
->length
> 0) {
69 buf
[0] = base
[((source
->base
[0]>>3)&0x1f)]; /* 5 + */
70 if (source
->length
== 1) {
71 buf
[1] = base
[(source
->base
[0]<<2)&0x1c];
72 buf
[2] = buf
[3] = buf
[4] = '=';
73 buf
[5] = buf
[6] = buf
[7] = '=';
74 RETERR(str_totext(buf
, target
));
77 buf
[1] = base
[((source
->base
[0]<<2)&0x1c)| /* 3 = 8 */
78 ((source
->base
[1]>>6)&0x03)]; /* 2 + */
79 buf
[2] = base
[((source
->base
[1]>>1)&0x1f)]; /* 5 + */
80 if (source
->length
== 2) {
81 buf
[3] = base
[(source
->base
[1]<<4)&0x10];
82 buf
[4] = buf
[5] = buf
[6] = buf
[7] = '=';
83 RETERR(str_totext(buf
, target
));
86 buf
[3] = base
[((source
->base
[1]<<4)&0x10)| /* 1 = 8 */
87 ((source
->base
[2]>>4)&0x0f)]; /* 4 + */
88 if (source
->length
== 3) {
89 buf
[4] = base
[(source
->base
[2]<<1)&0x1e];
90 buf
[5] = buf
[6] = buf
[7] = '=';
91 RETERR(str_totext(buf
, target
));
94 buf
[4] = base
[((source
->base
[2]<<1)&0x1e)| /* 4 = 8 */
95 ((source
->base
[3]>>7)&0x01)]; /* 1 + */
96 buf
[5] = base
[((source
->base
[3]>>2)&0x1f)]; /* 5 + */
97 if (source
->length
== 4) {
98 buf
[6] = base
[(source
->base
[3]<<3)&0x18];
100 RETERR(str_totext(buf
, target
));
103 buf
[6] = base
[((source
->base
[3]<<3)&0x18)| /* 2 = 8 */
104 ((source
->base
[4]>>5)&0x07)]; /* 3 + */
105 buf
[7] = base
[source
->base
[4]&0x1f]; /* 5 = 8 */
106 RETERR(str_totext(buf
, target
));
107 isc_region_consume(source
, 5);
110 if (source
->length
!= 0 && wordlength
>= 0 &&
111 (int)((loops
+ 1) * 8) >= wordlength
)
114 RETERR(str_totext(wordbreak
, target
));
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
, base32
));
128 isc_base32hex_totext(isc_region_t
*source
, int wordlength
,
129 const char *wordbreak
, isc_buffer_t
*target
)
131 return (base32_totext(source
, wordlength
, wordbreak
, target
,
136 * State of a base32 decoding process in progress.
139 int length
; /*%< Desired length of binary data or -1 */
140 isc_buffer_t
*target
; /*%< Buffer for resulting binary data */
141 int digits
; /*%< Number of buffered base32 digits */
142 isc_boolean_t seen_end
; /*%< True if "=" end marker seen */
144 const char *base
; /*%< Which encoding we are using */
145 int seen_32
; /*%< Number of significant bytes if non zero */
146 } base32_decode_ctx_t
;
149 base32_decode_init(base32_decode_ctx_t
*ctx
, int length
,
150 const char base
[], isc_buffer_t
*target
)
153 ctx
->seen_end
= ISC_FALSE
;
155 ctx
->length
= length
;
156 ctx
->target
= target
;
160 static inline isc_result_t
161 base32_decode_char(base32_decode_ctx_t
*ctx
, int c
) {
166 return (ISC_R_BADBASE32
);
167 if ((s
= strchr(ctx
->base
, c
)) == NULL
)
168 return (ISC_R_BADBASE32
);
169 last
= s
- ctx
->base
;
176 * Check that padding is contiguous.
178 if (last
!= 32 && ctx
->seen_32
!= 0)
179 return (ISC_R_BADBASE32
);
181 * Check that padding starts at the right place and that
182 * bits that should be zero are.
183 * Record how many significant bytes in answer (seen_32).
185 if (last
== 32 && ctx
->seen_32
== 0)
186 switch (ctx
->digits
) {
189 return (ISC_R_BADBASE32
);
191 if ((ctx
->val
[1]&0x03) != 0)
192 return (ISC_R_BADBASE32
);
196 return (ISC_R_BADBASE32
);
198 if ((ctx
->val
[3]&0x0f) != 0)
199 return (ISC_R_BADBASE32
);
203 if ((ctx
->val
[4]&0x01) != 0)
204 return (ISC_R_BADBASE32
);
208 return (ISC_R_BADBASE32
);
210 if ((ctx
->val
[6]&0x07) != 0)
211 return (ISC_R_BADBASE32
);
216 * Zero fill pad values.
218 ctx
->val
[ctx
->digits
++] = (last
== 32) ? 0 : last
;
220 if (ctx
->digits
== 8) {
222 unsigned char buf
[5];
224 if (ctx
->seen_32
!= 0) {
225 ctx
->seen_end
= ISC_TRUE
;
228 buf
[0] = (ctx
->val
[0]<<3)|(ctx
->val
[1]>>2);
229 buf
[1] = (ctx
->val
[1]<<6)|(ctx
->val
[2]<<1)|(ctx
->val
[3]>>4);
230 buf
[2] = (ctx
->val
[3]<<4)|(ctx
->val
[4]>>1);
231 buf
[3] = (ctx
->val
[4]<<7)|(ctx
->val
[5]<<2)|(ctx
->val
[6]>>3);
232 buf
[4] = (ctx
->val
[6]<<5)|(ctx
->val
[7]);
233 RETERR(mem_tobuffer(ctx
->target
, buf
, n
));
234 if (ctx
->length
>= 0) {
236 return (ISC_R_BADBASE32
);
242 return (ISC_R_SUCCESS
);
245 static inline isc_result_t
246 base32_decode_finish(base32_decode_ctx_t
*ctx
) {
248 return (ISC_R_UNEXPECTEDEND
);
249 if (ctx
->digits
!= 0)
250 return (ISC_R_BADBASE32
);
251 return (ISC_R_SUCCESS
);
255 base32_tobuffer(isc_lex_t
*lexer
, const char base
[], isc_buffer_t
*target
,
258 base32_decode_ctx_t ctx
;
259 isc_textregion_t
*tr
;
263 base32_decode_init(&ctx
, length
, base
, target
);
265 while (!ctx
.seen_end
&& (ctx
.length
!= 0)) {
272 RETERR(isc_lex_getmastertoken(lexer
, &token
,
273 isc_tokentype_string
, eol
));
274 if (token
.type
!= isc_tokentype_string
)
276 tr
= &token
.value
.as_textregion
;
277 for (i
= 0; i
< tr
->length
; i
++)
278 RETERR(base32_decode_char(&ctx
, tr
->base
[i
]));
280 if (ctx
.length
< 0 && !ctx
.seen_end
)
281 isc_lex_ungettoken(lexer
, &token
);
282 RETERR(base32_decode_finish(&ctx
));
283 return (ISC_R_SUCCESS
);
287 isc_base32_tobuffer(isc_lex_t
*lexer
, isc_buffer_t
*target
, int length
) {
288 return (base32_tobuffer(lexer
, base32
, target
, length
));
292 isc_base32hex_tobuffer(isc_lex_t
*lexer
, isc_buffer_t
*target
, int length
) {
293 return (base32_tobuffer(lexer
, base32hex
, target
, length
));
297 base32_decodestring(const char *cstr
, const char base
[], isc_buffer_t
*target
) {
298 base32_decode_ctx_t ctx
;
300 base32_decode_init(&ctx
, -1, base
, target
);
305 if (c
== ' ' || c
== '\t' || c
== '\n' || c
== '\r')
307 RETERR(base32_decode_char(&ctx
, c
));
309 RETERR(base32_decode_finish(&ctx
));
310 return (ISC_R_SUCCESS
);
314 isc_base32_decodestring(const char *cstr
, isc_buffer_t
*target
) {
315 return (base32_decodestring(cstr
, base32
, target
));
319 isc_base32hex_decodestring(const char *cstr
, isc_buffer_t
*target
) {
320 return (base32_decodestring(cstr
, base32hex
, target
));
324 base32_decoderegion(isc_region_t
*source
, const char base
[], isc_buffer_t
*target
) {
325 base32_decode_ctx_t ctx
;
327 base32_decode_init(&ctx
, -1, base
, target
);
328 while (source
->length
!= 0) {
329 int c
= *source
->base
;
330 RETERR(base32_decode_char(&ctx
, c
));
331 isc_region_consume(source
, 1);
333 RETERR(base32_decode_finish(&ctx
));
334 return (ISC_R_SUCCESS
);
338 isc_base32_decoderegion(isc_region_t
*source
, isc_buffer_t
*target
) {
339 return (base32_decoderegion(source
, base32
, target
));
343 isc_base32hex_decoderegion(isc_region_t
*source
, isc_buffer_t
*target
) {
344 return (base32_decoderegion(source
, base32hex
, target
));
348 str_totext(const char *source
, isc_buffer_t
*target
) {
352 isc_buffer_availableregion(target
, ®ion
);
355 if (l
> region
.length
)
356 return (ISC_R_NOSPACE
);
358 memcpy(region
.base
, source
, l
);
359 isc_buffer_add(target
, l
);
360 return (ISC_R_SUCCESS
);
364 mem_tobuffer(isc_buffer_t
*target
, void *base
, unsigned int length
) {
367 isc_buffer_availableregion(target
, &tr
);
368 if (length
> tr
.length
)
369 return (ISC_R_NOSPACE
);
370 memcpy(tr
.base
, base
, length
);
371 isc_buffer_add(target
, length
);
372 return (ISC_R_SUCCESS
);