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