comparison libutil/hexstr.c @ 8:34bbb0585cab

libutil: import from previous fc-pcsc-tools version
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 05:42:37 +0000
parents
children
comparison
equal deleted inserted replaced
7:b25d4dfe5798 8:34bbb0585cab
1 /*
2 * This module contains the function for decoding hex strings.
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <stdio.h>
8
9 decode_hex_data_from_string(arg, databuf, minlen, maxlen)
10 char *arg;
11 u_char *databuf;
12 unsigned minlen, maxlen;
13 {
14 unsigned count;
15
16 for (count = 0; ; count++) {
17 while (isspace(*arg))
18 arg++;
19 if (!*arg)
20 break;
21 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) {
22 fprintf(stderr, "error: invalid hex string input\n");
23 return(-1);
24 }
25 if (count >= maxlen) {
26 fprintf(stderr, "error: hex string is too long\n");
27 return(-1);
28 }
29 databuf[count] = (decode_hex_digit(arg[0]) << 4) |
30 decode_hex_digit(arg[1]);
31 arg += 2;
32 }
33 if (count < minlen) {
34 fprintf(stderr, "error: hex string is too short\n");
35 return(-1);
36 }
37 return(count);
38 }