1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
|
/*
* PSX-Tools Bundle Pack
* Copyright (C) 2002 Nicolas "Pixel" Noble
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "cdutils.h"
#include "generic.h"
#include "fileutils.h"
int lga = 0;
struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"ppf", 1, NULL, 'p'},
{"mode", 1, NULL, 'm'},
{"force", 1, NULL, 'f'},
{0, 0, NULL, 0 }
};
void showhelp(void) {
printm(M_BARE,
"Usage:\n"
"cd-tool [-m <mode>] [-p <ppf file>] [-f] <isofile> <command> [command args]\n"
"Where mode can be 1 for MODE 1, 2 for MODE 2, 3 for MODE 2 FORM 1,\n"
"4 for MODE 2 FORM 2 and 5 for autodetect, which is the default behaviour.\n"
"\n"
"Command can be one of:\n"
" infos - dumps a bunch of infos about the iso.\n"
" path - dumps the path table.\n"
" printdir <path> - show the directoy listing of <path>\n"
" extract-file <file> <path> - extract the file <path> to <file>\n"
" extract <file> <addr> <size> - extract some sectors to <file>\n"
" insert-file <file> <path> - insert the file to <path>\n"
" insert <file> <addr> - insert some sectors at <addr>\n"
"\n");
}
int main(int argc, char ** argv) {
int type = GUESS, c, size, force = 0, sector;
int iso_r, iso_w;
char * ppf = 0, * iso_name = 0, * arg1 = 0, * arg2 = 0, * f;
verbosity = M_WARNING;
while ((c = getopt_long(argc, argv, "Hhm:p:f", long_options, NULL)) != EOF) {
switch (c) {
case 'h':
case 'H':
case '?':
showhelp();
exit(0);
case 'm':
type = atoi(optarg);
break;
case 'p':
ppf = strdup(optarg);
break;
case 'f':
force = 1;
break;
}
}
if (argc == optind) {
showhelp();
printm(M_ERROR, "Need an iso filename to work on.\n");
exit(-1);
}
iso_name = argv[optind++];
if ((iso_r = open(iso_name, O_RDONLY)) < 0) {
printm(M_ERROR, "Failed to open %s for reading.\n", iso_name);
exit(-1);
}
if (argc == optind) {
showhelp();
printm(M_ERROR, "Need a command to execute.\n");
exit(-1);
}
get_iso_infos(iso_r);
if (!strcmp(argv[optind], "infos")) {
show_iso_infos(iso_r);
} else if (!strcmp(argv[optind], "path")) {
show_pt_infos(iso_r);
} else if (!strcmp(argv[optind], "printdir")) {
struct DirEntry dir;
optind++;
if ((argc - 1) != optind) {
showhelp();
printm(M_ERROR, "printdir needs one argument.\n");
exit(-1);
}
arg1 = argv[optind];
dir = find_path(iso_r, f = strdup(arg1));
free(f);
if (!dir.R) {
printm(M_ERROR, "Path %s was not found on iso.\n", arg1);
exit(-1);
}
if (!(dir.Flags & 2)) {
printm(M_ERROR, "Path %s design a file and not a directory.\n", arg1);
exit(-1);
}
show_head_entry();
show_dir(iso_r, &dir);
} else if (!strcmp(argv[optind], "extract-file")) {
struct DirEntry dir;
int file;
optind++;
if ((argc - 2) != optind) {
showhelp();
printm(M_ERROR, "extract-file needs two arguments.\n");
exit(-1);
}
arg1 = argv[optind++];
arg2 = argv[optind++];
if ((file = open(arg1, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
printm(M_ERROR, "Failed to open file %s for writing.\n", arg1);
exit(-1);
}
dir = find_path(iso_r, f = strdup(arg2));
free(f);
if (!dir.R) {
printm(M_ERROR, "Path %s was not found on iso.\n", arg2);
exit(-1);
}
printm(M_STATUS, "Reading path %s to file %s.\n", arg2, arg1);
read_file(iso_r, file, type, dir.Sector, dir.Size);
} else if (!strcmp(argv[optind], "extract")) {
int file;
optind++;
if ((argc - 3) != optind) {
showhelp();
printm(M_ERROR, "extract needs three arguments.\n");
exit(-1);
}
arg1 = argv[optind++];
size = atoi(argv[optind++]);
sector = atoi(argv[optind++]);
if ((file = open(arg1, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) {
printm(M_ERROR, "Failed to open file %s for writing.\n", arg1);
exit(-1);
}
printm(M_STATUS, "Reading %i bytes from sector %i to file %s.\n", size, sector, arg1);
read_file(iso_r, file, type, sector, size);
} else if (!strcmp(argv[optind], "insert-file")) {
struct DirEntry dir, * d;
unsigned char * buffer;
int file;
int old_type;
if ((iso_w = open(iso_name, O_WRONLY)) < 0) {
printm(M_ERROR, "Failed to open %s for writing.\n", iso_name);
exit(-1);
}
lseek(iso_w, 0, SEEK_SET);
if (ppf) {
if (open_ppf(ppf, iso_r, "Created by CD-Tool") < 0) {
printm(M_ERROR, "Failed to open file %s for writing.\n", ppf);
}
}
optind++;
if ((argc - 2) != optind) {
showhelp();
printm(M_ERROR, "insert-file needs two arguments.\n");
exit(-1);
}
arg1 = argv[optind++];
arg2 = argv[optind++];
if ((file = open(arg1, O_RDONLY)) < 0) {
printm(M_ERROR, "Failed to open file %s for reading.\n", arg1);
exit(-1);
}
dir = find_path(iso_r, f = strdup(arg2));
free(f);
if (!dir.R) {
printm(M_ERROR, "Path %s was not found on iso.\n", arg2);
exit(-1);
}
old_type = guess_type(iso_r, dir.Sector);
if (type == GUESS) {
type = old_type;
}
if (((dir.Size / sec_sizes[old_type]) + ((dir.Size % sec_sizes[old_type]) ? 1 : 0)) <
((filesize(file) / sec_sizes[type]) + ((filesize(file) % sec_sizes[type]) ? 1 : 0))) {
if (force) {
printm(M_WARNING, "New file too big: %i vs %i in %s.\n", dir.Size, filesize(file), sec_modes[type]);
} else {
printm(M_ERROR, "New file too big: %i vs %i in %s.\n", dir.Size, filesize(file), sec_modes[type]);
exit(-1);
}
}
printm(M_STATUS, "Writing file %s at path %s.\n", arg1, arg2);
write_file(iso_r, iso_w, file, type, dir.Sector);
printm(M_STATUS, "Updating directory entry.\n");
dir = find_parent(iso_r, f = strdup(arg2));
free(f);
if ((f = strrchr(arg2, '/'))) {
f++;
} else {
f = arg2;
}
d = find_dir_entry(iso_r, &buffer, &dir, f);
d->Size = filesize(file);
write_datas(iso_r, iso_w, buffer, GUESS, dir.Sector, dir.Size);
free(buffer);
} else if (!strcmp(argv[optind], "insert")) {
int file;
if ((iso_w = open(iso_name, O_WRONLY)) < 0) {
printm(M_ERROR, "Failed to open %s for writing.\n", iso_name);
exit(-1);
}
lseek(iso_w, 0, SEEK_SET);
if (ppf) {
if (open_ppf(ppf, iso_r, "Created by CD-Tool") < 0) {
printm(M_ERROR, "Failed to open file %s for writing.\n", ppf);
}
}
optind++;
if ((argc - 2) != optind) {
showhelp();
printm(M_ERROR, "insert needs two arguments.\n");
exit(-1);
}
arg1 = argv[optind++];
sector = atoi(argv[optind++]);
if ((file = open(arg1, O_RDONLY)) < 0) {
printm(M_ERROR, "Failed to open file %s for reading.\n", arg1);
exit(-1);
}
printm(M_STATUS, "Writing file %s at sector %i.\n", arg1, sector);
write_file(iso_r, iso_w, file, type, sector);
} else {
showhelp();
printm(M_ERROR, "Command %s unknow.\n", argv[optind]);
exit(-1);
}
exit(0);
}
|