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 reading hex data from stdin.
|
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 <stdio.h>
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdlib.h>
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 read_hex_from_stdin(databuf, maxlen)
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 u_char *databuf;
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 unsigned maxlen;
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 {
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 unsigned count;
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 int c, c2;
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 for (count = 0; ; count++) {
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 do
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 c = getchar();
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 while (isspace(c));
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 if (c < 0)
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 break;
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 if (!isxdigit(c)) {
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 inv_input: fprintf(stderr, "error: invalid hex input on stdin\n");
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 return(-1);
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 }
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 c2 = getchar();
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 if (!isxdigit(c2))
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 goto inv_input;
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 if (count >= maxlen) {
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 fprintf(stderr, "error: stdin hex data is too long\n");
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 return(-1);
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 }
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 databuf[count] = (decode_hex_digit(c) << 4) |
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 decode_hex_digit(c2);
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 }
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 if (!count) {
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 fprintf(stderr, "error: no hex data given on stdin\n");
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 return(-1);
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 }
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 return(count);
|
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 }
|