FreeCalypso > hg > fc-pcsc-tools
annotate libcommon/gsm7_decode.c @ 74:8562d8508cf2
grcard2-set-{adm,super}-hex commands implemented
It appears that GrcardSIM2 cards allow arbitrary 64-bit keys
for ADM and SUPER ADM, not necessarily consisting of ASCII digits
like the specs require for standard PIN and PUK, and pySim-prog.py
in fact sets the ADM key to 4444444444444444 in hex by default,
which is not an ASCII digit string. If the cards allow such keys,
we need to support them too.
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Tue, 16 Feb 2021 04:10:36 +0000 |
parents | d4dc86195382 |
children |
rev | line source |
---|---|
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
21
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
2 * This module contains functions for decoding GSM7 strings |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * that exist in various SIM files. |
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 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <sys/types.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 static char gsm7_decode_table[128] = { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 '@', 0, '$', 0, 0, 0, 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 0, 0, '\n', 0, 0, '\r', 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 0, '_', 0, 0, 0, 0, 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 0, 0, 0, 0, 0, 0, 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 ' ', '!', '"', '#', 0, '%', '&', 0x27, |
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 '0', '1', '2', '3', '4', '5', '6', '7', |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 '8', '9', ':', ';', '<', '=', '>', '?', |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 'X', 'Y', 'Z', 0, 0, 0, 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 'x', 'y', 'z', 0, 0, 0, 0, 0 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 }; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 static char gsm7ext_decode_table[128] = { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 0, 0, 0, 0, '^', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 0, 0, 0, 0, 0, 0, 0, 0, '{', '}', 0, 0, 0, 0, 0, '\\', |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '[', '~', ']', 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 '|', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
35 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
36 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
37 }; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
38 |
21
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
39 void |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
40 print_gsm7_string_to_file(data, nbytes, outf) |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 u_char *data; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 unsigned nbytes; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 FILE *outf; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 u_char *dp, *endp; |
21
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
46 int b, c; |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 dp = data; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 endp = data + nbytes; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 putc('"', outf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 while (dp < endp) { |
21
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
52 b = *dp++; |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
53 if (b == 0x1B) { |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
54 if (dp >= endp || *dp == 0x1B || *dp == '\n' || |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
55 *dp == '\r') { |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
56 putc('\\', outf); |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
57 putc('e', outf); |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
58 continue; |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
59 } |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
60 b = *dp++; |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
61 c = gsm7ext_decode_table[b]; |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
62 if (!c) { |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
63 fprintf(outf, "\\e\\%02X", b); |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
64 continue; |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
65 } |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
66 } else { |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
67 c = gsm7_decode_table[b]; |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
68 if (!c) { |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
69 fprintf(outf, "\\%02X", b); |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
70 continue; |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
71 } |
d4dc86195382
GSM7 quoted string output factored out, uses new escapes
Mychaela Falconia <falcon@freecalypso.org>
parents:
0
diff
changeset
|
72 } |
0
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 if (c == '\n') { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 putc('\\', outf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 putc('n', outf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 continue; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 if (c == '\r') { |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 putc('\\', outf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 putc('r', outf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 continue; |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 if (c == '"' || c == '\\') |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 putc('\\', outf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 putc(c, outf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 } |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 putc('"', outf); |
f7145c77b7fb
starting libcommon: factored out of fc-simtool
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 } |