FreeCalypso > hg > fc-pcsc-tools
annotate libutil/hexstr.c @ 189:123dc7370581
top Makefile: install scripts
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 06 Mar 2021 21:11:21 +0000 |
parents | 3698c8192d2d |
children |
rev | line source |
---|---|
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains the function for decoding hex strings. |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <ctype.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdio.h> |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 decode_hex_data_from_string(arg, databuf, minlen, maxlen) |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 char *arg; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 u_char *databuf; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 unsigned minlen, maxlen; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 unsigned count; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 for (count = 0; ; count++) { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 while (isspace(*arg)) |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 arg++; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 if (!*arg) |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 break; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 fprintf(stderr, "error: invalid hex string input\n"); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 return(-1); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 if (count >= maxlen) { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 fprintf(stderr, "error: hex string is too long\n"); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 return(-1); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 databuf[count] = (decode_hex_digit(arg[0]) << 4) | |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 decode_hex_digit(arg[1]); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 arg += 2; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (count < minlen) { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 fprintf(stderr, "error: hex string is too short\n"); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 return(-1); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 return(count); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |