Merge branch 'vendor/MPFR' into gcc441
[dragonfly.git] / contrib / bind-9.3 / lib / isc / hex.c
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 2000-2003  Internet Software Consortium.
4  *
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.
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
18 /* $Id: hex.c,v 1.8.2.2.8.3 2004/03/06 08:14:30 marka Exp $ */
19
20 #include <config.h>
21
22 #include <ctype.h>
23
24 #include <isc/buffer.h>
25 #include <isc/hex.h>
26 #include <isc/lex.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  * BEW: These static functions are copied from lib/dns/rdata.c.
39  */
40 static isc_result_t
41 str_totext(const char *source, isc_buffer_t *target);
42
43 static isc_result_t
44 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
45
46 static const char hex[] = "0123456789ABCDEF";
47
48 isc_result_t
49 isc_hex_totext(isc_region_t *source, int wordlength,
50                const char *wordbreak, isc_buffer_t *target)
51 {
52         char buf[3];
53         unsigned int loops = 0;
54
55         if (wordlength < 2)
56                 wordlength = 2;
57
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);
64
65                 loops++;
66                 if (source->length != 0 &&
67                     (int)((loops + 1) * 2) >= wordlength)
68                 {
69                         loops = 0;
70                         RETERR(str_totext(wordbreak, target));
71                 }
72         }
73         return (ISC_R_SUCCESS);
74 }
75
76 /*
77  * State of a hex decoding process in progress.
78  */
79 typedef struct {
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 */
83         int val[2];
84 } hex_decode_ctx_t;
85
86 static inline void
87 hex_decode_init(hex_decode_ctx_t *ctx, int length, isc_buffer_t *target)
88 {
89         ctx->digits = 0;
90         ctx->length = length;
91         ctx->target = target;
92 }
93
94 static inline isc_result_t
95 hex_decode_char(hex_decode_ctx_t *ctx, int c) {
96         char *s;
97
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) {
102                 unsigned char num;
103
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);
109                         else
110                                 ctx->length -= 1;
111                 }
112                 ctx->digits = 0;
113         }
114         return (ISC_R_SUCCESS);
115 }
116
117 static inline isc_result_t
118 hex_decode_finish(hex_decode_ctx_t *ctx) {
119         if (ctx->length > 0)
120                 return (ISC_R_UNEXPECTEDEND);
121         if (ctx->digits != 0)
122                 return (ISC_R_BADHEX);
123         return (ISC_R_SUCCESS);
124 }
125
126 isc_result_t
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;
130         isc_token_t token;
131         isc_boolean_t eol;
132
133         hex_decode_init(&ctx, length, target);
134
135         while (ctx.length != 0) {
136                 unsigned int i;
137
138                 if (length > 0)
139                         eol = ISC_FALSE;
140                 else
141                         eol = ISC_TRUE;
142                 RETERR(isc_lex_getmastertoken(lexer, &token,
143                                               isc_tokentype_string, eol));
144                 if (token.type != isc_tokentype_string)
145                         break;
146                 tr = &token.value.as_textregion;
147                 for (i = 0; i < tr->length; i++)
148                         RETERR(hex_decode_char(&ctx, tr->base[i]));
149         }
150         if (ctx.length < 0)
151                 isc_lex_ungettoken(lexer, &token);
152         RETERR(hex_decode_finish(&ctx));
153         return (ISC_R_SUCCESS);
154 }
155
156 isc_result_t
157 isc_hex_decodestring(char *cstr, isc_buffer_t *target) {
158         hex_decode_ctx_t ctx;
159
160         hex_decode_init(&ctx, -1, target);
161         for (;;) {
162                 int c = *cstr++;
163                 if (c == '\0')
164                         break;
165                 if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
166                         continue;
167                 RETERR(hex_decode_char(&ctx, c));
168         }
169         RETERR(hex_decode_finish(&ctx));        
170         return (ISC_R_SUCCESS);
171 }
172
173 static isc_result_t
174 str_totext(const char *source, isc_buffer_t *target) {
175         unsigned int l;
176         isc_region_t region;
177
178         isc_buffer_availableregion(target, &region);
179         l = strlen(source);
180
181         if (l > region.length)
182                 return (ISC_R_NOSPACE);
183
184         memcpy(region.base, source, l);
185         isc_buffer_add(target, l);
186         return (ISC_R_SUCCESS);
187 }
188
189 static isc_result_t
190 mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
191         isc_region_t tr;
192
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);
199 }