comparison uicc/bfsearch.c @ 153:2ef31306be22

fc-uicc-tool bfsearch-{adf,mf} implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 25 Feb 2021 17:46:53 +0000
parents simtool/bfsearch.c@26d7a8815515
children cb3c40ff443e
comparison
equal deleted inserted replaced
152:77832c9f2001 153:2ef31306be22
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 (!rc) {
29 fprintf(stderr,
30 "error selecting 0x%04X: file not found\n",
31 path[n]);
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 (!rc)
55 continue;
56 rc = get_response_op();
57 if (rc < 0)
58 return(rc);
59 tlv_file_desc = extract_select_resp_tag(0x82);
60 if (!tlv_file_desc)
61 return(-1);
62 if (tlv_file_desc[1] < 1) {
63 fprintf(stderr,
64 "error: file desc TLV object is too short\n");
65 return(-1);
66 }
67 for (n = 0; n < pathlen; n++)
68 fprintf(outf, "%04X/", path[n]);
69 fprintf(outf, "%04X: file desc 0x%02X", bfs, tlv_file_desc[2]);
70 if ((tlv_file_desc[2] & 0xBF) == 0x38) {
71 fprintf(outf, ", DF\n");
72 if (ndfc < 255)
73 df_children[ndfc++] = bfs;
74 } else {
75 tlv_file_size = extract_select_resp_tag(0x80);
76 if (tlv_file_size && tlv_file_size[1] == 2)
77 fprintf(outf, ", total size %u",
78 (tlv_file_size[2] << 8) |
79 tlv_file_size[3]);
80 if (tlv_file_desc[1] == 5)
81 fprintf(outf, ", %u records of %u bytes",
82 tlv_file_desc[6],
83 (tlv_file_desc[4] << 8) |
84 tlv_file_desc[5]);
85 putc('\n', outf);
86 }
87 rc = elem_select_op(path[pathlen-1]);
88 if (rc < 0)
89 return(rc);
90 if (!rc) {
91 fprintf(stderr,
92 "reselecting starting file ID 0x%04X not-found error\n",
93 path[pathlen-1]);
94 return(-1);
95 }
96 }
97 if (pathlen >= 8)
98 return(0);
99 for (n = 0; n < pathlen; n++)
100 childpath[n] = path[n];
101 for (n = 0; n < ndfc; n++) {
102 childpath[pathlen] = df_children[n];
103 rc = bfsearch_dir(childpath, pathlen + 1, df_children, ndfc,
104 outf);
105 if (rc < 0)
106 return(rc);
107 }
108 return(0);
109 }
110
111 cmd_bfsearch_mf(argc, argv, outf)
112 char **argv;
113 FILE *outf;
114 {
115 unsigned initpath;
116
117 initpath = FILEID_MF;
118 return bfsearch_dir(&initpath, 1, &initpath, 1, outf);
119 }
120
121 cmd_bfsearch_adf(argc, argv, outf)
122 char **argv;
123 FILE *outf;
124 {
125 unsigned initpath;
126
127 initpath = FILEID_ADF;
128 return bfsearch_dir(&initpath, 1, &initpath, 1, outf);
129 }