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