FreeCalypso > hg > freecalypso-hwlab
annotate uicc/hexstr.c @ 151:d515cfbb3f39
fc-simtool: hex string parsing: add minimum length parameter
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sun, 07 Feb 2021 00:18:30 +0000 |
parents | baf5bd698764 |
children | 65a2a96386cd |
rev | line source |
---|---|
138
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains the function for decoding hex strings. |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <ctype.h> |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <string.h> |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <strings.h> |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <stdio.h> |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <stdlib.h> |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 decode_hex_digit(c) |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 { |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 if (c >= '0' && c <= '9') |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 return(c - '0'); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 if (c >= 'A' && c <= 'F') |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 return(c - 'A' + 10); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 if (c >= 'a' && c <= 'f') |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 return(c - 'a' + 10); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 return(-1); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 } |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 decode_hex_data_from_string(arg, databuf, maxlen) |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 char *arg; |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 u_char *databuf; |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 unsigned maxlen; |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 { |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 unsigned count; |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 for (count = 0; ; count++) { |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 while (isspace(*arg)) |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 arg++; |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 if (!*arg) |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 break; |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 if (!isxdigit(arg[0]) || !isxdigit(arg[1])) { |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 fprintf(stderr, "error: invalid hex string input\n"); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 return(-1); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 } |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 if (count >= maxlen) { |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 fprintf(stderr, "error: hex input data is too long\n"); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 return(-1); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 } |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 databuf[count] = (decode_hex_digit(arg[0]) << 4) | |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 decode_hex_digit(arg[1]); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 arg += 2; |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 } |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 if (!count) { |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 fprintf(stderr, "error: empty hex string argument\n"); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 return(-1); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 } |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 return(count); |
baf5bd698764
fc-uicc-tool: select-aid command implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 } |