base32.c revision 280849
1/* 2 * Copyright (C) 2008, 2009 Internet Systems Consortium, Inc. ("ISC") 3 * 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. 7 * 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. 15 */ 16 17/* $Id: base32.c,v 1.6 2009/10/21 01:22:29 each Exp $ */ 18 19/*! \file */ 20 21#include <config.h> 22 23#include <isc/base32.h> 24#include <isc/buffer.h> 25#include <isc/lex.h> 26#include <isc/region.h> 27#include <isc/string.h> 28#include <isc/util.h> 29 30#define RETERR(x) do { \ 31 isc_result_t _r = (x); \ 32 if (_r != ISC_R_SUCCESS) \ 33 return (_r); \ 34 } while (0) 35 36 37/*@{*/ 38/*! 39 * These static functions are also present in lib/dns/rdata.c. I'm not 40 * sure where they should go. -- bwelling 41 */ 42static isc_result_t 43str_totext(const char *source, isc_buffer_t *target); 44 45static isc_result_t 46mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length); 47 48/*@}*/ 49 50static const char base32[] = 51 "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=abcdefghijklmnopqrstuvwxyz234567"; 52static const char base32hex[] = 53 "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv"; 54 55static isc_result_t 56base32_totext(isc_region_t *source, int wordlength, const char *wordbreak, 57 isc_buffer_t *target, const char base[]) 58{ 59 char buf[9]; 60 unsigned int loops = 0; 61 62 if (wordlength >= 0 && wordlength < 8) 63 wordlength = 8; 64 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] = '='; 71 buf[5] = buf[6] = buf[7] = '='; 72 RETERR(str_totext(buf, target)); 73 break; 74 } 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] = '='; 81 RETERR(str_totext(buf, target)); 82 break; 83 } 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] = '='; 89 RETERR(str_totext(buf, target)); 90 break; 91 } 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]; 97 buf[7] = '='; 98 RETERR(str_totext(buf, target)); 99 break; 100 } 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); 106 107 loops++; 108 if (source->length != 0 && wordlength >= 0 && 109 (int)((loops + 1) * 8) >= wordlength) 110 { 111 loops = 0; 112 RETERR(str_totext(wordbreak, target)); 113 } 114 } 115 if (source->length > 0) 116 isc_region_consume(source, source->length); 117 return (ISC_R_SUCCESS); 118} 119 120isc_result_t 121isc_base32_totext(isc_region_t *source, int wordlength, 122 const char *wordbreak, isc_buffer_t *target) 123{ 124 return (base32_totext(source, wordlength, wordbreak, target, base32)); 125} 126 127isc_result_t 128isc_base32hex_totext(isc_region_t *source, int wordlength, 129 const char *wordbreak, isc_buffer_t *target) 130{ 131 return (base32_totext(source, wordlength, wordbreak, target, 132 base32hex)); 133} 134 135/*% 136 * State of a base32 decoding process in progress. 137 */ 138typedef struct { 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 */ 143 int val[8]; 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; 147 148static inline void 149base32_decode_init(base32_decode_ctx_t *ctx, int length, 150 const char base[], isc_buffer_t *target) 151{ 152 ctx->digits = 0; 153 ctx->seen_end = ISC_FALSE; 154 ctx->seen_32 = 0; 155 ctx->length = length; 156 ctx->target = target; 157 ctx->base = base; 158} 159 160static inline isc_result_t 161base32_decode_char(base32_decode_ctx_t *ctx, int c) { 162 char *s; 163 unsigned int last; 164 165 if (ctx->seen_end) 166 return (ISC_R_BADBASE32); 167 if ((s = strchr(ctx->base, c)) == NULL) 168 return (ISC_R_BADBASE32); 169 last = s - ctx->base; 170 /* 171 * Handle lower case. 172 */ 173 if (last > 32) 174 last -= 33; 175 /* 176 * Check that padding is contiguous. 177 */ 178 if (last != 32 && ctx->seen_32 != 0) 179 return (ISC_R_BADBASE32); 180 /* 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). 184 */ 185 if (last == 32 && ctx->seen_32 == 0) 186 switch (ctx->digits) { 187 case 0: 188 case 1: 189 return (ISC_R_BADBASE32); 190 case 2: 191 if ((ctx->val[1]&0x03) != 0) 192 return (ISC_R_BADBASE32); 193 ctx->seen_32 = 1; 194 break; 195 case 3: 196 return (ISC_R_BADBASE32); 197 case 4: 198 if ((ctx->val[3]&0x0f) != 0) 199 return (ISC_R_BADBASE32); 200 ctx->seen_32 = 3; 201 break; 202 case 5: 203 if ((ctx->val[4]&0x01) != 0) 204 return (ISC_R_BADBASE32); 205 ctx->seen_32 = 3; 206 break; 207 case 6: 208 return (ISC_R_BADBASE32); 209 case 7: 210 if ((ctx->val[6]&0x07) != 0) 211 return (ISC_R_BADBASE32); 212 ctx->seen_32 = 4; 213 break; 214 } 215 /* 216 * Zero fill pad values. 217 */ 218 ctx->val[ctx->digits++] = (last == 32) ? 0 : last; 219 220 if (ctx->digits == 8) { 221 int n = 5; 222 unsigned char buf[5]; 223 224 if (ctx->seen_32 != 0) { 225 ctx->seen_end = ISC_TRUE; 226 n = ctx->seen_32; 227 } 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) { 235 if (n > ctx->length) 236 return (ISC_R_BADBASE32); 237 else 238 ctx->length -= n; 239 } 240 ctx->digits = 0; 241 } 242 return (ISC_R_SUCCESS); 243} 244 245static inline isc_result_t 246base32_decode_finish(base32_decode_ctx_t *ctx) { 247 if (ctx->length > 0) 248 return (ISC_R_UNEXPECTEDEND); 249 if (ctx->digits != 0) 250 return (ISC_R_BADBASE32); 251 return (ISC_R_SUCCESS); 252} 253 254static isc_result_t 255base32_tobuffer(isc_lex_t *lexer, const char base[], isc_buffer_t *target, 256 int length) 257{ 258 base32_decode_ctx_t ctx; 259 isc_textregion_t *tr; 260 isc_token_t token; 261 isc_boolean_t eol; 262 263 base32_decode_init(&ctx, length, base, target); 264 265 while (!ctx.seen_end && (ctx.length != 0)) { 266 unsigned int i; 267 268 if (length > 0) 269 eol = ISC_FALSE; 270 else 271 eol = ISC_TRUE; 272 RETERR(isc_lex_getmastertoken(lexer, &token, 273 isc_tokentype_string, eol)); 274 if (token.type != isc_tokentype_string) 275 break; 276 tr = &token.value.as_textregion; 277 for (i = 0; i < tr->length; i++) 278 RETERR(base32_decode_char(&ctx, tr->base[i])); 279 } 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); 284} 285 286isc_result_t 287isc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) { 288 return (base32_tobuffer(lexer, base32, target, length)); 289} 290 291isc_result_t 292isc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) { 293 return (base32_tobuffer(lexer, base32hex, target, length)); 294} 295 296static isc_result_t 297base32_decodestring(const char *cstr, const char base[], isc_buffer_t *target) { 298 base32_decode_ctx_t ctx; 299 300 base32_decode_init(&ctx, -1, base, target); 301 for (;;) { 302 int c = *cstr++; 303 if (c == '\0') 304 break; 305 if (c == ' ' || c == '\t' || c == '\n' || c== '\r') 306 continue; 307 RETERR(base32_decode_char(&ctx, c)); 308 } 309 RETERR(base32_decode_finish(&ctx)); 310 return (ISC_R_SUCCESS); 311} 312 313isc_result_t 314isc_base32_decodestring(const char *cstr, isc_buffer_t *target) { 315 return (base32_decodestring(cstr, base32, target)); 316} 317 318isc_result_t 319isc_base32hex_decodestring(const char *cstr, isc_buffer_t *target) { 320 return (base32_decodestring(cstr, base32hex, target)); 321} 322 323static isc_result_t 324base32_decoderegion(isc_region_t *source, const char base[], isc_buffer_t *target) { 325 base32_decode_ctx_t ctx; 326 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); 332 } 333 RETERR(base32_decode_finish(&ctx)); 334 return (ISC_R_SUCCESS); 335} 336 337isc_result_t 338isc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target) { 339 return (base32_decoderegion(source, base32, target)); 340} 341 342isc_result_t 343isc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target) { 344 return (base32_decoderegion(source, base32hex, target)); 345} 346 347static isc_result_t 348str_totext(const char *source, isc_buffer_t *target) { 349 unsigned int l; 350 isc_region_t region; 351 352 isc_buffer_availableregion(target, ®ion); 353 l = strlen(source); 354 355 if (l > region.length) 356 return (ISC_R_NOSPACE); 357 358 memcpy(region.base, source, l); 359 isc_buffer_add(target, l); 360 return (ISC_R_SUCCESS); 361} 362 363static isc_result_t 364mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) { 365 isc_region_t tr; 366 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); 373} 374