FreeCalypso > hg > fc-pcsc-tools
comparison simtool/dumpdir.c @ 1:2071b28cd0c7
simtool: first refactored version
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Thu, 11 Feb 2021 23:04:28 +0000 |
parents | |
children | ce189c97b7b1 |
comparison
equal
deleted
inserted
replaced
0:f7145c77b7fb | 1:2071b28cd0c7 |
---|---|
1 /* | |
2 * This module implements the dump of EF_DIR. | |
3 */ | |
4 | |
5 #include <sys/types.h> | |
6 #include <stdio.h> | |
7 #include <stdlib.h> | |
8 #include "simresp.h" | |
9 #include "curfile.h" | |
10 #include "file_id.h" | |
11 | |
12 static | |
13 check_all_blank() | |
14 { | |
15 u_char *dp, *endp; | |
16 | |
17 dp = sim_resp_data; | |
18 endp = sim_resp_data + sim_resp_data_len; | |
19 while (dp < endp) | |
20 if (*dp++ != 0xFF) | |
21 return(0); | |
22 return(1); | |
23 } | |
24 | |
25 static void | |
26 dump_aid(tlv) | |
27 u_char *tlv; | |
28 { | |
29 unsigned reclen, n; | |
30 | |
31 reclen = tlv[1]; | |
32 printf(" AID:"); | |
33 for (n = 0; n < reclen; n++) | |
34 printf(" %02X", tlv[n+2]); | |
35 putchar('\n'); | |
36 } | |
37 | |
38 static void | |
39 dump_label(tlv) | |
40 u_char *tlv; | |
41 { | |
42 int rc; | |
43 unsigned textlen; | |
44 | |
45 printf(" Label: "); | |
46 rc = validate_alpha_field(tlv + 2, tlv[1], &textlen); | |
47 if (rc < 0) { | |
48 printf("malformed\n"); | |
49 return; | |
50 } | |
51 print_alpha_field(tlv + 2, textlen, stdout); | |
52 putchar('\n'); | |
53 } | |
54 | |
55 static void | |
56 dump_unknown_tlv(tlv) | |
57 u_char *tlv; | |
58 { | |
59 unsigned reclen, n; | |
60 | |
61 reclen = tlv[1] + 2; | |
62 printf(" TLV:"); | |
63 for (n = 0; n < reclen; n++) | |
64 printf(" %02X", tlv[n]); | |
65 putchar('\n'); | |
66 } | |
67 | |
68 static void | |
69 dump_record(recno) | |
70 unsigned recno; | |
71 { | |
72 unsigned totlen, reclen; | |
73 u_char *dp, *endp; | |
74 | |
75 printf("Record #%u:\n", recno); | |
76 if (sim_resp_data[0] != 0x61) { | |
77 printf(" bad: first byte != 0x61\n"); | |
78 return; | |
79 } | |
80 totlen = sim_resp_data[1]; | |
81 if (totlen < 3 || totlen > 0x7F) { | |
82 printf(" bad: global length byte 0x%02X is invalid\n", totlen); | |
83 return; | |
84 } | |
85 if (totlen + 2 > sim_resp_data_len) { | |
86 printf(" bad: TLV global length exceeds EF record length\n"); | |
87 return; | |
88 } | |
89 dp = sim_resp_data + 2; | |
90 endp = sim_resp_data + 2 + totlen; | |
91 while (dp < endp) { | |
92 if (endp - dp < 2) { | |
93 trunc_error: printf(" bad: truncated TLV record\n"); | |
94 return; | |
95 } | |
96 if ((dp[0] & 0x1F) == 0x1F) { | |
97 printf(" bad: extended tag not supported\n"); | |
98 return; | |
99 } | |
100 if (dp[1] & 0x80) { | |
101 printf(" bad: extended length not supported\n"); | |
102 return; | |
103 } | |
104 reclen = dp[1] + 2; | |
105 if (endp - dp < reclen) | |
106 goto trunc_error; | |
107 switch (dp[0]) { | |
108 case 0x4F: | |
109 dump_aid(dp); | |
110 break; | |
111 case 0x50: | |
112 dump_label(dp); | |
113 break; | |
114 default: | |
115 dump_unknown_tlv(dp); | |
116 } | |
117 dp += reclen; | |
118 } | |
119 } | |
120 | |
121 cmd_uicc_dir() | |
122 { | |
123 int rc; | |
124 unsigned recno; | |
125 | |
126 rc = select_op(FILEID_MF); | |
127 if (rc < 0) | |
128 return(rc); | |
129 rc = select_op(EF_DIR); | |
130 if (rc < 0) | |
131 return(rc); | |
132 rc = parse_ef_select_response(); | |
133 if (rc < 0) | |
134 return(rc); | |
135 if (curfile_structure != 0x01) { | |
136 fprintf(stderr, "error: EF_DIR is not linear fixed\n"); | |
137 return(-1); | |
138 } | |
139 if (curfile_record_len < 5) { | |
140 fprintf(stderr, "error: EF_DIR record length is too short\n"); | |
141 return(-1); | |
142 } | |
143 for (recno = 1; recno <= curfile_record_count; recno++) { | |
144 rc = readrec_op(recno, 0x04, curfile_record_len); | |
145 if (rc < 0) | |
146 return(rc); | |
147 if (check_all_blank()) | |
148 continue; | |
149 dump_record(recno); | |
150 } | |
151 return(0); | |
152 } |