comparison simtool/saverestore.c @ 1:2071b28cd0c7

simtool: first refactored version
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 11 Feb 2021 23:04:28 +0000
parents
children
comparison
equal deleted inserted replaced
0:f7145c77b7fb 1:2071b28cd0c7
1 /*
2 * This module implements commands for saving SIM file content in UNIX host
3 * files and restoring these backups back to the SIM.
4 */
5
6 #include <sys/types.h>
7 #include <sys/file.h>
8 #include <sys/stat.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include "simresp.h"
13 #include "curfile.h"
14 #include "file_id.h"
15
16 static
17 savebin_transparent(outf)
18 FILE *outf;
19 {
20 unsigned off, cc;
21 int rc;
22
23 for (off = 0; off < curfile_total_size; off += cc) {
24 cc = curfile_total_size - off;
25 if (cc > 256)
26 cc = 256;
27 rc = readbin_op(off, cc);
28 if (rc < 0)
29 return(rc);
30 fwrite(sim_resp_data, 1, cc, outf);
31 }
32 return(0);
33 }
34
35 static
36 savebin_records(outf)
37 FILE *outf;
38 {
39 unsigned recno;
40 int rc;
41
42 for (recno = 1; recno <= curfile_record_count; recno++) {
43 rc = readrec_op(recno, 0x04, curfile_record_len);
44 if (rc < 0)
45 return(rc);
46 fwrite(sim_resp_data, curfile_record_len, 1, outf);
47 }
48 return(0);
49 }
50
51 cmd_savebin(argc, argv)
52 char **argv;
53 {
54 int file_id, rc;
55 FILE *of;
56
57 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
58 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
59 file_id = strtoul(argv[1], 0, 16);
60 else
61 file_id = find_symbolic_file_name(argv[1]);
62 if (file_id < 0) {
63 fprintf(stderr,
64 "error: file ID argument is not a hex value or a recognized symbolic name\n");
65 return(-1);
66 }
67 rc = select_op(file_id);
68 if (rc < 0)
69 return(rc);
70 rc = parse_ef_select_response();
71 if (rc < 0)
72 return(rc);
73 of = fopen(argv[2], "w");
74 if (!of) {
75 perror(argv[2]);
76 return(-1);
77 }
78 switch (curfile_structure) {
79 case 0x00:
80 /* transparent */
81 rc = savebin_transparent(of);
82 break;
83 case 0x01:
84 case 0x03:
85 /* record-based */
86 rc = savebin_records(of);
87 break;
88 }
89 fclose(of);
90 return(rc);
91 }
92
93 cmd_save_sms_bin(argc, argv)
94 char **argv;
95 {
96 int rc;
97 FILE *of;
98
99 rc = select_op(DF_TELECOM);
100 if (rc < 0)
101 return(rc);
102 rc = select_op(EF_SMS);
103 if (rc < 0)
104 return(rc);
105 rc = parse_ef_select_response();
106 if (rc < 0)
107 return(rc);
108 if (curfile_structure != 0x01 || curfile_record_len != 176) {
109 fprintf(stderr,
110 "error: EF_SMS is not linear fixed with 176-byte records\n");
111 return(-1);
112 }
113 of = fopen(argv[1], "w");
114 if (!of) {
115 perror(argv[1]);
116 return(-1);
117 }
118 rc = savebin_records(of);
119 fclose(of);
120 return(rc);
121 }
122
123 static
124 restore_transparent(data)
125 u_char *data;
126 {
127 unsigned off, cc;
128 int rc;
129
130 for (off = 0; off < curfile_total_size; off += cc) {
131 cc = curfile_total_size - off;
132 if (cc > 255)
133 cc = 255;
134 rc = update_bin_op(off, data + off, cc);
135 if (rc < 0)
136 return(rc);
137 }
138 return(0);
139 }
140
141 static
142 restore_records(data)
143 u_char *data;
144 {
145 unsigned recno;
146 u_char *dp;
147 int rc;
148
149 dp = data;
150 for (recno = 1; recno <= curfile_record_count; recno++) {
151 rc = update_rec_op(recno, 0x04, dp, curfile_record_len);
152 if (rc < 0)
153 return(rc);
154 dp += curfile_record_len;
155 }
156 return(0);
157 }
158
159 cmd_restore_file(argc, argv)
160 char **argv;
161 {
162 int file_id, rc, fd;
163 struct stat st;
164 u_char *databuf;
165
166 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) &&
167 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4])
168 file_id = strtoul(argv[1], 0, 16);
169 else
170 file_id = find_symbolic_file_name(argv[1]);
171 if (file_id < 0) {
172 fprintf(stderr,
173 "error: file ID argument is not a hex value or a recognized symbolic name\n");
174 return(-1);
175 }
176 rc = select_op(file_id);
177 if (rc < 0)
178 return(rc);
179 rc = parse_ef_select_response();
180 if (rc < 0)
181 return(rc);
182 if (!curfile_total_size) {
183 printf("SIM indicates file of zero length, nothing to do\n");
184 return(0);
185 }
186 fd = open(argv[2], O_RDONLY);
187 if (fd < 0) {
188 perror(argv[2]);
189 return(-1);
190 }
191 fstat(fd, &st);
192 if ((st.st_mode & S_IFMT) != S_IFREG) {
193 fprintf(stderr, "error: %s is not a regular file\n", argv[2]);
194 close(fd);
195 return(-1);
196 }
197 if (st.st_size != curfile_total_size) {
198 fprintf(stderr,
199 "error: length of %s does not match SIM EF length of %u bytes\n",
200 argv[2], curfile_total_size);
201 close(fd);
202 return(-1);
203 }
204 databuf = malloc(curfile_total_size);
205 if (!databuf) {
206 perror("malloc for file data");
207 close(fd);
208 return(-1);
209 }
210 read(fd, databuf, curfile_total_size);
211 close(fd);
212 switch (curfile_structure) {
213 case 0x00:
214 /* transparent */
215 rc = restore_transparent(databuf);
216 break;
217 case 0x01:
218 /* record-based */
219 rc = restore_records(databuf);
220 break;
221 case 0x03:
222 fprintf(stderr, "error: cyclic files are not supported\n");
223 rc = -1;
224 }
225 free(databuf);
226 return(rc);
227 }