2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000-2002 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.
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.
18 /* $Id: hex.c,v 1.8.2.3 2004/03/09 06:11:46 marka Exp $ */
24 #include <isc/buffer.h>
27 #include <isc/string.h>
30 #define RETERR(x) do { \
31 isc_result_t _r = (x); \
32 if (_r != ISC_R_SUCCESS) \
38 * BEW: These static functions are copied from lib/dns/rdata.c.
41 str_totext(const char *source, isc_buffer_t *target);
44 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
46 static const char hex[] = "0123456789ABCDEF";
49 isc_hex_totext(isc_region_t *source, int wordlength,
50 const char *wordbreak, isc_buffer_t *target)
53 unsigned int loops = 0;
58 memset(buf, 0, sizeof buf);
59 while (source->length > 0) {
60 buf[0] = hex[(source->base[0] >> 4) & 0xf];
61 buf[1] = hex[(source->base[0]) & 0xf];
62 RETERR(str_totext(buf, target));
63 isc_region_consume(source, 1);
66 if (source->length != 0 &&
67 (int)((loops + 1) * 2) >= wordlength)
70 RETERR(str_totext(wordbreak, target));
73 return (ISC_R_SUCCESS);
77 * State of a hex decoding process in progress.
80 int length; /* Desired length of binary data or -1 */
81 isc_buffer_t *target; /* Buffer for resulting binary data */
82 int digits; /* Number of buffered hex digits */
87 hex_decode_init(hex_decode_ctx_t *ctx, int length, isc_buffer_t *target)
94 static inline isc_result_t
95 hex_decode_char(hex_decode_ctx_t *ctx, int c) {
98 if ((s = strchr(hex, toupper(c))) == NULL)
99 return (ISC_R_BADHEX);
100 ctx->val[ctx->digits++] = s - hex;
101 if (ctx->digits == 2) {
104 num = (ctx->val[0] << 4) + (ctx->val[1]);
105 RETERR(mem_tobuffer(ctx->target, &num, 1));
106 if (ctx->length >= 0) {
107 if (ctx->length == 0)
108 return (ISC_R_BADHEX);
114 return (ISC_R_SUCCESS);
117 static inline isc_result_t
118 hex_decode_finish(hex_decode_ctx_t *ctx) {
120 return (ISC_R_UNEXPECTEDEND);
121 if (ctx->digits != 0)
122 return (ISC_R_BADHEX);
123 return (ISC_R_SUCCESS);
127 isc_hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
128 hex_decode_ctx_t ctx;
129 isc_textregion_t *tr;
133 hex_decode_init(&ctx, length, target);
135 while (ctx.length != 0) {
142 RETERR(isc_lex_getmastertoken(lexer, &token,
143 isc_tokentype_string, eol));
144 if (token.type != isc_tokentype_string)
146 tr = &token.value.as_textregion;
147 for (i = 0 ;i < tr->length; i++)
148 RETERR(hex_decode_char(&ctx, tr->base[i]));
151 isc_lex_ungettoken(lexer, &token);
152 RETERR(hex_decode_finish(&ctx));
153 return (ISC_R_SUCCESS);
157 isc_hex_decodestring(char *cstr, isc_buffer_t *target) {
158 hex_decode_ctx_t ctx;
160 hex_decode_init(&ctx, -1, target);
165 if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
167 RETERR(hex_decode_char(&ctx, c));
169 RETERR(hex_decode_finish(&ctx));
170 return (ISC_R_SUCCESS);
174 str_totext(const char *source, isc_buffer_t *target) {
178 isc_buffer_availableregion(target, ®ion);
181 if (l > region.length)
182 return (ISC_R_NOSPACE);
184 memcpy(region.base, source, l);
185 isc_buffer_add(target, l);
186 return (ISC_R_SUCCESS);
190 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
193 isc_buffer_availableregion(target, &tr);
194 if (length > tr.length)
195 return (ISC_R_NOSPACE);
196 memcpy(tr.base, base, length);
197 isc_buffer_add(target, length);
198 return (ISC_R_SUCCESS);