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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
/*
Copyright (c) 2011, 2012, Simon Howard
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice appear
in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "lib/lha_arch.h"
#include "extract.h"
#include "safe.h"
// Maximum number of dots in progress output:
#define MAX_PROGRESS_LEN 58
typedef struct {
int invoked;
LHAFileHeader *header;
LHAOptions *options;
char *filename;
char *operation;
} ProgressCallbackData;
// Given a file header structure, get the path to extract to.
// Returns a newly allocated string that must be free()d.
static char *file_full_path(LHAFileHeader *header, LHAOptions *options)
{
size_t len;
char *result;
char *p;
// Full path, or filename only?
len = 0;
if (options->extract_path != NULL) {
len += strlen(options->extract_path) + 1;
}
if (options->use_path && header->path != NULL) {
len += strlen(header->path);
}
if (header->filename != NULL) {
len += strlen(header->filename);
}
result = malloc(len + 1);
if (result == NULL) {
// TODO?
exit(-1);
}
result[0] = '\0';
if (options->extract_path != NULL) {
strcat(result, options->extract_path);
strcat(result, "/");
}
// Add path. If it is an absolute path (contains a leading '/')
// then skip over the leading '/' to make it a relative path.
// This prevents the possibility of a security threat with a
// malicious archive that might try to write to arbitrary
// filesystem locations.
// It also removes the double '/' when using the -w option.
if (options->use_path && header->path != NULL) {
p = header->path;
while (*p == '/') {
++p;
}
strcat(result, p);
}
// The filename header field might conceivably try to include
// a path separator as well, so skip over any leading '/'
// here too.
if (header->filename != NULL) {
p = header->filename;
while (*p == '/') {
++p;
}
strcat(result, p);
}
return result;
}
static void print_filename(char *filename, char *status)
{
printf("\r");
safe_printf("%s", filename);
printf("\t- %s ", status);
}
static void print_filename_brief(char *filename)
{
printf("\r");
safe_printf("%s :", filename);
}
// Callback function invoked during decompression progress.
static void progress_callback(unsigned int block,
unsigned int num_blocks,
void *data)
{
ProgressCallbackData *progress = data;
unsigned int factor;
unsigned int i;
progress->invoked = 1;
// If the quiet mode options are specified, print a limited amount
// of information without a progress bar (level 1) or no message
// at all (level 2).
if (progress->options->quiet >= 2) {
return;
} else if (progress->options->quiet == 1) {
if (block == 0) {
print_filename_brief(progress->filename);
fflush(stdout);
}
return;
}
// Scale factor for blocks, so that the line is never too long. When
// MAX_PROGRESS_LEN is exceeded, the length is halved (factor=2), then
// progressively larger scale factors are applied.
factor = 1 + (num_blocks / MAX_PROGRESS_LEN);
num_blocks = (num_blocks + factor - 1) / factor;
// First call to specify number of blocks?
if (block == 0) {
print_filename(progress->filename, progress->operation);
for (i = 0; i < num_blocks; ++i) {
printf(".");
}
print_filename(progress->filename, progress->operation);
} else if (((block + factor - 1) % factor) == 0) {
// Otherwise, signal progress:
printf("o");
}
fflush(stdout);
}
// Print a line to stdout describing a symlink.
static void print_symlink_line(char *src, char *dest)
{
safe_printf("Symbolic Link %s -> %s", src, dest);
printf("\n");
}
// Perform CRC check of an archived file.
static int test_archived_file_crc(LHAReader *reader,
LHAFileHeader *header,
LHAOptions *options)
{
ProgressCallbackData progress;
char *filename;
int success;
filename = file_full_path(header, options);
if (options->dry_run) {
if (strcmp(header->compress_method,
LHA_COMPRESS_TYPE_DIR) != 0) {
safe_printf("VERIFY %s", filename);
printf("\n");
}
free(filename);
return 1;
}
progress.invoked = 0;
progress.operation = "Testing :";
progress.options = options;
progress.filename = filename;
progress.header = header;
success = lha_reader_check(reader, progress_callback, &progress);
if (progress.invoked && options->quiet < 2) {
if (success) {
print_filename(filename, "Tested");
printf("\n");
} else {
print_filename(filename, "CRC error");
printf("\n");
}
fflush(stdout);
}
if (!success) {
// TODO: Exit with error
}
free(filename);
return success;
}
// Check that the specified directory exists, and create it if it
// does not.
static int check_parent_directory(char *path)
{
LHAFileType file_type;
file_type = lha_arch_exists(path);
switch (file_type) {
case LHA_FILE_DIRECTORY:
// Already exists.
break;
case LHA_FILE_NONE:
// Create the missing directory:
if (!lha_arch_mkdir(path, 0755)) {
fprintf(stderr,
"Failed to create parent directory %s\n",
path);
return 0;
}
break;
case LHA_FILE_FILE:
fprintf(stderr, "Parent path %s is not a directory!\n",
path);
return 0;
case LHA_FILE_ERROR:
fprintf(stderr, "Failed to stat %s\n", path);
return 0;
}
return 1;
}
// Given a filename, create its parent directories as necessary.
static int make_parent_directories(char *orig_path)
{
int result;
char *p;
char *path;
result = 1;
// Duplicate the path and strip off any trailing '/'s:
path = strdup(orig_path);
if (path == NULL) {
exit(-1);
}
p = path + strlen(path) - 1;
while (p >= path && *p == '/') {
*p = '\0';
--p;
}
// Iterate through the string, finding each path separator. At
// each place, temporarily chop off the end of the path to get
// each parent directory in turn.
for (p = path; *p == '/'; ++p);
for (;;) {
p = strchr(p, '/');
if (p == NULL) {
break;
}
*p = '\0';
// Check if this parent directory exists and create it:
if (!check_parent_directory(path)) {
result = 0;
break;
}
// Restore path separator and advance to the next path.
*p = '/';
++p;
}
free(path);
return result;
}
// Prompt the user with a message, and return the first character of
// the typed response.
static char prompt_user(char *message)
{
char result;
int c;
fprintf(stderr, "%s", message);
fflush(stderr);
// Read characters until a newline is found, saving the first
// character entered.
result = 0;
do {
c = getchar();
if (c < 0) {
exit(-1);
}
if (result == 0) {
result = c;
}
} while (c != '\n');
return result;
}
// A file to be extracted already exists. Apply the overwrite policy
// to decide whether to overwrite the existing file, prompting the
// user if necessary.
static int confirm_file_overwrite(char *filename, LHAOptions *options)
{
char response;
switch (options->overwrite_policy) {
case LHA_OVERWRITE_PROMPT:
break;
case LHA_OVERWRITE_SKIP:
return 0;
case LHA_OVERWRITE_ALL:
return 1;
}
for (;;) {
safe_fprintf(stderr, "%s ", filename);
response = prompt_user("OverWrite ?(Yes/[No]/All/Skip) ");
switch (tolower((unsigned int) response)) {
case 'y':
return 1;
case 'n':
case '\n':
return 0;
case 'a':
options->overwrite_policy = LHA_OVERWRITE_ALL;
return 1;
case 's':
options->overwrite_policy = LHA_OVERWRITE_SKIP;
return 0;
default:
break;
}
}
return 0;
}
// Check if the file pointed to by the specified header exists.
static int file_exists(char *filename)
{
LHAFileType file_type;
file_type = lha_arch_exists(filename);
if (file_type == LHA_FILE_ERROR) {
fprintf(stderr, "Failed to read file type of '%s'\n", filename);
exit(-1);
}
return file_type != LHA_FILE_NONE;
}
// Extract an archived file.
static int extract_archived_file(LHAReader *reader,
LHAFileHeader *header,
LHAOptions *options)
{
ProgressCallbackData progress;
char *filename;
int success;
int is_dir, is_symlink;
filename = file_full_path(header, options);
is_symlink = header->symlink_target != NULL;
is_dir = !strcmp(header->compress_method, LHA_COMPRESS_TYPE_DIR)
&& !is_symlink;
// If a file already exists with this name, confirm overwrite.
if (!is_dir && !is_symlink && file_exists(filename)
&& !confirm_file_overwrite(filename, options)) {
if (options->overwrite_policy == LHA_OVERWRITE_SKIP) {
safe_printf("%s : Skipped...", filename);
printf("\n");
}
free(filename);
return 1;
}
// No need to extract directories if use_path is disabled.
if (!options->use_path && is_dir) {
return 1;
}
// Create parent directories for file:
if (!make_parent_directories(filename)) {
free(filename);
return 0;
}
progress.invoked = 0;
progress.operation = "Melting :";
progress.options = options;
progress.header = header;
progress.filename = filename;
success = lha_reader_extract(reader, filename,
progress_callback, &progress);
if (!lha_reader_current_is_fake(reader) && options->quiet < 2) {
if (progress.invoked) {
if (success) {
print_filename(filename, "Melted");
printf("\n");
} else {
print_filename(filename, "Failure");
printf("\n");
}
} else if (is_symlink) {
print_symlink_line(filename, header->symlink_target);
}
fflush(stdout);
}
if (!success) {
// TODO: Exit with error
}
free(filename);
return success;
}
// lha -t command.
int test_file_crc(LHAFilter *filter, LHAOptions *options)
{
int result;
result = 1;
for (;;) {
LHAFileHeader *header;
header = lha_filter_next_file(filter);
if (header == NULL) {
break;
}
if (!test_archived_file_crc(filter->reader, header, options)) {
result = 0;
}
}
return result;
}
// lha -en / -xn / -pn:
// Simulate extracting an archive file, but just print the operations
// that would have been performed to stdout.
static int extract_archive_dry_run(LHAFilter *filter, LHAOptions *options)
{
char *filename;
int result;
result = 1;
for (;;) {
LHAFileHeader *header;
header = lha_filter_next_file(filter);
if (header == NULL) {
break;
}
filename = file_full_path(header, options);
// Every line begins the same - "EXTRACT filename..."
safe_printf("EXTRACT %s", filename);
// After the filename we might print something extra.
// The message if we have an existing file is weird, but this
// is just accurately duplicating what the Unix LHA tool says.
// The symlink handling is particularly odd - they are treated
// as directories (a bleed-through of the way in which
// symlinks are stored).
if (header->symlink_target != NULL) {
safe_printf("|%s (directory)", header->symlink_target);
} else if (!strcmp(header->compress_method,
LHA_COMPRESS_TYPE_DIR)) {
safe_printf(" (directory)");
} else if (file_exists(filename)) {
safe_printf(" but file is exist.");
}
printf("\n");
free(filename);
}
return result;
}
// lha -e / -x
int extract_archive(LHAFilter *filter, LHAOptions *options)
{
int result;
if (options->dry_run) {
return extract_archive_dry_run(filter, options);
}
result = 1;
for (;;) {
LHAFileHeader *header;
header = lha_filter_next_file(filter);
if (header == NULL) {
break;
}
if (!extract_archived_file(filter->reader, header, options)) {
result = 0;
}
}
return result;
}
// Dump contents of the current file from the specified reader to stdout.
static int print_archived_file(LHAReader *reader)
{
char buf[512];
size_t bytes;
for (;;) {
bytes = lha_reader_read(reader, buf, sizeof(buf));
if (bytes <= 0) {
break;
}
if (fwrite(buf, 1, bytes, stdout) < bytes) {
return 0;
}
}
return 1;
}
// lha -p
int print_archive(LHAFilter *filter, LHAOptions *options)
{
LHAFileHeader *header;
int is_normal_file;
char *full_path;
// As a weird quirk of Unix LHA, lha -pn is equivalent to lha -en:
if (options->dry_run) {
return extract_archive_dry_run(filter, options);
}
for (;;) {
header = lha_filter_next_file(filter);
if (header == NULL) {
break;
}
is_normal_file = strcmp(header->compress_method,
LHA_COMPRESS_TYPE_DIR) != 0;
// Print "header" before the file containing the filename.
// For normal files this is a three line separator.
// Symlinks get shown in the same way as during extract.
// Directories are ignored.
if (options->quiet < 2) {
full_path = file_full_path(header, options);
if (header->symlink_target != NULL) {
print_symlink_line(full_path,
header->symlink_target);
} else if (is_normal_file) {
printf("::::::::\n");
safe_printf("%s", full_path);
printf("\n::::::::\n");
}
free(full_path);
}
// If this is a normal file, dump the contents to stdout.
if (is_normal_file && !print_archived_file(filter->reader)) {
return 0;
}
}
return 1;
}
|