/contrib/bind9/lib/isc/base32.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 373 lines · 290 code · 43 blank · 40 comment · 72 complexity · 3e9918e65a513148f5b2b0c8dd22bae8 MD5 · raw file

  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. /* $Id: base32.c,v 1.6 2009/10/21 01:22:29 each Exp $ */
  17. /*! \file */
  18. #include <config.h>
  19. #include <isc/base32.h>
  20. #include <isc/buffer.h>
  21. #include <isc/lex.h>
  22. #include <isc/region.h>
  23. #include <isc/string.h>
  24. #include <isc/util.h>
  25. #define RETERR(x) do { \
  26. isc_result_t _r = (x); \
  27. if (_r != ISC_R_SUCCESS) \
  28. return (_r); \
  29. } while (0)
  30. /*@{*/
  31. /*!
  32. * These static functions are also present in lib/dns/rdata.c. I'm not
  33. * sure where they should go. -- bwelling
  34. */
  35. static isc_result_t
  36. str_totext(const char *source, isc_buffer_t *target);
  37. static isc_result_t
  38. mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
  39. /*@}*/
  40. static const char base32[] =
  41. "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=abcdefghijklmnopqrstuvwxyz234567";
  42. static const char base32hex[] =
  43. "0123456789ABCDEFGHIJKLMNOPQRSTUV=0123456789abcdefghijklmnopqrstuv";
  44. static isc_result_t
  45. base32_totext(isc_region_t *source, int wordlength, const char *wordbreak,
  46. isc_buffer_t *target, const char base[])
  47. {
  48. char buf[9];
  49. unsigned int loops = 0;
  50. if (wordlength >= 0 && wordlength < 8)
  51. wordlength = 8;
  52. memset(buf, 0, sizeof(buf));
  53. while (source->length > 0) {
  54. buf[0] = base[((source->base[0]>>3)&0x1f)]; /* 5 + */
  55. if (source->length == 1) {
  56. buf[1] = base[(source->base[0]<<2)&0x1c];
  57. buf[2] = buf[3] = buf[4] = '=';
  58. buf[5] = buf[6] = buf[7] = '=';
  59. RETERR(str_totext(buf, target));
  60. break;
  61. }
  62. buf[1] = base[((source->base[0]<<2)&0x1c)| /* 3 = 8 */
  63. ((source->base[1]>>6)&0x03)]; /* 2 + */
  64. buf[2] = base[((source->base[1]>>1)&0x1f)]; /* 5 + */
  65. if (source->length == 2) {
  66. buf[3] = base[(source->base[1]<<4)&0x10];
  67. buf[4] = buf[5] = buf[6] = buf[7] = '=';
  68. RETERR(str_totext(buf, target));
  69. break;
  70. }
  71. buf[3] = base[((source->base[1]<<4)&0x10)| /* 1 = 8 */
  72. ((source->base[2]>>4)&0x0f)]; /* 4 + */
  73. if (source->length == 3) {
  74. buf[4] = base[(source->base[2]<<1)&0x1e];
  75. buf[5] = buf[6] = buf[7] = '=';
  76. RETERR(str_totext(buf, target));
  77. break;
  78. }
  79. buf[4] = base[((source->base[2]<<1)&0x1e)| /* 4 = 8 */
  80. ((source->base[3]>>7)&0x01)]; /* 1 + */
  81. buf[5] = base[((source->base[3]>>2)&0x1f)]; /* 5 + */
  82. if (source->length == 4) {
  83. buf[6] = base[(source->base[3]<<3)&0x18];
  84. buf[7] = '=';
  85. RETERR(str_totext(buf, target));
  86. break;
  87. }
  88. buf[6] = base[((source->base[3]<<3)&0x18)| /* 2 = 8 */
  89. ((source->base[4]>>5)&0x07)]; /* 3 + */
  90. buf[7] = base[source->base[4]&0x1f]; /* 5 = 8 */
  91. RETERR(str_totext(buf, target));
  92. isc_region_consume(source, 5);
  93. loops++;
  94. if (source->length != 0 && wordlength >= 0 &&
  95. (int)((loops + 1) * 8) >= wordlength)
  96. {
  97. loops = 0;
  98. RETERR(str_totext(wordbreak, target));
  99. }
  100. }
  101. if (source->length > 0)
  102. isc_region_consume(source, source->length);
  103. return (ISC_R_SUCCESS);
  104. }
  105. isc_result_t
  106. isc_base32_totext(isc_region_t *source, int wordlength,
  107. const char *wordbreak, isc_buffer_t *target)
  108. {
  109. return (base32_totext(source, wordlength, wordbreak, target, base32));
  110. }
  111. isc_result_t
  112. isc_base32hex_totext(isc_region_t *source, int wordlength,
  113. const char *wordbreak, isc_buffer_t *target)
  114. {
  115. return (base32_totext(source, wordlength, wordbreak, target,
  116. base32hex));
  117. }
  118. /*%
  119. * State of a base32 decoding process in progress.
  120. */
  121. typedef struct {
  122. int length; /*%< Desired length of binary data or -1 */
  123. isc_buffer_t *target; /*%< Buffer for resulting binary data */
  124. int digits; /*%< Number of buffered base32 digits */
  125. isc_boolean_t seen_end; /*%< True if "=" end marker seen */
  126. int val[8];
  127. const char *base; /*%< Which encoding we are using */
  128. int seen_32; /*%< Number of significant bytes if non zero */
  129. } base32_decode_ctx_t;
  130. static inline void
  131. base32_decode_init(base32_decode_ctx_t *ctx, int length,
  132. const char base[], isc_buffer_t *target)
  133. {
  134. ctx->digits = 0;
  135. ctx->seen_end = ISC_FALSE;
  136. ctx->seen_32 = 0;
  137. ctx->length = length;
  138. ctx->target = target;
  139. ctx->base = base;
  140. }
  141. static inline isc_result_t
  142. base32_decode_char(base32_decode_ctx_t *ctx, int c) {
  143. char *s;
  144. unsigned int last;
  145. if (ctx->seen_end)
  146. return (ISC_R_BADBASE32);
  147. if ((s = strchr(ctx->base, c)) == NULL)
  148. return (ISC_R_BADBASE32);
  149. last = s - ctx->base;
  150. /*
  151. * Handle lower case.
  152. */
  153. if (last > 32)
  154. last -= 33;
  155. /*
  156. * Check that padding is contiguous.
  157. */
  158. if (last != 32 && ctx->seen_32 != 0)
  159. return (ISC_R_BADBASE32);
  160. /*
  161. * Check that padding starts at the right place and that
  162. * bits that should be zero are.
  163. * Record how many significant bytes in answer (seen_32).
  164. */
  165. if (last == 32 && ctx->seen_32 == 0)
  166. switch (ctx->digits) {
  167. case 0:
  168. case 1:
  169. return (ISC_R_BADBASE32);
  170. case 2:
  171. if ((ctx->val[1]&0x03) != 0)
  172. return (ISC_R_BADBASE32);
  173. ctx->seen_32 = 1;
  174. break;
  175. case 3:
  176. return (ISC_R_BADBASE32);
  177. case 4:
  178. if ((ctx->val[3]&0x0f) != 0)
  179. return (ISC_R_BADBASE32);
  180. ctx->seen_32 = 3;
  181. break;
  182. case 5:
  183. if ((ctx->val[4]&0x01) != 0)
  184. return (ISC_R_BADBASE32);
  185. ctx->seen_32 = 3;
  186. break;
  187. case 6:
  188. return (ISC_R_BADBASE32);
  189. case 7:
  190. if ((ctx->val[6]&0x07) != 0)
  191. return (ISC_R_BADBASE32);
  192. ctx->seen_32 = 4;
  193. break;
  194. }
  195. /*
  196. * Zero fill pad values.
  197. */
  198. ctx->val[ctx->digits++] = (last == 32) ? 0 : last;
  199. if (ctx->digits == 8) {
  200. int n = 5;
  201. unsigned char buf[5];
  202. if (ctx->seen_32 != 0) {
  203. ctx->seen_end = ISC_TRUE;
  204. n = ctx->seen_32;
  205. }
  206. buf[0] = (ctx->val[0]<<3)|(ctx->val[1]>>2);
  207. buf[1] = (ctx->val[1]<<6)|(ctx->val[2]<<1)|(ctx->val[3]>>4);
  208. buf[2] = (ctx->val[3]<<4)|(ctx->val[4]>>1);
  209. buf[3] = (ctx->val[4]<<7)|(ctx->val[5]<<2)|(ctx->val[6]>>3);
  210. buf[4] = (ctx->val[6]<<5)|(ctx->val[7]);
  211. RETERR(mem_tobuffer(ctx->target, buf, n));
  212. if (ctx->length >= 0) {
  213. if (n > ctx->length)
  214. return (ISC_R_BADBASE32);
  215. else
  216. ctx->length -= n;
  217. }
  218. ctx->digits = 0;
  219. }
  220. return (ISC_R_SUCCESS);
  221. }
  222. static inline isc_result_t
  223. base32_decode_finish(base32_decode_ctx_t *ctx) {
  224. if (ctx->length > 0)
  225. return (ISC_R_UNEXPECTEDEND);
  226. if (ctx->digits != 0)
  227. return (ISC_R_BADBASE32);
  228. return (ISC_R_SUCCESS);
  229. }
  230. static isc_result_t
  231. base32_tobuffer(isc_lex_t *lexer, const char base[], isc_buffer_t *target,
  232. int length)
  233. {
  234. base32_decode_ctx_t ctx;
  235. isc_textregion_t *tr;
  236. isc_token_t token;
  237. isc_boolean_t eol;
  238. base32_decode_init(&ctx, length, base, target);
  239. while (!ctx.seen_end && (ctx.length != 0)) {
  240. unsigned int i;
  241. if (length > 0)
  242. eol = ISC_FALSE;
  243. else
  244. eol = ISC_TRUE;
  245. RETERR(isc_lex_getmastertoken(lexer, &token,
  246. isc_tokentype_string, eol));
  247. if (token.type != isc_tokentype_string)
  248. break;
  249. tr = &token.value.as_textregion;
  250. for (i = 0; i < tr->length; i++)
  251. RETERR(base32_decode_char(&ctx, tr->base[i]));
  252. }
  253. if (ctx.length < 0 && !ctx.seen_end)
  254. isc_lex_ungettoken(lexer, &token);
  255. RETERR(base32_decode_finish(&ctx));
  256. return (ISC_R_SUCCESS);
  257. }
  258. isc_result_t
  259. isc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
  260. return (base32_tobuffer(lexer, base32, target, length));
  261. }
  262. isc_result_t
  263. isc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
  264. return (base32_tobuffer(lexer, base32hex, target, length));
  265. }
  266. static isc_result_t
  267. base32_decodestring(const char *cstr, const char base[], isc_buffer_t *target) {
  268. base32_decode_ctx_t ctx;
  269. base32_decode_init(&ctx, -1, base, target);
  270. for (;;) {
  271. int c = *cstr++;
  272. if (c == '\0')
  273. break;
  274. if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
  275. continue;
  276. RETERR(base32_decode_char(&ctx, c));
  277. }
  278. RETERR(base32_decode_finish(&ctx));
  279. return (ISC_R_SUCCESS);
  280. }
  281. isc_result_t
  282. isc_base32_decodestring(const char *cstr, isc_buffer_t *target) {
  283. return (base32_decodestring(cstr, base32, target));
  284. }
  285. isc_result_t
  286. isc_base32hex_decodestring(const char *cstr, isc_buffer_t *target) {
  287. return (base32_decodestring(cstr, base32hex, target));
  288. }
  289. static isc_result_t
  290. base32_decoderegion(isc_region_t *source, const char base[], isc_buffer_t *target) {
  291. base32_decode_ctx_t ctx;
  292. base32_decode_init(&ctx, -1, base, target);
  293. while (source->length != 0) {
  294. int c = *source->base;
  295. RETERR(base32_decode_char(&ctx, c));
  296. isc_region_consume(source, 1);
  297. }
  298. RETERR(base32_decode_finish(&ctx));
  299. return (ISC_R_SUCCESS);
  300. }
  301. isc_result_t
  302. isc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target) {
  303. return (base32_decoderegion(source, base32, target));
  304. }
  305. isc_result_t
  306. isc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target) {
  307. return (base32_decoderegion(source, base32hex, target));
  308. }
  309. static isc_result_t
  310. str_totext(const char *source, isc_buffer_t *target) {
  311. unsigned int l;
  312. isc_region_t region;
  313. isc_buffer_availableregion(target, &region);
  314. l = strlen(source);
  315. if (l > region.length)
  316. return (ISC_R_NOSPACE);
  317. memcpy(region.base, source, l);
  318. isc_buffer_add(target, l);
  319. return (ISC_R_SUCCESS);
  320. }
  321. static isc_result_t
  322. mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
  323. isc_region_t tr;
  324. isc_buffer_availableregion(target, &tr);
  325. if (length > tr.length)
  326. return (ISC_R_NOSPACE);
  327. memcpy(tr.base, base, length);
  328. isc_buffer_add(target, length);
  329. return (ISC_R_SUCCESS);
  330. }