1/* $NetBSD: base32.c,v 1.6 2020/05/25 20:47:20 christos Exp $ */ 2 3/* 4 * Copyright (C) 2008, 2009 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 2009/10/21 01:22:29 each 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 (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 */ 44static isc_result_t 45str_totext(const char *source, isc_buffer_t *target); 46 47static isc_result_t 48mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length); 49 50/*@}*/ 51 52static const char base32[] = 53 "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=abcdefghijklmnopqrstuvwxyz234567"; 54static const char base32hex[] = 55 "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv"; 56 57static isc_result_t 58base32_totext(isc_region_t *source, int wordlength, const char *wordbreak, 59 isc_buffer_t *target, const char base[]) 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] = '='; 73 buf[5] = buf[6] = buf[7] = '='; 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] = '='; 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] = '='; 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] = '='; 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 122isc_result_t 123isc_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, base32)); 127} 128 129isc_result_t 130isc_base32hex_totext(isc_region_t *source, int wordlength, 131 const char *wordbreak, isc_buffer_t *target) 132{ 133 return (base32_totext(source, wordlength, wordbreak, target, 134 base32hex)); 135} 136 137/*% 138 * State of a base32 decoding process in progress. 139 */ 140typedef struct { 141 int length; /*%< Desired length of binary data or -1 */ 142 isc_buffer_t *target; /*%< Buffer for resulting binary data */ 143 int digits; /*%< Number of buffered base32 digits */ 144 isc_boolean_t seen_end; /*%< True if "=" end marker seen */ 145 int val[8]; 146 const char *base; /*%< Which encoding we are using */ 147 int seen_32; /*%< Number of significant bytes if non zero */ 148} base32_decode_ctx_t; 149 150static inline void 151base32_decode_init(base32_decode_ctx_t *ctx, int length, 152 const char base[], isc_buffer_t *target) 153{ 154 ctx->digits = 0; 155 ctx->seen_end = ISC_FALSE; 156 ctx->seen_32 = 0; 157 ctx->length = length; 158 ctx->target = target; 159 ctx->base = base; 160} 161 162static inline isc_result_t 163base32_decode_char(base32_decode_ctx_t *ctx, int c) { 164 char *s; 165 unsigned int last; 166 167 if (ctx->seen_end) 168 return (ISC_R_BADBASE32); 169 if ((s = strchr(ctx->base, c)) == NULL) 170 return (ISC_R_BADBASE32); 171 last = s - ctx->base; 172 /* 173 * Handle lower case. 174 */ 175 if (last > 32) 176 last -= 33; 177 /* 178 * Check that padding is contiguous. 179 */ 180 if (last != 32 && ctx->seen_32 != 0) 181 return (ISC_R_BADBASE32); 182 /* 183 * Check that padding starts at the right place and that 184 * bits that should be zero are. 185 * Record how many significant bytes in answer (seen_32). 186 */ 187 if (last == 32 && ctx->seen_32 == 0) 188 switch (ctx->digits) { 189 case 0: 190 case 1: 191 return (ISC_R_BADBASE32); 192 case 2: 193 if ((ctx->val[1]&0x03) != 0) 194 return (ISC_R_BADBASE32); 195 ctx->seen_32 = 1; 196 break; 197 case 3: 198 return (ISC_R_BADBASE32); 199 case 4: 200 if ((ctx->val[3]&0x0f) != 0) 201 return (ISC_R_BADBASE32); 202 ctx->seen_32 = 3; 203 break; 204 case 5: 205 if ((ctx->val[4]&0x01) != 0) 206 return (ISC_R_BADBASE32); 207 ctx->seen_32 = 3; 208 break; 209 case 6: 210 return (ISC_R_BADBASE32); 211 case 7: 212 if ((ctx->val[6]&0x07) != 0) 213 return (ISC_R_BADBASE32); 214 ctx->seen_32 = 4; 215 break; 216 } 217 /* 218 * Zero fill pad values. 219 */ 220 ctx->val[ctx->digits++] = (last == 32) ? 0 : last; 221 222 if (ctx->digits == 8) { 223 int n = 5; 224 unsigned char buf[5]; 225 226 if (ctx->seen_32 != 0) { 227 ctx->seen_end = ISC_TRUE; 228 n = ctx->seen_32; 229 } 230 buf[0] = (ctx->val[0]<<3)|(ctx->val[1]>>2); 231 buf[1] = (ctx->val[1]<<6)|(ctx->val[2]<<1)|(ctx->val[3]>>4); 232 buf[2] = (ctx->val[3]<<4)|(ctx->val[4]>>1); 233 buf[3] = (ctx->val[4]<<7)|(ctx->val[5]<<2)|(ctx->val[6]>>3); 234 buf[4] = (ctx->val[6]<<5)|(ctx->val[7]); 235 RETERR(mem_tobuffer(ctx->target, buf, n)); 236 if (ctx->length >= 0) { 237 if (n > ctx->length) 238 return (ISC_R_BADBASE32); 239 else 240 ctx->length -= n; 241 } 242 ctx->digits = 0; 243 } 244 return (ISC_R_SUCCESS); 245} 246 247static inline isc_result_t 248base32_decode_finish(base32_decode_ctx_t *ctx) { 249 if (ctx->length > 0) 250 return (ISC_R_UNEXPECTEDEND); 251 if (ctx->digits != 0) 252 return (ISC_R_BADBASE32); 253 return (ISC_R_SUCCESS); 254} 255 256static isc_result_t 257base32_tobuffer(isc_lex_t *lexer, const char base[], isc_buffer_t *target, 258 int length) 259{ 260 base32_decode_ctx_t ctx; 261 isc_textregion_t *tr; 262 isc_token_t token; 263 isc_boolean_t eol; 264 265 base32_decode_init(&ctx, length, base, target); 266 267 while (!ctx.seen_end && (ctx.length != 0)) { 268 unsigned int i; 269 270 if (length > 0) 271 eol = ISC_FALSE; 272 else 273 eol = ISC_TRUE; 274 RETERR(isc_lex_getmastertoken(lexer, &token, 275 isc_tokentype_string, eol)); 276 if (token.type != isc_tokentype_string) 277 break; 278 tr = &token.value.as_textregion; 279 for (i = 0; i < tr->length; i++) 280 RETERR(base32_decode_char(&ctx, tr->base[i])); 281 } 282 if (ctx.length < 0 && !ctx.seen_end) 283 isc_lex_ungettoken(lexer, &token); 284 RETERR(base32_decode_finish(&ctx)); 285 return (ISC_R_SUCCESS); 286} 287 288isc_result_t 289isc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) { 290 return (base32_tobuffer(lexer, base32, target, length)); 291} 292 293isc_result_t 294isc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) { 295 return (base32_tobuffer(lexer, base32hex, target, length)); 296} 297 298static isc_result_t 299base32_decodestring(const char *cstr, const char base[], isc_buffer_t *target) { 300 base32_decode_ctx_t ctx; 301 302 base32_decode_init(&ctx, -1, base, target); 303 for (;;) { 304 int c = *cstr++; 305 if (c == '\0') 306 break; 307 if (c == ' ' || c == '\t' || c == '\n' || c== '\r') 308 continue; 309 RETERR(base32_decode_char(&ctx, c)); 310 } 311 RETERR(base32_decode_finish(&ctx)); 312 return (ISC_R_SUCCESS); 313} 314 315isc_result_t 316isc_base32_decodestring(const char *cstr, isc_buffer_t *target) { 317 return (base32_decodestring(cstr, base32, target)); 318} 319 320isc_result_t 321isc_base32hex_decodestring(const char *cstr, isc_buffer_t *target) { 322 return (base32_decodestring(cstr, base32hex, target)); 323} 324 325static isc_result_t 326base32_decoderegion(isc_region_t *source, const char base[], isc_buffer_t *target) { 327 base32_decode_ctx_t ctx; 328 329 base32_decode_init(&ctx, -1, base, target); 330 while (source->length != 0) { 331 int c = *source->base; 332 RETERR(base32_decode_char(&ctx, c)); 333 isc_region_consume(source, 1); 334 } 335 RETERR(base32_decode_finish(&ctx)); 336 return (ISC_R_SUCCESS); 337} 338 339isc_result_t 340isc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target) { 341 return (base32_decoderegion(source, base32, target)); 342} 343 344isc_result_t 345isc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target) { 346 return (base32_decoderegion(source, base32hex, target)); 347} 348 349static isc_result_t 350str_totext(const char *source, isc_buffer_t *target) { 351 unsigned int l; 352 isc_region_t region; 353 354 isc_buffer_availableregion(target, ®ion); 355 l = strlen(source); 356 357 if (l > region.length) 358 return (ISC_R_NOSPACE); 359 360 memcpy(region.base, source, l); 361 isc_buffer_add(target, l); 362 return (ISC_R_SUCCESS); 363} 364 365static isc_result_t 366mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) { 367 isc_region_t tr; 368 369 isc_buffer_availableregion(target, &tr); 370 if (length > tr.length) 371 return (ISC_R_NOSPACE); 372 memcpy(tr.base, base, length); 373 isc_buffer_add(target, length); 374 return (ISC_R_SUCCESS); 375} 376