-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha.c
1227 lines (1178 loc) · 41.9 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
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* 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 "z.h"
/* baz 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_compression = 0; /* use compression; 1: DEFLATE */
int g_comprerrc = 0; /* extraction compression error count */
int g_zopfli_i = 0; /* 0 or # of zopfli iterations */
int g_format = 0; /* 'b': BSAR, 'a': ASAR, 'c': C dump, 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 if (streql(key, "compression")) {
jfgetobrc(jfp);
while (!jfatcbrc(jfp)) {
key = jfgetkey(jfp, &kcb);
if (streql(key, "algorithm")) {
char *ia = jfgetstr(jfp, &kcb);
pfde->compression_algorithm = streql(ia, "DEFLATE");
} else if (streql(key, "originalSize")) {
pfde->compression_original_size = jfgetnumull(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 if (streql(key, "compression")) {
int compression = 0;
bfgetobrc(bfp);
while (!bfatcbrc(bfp)) {
key = bfgetkey(bfp, &kcb);
if (streql(key, "algorithm")) {
compression = bfgetnum(bfp);
if (compression == 1) pfde->compression_algorithm = 1; /* DEFLATE */
} else if (streql(key, "originalSize")) {
uint64_t n = bfgetnumull(bfp);
if (compression == 1) pfde->compression_original_size = n;
}
}
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);
}
if (pfde->compression_algorithm == 1/*DEFLATE*/) {
jfputkey(jfp, "compression");
jfputobrc(jfp);
jfputkey(jfp, "algorithm");
jfputstr(jfp, "DEFLATE");
jfputkey(jfp, "originalSize");
jfputnumull(jfp, pfde->compression_original_size);
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);
}
if (pfde->compression_algorithm == 1/*DEFLATE*/) {
bfputkey(bfp, "compression");
bfputobrc(bfp);
bfputkey(bfp, "algorithm");
bfputnum(bfp, pfde->compression_algorithm);
bfputkey(bfp, "originalSize");
bfputnumull(bfp, pfde->compression_original_size);
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' : pfde->compression_algorithm ? 'z' : '-');
if (pfde->compression_algorithm) {
fprintf(pf, "#%-12lu %12lu ",
(unsigned long)pfde->size,
(unsigned long)pfde->compression_original_size);
} else {
fprintf(pf, " %12lu ",
(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;
size_t dsize; char *data = NULL;
char *wptr = NULL, *eptr = NULL;
sha256ctx_t fhash, bhash;
uint8_t digest[SHA256DG_SIZE];
size_t bc = 0;
if (g_compression == 1) {
if (pfde->size < SIZE_MAX && (data = malloc((size_t)pfde->size)) != NULL) {
dsize = (size_t)pfde->size; wptr = data;
eptr = data + dsize;
} else {
logef("Warning: %s: too big for compression; will be written as-is\n", path);
}
}
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;
if (data) {
if (wptr + n > eptr)
eprintf("%s: actual file size (%lu) is different from stat file size (%lu)",
path, (unsigned long long)bc, (unsigned long)dsize);
memcpy(wptr, g_buffer, n);
wptr += n;
} else {
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)",
path, (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);
if (data) {
size_t dlen = g_bufsize, slen = wptr-data;
if (g_zopfli_i > 0) {
cbsetf(&cb, "zopfli -c --deflate --i%d ", g_zopfli_i);
cbputarg(path, &cb);
logef("%s\n", cbdata(&cb));
ifp = epopen(cbdata(&cb), "rb");
dlen = fread(g_buffer, 1, dlen, ifp);
if (dlen >= g_bufsize) eprintf("%s: compressed data too large", path);
epclose(ifp);
} else {
int err = zdeflate((uint8_t*)g_buffer, &dlen, (uint8_t*)data, &slen, 9);
if (err) eprintf("%s: compression error (%d)", path, err);
}
fwrite(g_buffer, 1, dlen, ofp);
pfde->compression_algorithm = 1;
pfde->compression_original_size = pfde->size;
pfde->size = dlen;
free(data);
}
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;
write_file(path, pfde, ofp);
off += pfde->size;
}
}
} else {
eprintf("can't stat file or directory: %s", path);
}
return off;
}
void dump_files(const char *bname, const char *base, fdebuf_t *pfdb, dsbuf_t *ppats, FILE *ifp, FILE *ofp, buf_t *phb)
{
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) {
dump_files(bname, sbase, &pfde->files, ppatsi, ifp, ofp, phb);
} else {
if (!ppatsi && !pfde->unpacked) {
size_t n, fsz = (size_t)pfde->size, j, idx;
long long pos = (long long)pfde->offset;
cbuf_t cbp = mkcb(); uint64_t *phe;
char *path = base
? cbsetf(&cbp, "%s/%s", base, pfde->name)
: cbsetf(&cbp, "%s", pfde->name);
idx = buflen(phb);
if (pfde->size > g_bufsize) eprintf("member too big: %s", path);
if (fseekll(ifp, pos, SEEK_SET) != 0) eprintf("%s: seek failed", path);
n = fread(g_buffer, 1, fsz, ifp);
if (n != fsz) eprintf("%s: read failed", path);
if (pfde->compression_algorithm == 1) {
fprintf(ofp, "/* %s (DEFLATEd, org. size %lu) */\n", path,
(unsigned long)pfde->compression_original_size);
} else {
fprintf(ofp, "/* %s */\n", path);
}
fprintf(ofp, "static unsigned char file_%s_%d[%lu] =\n", bname, (int)idx, (unsigned long)fsz);
fprintf(ofp, " \"");
for (j = 0; j < n; ++j) {
fprintf(ofp, "\\%o", g_buffer[j] & 0xFF);
if (j > 0 && j % 32 == 0) fprintf(ofp, "\"\n \"");
}
fprintf(ofp, "\";\n\n");
phe = bufnewbk(phb);
phe[0] = (uint64_t)estrdup(path);
phe[1] = fsz;
phe[2] = pfde->compression_algorithm;
phe[3] = pfde->compression_original_size;
phe[4] = idx;
cbfini(&cbp);
}
}
}
cbfini(&cb);
}
static int he_cmp(const void *p1, const void *p2)
{
const uint64_t *phe1 = p1, *phe2 = p2;
const dstr_t name1 = (dstr_t)phe1[0], name2 = (dstr_t)phe2[0];
return strcmp(name1, name2);
}
static char *baz_dump_start = "/* start of in-memory archive */";
static char *baz_dump_end = "/* end of in-memory archive */";
void dump(FILE *ifp, fdebuf_t *pfdeb, FILE *ofp)
{
cbuf_t cb = mkcb(), ncb = mkcb();
char *aname = getfname(g_arfile);
char *bname = cbset(&ncb, aname, spanfbase(aname));
buf_t hb = mkbuf(sizeof(uint64_t)*5);
size_t i;
fprintf(ofp, "%s\n\n", baz_dump_start);
dump_files(bname, NULL, pfdeb, dsbempty(&g_inpats) ? NULL : &g_inpats, ifp, ofp, &hb);
fprintf(ofp, "/* %s directory (sorted by path) */\n", bname);
bufqsort(&hb, &he_cmp);
fprintf(ofp, "struct memdir directory_%s[%d] = {\n", bname, (int)buflen(&hb));
for (i = 0; i < buflen(&hb); ++i) {
uint64_t *phe = bufref(&hb, i);
dstr_t name = (dstr_t)phe[0];
size_t fsz = (size_t)phe[1];
int alg = (int)phe[2];
size_t osz = (size_t)phe[3];
int idx = (int)phe[4];
fprintf(ofp, " { \"%s\", %lu, %d, %lu, &file_%s_%d[0] },\n",
name, (unsigned long)fsz, alg, (unsigned long)osz, bname, idx);
free(name);
}
fprintf(ofp, "};\n\n");
fprintf(ofp, "%s\n", baz_dump_end);
cbfini(&cb), cbfini(&ncb);
}
void read_prologue_epilogue(FILE *fp, cbuf_t *pcbp, cbuf_t *pcbe)
{
cbuf_t lcb = mkcb();
char *line; int state = 0;
while ((line = fgetlb(&lcb, fp)) != NULL) {
switch (state) {
case 0: {
if (streql(line, baz_dump_start)) state = 1;
else cbputf(pcbp, "%s\n", line);
} break;
case 1: {
if (streql(line, baz_dump_end)) state = 2;
} break;
case 2: {
cbputf(pcbe, "%s\n", line);
} break;
}
}
cbfini(&lcb);
}
void create(int argc, char **argv)
{
FILE *fp, *tfp; fdebuf_t fdeb;
int i, format; uint64_t off = 0;
cbuf_t cbp = mkcb(), cbe = mkcb();
format = g_format;
if (!format && strsuf(g_arfile, ".asar")) format = 'a';
if (!format && strsuf(g_arfile, ".bsar")) format = 'b';
if (!format && strsuf(g_arfile, ".h")) format = 'c';
if (!format && strsuf(g_arfile, ".c")) format = 'c';
if (!format) format = 'b';
if (format == 'c' && (fp = fopen(g_arfile, "r")) != NULL) {
logef("%s exists; replacing dump section...\n", g_arfile);
read_prologue_epilogue(fp, &cbp, &cbe);
fclose(fp);
}
if (!(fp = fopen(g_arfile, format == 'c' ? "w" : "wb")))
eprintf("can't open archive file %s:", g_arfile);
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);
}
rewind(tfp);
list_files(NULL, &fdeb, NULL, getverbosity()>0, stdout);
if (format == 'c') {
fputs(cbdata(&cbp), fp);
dump(tfp, &fdeb, fp);
fputs(cbdata(&cbe), fp);
} else {
write_header(format, &fdeb, fp);
copy_file(tfp, fp);
}
fclose(tfp);
fclose(fp);
fdebfini(&fdeb);
cbfini(&cbp), cbfini(&cbe);
}
/* extract file via fread/fwrite; return # of bytes read from ifp */
size_t extract_file(FILE *ifp, FILE *ofp, fdent_t *pfde)
{
size_t dsize, csize; char *data = NULL;
char *rptr = NULL, *eptr = NULL;
sha256ctx_t fhash, bhash;
uint8_t digest[SHA256DG_SIZE];
size_t bc = 0, ibidx = 0;
bool ckint = false;
size_t bytec;
assert(ifp); assert(ofp);
if (pfde->compression_algorithm == 1) {
const char *name = pfde->name;
csize = (size_t)pfde->size;
dsize = (size_t)pfde->compression_original_size;
if (pfde->compression_original_size <= g_bufsize && (data = malloc(dsize)) != NULL) {
size_t n = fread(g_buffer, 1, csize, ifp);
int err; size_t dlen = dsize, slen = csize;
if (n != pfde->size) eprintf("%s: unexpected eof in %s\n", g_arfile, name);
err = zinflate((uint8_t*)data, &dlen, (uint8_t*)g_buffer, &slen);
if (err) eprintf("%s: decompression error in %s (%d)\n", g_arfile, name, err);
if (dlen != dsize) eprintf("%s: decompression error in %s\n", g_arfile, name);
rptr = data;
eptr = data + dsize;
} else {
eprintf("%s: %s is too big for decompression\n", g_arfile, name);
}
bytec = dsize;
} else {
bytec = (size_t)pfde->size;
}
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;
if (rptr) {
n = (rptr + c > eptr) ? eptr-rptr : c;
memcpy(g_buffer, rptr, n);
rptr += n;
} else {
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 # of bytes read from ifp */
if (data) { free(data); return csize; }
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' :
pfde->compression_algorithm ? 'z' : '-');
if (pfde->compression_algorithm) {
logef("#%-12lu %12lu ",
(unsigned long)pfde->size,
(unsigned long)pfde->compression_original_size);