FreeCalypso > hg > fc-pcsc-tools
annotate simtool/hlread.c @ 162:274bfd8bccd0
libutil: ICCID Luhn function implemented
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 26 Feb 2021 21:16:45 +0000 |
parents | 2aa92dfcb4b5 |
children | 8d073ff62c48 |
rev | line source |
---|---|
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module implements some high-level or user-friendly read commands. |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 */ |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <sys/types.h> |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <stdio.h> |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include "simresp.h" |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include "curfile.h" |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include "file_id.h" |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
110
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
11 cmd_iccid(argc, argv, outf) |
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
12 char **argv; |
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
13 FILE *outf; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 int rc; |
55
4c570522de5d
fc-simtool iccid command: strip padding F digits from display
Mychaela Falconia <falcon@freecalypso.org>
parents:
7
diff
changeset
|
16 char buf[21], *cp; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 rc = select_op(FILEID_MF); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 rc = select_op(EF_ICCID); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 rc = parse_ef_select_response(); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 if (curfile_structure != 0x00 || curfile_total_size != 10) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
28 fprintf(stderr, "error: expected transparent EF of 10 bytes\n"); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
29 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
30 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
31 rc = readbin_op(0, 10); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
32 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
33 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
34 decode_reversed_nibbles(sim_resp_data, 10, buf); |
55
4c570522de5d
fc-simtool iccid command: strip padding F digits from display
Mychaela Falconia <falcon@freecalypso.org>
parents:
7
diff
changeset
|
35 for (cp = buf + 20; (cp > buf + 1) && (cp[-1] == 'F'); cp--) |
4c570522de5d
fc-simtool iccid command: strip padding F digits from display
Mychaela Falconia <falcon@freecalypso.org>
parents:
7
diff
changeset
|
36 ; |
4c570522de5d
fc-simtool iccid command: strip padding F digits from display
Mychaela Falconia <falcon@freecalypso.org>
parents:
7
diff
changeset
|
37 *cp = '\0'; |
110
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
38 fprintf(outf, "%s\n", buf); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 return(0); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 |
110
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
42 cmd_imsi(argc, argv, outf) |
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
43 char **argv; |
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
44 FILE *outf; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 int rc; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 char buf[17]; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 rc = select_op(DF_GSM); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 rc = select_op(EF_IMSI); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 rc = parse_ef_select_response(); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 if (curfile_structure != 0x00 || curfile_total_size != 9) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 fprintf(stderr, "error: expected transparent EF of 9 bytes\n"); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 rc = readbin_op(0, 9); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 decode_reversed_nibbles(sim_resp_data + 1, 8, buf); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 buf[16] = '\0'; |
110
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
67 fprintf(outf, "%s parity=%c len=%u\n", buf + 1, buf[0], |
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
68 sim_resp_data[0]); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
69 return(0); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
70 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
71 |
110
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
72 cmd_spn(argc, argv, outf) |
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
73 char **argv; |
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
74 FILE *outf; |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 int rc; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 unsigned textlen; |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 rc = select_op(DF_GSM); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 rc = select_op(EF_SPN); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 rc = parse_ef_select_response(); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
87 return(rc); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
88 if (curfile_structure != 0x00 || curfile_total_size != 17) { |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
89 fprintf(stderr, "error: expected transparent EF of 17 bytes\n"); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
90 return(-1); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
91 } |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
92 rc = readbin_op(0, 17); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
93 if (rc < 0) |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
94 return(rc); |
110
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
95 fprintf(outf, "Display condition: %02X\n", sim_resp_data[0]); |
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
96 fputs("SPN: ", outf); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
97 rc = validate_alpha_field(sim_resp_data + 1, 16, &textlen); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
98 if (rc >= 0) |
110
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
99 print_alpha_field(sim_resp_data, textlen, outf); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
100 else |
110
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
101 fputs("malformed alpha field", outf); |
2aa92dfcb4b5
fc-simtool iccid, imsi, spn: support output redirection
Mychaela Falconia <falcon@freecalypso.org>
parents:
55
diff
changeset
|
102 putc('\n', outf); |
1
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
103 return(0); |
2071b28cd0c7
simtool: first refactored version
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
104 } |