Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright (C) Internet Systems Consortium, Inc. ("ISC") |
3 | | * |
4 | | * SPDX-License-Identifier: MPL-2.0 |
5 | | * |
6 | | * This Source Code Form is subject to the terms of the Mozilla Public |
7 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
8 | | * file, you can obtain one at https://mozilla.org/MPL/2.0/. |
9 | | * |
10 | | * See the COPYRIGHT file distributed with this work for additional |
11 | | * information regarding copyright ownership. |
12 | | */ |
13 | | |
14 | | /*! \file */ |
15 | | |
16 | | #include <stdbool.h> |
17 | | |
18 | | #include <isc/buffer.h> |
19 | | #include <isc/hex.h> |
20 | | #include <isc/lex.h> |
21 | | #include <isc/string.h> |
22 | | #include <isc/util.h> |
23 | | |
24 | | #define D ('0' - 0x0) /* ascii '0' to hex */ |
25 | | #define U ('A' - 0xA) /* ascii 'A' to hex */ |
26 | | #define L ('a' - 0xa) /* ascii 'a' to hex */ |
27 | | |
28 | | const uint8_t isc__hex_char[256] = { |
29 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
30 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
31 | | 0, 0, 0, 0, D, D, D, D, D, D, D, D, D, D, 0, 0, 0, 0, 0, 0, 0, U, |
32 | | U, U, U, U, U, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
33 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, L, L, L, L, L, L, 0, 0, 0, 0, 0, 0, 0, |
34 | | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
35 | | }; |
36 | | |
37 | | #undef D |
38 | | #undef U |
39 | | #undef L |
40 | | |
41 | | #define RETERR(x) \ |
42 | 0 | do { \ |
43 | 0 | isc_result_t _r = (x); \ |
44 | 0 | if (_r != ISC_R_SUCCESS) \ |
45 | 0 | return ((_r)); \ |
46 | 0 | } while (0) |
47 | | |
48 | | /* |
49 | | * BEW: These static functions are copied from lib/dns/rdata.c. |
50 | | */ |
51 | | static isc_result_t |
52 | | str_totext(const char *source, isc_buffer_t *target); |
53 | | |
54 | | static isc_result_t |
55 | | mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length); |
56 | | |
57 | | static const char hex[] = "0123456789ABCDEF"; |
58 | | |
59 | | isc_result_t |
60 | | isc_hex_totext(isc_region_t *source, int wordlength, const char *wordbreak, |
61 | 0 | isc_buffer_t *target) { |
62 | 0 | char buf[3]; |
63 | 0 | unsigned int loops = 0; |
64 | |
|
65 | 0 | if (wordlength < 2) { |
66 | 0 | wordlength = 2; |
67 | 0 | } |
68 | |
|
69 | 0 | memset(buf, 0, sizeof(buf)); |
70 | 0 | while (source->length > 0) { |
71 | 0 | buf[0] = hex[(source->base[0] >> 4) & 0xf]; |
72 | 0 | buf[1] = hex[(source->base[0]) & 0xf]; |
73 | 0 | RETERR(str_totext(buf, target)); |
74 | 0 | isc_region_consume(source, 1); |
75 | |
|
76 | 0 | loops++; |
77 | 0 | if (source->length != 0 && (int)((loops + 1) * 2) >= wordlength) |
78 | 0 | { |
79 | 0 | loops = 0; |
80 | 0 | RETERR(str_totext(wordbreak, target)); |
81 | 0 | } |
82 | 0 | } |
83 | 0 | return ISC_R_SUCCESS; |
84 | 0 | } |
85 | | |
86 | | /*% |
87 | | * State of a hex decoding process in progress. |
88 | | */ |
89 | | typedef struct { |
90 | | int length; /*%< Desired length of binary data or -1 */ |
91 | | isc_buffer_t *target; /*%< Buffer for resulting binary data */ |
92 | | int digits; /*%< Number of buffered hex digits */ |
93 | | int val[2]; |
94 | | } hex_decode_ctx_t; |
95 | | |
96 | | static void |
97 | 0 | hex_decode_init(hex_decode_ctx_t *ctx, int length, isc_buffer_t *target) { |
98 | 0 | ctx->digits = 0; |
99 | 0 | ctx->length = length; |
100 | 0 | ctx->target = target; |
101 | 0 | } |
102 | | |
103 | | static isc_result_t |
104 | 0 | hex_decode_char(hex_decode_ctx_t *ctx, int c) { |
105 | 0 | uint8_t hexval; |
106 | |
|
107 | 0 | hexval = isc_hex_char(c); |
108 | 0 | if (hexval == 0) { |
109 | 0 | return ISC_R_BADHEX; |
110 | 0 | } |
111 | 0 | ctx->val[ctx->digits++] = c - hexval; |
112 | 0 | if (ctx->digits == 2) { |
113 | 0 | unsigned char num; |
114 | |
|
115 | 0 | num = (ctx->val[0] << 4) + (ctx->val[1]); |
116 | 0 | RETERR(mem_tobuffer(ctx->target, &num, 1)); |
117 | 0 | if (ctx->length >= 0) { |
118 | 0 | if (ctx->length == 0) { |
119 | 0 | return ISC_R_BADHEX; |
120 | 0 | } else { |
121 | 0 | ctx->length -= 1; |
122 | 0 | } |
123 | 0 | } |
124 | 0 | ctx->digits = 0; |
125 | 0 | } |
126 | 0 | return ISC_R_SUCCESS; |
127 | 0 | } |
128 | | |
129 | | static isc_result_t |
130 | 0 | hex_decode_finish(hex_decode_ctx_t *ctx) { |
131 | 0 | if (ctx->length > 0) { |
132 | 0 | return ISC_R_UNEXPECTEDEND; |
133 | 0 | } |
134 | 0 | if (ctx->digits != 0) { |
135 | 0 | return ISC_R_BADHEX; |
136 | 0 | } |
137 | 0 | return ISC_R_SUCCESS; |
138 | 0 | } |
139 | | |
140 | | isc_result_t |
141 | 0 | isc_hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) { |
142 | 0 | unsigned int before, after; |
143 | 0 | hex_decode_ctx_t ctx; |
144 | 0 | isc_textregion_t *tr; |
145 | 0 | isc_token_t token; |
146 | 0 | bool eol; |
147 | |
|
148 | 0 | REQUIRE(length >= -2); |
149 | |
|
150 | 0 | hex_decode_init(&ctx, length, target); |
151 | |
|
152 | 0 | before = isc_buffer_usedlength(target); |
153 | 0 | while (ctx.length != 0) { |
154 | 0 | unsigned int i; |
155 | |
|
156 | 0 | if (length > 0) { |
157 | 0 | eol = false; |
158 | 0 | } else { |
159 | 0 | eol = true; |
160 | 0 | } |
161 | 0 | RETERR(isc_lex_getmastertoken(lexer, &token, |
162 | 0 | isc_tokentype_string, eol)); |
163 | 0 | if (token.type != isc_tokentype_string) { |
164 | 0 | break; |
165 | 0 | } |
166 | 0 | tr = &token.value.as_textregion; |
167 | 0 | for (i = 0; i < tr->length; i++) { |
168 | 0 | RETERR(hex_decode_char(&ctx, tr->base[i])); |
169 | 0 | } |
170 | 0 | } |
171 | 0 | after = isc_buffer_usedlength(target); |
172 | 0 | if (ctx.length < 0) { |
173 | 0 | isc_lex_ungettoken(lexer, &token); |
174 | 0 | } |
175 | 0 | RETERR(hex_decode_finish(&ctx)); |
176 | 0 | if (length == -2 && before == after) { |
177 | 0 | return ISC_R_UNEXPECTEDEND; |
178 | 0 | } |
179 | 0 | return ISC_R_SUCCESS; |
180 | 0 | } |
181 | | |
182 | | isc_result_t |
183 | 0 | isc_hex_decodestring(const char *cstr, isc_buffer_t *target) { |
184 | 0 | hex_decode_ctx_t ctx; |
185 | |
|
186 | 0 | hex_decode_init(&ctx, -1, target); |
187 | 0 | for (;;) { |
188 | 0 | int c = *cstr++; |
189 | 0 | if (c == '\0') { |
190 | 0 | break; |
191 | 0 | } |
192 | 0 | if (c == ' ' || c == '\t' || c == '\n' || c == '\r') { |
193 | 0 | continue; |
194 | 0 | } |
195 | 0 | RETERR(hex_decode_char(&ctx, c)); |
196 | 0 | } |
197 | 0 | RETERR(hex_decode_finish(&ctx)); |
198 | 0 | return ISC_R_SUCCESS; |
199 | 0 | } |
200 | | |
201 | | static isc_result_t |
202 | 0 | str_totext(const char *source, isc_buffer_t *target) { |
203 | 0 | unsigned int l; |
204 | 0 | isc_region_t region; |
205 | |
|
206 | 0 | isc_buffer_availableregion(target, ®ion); |
207 | 0 | l = strlen(source); |
208 | |
|
209 | 0 | if (l > region.length) { |
210 | 0 | return ISC_R_NOSPACE; |
211 | 0 | } |
212 | | |
213 | 0 | memmove(region.base, source, l); |
214 | 0 | isc_buffer_add(target, l); |
215 | 0 | return ISC_R_SUCCESS; |
216 | 0 | } |
217 | | |
218 | | static isc_result_t |
219 | 0 | mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) { |
220 | 0 | isc_region_t tr; |
221 | |
|
222 | 0 | isc_buffer_availableregion(target, &tr); |
223 | 0 | if (length > tr.length) { |
224 | 0 | return ISC_R_NOSPACE; |
225 | 0 | } |
226 | 0 | memmove(tr.base, base, length); |
227 | 0 | isc_buffer_add(target, length); |
228 | 0 | return ISC_R_SUCCESS; |
229 | 0 | } |