-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpar.c
575 lines (508 loc) · 17.7 KB
/
grpar.c
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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*-
* Copyright (c) 2010-2014 Ganael LAPLANCHE <[email protected]>
* All rights reserved.
*
* 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 the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, 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 AUTHORS 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 AUTHORS 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.
*/
/*
Group file format, see : http://advsys.net/ken/build.htm
12 bytes : "KenSilverman"
4 bytes : number of files (little-endian)
Then, for each file :
12 bytes : file name (zero-filled)
4 bytes : file size (little-endian)
Then, for each file :
n bytes : file data
[...]
*/
/* uint32_t, uint8_t */
#include <stdint.h>
/* printf(3) */
#include <stdio.h>
/* malloc(3) */
#include <stdlib.h>
/* strncmp(3) */
#include <string.h>
/* open(2) */
#include <fcntl.h>
/* read(2), getopt(3) */
#include <sys/types.h>
#if defined(_WIN32)
#include <getopt.h>
#else
#include <sys/uio.h>
#include <unistd.h>
#endif
#if defined(__FreeBSD__)
/* le32toh(9) */
#include <sys/endian.h>
#elif defined(__linux__)
/* le32toh(3) */
#include <endian.h>
#else
#define le32toh(x) (x)
#endif
/* stat(2) */
#include <sys/stat.h>
/* Windows-Unix compat */
#if !defined(O_BINARY)
#define O_BINARY 0
#endif
#if !defined(S_ISDIR)
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
#define GRPAR_VERSION "0.2"
#define GRPHDR_MAGIC "KenSilverman" /* magic */
#define GRPHDR_MAGICLEN 12 /* magic length */
#define GRPHDR_NUMFILESLEN 4 /* bytes for number of files */
#define GRPHDR_FILENAMELEN 12 /* bytes for file name */
#define GRPHDR_FILESIZELEN 4 /* bytes for file size */
/* File entry within group archive */
struct grp_file;
struct grp_file {
uint32_t index; /* file position */
char file_name[GRPHDR_FILENAMELEN + 1]; /* file name + '\0' */
uint32_t file_size; /* file size in bytes */
off_t file_offset; /* file offset in group archive */
struct grp_file *next; /* next one */
};
/* Program options */
struct program_options {
char *grp_filename;
char *dst_dirname;
#define ACTION_NONE 0
#define ACTION_LIST 1
#define ACTION_EXTRACT 2
uint8_t action;
uint8_t verbose;
};
/* Initialize grp_file structures from a grp file
Returns a file handle on group file as well as num_files found in archive */
int
init_grp_files(const char *filename, struct grp_file **head,
uint32_t *num_files)
{
int grp_file_handle;
/* Main header */
char headbuf[GRPHDR_MAGICLEN + GRPHDR_NUMFILESLEN];
/* Per-file header */
char filebuf[GRPHDR_FILENAMELEN + GRPHDR_FILESIZELEN];
struct grp_file *current = NULL;
struct grp_file *previous = NULL;
if((filename == NULL) || (head == NULL) || (*head != NULL) ||
(num_files == NULL)) {
fprintf(stderr, "%s(): invalid argument\n", __func__);
return (-1);
}
/* Open group archive */
if((grp_file_handle = open(filename, O_RDONLY|O_BINARY)) < 0) {
fprintf(stderr, "cannot open group archive : %s\n", filename);
return (-1);
}
/* Read main header */
if(read(grp_file_handle, &headbuf[0], GRPHDR_MAGICLEN + GRPHDR_NUMFILESLEN)
< (GRPHDR_MAGICLEN + GRPHDR_NUMFILESLEN)) {
fprintf(stderr, "group archive header truncated\n");
close(grp_file_handle);
return (-1);
}
/* Check file type */
if(strncmp(&headbuf[0], GRPHDR_MAGIC, GRPHDR_MAGICLEN) != 0) {
fprintf(stderr, "unrecognized group archive : %s\n", filename);
close(grp_file_handle);
return (-1);
}
/* Get number of files */
(*num_files) = le32toh(*((uint32_t *)&headbuf[GRPHDR_MAGICLEN]));
uint32_t i = 0;
while(i < (*num_files)) {
/* Backup current structure pointer and initialize a new structure */
previous = current;
if((current = malloc(sizeof(struct grp_file))) == NULL) {
fprintf(stderr, "cannot allocate memory\n");
close(grp_file_handle);
return (-1);
}
/* Set head on first pass */
if(*head == NULL)
*head = current;
/* Read group archive's next 16 bytes */
if(read(grp_file_handle, &filebuf[0],
GRPHDR_FILENAMELEN + GRPHDR_FILESIZELEN)
< (GRPHDR_FILENAMELEN + GRPHDR_FILESIZELEN)) {
fprintf(stderr, "group archive header truncated\n");
close(grp_file_handle);
return (-1);
}
/* Update current structure */
current->index = i + 1;
strncpy(¤t->file_name[0], &filebuf[0], GRPHDR_FILENAMELEN);
current->file_name[GRPHDR_FILENAMELEN] = '\0';
current->file_size =
le32toh(*((uint32_t *)&filebuf[GRPHDR_FILENAMELEN]));
current->file_offset =
(previous == NULL) ?
/* first file */
(GRPHDR_MAGICLEN + GRPHDR_NUMFILESLEN +
((GRPHDR_FILENAMELEN + GRPHDR_FILESIZELEN) * (*num_files))) :
/* next ones */
(previous->file_offset + previous->file_size);
current->next = NULL; /* will be set in next pass */
/* Set previous' next pointer */
if(previous != NULL)
previous->next = current;
i++;
}
return (grp_file_handle);
}
/* Un-initialize grp_file structures */
void
uninit_grp_files(int grp_file_handle, struct grp_file *head)
{
struct grp_file *current = head;
struct grp_file *next;
while(current != NULL) {
next = current->next;
free(current);
current = next;
}
close(grp_file_handle);
return;
}
/* Dump grp_file structures */
void
dump_grp_files(struct grp_file *head, uint8_t verbose)
{
struct grp_file *current = head;
while(current != NULL) {
if(verbose == 1)
fprintf(stdout, "%s (%d bytes, offset %d (0x%x))\n",
current->file_name, current->file_size,
(int)current->file_offset,
(int)current->file_offset);
else
fprintf(stdout, "%s\n", current->file_name);
current = current->next;
}
return;
}
/* Extract a single file from group archive */
int
extract_single_file(int grp_file_handle, const char *lookup_filename,
const char *dest_filename, struct grp_file *head, uint8_t verbose)
{
struct grp_file *current = head;
int dest_file_handle;
#define RBUF_SIZE 512
char rbuf[RBUF_SIZE]; /* our read buffer */
if((lookup_filename == NULL) || (dest_filename == NULL)) {
fprintf(stderr, "%s(): invalid argument\n", __func__);
return (-1);
}
while(current != NULL) {
/* If requested file found */
if(strncmp(lookup_filename, current->file_name, GRPHDR_FILENAMELEN)
== 0) {
if(verbose == 1)
fprintf(stdout, "%s\n", lookup_filename);
/* Open output file */
if((dest_file_handle =
open(dest_filename, O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, 0660)) < 0) {
fprintf(stderr, "cannot create destination file : %s\n",
dest_filename);
return (-1);
}
/* Seek to file position and copy data */
lseek(grp_file_handle, current->file_offset, SEEK_SET);
/* And copy file to destination */
uint32_t remaining_bytes = current->file_size;
int bytes_read;
while((bytes_read =
read(grp_file_handle, &rbuf[0], (remaining_bytes > RBUF_SIZE) ?
RBUF_SIZE : remaining_bytes)) > 0) {
if(write(dest_file_handle, &rbuf[0], bytes_read) < bytes_read) {
fprintf(stderr, "incomplete write to destination file : "
"%s\n", dest_filename);
close(dest_file_handle);
return (-1);
}
remaining_bytes -= bytes_read;
}
/* Has whole file been copied ? */
if(remaining_bytes > 0) {
fprintf(stderr, "file partially extracted : %s\n",
lookup_filename);
close(dest_file_handle);
return (-1);
}
close(dest_file_handle);
return (0);
}
current = current->next;
}
fprintf(stderr, "%s : not found in group archive\n", lookup_filename);
return (-1);
}
/* Extract all files from group archive */
int
extract_all_files(int grp_file_handle, const char *base_path,
struct grp_file *head, uint8_t verbose)
{
struct grp_file *current = head;
char *dest_path;
int err = 0;
if(base_path == NULL) {
fprintf(stderr, "%s(): invalid argument\n", __func__);
return (-1);
}
while(current != NULL) {
dest_path = (char *)malloc(strlen(base_path) + 1 +
strlen(current->file_name) + 1); /* includes '/' and final '\0' */
if(dest_path == NULL) {
fprintf(stderr, "cannot allocate memory\n");
return (-1);
}
dest_path[0] = '\0';
strcat(dest_path, base_path);
strcat(dest_path, "/");
strcat(dest_path, current->file_name);
err |= extract_single_file(grp_file_handle,
current->file_name, dest_path, head, verbose);
free(dest_path);
current = current->next;
}
return (err);
}
/* Print grpar version */
void
version(void)
{
fprintf(stderr, "grpar, v." GRPAR_VERSION
", (c) 2010 - Ganael LAPLANCHE, http://contribs.martymac.org\n");
}
/* Print grpar usage */
void
usage(void)
{
version();
fprintf(stderr, "usage: grpar [-h] [-V] [-t|-x] [-C path] [-v] "
"-f grp_file [file_1] [file_2] [...]\n");
fprintf(stderr, "-h : this help\n");
fprintf(stderr, "-V : version\n");
fprintf(stderr, "-t : list files from group archive\n");
fprintf(stderr, "-x : extract files from group archive\n");
fprintf(stderr, "-C : specify destination directory\n");
fprintf(stderr, "-v : verbose mode\n");
fprintf(stderr, "-f : group archive\n");
return;
}
/* Initialize global options structure */
void
init_options(struct program_options *options)
{
/* Set default options */
options->grp_filename = NULL;
options->dst_dirname = NULL;
options->action = ACTION_NONE;
options->verbose = 0;
}
/* Un-initialize global options structure */
void
uninit_options(struct program_options *options)
{
if(options->grp_filename != NULL)
free(options->grp_filename);
if(options->dst_dirname != NULL)
free(options->dst_dirname);
options->action = ACTION_NONE;
options->verbose = 0;
}
int
main(int argc, char **argv)
{
int ch;
struct grp_file *head = NULL;
uint32_t num_files = 0;
int grp_file_handle;
/* Program options */
struct program_options options;
/* Set default options */
init_options(&options);
if(argc <= 1) {
usage();
uninit_options(&options);
return (1);
}
/* Options handling */
while ((ch = getopt(argc, argv, "?hVtxC:vf:")) != -1) {
switch(ch) {
case '?':
case 'h':
usage();
uninit_options(&options);
return (0);
break;
case 'V':
version();
uninit_options(&options);
return (0);
break;
case 't':
if(options.action != ACTION_NONE) {
fprintf(stderr, "please specify either -t or -x option, "
"not both\n");
uninit_options(&options);
return (1);
}
options.action = ACTION_LIST;
break;
case 'x':
if(options.action != ACTION_NONE) {
fprintf(stderr, "please specify either -t or -x option, "
"not both\n");
uninit_options(&options);
return (1);
}
options.action = ACTION_EXTRACT;
break;
case 'C':
options.dst_dirname = malloc(strlen(optarg) + 1);
if(options.dst_dirname == NULL) {
fprintf(stderr, "cannot allocate memory\n");
uninit_options(&options);
return (1);
}
strcpy(options.dst_dirname, optarg);
break;
case 'v':
options.verbose = 1;
break;
case 'f':
options.grp_filename = malloc(strlen(optarg) + 1);
if(options.grp_filename == NULL) {
fprintf(stderr, "cannot allocate memory\n");
uninit_options(&options);
return (1);
}
strcpy(options.grp_filename, optarg);
break;
}
}
argc -= optind;
argv += optind;
if(options.action == ACTION_NONE) {
fprintf(stderr, "please specify either -t or -x option\n");
uninit_options(&options);
return (1);
}
if(options.grp_filename == NULL) {
fprintf(stderr, "please specify a group archive\n");
uninit_options(&options);
return (1);
}
/* Load grp file TOC into memory */
if((grp_file_handle = init_grp_files(options.grp_filename, &head,
&num_files)) < 0) {
fprintf(stderr, "error reading group archive TOC\n");
uninit_grp_files(grp_file_handle, head);
uninit_options(&options);
return (1);
}
/* Let's go */
if(options.action == ACTION_LIST) {
dump_grp_files(head, options.verbose);
if(options.verbose == 1)
fprintf(stdout, "%d files found\n", num_files);
}
else if(options.action == ACTION_EXTRACT) {
struct stat dst_dirname_stat;
/* Set default destination directory */
if(options.dst_dirname == NULL) {
options.dst_dirname = malloc(strlen(".") + 1);
if(options.dst_dirname == NULL) {
fprintf(stderr, "cannot allocate memory\n");
uninit_grp_files(grp_file_handle, head);
uninit_options(&options);
return (1);
}
strcpy(options.dst_dirname, ".");
}
/* Cleanup destination directory */
while((strlen(options.dst_dirname) > 0) &&
(options.dst_dirname[strlen(options.dst_dirname) - 1] == '/')) {
options.dst_dirname[strlen(options.dst_dirname) - 1] = '\0';
}
/* Validate destination directory */
if((stat(options.dst_dirname, &dst_dirname_stat) < 0) ||
(!S_ISDIR(dst_dirname_stat.st_mode))) {
fprintf(stderr, "invalid destination directory specified : %s\n",
options.dst_dirname);
uninit_grp_files(grp_file_handle, head);
uninit_options(&options);
return (1);
}
if(argc <= 0) {
/* No file specified, extract everything */
if(extract_all_files(grp_file_handle, options.dst_dirname, head,
options.verbose) < 0) {
fprintf(stderr, "files extracted, with error(s)\n");
}
else {
if(options.verbose == 1)
fprintf(stdout, "%d files extracted\n", num_files);
}
}
else {
int i;
char *dest_path = NULL;
/* Extract specified file(s) */
for(i = 0 ; i < argc ; i++) {
dest_path = (char *)malloc(strlen(options.dst_dirname) + 1 +
strlen(argv[i]) + 1); /* includes '/' and final '\0' */
if(dest_path == NULL) {
fprintf(stderr, "cannot allocate memory\n");
uninit_grp_files(grp_file_handle, head);
uninit_options(&options);
return (1);
}
dest_path[0] = '\0';
strcat(dest_path, options.dst_dirname);
strcat(dest_path, "/");
strcat(dest_path, argv[i]);
extract_single_file(grp_file_handle, argv[i], dest_path, head,
options.verbose);
free(dest_path);
}
}
}
else {
/* NOTREACHED */
fprintf(stderr, "congratulations, you have reached an unreachable part "
"of the code\n");
uninit_grp_files(grp_file_handle, head);
uninit_options(&options);
return (1);
}
uninit_grp_files(grp_file_handle, head);
uninit_options(&options);
return (0);
}