-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepack.cpp
More file actions
1258 lines (1100 loc) · 36.9 KB
/
repack.cpp
File metadata and controls
1258 lines (1100 loc) · 36.9 KB
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
#include <algorithm>
#include <cassert>
#include <cerrno>
#include <cmath>
#include <cstdarg>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define DBUFFER_SIZE 8 * 1024 * 1024 // 8 MiB for now
void printErr(const char* format, ...)
{
va_list args;
va_start(args, format);
printf("-\033[1;31mE\033[1;0m ");
vprintf(format, args);
printf("\n");
va_end(args);
}
void printHelp(const char* m)
{
printf("Usage: %s <filename> [-e | -p]\n\t-e | --extract (default)\n\t-p | --pack\n\n", m);
}
void printUsageError(const char* m, const char* arg)
{
printErr("Unknown argument: \"%s\"", arg);
printHelp(m);
}
void pack(const char*);
/*
** ⠀⠀⠀⠀⢀⡴⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⠃⣀⠀⠀⣄⡀⠀⠀⠰⠆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⣦⡀⠙⢷⣄⠀⠀⠀⠀⠀⢀⣀⣤⣴⣶⣾⣿⣿⣿⡿⠿⠷⢶⣾⣦⣐⠶⠤⠄⣀⣲⡀⠀⠀⠀⠀⠀⢻⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
** ⠀⠀⠀⢀⡖⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡇⠀⡟⠀⠀⠈⢷⡀⠀⠀⠘⠶⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣿⣛⢶⣄⡙⢷⣤⣤⣴⣿⣿⣿⣿⣿⣿⣿⡟⠉⠀⠀⠀⠀⠈⣷⡈⠻⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
** ⠀⠀⣠⡟⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⣿⠇⢠⡇⠀⠀⠀⠈⣷⣄⠀⠀⠀⠘⢷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢻⡎⠛⢾⣿⣶⣿⣿⠟⠋⠙⠻⢿⡿⠿⠋⠀⠀⠀⠀⠀⠀⠀⣿⣷⣤⠈⠛⢧⣄⠀⠀⠀⠀⠀⠀⠀⠀⠘⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
** ⠀⢠⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣷⣾⠃⠀⠀⠀⡀⢹⣿⣦⡀⠀⠀⠈⢻⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⢿⡄⢨⣿⡟⠉⠿⢿⣷⣤⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⢃⡿⠀⠀⠀⠉⠷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠙⢷⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
** ⢠⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡼⢰⠀⠀⠻⣿⡿⡆⠀⠀⠀⡇⠀⢿⡏⠻⣆⠀⠀⠀⠹⣦⠀⠀⠀⠀⠀⠀⠀⠀⠈⣷⡈⠉⢿⡀⠀⠀⠈⠉⠉⠃⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⠋⣾⠃⠀⠀⠀⠀⠀⠙⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢷⣶⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
** ⣾⠃⠀⠀⠀⠀⠀⠀⠀⣶⡇⢸⡇⢸⡇⠀⠀⣻⣿⡖⠀⠀⢰⡇⠀⣸⣷⠀⠈⠳⣦⡀⠀⠈⢳⣄⠀⠀⠀⠀⠀⠀⠀⠘⢷⡀⠈⢷⣄⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡴⠟⠁⣿⠃⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠛⢯⡙⠳⣦⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀
** ⡟⠀⠀⠀⠀⠀⠀⠀⠠⣿⡇⠀⠇⠸⠇⠀⠀⢸⣿⣷⠀⠀⡿⠀⠀⢸⣿⡄⠀⠀⠈⠻⣦⣄⡀⠙⢷⣄⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⠙⠿⣿⣷⣶⣦⣤⣶⠶⠚⠛⠛⠉⠀⣠⡾⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣽⣦⡀⠙⠛⢶⣤⡀⠀⠀⠀⠀⠀
** ⠁⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠁⠀⠀⠀⢸⡏⢻⣇⣼⠃⠀⠀⠸⣿⡇⠀⣀⣤⣶⣤⣽⡿⠶⣤⣙⣷⣀⠀⠀⠀⠀⠀⠸⣧⠀⠀⠀⠈⠛⠛⠛⠛⠃⠀⠀⠀⣀⣤⠾⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢻⣿⣿⣦⡀⠀⠈⠙⠳⢦⡀⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡇⠀⠀⠀⠀⠀⠀⢸⡇⠠⣿⣇⠀⠀⠀⠀⣿⣣⣶⣿⣿⡿⠛⠿⣄⠀⠀⠉⠛⠿⢶⣄⠀⠀⠀⠀⠙⣧⣀⣀⣀⣀⣀⣀⣀⣤⣤⠶⠞⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⠻⣿⣿⣿⣦⣄⠀⠀⠀⠈⠒⠂
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠻⣿⠀⠀⠀⠀⠀⠀⣸⡇⠰⢿⣿⡄⠀⢀⣴⣿⣿⣿⡿⠋⠀⠀⠀⠙⢷⡄⠀⠀⠀⠀⠉⣿⠶⣤⣄⡀⠈⣯⡉⠉⠉⠉⠉⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣹⡃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⣿⣿⣿⣿⣷⣄⠀⠀⠀⠀
** ⠘⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⡀⠀⠀⠀⠀⠀⣾⡟⠁⠀⠻⣇⢰⣿⣿⣿⡿⠋⠀⠀⠀⠀⠀⠀⠈⣷⠀⠀⠀⠀⢰⡏⠀⠀⠈⠙⠛⠺⠷⣤⠀⠀⠀⢀⣄⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⣿⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣽⣿⣧⡝⠻⢦⣄⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⠀⠀⣠⣿⡇⠀⠀⠀⣿⣿⣿⣿⣿⠀⠀⠀⠀⠀⠀⠀⠀⢀⣿⡆⠀⠀⢀⡿⠀⠀⠀⠀⠀⠀⠀⠀⣀⣀⣀⣴⠟⠛⠋⠛⢷⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⠇⠙⢷⡄⠀⠀⠀⠀⠀⠀⠀⠠⣤⡀⠀⠀⠈⠙⠻⣧⡄⠀⠉⠓⠀
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡇⠀⢀⣴⠏⢸⡇⠀⢀⣾⣿⣿⣿⡏⢻⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⡇⠀⢀⣾⠁⠀⠀⠀⠀⠀⢠⣶⠟⠿⠿⠋⠁⠀⠀⠀⠀⠘⣿⣷⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡏⠀⠀⠀⠻⣦⡀⠀⠀⠀⠀⠀⠀⠘⢿⡄⠀⠀⠀⠀⠘⢿⣦⡀⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣴⠟⠁⠀⢈⣧⠀⣿⣿⣿⣿⡿⠀⣿⠀⠀⠀⠀⠀⠀⢀⣾⣿⣿⠇⠀⣾⠁⠀⠀⠀⠀⠀⣀⣼⠏⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⠟⠹⣷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠃⠀⠀⠀⠐⢹⣷⡴⠀⠀⠀⠀⠀⠀⠈⣿⡄⠀⠀⠀⠀⠈⠻⣍⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡴⠛⢧⡀⠀⠀⠈⣿⣿⣿⣿⣿⡿⠁⢠⡏⠀⠀⠀⠀⠀⣠⣾⣿⡿⠃⢀⣼⠃⠀⠀⢀⣾⣿⡿⠛⠉⠀⠀⠀⠀⠀⠀⠀⢀⣴⠟⠁⠀⢀⣼⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡏⠀⠀⠀⠀⠀⠀⠘⢻⣄⠀⠀⠀⠀⠀⠀⢼⣿⣄⠀⠀⠀⠀⠀⠙⣧⡀
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠀⠀⠘⢷⡀⠀⣴⣿⣿⠛⠋⠁⠀⠀⠸⠇⠀⠀⠀⠀⣸⣿⠟⠉⠀⣠⠞⠁⠀⠀⠀⠸⣿⣿⡄⠀⠀⠀⠀⠀⢀⣀⣤⡾⠛⠁⠀⣴⠟⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⣹⣦⠀⠀⠀⠀⠀⠀⣿⣿⢷⣄⠀⠀⠀⠀⠈⠻
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣄⣹⣿⣿⣷⠀⠀⠀⠀⠀⠀⠀⢀⣰⣿⠿⠋⠀⣠⡾⠋⠀⠀⠀⠀⠀⠀⢹⣟⣁⣤⡶⠶⠞⠛⠛⠉⠁⠀⠀⠀⢠⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠘⣷⡀⠀⠀⠀⠀⠀⢿⡄⠉⠀⠀⠀⠀⠀⠀
** ⠀⠀⠀⠀⠀⠀⢰⡄⠀⠀⠀⠀⠀⠀⠀⠀⠙⣿⡁⢹⠈⢷⣀⠀⣀⣀⣤⡶⠟⠋⠁⢀⣤⠾⠋⠀⠀⠀⠀⠀⠀⠀⠀⣾⣿⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡾⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⠙⠀⠀⢀⡀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢷⡀⠀⠀⠀⠀⠸⣇⠀⠀⠀⠀⠀⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⢿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣾⣇⠈⢿⣿⠟⠛⠁⠀⣠⣤⠾⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢻⣷⠀⠀⠀⠀⠀⠀⢀⣠⠶⠛⠛⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⡿⠷⠚⠛⠛⠛⠻⢷⣴⡀⠀⠀⠀⠀⠀⠀⠈⢻⡄⠀⠀⠀⠀⣿⠚⠛⠉⠀⠀⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⠘⣧⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠀⠀⠻⣆⠶⠞⠛⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠛⠶⠶⠶⢿⣤⠴⠟⠁⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⡴⠖⠛⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣶⡖⠀⢶⣦⠀⢀⣠⣴⠿⣆⠀⠀⠀⢿⡀⠀⠀⠀⠀⠀⠀
** ⠀⠀⠀⠀⠀⠀⠠⣤⡸⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⣾⢟⡁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⢿⣷⡿⠳⠾⠛⠉⠀⠀⠹⣆⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⠈⠙⢿⡆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢧⣄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⠟⠁⠁⠈⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠋⠀⠀⠀⠀⠀⠀⠀⠀⢻⡄⠀⠈⡇⠀⠀⠀⠀⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢻⡄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢿⣦⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⡿⠁⠀⠀⠀⠀⠀⠈⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⡀⠀⡇⠀⠀⠀⠀⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢷⡀⠀⠀⠀⠀⠀⠀⠀⢰⠀⠀⠀⠀⠀⠀⠀⢽⣧⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣴⠏⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠢⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⣿⢷⠀⡇⠀⠀⠀⠀⠀⠀
** ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢧⠀⠀⠀⠀⠀⠀⠀⠈⠃⠀⠀⠀⠀⠀⠀⠀⠘⢷⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣠⣾⠃⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⡀⠓⢀⣀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⠃⠘⣷⡇⠀⠀⠀⠀⠀⠀
**
**
** SOME BLACK MAGIC HAPPENING HERE **
**************************************/
size_t compress(const std::vector<char>& inputBuffer, std::vector<char>& outputBuffer)
{
uint32_t bitBuffer = 0;
uint32_t bitMask = 0x8000;
int searchWindow = 2047;
size_t size = inputBuffer.size() / sizeof(uint16_t);
const uint16_t* inp = reinterpret_cast<const uint16_t*>(inputBuffer.data());
outputBuffer.clear();
outputBuffer.resize(size * 2 + 2048);
uint16_t* outPtr = reinterpret_cast<uint16_t*>(outputBuffer.data());
const uint16_t* inPtr = inp;
const uint16_t* inEnd = inp + size;
uint16_t* flagWordPtr = outPtr++;
*flagWordPtr = 0;
while (inPtr < inEnd) {
if (bitMask == 0) {
flagWordPtr = outPtr++;
*flagWordPtr = 0;
bitMask = 0x8000;
}
const uint16_t* searchEnd = (inPtr - searchWindow) > inp ? (inPtr - searchWindow) : inp;
const uint16_t* bestMatch = nullptr;
uint32_t bestLength = 0;
const uint16_t* searchPtr = inPtr - 1;
while (searchPtr >= searchEnd) {
uint32_t length = 0;
while (searchPtr[length] == inPtr[length] && (inPtr + length) < inEnd && length < 11) {
length++;
}
if (length > bestLength) {
bestLength = length;
bestMatch = searchPtr;
}
searchPtr--;
}
if (bestLength >= 2) {
uint32_t offset = inPtr - bestMatch;
*outPtr++ = (bestLength << 11) | offset;
inPtr += bestLength;
*flagWordPtr |= bitMask;
} else {
*outPtr++ = *inPtr++;
}
bitMask >>= 1;
}
if (bitMask != 0x8000) {
*flagWordPtr |= bitMask;
}
size_t outputSize = (reinterpret_cast<char*>(outPtr) - outputBuffer.data());
size_t align = (outputSize / 2048) * 2048;
if(align == 0 || align < outputSize)
align += 2048;
outputBuffer.resize(align);
return align;
}
size_t decompress(int16_t* inputBuffer, int16_t* outputBuffer, size_t size) {
uint32_t bitBuffer = 0;
uint32_t bitMask = 0;
int16_t* inpPtr = inputBuffer;
int16_t* outPtr = outputBuffer;
int16_t* inpEnd = inputBuffer + size / sizeof(int16_t);
while (inpPtr < inpEnd) {
if (bitMask == 0) {
if (inpPtr >= inpEnd) break;
bitBuffer = *inpPtr;
bitMask = 0x8000;
inpPtr++;
}
if ((bitMask & bitBuffer) == 0) {
if (inpPtr >= inpEnd) break;
*outPtr = *inpPtr;
inpPtr++;
outPtr++;
} else {
if (inpPtr >= inpEnd) break;
uint16_t controlWord = *inpPtr;
inpPtr++;
uint32_t offset = controlWord & 0x7FF;
uint32_t length = controlWord >> 11; // 0xB
if (length == 0) {
if (inpPtr >= inpEnd) break;
length = *inpPtr;
inpPtr++;
}
if (offset == 0) {
if (length == 0) break;
std::memset(outPtr, 0, length * sizeof(int16_t));
outPtr += length;
} else {
int16_t* copySrc = outPtr - offset;
for (uint32_t i = 0; i < length; i++) {
*outPtr = *copySrc;
outPtr++;
copySrc++;
}
}
}
bitMask >>= 1;
}
return (outPtr - outputBuffer) * sizeof(int16_t);
}
enum fileType
{
unk = -1,
tim2,
ptx, // where offset is 2048 (0x800)
ipu,
icon_sys, // aka icon.sys file
ps2icn, // ps2 icon
momo
};
const char* fileTypeExt(fileType ft)
{
switch (ft) {
case momo: return "bin";
case tim2: return "tm2";
case ptx: return "ptx";
case ipu: return "ipu";
case icon_sys: return "icon.sys";
case ps2icn: return "icn";
default: return "unk";
}
}
fileType findFileType(std::vector<char>& f, size_t fsize)
{
char magic[4] = {f[0], f[1], f[2], f[3]};
char momoHeader[4] = {'M', 'O', 'M', 'O'};
char tim2Header[4] = {'T', 'I', 'M', '2'};
char ipumHeader[4] = {'i', 'p', 'u', 'm'};
char ps2dHeader[4] = {'P', 'S', '2', 'D'};
if((magic[2] == momoHeader[0] && magic[3] == momoHeader[1] && magic[0] != momoHeader[0]) || (magic[2] == tim2Header[0] && magic[3] == tim2Header[1]))
{
char tmp[4] = {f[2], f[3], f[4], f[5]};
if(memcmp(tmp, tim2Header, 4) == 0 || memcmp(tmp, momoHeader, 4) == 0)
{
std::vector<char> decompressed_data(DBUFFER_SIZE * sizeof(int16_t));
size_t decompressed_size = decompress(reinterpret_cast<int16_t*>(f.data()), reinterpret_cast<int16_t*>(decompressed_data.data()), fsize);
if (decompressed_size == 0) return unk;
decompressed_data.resize(decompressed_size);
f = std::move(decompressed_data);
return findFileType(f, decompressed_size);
}
}
else if(memcmp(magic, momoHeader, 4) == 0)
return momo;
else if(memcmp(magic, tim2Header, 4) == 0)
return tim2;
else if(memcmp(magic, ipumHeader, 4) == 0)
return ipu;
else if(memcmp(magic, ps2dHeader, 4) == 0)
return icon_sys;
else if(memcmp(magic, "\x00\x00\x01\x00", 4) == 0)
return ps2icn;
else{
if(fsize > (2048 + 512)){ // offset + some data stuff
char lm[4] = {f[2048], f[2049], f[2050], f[2051]};
if(memcmp(lm, tim2Header, 4) == 0)
return ptx;
else
{
auto findCompressedPtx = std::search(f.begin(), f.end(), std::begin(tim2Header), std::end(tim2Header));
if(findCompressedPtx != f.end())
{
std::vector<char> decompressed_data(DBUFFER_SIZE * sizeof(int16_t));
size_t decompressed_size = decompress(reinterpret_cast<int16_t*>(f.data()), reinterpret_cast<int16_t*>(decompressed_data.data()), decompressed_data.size());
if (decompressed_size == 0) return unk;
decompressed_data.resize(decompressed_size);
f = std::move(decompressed_data);
return findFileType(f, decompressed_size);
}
}
}
}
return unk;
}
fileType findFileType(const char* path)
{
std::ifstream f(path, std::ios::binary | std::ios::ate);
size_t fsize = f.tellg();
f.seekg(0, std::ios::beg);
std::vector<char> buff(fsize);
f.read(buff.data(), buff.size());
f.close();
return findFileType(buff, fsize);
}
struct ipumHeader
{
char magic[4];
uint32_t unk1;
uint32_t unk2;
uint32_t frameCount;
uint32_t framerate;
};
void ipumPack(std::string dirname, std::string metadataFile, std::string origPath)
{
// header aka .meta.your.ipu
// dds size
// dds data
// frame end aka frame0.meta
bool isc = false;
std::string basename, line;
std::filesystem::path _tp = dirname;
std::vector<std::string> files;
std::ifstream meta(metadataFile);
std::getline(meta, basename);
while(std::getline(meta, line))
{
if(line.find("_compressed") != std::string::npos)
{
isc = true;
continue;
}
if(line == "") break;
_tp = _tp.append(line);
files.push_back(_tp.string());
_tp = dirname;
}
meta.close();
std::string fn = origPath + ".bak";
if(!std::filesystem::exists(fn))
{
std::ifstream _fib(origPath, std::ios::binary);
std::ofstream _fob(fn, std::ios::binary);
_fob << _fib.rdbuf();
_fob.close();
_fib.close();
}
std::ostringstream f(std::ios::binary);
_tp = _tp.append(".meta." + basename);
std::ifstream ipuHeader(_tp.string(), std::ios::binary);
f << ipuHeader.rdbuf();
ipuHeader.close();
for(size_t i = 0; i < files.size(); i++)
{
printf("-- Writing \"%s\"\n", files[i].c_str());
std::ifstream _t(files[i], std::ios::binary | std::ios::ate);
size_t fsize = _t.tellg();
_t.seekg(0, std::ios::beg);
printf(" Size: %lu\n", fsize);
f << "frmj";
f.write(reinterpret_cast<char*>(&fsize), sizeof(uint32_t));
f << _t.rdbuf();
_t.close();
std::string ddsMeta = files[i].erase(files[i].size() - 3) + "meta";
_t.open(ddsMeta, std::ios::binary);
_t.seekg(0, std::ios::beg);
f.seekp(-4, std::ios::cur);
f << _t.rdbuf();
_t.close();
}
std::vector<char> buff;
std::string _buff = f.str();
buff.insert(buff.end(), _buff.begin(), _buff.end());
if(isc)
{
printf("-- Compressing file\n");
size_t _size = buff.size();
std::vector<char> out(_size / 2);
size_t csize = compress(buff, out);
out.resize(csize);
buff = std::move(out);
}
std::ofstream _f(origPath, std::ios::binary);
_f.write(buff.data(), buff.size());
_f.close();
}
void ipumUnpack(std::vector<char>& buffer, const char* dirname, std::filesystem::path basename, bool isc)
{
std::istringstream f(std::string(buffer.begin(), buffer.end()), std::ios::binary);
f.seekg(0, std::ios::beg);
ipumHeader ipu;
f.read(reinterpret_cast<char*>(&ipu), sizeof(ipu));
printf("-- Ipum frame count: %i\n", ipu.frameCount);
if(!std::filesystem::is_directory(dirname))
{
if(std::filesystem::create_directory(dirname))
{
printf("-- Directory \"%s\" created\n", dirname);
}
}
char* _metan = new char[strlen(dirname) + 12];
sprintf(_metan, "%s/.metadata", dirname);
std::vector<std::string> _meta;
_meta.push_back(basename.string());
if(isc)
_meta.push_back("_compressed");
printf("-- Creating \"%s/.metadata\" file\n", dirname);
std::ofstream _metadata(_metan, std::ios::out);
for(const auto e : _meta)
{
_metadata << e << '\n';
}
// sprintf(tc, "%s/.meta.%s", dirname, basename.string().c_str());
std::filesystem::path tc = dirname;
tc = tc.append(".meta." + basename.string());
std::ofstream metadata(tc, std::ios::binary);
metadata.write(reinterpret_cast<char*>(&ipu), sizeof(ipu));
metadata.close();
for(uint32_t i = 0; i < ipu.frameCount; i++)
{
char frameStart[4];
char frameEnd[4];
uint32_t tSize;
f.read(frameStart, 4);
f.read(reinterpret_cast<char*>(&tSize), 4);
char* texture = new char[tSize];
f.read(texture, tSize-4);
f.read(frameEnd, 4);
char* tn = new char[strlen(dirname) + sizeof(uint32_t) + 12];
sprintf(tn, "%s/frame%i.dds", dirname, i);
_metadata << "frame" << i << ".dds" << '\n';
char* tnm = new char[strlen(dirname) + sizeof(uint32_t) + 12 + 1];
sprintf(tnm, "%s/frame%i.meta", dirname, i);
printf("-- Writing texture to \"%s\"\n", tn);
std::ofstream dt(tnm, std::ios::binary);
dt.write(frameEnd, 4);
dt.close();
std::ofstream dds(tn, std::ios::binary);
dds.write(texture, tSize);
delete [] texture;
dds.close();
delete [] tnm;
delete [] tn;
}
delete [] _metan;
_metadata.close();
}
struct Tim2Header
{
char magic[4];
char formatVer;
char formatId;
uint16_t count;
// char pad[8];
// Maybe:
uint32_t texOff;
uint32_t texSize;
//
};
struct GsTex {
unsigned long TBP0 : 14;
unsigned long TBW : 6;
unsigned long PSM : 6;
unsigned long TW : 4;
unsigned long TH : 4;
unsigned long TCC : 1;
unsigned long TFX : 2;
unsigned long CBP : 14;
unsigned long CPSM : 4;
unsigned long CSM : 1;
unsigned long CSA : 5;
unsigned long CLD : 3;
};
struct Tim2PicHeader
{
uint32_t totalSize;
uint32_t clutSize;
uint32_t imgSize;
uint16_t headerSize;
uint16_t clutColors;
char picFormat;
char MipMapTextures;
char clutType;
char ImageType;
uint16_t ImageWidth;
uint16_t ImageHeight;
GsTex GsTex0;
GsTex GsTex1;
uint32_t GsTexaFbaPabe;
uint32_t GsTexClut;
};
void tim2Pack(std::string dirname, std::string metadataFile, std::string origPath)
{
std::ifstream meta(metadataFile);
std::string basename;
std::string _tmpc;
std::getline(meta, basename);
std::getline(meta, _tmpc);
meta.close();
bool isc = false;
if(_tmpc != "")
isc = true;
std::filesystem::path texFile = dirname;
texFile = texFile.append(basename + ".dds");
std::filesystem::path texMetaFile = dirname;
texMetaFile = texMetaFile.append(".meta." + basename);
std::vector<char> tim2;
std::ifstream _tim2(texMetaFile, std::ios::binary);
if(!_tim2.is_open())
{
printErr("Cannot open \"%s\"", texMetaFile.string().c_str());
exit(-1);
}
Tim2Header t2header;
Tim2PicHeader t2pic;
_tim2.seekg(0, std::ios::end);
size_t ts = _tim2.tellg();
_tim2.seekg(0, std::ios::beg);
_tim2.read(reinterpret_cast<char*>(&t2header), sizeof(t2header));
std::vector<char> addData;
std::vector<char> addData2;
if(t2header.formatId != 0x0)
{
addData.resize(128 - _tim2.tellg());
_tim2.read(addData.data(), addData.size());
}
_tim2.read(reinterpret_cast<char*>(&t2pic), sizeof(t2pic));
size_t s = _tim2.tellg();
if(ts > s)
{
addData2.resize(ts-s);
_tim2.read(addData2.data(), (ts - s));
}
size_t hS = _tim2.tellg();
_tim2.close();
std::ifstream _dds(texFile, std::ios::binary | std::ios::ate);
if(!_dds.is_open())
{
printErr("Cannot open \"%s\"", texFile.string().c_str());
exit(-1);
}
size_t ddsSize = _dds.tellg();
_dds.seekg(12, std::ios::beg);
uint32_t w, h;
_dds.read(reinterpret_cast<char*>(&w), sizeof(uint32_t));
_dds.read(reinterpret_cast<char*>(&h), sizeof(uint32_t));
_dds.seekg(0, std::ios::beg);
std::vector<char> ddsData(ddsSize);
_dds.read(ddsData.data(), ddsData.size());
/// :( don't works....
// GsTex tex = t2pic.GsTex0;
// t2header.texSize = ddsSize;
// t2pic.ImageWidth = w;
// t2pic.ImageHeight = h;
//
// tex.TW = log2(w);
// tex.TH = log2(h);
//
// t2pic.GsTex0 = tex;
t2pic.imgSize = ddsSize;
t2pic.totalSize = ddsSize + hS - t2pic.headerSize;
tim2.insert(tim2.end(), reinterpret_cast<char*>(&t2header), reinterpret_cast<char*>(&t2header) + sizeof(t2header));
if(t2header.formatId != 0x0)
{
tim2.insert(tim2.end(), addData.begin(), addData.end());
}
tim2.insert(tim2.end(), reinterpret_cast<char*>(&t2pic), reinterpret_cast<char*>(&t2pic) + sizeof(t2pic));
if(addData2.size() != 0)
tim2.insert(tim2.end(), addData2.begin(), addData2.end());
tim2.insert(tim2.end(), ddsData.begin(), ddsData.end());
_dds.close();
if(isc)
{
std::vector<char> out(tim2.size());
size_t csize = compress(tim2, out);
out.resize(csize);
tim2 = std::move(out);
}
std::string fn = origPath + ".bak";
if(!std::filesystem::exists(fn))
{
std::ifstream _fib(origPath, std::ios::binary);
std::ofstream _fob(fn, std::ios::binary);
_fob << _fib.rdbuf();
_fob.close();
_fib.close();
}
if(tim2.size() % 2048 != 0)
{
size_t old = tim2.size();
size_t newsize = (old + 2047) & ~2047;
tim2.resize(newsize);
}
std::ofstream f(origPath, std::ios::binary);
f.write(tim2.data(), tim2.size());
f.close();
}
void tim2Unpack(std::vector<char>& buffer, const char* dirname, std::filesystem::path basename, bool isc)
{
std::istringstream f(std::string(buffer.begin(), buffer.end()), std::ios::binary);
f.seekg(0, std::ios::beg);
Tim2Header t2header;
Tim2PicHeader t2pic;
f.read(reinterpret_cast<char*>(&t2header), sizeof(t2header));
if(t2header.formatId != 0x0)
f.seekg(0x80, std::ios::beg);
f.read(reinterpret_cast<char*>(&t2pic), sizeof(t2pic));
f.seekg(t2pic.headerSize - sizeof(t2pic), std::ios::cur);
size_t pos = f.tellg();
char ddsMagic[4] = {'D', 'D', 'S', ' '};
char* texture = new char[t2pic.imgSize];
f.read(texture, t2pic.imgSize);
if(memcmp(texture, ddsMagic, 4) != 0)
{
printErr("Supported only PC format of DMC2 textures");
exit(-1);
}
if(!std::filesystem::is_directory(dirname))
{
if(std::filesystem::create_directory(dirname))
{
printf("-- Directory \"%s\" created\n", dirname);
}
}
char* tn = new char[strlen(dirname) + strlen(basename.string().c_str()) + 6];
sprintf(tn, "%s/%s.dds", dirname, basename.string().c_str());
printf("-- Writing texture to \"%s\"\n", tn);
std::ofstream dds(tn, std::ios::binary);
dds.write(texture, t2pic.imgSize);
dds.close();
delete [] tn;
// for ex. item0.biz is compressed tim2, so... needed (also filename)
char* _metan = new char[strlen(dirname) + 12];
sprintf(_metan, "%s/.metadata", dirname);
std::vector<std::string> _meta;
_meta.push_back(basename.string());
if(isc)
_meta.push_back("_compressed");
printf("-- Creating \"%s/.metadata\" file\n", dirname);
std::ofstream _metadata(_metan, std::ios::out);
for(const auto e : _meta)
{
_metadata << e << '\n';
}
delete [] _metan;
_metadata.close();
// dump a header or data to texture point
// sprintf(tc, "%s/.meta.%s", dirname, basename.string().c_str());
std::filesystem::path tc = dirname;
tc = tc.append(".meta." + basename.string());
std::ofstream metadata(tc, std::ios::binary);
f.seekg(0, std::ios::beg);
char* _r = new char[pos];
f.read(_r, pos);
metadata.write(_r, pos);
delete [] _r;
metadata.close();
}
void ptxPack(std::string dirname, std::string metadataFile, std::string origPath)
{
bool isc = false;
std::string basename, line;
std::filesystem::path _tp = dirname;
std::vector<std::string> files;
std::ifstream meta(metadataFile);
std::getline(meta, basename);
while(std::getline(meta, line))
{
if(line.find("_compressed") != std::string::npos)
{
isc = true;
continue;
}
if(line == "") break;
std::filesystem::path p = _tp;
p.append("_" + line);
if(std::filesystem::is_directory(p))
pack(p.string().c_str());
_tp = _tp.append(line);
files.push_back(_tp.string());
_tp = dirname;
}
meta.close();
std::string fn = origPath + ".bak";
if(!std::filesystem::exists(fn))
{
std::ifstream _fib(origPath, std::ios::binary);
std::ofstream _fob(fn, std::ios::binary);
_fob << _fib.rdbuf();
_fob.close();
_fib.close();
}
std::ostringstream f(std::ios::binary);
std::vector<char> zeros(0x800, 0);
f.write(zeros.data(), zeros.size());
f.seekp(0, std::ios::beg);
uint32_t count = files.size();
f.write(reinterpret_cast<char*>(&count), sizeof(uint32_t));
f.seekp(0x800, std::ios::beg);
std::streampos p;
for(size_t i = 0; i < files.size(); i++)
{
printf("-- Writing \"%s\"\n", files[i].c_str());
std::ifstream _t(files[i], std::ios::binary | std::ios::ate);
size_t fsize = _t.tellg();
_t.seekg(0, std::ios::beg);
printf(" Size: %lu\n", fsize);
f << _t.rdbuf();
p = f.tellp();
f.seekp(i * sizeof(uint32_t) + sizeof(uint32_t), std::ios::beg);
uint32_t ptxSize = fsize / 2048;
f.write(reinterpret_cast<char*>(&ptxSize), sizeof(uint32_t));
f.seekp(p);
_t.close();
}
std::vector<char> buff;
std::string _buff = f.str();
buff.insert(buff.end(), _buff.begin(), _buff.end());
if(isc)
{
printf("-- Compressing file\n");
size_t _size = buff.size();
std::vector<char> out(_size / 2);
size_t csize = compress(buff, out);
out.resize(csize);
buff = std::move(out);
}
std::ofstream _f(origPath, std::ios::binary);
_f.write(buff.data(), buff.size());
_f.close();
}
void ptxUnpack(std::vector<char>& buffer, const char* dirname, std::filesystem::path basename, bool isc)
{
std::istringstream f(std::string(buffer.begin(), buffer.end()), std::ios::binary);
f.seekg(0, std::ios::beg);
uint32_t count;
f.read(reinterpret_cast<char*>(&count), sizeof(uint32_t));
std::vector<uint32_t> align; // tim2 size / 2048
for(size_t i = 0; i < count; i++)
{
uint32_t _tu;
f.read(reinterpret_cast<char*>(&_tu), sizeof(uint32_t));
align.push_back(_tu);
}
f.seekg(0x800, std::ios::beg);
if(!std::filesystem::is_directory(dirname))
{
if(std::filesystem::create_directory(dirname))
{
printf("-- Directory \"%s\" created\n", dirname);
}
}
// sprintf(tc, "%s/.metadata", dirname);
std::filesystem::path tc = dirname;
tc = tc.append(".metadata");
std::vector<std::string> metadataBuffer;
metadataBuffer.push_back(basename.string());
if(isc)
metadataBuffer.push_back("_compressed");
for(size_t i = 0; i < count; i++)
{
size_t s = align[i] * 2048;
char* ufn = new char[6 + strlen(dirname) + 1 + 3];
std::vector<char> buff(s);
// f.seekg(o, std::ios::beg);
f.read(buff.data(), buff.size());
fileType ft = findFileType(buff, buff.size());
sprintf(ufn, "%s/id%lu.%s", dirname, i, fileTypeExt(ft));
printf("-- Writing to \"%s\", size: %lu\n", ufn, s);
std::ofstream uf(ufn, std::ios::binary);
uf.write(buff.data(), s);
uf.close();
std::filesystem::path ap = std::filesystem::absolute(ufn);
std::filesystem::path bn = ap.filename();
std::filesystem::path dn = ap.parent_path() / ("_" + bn.string());
switch (ft) {
case tim2:
tim2Unpack(buff, dn.string().c_str(), bn, false);
break;
default:
break;
}
buff.clear();
std::filesystem::path _t = std::filesystem::absolute(ufn).filename();
metadataBuffer.push_back(_t.string());
delete [] ufn;
}
printf("-- Creating \"%s/.metadata\" file\n", dirname);
std::ofstream metadata(tc, std::ios::out);
for(const auto e : metadataBuffer)
{
metadata << e << '\n';
}
metadata.close();
}
struct blockM
{
uint64_t offset;
uint64_t size;
};
struct miniBlock
{
uint32_t offset;
uint32_t size;
};
void momoPack(std::string dirname, std::string metadataFile, std::string origPath)
{
bool mb = false;
std::ifstream _m(origPath, std::ios::binary);
char c;
_m.seekg(sizeof(uint32_t));
_m.read(&c, 1);
if(c == 'M' || c == 'O')
{
_m.seekg(sizeof(uint32_t) + 2);
_m.read(&c, 1);
}
if(c != 0x0)
mb = true;
_m.close();
bool isc = false;
std::ifstream meta(metadataFile);
std::string basename;
std::string line;
std::vector<std::string> files;
std::getline(meta, basename);
std::filesystem::path _tp = dirname;
while(std::getline(meta, line))
{
if(line.find("_compressed") != std::string::npos)
{
isc = true;
continue;
}
if(line == "") break;
std::filesystem::path p = _tp;
p.append("_" + line);
if(std::filesystem::is_directory(p))
pack(p.string().c_str());
_tp = _tp.append(line);
files.push_back(_tp.string());
_tp = dirname;
}
meta.close();
char magic[4] = {'M', 'O', 'M', 'O'};
std::string fn = origPath + ".bak";
if(!std::filesystem::exists(fn))
{
std::ifstream _fib(origPath, std::ios::binary);
std::ofstream _fob(fn, std::ios::binary);
_fob << _fib.rdbuf();
_fob.close();
_fib.close();
}
std::ostringstream f(std::ios::binary);
size_t lastpos = 0;
size_t lastoff = 0;
size_t fsize = 0;
size_t bcount = sizeof(uint64_t);
if(mb)
bcount = sizeof(uint32_t);
f.write(magic, 4);
if(!mb)
{
uint32_t q = 0;
f.write(reinterpret_cast<char*>(&q), sizeof(uint32_t));
}
size_t tmp = files.size();
f.write(reinterpret_cast<char*>(&tmp), bcount);
char* _tmp = new char[(bcount*2) * files.size() + (bcount*2)];
f.write(_tmp, (bcount*2) * files.size());
delete [] _tmp;
lastpos = f.tellp();
if(lastpos % 64 != 0)
{
size_t old = lastpos;
size_t newpos = (old + 63) & ~63;
std::string _tmpBuff(newpos - lastpos, '\0');
f.write(_tmpBuff.data(), _tmpBuff.size());
lastpos = newpos;
}
for(size_t i = 0; i < files.size(); i++)
{
printf("-- Writing \"%s\"\n", files[i].c_str());
std::ifstream _t(files[i], std::ios::binary | std::ios::ate);
lastoff = lastpos;
fsize = _t.tellg();
_t.seekg(0, std::ios::beg);
printf(" Size: %lu\n", fsize);
printf(" Offset: %lu\n", lastoff);
f << _t.rdbuf();
lastpos = f.tellp();
if(lastpos % 64 != 0)
{
size_t old = lastpos;
size_t newpos = (old + 63) & ~63;
std::string _tmpBuff(newpos - lastpos, '\0');
f.write(_tmpBuff.data(), _tmpBuff.size());
lastpos = newpos;
}
f.seekp((bcount*2) + (bcount * 2) * i);
f.write(reinterpret_cast<char*>(&lastoff), bcount);
f.write(reinterpret_cast<char*>(&fsize), bcount);
f.seekp(lastpos);
_t.close();
}
lastpos = f.tellp();
std::vector<char> buff;
std::string _buff = f.str();
buff.insert(buff.end(), _buff.begin(), _buff.end());
std::ofstream __f(origPath + ".bin", std::ios::binary);
__f.write(buff.data(), buff.size());
__f.close();
if(isc)
{
printf("-- Compressing file\n");
size_t _size = buff.size();
std::vector<char> out(_size / 2);
size_t csize = compress(buff, out);
out.resize(csize);
buff = std::move(out);
}
std::ofstream _f(origPath, std::ios::binary);
_f.write(buff.data(), buff.size());
_f.close();
}
void momoUnpack(std::vector<char>& buffer, const char* dirname, std::filesystem::path basename, bool isc)
{
std::istringstream f(std::string(buffer.begin(), buffer.end()), std::ios::binary);
std::vector<blockM> blocks;
std::vector<miniBlock> mBlocks;
size_t blocks_count;