forked from nih-at/libzip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipcmp.c
706 lines (572 loc) · 19.1 KB
/
zipcmp.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
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/*
zipcmp.c -- compare zip files
Copyright (C) 2003-2019 Dieter Baron and Thomas Klausner
This file is part of libzip, a library to manipulate ZIP archives.
The authors can be contacted at <[email protected]>
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.
3. The names of the authors may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``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 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 <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#ifdef HAVE_FTS_H
#include <fts.h>
#endif
#include <zlib.h>
#ifndef HAVE_GETOPT
#include "getopt.h"
#endif
#include "zip.h"
#include "compat.h"
struct archive {
const char *name;
zip_t *za;
zip_uint64_t nentry;
struct entry *entry;
const char *comment;
size_t comment_length;
};
struct ef {
const char *name;
zip_uint16_t flags;
zip_uint16_t id;
zip_uint16_t size;
const zip_uint8_t *data;
};
struct entry {
char *name;
zip_uint64_t size;
zip_uint32_t crc;
zip_uint32_t comp_method;
struct ef *extra_fields;
zip_uint16_t n_extra_fields;
const char *comment;
zip_uint32_t comment_length;
};
const char *progname;
#define PROGRAM "zipcmp"
#define USAGE "usage: %s [-hipqtVv] archive1 archive2\n"
char help_head[] = PROGRAM " (" PACKAGE ") by Dieter Baron and Thomas Klausner\n\n";
char help[] = "\n\
-h display this help message\n\
-i compare names ignoring case distinctions\n\
-p compare as many details as possible\n\
-q be quiet\n\
-t test zip files (compare file contents to checksum)\n\
-V display version number\n\
-v be verbose (print differences, default)\n\
\n\
Report bugs to <[email protected]>.\n";
char version_string[] = PROGRAM " (" PACKAGE " " VERSION ")\n\
Copyright (C) 2003-2019 Dieter Baron and Thomas Klausner\n\
" PACKAGE " comes with ABSOLUTELY NO WARRANTY, to the extent permitted by law.\n";
#define OPTIONS "hVipqtv"
#define BOTH_ARE_ZIPS(a) (a[0].za && a[1].za)
static int comment_compare(const char *c1, size_t l1, const char *c2, size_t l2);
static int compare_list(char *const name[], const void *l[], const zip_uint64_t n[], int size, int (*cmp)(const void *, const void *), int (*checks)(char *const name[2], const void *, const void *), void (*print)(const void *));
static int compare_zip(char *const zn[]);
static int ef_compare(char *const name[2], const struct entry *e1, const struct entry *e2);
static int ef_order(const void *a, const void *b);
static void ef_print(const void *p);
static int ef_read(zip_t *za, zip_uint64_t idx, struct entry *e);
static int entry_cmp(const void *p1, const void *p2);
static int entry_paranoia_checks(char *const name[2], const void *p1, const void *p2);
static void entry_print(const void *p);
static int is_directory(const char *name);
#ifdef HAVE_FTS_H
static int list_directory(const char *name, struct archive *a);
#endif
static int list_zip(const char *name, struct archive *a);
static int test_file(zip_t *za, zip_uint64_t idx, const char *zipname, const char *filename, zip_uint64_t size, zip_uint32_t crc);
int ignore_case, test_files, paranoid, verbose;
int header_done;
int
main(int argc, char *const argv[]) {
int c;
progname = argv[0];
ignore_case = 0;
test_files = 0;
paranoid = 0;
verbose = 1;
while ((c = getopt(argc, argv, OPTIONS)) != -1) {
switch (c) {
case 'i':
ignore_case = 1;
break;
case 'p':
paranoid = 1;
break;
case 'q':
verbose = 0;
break;
case 't':
test_files = 1;
break;
case 'v':
verbose = 1;
break;
case 'h':
fputs(help_head, stdout);
printf(USAGE, progname);
fputs(help, stdout);
exit(0);
case 'V':
fputs(version_string, stdout);
exit(0);
default:
fprintf(stderr, USAGE, progname);
exit(2);
}
}
if (argc != optind + 2) {
fprintf(stderr, USAGE, progname);
exit(2);
}
exit((compare_zip(argv + optind) == 0) ? 0 : 1);
}
static int
compare_zip(char *const zn[]) {
struct archive a[2];
struct entry *e[2];
zip_uint64_t n[2];
int i;
int res;
for (i = 0; i < 2; i++) {
a[i].name = zn[i];
a[i].entry = NULL;
a[i].nentry = 0;
a[i].za = NULL;
a[i].comment = NULL;
a[i].comment_length = 0;
if (is_directory(zn[i])) {
#ifndef HAVE_FTS_H
fprintf(stderr, "%s: reading directories not supported\n", progname);
exit(2);
#else
if (list_directory(zn[i], a + i) < 0)
exit(2);
paranoid = 0; /* paranoid checks make no sense for directories, since they compare zip metadata */
#endif
}
else {
if (list_zip(zn[i], a + i) < 0)
exit(2);
}
if (a[i].nentry > 0)
qsort(a[i].entry, a[i].nentry, sizeof(a[i].entry[0]), entry_cmp);
}
header_done = 0;
e[0] = a[0].entry;
e[1] = a[1].entry;
n[0] = a[0].nentry;
n[1] = a[1].nentry;
res = compare_list(zn, (const void **)e, n, sizeof(e[i][0]), entry_cmp, paranoid ? entry_paranoia_checks : NULL, entry_print);
if (paranoid) {
if (comment_compare(a[0].comment, a[0].comment_length, a[1].comment, a[1].comment_length) != 0) {
if (verbose) {
printf("--- archive comment (%zu)\n", a[0].comment_length);
printf("+++ archive comment (%zu)\n", a[1].comment_length);
}
res = 1;
}
}
for (i = 0; i < 2; i++) {
zip_uint64_t j;
if (a[i].za) {
zip_close(a[i].za);
}
for (j = 0; j < a[i].nentry; j++) {
free(a[i].entry[j].name);
}
free(a[i].entry);
}
switch (res) {
case 0:
exit(0);
case 1:
exit(1);
default:
exit(2);
}
}
#ifdef HAVE_FTS_H
static zip_int64_t
compute_crc(const char *fname) {
FILE *f;
uLong crc = crc32(0L, Z_NULL, 0);
size_t n;
Bytef buffer[8192];
if ((f = fopen(fname, "rb")) == NULL) {
fprintf(stderr, "%s: can't open %s: %s\n", progname, fname, strerror(errno));
return -1;
}
while ((n = fread(buffer, 1, sizeof(buffer), f)) > 0) {
crc = crc32(crc, buffer, (unsigned int)n);
}
if (ferror(f)) {
fprintf(stderr, "%s: read error on %s: %s\n", progname, fname, strerror(errno));
fclose(f);
return -1;
}
fclose(f);
return (zip_int64_t)crc;
}
#endif
static int
is_directory(const char *name) {
struct stat st;
if (stat(name, &st) < 0)
return 0;
return S_ISDIR(st.st_mode);
}
#ifdef HAVE_FTS_H
static int
list_directory(const char *name, struct archive *a) {
FTS *fts;
FTSENT *ent;
zip_uint64_t nalloc;
size_t prefix_length;
char *const names[2] = {(char *)name, NULL};
if ((fts = fts_open(names, FTS_NOCHDIR | FTS_LOGICAL, NULL)) == NULL) {
fprintf(stderr, "%s: can't open directory '%s': %s\n", progname, name, strerror(errno));
return -1;
}
prefix_length = strlen(name) + 1;
nalloc = 0;
while ((ent = fts_read(fts))) {
zip_int64_t crc;
switch (ent->fts_info) {
case FTS_D:
case FTS_DOT:
case FTS_DP:
case FTS_DEFAULT:
case FTS_SL:
case FTS_NSOK:
break;
case FTS_DC:
case FTS_DNR:
case FTS_ERR:
case FTS_NS:
case FTS_SLNONE:
/* TODO: error */
fts_close(fts);
return -1;
case FTS_F:
if (a->nentry >= nalloc) {
nalloc += 16;
if (nalloc > SIZE_MAX / sizeof(a->entry[0])) {
fprintf(stderr, "%s: malloc failure\n", progname);
exit(1);
}
a->entry = realloc(a->entry, sizeof(a->entry[0]) * nalloc);
if (a->entry == NULL) {
fprintf(stderr, "%s: malloc failure\n", progname);
exit(1);
}
}
a->entry[a->nentry].name = strdup(ent->fts_path + prefix_length);
a->entry[a->nentry].size = (zip_uint64_t)ent->fts_statp->st_size;
if ((crc = compute_crc(ent->fts_accpath)) < 0) {
fts_close(fts);
return -1;
}
a->entry[a->nentry].crc = (zip_uint32_t)crc;
a->nentry++;
break;
}
}
if (fts_close(fts)) {
fprintf(stderr, "%s: error closing directory '%s': %s\n", progname, a->name, strerror(errno));
return -1;
}
return 0;
}
#endif
static int
list_zip(const char *name, struct archive *a) {
zip_t *za;
int err;
struct zip_stat st;
unsigned int i;
if ((za = zip_open(name, paranoid ? ZIP_CHECKCONS : 0, &err)) == NULL) {
zip_error_t error;
zip_error_init_with_code(&error, err);
fprintf(stderr, "%s: cannot open zip archive '%s': %s\n", progname, name, zip_error_strerror(&error));
zip_error_fini(&error);
return -1;
}
a->za = za;
a->nentry = (zip_uint64_t)zip_get_num_entries(za, 0);
if (a->nentry == 0)
a->entry = NULL;
else {
if ((a->nentry > SIZE_MAX / sizeof(a->entry[0])) || (a->entry = (struct entry *)malloc(sizeof(a->entry[0]) * a->nentry)) == NULL) {
fprintf(stderr, "%s: malloc failure\n", progname);
exit(1);
}
for (i = 0; i < a->nentry; i++) {
zip_stat_index(za, i, 0, &st);
a->entry[i].name = strdup(st.name);
a->entry[i].size = st.size;
a->entry[i].crc = st.crc;
if (test_files)
test_file(za, i, name, st.name, st.size, st.crc);
if (paranoid) {
a->entry[i].comp_method = st.comp_method;
ef_read(za, i, a->entry + i);
a->entry[i].comment = zip_file_get_comment(za, i, &a->entry[i].comment_length, 0);
}
else {
a->entry[i].comp_method = 0;
a->entry[i].n_extra_fields = 0;
}
}
if (paranoid) {
int length;
a->comment = zip_get_archive_comment(za, &length, 0);
a->comment_length = (size_t)length;
}
else {
a->comment = NULL;
a->comment_length = 0;
}
}
return 0;
}
static int
comment_compare(const char *c1, size_t l1, const char *c2, size_t l2) {
if (l1 != l2)
return 1;
if (l1 == 0)
return 0;
if (c1 == NULL || c2 == NULL)
return c1 == c2;
return memcmp(c1, c2, (size_t)l2);
}
static int
compare_list(char *const name[2], const void *l[2], const zip_uint64_t n[2], int size, int (*cmp)(const void *, const void *), int (*check)(char *const name[2], const void *, const void *), void (*print)(const void *)) {
unsigned int i[2];
int j, c;
int diff;
#define INC(k) (i[k]++, l[k] = ((const char *)l[k]) + size)
#define PRINT(k) \
do { \
if (header_done == 0 && verbose) { \
printf("--- %s\n+++ %s\n", name[0], name[1]); \
header_done = 1; \
} \
if (verbose) { \
printf("%c ", (k) ? '+' : '-'); \
print(l[k]); \
} \
diff = 1; \
} while (0)
i[0] = i[1] = 0;
diff = 0;
while (i[0] < n[0] && i[1] < n[1]) {
c = cmp(l[0], l[1]);
if (c == 0) {
if (check)
diff |= check(name, l[0], l[1]);
INC(0);
INC(1);
}
else if (c < 0) {
PRINT(0);
INC(0);
}
else {
PRINT(1);
INC(1);
}
}
for (j = 0; j < 2; j++) {
while (i[j] < n[j]) {
PRINT(j);
INC(j);
}
}
return diff;
}
static int
ef_read(zip_t *za, zip_uint64_t idx, struct entry *e) {
zip_int16_t n_local, n_central;
zip_uint16_t i;
if ((n_local = zip_file_extra_fields_count(za, idx, ZIP_FL_LOCAL)) < 0 || (n_central = zip_file_extra_fields_count(za, idx, ZIP_FL_CENTRAL)) < 0) {
return -1;
}
e->n_extra_fields = (zip_uint16_t)(n_local + n_central);
if ((e->extra_fields = (struct ef *)malloc(sizeof(e->extra_fields[0]) * e->n_extra_fields)) == NULL)
return -1;
for (i = 0; i < n_local; i++) {
e->extra_fields[i].name = e->name;
e->extra_fields[i].data = zip_file_extra_field_get(za, idx, i, &e->extra_fields[i].id, &e->extra_fields[i].size, ZIP_FL_LOCAL);
if (e->extra_fields[i].data == NULL)
return -1;
e->extra_fields[i].flags = ZIP_FL_LOCAL;
}
for (; i < e->n_extra_fields; i++) {
e->extra_fields[i].name = e->name;
e->extra_fields[i].data = zip_file_extra_field_get(za, idx, (zip_uint16_t)(i - n_local), &e->extra_fields[i].id, &e->extra_fields[i].size, ZIP_FL_CENTRAL);
if (e->extra_fields[i].data == NULL)
return -1;
e->extra_fields[i].flags = ZIP_FL_CENTRAL;
}
qsort(e->extra_fields, e->n_extra_fields, sizeof(e->extra_fields[0]), ef_order);
return 0;
}
static int
ef_compare(char *const name[2], const struct entry *e1, const struct entry *e2) {
struct ef *ef[2];
zip_uint64_t n[2];
ef[0] = e1->extra_fields;
ef[1] = e2->extra_fields;
n[0] = e1->n_extra_fields;
n[1] = e2->n_extra_fields;
return compare_list(name, (const void **)ef, n, sizeof(struct ef), ef_order, NULL, ef_print);
}
static int
ef_order(const void *ap, const void *bp) {
const struct ef *a, *b;
a = (struct ef *)ap;
b = (struct ef *)bp;
if (a->flags != b->flags)
return a->flags - b->flags;
if (a->id != b->id)
return a->id - b->id;
if (a->size != b->size)
return a->size - b->size;
return memcmp(a->data, b->data, a->size);
}
static void
ef_print(const void *p) {
const struct ef *ef = (struct ef *)p;
int i;
printf(" %s ", ef->name);
printf("%04x %c <", ef->id, ef->flags == ZIP_FL_LOCAL ? 'l' : 'c');
for (i = 0; i < ef->size; i++)
printf("%s%02x", i ? " " : "", ef->data[i]);
printf(">\n");
}
static int
entry_cmp(const void *p1, const void *p2) {
const struct entry *e1, *e2;
int c;
e1 = (struct entry *)p1;
e2 = (struct entry *)p2;
if ((c = (ignore_case ? strcasecmp : strcmp)(e1->name, e2->name)) != 0)
return c;
if (e1->size != e2->size) {
if (e1->size > e2->size)
return 1;
else
return -1;
}
if (e1->crc != e2->crc)
return (int)e1->crc - (int)e2->crc;
return 0;
}
static int
entry_paranoia_checks(char *const name[2], const void *p1, const void *p2) {
const struct entry *e1, *e2;
int ret;
e1 = (struct entry *)p1;
e2 = (struct entry *)p2;
ret = 0;
if (ef_compare(name, e1, e2) != 0)
ret = 1;
if (e1->comp_method != e2->comp_method) {
if (verbose) {
if (header_done == 0) {
printf("--- %s\n+++ %s\n", name[0], name[1]);
header_done = 1;
}
printf("--- %s ", e1->name);
printf("method %u\n", e1->comp_method);
printf("+++ %s ", e1->name);
printf("method %u\n", e2->comp_method);
}
ret = 1;
}
if (comment_compare(e1->comment, e1->comment_length, e2->comment, e2->comment_length) != 0) {
if (verbose) {
if (header_done == 0) {
printf("--- %s\n+++ %s\n", name[0], name[1]);
header_done = 1;
}
printf("--- %s ", e1->name);
printf("comment %" PRIu32 "\n", e1->comment_length);
printf("+++ %s ", e1->name);
printf("comment %" PRIu32 "\n", e2->comment_length);
}
ret = 1;
}
return ret;
}
static void
entry_print(const void *p) {
const struct entry *e;
e = (struct entry *)p;
/* TODO PRId64 */
printf("%10lu %08x %s\n", (unsigned long)e->size, e->crc, e->name);
}
static int
test_file(zip_t *za, zip_uint64_t idx, const char *zipname, const char *filename, zip_uint64_t size, zip_uint32_t crc) {
zip_file_t *zf;
char buf[8192];
zip_uint64_t nsize;
zip_int64_t n;
zip_uint32_t ncrc;
if ((zf = zip_fopen_index(za, idx, 0)) == NULL) {
fprintf(stderr, "%s: %s: cannot open file %s (index %" PRIu64 "): %s\n", progname, zipname, filename, idx, zip_strerror(za));
return -1;
}
ncrc = (zip_uint32_t)crc32(0, NULL, 0);
nsize = 0;
while ((n = zip_fread(zf, buf, sizeof(buf))) > 0) {
nsize += (zip_uint64_t)n;
ncrc = (zip_uint32_t)crc32(ncrc, (const Bytef *)buf, (unsigned int)n);
}
if (n < 0) {
fprintf(stderr, "%s: %s: error reading file %s (index %" PRIu64 "): %s\n", progname, zipname, filename, idx, zip_file_strerror(zf));
zip_fclose(zf);
return -1;
}
zip_fclose(zf);
if (nsize != size) {
fprintf(stderr, "%s: %s: file %s (index %" PRIu64 "): unexpected length %" PRId64 " (should be %" PRId64 ")\n", progname, zipname, filename, idx, nsize, size);
return -2;
}
if (ncrc != crc) {
fprintf(stderr, "%s: %s: file %s (index %" PRIu64 "): unexpected length %x (should be %x)\n", progname, zipname, filename, idx, ncrc, crc);
return -2;
}
return 0;
}