-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.c
949 lines (905 loc) · 31.8 KB
/
a.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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
/* a.c (archiver) -- esl */
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <limits.h>
#include <errno.h>
#include <assert.h>
#include <wchar.h>
#include "b.h"
#include "a.h"
#include "r.h"
/* bar globals */
char g_cmd = 'h'; /* 't', 'x', 'c', or 'h' */
const char *g_arfile = NULL; /* archive file name */
const char *g_dstdir = NULL; /* destination dir or - for stdout */
const char *g_infile = NULL; /* included glob patterns file name */
const char *g_exfile = NULL; /* excluded glob patterns file name */
bool g_keepold = false; /* do not owerwrite existing files (-k) */
int g_integrity = 0; /* check/calc integrity hashes; 1: SHA256 */
int g_integerrc = 0; /* extraction integrity error count */
int g_format = 0; /* 'b': BSAR, 'a': ASAR, 0: check extension */
dsbuf_t g_inpats; /* list of included patterns */
dsbuf_t g_expats; /* list of excluded patterns */
dsbuf_t g_unpats; /* list of unpacked patterns */
size_t g_bufsize = 0x400000;
char *g_buffer = NULL;
void init_archiver(void)
{
dsbinit(&g_inpats);
dsbinit(&g_expats);
dsbinit(&g_unpats);
g_buffer = emalloc(g_bufsize);
}
void fini_archiver(void)
{
dsbfini(&g_inpats);
dsbfini(&g_expats);
dsbfini(&g_unpats);
free(g_buffer);
}
#define PAT_LITERAL 1
#define PAT_ANCHORED 2
#define PAT_WCMATCHSLASH 4
void addpat(dsbuf_t *pdsb, const char *arg, int flags)
{
cbuf_t cb = mkcb(); char *pat;
size_t len = strlen(arg);
if (len > 0 && arg[len-1] == '/') --len;
cbset(&cb, arg, len);
cbinsc(&cb, 0, '0'+flags);
pat = cbdata(&cb);
dsbpushbk(pdsb, &pat);
cbfini(&cb);
}
void loadpats(dsbuf_t *pdsb, const char *fname, int flags)
{
FILE *fp = fopen(fname, "r");
cbuf_t cb = mkcb(); char *line;
if (!fp) eprintf("can't open excluded patterns file %s:", g_exfile);
while ((line = fgetlb(&cb, fp)) != NULL) {
line = strtrim(line);
if (*line == 0 || *line == '#') continue;
addpat(&g_expats, line, flags);
}
fclose(fp);
cbfini(&cb);
}
bool matchpats(const char *path, const char *fname, dsbuf_t *pdsb)
{
size_t i;
for (i = 0; i < dsblen(pdsb); ++i) {
dstr_t *pds = dsbref(pdsb, i), pat = *pds;
int flags = *pat++ - '0'; bool res;
if (flags & PAT_LITERAL) res = streql(path, pat);
else if (flags & PAT_ANCHORED) res = gmatch(path, pat);
else if (strprf(pat, "./")) res = gmatch(path, pat+2);
else if (strchr(pat, '/')) res = gmatch(path, pat);
else res = gmatch(fname, pat);
if (res) return true;
}
return false;
}
/* file/directory entry */
fdent_t* fdeinit(fdent_t* mem)
{
memset(mem, 0, sizeof(fdent_t));
fdebinit(&mem->files);
dsinit(&mem->name);
dsinit(&mem->integrity_hash);
dsbinit(&mem->integrity_blocks);
return mem;
}
void fdefini(fdent_t* pe)
{
fdebfini(&pe->files);
dsfini(&pe->name);
dsfini(&pe->integrity_hash);
dsbfini(&pe->integrity_blocks);
}
fdebuf_t* fdebinit(fdebuf_t* mem)
{
bufinit(mem, sizeof(fdent_t));
return mem;
}
void fdebfini(fdebuf_t* pb)
{
size_t i;
for (i = 0; i < buflen(pb); ++i) fdefini(bufref(pb, i));
buffini(pb);
}
void pack_uint32_le(uint32_t v, uint8_t buf[4])
{
buf[0] = v & 0xff; v >>= 8;
buf[1] = v & 0xff; v >>= 8;
buf[2] = v & 0xff; v >>= 8;
buf[3] = v & 0xff;
}
uint32_t unpack_uint32_le(uint8_t buf[4])
{
return buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
}
void parse_header_files_json(JFILE *jfp, const char *base, fdebuf_t *pfdb)
{
cbuf_t kcb = mkcb(), ncb = mkcb();
jfgetobrc(jfp);
while (!jfatcbrc(jfp)) {
fdent_t *pfde = fdebnewbk(pfdb);
jfgetkey(jfp, &ncb); /* name: */
vverbosef("%s/%s\n", base, cbdata(&ncb));
pfde->name = estrdup(cbdata(&ncb));
jfgetobrc(jfp);
while (!jfatcbrc(jfp)) {
char *key = jfgetkey(jfp, &kcb);
if (streql(key, "files")) {
char *nbase = cbsetf(&kcb, "%s/%s", base, cbdata(&ncb));
pfde->isdir = true;
parse_header_files_json(jfp, nbase, &pfde->files);
} else if (streql(key, "offset")) {
/* NB: asar string (js exact num range is 53 bits) */
char *soff = jfgetstr(jfp, &kcb);
pfde->offset = strtoull(soff, NULL, 10);
} else if (streql(key, "size")) {
pfde->size = jfgetnumull(jfp);
} else if (streql(key, "executable")) {
pfde->executable = jfgetbool(jfp);
} else if (streql(key, "unpacked")) {
pfde->unpacked = jfgetbool(jfp);
} else if (streql(key, "integrity")) {
jfgetobrc(jfp);
while (!jfatcbrc(jfp)) {
key = jfgetkey(jfp, &kcb);
if (streql(key, "algorithm")) {
char *ia = jfgetstr(jfp, &kcb);
pfde->integrity_algorithm = streql(ia, "SHA256");
} else if (streql(key, "hash")) {
char *hash = jfgetbin(jfp, &kcb);
if (pfde->integrity_algorithm == 1 && cblen(&kcb) == SHA256DG_SIZE)
pfde->integrity_hash = ememdup(hash, SHA256DG_SIZE);
} else if (streql(key, "blockSize")) {
pfde->integrity_block_size = (unsigned long)jfgetnumull(jfp);
} else if (streql(key, "blocks")) {
jfgetobrk(jfp);
while (!jfatcbrk(jfp)) {
char *block = jfgetstr(jfp, &kcb);
if (pfde->integrity_algorithm == 1 && cblen(&kcb) == SHA256DG_SIZE) {
*dsbnewbk(&pfde->integrity_blocks) = ememdup(block, SHA256DG_SIZE);
}
}
jfgetcbrk(jfp);
}
}
jfgetcbrc(jfp);
} else {
eprintf("%s: invalid entry: %s", g_arfile, cbdata(&kcb));
}
}
if (!pfde->isdir) {
vverbosef(" offset = 0x%.8lx (%ld)\n",
(unsigned long)pfde->offset, (unsigned long)pfde->offset);
vverbosef(" size = 0x%.8lx (%ld)\n",
(unsigned long)pfde->size, (unsigned long)pfde->size);
vverbosef(" executable = %d\n", (int)pfde->executable);
vverbosef(" unpacked = %d\n", (int)pfde->unpacked);
}
jfgetcbrc(jfp);
}
jfgetcbrc(jfp);
cbfini(&kcb), cbfini(&ncb);
}
void parse_header_files_bson(BFILE *bfp, const char *base, fdebuf_t *pfdb)
{
cbuf_t kcb = mkcb(), ncb = mkcb();
bfgetobrc(bfp);
while (!bfatcbrc(bfp)) {
fdent_t *pfde = fdebnewbk(pfdb);
bfgetkey(bfp, &ncb); /* name: */
vverbosef("%s/%s\n", base, cbdata(&ncb));
pfde->name = estrdup(cbdata(&ncb));
bfgetobrc(bfp);
while (!bfatcbrc(bfp)) {
char *key = bfgetkey(bfp, &kcb);
if (streql(key, "files")) {
char *nbase = cbsetf(&kcb, "%s/%s", base, cbdata(&ncb));
pfde->isdir = true;
parse_header_files_bson(bfp, nbase, &pfde->files);
} else if (streql(key, "offset")) {
pfde->offset = bfgetnumull(bfp);
} else if (streql(key, "size")) {
pfde->size = bfgetnumull(bfp);
} else if (streql(key, "executable")) {
pfde->executable = bfgetbool(bfp);
} else if (streql(key, "unpacked")) {
pfde->unpacked = bfgetbool(bfp);
} else if (streql(key, "integrity")) {
int integrity = 0;
bfgetobrc(bfp);
while (!bfatcbrc(bfp)) {
key = bfgetkey(bfp, &kcb);
if (streql(key, "algorithm")) {
integrity = bfgetnum(bfp);
if (integrity == 1) pfde->integrity_algorithm = 1; /* SHA256 */
} else if (streql(key, "hash")) {
char *hash = bfgetbin(bfp, &kcb);
if (integrity == 1 && cblen(&kcb) == SHA256DG_SIZE)
pfde->integrity_hash = ememdup(hash, SHA256DG_SIZE);
} else if (streql(key, "blockSize")) {
unsigned long n = (unsigned long)bfgetnumull(bfp);
if (integrity == 1) pfde->integrity_block_size = n;
} else if (streql(key, "blocks")) {
bfgetobrk(bfp);
while (!bfatcbrk(bfp)) {
char *block = bfgetbin(bfp, &kcb);
if (integrity == 1 && cblen(&kcb) == SHA256DG_SIZE) {
*dsbnewbk(&pfde->integrity_blocks) = ememdup(block, SHA256DG_SIZE);
}
}
bfgetcbrk(bfp);
}
}
bfgetcbrc(bfp);
} else {
eprintf("%s: invalid entry: %s", g_arfile, cbdata(&kcb));
}
}
if (!pfde->isdir) {
vverbosef(" offset = 0x%.8lx (%ld)\n",
(unsigned long)pfde->offset, (unsigned long)pfde->offset);
vverbosef(" size = 0x%.8lx (%ld)\n",
(unsigned long)pfde->size, (unsigned long)pfde->size);
vverbosef(" executable = %d\n", (int)pfde->executable);
vverbosef(" unpacked = %d\n", (int)pfde->unpacked);
}
bfgetcbrc(bfp);
}
bfgetcbrc(bfp);
cbfini(&kcb), cbfini(&ncb);
}
uint32_t read_header(FILE *fp, fdebuf_t *pfdb)
{
uint8_t hbuf[16]; uint32_t psz, off, asz, ssz;
int format = 0; /* 'a': asar, 'b': bsar */
/* read header data from fp */
if (fread(hbuf, 4, 1, fp) != 1) goto err;
psz = unpack_uint32_le(hbuf);
if (psz == 3) format = 'b';
else if (psz == 4) format = 'a';
else eprintf("%s: invalid archive header", g_arfile);
if (format == 'a') { /* asar, 4-word signature */
JFILE *jfp; uint32_t x;
cbuf_t kcb = mkcb();
if (fread(hbuf+4, 12, 1, fp) != 1) goto err;
off = unpack_uint32_le(hbuf+4);
asz = unpack_uint32_le(hbuf+8);
ssz = unpack_uint32_le(hbuf+12);
if (ssz < 12) eprintf("%s: invalid asar archive header [3]", g_arfile);
x = ssz + 4; if (x % 4 > 0) x += 4 - (x % 4); /* align to 32 bit */
if (x != asz) eprintf("%s: invalid asar archive header [2]", g_arfile);
x += 4;
if (x != off) eprintf("%s: invalid asar archive header [1]", g_arfile);
off += 12; /* from the start of the file */
/* header starts right after 4-word signature */
jfp = newjfii(FILE_pii, fp);
jfgetobrc(jfp);
jfgetkey(jfp, &kcb); /* "files": */
if (!streql(cbdata(&kcb), "files")) eprintf("%s: invalid asar file list", g_arfile);
parse_header_files_json(jfp, "", pfdb);
jfgetcbrc(jfp);
freejf(jfp);
cbfini(&kcb);
} else { /* bsar, 3-word signature */
BFILE *bfp; cbuf_t kcb = mkcb();
if (fread(hbuf+4, 8, 1, fp) != 1) goto err;
off = unpack_uint32_le(hbuf+4);
asz = unpack_uint32_le(hbuf+8);
ssz = 0;
if (asz != off) eprintf("%s: invalid bsar archive header [1]", g_arfile);
off += 12; /* from the start of the file */
/* header starts right after 3-word signature */
bfp = newbfii(FILE_pii, fp);
bfgetobrc(bfp);
bfgetkey(bfp, &kcb); /* "files": */
if (!streql(cbdata(&kcb), "files")) eprintf("%s: invalid bsar file list", g_arfile);
parse_header_files_bson(bfp, "", pfdb);
bfgetcbrc(bfp);
freebf(bfp);
cbfini(&kcb);
}
return off;
err:
eprintf("%s: can't read archive header", g_arfile);
return 0;
}
void unparse_header_files_json(JFILE *jfp, fdebuf_t *pfdb)
{
size_t i; cbuf_t cb = mkcb();
jfputobrc(jfp);
for (i = 0; i < fdeblen(pfdb); ++i) {
fdent_t *pfde = fdebref(pfdb, i);
if (pfde->name) jfputkey(jfp, pfde->name);
jfputobrc(jfp);
if (pfde->isdir) {
if (pfde->unpacked) {
jfputkey(jfp, "unpacked");
jfputbool(jfp, true);
}
jfputkey(jfp, "files");
unparse_header_files_json(jfp, &pfde->files);
} else {
jfputkey(jfp, "size");
jfputnumull(jfp, pfde->size);
if (pfde->unpacked) {
jfputkey(jfp, "unpacked");
jfputbool(jfp, true);
} else {
jfputkey(jfp, "offset");
/* NB: asar string (js exact num range is 53 bits) */
jfputstr(jfp, cbsetf(&cb, "%llu", pfde->offset));
}
if (pfde->executable) {
jfputkey(jfp, "executable");
jfputbool(jfp, true);
}
if (pfde->integrity_algorithm == 1/*SHA256*/) {
jfputkey(jfp, "integrity");
jfputobrc(jfp);
jfputkey(jfp, "algorithm");
jfputstr(jfp, "SHA256");
if (pfde->integrity_hash) {
jfputkey(jfp, "hash");
jfputbin(jfp, pfde->integrity_hash, SHA256DG_SIZE);
}
if (pfde->integrity_block_size) {
size_t k;
jfputkey(jfp, "blockSize");
jfputnumull(jfp, pfde->integrity_block_size);
jfputkey(jfp, "blocks");
jfputobrk(jfp);
for (k = 0; k < dsblen(&pfde->integrity_blocks); ++k) {
dstr_t *pds = dsbref(&pfde->integrity_blocks, k);
jfputbin(jfp, *pds, SHA256DG_SIZE);
}
jfputcbrk(jfp);
}
jfputcbrc(jfp);
}
}
jfputcbrc(jfp);
}
jfputcbrc(jfp);
cbfini(&cb);
}
void unparse_header_files_bson(BFILE *bfp, fdebuf_t *pfdb)
{
size_t i; cbuf_t cb = mkcb();
bfputobrc(bfp);
for (i = 0; i < fdeblen(pfdb); ++i) {
fdent_t *pfde = fdebref(pfdb, i);
if (pfde->name) bfputkey(bfp, pfde->name);
bfputobrc(bfp);
if (pfde->isdir) {
if (pfde->unpacked) {
bfputkey(bfp, "unpacked");
bfputbool(bfp, true);
}
bfputkey(bfp, "files");
unparse_header_files_bson(bfp, &pfde->files);
} else {
bfputkey(bfp, "size");
bfputnumull(bfp, pfde->size);
if (pfde->unpacked) {
bfputkey(bfp, "unpacked");
bfputbool(bfp, true);
} else {
bfputkey(bfp, "offset");
bfputnumull(bfp, pfde->offset);
}
if (pfde->executable) {
bfputkey(bfp, "executable");
bfputbool(bfp, true);
}
if (pfde->integrity_algorithm == 1/*SHA256*/) {
bfputkey(bfp, "integrity");
bfputobrc(bfp);
bfputkey(bfp, "algorithm");
bfputnum(bfp, pfde->integrity_algorithm);
if (pfde->integrity_hash) {
bfputkey(bfp, "hash");
bfputbin(bfp, pfde->integrity_hash, SHA256DG_SIZE);
}
if (pfde->integrity_block_size) {
size_t k;
bfputkey(bfp, "blockSize");
bfputnumull(bfp, pfde->integrity_block_size);
bfputkey(bfp, "blocks");
bfputobrk(bfp);
for (k = 0; k < dsblen(&pfde->integrity_blocks); ++k) {
dstr_t *pds = dsbref(&pfde->integrity_blocks, k);
bfputbin(bfp, *pds, SHA256DG_SIZE);
}
bfputcbrk(bfp);
}
bfputcbrc(bfp);
}
}
bfputcbrc(bfp);
}
bfputcbrc(bfp);
cbfini(&cb);
}
void write_header(int format, fdebuf_t *pfdb, FILE *fp)
{
uint8_t hbuf[16]; uint32_t psz, off, asz, ssz;
cbuf_t hcb = mkcb();
/* serialize header data to hcb */
if (format == 'a') {
JFILE *jfp = newjfoi(cbuf_poi, &hcb);
jfputobrc(jfp);
jfputkey(jfp, "files");
unparse_header_files_json(jfp, pfdb);
jfputcbrc(jfp);
freejf(jfp);
} else {
BFILE *bfp = newbfoi(cbuf_poi, &hcb);
bfputobrc(bfp);
bfputkey(bfp, "files");
unparse_header_files_bson(bfp, pfdb);
bfputcbrc(bfp);
freebf(bfp);
}
/* write header data to fp */
ssz = (uint32_t)cblen(&hcb);
if (format == 'a') { /* asar, 4-word signature */
psz = 4;
asz = ssz + 4; /* add at least 4 bytes */
if (asz % 4 > 0) asz += 4 - (asz % 4); /* align to 32 bit */
off = sizeof(ssz) + asz; /* offset from the end of asz */
pack_uint32_le(psz, hbuf);
pack_uint32_le(off, hbuf+4);
pack_uint32_le(asz, hbuf+8);
pack_uint32_le(ssz, hbuf+12);
} else { /* bsar, 3-word signature */
psz = 3;
asz = ssz; /* no extra padding */
if (asz % 4 > 0) asz += 4 - (asz % 4); /* align to 32 bit */
off = asz; /* offset from the end of asz */
pack_uint32_le(psz, hbuf);
pack_uint32_le(off, hbuf+4);
pack_uint32_le(asz, hbuf+8);
}
if (fwrite(hbuf, psz*4, 1, fp) != 1) goto err;
if (fwrite(cbdata(&hcb), ssz, 1, fp) != 1) goto err;
if (asz > ssz) {
memset(hbuf, 0, 16);
if (fwrite(hbuf, asz-ssz, 1, fp) != 1) goto err;
}
cbfini(&hcb);
return;
err:
eprintf("%s: can't write archive header", g_arfile);
}
void list_files(const char *base, fdebuf_t *pfdb, dsbuf_t *ppats, bool full, FILE *pf)
{
size_t i; cbuf_t cb = mkcb();
for (i = 0; i < fdeblen(pfdb); ++i) {
fdent_t *pfde = fdebref(pfdb, i);
dsbuf_t *ppatsi = ppats; const char *sbase;
if (!base) sbase = pfde->name;
else sbase = cbsetf(&cb, "%s/%s", base, pfde->name);
if (matchpats(sbase, pfde->name, &g_expats)) continue;
/* ppats == NULL means list this one and everything below */
if (ppatsi && matchpats(sbase, pfde->name, ppatsi)) ppatsi = NULL;
if (pfde->isdir) {
if (!ppatsi) {
if (full) {
fprintf(pf, "d--%c ", pfde->unpacked ? 'u' : '-');
fprintf(pf, " ");
}
if (!base) fprintf(pf, "%s/\n", pfde->name);
else fprintf(pf, "%s/%s/\n", base, pfde->name);
}
list_files(sbase, &pfde->files, ppatsi, full, pf);
} else {
if (!ppatsi) {
if (full) {
fprintf(pf, "-%c%c%c ", pfde->integrity_hash ? 'i' : '-',
pfde->executable ? 'x' : '-', pfde->unpacked ? 'u' : '-');
if (pfde->unpacked) fprintf(pf, " %12lu ", (unsigned long)pfde->size);
else fprintf(pf, "@%-12lu %12lu ", (unsigned long)pfde->offset, (unsigned long)pfde->size);
}
if (!base) fprintf(pf, "%s\n", pfde->name);
else fprintf(pf, "%s/%s\n", base, pfde->name);
}
}
}
cbfini(&cb);
}
void list(int argc, char **argv)
{
FILE *fp; uint32_t hsz;
fdebuf_t fdeb; fdebinit(&fdeb);
while (argc-- > 0) addpat(&g_inpats, *argv++, PAT_LITERAL);
if (!(fp = fopen(g_arfile, "rb"))) eprintf("can't open archive file %s:", g_arfile);
hsz = read_header(fp, &fdeb);
list_files(NULL, &fdeb, dsbempty(&g_inpats) ? NULL : &g_inpats, getverbosity()>0, stdout);
fdebfini(&fdeb);
fclose(fp);
}
/* copy file via fread/fwrite */
size_t copy_file(FILE *ifp, FILE *ofp)
{
size_t bc = 0;
assert(ifp); assert(ofp);
for (;;) {
size_t n = fread(g_buffer, 1, g_bufsize, ifp);
if (!n) break;
fwrite(g_buffer, 1, n, ofp);
bc += n;
if (n < g_bufsize) break;
}
return bc;
}
void write_file(const char *path, fdent_t *pfde, FILE *ofp)
{
cbuf_t cb = mkcb(); FILE *ifp;
sha256ctx_t fhash, bhash;
uint8_t digest[SHA256DG_SIZE];
size_t bc = 0;
if ((ifp = fopen(path, "rb")) == NULL) {
eprintf("%s: cannot open file:", path);
}
if (g_integrity == 1) {
pfde->integrity_algorithm = g_integrity;
pfde->integrity_block_size = (uint32_t)g_bufsize;
sha256init(&fhash);
}
for (;;) {
size_t n = fread(g_buffer, 1, g_bufsize, ifp);
if (g_integrity == 1) {
sha256init(&bhash);
sha256update(&bhash, g_buffer, n);
sha256fini(&bhash, digest);
*dsbnewbk(&pfde->integrity_blocks) = ememdup((char*)&digest[0], SHA256DG_SIZE);
sha256update(&fhash, g_buffer, n);
}
if (!n) break;
fwrite(g_buffer, 1, n, ofp);
bc += n;
if (n < g_bufsize) break;
}
if (bc != pfde->size) {
eprintf("%s: actual file size (%llu) is different from stat file size (%llu)",
(unsigned long long)bc, (unsigned long long)pfde->size);
}
if (g_integrity == 1) {
sha256fini(&fhash, digest);
pfde->integrity_hash = ememdup((char*)&digest[0], SHA256DG_SIZE);
}
fclose(ifp);
cbfini(&cb);
}
uint64_t create_files(uint64_t off, const char *base, const char *path, fdebuf_t *pfdeb, FILE *ofp)
{
fsstat_t st;
if (fsstat(path, &st) && (st.isdir || st.isreg)) {
char *fname; fdent_t *pfde;
fname = getfname(path);
if (matchpats(base, fname, &g_expats)) return off;
pfde = fdebnewbk(pfdeb);
pfde->name = estrdup(fname);
pfde->isdir = st.isdir;
pfde->size = st.size;
if (matchpats(base, fname, &g_unpats)) {
pfde->unpacked = true;
} else {
if (pfde->isdir) {
cbuf_t cbb = mkcb(), cbp = mkcb();
dsbuf_t dsb; dsbinit(&dsb);
if (dir(path, &dsb)) {
size_t i;
for (i = 0; i < dsblen(&dsb); ++i) {
dstr_t *pds = dsbref(&dsb, i); char *nb, *np;
if (streql(*pds, ".") || streql(*pds, "..")) continue;
nb = *base ? cbsetf(&cbb, "%s/%s", base, *pds) : *pds;
np = cbsetf(&cbp, "%s/%s", path, *pds);
off = create_files(off, nb, np, &pfde->files, ofp);
}
} else {
eprintf("can't open directory: %s", path);
}
dsbfini(&dsb);
cbfini(&cbb), cbfini(&cbp);
} else {
pfde->offset = off;
off += pfde->size;
write_file(path, pfde, ofp);
}
}
} else {
eprintf("can't stat file or directory: %s", path);
}
return off;
}
void create(int argc, char **argv)
{
FILE *fp, *tfp; fdebuf_t fdeb;
int i, format; uint64_t off = 0;
if (!(fp = fopen(g_arfile, "wb"))) eprintf("can't open archive file %s:", g_arfile);
format = g_format ? g_format : strsuf(g_arfile, ".asar") ? 'a' : 'b';
tfp = etmpopen("w+b");
fdebinit(&fdeb);
for (i = 0; i < argc; ++i) {
/* NB: we don't care where file/dir arg is located */
off = create_files(off, getfname(argv[i]), argv[i], &fdeb, tfp);
}
list_files(NULL, &fdeb, NULL, getverbosity()>0, stdout);
write_header(format, &fdeb, fp);
rewind(tfp);
copy_file(tfp, fp);
fclose(tfp);
fclose(fp);
fdebfini(&fdeb);
}
/* copy file via fread/fwrite */
size_t copy_file_n(FILE *ifp, FILE *ofp, size_t bytec, fdent_t *pfde)
{
sha256ctx_t fhash, bhash;
uint8_t digest[SHA256DG_SIZE];
size_t bc = 0, ibidx = 0;
bool ckint = false;
assert(ifp); assert(ofp);
if (g_integrity == 1 && pfde->integrity_algorithm == 1) {
sha256init(&fhash);
ckint = true;
}
while (bytec > 0) {
size_t c = (bytec < g_bufsize) ? bytec : g_bufsize;
size_t n = fread(g_buffer, 1, c, ifp);
if (!n) break;
if (ckint) {
sha256init(&bhash);
sha256update(&bhash, g_buffer, n);
sha256fini(&bhash, digest);
if (ibidx < dsblen(&pfde->integrity_blocks)) {
if (memcmp(*dsbref(&pfde->integrity_blocks, ibidx), &digest[0], SHA256DG_SIZE) != 0) {
logef("Warning: %s: integrity info does not match member block: %s [%d]\n", g_arfile, pfde->name, (int)ibidx);
}
}
++ibidx;
sha256update(&fhash, g_buffer, n);
}
fwrite(g_buffer, 1, n, ofp);
bc += n;
bytec -= c;
}
if (ckint) {
sha256fini(&fhash, digest);
if (memcmp(pfde->integrity_hash, &digest[0], SHA256DG_SIZE) != 0) {
logef("Warning: %s: integrity info does not match member contents: %s\n", g_arfile, pfde->name);
g_integerrc += 1;
} else {
if (getverbosity() > 1) logef("**** integrity checks passed\n");
}
}
return bc;
}
void extract_files(const char *base, uint32_t hsz, fdebuf_t *pfdb, dsbuf_t *ppats, FILE *fp)
{
size_t i; cbuf_t cb = mkcb();
for (i = 0; i < fdeblen(pfdb); ++i) {
fdent_t *pfde = fdebref(pfdb, i);
dsbuf_t *ppatsi = ppats; const char *sbase;
if (!base) sbase = pfde->name;
else sbase = cbsetf(&cb, "%s/%s", base, pfde->name);
if (matchpats(sbase, pfde->name, &g_expats)) continue;
/* ppats == NULL means extract this one and everything below */
if (ppatsi && matchpats(sbase, pfde->name, ppatsi)) ppatsi = NULL;
if (pfde->isdir) {
extract_files(sbase, hsz, &pfde->files, ppatsi, fp);
} else if (!ppatsi && !pfde->unpacked) {
size_t n = 0, fsz = (size_t)pfde->size;
long long pos = (long long)hsz + (long long)pfde->offset;
if (getverbosity() > 0) {
logef("-%c%c%c ", pfde->integrity_hash ? 'i' : '-',
pfde->executable ? 'x' : '-', pfde->unpacked ? 'u' : '-');
if (pfde->unpacked) logef(" %12lu ", (unsigned long)pfde->size);
else logef("@%-12lu %12lu ", (unsigned long)pfde->offset, (unsigned long)pfde->size);
}
logef("%s\n", sbase);
if (fseekll(fp, pos, SEEK_SET) != 0) eprintf("%s: seek failed", g_arfile);
if (streql(g_dstdir, "-")) {
n = copy_file_n(fp, stdout, fsz, pfde);
} else {
cbuf_t fcb = mkcb(), dcb = mkcb();
char *dstdir = trimdirsep(cbsets(&dcb, g_dstdir));
char *dpath = (streql(dstdir, ".")) ?
cbsets(&fcb, sbase) : cbsetf(&fcb, "%s%c%s", dstdir, dirsep, sbase);
char *ddir = cbset(&dcb, dpath, spanfdir(dpath));
FILE *ofp;
if (!direxists(ddir)) {
vverbosef("%s: creating missing directories on path: %s\n", progname(), ddir);
emkdirp(ddir);
}
if (fexists(dpath) && g_keepold) {
logef("%s: keeping existing file in -k mode: %s\n", progname(), dpath);
continue;
}
ofp = fopen(dpath, "wb");
if (!ofp) {
eprintf("%s: can't open file for writing:", dpath);
} else {
n = copy_file_n(fp, ofp, fsz, pfde);
fclose(ofp);
}
cbfini(&fcb), cbfini(&dcb);
}
if (n != fsz) eprintf("%s: unexpected end of archive", g_arfile);
}
}
cbfini(&cb);
}
void extract(int argc, char **argv)
{
FILE *fp; uint32_t hsz;
fdebuf_t fdeb; fdebinit(&fdeb);
while (argc-- > 0) addpat(&g_inpats, *argv++, PAT_LITERAL);
if (!(fp = fopen(g_arfile, "rb"))) eprintf("can't open archive file %s:", g_arfile);
hsz = read_header(fp, &fdeb);
extract_files(NULL, hsz, &fdeb, dsbempty(&g_inpats) ? NULL : &g_inpats, fp);
fdebfini(&fdeb);
fclose(fp);
if (g_integerrc > 0) {
logef("%s: %d member(s) had integrity problems\n", g_arfile, (int)g_integerrc);
exit(1);
}
}
int main(int argc, char **argv)
{
int opt;
int patflags = 0;
setu8cp();
init_archiver();
setprogname(argv[0]);
setusage
("[OPTION]... [FILE/DIR]...\n"
"The archiver works with .asar (json header) and .bsar (bson header) archives.\n"
"\n"
"Examples:\n"
" bar -cf arch.bsar foo bar # Create bsar archive from files foo and bar\n"
" bar -cf arch.asar foo bar # Create asar archive from files foo and bar\n"
" bar -tvf arch.bsar # List all files in arch.bsar verbosely\n"
" bar -xf arch.bsar foo bar # Extract files foo and bar from arch.bsar\n"
" bar -xf arch.bsar # Extract all files from arch.bsar\n"
"\n"
"If a long option shows an argument as mandatory, then it is mandatory\n"
"for the equivalent short option also. Similarly for optional arguments.\n"
"\n"
"Main operation mode:\n"
" -c, --create Create a new archive\n"
" -t, --list List the contents of an archive\n"
" -x, --extract Extract files from an archive\n"
"\n"
"Operation modifiers:\n"
" -f, --file=FILE Use archive FILE (required in all modes)\n"
" -k, --keep-old-files Don't overwrite existing files when extracting\n"
" -C, --directory=DIR Use directory DIR for extracted files\n"
" -O, --to-stdout Extract files to standard output\n"
" -X, --exclude-from=FILE Exclude files via globbing patterns in FILE\n"
" --exclude=\"PATTERN\" Exclude files, given as a globbing PATTERN\n"
" --unpack=\"PATTERN\" Exclude files, but keep their info in archive\n"
" --include-from=FILE List/extract files via globbing patterns in FILE\n"
" --include=\"PATTERN\" List/extract files, given as a globbing PATTERN\n"
" --integrity=SHA256 Calculate or check file integrity info\n"
"\n"
"Archive format selection:\n"
" -o, --format=asar Create asar archive even if extension is not .asar\n"
" --format=bsar Create bsar archive even if extension is .asar\n"
"\n"
"File name matching options:\n"
" --anchored Patterns match path\n"
" --no-anchored Patterns match file/directory name\n"
" --wildcards Patterns are wildcards\n"
" --no-wildcards Patterns match verbatim\n"
"\n"
"Informative output:\n"
" -v, --verbose Increase output verbosity\n"
" -q, --quiet Suppress logging\n"
" -h, --help Print this help, then exit\n"
"\n"
"Note: when creating archives (-c), only the name of each argument file/dir\n"
"is stored in the archive, not a complete path to the argument file/dir.\n");
if (argc >= 3 && *argv[1] != '-') {
/* traditional tar-like usage: cmd archive ... */
char *cmd = argv[1];
bool gotf = false, gotmode = false;
while (*cmd) switch (*cmd++) {
case 'c': g_cmd = 'c'; gotmode = true; break;
case 't': g_cmd = 't'; gotmode = true; break;
case 'x': g_cmd = 'x'; gotmode = true; break;
case 'f': gotf = true; break;
case 'o': g_format = 'a'; break;
case 'k': g_keepold = true; break;
case 'v': incverbosity(); break;
default: eusage("unexpected flag in command combo: %c", *--cmd);
}
if (!gotf || !gotmode) eusage("invalid command combo: %s", argv[1]);
/* next argument is archive name */
g_arfile = argv[2];
/* continue parsing options/args from argv[3] */
eoptind = 3;
}
while ((opt = egetopt(argc, argv, "ctxf:kC:OX:owvqh-:")) != EOF) {
switch (opt) {
case 'c': g_cmd = 'c'; break;
case 't': g_cmd = 't'; break;
case 'x': g_cmd = 'x'; break;
case 'f': g_arfile = eoptarg; break;
case 'k': g_keepold = true; break;
case 'C': g_dstdir = eoptarg; break;
case 'O': g_dstdir = "-"; break;
case 'X': g_exfile = eoptarg; break;
case 'o': g_format = 'a'; break;
case 'w': setwlevel(3); break;
case 'v': incverbosity(); break;
case 'q': incquietness(); break;
case 'h': g_cmd = 'h'; break;
case '-': {
char *arg;
if (streql(eoptarg, "create")) g_cmd = 'c';
else if (streql(eoptarg, "list")) g_cmd = 't';
else if (streql(eoptarg, "extract")) g_cmd = 'x';
else if ((arg = strprf(eoptarg, "file=")) != NULL) g_arfile = arg;
else if (streql(eoptarg, "keep-old-files")) g_keepold = true;
else if ((arg = strprf(eoptarg, "directory=")) != NULL) g_dstdir = arg;
else if ((arg = strprf(eoptarg, "exclude-from=")) != NULL) g_exfile = arg;
else if ((arg = strprf(eoptarg, "exclude=")) != NULL) addpat(&g_expats, arg, patflags);
else if ((arg = strprf(eoptarg, "unpack=")) != NULL) addpat(&g_unpats, arg, patflags);
else if ((arg = strprf(eoptarg, "include-from=")) != NULL) g_infile = arg;
else if ((arg = strprf(eoptarg, "include=")) != NULL) addpat(&g_inpats, arg, patflags);
else if (streql(eoptarg, "anchored")) patflags |= PAT_ANCHORED;
else if (streql(eoptarg, "no-anchored")) patflags &= ~PAT_ANCHORED;
else if (streql(eoptarg, "wildcards")) patflags &= ~PAT_LITERAL;
else if (streql(eoptarg, "no-wildcards")) patflags |= PAT_LITERAL;
else if (streql(eoptarg, "verbose")) incverbosity();
else if (streql(eoptarg, "quiet")) incquietness();
else if (streql(eoptarg, "help")) g_cmd = 'h';
else if (streql(eoptarg, "integrity=SHA256")) g_integrity = 1;
else if (streql(eoptarg, "integrity")) g_integrity = 1;
else if (streql(eoptarg, "old-archive")) g_format = 'a';
else if (streql(eoptarg, "format=asar")) g_format = 'a';
else if (streql(eoptarg, "format=bsar")) g_format = 'b';
else eusage("illegal option: --%s", eoptarg);
} break;
}
}
if (streql(g_dstdir, "-")) fbinary(stdout);
if (g_exfile) loadpats(&g_expats, g_exfile, patflags);
if (g_infile) loadpats(&g_inpats, g_infile, patflags);
switch (g_cmd) {
case 't': {
if (!g_arfile) eusage("-f FILE argument is missing");
if (g_dstdir) eusage("unexpected -C/-O options in list mode");
list(argc-eoptind, argv+eoptind);
} break;
case 'c': {
if (!g_arfile) eusage("-f FILE argument is missing");
if (g_dstdir) eusage("unexpected -C/-O options in create mode");
if (!dsbempty(&g_inpats)) eusage("unexpected include options in create mode");
create(argc-eoptind, argv+eoptind);
} break;
case 'x': {
if (!g_arfile) eusage("-f FILE argument is missing");
if (!g_dstdir) g_dstdir = ".";
extract(argc-eoptind, argv+eoptind);
} break;
case 'h': {
eusage("BAR (Basic Archiver) 1.00 built on " __DATE__);
} break;
}
fini_archiver();
return EXIT_SUCCESS;
}