-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakePureDOSDAT.cpp
More file actions
1757 lines (1608 loc) · 79.1 KB
/
MakePureDOSDAT.cpp
File metadata and controls
1757 lines (1608 loc) · 79.1 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
//--------------------------------------------//
// MakePureDOSDAT //
// License: Public Domain (www.unlicense.org) //
//--------------------------------------------//
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <vector>
typedef unsigned char Bit8u;
typedef unsigned short Bit16u;
typedef signed short Bit16s;
typedef unsigned int Bit32u;
typedef signed int Bit32s;
#if defined(_MSC_VER)
typedef unsigned __int64 Bit64u;
#else
typedef unsigned long long Bit64u;
#endif
#if defined (_MSC_VER)
#define strcasecmp(a,b) stricmp(a,b)
#define strncasecmp(a,b,n) _strnicmp(a,b,n)
#endif
// Use 64-bit fseek and ftell
#if defined(_MSC_VER) && _MSC_VER >= 1400 // VC2005 and up have a special 64-bit fseek
#define fseek_wrap(fp, offset, whence) _fseeki64(fp, (__int64)offset, whence)
#define ftell_wrap(fp) _ftelli64(fp)
#elif defined(HAVE_64BIT_OFFSETS) || (defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE - 0) >= 200112) || (defined(__POSIX_VISIBLE) && __POSIX_VISIBLE >= 200112) || (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112) || __USE_LARGEFILE || (defined(_FILE_OFFSET_BITS) && _FILE_OFFSET_BITS == 64)
#define fseek_wrap(fp, offset, whence) fseeko(fp, (off_t)offset, whence)
#define ftell_wrap(fp) ftello(fp)
#else
#define fseek_wrap(fp, offset, whence) fseek(fp, (long)offset, whence)
#define ftell_wrap(fp) ftell(fp)
#endif
static Bit32u CRC32(const void* data, size_t data_size)
{
// A compact CCITT crc16 and crc32 C implementation that balances processor cache usage against speed
// By Karl Malbrain - http://www.geocities.ws/malbrain/
static const Bit32u s_crc32[16] = { 0, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c, 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c };
Bit32u crcu32 = (Bit32u)~(Bit32u)0;
for (Bit8u b, *p = (Bit8u*)data;data_size--;) { b = *p++; crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b & 0xF)]; crcu32 = (crcu32 >> 4) ^ s_crc32[(crcu32 & 0xF) ^ (b >> 4)]; }
return ~crcu32;
}
#if 0
static void MD5(const void* data, size_t data_size, Bit8u res[16])
{
// BASED ON MD5 (public domain)
// By Bryce Wilson - https://github.com/Zunawe/md5-c
static const Bit8u PADDING[64] = { 0x80, 0 };
struct MD5_CTX
{
Bit32u a, b, c, d;
void Step(const Bit32u* block)
{
static const Bit32u S[64] = {7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21 };
static const Bit32u K[64] = {0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0xf61e2562, 0xc040b340, 0x265e5a51, 0xe9b6c7aa, 0xd62f105d, 0x02441453, 0xd8a1e681, 0xe7d3fbc8, 0x21e1cde6, 0xc33707d6, 0xf4d50d87, 0x455a14ed, 0xa9e3e905, 0xfcefa3f8, 0x676f02d9, 0x8d2a4c8a, 0xfffa3942, 0x8771f681, 0x6d9d6122, 0xfde5380c, 0xa4beea44, 0x4bdecfa9, 0xf6bb4b60, 0xbebfbc70, 0x289b7ec6, 0xeaa127fa, 0xd4ef3085, 0x04881d05, 0xd9d4d039, 0xe6db99e5, 0x1fa27cf8, 0xc4ac5665, 0xf4292244, 0x432aff97, 0xab9423a7, 0xfc93a039, 0x655b59c3, 0x8f0ccc92, 0xffeff47d, 0x85845dd1, 0x6fa87e4f, 0xfe2ce6e0, 0xa3014314, 0x4e0811a1, 0xf7537e82, 0xbd3af235, 0x2ad7d2bb, 0xeb86d391 };
Bit32u AA = a, BB = b, CC = c, DD = d, E;
for (Bit32u i = 0, j; i != 64; i++)
{
switch (i / 16)
{
case 0: E = ((BB & CC) | (~BB & DD)); j = i; break;
case 1: E = ((BB & DD) | (CC & ~DD)); j = ((i * 5) + 1) % 16; break;
case 2: E = (BB ^ CC ^ DD); j = ((i * 3) + 5) % 16; break;
default: E = (CC ^ (BB | ~DD)); j = (i * 7) % 16; break;
}
Bit32u temp = DD, x = AA + E + K[i] + block[j], n = S[i];
DD = CC; CC = BB; BB = BB + ((x << n) | (x >> (32 - n))); AA = temp;
}
a += AA; b += BB; c += CC; d += DD;
}
void Update(Bit8u* tmp, size_t offset, const Bit8u* data, size_t len)
{
for (const Bit8u* end = data + len; data != end; data++)
{
tmp[offset++] = *data;
if (!(offset % 64 == 0)) continue;
Bit32u block[16];
for (Bit32u j = 0; j != 16; j++) block[j] = ((Bit32u)tmp[j*4+3]<<24) | ((Bit32u)tmp[j*4+2]<<16) | ((Bit32u)tmp[j*4+1]<<8) | (Bit32u)tmp[j*4];
Step(block);
offset = 0;
}
}
} ctx = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
Bit8u tmp[64]; Bit32u last[16], offset = (data_size % 64);
ctx.Update(tmp, 0, (const Bit8u *)data, data_size);
ctx.Update(tmp, offset, PADDING, (offset < 56 ? 56 - offset : (56 + 64) - offset));
for (Bit32u j = 0; j != 14; j++) last[j] = ((Bit32u)tmp[j*4+3]<<24) | ((Bit32u)tmp[j*4+2]<<16) | ((Bit32u)tmp[j*4+1]<<8) | (Bit32u)tmp[j*4];
last[14] = (Bit32u)(data_size * 8); last[15] = (Bit32u)(((Bit64u)data_size * 8) >> 32);
ctx.Step(last);
for (Bit32u i = 0; i != 16; i++) res[i] = (Bit8u)((((Bit32u*)&ctx.a)[i/4] >> ((i%4)*8)) & 0x000000FF);
}
#endif
static void FastMD5(const void* data, size_t data_size, Bit8u res[16])
{
// BASED ON MD5 (public domain)
// By Galen Guyer - https://github.com/galenguyer/md5
struct MD5_CTX
{
Bit32u A, B, C, D;
const void* Body(const void *data, size_t size)
{
const Bit8u *ptr = (const Bit8u*)data;
Bit32u a = A, b = B, c = C, d = D;
do
{
Bit32u saved_a = a, saved_b = b, saved_c = c, saved_d = d;
#define STEP(f, a, b, c, d, x, t, s) (a) += f((b), (c), (d)) + (x) + (t); (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); (a) += (b);
#if defined(__i386__) || _M_IX86 || defined(__x86_64__) || _M_AMD64 || defined(__vax__)
#define SET(n) (*(Bit32u *)&ptr[(n) * 4])
#define GET(n) SET(n)
#else
Bit32u block[16];
#define SET(n) (block[(n)] = (Bit32u)ptr[(n) * 4] | ((Bit32u)ptr[(n) * 4 + 1] << 8) | ((Bit32u)ptr[(n) * 4 + 2] << 16) | ((Bit32u)ptr[(n) * 4 + 3] << 24))
#define GET(n) (block[(n)])
#endif
#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z))))
#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y))))
#define H(x, y, z) (((x) ^ (y)) ^ (z))
#define J(x, y, z) ((x) ^ ((y) ^ (z)))
#define I(x, y, z) ((y) ^ ((x) | ~(z)))
STEP(F, a, b, c, d, SET( 0), 0xd76aa478, 7) STEP(F, d, a, b, c, SET( 1), 0xe8c7b756, 12) STEP(F, c, d, a, b, SET( 2), 0x242070db, 17) STEP(F, b, c, d, a, SET( 3), 0xc1bdceee, 22)
STEP(F, a, b, c, d, SET( 4), 0xf57c0faf, 7) STEP(F, d, a, b, c, SET( 5), 0x4787c62a, 12) STEP(F, c, d, a, b, SET( 6), 0xa8304613, 17) STEP(F, b, c, d, a, SET( 7), 0xfd469501, 22)
STEP(F, a, b, c, d, SET( 8), 0x698098d8, 7) STEP(F, d, a, b, c, SET( 9), 0x8b44f7af, 12) STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22)
STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) STEP(F, b, c, d, a, SET(15), 0x49b40821, 22)
STEP(G, a, b, c, d, GET( 1), 0xf61e2562, 5) STEP(G, d, a, b, c, GET( 6), 0xc040b340, 9) STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) STEP(G, b, c, d, a, GET( 0), 0xe9b6c7aa, 20)
STEP(G, a, b, c, d, GET( 5), 0xd62f105d, 5) STEP(G, d, a, b, c, GET(10), 0x02441453, 9) STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) STEP(G, b, c, d, a, GET( 4), 0xe7d3fbc8, 20)
STEP(G, a, b, c, d, GET( 9), 0x21e1cde6, 5) STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) STEP(G, c, d, a, b, GET( 3), 0xf4d50d87, 14) STEP(G, b, c, d, a, GET( 8), 0x455a14ed, 20)
STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) STEP(G, d, a, b, c, GET( 2), 0xfcefa3f8, 9) STEP(G, c, d, a, b, GET( 7), 0x676f02d9, 14) STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20)
STEP(H, a, b, c, d, GET( 5), 0xfffa3942, 4) STEP(J, d, a, b, c, GET( 8), 0x8771f681, 11) STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) STEP(J, b, c, d, a, GET(14), 0xfde5380c, 23)
STEP(H, a, b, c, d, GET( 1), 0xa4beea44, 4) STEP(J, d, a, b, c, GET( 4), 0x4bdecfa9, 11) STEP(H, c, d, a, b, GET( 7), 0xf6bb4b60, 16) STEP(J, b, c, d, a, GET(10), 0xbebfbc70, 23)
STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) STEP(J, d, a, b, c, GET( 0), 0xeaa127fa, 11) STEP(H, c, d, a, b, GET( 3), 0xd4ef3085, 16) STEP(J, b, c, d, a, GET( 6), 0x04881d05, 23)
STEP(H, a, b, c, d, GET( 9), 0xd9d4d039, 4) STEP(J, d, a, b, c, GET(12), 0xe6db99e5, 11) STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) STEP(J, b, c, d, a, GET( 2), 0xc4ac5665, 23)
STEP(I, a, b, c, d, GET( 0), 0xf4292244, 6) STEP(I, d, a, b, c, GET( 7), 0x432aff97, 10) STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) STEP(I, b, c, d, a, GET( 5), 0xfc93a039, 21)
STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) STEP(I, d, a, b, c, GET( 3), 0x8f0ccc92, 10) STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) STEP(I, b, c, d, a, GET( 1), 0x85845dd1, 21)
STEP(I, a, b, c, d, GET( 8), 0x6fa87e4f, 6) STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) STEP(I, c, d, a, b, GET( 6), 0xa3014314, 15) STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21)
STEP(I, a, b, c, d, GET( 4), 0xf7537e82, 6) STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) STEP(I, c, d, a, b, GET( 2), 0x2ad7d2bb, 15) STEP(I, b, c, d, a, GET( 9), 0xeb86d391, 21)
#undef F
#undef G
#undef H
#undef J
#undef I
#undef GET
#undef SET
#undef STEP
a += saved_a; b += saved_b; c += saved_c; d += saved_d; ptr += 64;
} while (size -= 64);
A = a; B = b; C = c; D = d;
return ptr;
}
} ctx = { 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476 };
size_t ctx_lo = (data_size & 0x1fffffff) << 3, ctx_hi = data_size >> 29;
if (data_size >= 64)
{
data = ctx.Body(data, data_size & ~(unsigned long)63);
data_size &= 63;
}
Bit8u ctx_buffer[64];
memcpy(ctx_buffer, data, data_size);
ctx_buffer[data_size++] = 0x80;
size_t available = 64 - data_size;
if (available < 8)
{
memset(&ctx_buffer[data_size], 0, available);
ctx.Body(ctx_buffer, 64);
data_size = 0;
available = 64;
}
memset(&ctx_buffer[data_size], 0, available - 8);
#define OUT(dst, src) (dst)[0] = (Bit8u)(src); (dst)[1] = (Bit8u)((src) >> 8); (dst)[2] = (Bit8u)((src) >> 16); (dst)[3] = (Bit8u)((src) >> 24);
OUT(&ctx_buffer[56], ctx_lo)
OUT(&ctx_buffer[60], ctx_hi)
ctx.Body(ctx_buffer, 64);
OUT(&res[0], ctx.A)
OUT(&res[4], ctx.B)
OUT(&res[8], ctx.C)
OUT(&res[12], ctx.D)
#undef OUT
}
static void SHA1(const Bit8u* data, size_t data_size, Bit8u res[20])
{
// BASED ON SHA-1 in C (public domain)
// By Steve Reid - https://github.com/clibs/sha1
struct SHA1_CTX
{
static void Transform(Bit32u* state, const Bit8u* buffer)
{
Bit32u block[16]; memcpy(block, buffer, 64); // Non destructive (can have input buffer be const)
//Bit32u* block = buffer; // Destructive (buffer will be modified in place)
Bit32u a = state[0], b = state[1], c = state[2], d = state[3], e = state[4];
#define SHA1ROL(value, bits) (((value) << (bits)) | ((value) >> (32 - (bits))))
#ifdef WORDS_BIGENDIAN
#define SHA1BLK0(i) block[i]
#else
#define SHA1BLK0(i) (block[i] = (SHA1ROL(block[i],24)&0xFF00FF00)|(SHA1ROL(block[i],8)&0x00FF00FF))
#endif
#define SHA1BLK(i) (block[i&15] = SHA1ROL(block[(i+13)&15]^block[(i+8)&15]^block[(i+2)&15]^block[i&15],1))
#define SHA1R0(v,w,x,y,z,i) z+=((w&(x^y))^y)+SHA1BLK0(i)+0x5A827999+SHA1ROL(v,5);w=SHA1ROL(w,30);
#define SHA1R1(v,w,x,y,z,i) z+=((w&(x^y))^y)+SHA1BLK(i)+0x5A827999+SHA1ROL(v,5);w=SHA1ROL(w,30);
#define SHA1R2(v,w,x,y,z,i) z+=(w^x^y)+SHA1BLK(i)+0x6ED9EBA1+SHA1ROL(v,5);w=SHA1ROL(w,30);
#define SHA1R3(v,w,x,y,z,i) z+=(((w|x)&y)|(w&x))+SHA1BLK(i)+0x8F1BBCDC+SHA1ROL(v,5);w=SHA1ROL(w,30);
#define SHA1R4(v,w,x,y,z,i) z+=(w^x^y)+SHA1BLK(i)+0xCA62C1D6+SHA1ROL(v,5);w=SHA1ROL(w,30);
SHA1R0(a,b,c,d,e, 0); SHA1R0(e,a,b,c,d, 1); SHA1R0(d,e,a,b,c, 2); SHA1R0(c,d,e,a,b, 3);
SHA1R0(b,c,d,e,a, 4); SHA1R0(a,b,c,d,e, 5); SHA1R0(e,a,b,c,d, 6); SHA1R0(d,e,a,b,c, 7);
SHA1R0(c,d,e,a,b, 8); SHA1R0(b,c,d,e,a, 9); SHA1R0(a,b,c,d,e,10); SHA1R0(e,a,b,c,d,11);
SHA1R0(d,e,a,b,c,12); SHA1R0(c,d,e,a,b,13); SHA1R0(b,c,d,e,a,14); SHA1R0(a,b,c,d,e,15);
SHA1R1(e,a,b,c,d,16); SHA1R1(d,e,a,b,c,17); SHA1R1(c,d,e,a,b,18); SHA1R1(b,c,d,e,a,19);
SHA1R2(a,b,c,d,e,20); SHA1R2(e,a,b,c,d,21); SHA1R2(d,e,a,b,c,22); SHA1R2(c,d,e,a,b,23);
SHA1R2(b,c,d,e,a,24); SHA1R2(a,b,c,d,e,25); SHA1R2(e,a,b,c,d,26); SHA1R2(d,e,a,b,c,27);
SHA1R2(c,d,e,a,b,28); SHA1R2(b,c,d,e,a,29); SHA1R2(a,b,c,d,e,30); SHA1R2(e,a,b,c,d,31);
SHA1R2(d,e,a,b,c,32); SHA1R2(c,d,e,a,b,33); SHA1R2(b,c,d,e,a,34); SHA1R2(a,b,c,d,e,35);
SHA1R2(e,a,b,c,d,36); SHA1R2(d,e,a,b,c,37); SHA1R2(c,d,e,a,b,38); SHA1R2(b,c,d,e,a,39);
SHA1R3(a,b,c,d,e,40); SHA1R3(e,a,b,c,d,41); SHA1R3(d,e,a,b,c,42); SHA1R3(c,d,e,a,b,43);
SHA1R3(b,c,d,e,a,44); SHA1R3(a,b,c,d,e,45); SHA1R3(e,a,b,c,d,46); SHA1R3(d,e,a,b,c,47);
SHA1R3(c,d,e,a,b,48); SHA1R3(b,c,d,e,a,49); SHA1R3(a,b,c,d,e,50); SHA1R3(e,a,b,c,d,51);
SHA1R3(d,e,a,b,c,52); SHA1R3(c,d,e,a,b,53); SHA1R3(b,c,d,e,a,54); SHA1R3(a,b,c,d,e,55);
SHA1R3(e,a,b,c,d,56); SHA1R3(d,e,a,b,c,57); SHA1R3(c,d,e,a,b,58); SHA1R3(b,c,d,e,a,59);
SHA1R4(a,b,c,d,e,60); SHA1R4(e,a,b,c,d,61); SHA1R4(d,e,a,b,c,62); SHA1R4(c,d,e,a,b,63);
SHA1R4(b,c,d,e,a,64); SHA1R4(a,b,c,d,e,65); SHA1R4(e,a,b,c,d,66); SHA1R4(d,e,a,b,c,67);
SHA1R4(c,d,e,a,b,68); SHA1R4(b,c,d,e,a,69); SHA1R4(a,b,c,d,e,70); SHA1R4(e,a,b,c,d,71);
SHA1R4(d,e,a,b,c,72); SHA1R4(c,d,e,a,b,73); SHA1R4(b,c,d,e,a,74); SHA1R4(a,b,c,d,e,75);
SHA1R4(e,a,b,c,d,76); SHA1R4(d,e,a,b,c,77); SHA1R4(c,d,e,a,b,78); SHA1R4(b,c,d,e,a,79);
state[0] += a; state[1] += b; state[2] += c; state[3] += d; state[4] += e;
}
void Process(const Bit8u* data, size_t len)
{
size_t i; Bit32u j = count[0];
if ((count[0] += (Bit32u)(len << 3)) < j) count[1]++;
count[1] += (Bit32u)(len>>29);
j = (j >> 3) & 63;
if ((j + len) > 63)
{
memcpy(&buffer[j], data, (i = 64-j));
Transform(state, buffer);
for (; i + 63 < len; i += 64) Transform(state, &data[i]);
j = 0;
}
else i = 0;
memcpy(&buffer[j], &data[i], len - i);
}
Bit32u count[2], state[5];
Bit8u buffer[64];
} ctx;
ctx.count[0] = ctx.count[1] = 0;
ctx.state[0] = 0x67452301;
ctx.state[1] = 0xEFCDAB89;
ctx.state[2] = 0x98BADCFE;
ctx.state[3] = 0x10325476;
ctx.state[4] = 0xC3D2E1F0;
ctx.Process(data, data_size);
Bit8u finalcount[8];
for (unsigned i = 0; i < 8; i++) finalcount[i] = (Bit8u)((ctx.count[(i >= 4 ? 0 : 1)] >> ((3-(i & 3)) * 8) ) & 255);
Bit8u c = 0200;
ctx.Process(&c, 1);
while ((ctx.count[0] & 504) != 448) { c = 0000; ctx.Process(&c, 1); }
ctx.Process(finalcount, 8);
for (unsigned j = 0; j < 20; j++) res[j] = (Bit8u)((ctx.state[j>>2] >> ((3-(j & 3)) * 8) ) & 255);
}
struct Zip_Archive
{
FILE* zip;
Bit64u ofs, size;
Zip_Archive(FILE* _zip) : zip(_zip)
{
fseek_wrap(zip, 0, SEEK_END);
ofs = size = (Bit64u)ftell_wrap(zip);
}
~Zip_Archive()
{
if (!zip) return;
fclose(zip);
}
Bit32u Read(Bit64u seek_ofs, void *pBuf, Bit32u n)
{
if (seek_ofs >= size) n = 0;
else if ((Bit64u)n > (size - seek_ofs)) n = (Bit32u)(size - seek_ofs);
if (seek_ofs != ofs) { fseek_wrap(zip, seek_ofs, SEEK_SET); ofs = seek_ofs; }
Bit32u got = (Bit32u)fread(pBuf, 1, n, zip);
ofs += got;
return got;
}
bool Unpack(Bit64u zf_data_ofs, Bit32u zf_comp_size, Bit32u zf_uncomp_size, Bit8u zf_bit_flags, Bit8u zf_method, std::vector<Bit8u>& mem_data);
enum { METHOD_STORED = 0, METHOD_SHRUNK = 1, METHOD_IMPLODED = 6, METHOD_DEFLATED = 8 };
static bool MethodSupported(Bit32u method) { return (method == METHOD_DEFLATED || method == METHOD_STORED || method == METHOD_SHRUNK || method == METHOD_IMPLODED); }
};
// Various ZIP archive enums. To completely avoid cross platform compiler alignment and platform endian issues, we don't use structs for any of this stuff
enum
{
// ZIP archive identifiers and record sizes
ZIP_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06054b50, ZIP_CENTRAL_DIR_HEADER_SIG = 0x02014b50, ZIP_LOCAL_DIR_HEADER_SIG = 0x04034b50,
ZIP_LOCAL_DIR_HEADER_SIZE = 30, ZIP_CENTRAL_DIR_HEADER_SIZE = 46, ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE = 22,
ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG = 0x06064b50, ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE = 56,
ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG = 0x07064b50, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE = 20,
// End of central directory offsets
ZIP_ECDH_NUM_THIS_DISK_OFS = 4, ZIP_ECDH_NUM_DISK_CDIR_OFS = 6, ZIP_ECDH_CDIR_NUM_ENTRIES_ON_DISK_OFS = 8,
ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS = 10, ZIP_ECDH_CDIR_SIZE_OFS = 12, ZIP_ECDH_CDIR_OFS_OFS = 16, ZIP_ECDH_COMMENT_SIZE_OFS = 20,
ZIP64_ECDL_ECDH_OFS_OFS = 8, ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS = 32, ZIP64_ECDH_CDIR_SIZE_OFS = 40, ZIP64_ECDH_CDIR_OFS_OFS = 48,
// Central directory header record offsets
ZIP_CDH_BIT_FLAG_OFS = 8, ZIP_CDH_METHOD_OFS = 10, ZIP_CDH_FILE_TIME_OFS = 12, ZIP_CDH_FILE_DATE_OFS = 14, ZIP_CDH_CRC32_OFS = 16,
ZIP_CDH_COMPRESSED_SIZE_OFS = 20, ZIP_CDH_DECOMPRESSED_SIZE_OFS = 24, ZIP_CDH_FILENAME_LEN_OFS = 28, ZIP_CDH_EXTRA_LEN_OFS = 30,
ZIP_CDH_COMMENT_LEN_OFS = 32, ZIP_CDH_EXTERNAL_ATTR_OFS = 38, ZIP_CDH_LOCAL_HEADER_OFS = 42,
// Local directory header offsets
ZIP_LDH_FILENAME_LEN_OFS = 26, ZIP_LDH_EXTRA_LEN_OFS = 28,
};
#define ZIP_MAX(a,b) (((a)>(b))?(a):(b))
#define ZIP_MIN(a,b) (((a)<(b))?(a):(b))
#define ZIP_READ_LE16(p) ((Bit16u)(((const Bit8u *)(p))[0]) | ((Bit16u)(((const Bit8u *)(p))[1]) << 8U))
#define ZIP_READ_LE32(p) ((Bit32u)(((const Bit8u *)(p))[0]) | ((Bit32u)(((const Bit8u *)(p))[1]) << 8U) | ((Bit32u)(((const Bit8u *)(p))[2]) << 16U) | ((Bit32u)(((const Bit8u *)(p))[3]) << 24U))
#define ZIP_READ_LE64(p) ((Bit64u)(((const Bit8u *)(p))[0]) | ((Bit64u)(((const Bit8u *)(p))[1]) << 8U) | ((Bit64u)(((const Bit8u *)(p))[2]) << 16U) | ((Bit64u)(((const Bit8u *)(p))[3]) << 24U) | ((Bit64u)(((const Bit8u *)(p))[4]) << 32U) | ((Bit64u)(((const Bit8u *)(p))[5]) << 40U) | ((Bit64u)(((const Bit8u *)(p))[6]) << 48U) | ((Bit64u)(((const Bit8u *)(p))[7]) << 56U))
#define ZIP_READ_BE32(p) ((Bit32u)((((const Bit8u *)(p))[0] << 24) | (((const Bit8u *)(p))[1] << 16) | (((const Bit8u *)(p))[2] << 8) | ((const Bit8u *)(p))[3]))
#define ZIP_READ_BE64(p) ((Bit64u)((((Bit64u)((const Bit8u *)(p))[0] << 56) | ((Bit64u)((const Bit8u *)(p))[1] << 48) | ((Bit64u)((const Bit8u *)(p))[2] << 40) | ((Bit64u)((const Bit8u *)(p))[3] << 32) | ((Bit64u)((const Bit8u *)(p))[4] << 24) | ((Bit64u)((const Bit8u *)(p))[5] << 16) | ((Bit64u)((const Bit8u *)(p))[6] << 8) | (Bit64u)((const Bit8u *)(p))[7])))
#ifdef NDEBUG
#define ZIP_ASSERT(cond)
#else
#define ZIP_ASSERT(cond) (void)((cond) ? ((int)0) : *(volatile int*)0 |= 0xbad|fprintf(stderr, "FAILED ASSERT (%s)\n", #cond))
#endif
void XMLAppendRawF(std::vector<Bit8u>& buf, size_t maxlen, char const* format, ...)
{
size_t oldlen = buf.size();
buf.resize(oldlen + maxlen + 1);
va_list va;
va_start(va, format);
int res = vsprintf((char*)&buf[oldlen], format, va);
va_end(va);
ZIP_ASSERT(maxlen >= (size_t)res);
buf.resize(oldlen + res);
}
void XMLAppendRaw(std::vector<Bit8u>& buf, size_t len, const char* str)
{
buf.resize(buf.size() + len);
memcpy(&buf[buf.size() - len], str, len);
}
void XMLAppendEscaped(std::vector<Bit8u>& buf, size_t len, const char* str)
{
for (; len--; str++)
if (*str == '&') XMLAppendRaw(buf, 5, "&");
else if (*str == '<') XMLAppendRaw(buf, 4, "<");
else if (*str == '>') XMLAppendRaw(buf, 4, ">");
else buf.push_back((Bit8u)*str);
}
void AppendBase64(std::vector<Bit8u>& buf, const Bit8u* data, size_t size)
{
static const char base64enc[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
for (const Bit8u *p = data, *pEnd = p + size; p < pEnd; p += 3)
{
unsigned n = (*p << 16) | (((p + 2) < pEnd) ? ((p[1] << 8) | p[2]) : (((p + 1) < pEnd) ? (p[1] << 8) : 0));
buf.push_back((Bit8u)base64enc[(n >> 18) & 63]);
buf.push_back((Bit8u)base64enc[(n >> 12) & 63]);
buf.push_back(((p + 1) < pEnd) ? (Bit8u)base64enc[(n >> 6) & 63] : (Bit8u)'=');
buf.push_back(((p + 2) < pEnd) ? (Bit8u)base64enc[n & 63] : (Bit8u)'=');
}
}
void ListCHDTracks(std::vector<Bit8u>& line, const Bit8u* chd_data, size_t chd_size)
{
enum { CHD_V5_HEADER_SIZE = 124, CHD_V5_UNCOMPMAPENTRYBYTES = 4, CD_MAX_SECTOR_DATA = 2352, CD_MAX_SUBCODE_DATA = 96, CD_FRAME_SIZE = CD_MAX_SECTOR_DATA + CD_MAX_SUBCODE_DATA };
enum { METADATA_HEADER_SIZE = 16, CDROM_TRACK_METADATA_TAG = 1128813650, CDROM_TRACK_METADATA2_TAG = 1128813618, CD_TRACK_PADDING = 4 };
// Read CHD header and check signature
Bit32u* chd_hunkmap = NULL;
const Bit8u* rawheader = chd_data;
if (chd_size < CHD_V5_HEADER_SIZE || memcmp(rawheader, "MComprHD", 8))
{
chderr:
fprintf(stderr, "Invalid/unsupported CHD file\n");
if (chd_hunkmap) free(chd_hunkmap);
return;
}
// Check supported version, flags and compression
Bit32u hdr_length = ZIP_READ_BE32(&rawheader[8]);
Bit32u hdr_version = ZIP_READ_BE32(&rawheader[12]);
if (hdr_version != 5 || hdr_length != CHD_V5_HEADER_SIZE) goto chderr; // only ver 5 is supported
if (ZIP_READ_BE32(&rawheader[16])) goto chderr; // compression is not supported
// Make sure it's a CD image
Bit32u unitsize = ZIP_READ_BE32(&rawheader[60]);
int chd_hunkbytes = (int)ZIP_READ_BE32(&rawheader[56]);
if (unitsize != CD_FRAME_SIZE || (chd_hunkbytes % CD_FRAME_SIZE) || !chd_hunkbytes) goto chderr; // not CD sector size
// Read file offsets for hunk mapping and track meta data
Bit64u filelen = (Bit64u)chd_size;
Bit64u logicalbytes = ZIP_READ_BE64(&rawheader[32]);
Bit64u mapoffset = ZIP_READ_BE64(&rawheader[40]);
Bit64u metaoffset = ZIP_READ_BE64(&rawheader[48]);
if (mapoffset < CHD_V5_HEADER_SIZE || mapoffset >= filelen || metaoffset < CHD_V5_HEADER_SIZE || metaoffset >= filelen || !logicalbytes) goto chderr;
// Read hunk mapping and convert to file offsets
Bit32u hunkcount = (Bit32u)((logicalbytes + chd_hunkbytes - 1) / chd_hunkbytes);
if (chd_size < mapoffset + hunkcount * CHD_V5_UNCOMPMAPENTRYBYTES) goto chderr;
const Bit8u* raw_hunkmap = chd_data + (size_t)mapoffset;
chd_hunkmap = (Bit32u*)malloc(hunkcount * CHD_V5_UNCOMPMAPENTRYBYTES);
for (Bit32u i = 0; i != hunkcount; i++)
{
chd_hunkmap[i] = ZIP_READ_BE32(&raw_hunkmap[i * CHD_V5_UNCOMPMAPENTRYBYTES]) * chd_hunkbytes;
if (chd_size < chd_hunkmap[i] + chd_hunkbytes) goto chderr;
}
// Read track meta data
Bit32u track_frame = 0;
for (Bit64u metaentry_offset = metaoffset, metaentry_next; metaentry_offset != 0; metaentry_offset = metaentry_next)
{
char mt_type[32], mt_subtype[32], mt_pgtype[32];
if (chd_size < metaentry_offset + METADATA_HEADER_SIZE) goto chderr;
const Bit8u* raw_meta_header = chd_data + metaentry_offset;
Bit32u metaentry_metatag = ZIP_READ_BE32(&raw_meta_header[0]);
Bit32u metaentry_length = (ZIP_READ_BE32(&raw_meta_header[4]) & 0x00ffffff);
metaentry_next = ZIP_READ_BE64(&raw_meta_header[8]);
if (metaentry_metatag != CDROM_TRACK_METADATA_TAG && metaentry_metatag != CDROM_TRACK_METADATA2_TAG) continue;
if (chd_size < (size_t)(metaentry_offset + METADATA_HEADER_SIZE) + metaentry_length) goto chderr;
const char* meta = (const char*)(chd_data + metaentry_offset + METADATA_HEADER_SIZE);
int mt_track_no = 0, mt_frames = 0, mt_pregap = 0;
if (sscanf(meta,
(metaentry_metatag == CDROM_TRACK_METADATA2_TAG ? "TRACK:%d TYPE:%30s SUBTYPE:%30s FRAMES:%d PREGAP:%d PGTYPE:%30s" : "TRACK:%d TYPE:%30s SUBTYPE:%30s FRAMES:%d"),
&mt_track_no, mt_type, mt_subtype, &mt_frames, &mt_pregap, mt_pgtype) < 4) continue;
// In CHD files tracks are padded to a to a 4-sector boundary.
track_frame += ((CD_TRACK_PADDING - (track_frame % CD_TRACK_PADDING)) % CD_TRACK_PADDING);
// Read track data and calculate hashes (CHD sectorSize is always 2448, data_size is based on chdman source)
const bool isAudio = !strcmp(mt_type, "AUDIO");
const bool ds2048 = !strcmp(mt_type, "MODE1") || !strcmp(mt_type, "MODE2_FORM1");
const bool ds2324 = !strcmp(mt_type, "MODE2_FORM2");
const bool ds2336 = !strcmp(mt_type, "MODE2") || !strcmp(mt_type, "MODE2_FORM_MIX");
const size_t data_size = (ds2048 ? 2048 : ds2324 ? 2324 : ds2336 ? 2336 : CD_MAX_SECTOR_DATA);
const size_t track_size = (size_t)mt_frames * data_size;
Bit8u* track_data = (Bit8u*)malloc(track_size), *track_out = track_data;
for (Bit32u track_frame_end = track_frame + mt_frames; track_frame != track_frame_end; track_frame++, track_out += data_size)
{
size_t p = track_frame * CD_FRAME_SIZE, hunk = (p / chd_hunkbytes), hunk_ofs = (p % chd_hunkbytes), hunk_pos = chd_hunkmap[hunk];
if (!hunk_pos) { memset(track_out, 0, data_size); }
else { memcpy(track_out, chd_data + hunk_pos + hunk_ofs, data_size); }
}
Bit32u in_zeros = 0, out_zeros = 0;
if (isAudio)
{
// CHD audio endian swap
for (Bit8u *p = track_data, *pEnd = p + track_size, tmp; p != pEnd; p += 2)
{ tmp = p[0]; p[0] = p[1]; p[1] = tmp; }
// Additional info for audio tracks
for (; in_zeros != track_size && track_data[in_zeros] == 0; in_zeros++) {}
if (in_zeros != track_size) for (; out_zeros != track_size && track_data[track_size - 1 - out_zeros] == 0; out_zeros++) {}
}
Bit32u trackcrc32 = CRC32(track_data, (size_t)track_size);
Bit8u trackmd5[16], tracksha1[20];
FastMD5(track_data, (size_t)track_size, trackmd5);
SHA1(track_data, (size_t)track_size, tracksha1);
XMLAppendRawF(line, 128, " <track number=\"%d\" type=\"%s\" frames=\"%d\"", mt_track_no, mt_type, mt_frames);
if (mt_pregap) XMLAppendRawF(line, 40, " %spregap=\"%d\"", (mt_pgtype[0] != 'V' ? "omitted_" : ""), mt_pregap);
XMLAppendRawF(line, 64, " duration=\"%02d:%02d:%02d\" size=\"%u\"", (mt_frames/75/60), (mt_frames/75)%60, mt_frames%75, (Bit32u)track_size);
XMLAppendRawF(line, 21, " crc=\"%08x\" md5=\"", trackcrc32);
for (int md5i = 0; md5i != 16; md5i++) XMLAppendRawF(line, 2, "%02x", trackmd5[md5i]);
XMLAppendRaw(line, 8, "\" sha1=\"");
for (int sha1i = 0; sha1i != 20; sha1i++) XMLAppendRawF(line, 2, "%02x", tracksha1[sha1i]);
if (isAudio) XMLAppendRawF(line, 72, "\" in_zeros=\"%u\" out_zeros=\"%u", in_zeros, out_zeros);
XMLAppendRaw(line, 4, "\"/>\n");
free(track_data);
}
free(chd_hunkmap);
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
fprintf(stderr, "Missing DOSZ path(s)\n\nRun tool with:\n %s <DOSZ PATH> ...\n\n", (argc ? argv[0] : "MakePureDOSDat"));
return 1;
}
for (int test = 1; test != argc; test++)
{
FILE *fZIP = fopen(argv[test], "rb");
if (!fZIP)
{
fprintf(stderr, "Unable to find input file %s\n", argv[test]);
return 1;
}
fclose(fZIP);
}
time_t t = time(NULL);
struct tm tm = *gmtime(&t);
printf("<?xml version=\"1.0\"?>\n");
printf("<!DOCTYPE datafile PUBLIC \"-//Logiqx//DTD ROM Management Datafile//EN\" \"http://www.logiqx.com/Dats/datafile.dtd\">\n");
printf("<datafile>\n");
printf(" <header>\n");
printf(" <name>Pure DOS DAT</name>\n");
printf(" <description>DOS Games</description>\n");
printf(" <version>%04d-%02d-%02d %02d-%02d-%02d</version>\n", (tm.tm_year+1900), (tm.tm_mon+1), tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
printf(" <date>%04d-%02d-%02d %02d-%02d-%02d</date>\n", (tm.tm_year+1900), (tm.tm_mon+1), tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
printf(" <author>Pure DOS DAT</author>\n");
printf(" <homepage>Pure DOS DAT</homepage>\n");
printf(" <url>https://github.com/PureDOS/DAT</url>\n");
printf(" </header>\n");
for (int argi = 1; argi != argc; argi++)
{
const char *path = argv[argi];
const char *lasts = strrchr(path, '/'), *lastbs = strrchr(path, '\\');
const char *fname = (lasts > lastbs && lasts[1] ? lasts+1 : (lastbs && lastbs[1] ? lastbs+1 : path));
const char *pathend = fname + strlen(fname), *fname_gameend = pathend, *ext = NULL;
for (const char* pDot = fname_gameend;; fname_gameend = pDot--)
{
while (pDot > fname && *pDot != '.') pDot--;
if (pDot == fname) break;
else if (!strncasecmp(pDot, ".dosz", 5)) ext = pDot;
else if (!strncasecmp(pDot, ".dosc", 5)) ext = pDot;
else if (!strncasecmp(pDot, ".zip", 4)) ext = pDot;
else break;
}
int year = 0;
const char *developer = NULL, *developerEd = NULL, *variant = NULL, *variantEd = NULL;
for (const char *op, *ed = fname; (op = strstr(ed+1, " (")) != NULL && (ed = strchr(op, ')')) != NULL;)
{
if (op < fname_gameend) fname_gameend = op;
if (year < 1970 && (ed - op) == 6 && (year = atoi(op+2)) >= 1970) { }
else if (!developer) { developer = op+2; developerEd = ed; }
else if (!variant) { variant = op+2; variantEd = ed; }
}
FILE *fZIP = fopen(path, "rb");
if (!fZIP)
{
fprintf(stderr, "Unable to find input file %s\n", path);
return 1;
}
FILE *fTXT = NULL, *fDOSCTXT = NULL;
if (ext)
{
char* tmp = (char*)malloc(pathend - path + 5), *tmpzc = tmp + (ext - path) + 4;
strcpy(tmp, path);
if (tmpzc[0] && tmpzc[1] == '.') tmpzc[1] = '\0'; // cut between ".dosz.zip"
strcat(tmp, ".txt");
fTXT = fopen(tmp, "rb");
if ((tmpzc[0]|0x20) == 'z')
{
*tmpzc = (*tmpzc == 'z' ? 'c' : 'C');
fDOSCTXT = fopen(tmp, "rb");
}
free(tmp);
}
std::vector<Bit8u> gametag;
XMLAppendRaw(gametag, 13, " <game name=\""); XMLAppendEscaped(gametag, ((ext ? ext : pathend)-fname), fname); XMLAppendRaw(gametag, 8, (ext && (ext[4]|0x20) == 'c' ? ".dosc\">\n" : ".dosz\">\n"));
XMLAppendRaw(gametag, 15, " <description>"); XMLAppendEscaped(gametag, (fname_gameend-fname), fname); XMLAppendRaw(gametag, 15, "</description>\n");
if (fTXT)
{
XMLAppendRaw(gametag, 11, " <comment>");
for (int n, c[4]; (n = (int)fread(c, 1, sizeof(c), fTXT)) != 0;) XMLAppendEscaped(gametag, n, (char*)c);
XMLAppendRaw(gametag, 11, "</comment>\n");
fclose(fTXT);
}
if (fDOSCTXT)
{
XMLAppendRawF(gametag, 16, " <comment_dosc>");
for (int n, c[4]; (n = (int)fread(c, 1, sizeof(c), fDOSCTXT)) != 0;) XMLAppendEscaped(gametag, n, (char*)c);
XMLAppendRawF(gametag, 16, "</comment_dosc>\n");
fclose(fDOSCTXT);
}
if (year >= 1970 && year <= 9999) XMLAppendRawF(gametag, 20, " <year>%d</year>\n", year);
if (developer) { XMLAppendRaw(gametag, 13, " <developer>"); XMLAppendEscaped(gametag, (developerEd-developer), developer); XMLAppendRaw(gametag, 13, "</developer>\n"); }
if (variant) { XMLAppendRaw(gametag, 11, " <variant>"); XMLAppendEscaped(gametag, (variantEd-variant), variant); XMLAppendRaw(gametag, 11, "</variant>\n"); }
Zip_Archive archive(fZIP);
// Basic sanity checks - reject files which are too small.
if (archive.size < ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
{
invalid_zip:
fprintf(stderr, "Invalid or unsupported ZIP file: %s\n", path);
return 1;
}
// Find the end of central directory record by scanning the file from the end towards the beginning.
Bit8u buf[4096];
Bit64u ecdh_ofs = (archive.size < sizeof(buf) ? 0 : archive.size - sizeof(buf));
for (;; ecdh_ofs = ZIP_MAX(ecdh_ofs - (sizeof(buf) - 3), 0))
{
Bit32s i, n = (Bit32s)ZIP_MIN(sizeof(buf), archive.size - ecdh_ofs);
if (archive.Read(ecdh_ofs, buf, (Bit32u)n) != (Bit32u)n) goto invalid_zip;
for (i = n - 4; i >= 0; --i) { if (ZIP_READ_LE32(buf + i) == ZIP_END_OF_CENTRAL_DIR_HEADER_SIG) break; }
if (i >= 0) { ecdh_ofs += i; break; }
if (!ecdh_ofs || (archive.size - ecdh_ofs) >= (0xFFFF + ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)) goto invalid_zip;
}
// Read and verify the end of central directory record.
if (archive.Read(ecdh_ofs, buf, ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE) != ZIP_END_OF_CENTRAL_DIR_HEADER_SIZE)
return 1;
Bit64u total_files = ZIP_READ_LE16(buf + ZIP_ECDH_CDIR_TOTAL_ENTRIES_OFS);
Bit64u cdir_size = ZIP_READ_LE32(buf + ZIP_ECDH_CDIR_SIZE_OFS);
Bit64u cdir_ofs = ZIP_READ_LE32(buf + ZIP_ECDH_CDIR_OFS_OFS);
if ((cdir_ofs == 0xFFFFFFFF || cdir_size == 0xFFFFFFFF || total_files == 0xFFFF)
&& ecdh_ofs >= (ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE + ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)
&& archive.Read(ecdh_ofs - ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE, buf, ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE) == ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIZE
&& ZIP_READ_LE32(buf) == ZIP64_END_OF_CENTRAL_DIR_LOCATOR_SIG)
{
Bit64u ecdh64_ofs = ZIP_READ_LE64(buf + ZIP64_ECDL_ECDH_OFS_OFS);
if (ecdh64_ofs <= (archive.size - ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE)
&& archive.Read(ecdh64_ofs, buf, ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE) == ZIP64_END_OF_CENTRAL_DIR_HEADER_SIZE
&& ZIP_READ_LE32(buf) == ZIP64_END_OF_CENTRAL_DIR_HEADER_SIG)
{
total_files = ZIP_READ_LE64(buf + ZIP64_ECDH_CDIR_TOTAL_ENTRIES_OFS);
cdir_size = ZIP_READ_LE64(buf + ZIP64_ECDH_CDIR_SIZE_OFS);
cdir_ofs = ZIP_READ_LE64(buf + ZIP64_ECDH_CDIR_OFS_OFS);
}
}
if (!total_files
|| (cdir_size >= 0x10000000) // limit to 256MB content directory
|| (cdir_size < total_files * ZIP_CENTRAL_DIR_HEADER_SIZE)
|| ((cdir_ofs + cdir_size) > archive.size)
) goto invalid_zip;
void* m_central_dir = malloc((size_t)cdir_size);
if (archive.Read(cdir_ofs, m_central_dir, (Bit32u)cdir_size) != cdir_size)
goto invalid_zip;
const Bit8u *cdir_start = (const Bit8u*)m_central_dir, *cdir_end = cdir_start + cdir_size, *p = cdir_start;
// Now create an index into the central directory file records, do some basic sanity checking on each record, and check for zip64 entries (which are not yet supported).
p = cdir_start;
std::vector< std::vector<Bit8u> > roms;
for (Bit32u i = 0, nparents = 0, total_header_size; i < total_files && p >= cdir_start && p < cdir_end && ZIP_READ_LE32(p) == ZIP_CENTRAL_DIR_HEADER_SIG; i++, p += total_header_size)
{
Bit32u bit_flag = ZIP_READ_LE16(p + ZIP_CDH_BIT_FLAG_OFS);
Bit32u method = ZIP_READ_LE16(p + ZIP_CDH_METHOD_OFS);
Bit16u file_time = ZIP_READ_LE16(p + ZIP_CDH_FILE_TIME_OFS);
Bit16u file_date = ZIP_READ_LE16(p + ZIP_CDH_FILE_DATE_OFS);
Bit32u crc32 = ZIP_READ_LE32(p + ZIP_CDH_CRC32_OFS);
Bit64u comp_size = ZIP_READ_LE32(p + ZIP_CDH_COMPRESSED_SIZE_OFS);
Bit64u decomp_size = ZIP_READ_LE32(p + ZIP_CDH_DECOMPRESSED_SIZE_OFS);
Bit32u filename_len = ZIP_READ_LE16(p + ZIP_CDH_FILENAME_LEN_OFS);
Bit32s extra_len = ZIP_READ_LE16(p + ZIP_CDH_EXTRA_LEN_OFS);
Bit32s external_attr = ZIP_READ_LE32(p + ZIP_CDH_EXTERNAL_ATTR_OFS);
Bit64u local_header_ofs = ZIP_READ_LE32(p + ZIP_CDH_LOCAL_HEADER_OFS);
total_header_size = ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len + extra_len + ZIP_READ_LE16(p + ZIP_CDH_COMMENT_LEN_OFS);
if (!Zip_Archive::MethodSupported(method)
|| (p + total_header_size > cdir_end)
|| (bit_flag & (1 | 32)) // Encryption and patch files are not supported.
) goto invalid_zip;
if (decomp_size == 0xFFFFFFFF || comp_size == 0xFFFFFFFF || local_header_ofs == 0xFFFFFFFF)
{
for (const Bit8u *x = p + ZIP_CENTRAL_DIR_HEADER_SIZE + filename_len, *xEnd = x + extra_len; (x + (sizeof(Bit16u) * 2)) < xEnd;)
{
const Bit8u *field = x + (sizeof(Bit16u) * 2), *fieldEnd = field + ZIP_READ_LE16(x + 2);
if (ZIP_READ_LE16(x) != 0x0001 || fieldEnd > xEnd) { x = fieldEnd; continue; } // Not Zip64 extended information extra field
if (decomp_size == 0xFFFFFFFF)
{
if ((size_t)(fieldEnd - field) < sizeof(Bit64u)) goto invalid_zip;
decomp_size = ZIP_READ_LE64(field);
field += sizeof(Bit64u);
}
if (comp_size == 0xFFFFFFFF)
{
if ((size_t)(fieldEnd - field) < sizeof(Bit64u)) goto invalid_zip;
comp_size = ZIP_READ_LE64(field);
field += sizeof(Bit64u);
}
if (local_header_ofs == 0xFFFFFFFF)
{
if ((size_t)(fieldEnd - field) < sizeof(Bit64u)) goto invalid_zip;
local_header_ofs = ZIP_READ_LE64(field);
field += sizeof(Bit64u);
}
break;
}
}
if (((!method) && (decomp_size != comp_size)) || (decomp_size && !comp_size)
|| (decomp_size > 0xFFFFFFFF) || (comp_size > 0xFFFFFFFF) // not supported on DOS file systems
|| ((local_header_ofs + ZIP_LOCAL_DIR_HEADER_SIZE + comp_size) > archive.size)
) goto invalid_zip;
std::vector<Bit8u> mem_data;
archive.Unpack(local_header_ofs, (Bit32u)comp_size, (Bit32u)decomp_size, (Bit8u)bit_flag, (Bit8u)method, mem_data);
const Bit8u* mem_ptr = (decomp_size ? &mem_data[0] : NULL);
ZIP_ASSERT(mem_data.size() == decomp_size);
ZIP_ASSERT(crc32 == CRC32(mem_ptr, (size_t)decomp_size));
Bit8u md5[16], sha1[20];
FastMD5(mem_ptr, (size_t)decomp_size, md5);
SHA1(mem_ptr, (size_t)decomp_size, sha1);
char *name = (char*)(p + ZIP_CENTRAL_DIR_HEADER_SIZE);
for (char *p = name, *name_end = name + filename_len; p != name_end; p++)
if (*p == '\\')
*p = '/'; // convert back-slashes to regular slashes
bool is_dir = (name[filename_len - 1] == '/' || (external_attr & 0x10));
if (is_dir && (file_date >> 9) >= 45)
{
// Fix time stamp of directory records from 2025 and newer to 1999-12-31 (and skip them when outputting records of non-empty directories below)
file_date = ((19 << 9) | (12 << 5) | 31);
file_time = 0;
}
else if (file_date == 8600 && file_time == 48128)
{
// Don't output the TrrntZip default time stamp (1996-12-24 23:32:00)
file_date = file_time = 0;
}
else if ((file_date >> 9) >= 45)
{
// Don't output file time stamps from 2025 and newer
file_date = file_time = 0;
}
// Terminate file name with \b for sorting below (is changed to " during output)
roms.push_back(std::vector<Bit8u>());
std::vector<Bit8u>& line = roms.back();
XMLAppendRaw(line, 13, " <rom name=\"");
XMLAppendEscaped(line, filename_len, name);
if (is_dir && name[filename_len - 1] != '/') line.push_back('/');
XMLAppendRawF(line, 41, "\b size=\"%u\" crc=\"%08x\" md5=\"", (unsigned)decomp_size, crc32);
for (int md5i = 0; md5i != 16; md5i++) XMLAppendRawF(line, 2, "%02x", md5[md5i]);
XMLAppendRaw(line, 8, "\" sha1=\"");
for (int sha1i = 0; sha1i != 20; sha1i++) XMLAppendRawF(line, 2, "%02x", sha1[sha1i]);
if (file_date || file_time)
XMLAppendRawF(line, 27, "\" date=\"%04d-%02d-%02d %02d:%02d:%02d", ((file_date >> 9) + 1980), ((file_date >> 5) & 0xf), (file_date & 0x1f), (file_time >> 11), ((file_time >> 5) & 0x3f), ((file_time & 0x1f) * 2));
// If this is a CHD file, print out track information
if (filename_len > 4 && !strncasecmp(name + filename_len - 4, ".chd", 4))
{
XMLAppendRaw(line, 3, "\">\n");
ListCHDTracks(line, mem_ptr, (size_t)decomp_size);
XMLAppendRaw(line, 9, " </rom>\n");
}
else if (filename_len == 7 && !strncasecmp(name, "DOS.YML", 7))
{
XMLAppendRaw(line, 8, "\" data=\"");
AppendBase64(line, mem_ptr, (size_t)decomp_size);
XMLAppendRaw(line, 4, "\"/>\n");
}
else
{
XMLAppendRaw(line, 4, "\"/>\n");
}
/* DOSZ files can contain special empty <base>.dosz.parent files which means a parent dosz file is used */
if (filename_len > 7 && decomp_size == 0 && !strncasecmp(name + filename_len - 7, ".parent", 7) && !memchr(name, '/', filename_len))
{
if (nparents++)
{
fprintf(stderr, "DOSZ file has multiple parents: %s\n", path);
return 1;
}
XMLAppendRawF(gametag, 20 + filename_len - 7, " <parent>%.*s</parent>\n", filename_len - 7, name);
}
}
free(m_central_dir);
// Sort strings (which sorts it by filename)
struct Local
{
static int SortFunc(const void* va, const void* vb)
{
std::vector<Bit8u> &a = *(std::vector<Bit8u>*)va, &b = *(std::vector<Bit8u>*)vb;
for (char *pa = (char*)&a[0], *pb = (char*)&b[0];;)
{
char ca = *(pa++), cb = *(pb++);
int d = ((ca >= 'A' && ca <= 'Z') ? (ca|0x20) : ca) - ((cb >= 'A' && cb <= 'Z') ? (cb|0x20) : cb);
if (d) return d;
if (!ca) return 0;
}
}
};
qsort(&roms[0], roms.size(), sizeof(roms[0]), Local::SortFunc);
// Output game tag and rom strings (skip directory entries)
printf("%.*s", (unsigned)gametag.size(), (char*)&gametag[0]);
for (size_t j = 0, iend = roms.size(); j != iend; j++)
{
char *line = (char*)&roms[j][0], *name_end = strchr(line, '\b');
if (name_end[-1] == '/')
{
// Don't print rom element for non-empty directory record with a too new time stamp
bool empty_dir = (j == iend - 1 || memcmp(line, &roms[j+1][0], (name_end - line)));
bool ignore_dir = (!empty_dir && (strstr(line, " date=\"1999-12-31 00:00:00\"") || !strstr(line, " date=\"")));
if (ignore_dir)
continue;
}
*name_end = '"'; // fix \b to " now that entries are sorted and directories are checked
printf("%.*s", (unsigned)roms[j].size(), line);
}
printf(" </game>\n");
}
printf("</datafile>\n");
return 0;
}
struct miniz
{
// BASED ON MINIZ
// miniz.c v1.15 - public domain deflate
// Rich Geldreich <richgel99@gmail.com>, last updated Oct. 13, 2013
// Set MINIZ_HAS_64BIT_REGISTERS to 1 if operations on 64-bit integers are reasonably fast (and don't involve compiler generated calls to helper functions).
#if defined(_M_X64) || defined(_WIN64) || defined(__MINGW64__) || defined(_LP64) || defined(__LP64__) || defined(__ia64__) || defined(__x86_64__)
#define MINIZ_HAS_64BIT_REGISTERS 1
#else
#define MINIZ_HAS_64BIT_REGISTERS 0
#endif
enum
{
// Decompression flags used by tinfl_decompress().
TINFL_FLAG_HAS_MORE_INPUT = 2, // If set, there are more input bytes available beyond the end of the supplied input buffer. If clear, the input buffer contains all remaining input.
TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF = 4, // If set, the output buffer is large enough to hold the entire decompressed stream. If clear, the output buffer is at least the size of the dictionary (typically 32KB).
// Max size of read buffer.
MZ_ZIP_MAX_IO_BUF_SIZE = 16*1024, // Was 64*1024 originally (though max size readable through DOS_File would be 0xFFFF).
// Max size of LZ dictionary (output buffer).
TINFL_LZ_DICT_SIZE = 32*1024, // fixed for zip
// Internal/private bits follow.
TINFL_MAX_HUFF_TABLES = 3, TINFL_MAX_HUFF_SYMBOLS_0 = 288, TINFL_MAX_HUFF_SYMBOLS_1 = 32, TINFL_MAX_HUFF_SYMBOLS_2 = 19,
TINFL_FAST_LOOKUP_BITS = 10, TINFL_FAST_LOOKUP_SIZE = 1 << TINFL_FAST_LOOKUP_BITS,
// Number coroutine states consecutively
TINFL_STATE_INDEX_BLOCK_BOUNDRY = 1,
TINFL_STATE_3 , TINFL_STATE_5 , TINFL_STATE_6 , TINFL_STATE_7 , TINFL_STATE_51, TINFL_STATE_52,
TINFL_STATE_9 , TINFL_STATE_38, TINFL_STATE_11, TINFL_STATE_14, TINFL_STATE_16, TINFL_STATE_18,
TINFL_STATE_23, TINFL_STATE_24, TINFL_STATE_25, TINFL_STATE_26, TINFL_STATE_27, TINFL_STATE_53,
TINFL_STATE_END
};
// Return status.
enum tinfl_status
{
TINFL_STATUS_BAD_PARAM = -3,
TINFL_STATUS_FAILED = -1,
TINFL_STATUS_DONE = 0,
TINFL_STATUS_NEEDS_MORE_INPUT = 1,
TINFL_STATUS_HAS_MORE_OUTPUT = 2,
};
#if MINIZ_HAS_64BIT_REGISTERS
typedef Bit64u tinfl_bit_buf_t;
#else
typedef Bit32u tinfl_bit_buf_t;
#endif
struct tinfl_huff_table
{
Bit16s m_look_up[TINFL_FAST_LOOKUP_SIZE];
Bit16s m_tree[TINFL_MAX_HUFF_SYMBOLS_0 * 2];
Bit8u m_code_size[TINFL_MAX_HUFF_SYMBOLS_0];
};
struct tinfl_decompressor
{
tinfl_huff_table m_tables[TINFL_MAX_HUFF_TABLES];
Bit32u m_state, m_num_bits, m_final, m_type, m_dist, m_counter, m_num_extra, m_table_sizes[TINFL_MAX_HUFF_TABLES];
tinfl_bit_buf_t m_bit_buf;
size_t m_dist_from_out_buf_start;
Bit8u m_raw_header[4], m_len_codes[TINFL_MAX_HUFF_SYMBOLS_0 + TINFL_MAX_HUFF_SYMBOLS_1 + 137];
};
// Initializes the decompressor to its initial state.
static void tinfl_init(tinfl_decompressor *r) { r->m_state = 0; }
// Main low-level decompressor coroutine function. This is the only function actually needed for decompression. All the other functions are just high-level helpers for improved usability.
// This is a universal API, i.e. it can be used as a building block to build any desired higher level decompression API. In the limit case, it can be called once per every byte input or output.
static tinfl_status tinfl_decompress(tinfl_decompressor *r, const Bit8u *pIn_buf_next, Bit32u *pIn_buf_size, Bit8u *pOut_buf_start, Bit8u *pOut_buf_next, Bit32u *pOut_buf_size, const Bit32u decomp_flags)
{
// An attempt to work around MSVC's spammy "warning C4127: conditional expression is constant" message.
#ifdef _MSC_VER
#define TINFL_MACRO_END while (0, 0)
#else
#define TINFL_MACRO_END while (0)
#endif
#define TINFL_MEMCPY(d, s, l) memcpy(d, s, l)
#define TINFL_MEMSET(p, c, l) memset(p, c, l)
#define TINFL_CLEAR(obj) memset(&(obj), 0, sizeof(obj))
#define TINFL_CR_BEGIN switch(r->m_state) { case 0:
#define TINFL_CR_RETURN(state_index, result) do { status = result; r->m_state = state_index; goto common_exit; case state_index:; } TINFL_MACRO_END
#define TINFL_CR_RETURN_FOREVER(state_index, result) do { status = result; r->m_state = TINFL_STATE_END; goto common_exit; } TINFL_MACRO_END
#define TINFL_CR_FINISH }
// TODO: If the caller has indicated that there's no more input, and we attempt to read beyond the input buf, then something is wrong with the input because the inflator never
// reads ahead more than it needs to. Currently TINFL_GET_BYTE() pads the end of the stream with 0's in this scenario.
#define TINFL_GET_BYTE(state_index, c) do { \
if (pIn_buf_cur >= pIn_buf_end) { \
for ( ; ; ) { \
if (decomp_flags & TINFL_FLAG_HAS_MORE_INPUT) { \
TINFL_CR_RETURN(state_index, TINFL_STATUS_NEEDS_MORE_INPUT); \
if (pIn_buf_cur < pIn_buf_end) { \
c = *pIn_buf_cur++; \
break; \
} \
} else { \
c = 0; \
break; \
} \
} \
} else c = *pIn_buf_cur++; } TINFL_MACRO_END
#define TINFL_NEED_BITS(state_index, n) do { Bit32u c; TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; } while (num_bits < (Bit32u)(n))
#define TINFL_SKIP_BITS(state_index, n) do { if (num_bits < (Bit32u)(n)) { TINFL_NEED_BITS(state_index, n); } bit_buf >>= (n); num_bits -= (n); } TINFL_MACRO_END
#define TINFL_GET_BITS(state_index, b, n) do { if (num_bits < (Bit32u)(n)) { TINFL_NEED_BITS(state_index, n); } b = bit_buf & ((1 << (n)) - 1); bit_buf >>= (n); num_bits -= (n); } TINFL_MACRO_END
// TINFL_HUFF_BITBUF_FILL() is only used rarely, when the number of bytes remaining in the input buffer falls below 2.
// It reads just enough bytes from the input stream that are needed to decode the next Huffman code (and absolutely no more). It works by trying to fully decode a
// Huffman code by using whatever bits are currently present in the bit buffer. If this fails, it reads another byte, and tries again until it succeeds or until the
// bit buffer contains >=15 bits (deflate's max. Huffman code size).
#define TINFL_HUFF_BITBUF_FILL(state_index, pHuff) \
do { \
temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]; \
if (temp >= 0) { \
code_len = temp >> 9; \
if ((code_len) && (num_bits >= code_len)) \
break; \
} else if (num_bits > TINFL_FAST_LOOKUP_BITS) { \
code_len = TINFL_FAST_LOOKUP_BITS; \
do { \
temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; \
} while ((temp < 0) && (num_bits >= (code_len + 1))); if (temp >= 0) break; \
} TINFL_GET_BYTE(state_index, c); bit_buf |= (((tinfl_bit_buf_t)c) << num_bits); num_bits += 8; \
} while (num_bits < 15);
// TINFL_HUFF_DECODE() decodes the next Huffman coded symbol. It's more complex than you would initially expect because the zlib API expects the decompressor to never read
// beyond the final byte of the deflate stream. (In other words, when this macro wants to read another byte from the input, it REALLY needs another byte in order to fully
// decode the next Huffman code.) Handling this properly is particularly important on raw deflate (non-zlib) streams, which aren't followed by a byte aligned adler-32.
// The slow path is only executed at the very end of the input buffer.
#define TINFL_HUFF_DECODE(state_index, sym, pHuff) do { \
int temp; Bit32u code_len, c; \
if (num_bits < 15) { \
if ((pIn_buf_end - pIn_buf_cur) < 2) { \
TINFL_HUFF_BITBUF_FILL(state_index, pHuff); \
} else { \
bit_buf |= (((tinfl_bit_buf_t)pIn_buf_cur[0]) << num_bits) | (((tinfl_bit_buf_t)pIn_buf_cur[1]) << (num_bits + 8)); pIn_buf_cur += 2; num_bits += 16; \
} \
} \
if ((temp = (pHuff)->m_look_up[bit_buf & (TINFL_FAST_LOOKUP_SIZE - 1)]) >= 0) \
code_len = temp >> 9, temp &= 511; \
else { \
code_len = TINFL_FAST_LOOKUP_BITS; do { temp = (pHuff)->m_tree[~temp + ((bit_buf >> code_len++) & 1)]; } while (temp < 0); \
} sym = temp; bit_buf >>= code_len; num_bits -= code_len; } TINFL_MACRO_END
static const int s_length_base[31] = { 3,4,5,6,7,8,9,10,11,13, 15,17,19,23,27,31,35,43,51,59, 67,83,99,115,131,163,195,227,258,0,0 };
static const int s_length_extra[31]= { 0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0 };
static const int s_dist_base[32] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193, 257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577,0,0};
static const int s_dist_extra[32] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
static const Bit8u s_length_dezigzag[19] = { 16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15 };
static const int s_min_table_sizes[3] = { 257, 1, 4 };
tinfl_status status = TINFL_STATUS_FAILED; Bit32u num_bits, dist, counter, num_extra; tinfl_bit_buf_t bit_buf;
const Bit8u *pIn_buf_cur = pIn_buf_next, *const pIn_buf_end = pIn_buf_next + *pIn_buf_size, *const pIn_buf_end_m_4 = pIn_buf_end - 4;
Bit8u *pOut_buf_cur = pOut_buf_next, *const pOut_buf_end = pOut_buf_next + *pOut_buf_size, *const pOut_buf_end_m_2 = pOut_buf_end - 2;
size_t out_buf_size_mask = (decomp_flags & TINFL_FLAG_USING_NON_WRAPPING_OUTPUT_BUF) ? (size_t)-1 : ((pOut_buf_next - pOut_buf_start) + *pOut_buf_size) - 1, dist_from_out_buf_start;
Bit16s* r_tables_0_look_up = r->m_tables[0].m_look_up;
// Ensure the output buffer's size is a power of 2, unless the output buffer is large enough to hold the entire output file (in which case it doesn't matter).
if (((out_buf_size_mask + 1) & out_buf_size_mask) || (pOut_buf_next < pOut_buf_start)) { *pIn_buf_size = *pOut_buf_size = 0; return TINFL_STATUS_BAD_PARAM; }
num_bits = r->m_num_bits; bit_buf = r->m_bit_buf; dist = r->m_dist; counter = r->m_counter; num_extra = r->m_num_extra; dist_from_out_buf_start = r->m_dist_from_out_buf_start;
TINFL_CR_BEGIN
bit_buf = num_bits = dist = counter = num_extra = 0;
do
{
if (pIn_buf_cur - pIn_buf_next) { TINFL_CR_RETURN(TINFL_STATE_INDEX_BLOCK_BOUNDRY, TINFL_STATUS_HAS_MORE_OUTPUT); }
TINFL_GET_BITS(TINFL_STATE_3, r->m_final, 3); r->m_type = r->m_final >> 1;
if (r->m_type == 0)
{
TINFL_SKIP_BITS(TINFL_STATE_5, num_bits & 7);
for (counter = 0; counter < 4; ++counter) { if (num_bits) TINFL_GET_BITS(TINFL_STATE_6, r->m_raw_header[counter], 8); else TINFL_GET_BYTE(TINFL_STATE_7, r->m_raw_header[counter]); }