/contrib/bind9/lib/isc/hex.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 201 lines · 145 code · 32 blank · 24 comment · 36 complexity · 67278b0507878c34665ad95c8af39db9 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 2000-2003 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: hex.c,v 1.20 2008/09/25 04:02:39 tbox Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <ctype.h>
  21. #include <isc/buffer.h>
  22. #include <isc/hex.h>
  23. #include <isc/lex.h>
  24. #include <isc/string.h>
  25. #include <isc/util.h>
  26. #define RETERR(x) do { \
  27. isc_result_t _r = (x); \
  28. if (_r != ISC_R_SUCCESS) \
  29. return (_r); \
  30. } while (0)
  31. /*
  32. * BEW: These static functions are copied from lib/dns/rdata.c.
  33. */
  34. static isc_result_t
  35. str_totext(const char *source, isc_buffer_t *target);
  36. static isc_result_t
  37. mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
  38. static const char hex[] = "0123456789ABCDEF";
  39. isc_result_t
  40. isc_hex_totext(isc_region_t *source, int wordlength,
  41. const char *wordbreak, isc_buffer_t *target)
  42. {
  43. char buf[3];
  44. unsigned int loops = 0;
  45. if (wordlength < 2)
  46. wordlength = 2;
  47. memset(buf, 0, sizeof(buf));
  48. while (source->length > 0) {
  49. buf[0] = hex[(source->base[0] >> 4) & 0xf];
  50. buf[1] = hex[(source->base[0]) & 0xf];
  51. RETERR(str_totext(buf, target));
  52. isc_region_consume(source, 1);
  53. loops++;
  54. if (source->length != 0 &&
  55. (int)((loops + 1) * 2) >= wordlength)
  56. {
  57. loops = 0;
  58. RETERR(str_totext(wordbreak, target));
  59. }
  60. }
  61. return (ISC_R_SUCCESS);
  62. }
  63. /*%
  64. * State of a hex decoding process in progress.
  65. */
  66. typedef struct {
  67. int length; /*%< Desired length of binary data or -1 */
  68. isc_buffer_t *target; /*%< Buffer for resulting binary data */
  69. int digits; /*%< Number of buffered hex digits */
  70. int val[2];
  71. } hex_decode_ctx_t;
  72. static inline void
  73. hex_decode_init(hex_decode_ctx_t *ctx, int length, isc_buffer_t *target)
  74. {
  75. ctx->digits = 0;
  76. ctx->length = length;
  77. ctx->target = target;
  78. }
  79. static inline isc_result_t
  80. hex_decode_char(hex_decode_ctx_t *ctx, int c) {
  81. char *s;
  82. if ((s = strchr(hex, toupper(c))) == NULL)
  83. return (ISC_R_BADHEX);
  84. ctx->val[ctx->digits++] = s - hex;
  85. if (ctx->digits == 2) {
  86. unsigned char num;
  87. num = (ctx->val[0] << 4) + (ctx->val[1]);
  88. RETERR(mem_tobuffer(ctx->target, &num, 1));
  89. if (ctx->length >= 0) {
  90. if (ctx->length == 0)
  91. return (ISC_R_BADHEX);
  92. else
  93. ctx->length -= 1;
  94. }
  95. ctx->digits = 0;
  96. }
  97. return (ISC_R_SUCCESS);
  98. }
  99. static inline isc_result_t
  100. hex_decode_finish(hex_decode_ctx_t *ctx) {
  101. if (ctx->length > 0)
  102. return (ISC_R_UNEXPECTEDEND);
  103. if (ctx->digits != 0)
  104. return (ISC_R_BADHEX);
  105. return (ISC_R_SUCCESS);
  106. }
  107. isc_result_t
  108. isc_hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
  109. hex_decode_ctx_t ctx;
  110. isc_textregion_t *tr;
  111. isc_token_t token;
  112. isc_boolean_t eol;
  113. hex_decode_init(&ctx, length, target);
  114. while (ctx.length != 0) {
  115. unsigned int i;
  116. if (length > 0)
  117. eol = ISC_FALSE;
  118. else
  119. eol = ISC_TRUE;
  120. RETERR(isc_lex_getmastertoken(lexer, &token,
  121. isc_tokentype_string, eol));
  122. if (token.type != isc_tokentype_string)
  123. break;
  124. tr = &token.value.as_textregion;
  125. for (i = 0; i < tr->length; i++)
  126. RETERR(hex_decode_char(&ctx, tr->base[i]));
  127. }
  128. if (ctx.length < 0)
  129. isc_lex_ungettoken(lexer, &token);
  130. RETERR(hex_decode_finish(&ctx));
  131. return (ISC_R_SUCCESS);
  132. }
  133. isc_result_t
  134. isc_hex_decodestring(const char *cstr, isc_buffer_t *target) {
  135. hex_decode_ctx_t ctx;
  136. hex_decode_init(&ctx, -1, target);
  137. for (;;) {
  138. int c = *cstr++;
  139. if (c == '\0')
  140. break;
  141. if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
  142. continue;
  143. RETERR(hex_decode_char(&ctx, c));
  144. }
  145. RETERR(hex_decode_finish(&ctx));
  146. return (ISC_R_SUCCESS);
  147. }
  148. static isc_result_t
  149. str_totext(const char *source, isc_buffer_t *target) {
  150. unsigned int l;
  151. isc_region_t region;
  152. isc_buffer_availableregion(target, &region);
  153. l = strlen(source);
  154. if (l > region.length)
  155. return (ISC_R_NOSPACE);
  156. memcpy(region.base, source, l);
  157. isc_buffer_add(target, l);
  158. return (ISC_R_SUCCESS);
  159. }
  160. static isc_result_t
  161. mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
  162. isc_region_t tr;
  163. isc_buffer_availableregion(target, &tr);
  164. if (length > tr.length)
  165. return (ISC_R_NOSPACE);
  166. memcpy(tr.base, base, length);
  167. isc_buffer_add(target, length);
  168. return (ISC_R_SUCCESS);
  169. }