xref: /openbsd-src/usr.sbin/unbound/testcode/readhex.c (revision 712b2f300c854afbcde4b11834961322e3c11161)
1*712b2f30Ssthen /*
2*712b2f30Ssthen  * testcode/readhex.c - read hex data.
3*712b2f30Ssthen  *
4*712b2f30Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5*712b2f30Ssthen  *
6*712b2f30Ssthen  * This software is open source.
7*712b2f30Ssthen  *
8*712b2f30Ssthen  * Redistribution and use in source and binary forms, with or without
9*712b2f30Ssthen  * modification, are permitted provided that the following conditions
10*712b2f30Ssthen  * are met:
11*712b2f30Ssthen  *
12*712b2f30Ssthen  * Redistributions of source code must retain the above copyright notice,
13*712b2f30Ssthen  * this list of conditions and the following disclaimer.
14*712b2f30Ssthen  *
15*712b2f30Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16*712b2f30Ssthen  * this list of conditions and the following disclaimer in the documentation
17*712b2f30Ssthen  * and/or other materials provided with the distribution.
18*712b2f30Ssthen  *
19*712b2f30Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20*712b2f30Ssthen  * be used to endorse or promote products derived from this software without
21*712b2f30Ssthen  * specific prior written permission.
22*712b2f30Ssthen  *
23*712b2f30Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*712b2f30Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*712b2f30Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*712b2f30Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*712b2f30Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*712b2f30Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*712b2f30Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*712b2f30Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*712b2f30Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*712b2f30Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*712b2f30Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*712b2f30Ssthen  *
35*712b2f30Ssthen  */
36*712b2f30Ssthen /**
37*712b2f30Ssthen  * \file
38*712b2f30Ssthen  * Declarations useful for the unit tests.
39*712b2f30Ssthen  */
40*712b2f30Ssthen #include "config.h"
41*712b2f30Ssthen #include <ctype.h>
42*712b2f30Ssthen #include "testcode/readhex.h"
43*712b2f30Ssthen #include "util/log.h"
44*712b2f30Ssthen #include "sldns/sbuffer.h"
45*712b2f30Ssthen #include "sldns/parseutil.h"
46*712b2f30Ssthen 
47*712b2f30Ssthen /** skip whitespace */
48*712b2f30Ssthen static void
skip_whites(const char ** p)49*712b2f30Ssthen skip_whites(const char** p)
50*712b2f30Ssthen {
51*712b2f30Ssthen 	while(1) {
52*712b2f30Ssthen 		while(isspace((unsigned char)**p))
53*712b2f30Ssthen 			(*p)++;
54*712b2f30Ssthen 		if(**p == ';') {
55*712b2f30Ssthen 			/* comment, skip until newline */
56*712b2f30Ssthen 			while(**p && **p != '\n')
57*712b2f30Ssthen 				(*p)++;
58*712b2f30Ssthen 			if(**p == '\n')
59*712b2f30Ssthen 				(*p)++;
60*712b2f30Ssthen 		} else return;
61*712b2f30Ssthen 	}
62*712b2f30Ssthen }
63*712b2f30Ssthen 
64*712b2f30Ssthen /* takes a hex string and puts into buffer */
hex_to_buf(sldns_buffer * pkt,const char * hex)65*712b2f30Ssthen void hex_to_buf(sldns_buffer* pkt, const char* hex)
66*712b2f30Ssthen {
67*712b2f30Ssthen 	const char* p = hex;
68*712b2f30Ssthen 	int val;
69*712b2f30Ssthen 	sldns_buffer_clear(pkt);
70*712b2f30Ssthen 	while(*p) {
71*712b2f30Ssthen 		skip_whites(&p);
72*712b2f30Ssthen 		if(sldns_buffer_position(pkt) == sldns_buffer_limit(pkt))
73*712b2f30Ssthen 			fatal_exit("hex_to_buf: buffer too small");
74*712b2f30Ssthen 		if(!isalnum((unsigned char)*p))
75*712b2f30Ssthen 			break;
76*712b2f30Ssthen 		val = sldns_hexdigit_to_int(*p++) << 4;
77*712b2f30Ssthen 		skip_whites(&p);
78*712b2f30Ssthen 		log_assert(*p && isalnum((unsigned char)*p));
79*712b2f30Ssthen 		val |= sldns_hexdigit_to_int(*p++);
80*712b2f30Ssthen 		sldns_buffer_write_u8(pkt, (uint8_t)val);
81*712b2f30Ssthen 		skip_whites(&p);
82*712b2f30Ssthen 	}
83*712b2f30Ssthen 	sldns_buffer_flip(pkt);
84*712b2f30Ssthen }
85*712b2f30Ssthen 
86