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