comparison uicc/bfsearch.c @ 15:b70d35f5476f

fc-uicc-tool ported over
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 14 Mar 2021 07:41:09 +0000
parents
children
comparison
equal deleted inserted replaced
14:b7ee2e85686b 15:b70d35f5476f
1 /*
2 * This module implements a brute force search of file ID space,
3 * both MF and ADF trees.
4 */
5
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include "simresp.h"
9 #include "file_id.h"
10
11 extern u_char *extract_select_resp_tag();
12
13 static
14 bfsearch_dir(path, pathlen, siblings, nsiblings, outf)
15 unsigned *path, pathlen, *siblings, nsiblings;
16 FILE *outf;
17 {
18 unsigned bfs, n;
19 unsigned df_children[255], ndfc;
20 unsigned childpath[8];
21 u_char *tlv_file_desc, *tlv_file_size;
22 int rc;
23
24 for (n = 0; n < pathlen; n++) {
25 rc = elem_select_op(path[n]);
26 if (rc < 0)
27 return(rc);
28 if ((sim_resp_sw & 0xFF00) != 0x6100) {
29 fprintf(stderr,
30 "error selecting 0x%04X: SW resp 0x%04X\n",
31 path[n], sim_resp_sw);
32 return(-1);
33 }
34 }
35 ndfc = 0;
36 for (bfs = 0; bfs <= 0xFFFF; bfs++) {
37 if (bfs == FILEID_MF || bfs == FILEID_ADF)
38 continue;
39 for (n = 0; n < pathlen; n++) {
40 if (bfs == path[n])
41 break;
42 }
43 if (n < pathlen)
44 continue;
45 for (n = 0; n < nsiblings; n++) {
46 if (bfs == siblings[n])
47 break;
48 }
49 if (n < nsiblings)
50 continue;
51 rc = elem_select_op(bfs);
52 if (rc < 0)
53 return(rc);
54 if (sim_resp_sw == 0x6A82)
55 continue;
56 if ((sim_resp_sw & 0xFF00) != 0x6100) {
57 for (n = 0; n < pathlen; n++)
58 fprintf(outf, "%04X/", path[n]);
59 fprintf(outf, "%04X: SW response 0x%04X\n", bfs,
60 sim_resp_sw);
61 continue;
62 }
63 rc = get_response_op();
64 if (rc < 0)
65 return(rc);
66 tlv_file_desc = extract_select_resp_tag(0x82);
67 if (!tlv_file_desc)
68 return(-1);
69 if (tlv_file_desc[1] < 1) {
70 fprintf(stderr,
71 "error: file desc TLV object is too short\n");
72 return(-1);
73 }
74 for (n = 0; n < pathlen; n++)
75 fprintf(outf, "%04X/", path[n]);
76 fprintf(outf, "%04X: file desc 0x%02X", bfs, tlv_file_desc[2]);
77 if ((tlv_file_desc[2] & 0xBF) == 0x38) {
78 fprintf(outf, ", DF\n");
79 if (ndfc < 255)
80 df_children[ndfc++] = bfs;
81 } else {
82 tlv_file_size = extract_select_resp_tag(0x80);
83 if (tlv_file_size && tlv_file_size[1] == 2)
84 fprintf(outf, ", total size %u",
85 (tlv_file_size[2] << 8) |
86 tlv_file_size[3]);
87 if (tlv_file_desc[1] == 5)
88 fprintf(outf, ", %u records of %u bytes",
89 tlv_file_desc[6],
90 (tlv_file_desc[4] << 8) |
91 tlv_file_desc[5]);
92 putc('\n', outf);
93 }
94 rc = elem_select_op(path[pathlen-1]);
95 if (rc < 0)
96 return(rc);
97 if ((sim_resp_sw & 0xFF00) != 0x6100) {
98 fprintf(stderr,
99 "reselecting starting file ID 0x%04X: SW resp 0x%04X\n",
100 path[pathlen-1], sim_resp_sw);
101 return(-1);
102 }
103 }
104 if (pathlen >= 8)
105 return(0);
106 for (n = 0; n < pathlen; n++)
107 childpath[n] = path[n];
108 for (n = 0; n < ndfc; n++) {
109 childpath[pathlen] = df_children[n];
110 rc = bfsearch_dir(childpath, pathlen + 1, df_children, ndfc,
111 outf);
112 if (rc < 0)
113 return(rc);
114 }
115 return(0);
116 }
117
118 cmd_bfsearch_mf(argc, argv, outf)
119 char **argv;
120 FILE *outf;
121 {
122 unsigned initpath;
123
124 initpath = FILEID_MF;
125 return bfsearch_dir(&initpath, 1, &initpath, 1, outf);
126 }
127
128 cmd_bfsearch_adf(argc, argv, outf)
129 char **argv;
130 FILE *outf;
131 {
132 unsigned initpath;
133
134 initpath = FILEID_ADF;
135 return bfsearch_dir(&initpath, 1, &initpath, 1, outf);
136 }