comparison libutil/hexstdin.c @ 1:b149db92cb0e

libutil started
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 21 Feb 2021 19:08:14 +0000
parents
children
comparison
equal deleted inserted replaced
0:0ac4c3314bf2 1:b149db92cb0e
1 /*
2 * This module contains the function for reading hex data from stdin.
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 read_hex_from_stdin(databuf, maxlen)
11 u_char *databuf;
12 unsigned maxlen;
13 {
14 unsigned count;
15 int c, c2;
16
17 for (count = 0; ; count++) {
18 do
19 c = getchar();
20 while (isspace(c));
21 if (c < 0)
22 break;
23 if (!isxdigit(c)) {
24 inv_input: fprintf(stderr, "error: invalid hex input on stdin\n");
25 return(-1);
26 }
27 c2 = getchar();
28 if (!isxdigit(c2))
29 goto inv_input;
30 if (count >= maxlen) {
31 fprintf(stderr, "error: stdin hex data is too long\n");
32 return(-1);
33 }
34 databuf[count] = (decode_hex_digit(c) << 4) |
35 decode_hex_digit(c2);
36 }
37 if (!count) {
38 fprintf(stderr, "error: no hex data given on stdin\n");
39 return(-1);
40 }
41 return(count);
42 }