1 #include "private.h"
2
3 #include <wdns.h>
4
5 extern bool loadfunc(uint8_t *data, size_t len);
6 extern bool testfunc(void);
7 extern void freefunc(void);
8
9 static bool
hex_to_int(char hex,uint8_t * val)10 hex_to_int(char hex, uint8_t *val)
11 {
12 if (islower((unsigned char) hex))
13 hex = toupper((unsigned char) hex);
14
15 switch (hex) {
16 case '0':
17 case '1':
18 case '2':
19 case '3':
20 case '4':
21 case '5':
22 case '6':
23 case '7':
24 case '8':
25 case '9':
26 *val = (hex - '0');
27 return (true);
28 case 'A':
29 case 'B':
30 case 'C':
31 case 'D':
32 case 'E':
33 case 'F':
34 *val = (hex - 55);
35 return (true);
36 default:
37 printf("hex_to_int() failed\n");
38 return (false);
39 }
40 }
41
42 static bool
hex_decode(const char * hex,uint8_t ** raw,size_t * len)43 hex_decode(const char *hex, uint8_t **raw, size_t *len)
44 {
45 size_t hexlen = strlen(hex);
46 uint8_t *p;
47
48 if (hexlen == 0 || (hexlen % 2) != 0)
49 return (false);
50
51 *len = hexlen / 2;
52
53 p = *raw = malloc(*len);
54 if (*raw == NULL)
55 return (false);
56
57 while (hexlen != 0) {
58 uint8_t val[2];
59
60 if (!hex_to_int(*hex, &val[0]))
61 goto err;
62 hex++;
63 if (!hex_to_int(*hex, &val[1]))
64 goto err;
65 hex++;
66
67 *p = (val[0] << 4) | val[1];
68 p++;
69
70 hexlen -= 2;
71 }
72
73 return (true);
74 err:
75 free(*raw);
76 return (false);
77 }
78
79 int
main(int argc,char ** argv)80 main(int argc, char **argv)
81 {
82 size_t rawlen;
83 uint8_t *rawdata;
84
85 if (argc != 2) {
86 fprintf(stderr, "Usage: %s <HEXDATA>\n", argv[0]);
87 return (EXIT_FAILURE);
88 }
89
90 if (!hex_decode(argv[1], &rawdata, &rawlen)) {
91 fprintf(stderr, "Error: unable to decode hex\n");
92 return (EXIT_FAILURE);
93 }
94
95 if (loadfunc(rawdata, rawlen)) {
96 testfunc();
97 freefunc();
98 } else {
99 free(rawdata);
100 fprintf(stderr, "Error: load function failed\n");
101 return (EXIT_FAILURE);
102 }
103
104 free(rawdata);
105
106 return (EXIT_SUCCESS);
107 }
108