annotate libutil/hexstr.c @ 1:b149db92cb0e

libutil started
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Feb 2021 19:08:14 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * This module contains the function for decoding hex strings.
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <ctype.h>
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <string.h>
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <strings.h>
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 decode_hex_digit(c)
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 {
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 if (c >= '0' && c <= '9')
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 return(c - '0');
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 if (c >= 'A' && c <= 'F')
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 return(c - 'A' + 10);
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 if (c >= 'a' && c <= 'f')
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 return(c - 'a' + 10);
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 return(-1);
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 }
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 decode_hex_data_from_string(arg, databuf, minlen, maxlen)
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 char *arg;
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 u_char *databuf;
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 unsigned minlen, maxlen;
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 {
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 unsigned count;
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 for (count = 0; ; count++) {
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 while (isspace(*arg))
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 arg++;
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (!*arg)
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 break;
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) {
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 fprintf(stderr, "error: invalid hex string input\n");
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 return(-1);
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (count >= maxlen) {
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 fprintf(stderr, "error: hex string is too long\n");
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 return(-1);
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 }
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 databuf[count] = (decode_hex_digit(arg[0]) << 4) |
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 decode_hex_digit(arg[1]);
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 arg += 2;
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (count < minlen) {
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 fprintf(stderr, "error: hex string is too short\n");
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 return(-1);
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 }
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 return(count);
b149db92cb0e libutil started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 }