-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy paththdat.c
More file actions
510 lines (452 loc) · 14 KB
/
thdat.c
File metadata and controls
510 lines (452 loc) · 14 KB
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/*
* Redistribution and use in source and binary forms, with
* or without modification, are permitted provided that the
* following conditions are met:
*
* 1. Redistributions of source code must retain this list
* of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce this
* list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
* DAMAGE.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <thtk/thtk.h>
#include "program.h"
#include "util.h"
#include "mygetopt.h"
static void
print_usage(
void)
{
printf("Usage: %s [-V] [[-c | -l | -x] VERSION] [ARCHIVE [FILE...]]\n"
"Options:\n"
" -c create an archive\n"
" -l list the contents of an archive\n"
" -x extract an archive\n"
" -V display version information and exit\n"
"VERSION can be:\n"
" 1, 2, 3, 4, 5, 6, 7, 8, 9, 95, 10, 103 (for Uwabami Breakers), 105, 11, 12, 123, 125, 128, 13, 14, 143, 15, 16, 165, 17 or 18\n"
/* NEWHU: */
"Specify 'd' as VERSION to automatically detect archive format. (-l and -x only)\n\n"
"Report bugs to <" PACKAGE_BUGREPORT ">.\n", argv0);
}
static void
print_error(
thtk_error_t* error)
{
fprintf(stderr, "%s:%s\n", argv0, thtk_error_message(error));
}
typedef struct {
thdat_t* thdat;
thtk_io_t* stream;
} thdat_state_t;
static thdat_state_t*
thdat_state_alloc(void)
{
thdat_state_t* state = malloc(sizeof(*state));
state->thdat = NULL;
state->stream = NULL;
return state;
}
static void
thdat_state_free(
thdat_state_t* state)
{
if (state) {
if (state->thdat)
thdat_free(state->thdat);
if (state->stream)
thtk_io_close(state->stream);
free(state);
}
}
static thdat_state_t*
thdat_open_file(
unsigned int version,
const char* path,
thtk_error_t** error)
{
thdat_state_t* state = thdat_state_alloc();
if (!(state->stream = thtk_io_open_file(path, "rb", error))) {
thdat_state_free(state);
return NULL;
}
if (!(state->thdat = thdat_open(version, state->stream, error))) {
thdat_state_free(state);
return NULL;
}
return state;
}
static int
thdat_extract_file(
thdat_state_t* state,
size_t entry_index,
thtk_error_t** error)
{
const char* entry_name;
thtk_io_t* entry_stream;
if (!(entry_name = thdat_entry_get_name(state->thdat, entry_index, error)))
return 0;
// For th105: Make sure that the directory exists
util_makepath(entry_name);
if (!(entry_stream = thtk_io_open_file(entry_name, "wb", error)))
return 0;
if (thdat_entry_read_data(state->thdat, entry_index, entry_stream, error) == -1) {
thtk_io_close(entry_stream);
return 0;
}
printf("%s\n", entry_name);
thtk_io_close(entry_stream);
return 1;
}
static int
thdat_list(
unsigned int version,
const char* path,
thtk_error_t** error)
{
thdat_state_t* state = thdat_open_file(version, path, error);
if(!state) {
return 0;
}
ssize_t entry_count;
struct {
const char* name;
ssize_t size;
ssize_t zsize;
}* entries;
ssize_t e;
int name_width = 4;
if ((entry_count = thdat_entry_count(state->thdat, error)) == -1) {
thdat_state_free(state);
return 0;
}
entries = malloc(entry_count * sizeof(*entries));
for (e = 0; e < entry_count; ++e) {
thtk_error_t* error = NULL;
entries[e].name = thdat_entry_get_name(state->thdat, e, &error);
entries[e].size = thdat_entry_get_size(state->thdat, e, &error);
entries[e].zsize = thdat_entry_get_zsize(state->thdat, e, &error);
if (!entries[e].name || entries[e].size == -1 || entries[e].zsize == -1) {
print_error(error);
thtk_error_free(&error);
continue;
}
int entry_name_width = strlen(entries[e].name);
if (entry_name_width > name_width)
name_width = entry_name_width;
}
// th105: Stored = Size
if (version == 105 || version == 123)
printf("%-*s %7s\n", name_width, "Name", "Size");
else
printf("%-*s %7s %7s\n", name_width, "Name", "Size", "Stored");
for (e = 0; e < entry_count; ++e) {
if (version == 105 || version == 123)
printf("%-*s %7zd\n", name_width, entries[e].name, entries[e].size);
else
printf("%-*s %7zd %7zd\n", name_width, entries[e].name, entries[e].size, entries[e].zsize);
}
free(entries);
thdat_state_free(state);
return 1;
}
static int
thdat_create_wrapper(
unsigned int version,
const char* path,
const char** paths,
size_t entry_count,
thtk_error_t** error)
{
thdat_state_t* state = thdat_state_alloc();
char*** entries = calloc(entry_count, sizeof(char**));
char** realpaths;
int* entries_count = calloc(entry_count, sizeof(int));
size_t real_entry_count = 0;
if (!(state->stream = thtk_io_open_file(path, "wb", error))) {
thdat_state_free(state);
exit(1);
}
for (size_t i = 0; i < entry_count; i++) {
int n = util_scan_files(paths[i], &entries[i]);
if (n == -1) {
entries[i] = calloc(1, sizeof(char*));
entries[i][0] = malloc(strlen(paths[i])+1);
strcpy(entries[i][0], paths[i]);
n = 1;
}
entries_count[i] = n;
real_entry_count += n;
}
if (!(state->thdat = thdat_create(version, state->stream, real_entry_count, error))) {
thdat_state_free(state);
exit(1);
}
// Set entry names first...
realpaths = calloc(real_entry_count, sizeof(char*));
size_t k = 0;
for (size_t i = 0; i < entry_count; ++i) {
thtk_error_t* error = NULL;
for (size_t j = 0; j < entries_count[i]; j++) {
if (!thdat_entry_set_name(state->thdat, k, entries[i][j], &error)) {
print_error(error);
thtk_error_free(&error);
continue;
}
realpaths[k] = malloc(strlen(entries[i][j])+1);
strcpy(realpaths[k], entries[i][j]);
k++;
free(entries[i][j]);
}
free(entries[i]);
}
free(entries);
free(entries_count);
// ...and then module->create, if this is th105 archive.
// This is because the list of entries comes first in th105 archives.
if (version == 105 || version == 123) {
if (!thdat_init(state->thdat, error))
{
thdat_state_free(state);
exit(1);
}
}
k = 0;
/* TODO: Properly indicate when insertion fails. */
ssize_t i;
for (i = 0; i < real_entry_count; ++i) {
thtk_error_t* error = NULL;
thtk_io_t* entry_stream;
off_t entry_size;
printf("%s...\n", thdat_entry_get_name(state->thdat, i, &error));
// Is entry name set?
if (!(thdat_entry_get_name(state->thdat, i, &error))[0])
continue;
if (!(entry_stream = thtk_io_open_file(realpaths[i], "rb", &error))) {
print_error(error);
thtk_error_free(&error);
continue;
}
if ((entry_size = thtk_io_seek(entry_stream, 0, SEEK_END, &error)) == -1) {
print_error(error);
thtk_error_free(&error);
continue;
}
if (thtk_io_seek(entry_stream, 0, SEEK_SET, &error) == -1) {
print_error(error);
thtk_error_free(&error);
continue;
}
if (thdat_entry_write_data(state->thdat, i, entry_stream, entry_size, &error) == -1) {
print_error(error);
thtk_error_free(&error);
continue;
}
thtk_io_close(entry_stream);
free(realpaths[i]);
}
free(realpaths);
int ret = 1;
if (!thdat_close(state->thdat, error))
ret = 0;
thdat_state_free(state);
return ret;
}
/* TODO: Make sure errors are printed in all cases. */
int
main(
int argc,
char* argv[])
{
thtk_error_t* error = NULL;
unsigned int version = 0;
int mode = -1;
argv0 = util_shortname(argv[0]);
int opt;
int ind=0;
while(argv[util_optind]) {
switch(opt = util_getopt(argc, argv, ":c:l:x:Vd")) {
case 'c':
case 'l':
case 'x':
case 'd':
if(mode != -1) {
fprintf(stderr,"%s: More than one mode specified\n",argv0);
print_usage();
exit(1);
}
mode = opt;
if((opt == 'x' || mode == 'l') && *util_optarg == 'd') {
version = ~0;
}
else if(opt != 'd') version = parse_version(util_optarg);
break;
default:
util_getopt_default(&ind,argv,opt,print_usage);
}
}
argc = ind;
argv[argc] = NULL;
/* detect version */
if(argc && (mode == 'x' || mode == 'l') && version == ~0) {
thtk_io_t* file;
if(!(file = thtk_io_open_file(argv[0], "rb", &error))) {
print_error(error);
thtk_error_free(&error);
exit(1);
}
uint32_t out[4];
unsigned int heur;
printf("Detecting '%s'...\n",argv[0]);
if(-1 == thdat_detect(argv[0], file, out, &heur, &error)) {
thtk_io_close(file);
print_error(error);
thtk_error_free(&error);
exit(1);
}
if(heur == -1) {
const thdat_detect_entry_t* ent;
printf("Couldn't detect version!\nPossible versions: ");
while((ent = thdat_detect_iter(out))) {
printf("%d,",ent->alias);
}
printf("\n");
thtk_io_close(file);
exit(1);
}
else {
printf("Detected version %d\n",heur);
version = heur;
}
thtk_io_close(file);
}
switch (mode) {
case 'd': {
if (argc < 1) {
print_usage();
exit(1);
}
for (int i = 0; i < argc; i++) {
thtk_io_t* file;
if (!(file = thtk_io_open_file(argv[i], "rb", &error))) {
print_error(error);
thtk_error_free(&error);
exit(1);
}
uint32_t out[4];
unsigned int heur;
printf("Detecting '%s'... ",argv[i]);
if (-1 == thdat_detect(argv[i], file, out, &heur, &error)) {
printf("\n");
thtk_io_close(file);
print_error(error);
thtk_error_free(&error);
continue;
}
const thdat_detect_entry_t* ent;
printf("%d | possible versions: ", heur);
while((ent = thdat_detect_iter(out))) {
printf("%d,",ent->alias);
}
printf(" | filename: %d\n", thdat_detect_filename(argv[i]));
thtk_io_close(file);
}
exit(0);
}
case 'l': {
if (argc < 1) {
print_usage();
exit(1);
}
if (!thdat_list(version, argv[0], &error)) {
print_error(error);
thtk_error_free(&error);
exit(1);
}
exit(0);
}
case 'c': {
if (argc < 2) {
print_usage();
exit(1);
}
if (!thdat_create_wrapper(version, argv[0], (const char**)&argv[1], argc - 1, &error)) {
print_error(error);
thtk_error_free(&error);
exit(1);
}
exit(0);
}
case 'x': {
if (argc < 1) {
print_usage();
exit(1);
}
thdat_state_t* state = thdat_open_file(version, argv[0], &error);
if (!state) {
print_error(error);
thtk_error_free(&error);
exit(1);
}
if (argc > 1) {
ssize_t a;
for (a = 1; a < argc; ++a) {
thtk_error_t* error = NULL;
int entry_index;
if ((entry_index = thdat_entry_by_name(state->thdat, argv[a], &error)) == -1) {
print_error(error);
thtk_error_free(&error);
continue;
}
if (!thdat_extract_file(state, entry_index, &error)) {
print_error(error);
thtk_error_free(&error);
continue;
}
}
} else {
ssize_t entry_count;
if ((entry_count = thdat_entry_count(state->thdat, &error)) == -1) {
print_error(error);
thtk_error_free(&error);
exit(1);
}
ssize_t entry_index;
for (entry_index = 0; entry_index < entry_count; ++entry_index) {
thtk_error_t* error = NULL;
if (!thdat_extract_file(state, entry_index, &error)) {
print_error(error);
thtk_error_free(&error);
continue;
}
}
}
thdat_state_free(state);
exit(0);
}
default:
print_usage();
exit(1);
}
}