annotate uicc/hexstr.c @ 158:65a2a96386cd

fc-uicc-tool: hex string parsing with min and max length, matching fc-simtool
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 07 Feb 2021 04:00:27 +0000
parents baf5bd698764
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
158
65a2a96386cd fc-uicc-tool: hex string parsing with min and max length,
Mychaela Falconia <falcon@freecalypso.org>
parents: 138
diff changeset
23 decode_hex_data_from_string(arg, databuf, minlen, maxlen)
138
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;
158
65a2a96386cd fc-uicc-tool: hex string parsing with min and max length,
Mychaela Falconia <falcon@freecalypso.org>
parents: 138
diff changeset
26 unsigned minlen, maxlen;
138
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) {
158
65a2a96386cd fc-uicc-tool: hex string parsing with min and max length,
Mychaela Falconia <falcon@freecalypso.org>
parents: 138
diff changeset
40 fprintf(stderr, "error: hex string is too long\n");
138
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 }
158
65a2a96386cd fc-uicc-tool: hex string parsing with min and max length,
Mychaela Falconia <falcon@freecalypso.org>
parents: 138
diff changeset
47 if (count < minlen) {
65a2a96386cd fc-uicc-tool: hex string parsing with min and max length,
Mychaela Falconia <falcon@freecalypso.org>
parents: 138
diff changeset
48 fprintf(stderr, "error: hex string is too short\n");
138
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 }