xref: /openbsd/usr.bin/dig/lib/isc/hex.c (revision d89ec533)
1 /*
2  * Copyright (C) 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 
17 /* $Id: hex.c,v 1.5 2020/02/26 18:47:59 florian Exp $ */
18 
19 /*! \file */
20 
21 #include <ctype.h>
22 #include <string.h>
23 
24 #include <isc/buffer.h>
25 #include <isc/hex.h>
26 #include <isc/region.h>
27 #include <isc/types.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 static const char hex[] = "0123456789ABCDEF";
37 
38 isc_result_t
39 isc_hex_totext(isc_region_t *source, int wordlength,
40 	       const char *wordbreak, isc_buffer_t *target)
41 {
42 	char buf[3];
43 	unsigned int loops = 0;
44 
45 	if (wordlength < 2)
46 		wordlength = 2;
47 
48 	memset(buf, 0, sizeof(buf));
49 	while (source->length > 0) {
50 		buf[0] = hex[(source->base[0] >> 4) & 0xf];
51 		buf[1] = hex[(source->base[0]) & 0xf];
52 		RETERR(isc_str_tobuffer(buf, target));
53 		isc_region_consume(source, 1);
54 
55 		loops++;
56 		if (source->length != 0 &&
57 		    (int)((loops + 1) * 2) >= wordlength)
58 		{
59 			loops = 0;
60 			RETERR(isc_str_tobuffer(wordbreak, target));
61 		}
62 	}
63 	return (ISC_R_SUCCESS);
64 }
65 
66 /*%
67  * State of a hex decoding process in progress.
68  */
69 typedef struct {
70 	int length;		/*%< Desired length of binary data or -1 */
71 	isc_buffer_t *target;	/*%< Buffer for resulting binary data */
72 	int digits;		/*%< Number of buffered hex digits */
73 	int val[2];
74 } hex_decode_ctx_t;
75 
76 static inline void
77 hex_decode_init(hex_decode_ctx_t *ctx, int length, isc_buffer_t *target)
78 {
79 	ctx->digits = 0;
80 	ctx->length = length;
81 	ctx->target = target;
82 }
83 
84 static inline isc_result_t
85 hex_decode_char(hex_decode_ctx_t *ctx, int c) {
86 	const char *s;
87 
88 	if ((s = strchr(hex, toupper(c))) == NULL)
89 		return (ISC_R_BADHEX);
90 	ctx->val[ctx->digits++] = (int)(s - hex);
91 	if (ctx->digits == 2) {
92 		unsigned char num;
93 
94 		num = (ctx->val[0] << 4) + (ctx->val[1]);
95 		RETERR(isc_mem_tobuffer(ctx->target, &num, 1));
96 		if (ctx->length >= 0) {
97 			if (ctx->length == 0)
98 				return (ISC_R_BADHEX);
99 			else
100 				ctx->length -= 1;
101 		}
102 		ctx->digits = 0;
103 	}
104 	return (ISC_R_SUCCESS);
105 }
106 
107 static inline isc_result_t
108 hex_decode_finish(hex_decode_ctx_t *ctx) {
109 	if (ctx->length > 0)
110 		return (ISC_R_UNEXPECTEDEND);
111 	if (ctx->digits != 0)
112 		return (ISC_R_BADHEX);
113 	return (ISC_R_SUCCESS);
114 }
115 
116 isc_result_t
117 isc_hex_decodestring(const char *cstr, isc_buffer_t *target) {
118 	hex_decode_ctx_t ctx;
119 
120 	hex_decode_init(&ctx, -1, target);
121 	for (;;) {
122 		int c = *cstr++;
123 		if (c == '\0')
124 			break;
125 		if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
126 			continue;
127 		RETERR(hex_decode_char(&ctx, c));
128 	}
129 	RETERR(hex_decode_finish(&ctx));
130 	return (ISC_R_SUCCESS);
131 }
132