xref: /openbsd-src/usr.bin/dig/lib/isc/hex.c (revision 873f12b9e6aaf39ba104db6af3d0dc687cd95f7e)
15185a700Sflorian /*
25185a700Sflorian  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
35185a700Sflorian  *
45185a700Sflorian  * Permission to use, copy, modify, and/or distribute this software for any
55185a700Sflorian  * purpose with or without fee is hereby granted, provided that the above
65185a700Sflorian  * copyright notice and this permission notice appear in all copies.
75185a700Sflorian  *
85185a700Sflorian  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
95185a700Sflorian  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
105185a700Sflorian  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
115185a700Sflorian  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
125185a700Sflorian  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
135185a700Sflorian  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
145185a700Sflorian  * PERFORMANCE OF THIS SOFTWARE.
155185a700Sflorian  */
165185a700Sflorian 
17*873f12b9Sflorian /* $Id: hex.c,v 1.5 2020/02/26 18:47:59 florian Exp $ */
185185a700Sflorian 
195185a700Sflorian /*! \file */
205185a700Sflorian 
215185a700Sflorian #include <ctype.h>
224465bcfbSjsg #include <string.h>
235185a700Sflorian 
245185a700Sflorian #include <isc/buffer.h>
255185a700Sflorian #include <isc/hex.h>
264465bcfbSjsg #include <isc/region.h>
274465bcfbSjsg #include <isc/types.h>
285185a700Sflorian #include <isc/util.h>
295185a700Sflorian 
305185a700Sflorian #define RETERR(x) do { \
315185a700Sflorian 	isc_result_t _r = (x); \
325185a700Sflorian 	if (_r != ISC_R_SUCCESS) \
335185a700Sflorian 		return (_r); \
345185a700Sflorian 	} while (0)
355185a700Sflorian 
365185a700Sflorian static const char hex[] = "0123456789ABCDEF";
375185a700Sflorian 
385185a700Sflorian isc_result_t
isc_hex_totext(isc_region_t * source,int wordlength,const char * wordbreak,isc_buffer_t * target)395185a700Sflorian isc_hex_totext(isc_region_t *source, int wordlength,
405185a700Sflorian 	       const char *wordbreak, isc_buffer_t *target)
415185a700Sflorian {
425185a700Sflorian 	char buf[3];
435185a700Sflorian 	unsigned int loops = 0;
445185a700Sflorian 
455185a700Sflorian 	if (wordlength < 2)
465185a700Sflorian 		wordlength = 2;
475185a700Sflorian 
485185a700Sflorian 	memset(buf, 0, sizeof(buf));
495185a700Sflorian 	while (source->length > 0) {
505185a700Sflorian 		buf[0] = hex[(source->base[0] >> 4) & 0xf];
515185a700Sflorian 		buf[1] = hex[(source->base[0]) & 0xf];
52*873f12b9Sflorian 		RETERR(isc_str_tobuffer(buf, target));
535185a700Sflorian 		isc_region_consume(source, 1);
545185a700Sflorian 
555185a700Sflorian 		loops++;
565185a700Sflorian 		if (source->length != 0 &&
575185a700Sflorian 		    (int)((loops + 1) * 2) >= wordlength)
585185a700Sflorian 		{
595185a700Sflorian 			loops = 0;
60*873f12b9Sflorian 			RETERR(isc_str_tobuffer(wordbreak, target));
615185a700Sflorian 		}
625185a700Sflorian 	}
635185a700Sflorian 	return (ISC_R_SUCCESS);
645185a700Sflorian }
655185a700Sflorian 
665185a700Sflorian /*%
675185a700Sflorian  * State of a hex decoding process in progress.
685185a700Sflorian  */
695185a700Sflorian typedef struct {
705185a700Sflorian 	int length;		/*%< Desired length of binary data or -1 */
715185a700Sflorian 	isc_buffer_t *target;	/*%< Buffer for resulting binary data */
725185a700Sflorian 	int digits;		/*%< Number of buffered hex digits */
735185a700Sflorian 	int val[2];
745185a700Sflorian } hex_decode_ctx_t;
755185a700Sflorian 
765185a700Sflorian static inline void
hex_decode_init(hex_decode_ctx_t * ctx,int length,isc_buffer_t * target)775185a700Sflorian hex_decode_init(hex_decode_ctx_t *ctx, int length, isc_buffer_t *target)
785185a700Sflorian {
795185a700Sflorian 	ctx->digits = 0;
805185a700Sflorian 	ctx->length = length;
815185a700Sflorian 	ctx->target = target;
825185a700Sflorian }
835185a700Sflorian 
845185a700Sflorian static inline isc_result_t
hex_decode_char(hex_decode_ctx_t * ctx,int c)855185a700Sflorian hex_decode_char(hex_decode_ctx_t *ctx, int c) {
865185a700Sflorian 	const char *s;
875185a700Sflorian 
885185a700Sflorian 	if ((s = strchr(hex, toupper(c))) == NULL)
895185a700Sflorian 		return (ISC_R_BADHEX);
905185a700Sflorian 	ctx->val[ctx->digits++] = (int)(s - hex);
915185a700Sflorian 	if (ctx->digits == 2) {
925185a700Sflorian 		unsigned char num;
935185a700Sflorian 
945185a700Sflorian 		num = (ctx->val[0] << 4) + (ctx->val[1]);
95637d8eb6Sflorian 		RETERR(isc_mem_tobuffer(ctx->target, &num, 1));
965185a700Sflorian 		if (ctx->length >= 0) {
975185a700Sflorian 			if (ctx->length == 0)
985185a700Sflorian 				return (ISC_R_BADHEX);
995185a700Sflorian 			else
1005185a700Sflorian 				ctx->length -= 1;
1015185a700Sflorian 		}
1025185a700Sflorian 		ctx->digits = 0;
1035185a700Sflorian 	}
1045185a700Sflorian 	return (ISC_R_SUCCESS);
1055185a700Sflorian }
1065185a700Sflorian 
1075185a700Sflorian static inline isc_result_t
hex_decode_finish(hex_decode_ctx_t * ctx)1085185a700Sflorian hex_decode_finish(hex_decode_ctx_t *ctx) {
1095185a700Sflorian 	if (ctx->length > 0)
1105185a700Sflorian 		return (ISC_R_UNEXPECTEDEND);
1115185a700Sflorian 	if (ctx->digits != 0)
1125185a700Sflorian 		return (ISC_R_BADHEX);
1135185a700Sflorian 	return (ISC_R_SUCCESS);
1145185a700Sflorian }
1155185a700Sflorian 
1165185a700Sflorian isc_result_t
isc_hex_decodestring(const char * cstr,isc_buffer_t * target)1175185a700Sflorian isc_hex_decodestring(const char *cstr, isc_buffer_t *target) {
1185185a700Sflorian 	hex_decode_ctx_t ctx;
1195185a700Sflorian 
1205185a700Sflorian 	hex_decode_init(&ctx, -1, target);
1215185a700Sflorian 	for (;;) {
1225185a700Sflorian 		int c = *cstr++;
1235185a700Sflorian 		if (c == '\0')
1245185a700Sflorian 			break;
1255185a700Sflorian 		if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
1265185a700Sflorian 			continue;
1275185a700Sflorian 		RETERR(hex_decode_char(&ctx, c));
1285185a700Sflorian 	}
1295185a700Sflorian 	RETERR(hex_decode_finish(&ctx));
1305185a700Sflorian 	return (ISC_R_SUCCESS);
1315185a700Sflorian }
132