FreeCalypso > hg > fc-pcsc-tools
comparison simtool/restorebin.c @ 16:a6ca422323b9
simtool: saverestore.c split into savebin.c and restorebin.c
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 12 Feb 2021 01:41:21 +0000 |
parents | simtool/saverestore.c@2071b28cd0c7 |
children | 54cebab70c19 |
comparison
equal
deleted
inserted
replaced
15:5390dce9faa4 | 16:a6ca422323b9 |
---|---|
1 /* | |
2 * This module implements the restore-file command; this command | |
3 * reads binary files previously saved with the savebin command | |
4 * and writes the backed-up bits back to the SIM. | |
5 */ | |
6 | |
7 #include <sys/types.h> | |
8 #include <sys/file.h> | |
9 #include <sys/stat.h> | |
10 #include <ctype.h> | |
11 #include <stdio.h> | |
12 #include <stdlib.h> | |
13 #include <unistd.h> | |
14 #include "curfile.h" | |
15 | |
16 static | |
17 restore_transparent(data) | |
18 u_char *data; | |
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 > 255) | |
26 cc = 255; | |
27 rc = update_bin_op(off, data + off, cc); | |
28 if (rc < 0) | |
29 return(rc); | |
30 } | |
31 return(0); | |
32 } | |
33 | |
34 static | |
35 restore_records(data) | |
36 u_char *data; | |
37 { | |
38 unsigned recno; | |
39 u_char *dp; | |
40 int rc; | |
41 | |
42 dp = data; | |
43 for (recno = 1; recno <= curfile_record_count; recno++) { | |
44 rc = update_rec_op(recno, 0x04, dp, curfile_record_len); | |
45 if (rc < 0) | |
46 return(rc); | |
47 dp += curfile_record_len; | |
48 } | |
49 return(0); | |
50 } | |
51 | |
52 cmd_restore_file(argc, argv) | |
53 char **argv; | |
54 { | |
55 int file_id, rc, fd; | |
56 struct stat st; | |
57 u_char *databuf; | |
58 | |
59 if (isxdigit(argv[1][0]) && isxdigit(argv[1][1]) && | |
60 isxdigit(argv[1][2]) && isxdigit(argv[1][3]) && !argv[1][4]) | |
61 file_id = strtoul(argv[1], 0, 16); | |
62 else | |
63 file_id = find_symbolic_file_name(argv[1]); | |
64 if (file_id < 0) { | |
65 fprintf(stderr, | |
66 "error: file ID argument is not a hex value or a recognized symbolic name\n"); | |
67 return(-1); | |
68 } | |
69 rc = select_op(file_id); | |
70 if (rc < 0) | |
71 return(rc); | |
72 rc = parse_ef_select_response(); | |
73 if (rc < 0) | |
74 return(rc); | |
75 if (!curfile_total_size) { | |
76 printf("SIM indicates file of zero length, nothing to do\n"); | |
77 return(0); | |
78 } | |
79 fd = open(argv[2], O_RDONLY); | |
80 if (fd < 0) { | |
81 perror(argv[2]); | |
82 return(-1); | |
83 } | |
84 fstat(fd, &st); | |
85 if ((st.st_mode & S_IFMT) != S_IFREG) { | |
86 fprintf(stderr, "error: %s is not a regular file\n", argv[2]); | |
87 close(fd); | |
88 return(-1); | |
89 } | |
90 if (st.st_size != curfile_total_size) { | |
91 fprintf(stderr, | |
92 "error: length of %s does not match SIM EF length of %u bytes\n", | |
93 argv[2], curfile_total_size); | |
94 close(fd); | |
95 return(-1); | |
96 } | |
97 databuf = malloc(curfile_total_size); | |
98 if (!databuf) { | |
99 perror("malloc for file data"); | |
100 close(fd); | |
101 return(-1); | |
102 } | |
103 read(fd, databuf, curfile_total_size); | |
104 close(fd); | |
105 switch (curfile_structure) { | |
106 case 0x00: | |
107 /* transparent */ | |
108 rc = restore_transparent(databuf); | |
109 break; | |
110 case 0x01: | |
111 /* record-based */ | |
112 rc = restore_records(databuf); | |
113 break; | |
114 case 0x03: | |
115 fprintf(stderr, "error: cyclic files are not supported\n"); | |
116 rc = -1; | |
117 } | |
118 free(databuf); | |
119 return(rc); | |
120 } |