forked from Wargus/stargus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpq.cpp
1327 lines (1240 loc) · 37.8 KB
/
mpq.cpp
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
// _________ __ __
// / _____// |_____________ _/ |______ ____ __ __ ______
// \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/
// / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ |
// /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ >
// \/ \/ \//_____/ \/
// ______________________ ______________________
// T H E W A R B E G I N S
// Stratagus - A free fantasy real time strategy game engine
//
/**@name mpq.c - Mpq. */
//
// (c) Copyright 1998-2012 by Lutz Sammer, Jimmy Salmon and Pali Rohár
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 dated June, 1991.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
// 02111-1307, USA.
//
// $Id$
//@{
/*----------------------------------------------------------------------------
-- Includes
----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#ifdef _MSC_VER
#define strdup _strdup
#define DEBUG _DEBUG
#define PATH_MAX _MAX_PATH
#include <direct.h>
#include <io.h>
#else
#include <limits.h>
#include <unistd.h>
#endif
#include <ctype.h>
#include "mpq.h"
#include "huff.h"
/*----------------------------------------------------------------------------
-- Declarations
----------------------------------------------------------------------------*/
#define DCL_NO_ERROR 0L
#define DCL_ERROR_1 1L
#define DCL_ERROR_2 2L
#define DCL_ERROR_3 3L
#define DCL_ERROR_4 4L
#define DEFAULT_LIST "mpqlist.txt"
typedef struct {
UInt8 *buf_in;
UInt8 *buf_out;
} params;
static UInt16 __explode_1(UInt8 * buf, UIntPtr *length_write);
static UInt16 __explode_2(UInt8 * buf);
static UInt16 __explode_3(UInt8 * buf, UInt16 result);
static UInt16 __explode_4(UInt8 * buf, UInt32 flag);
static void __explode_5(UInt16 count, UInt8 * buf_1, const UInt8 * table, UInt8 * buf_2);
static void __explode_6(UInt8 * buf, const UInt8 * table);
static void __explode_7(UInt8 * buf, const UInt8 * table, UInt32 count);
static UInt32 Crc(char *string, UInt32 * massive_base, UInt32 massive_base_offset);
static void Decode(UInt32 * data_in, UInt32 * massive_base, UInt32 crc, UInt32 length);
static UInt16 read_data(UInt8 *buffer, UInt16 size, void *crap);
static void write_data(UInt8 * buffer, UInt16 size, void *crap, UIntPtr *length_write);
UIntPtr ExtWavUnp1(UIntPtr,UIntPtr,UIntPtr,UIntPtr);
UIntPtr ExtWavUnp2(UIntPtr,UIntPtr,UIntPtr,UIntPtr);
UIntPtr ExtWavUnp3(UIntPtr,UIntPtr,UIntPtr,UIntPtr);
UIntPtr Sub_WavUnp13(UIntPtr,UIntPtr,UIntPtr,UIntPtr,UIntPtr);
/*----------------------------------------------------------------------------
-- Variables
----------------------------------------------------------------------------*/
static UInt32 avail_metods[4] = { 0x08, 0x01, 0x40, 0x80 };
extern const unsigned char dcl_table[];
extern const UInt32 small_tbl1[90];
extern const UInt32 small_tbl2[32];
/*----------------------------------------------------------------------------
-- Functions
----------------------------------------------------------------------------*/
/**
** Analyzing archive
*/
int CMpq::ReadInfo(FILE *fpMpq, const char * list)
{
UInt32 mpq_header[2] = { 0x1a51504d, 0x00000020 };
int i, j;
UInt32 detected = 0;
UInt32 tmp, scrc1, scrc2, scrc3, pointer_ht;
FILE *fpList = NULL;
char prnbuf[PATH_MAX+100];
static char name_htable[] = "(hash table)"; // Name of hash table (used to decode hash table)
static char name_btable[] = "(block table)"; // Name of block table (used to decode block table)
while (fread(&tmp, sizeof(UInt32), 1, fpMpq)) {
if (mpq_header[0] == tmp) {
if (fread(&tmp, sizeof(UInt32), 1, fpMpq) == 0)
return -1;
if (mpq_header[1] == tmp) {
detected = 1;
break;
}
}
}
if (!detected) {
printf("Error: File is not valid MPQ archive\n");
return -1;
}
offset_mpq = ftell(fpMpq) - 8;
if (fread(&length_mpq_part, sizeof(UInt32), 1, fpMpq) == 0)
return -1;
fseek(fpMpq, offset_mpq + 16, SEEK_SET);
if (fread(&offset_htbl, sizeof(UInt32), 1, fpMpq) == 0)
return -1;
if (fread(&offset_btbl, sizeof(UInt32), 1, fpMpq) == 0)
return -1;
if (fread(&length_htbl, sizeof(UInt32), 1, fpMpq) == 0)
return -1;
length_htbl *= 4;
if (fread(&length_btbl, sizeof(UInt32), 1, fpMpq) == 0)
return -1;
FileCount = length_btbl;
length_btbl *= 4;
BuildBaseMassive();
if (InitializeLocals()) {
return -2;
}
fseek(fpMpq, offset_mpq + offset_htbl, SEEK_SET);
if (fread(hash_table, sizeof(UInt32), length_htbl, fpMpq) == 0)
return -1;
fseek(fpMpq, offset_mpq + offset_btbl, SEEK_SET);
if (fread(BlockTable, sizeof(UInt32), length_btbl, fpMpq) == 0)
return -1;
tmp = Crc(name_htable, massive_base, 0x300);
Decode(hash_table, massive_base, tmp, length_htbl);
tmp = Crc(name_btable, massive_base, 0x300);
Decode(BlockTable, massive_base, tmp, length_btbl);
if (list)
fpList = fopen(list, "rt");
if (list && !fpList) {
fprintf(stderr, "Error: Can't open specified list: %s\n", list);
return -1;
}
if (!fpList)
fpList = fopen(DEFAULT_LIST, "rt");
if (fpList) {
while (fgets(prnbuf, PATH_MAX, fpList) != 0) {
if (*(prnbuf + strlen(prnbuf) - 1) == '\n') {
*(prnbuf + strlen(prnbuf) - 1) = 0;
}
if (*(prnbuf + strlen(prnbuf) - 1) == '\r') {
*(prnbuf + strlen(prnbuf) - 1) = 0;
}
scrc1 = Crc(prnbuf, massive_base, 0);
scrc2 = Crc(prnbuf, massive_base, 0x100);
scrc3 = Crc(prnbuf, massive_base, 0x200);
pointer_ht = (scrc1 & (length_htbl / 4 - 1)) * 4;
for (; pointer_ht < length_htbl; pointer_ht += 4) {
if ((*(hash_table + pointer_ht) == scrc2)
&& (*(hash_table + pointer_ht + 1) == scrc3)) {
// - fill FilenameTable
int offset = *(hash_table + pointer_ht + 3);
if (offset < 0) {
fprintf(stderr, "Cannot add %s to filename table\n", prnbuf);
continue;
}
sprintf(FilenameTable + PATH_MAX * offset, "%s", prnbuf);
// . fill IdentifyTable
*(IdentifyTable + offset) = 1;
break;
}
}
}
fclose(fpList);
} else {
fprintf(stderr, "Warning: Can't open default list: %s\n", DEFAULT_LIST);
}
j = 1;
for (i = 0; i < FileCount; ++i) { // if there are not identified files, then
if (!*(IdentifyTable + i)) { // fill MpqFilenameTable with "unknow\unknowN
sprintf(FilenameTable + PATH_MAX * i, "unknow\\unk%05d.xxx", j);
++j;
}
}
return 0;
}
/**
** Allocation of memory for hash_table,MpqBlockTable,
** MpqFilenameTable,MpqIdentifyTable and working buffers
** 7to decompress files
*/
int CMpq::InitializeLocals(void)
{
global_buffer = (UInt8 *) malloc(0x60000); // Allocation 384 KB for global_buffer
if (!global_buffer) {
printf("\nError! Insufficient memory");
return -1;
}
read_buffer_start = global_buffer; // 4 KB for read_buffer
write_buffer_start = global_buffer + 0x1000; // 4 KB for write_buffer
explode_buffer = global_buffer + 0x2000; // 16 KB for explode_buffer
file_header = (UInt32 *)(global_buffer + 0x6000); // 360 KB for file_header (max size of unpacked file can't exceed 360 MB)
hash_table = (UInt32 *)malloc(length_htbl * 4);
BlockTable = (UInt32 *)malloc(length_btbl * 4);
FilenameTable = (char *)calloc(length_btbl / 4, sizeof(char) * PATH_MAX);
IdentifyTable = (char *)calloc(length_btbl / 4, sizeof(char));
if (hash_table && BlockTable && FilenameTable && IdentifyTable) {
return 0;
} else {
printf("\nError! Insufficient memory");
return -1;
}
}
/**
**
*/
CMpq::CMpq()
{
global_buffer = NULL;
hash_table = NULL;
BlockTable = NULL;
FilenameTable = NULL;
IdentifyTable = NULL;
}
/**
** Free memory
*/
CMpq::~CMpq()
{
if (global_buffer) {
free(global_buffer);
global_buffer = NULL;
}
if (hash_table) {
free(hash_table);
hash_table = NULL;
}
if (BlockTable) {
free(BlockTable);
BlockTable = NULL;
}
if (FilenameTable) {
free(FilenameTable);
FilenameTable = NULL;
}
if (IdentifyTable) {
free(IdentifyTable);
IdentifyTable = NULL;
}
return;
}
/**
** Fill massive_base
*/
void CMpq::BuildBaseMassive(void)
{
UInt32 s1;
int i, j;
ldiv_t divres;
divres.rem = 0x100001;
for (i = 0; i < 0x100; ++i) {
for (j = 0; j < 5; ++j) {
divres = ldiv(divres.rem * 125 + 3, 0x002AAAAB);
s1 = (divres.rem & 0xFFFFL) << 0x10;
divres = ldiv(divres.rem * 125 + 3, 0x002AAAAB);
s1 = s1 | (divres.rem & 0xFFFFL);
massive_base[i + 0x100 * j] = s1;
}
}
return;
}
/**
** Calculate crc
*/
static UInt32 Crc(char *string, UInt32 *massive_base, UInt32 massive_base_offset)
{
char byte;
UInt32 crc = 0x7fed7fed;
UInt32 s1 = 0xEEEEEEEE;
byte = *string;
while (byte) {
if (byte > 0x60 && byte < 0x7B) {
byte -= 0x20;
}
crc = *(massive_base + massive_base_offset + byte) ^ (crc + s1);
s1 += crc + (s1 << 5) + byte + 3;
string++;
byte = *string;
}
return crc;
}
/**
** (called by explode)
*/
UInt16 read_data(UInt8 *buffer, UInt16 size, void *crap)
{
params *param;
param = (params *)crap;
memcpy(buffer, param->buf_in, size);
param->buf_in += size;
return size;
}
/**
** (called by explode)
*/
void write_data(UInt8 * buffer, UInt16 size, void *crap, UIntPtr *length_write)
{
params *param;
param = (params *)crap;
memcpy(param->buf_out, buffer, size);
param->buf_out += size;
*length_write += size;
}
/**
** Decode data
*/
static void Decode(UInt32 * data_in, UInt32 * massive_base, UInt32 crc, UInt32 length)
{
UInt32 i, dec;
UInt32 s1;
s1 = 0xEEEEEEEE;
for (i = 0; i < length; i++) {
s1 += *(massive_base + 0x400 + (crc & 0xFFL));
dec = *(data_in + i) ^ (s1 + crc);
s1 += dec + (s1 << 5) + 3;
*(data_in + i) = dec;
crc = (crc >> 0x0b) | (0x11111111 + ((crc ^ 0x7FFL) << 0x15));
}
return;
}
/**
** Calculate crc for file without name
*/
UInt32 CMpq::GetUnknowCrc(UInt32 entry, FILE *fpMpq, UInt32 *BlockTable)
{
UInt32 tmp, i, j, coded_dword, crc_file;
UInt32 flag, size_unpack, num_block, offset_body;
UInt32 sign_riff1 = 0x46464952; // 'RIFF'
UInt32 sign_riff3 = 0x45564157; // 'WAVE'
UInt32 sign_mpq1 = 0x1a51504d; // 'MPQ'
UInt32 sign_mpq2 = 0x00000020;
ldiv_t divres;
offset_body = *(BlockTable + entry * 4); // get offset of analized file
flag = *(BlockTable + entry * 4 + 3); // get flag of analized file
fseek(fpMpq, offset_mpq + offset_body, SEEK_SET);
if (fread(&coded_dword, sizeof(UInt32), 1, fpMpq) == 0) // read first coded dword from file
return 0;
if (flag & 0x200 || flag & 0x100) { // IF FILE PACKED:
size_unpack = *(BlockTable + entry * 4 + 2); // . get size of unpacked file
divres = ldiv(size_unpack - 1, 0x1000);
num_block = divres.quot + 2; // . calculate length of file header
for (j = 0; j <= 0xff; j++) { // . now we're gonna find crc_file of 0x100 possible variants
crc_file = ((num_block * 4) ^ coded_dword) - 0xeeeeeeee - *(massive_base + 0x400 + j); // . calculate possible crc
if ((crc_file & 0xffL) == j) { // . IF FIRST CHECK is succesfull - do second one
fseek(fpMpq, offset_mpq + offset_body, SEEK_SET);
if (fread(file_header, sizeof(UInt32), num_block, fpMpq) == 0) // . read file header
return 0;
Decode(file_header, massive_base, crc_file, num_block); // . decode file header with possible crc
tmp = num_block * 4; // . tmp = size header (bytes)
if (tmp == *file_header) { // . IF SECOND CHECK is succesfull - do third one
for (i = 0; i < num_block - 1; i++) {
tmp += *(file_header + i + 1) - *(file_header + i);
if (*(file_header + i + 1) - *(file_header + i) >
0x1000) {
tmp = 0xffffffff;
break;
}
}
if (tmp != 0xffffffff) { // . IF THIRD CHECK is succesfull
crc_file++; // . great! we got right crc_file
break;
}
}
}
crc_file = 0; // . if its impossible to get right crc return 0
}
} else { // IF FILE IS NOT PACKED:
for (j = 0; j <= 0xff; j++) { // Calculate crc as if it was WAV FILE
crc_file = (sign_riff1 ^ coded_dword) - 0xeeeeeeee - *(massive_base + 0x400 + j); // . calculate possible crc
if ((crc_file & 0xff) == j) { // . IF FIRST CHECK is succesfull - do second one
fseek(fpMpq, offset_mpq + offset_body, SEEK_SET);
if (fread(file_header, sizeof(UInt32), 3, fpMpq) == 0) // . read file file_header
return 0;
Decode(file_header, massive_base, crc_file, 3); // . decode file file_header with possible crc
if (sign_riff1 == *file_header) {
if (sign_riff3 == *(file_header + 2)) { // . IF SECOND CHECK is succesfull - we got right crc
break;
}
}
}
crc_file = 0; // . if its impossible to get right crc return 0
}
if (!crc_file) { // Calculate crc as if it was MPQ FILE
for (j = 0; j <= 0xff; j++) {
crc_file =
(sign_mpq1 ^ coded_dword) - 0xeeeeeeee - *(massive_base +
0x400 + j);
if ((crc_file & 0xffL) == j) {
fseek(fpMpq, offset_mpq + offset_body, SEEK_SET);
if (fread(file_header, sizeof(UInt32), 2, fpMpq) == 0)
return 0;
Decode(file_header, massive_base, crc_file, 2);
if (sign_mpq1 == *file_header) {
if (sign_mpq2 == *(file_header + 1)) {
break;
}
}
}
crc_file = 0;
}
}
}
return crc_file;
}
/**
** Extract file from archive
*/
int CMpq::ExtractTo(unsigned char *mpqbuf, UInt32 entry, FILE *fpMpq)
{
UIntPtr size_pack, size_unpack;
UInt8 *read_buffer, *write_buffer;
UIntPtr i, j, offset_body, flag, crc_file;
UIntPtr num_block, length_read, iteration;
char *szNameFile;
UInt8 metod;
ldiv_t divres;
params param;
unsigned char *mpqptr;
mpqptr = mpqbuf;
offset_body = *(BlockTable + entry * 4); // get offset of file in mpq
size_unpack = *(BlockTable + entry * 4 + 2); // get unpacked size of file
flag = *(BlockTable + entry * 4 + 3); // get flags for file
crc_file = 0;
if (flag & 0x30000) { // If file is coded, calculate its crc
if (*(IdentifyTable + entry) & 0x1) { // . Calculate crc_file for identified file:
szNameFile = FilenameTable + PATH_MAX * entry; // . . get name of file
if (strrchr(szNameFile, '\\')) {
szNameFile = strrchr(szNameFile, '\\') + 1;
}
crc_file = Crc(szNameFile, massive_base, 0x300); // . . calculate crc_file (for Diablo I MPQs)
if ((flag & 0x20200) == 0x20200) { // . . if flag indicates Starcraft MPQ
crc_file = (crc_file + offset_body) ^ size_unpack; // . . calculate crc_file (for Starcraft MPQs)
}
} else {
crc_file = GetUnknowCrc(entry, fpMpq, BlockTable); // . calculate crc_file for not identified file:
}
}
if (flag & 0x200 || flag & 0x100) { // IF FILE IS PACKED:
divres = ldiv(size_unpack - 1, 0x1000);
num_block = divres.quot + 2; // . calculate length of file header
fseek(fpMpq, offset_mpq + offset_body, SEEK_SET);
if (fread(file_header, sizeof(UInt32), num_block, fpMpq) == 0) // . read file header
return 1;
if (flag & 0x30000) {
Decode(file_header, massive_base, (crc_file - 1), num_block); // . decode file header (if file is coded)
}
read_buffer = read_buffer_start;
for (j = 0; j < (num_block - 1); j++) {
length_read = *(file_header + j + 1) - *(file_header + j); // . get length of block to read
if (fread(read_buffer, sizeof(char), length_read, fpMpq) == 0) // . read block
return 1;
if (flag & 0x30000) {
Decode((UInt32 *) read_buffer, massive_base, crc_file, length_read / 4); // . decode block (if file is coded)
}
if (length_read == 0x1000 || (j == num_block - 2 && length_read == (size_unpack & 0xFFF))) { // . if block is unpacked (its length=0x1000 or its last block and length=remainder)
memcpy(mpqptr, read_buffer, length_read);
mpqptr += length_read;
// fwrite(read_buffer, sizeof(char), length_read, fp_new); // . write block "as is"
}
else { // . block is packed
if (flag & 0x200) { // . If this file is from Starcraft MPQ (or Diablo 2), then
metod = *read_buffer; // . . first byte determinates metod of packing
iteration = 0;
for (i = 0; i < 4; i++) { // . . calculate number of iterations
if (metod & avail_metods[i]) {
iteration++;
}
}
read_buffer += 1;
length_read -= 1;
} else { // . Else: file is from Diablo I MPQ, then
iteration = 1;
metod = 8; // . .file is compressed with DCL
}
write_buffer = write_buffer_start;
if (metod & 0x08) {
param.buf_in = read_buffer;
param.buf_out = write_buffer;
length_write = 0;
explode(¶m);
length_read = length_write;
iteration--;
if (iteration) {
read_buffer = write_buffer;
write_buffer = read_buffer_start;
}
}
if (metod & 0x01) { // Huffman
length_read =
ExtWavUnp1((UIntPtr) read_buffer,
(UIntPtr) length_read, (UIntPtr) write_buffer,
0x1000);
iteration--;
if (iteration) {
read_buffer = write_buffer;
write_buffer = read_buffer_start;
}
}
if (metod & 0x40) { // Mono ADPCM Wave
length_read =
ExtWavUnp2((UIntPtr) read_buffer,
(UIntPtr) length_read, (UIntPtr) write_buffer,
0x1000);
}
if (metod & 0x80) { // Stereo ADPCM Wave
length_read =
ExtWavUnp3((UIntPtr) read_buffer,
(UIntPtr) length_read, (UIntPtr) write_buffer,
0x1000);
}
memcpy(mpqptr, write_buffer, length_read);
mpqptr += length_read;
// fwrite(write_buffer, 1, length_read, fp_new);
read_buffer = read_buffer_start;
}
crc_file++; // . calculate crc_file for next block
}
}
else { // IF FILE IS NOT PACKED
size_pack = *(BlockTable + entry * 4 + 1); // get size of file
if (flag & 0x30000) {
length_read = 0x1000; // if file is coded, length_read=0x1000 (4 KB)
} else {
length_read = 0x60000; // if file is not coded, length_read=0x60000 (384KB)
}
if (size_pack < length_read) {
length_read = size_pack; // if size of file < length_read, length read = size of file
}
read_buffer = read_buffer_start;
if (length_read) {
divres = ldiv(size_pack, length_read); // .
num_block = divres.quot; // .
} else { // .
num_block = 0; // .
divres.rem = 0; // .
}
fseek(fpMpq, offset_mpq + offset_body, SEEK_SET);
for (j = 0; j < num_block; j++) {
if (fread(read_buffer, 1, length_read, fpMpq) == 0)
return 1;
if (flag & 0x30000) {
Decode((UInt32 *) read_buffer, massive_base, crc_file, length_read / 4); // if file is coded, decode block
crc_file++; // and calculate crc_file for next block
}
memcpy(mpqptr, read_buffer, length_read);
mpqptr += length_read;
// fwrite(read_buffer, 1, length_read, fp_new);
}
if (divres.rem) {
if (fread(read_buffer, 1, divres.rem, fpMpq) == 0)
return 1;
if (flag & 0x30000)
Decode((UInt32 *) read_buffer, massive_base, crc_file,
divres.rem / 4);
memcpy(mpqptr, read_buffer, divres.rem);
mpqptr += divres.rem;
// fwrite(read_buffer, 1, divres.rem, fp_new);
}
}
return 0;
}
/**
**
*/
UInt32 CMpq::explode(void *param)
{
UIntPtr result;
UInt16 read_result;
const UInt8 *table = dcl_table;
UInt8 *work_buff;
work_buff = (UInt8 *) explode_buffer;
*((void **)(work_buff + 0x12)) = param;
*((UInt16 *) (work_buff + 0x0E)) = 0x0800;
read_result = read_data(work_buff + 0x2222, 0x0800, param);
if (read_result == DCL_ERROR_4) {
result = DCL_ERROR_3;
} else {
UInt16 flag_0 = *(work_buff + 0x2222), flag_1 =
*(work_buff + 0x2223), flag_2 = *(work_buff + 0x2224);
*((UInt16 *) (work_buff + 0x02)) = flag_0;
*((UInt16 *) (work_buff + 0x06)) = flag_1;
*((UInt16 *) (work_buff + 0x0A)) = flag_2;
*((UInt16 *) (work_buff + 0x0C)) = 0x00;
*((UInt16 *) (work_buff + 0x0E)) = 0x03;
*((UInt16 *) (work_buff + 0x10)) = read_result;
if ((flag_1 < 0x04) || (flag_1 > 0x06)) {
result = DCL_ERROR_1;
} else {
*((UInt16 *) (work_buff + 0x08)) =
(UInt16) (0x0000FFFFL >> (0x0010 - flag_1));
if (flag_0 > 0x01) {
result = DCL_ERROR_2;
} else {
if (flag_0) {
__explode_7(work_buff + 0x2FA2, table + 0x00D0, 0x0100);
__explode_6(work_buff, table + 0x01D0);
}
__explode_7(work_buff + 0x30E2, table + 0x00B0, 0x0010);
__explode_5(0x0010, work_buff + 0x30E2, table + 0x00C0,
work_buff + 0x2B22);
__explode_7(work_buff + 0x30F2, table + 0x0080, 0x0010);
__explode_7(work_buff + 0x3102, table + 0x0090, 0x0020);
__explode_7(work_buff + 0x30A2, table, 0x0040);
__explode_5(0x0040, work_buff + 0x30A2, table + 0x0040,
work_buff + 0x2A22);
if (__explode_1(work_buff, &length_write) != 0x0306) {
result = DCL_NO_ERROR;
} else {
result = DCL_ERROR_4;
}
}
}
}
// free(work_buff);
return result;
}
/**
**
*/
static UInt16 __explode_1(UInt8 * buf, UIntPtr *length_write)
{
UIntPtr result, temp;
UInt8 *s, *d;
void *param;
*((UInt16 *) (buf + 0x04)) = 0x1000;
while (result = __explode_2(buf), (UInt16) result < 0x0305) {
if ((UInt16) result < 0x0100) {
temp = *((UInt16 *) (buf + 0x04));
*((UInt16 *) (buf + 0x04)) = (UInt16) (temp + 0x01);
*(buf + temp + 0x1E) = (UInt8) result;
} else {
result -= 0x00FE;
s = (UInt8 *) (UIntPtr)__explode_3(buf, (UInt16) result);
if (!s) {
result = 0x0306;
break;
} else {
temp = *((UInt16 *) (buf + 0x04));
d = temp + 0x1E + buf;
*((UInt16 *) (buf + 0x04)) = (UInt16) (temp + result);
s = (UInt8 *) ((UIntPtr) d - (UIntPtr) s);
do {
result--;
*(d++) = *(s++);
} while (result);
}
}
if (*((UInt16 *) (buf + 4)) >= 0x2000) {
result = 0x1000;
param = (void *)*((UIntPtr *) (buf + 0x12));
write_data(buf + 0x101E, 0x1000, param, length_write);
__explode_7(buf + 0x001E, buf + 0x101E,
*((UInt16 *) (buf + 0x04)) - 0x1000);
*((UInt16 *) (buf + 0x04)) -= 0x1000;
}
}
param = (void *)*((UIntPtr *) (buf + 0x12));
write_data(buf + 0x101E,
(UInt16) (*((UInt16 *) (buf + 0x04)) - 0x1000), param, length_write);
return (UInt16) result;
}
/**
**
*/
static UInt16 __explode_2(UInt8 * buf)
{
UIntPtr result, flag, flag_1;
if (*((UInt16 *) (buf + 0x0A)) & 0x01) {
if (__explode_4(buf, 0x01)) {
return 0x0306;
}
result = *(buf + ((UInt8) * ((UInt16 *) (buf + 0x0A))) + 0x2B22);
if (__explode_4(buf, *(buf + ((UInt16) result) + 0x30E2))) {
return 0x0306;
}
flag = *(buf + ((UInt16) result) + 0x30F2);
if (flag) {
flag_1 = (*((UInt16 *) (buf + 0x0A))) & ((0x01 << flag) - 0x01);
if (__explode_4(buf, flag)) {
if ((((UInt16) result) + ((UInt16) flag_1)) != 0x010E) {
return 0x0306;
}
}
result =
*((UInt16 *) (buf + (((UInt16) result) << 0x01) +
0x3102)) + flag_1;
}
result += 0x0100;
} else {
if (__explode_4(buf, 0x01)) {
return 0x0306;
}
if (!*((UInt16 *) (buf + 0x02))) {
result = (UInt8) * ((UInt16 *) (buf + 0x0A));
if (__explode_4(buf, 0x08)) {
return 0x0306;
}
} else {
flag = *((UInt16 *) (buf + 0x0A));
if ((UInt8) flag) {
result = *(buf + ((UInt8) flag) + 0x2C22);
if (((UInt16) result) == 0x00FF) {
if (flag & 0x003F) {
if (__explode_4(buf, 0x04)) {
return 0x0306;
}
result =
*(buf + ((UInt8) * ((UInt16 *) (buf + 0x0A))) +
0x2D22);
} else {
if (__explode_4(buf, 0x06)) {
return 0x0306;
}
result =
*(buf + ((*((UInt16 *) (buf + 0x0A))) & 0x007F) +
0x2E22);
}
}
} else {
if (__explode_4(buf, 0x08)) {
return 0x0306;
}
result =
*(buf + ((UInt8) * ((UInt16 *) (buf + 0x0A))) +
0x2EA2);
}
flag = *(buf + ((UInt16) result) + 0x2FA2);
if (__explode_4(buf, flag)) {
return 0x0306;
}
}
}
return (UInt16) result;
}
/**
**
*/
static UInt16 __explode_3(UInt8 * buf, UInt16 flag)
{
UIntPtr result, flag_1;
result = *(buf + ((UInt8) * ((UInt16 *) (buf + 0x0A))) + 0x2A22);
if (__explode_4(buf, *(buf + ((UInt16) result) + 0x30A2))) {
return 0x00;
}
if (((UInt16) flag) == 0x02) {
result <<= 0x02;
result |= *((UInt16 *) (buf + 0x0A)) & 0x03;
if (__explode_4(buf, 0x02)) {
return 0x00;
}
} else {
flag_1 = *((UInt16 *) (buf + 0x06));
result <<= flag_1;
result |= *((UInt16 *) (buf + 0x08)) & *((UInt16 *) (buf + 0x0A));
if (__explode_4(buf, flag_1)) {
return 0x00;
}
}
return (UInt16) (result + 0x01);
}
/**
**
*/
static UInt16 __explode_4(UInt8 * buf, UInt32 flag)
{
UIntPtr result;
UInt16 read_result;
void *param = (void *)*((UIntPtr *) (buf + 0x12));
result = *((UInt16 *) (buf + 0x0C));
if ((UInt16) flag <= result) {
*((UInt16 *) (buf + 0x0A)) >>= flag;
*((UInt16 *) (buf + 0x0C)) -= (UInt16) flag;
result = 0x00;
} else {
*((UInt16 *) (buf + 0x0A)) >>= result;
result = *((UInt16 *) (buf + 0x0E));
if (result == *((UInt16 *) (buf + 0x10))) {
*((UInt16 *) (buf + 0x0E)) = 0x0800;
read_result = read_data(buf + 0x2222, 0x0800, param);
*((UInt16 *) (buf + 0x10)) = read_result;
if (!read_result) {
return 0x01;
}
*((UInt16 *) (buf + 0x0E)) = 0x00;
}
result = *((UInt16 *) (buf + 0x0E)) + 0x2222;
*((UInt16 *) (buf + 0x0A)) |= *(buf + result) << 0x08;
*((UInt16 *) (buf + 0x0E)) += 0x01;
*((UInt16 *) (buf + 0x0A)) >>= flag - *((UInt16 *) (buf + 0x0C));
*((UInt16 *) (buf + 0x0C)) =
(UInt16) (0x08 - (flag - *((UInt16 *) (buf + 0x0C))));
result = 0x00;
}
return (UInt16) result;
}
/**
**
*/
static void __explode_5(UInt16 count, UInt8 * buf_1, const UInt8 * table,
UInt8 * buf_2)
{
SInt16 i = (SInt16) (count - 1);
UIntPtr idx_1, idx_2;
for (; i >= 0; i--) {
idx_1 = *(table + i);
idx_2 = 0x01 << *(buf_1 + i);
for (;;) {
*(buf_2 + (UInt16) idx_1) = (UInt8) i;
idx_1 += idx_2;
if ((UInt16) idx_1 >= 0x0100) {
break;
}
}
}
}
/**
**
*/
static void __explode_6(UInt8 * buf, const UInt8 * table)
{
SInt16 i;
UIntPtr idx_1, idx_2;
for (i = 0x00FF; i >= 0; i--) {
idx_1 = *(buf + i + 0x2FA2);
if (idx_1 <= 0x08) {
idx_2 = *((UInt16 *) (table + (i << 0x01)));
idx_1 = 0x01 << idx_1;
do {
*(buf + idx_2 + 0x2C22) = (UInt8) i;
idx_2 += idx_1;
} while ((UInt16) idx_2 < 0x0100);
} else {
idx_2 = *((UInt16 *) (table + (i << 0x01)));
if ((UInt8) idx_2) {
*(buf + (UInt8) idx_2 + 0x2C22) = 0xFF;
if (*((UInt16 *) (table + (i << 0x01))) & 0x003F) {
*(buf + i + 0x2FA2) -= 0x04;
idx_1 = 0x01 << *(buf + i + 0x2FA2);
idx_2 = *((UInt16 *) (table + (i << 0x01))) >> 0x04;
do {
*(buf + idx_2 + 0x2D22) = (UInt8) i;
idx_2 += idx_1;
} while ((UInt16) idx_2 < 0x0100);
} else {
*(buf + i + 0x2FA2) -= 0x06;
idx_1 = 0x01 << *(buf + i + 0x2FA2);
idx_2 = *((UInt16 *) (table + (i << 0x01))) >> 0x06;
do {
*(buf + idx_2 + 0x2E22) = (UInt8) i;
idx_2 += idx_1;
} while ((UInt16) idx_2 < 0x0080);
}
} else {
*(buf + i + 0x2FA2) -= 0x08;
idx_1 = 0x01 << *(buf + i + 0x2FA2);
idx_2 = *((UInt16 *) (table + (i << 0x01))) >> 0x08;
do {
*(buf + idx_2 + 0x2EA2) = (UInt8) i;
idx_2 += idx_1;
} while ((UInt16) idx_2 < 0x0100);
}
}
}
}
/**
**
*/
static void __explode_7(UInt8 *buf, const UInt8 *table, UInt32 count)
{
UIntPtr half_count;
UInt8 *buf_end;
half_count = count >> 0x02;
if (half_count) {
buf_end = buf + (half_count << 0x02);
do {
*((UInt32 *) buf) = *((UInt32 *) table);
buf += 4;
table += 4;
} while (buf < buf_end);
}
switch (count - (half_count << 0x02)) {
case 3:
*(buf++) = *(table++);
case 2:
*(buf++) = *(table++);
case 1:
*buf = *table;
default:
break;
}
}
/**
**
*/
UIntPtr ExtWavUnp3(UIntPtr buf_in, UIntPtr size_in, UIntPtr buf_out, UIntPtr size_out)
{
return Sub_WavUnp13(buf_in, size_in, 2, buf_out, size_out);
}
/**
**
*/
UIntPtr ExtWavUnp2(UIntPtr buf_in, UIntPtr size_in, UIntPtr buf_out, UIntPtr size_out)
{
return Sub_WavUnp13(buf_in, size_in, 1, buf_out, size_out);
}