1 /* $NetBSD: base32.c,v 1.6 2014/12/10 04:37:59 christos Exp $ */ 2 3 /* 4 * Copyright (C) 2008, 2009, 2013, 2014 Internet Systems Consortium, Inc. ("ISC") 5 * 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. 9 * 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. 17 */ 18 19 /* Id: base32.c,v 1.6.698.1 2012/02/15 05:00:16 marka Exp */ 20 21 /*! \file */ 22 23 #include <config.h> 24 25 #include <isc/base32.h> 26 #include <isc/buffer.h> 27 #include <isc/lex.h> 28 #include <isc/region.h> 29 #include <isc/string.h> 30 #include <isc/util.h> 31 32 #define RETERR(x) do { \ 33 isc_result_t _r = (x); \ 34 if (_r != ISC_R_SUCCESS) \ 35 return (_r); \ 36 } while (/*CONSTCOND*/0) 37 38 39 /*@{*/ 40 /*! 41 * These static functions are also present in lib/dns/rdata.c. I'm not 42 * sure where they should go. -- bwelling 43 */ 44 static isc_result_t 45 str_totext(const char *source, isc_buffer_t *target); 46 47 static isc_result_t 48 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length); 49 50 /*@}*/ 51 52 static const char base32[] = 53 "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=abcdefghijklmnopqrstuvwxyz234567"; 54 static const char base32hex[] = 55 "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv"; 56 57 static isc_result_t 58 base32_totext(isc_region_t *source, int wordlength, const char *wordbreak, 59 isc_buffer_t *target, const char base[], char pad) 60 { 61 char buf[9]; 62 unsigned int loops = 0; 63 64 if (wordlength >= 0 && wordlength < 8) 65 wordlength = 8; 66 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] = pad; 73 buf[5] = buf[6] = buf[7] = pad; 74 RETERR(str_totext(buf, target)); 75 break; 76 } 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] = pad; 83 RETERR(str_totext(buf, target)); 84 break; 85 } 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] = pad; 91 RETERR(str_totext(buf, target)); 92 break; 93 } 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]; 99 buf[7] = pad; 100 RETERR(str_totext(buf, target)); 101 break; 102 } 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); 108 109 loops++; 110 if (source->length != 0 && wordlength >= 0 && 111 (int)((loops + 1) * 8) >= wordlength) 112 { 113 loops = 0; 114 RETERR(str_totext(wordbreak, target)); 115 } 116 } 117 if (source->length > 0) 118 isc_region_consume(source, source->length); 119 return (ISC_R_SUCCESS); 120 } 121 122 isc_result_t 123 isc_base32_totext(isc_region_t *source, int wordlength, 124 const char *wordbreak, isc_buffer_t *target) 125 { 126 return (base32_totext(source, wordlength, wordbreak, target, 127 base32, '=')); 128 } 129 130 isc_result_t 131 isc_base32hex_totext(isc_region_t *source, int wordlength, 132 const char *wordbreak, isc_buffer_t *target) 133 { 134 return (base32_totext(source, wordlength, wordbreak, target, 135 base32hex, '=')); 136 } 137 138 isc_result_t 139 isc_base32hexnp_totext(isc_region_t *source, int wordlength, 140 const char *wordbreak, isc_buffer_t *target) 141 { 142 return (base32_totext(source, wordlength, wordbreak, target, 143 base32hex, 0)); 144 } 145 146 /*% 147 * State of a base32 decoding process in progress. 148 */ 149 typedef struct { 150 int length; /*%< Desired length of binary data or -1 */ 151 isc_buffer_t *target; /*%< Buffer for resulting binary data */ 152 int digits; /*%< Number of buffered base32 digits */ 153 isc_boolean_t seen_end; /*%< True if "=" end marker seen */ 154 int val[8]; 155 const char *base; /*%< Which encoding we are using */ 156 int seen_32; /*%< Number of significant bytes if non zero */ 157 isc_boolean_t pad; /*%< Expect padding */ 158 } base32_decode_ctx_t; 159 160 static inline void 161 base32_decode_init(base32_decode_ctx_t *ctx, int length, const char base[], 162 isc_boolean_t pad, isc_buffer_t *target) 163 { 164 ctx->digits = 0; 165 ctx->seen_end = ISC_FALSE; 166 ctx->seen_32 = 0; 167 ctx->length = length; 168 ctx->target = target; 169 ctx->base = base; 170 ctx->pad = pad; 171 } 172 173 static inline isc_result_t 174 base32_decode_char(base32_decode_ctx_t *ctx, int c) { 175 char *s; 176 unsigned int last; 177 178 if (ctx->seen_end) 179 return (ISC_R_BADBASE32); 180 if ((s = strchr(ctx->base, c)) == NULL) 181 return (ISC_R_BADBASE32); 182 last = (unsigned int)(s - ctx->base); 183 184 /* 185 * Handle lower case. 186 */ 187 if (last > 32) 188 last -= 33; 189 190 /* 191 * Check that padding is contiguous. 192 */ 193 if (last != 32 && ctx->seen_32 != 0) 194 return (ISC_R_BADBASE32); 195 196 /* 197 * If padding is not permitted flag padding as a error. 198 */ 199 if (last == 32 && !ctx->pad) 200 return (ISC_R_BADBASE32); 201 202 /* 203 * Check that padding starts at the right place and that 204 * bits that should be zero are. 205 * Record how many significant bytes in answer (seen_32). 206 */ 207 if (last == 32 && ctx->seen_32 == 0) 208 switch (ctx->digits) { 209 case 0: 210 case 1: 211 return (ISC_R_BADBASE32); 212 case 2: 213 if ((ctx->val[1]&0x03) != 0) 214 return (ISC_R_BADBASE32); 215 ctx->seen_32 = 1; 216 break; 217 case 3: 218 return (ISC_R_BADBASE32); 219 case 4: 220 if ((ctx->val[3]&0x0f) != 0) 221 return (ISC_R_BADBASE32); 222 ctx->seen_32 = 3; 223 break; 224 case 5: 225 if ((ctx->val[4]&0x01) != 0) 226 return (ISC_R_BADBASE32); 227 ctx->seen_32 = 3; 228 break; 229 case 6: 230 return (ISC_R_BADBASE32); 231 case 7: 232 if ((ctx->val[6]&0x07) != 0) 233 return (ISC_R_BADBASE32); 234 ctx->seen_32 = 4; 235 break; 236 } 237 238 /* 239 * Zero fill pad values. 240 */ 241 ctx->val[ctx->digits++] = (last == 32) ? 0 : last; 242 243 if (ctx->digits == 8) { 244 int n = 5; 245 unsigned char buf[5]; 246 247 if (ctx->seen_32 != 0) { 248 ctx->seen_end = ISC_TRUE; 249 n = ctx->seen_32; 250 } 251 buf[0] = (ctx->val[0]<<3)|(ctx->val[1]>>2); 252 buf[1] = (ctx->val[1]<<6)|(ctx->val[2]<<1)|(ctx->val[3]>>4); 253 buf[2] = (ctx->val[3]<<4)|(ctx->val[4]>>1); 254 buf[3] = (ctx->val[4]<<7)|(ctx->val[5]<<2)|(ctx->val[6]>>3); 255 buf[4] = (ctx->val[6]<<5)|(ctx->val[7]); 256 RETERR(mem_tobuffer(ctx->target, buf, n)); 257 if (ctx->length >= 0) { 258 if (n > ctx->length) 259 return (ISC_R_BADBASE32); 260 else 261 ctx->length -= n; 262 } 263 ctx->digits = 0; 264 } 265 return (ISC_R_SUCCESS); 266 } 267 268 static inline isc_result_t 269 base32_decode_finish(base32_decode_ctx_t *ctx) { 270 271 if (ctx->length > 0) 272 return (ISC_R_UNEXPECTEDEND); 273 /* 274 * Add missing padding if required. 275 */ 276 if (!ctx->pad && ctx->digits != 0) { 277 ctx->pad = ISC_TRUE; 278 do { 279 RETERR(base32_decode_char(ctx, '=')); 280 } while (ctx->digits != 0); 281 } 282 if (ctx->digits != 0) 283 return (ISC_R_BADBASE32); 284 return (ISC_R_SUCCESS); 285 } 286 287 static isc_result_t 288 base32_tobuffer(isc_lex_t *lexer, const char base[], isc_boolean_t pad, 289 isc_buffer_t *target, int length) 290 { 291 base32_decode_ctx_t ctx; 292 isc_textregion_t *tr; 293 isc_token_t token; 294 isc_boolean_t eol; 295 296 base32_decode_init(&ctx, length, base, pad, target); 297 298 while (!ctx.seen_end && (ctx.length != 0)) { 299 unsigned int i; 300 301 if (length > 0) 302 eol = ISC_FALSE; 303 else 304 eol = ISC_TRUE; 305 RETERR(isc_lex_getmastertoken(lexer, &token, 306 isc_tokentype_string, eol)); 307 if (token.type != isc_tokentype_string) 308 break; 309 tr = &token.value.as_textregion; 310 for (i = 0; i < tr->length; i++) 311 RETERR(base32_decode_char(&ctx, tr->base[i])); 312 } 313 if (ctx.length < 0 && !ctx.seen_end) 314 isc_lex_ungettoken(lexer, &token); 315 RETERR(base32_decode_finish(&ctx)); 316 return (ISC_R_SUCCESS); 317 } 318 319 isc_result_t 320 isc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) { 321 return (base32_tobuffer(lexer, base32, ISC_TRUE, target, length)); 322 } 323 324 isc_result_t 325 isc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) { 326 return (base32_tobuffer(lexer, base32hex, ISC_TRUE, target, length)); 327 } 328 329 isc_result_t 330 isc_base32hexnp_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) { 331 return (base32_tobuffer(lexer, base32hex, ISC_FALSE, target, length)); 332 } 333 334 static isc_result_t 335 base32_decodestring(const char *cstr, const char base[], isc_boolean_t pad, 336 isc_buffer_t *target) 337 { 338 base32_decode_ctx_t ctx; 339 340 base32_decode_init(&ctx, -1, base, pad, target); 341 for (;;) { 342 int c = *cstr++; 343 if (c == '\0') 344 break; 345 if (c == ' ' || c == '\t' || c == '\n' || c== '\r') 346 continue; 347 RETERR(base32_decode_char(&ctx, c)); 348 } 349 RETERR(base32_decode_finish(&ctx)); 350 return (ISC_R_SUCCESS); 351 } 352 353 isc_result_t 354 isc_base32_decodestring(const char *cstr, isc_buffer_t *target) { 355 return (base32_decodestring(cstr, base32, ISC_TRUE, target)); 356 } 357 358 isc_result_t 359 isc_base32hex_decodestring(const char *cstr, isc_buffer_t *target) { 360 return (base32_decodestring(cstr, base32hex, ISC_TRUE, target)); 361 } 362 363 isc_result_t 364 isc_base32hexnp_decodestring(const char *cstr, isc_buffer_t *target) { 365 return (base32_decodestring(cstr, base32hex, ISC_FALSE, target)); 366 } 367 368 static isc_result_t 369 base32_decoderegion(isc_region_t *source, const char base[], 370 isc_boolean_t pad, isc_buffer_t *target) 371 { 372 base32_decode_ctx_t ctx; 373 374 base32_decode_init(&ctx, -1, base, pad, target); 375 while (source->length != 0) { 376 int c = *source->base; 377 RETERR(base32_decode_char(&ctx, c)); 378 isc_region_consume(source, 1); 379 } 380 RETERR(base32_decode_finish(&ctx)); 381 return (ISC_R_SUCCESS); 382 } 383 384 isc_result_t 385 isc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target) { 386 return (base32_decoderegion(source, base32, ISC_TRUE, target)); 387 } 388 389 isc_result_t 390 isc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target) { 391 return (base32_decoderegion(source, base32hex, ISC_TRUE, target)); 392 } 393 394 isc_result_t 395 isc_base32hexnp_decoderegion(isc_region_t *source, isc_buffer_t *target) { 396 return (base32_decoderegion(source, base32hex, ISC_FALSE, target)); 397 } 398 399 static isc_result_t 400 str_totext(const char *source, isc_buffer_t *target) { 401 unsigned int l; 402 isc_region_t region; 403 404 isc_buffer_availableregion(target, ®ion); 405 l = strlen(source); 406 407 if (l > region.length) 408 return (ISC_R_NOSPACE); 409 410 memmove(region.base, source, l); 411 isc_buffer_add(target, l); 412 return (ISC_R_SUCCESS); 413 } 414 415 static isc_result_t 416 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) { 417 isc_region_t tr; 418 419 isc_buffer_availableregion(target, &tr); 420 if (length > tr.length) 421 return (ISC_R_NOSPACE); 422 memmove(tr.base, base, length); 423 isc_buffer_add(target, length); 424 return (ISC_R_SUCCESS); 425 } 426