forked from hz86/filepack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilepack31.c
1285 lines (1122 loc) · 26.8 KB
/
filepack31.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//by 銀の契约者
//github https://github.com/hz86/filepack
#include <io.h>
#include <tchar.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <mmintrin.h>
#include <direct.h>
#include <locale.h>
#include <time.h>
typedef struct PACKHEAD {
unsigned char signature[16];
unsigned int entry_count;
unsigned int entry_offset;
unsigned int unknown1;
} PACKHEAD;
typedef struct PACKKEY {
unsigned char signature[32];
unsigned int hash_size;
unsigned char key[1024];
} PACKKEY;
typedef struct PACKHASH {
unsigned char signature[16];
unsigned int table_size;
unsigned int file_count;
unsigned int index_size;
unsigned int data_size;
unsigned int is_compressed;
unsigned char unknown1[32];
} PACKHASH;
typedef struct PACKENTRY {
unsigned int offset;
unsigned int unknown1;
unsigned int length;
unsigned int original_length;
unsigned int is_compressed;
unsigned int is_obfuscated;
unsigned int hash;
} PACKENTRY;
////////////反编译出来的代码-开始//////////////////
//计算hash
static unsigned int sub_4E2578_gethash(unsigned char *data, unsigned int len)
{
unsigned int result;
__m64 *v3;
unsigned int v4;
__m64 v5;
__m64 v6;
__m64 v7;
__m64 v8;
__m64 v9;
if (len >= 8)
{
v3 = (__m64 *)data;
v4 = len >> 3;
v5.m64_u64 = 0i64;
v6.m64_u64 = 0i64;
v7 = _mm_cvtsi32_si64(0xA35793A7);
v8 = _m_punpckldq(v7, v7);
do
{
v6 = _m_paddw(v6, v8);
v9 = _m_paddw(v5, _m_pxor(*v3, v6));
v5 = _m_por(_m_pslld(v9, _mm_cvtsi32_si64(3u)), _m_psrld(v9, _mm_cvtsi32_si64(29u)));
++v3;
--v4;
} while (v4);
result = _mm_cvtsi64_si32(_m_pmaddwd(v5, _m_psrlq(v5, _mm_cvtsi32_si64(32u))));
_m_empty();
}
else
{
result = 0;
}
return result;
}
//解密算法
static void sub_4E182C_uncrypt(unsigned int key, unsigned char *data, unsigned int len)
{
unsigned int v2;
__m64 *v3;
__m64 v4;
__m64 v5;
__m64 v6;
__m64 v7;
__m64 v8;
__m64 v9;
if (len >> 3)
{
v2 = len >> 3;
v3 = (__m64 *)data;
v4 = _mm_cvtsi32_si64(0xA73C5F9D);
v5 = _m_punpckldq(v4, v4);
v6 = _mm_cvtsi32_si64(0xCE24F523);
v7 = _m_punpckldq(v6, v6);
v8 = _mm_cvtsi32_si64((key + len) ^ 0xFEC9753E);
v9 = _m_punpckldq(v8, v8);
do
{
v5 = _m_pxor(_m_paddd(v5, v7), v9);
v9 = _m_pxor(*v3, v5);
*v3 = v9;
++v3;
--v2;
} while (v2);
_m_empty();
}
}
//加密算法
static void sub_4E182C_encrypt(unsigned int key, unsigned char *data, unsigned int len)
{
unsigned int v2;
__m64 *v3;
__m64 v4;
__m64 v5;
__m64 v6;
__m64 v7;
__m64 v8;
__m64 v9;
__m64 v10;
if (len >> 3)
{
v2 = len >> 3;
v3 = (__m64 *)data;
v4 = _mm_cvtsi32_si64(0xA73C5F9D);
v5 = _m_punpckldq(v4, v4);
v6 = _mm_cvtsi32_si64(0xCE24F523);
v7 = _m_punpckldq(v6, v6);
v8 = _mm_cvtsi32_si64((key + len) ^ 0xFEC9753E);
v9 = _m_punpckldq(v8, v8);
do
{
v5 = _m_pxor(_m_paddd(v5, v7), v9);
v9 = _m_pxor(*v3, v5);
v10 = *v3;
*v3 = v9;
v9 = v10;
++v3;
--v2;
} while (v2);
_m_empty();
}
}
//加密/解密算法
static void sub_4E11C3_crypt(unsigned int key, unsigned char *data, unsigned int len)
{
key = key ^ ((key >> 0x10) & 0x0FFFF);
key = (key ^ (len ^ 0x3E13) ^ (len * len)) & 0x0FFFF;
if ((int)(len - 1) >= 0)
{
unsigned int v1 = 0;
unsigned int v2 = key;
unsigned short *v3 = (unsigned short *)data;
do {
v2 = (v1 + (v2 << 3) + key) & 0xffff;
*v3 = *v3 ^ v2;
v3++;
++v1;
--len;
} while (len);
}
}
//创建key
static void sub_4E8E28_createkey(unsigned int *outkey, unsigned int keylen, unsigned int key)
{
unsigned int v4;
unsigned int v5;
unsigned int *v6;
v4 = key;
v5 = keylen;
v6 = outkey;
do
{
v4 = (2381452337u * (unsigned __int64)(v4 ^ 0x8DF21431) >> 32) + -1913514959 * (v4 ^ 0x8DF21431);
*v6 = v4;
++v6;
--v5;
} while (v5);
}
//创建key
static void sub_4E8E64_createkey(unsigned int *outkey, unsigned int keylen, unsigned short *name, unsigned int namelen, unsigned int datalen, unsigned int key)
{
unsigned int v3;
unsigned int v4;
unsigned int v5;
unsigned int v6;
unsigned int v7;
unsigned int v10;
v3 = 8779058;
v4 = 3405377;
v5 = namelen;
if ((int)(v5 - 1) >= 0)
{
v6 = v5;
v7 = 0;
do
{
v3 += *(unsigned short *)((unsigned char *)name + 2 * (v7 + 1) - 2) << (v7 & 7);
v4 ^= v3;
++v7;
--v6;
} while (v6);
}
v10 = (key ^ (7 * (datalen & 0xFFFFFF) + datalen + v3 + (v3 ^ datalen ^ 0x8F32DC))) + v4;
sub_4E8E28_createkey(outkey, keylen, 9 * (v10 & 0xFFFFFF));
}
//解密算法
static void sub_4E9014_uncrypt(unsigned char *data, unsigned int datalen, unsigned int *key)
{
unsigned int v3;
unsigned int v4;
__m64 *v5;
__m64 v6;
__m64 v7;
__m64 v8;
unsigned char *v10;
unsigned int v11;
__m64 *v13;
int v15;
v15 = datalen >> 3;
if (v15)
{
v10 = (unsigned char *)key;
v13 = (__m64 *)data;
v3 = v15;
v11 = *(unsigned int *)&v10[52];
v4 = 8 * (v11 & 0xF);
v5 = v13;
v6 = *(__m64 *)&v10[24];
do
{
v7 = _m_paddd(_m_pxor(v6, *(__m64 *)&v10[v4]), *(__m64 *)&v10[v4]);
v8 = _m_pxor(*v5, v7);
*v5 = v8;
v6 = _m_paddw(_m_pslld(_m_pxor(_m_paddb(v7, v8), v8), _mm_cvtsi32_si64(1u)), v8);
++v5;
v4 = (v4 + 8) & 0x7F;
--v3;
} while (v3);
_m_empty();
}
}
//加密算法
static void sub_4E9014_encrypt(unsigned char *data, unsigned int datalen, unsigned int *key)
{
unsigned int v3;
unsigned int v4;
__m64 *v5;
__m64 v6;
__m64 v7;
__m64 v8;
unsigned char *v10;
unsigned int v11;
__m64 *v13;
int v15;
__m64 v16;
v15 = datalen >> 3;
if (v15)
{
v10 = (unsigned char *)key;
v13 = (__m64 *)data;
v3 = v15;
v11 = *(unsigned int *)&v10[52];
v4 = 8 * (v11 & 0xF);
v5 = v13;
v6 = *(__m64 *)&v10[24];
do
{
v7 = _m_paddd(_m_pxor(v6, *(__m64 *)&v10[v4]), *(__m64 *)&v10[v4]);
v8 = _m_pxor(*v5, v7);
v16 = *v5;
*v5 = v8;
++v5;
v6 = _m_paddw(_m_pslld(_m_pxor(_m_paddb(v7, v16), v16), _mm_cvtsi32_si64(1u)), v16);
v4 = (v4 + 8) & 0x7F;
--v3;
} while (v3);
_m_empty();
}
}
//创建key
static void sub_4E90FC_createkey(unsigned int *outkey, unsigned int keylen, unsigned int key)
{
unsigned int v4;
unsigned int v5;
unsigned int *v6;
v4 = key;
v5 = keylen;
v6 = outkey;
do
{
v4 = (2323117171u * (unsigned __int64)(v4 ^ 0x8A77F473) >> 32) + -1971850125 * (v4 ^ 0x8A77F473);
*v6 = v4;
++v6;
--v5;
} while (v5);
}
//创建key
static void sub_4E9138_createkey(unsigned int *outkey, unsigned int keylen, unsigned short *name, unsigned int namelen, unsigned int datalen, unsigned int key)
{
unsigned int v3;
unsigned int v4;
unsigned int v5;
unsigned int v6;
unsigned int v7;
unsigned int v10;
v3 = 8845282;
v4 = 4470769;
v5 = namelen;
if ((int)(v5 - 1) >= 0)
{
v6 = v5;
v7 = 0;
do
{
v3 += *(unsigned short *)((unsigned char *)name + 2 * (v7 + 1) - 2) << (v7 & 7);
v4 ^= v3;
++v7;
--v6;
} while (v6);
}
v10 = (v4 + (key ^ (13 * (datalen & 0xFFFFFF) + datalen + v3 + (v3 ^ datalen ^ 0x56E213))));
sub_4E90FC_createkey(outkey, keylen, 13 * (v10 & 0xFFFFFF));
}
//解密算法
static void sub_4E936D_uncrypt(unsigned char *data, unsigned int datalen, unsigned int *key, unsigned int *key2)
{
unsigned int v2;
unsigned int v3;
unsigned char *v5;
__m64 *v4;
__m64 v6;
__m64 v7;
__m64 v8;
__m64 v9;
unsigned char *v11;
unsigned int v13;
unsigned char *v14;
__m64 *v16;
unsigned int v18;
v18 = datalen >> 3;
if (v18)
{
v11 = (unsigned char *)key;
v16 = (__m64 *)data;
v14 = (unsigned char *)key2;
v13 = *(unsigned int *)&v11[32];
v2 = v18;
v3 = 8 * (v13 & 0xD);
v4 = v16;
v5 = v14;
v6 = *(__m64 *)&v11[24];
do
{
v7 = _m_pxor(*(__m64 *)&v11[8 * (v3 & 0xF)], *(__m64 *)&v5[8 * (v3 & 0x7F)]);
v8 = _m_paddd(_m_pxor(v6, v7), v7);
v9 = _m_pxor(*v4, v8);
*v4 = v9;
v6 = _m_paddw(_m_pslld(_m_pxor(_m_paddb(v8, v9), v9), _mm_cvtsi32_si64(1u)), v9);
++v4;
v3 = (v3 + 1) & 0x7F;
--v2;
} while (v2);
_m_empty();
}
}
//加密算法
static void sub_4E936D_encrypt(unsigned char *data, unsigned int datalen, unsigned int *key, unsigned int *key2)
{
unsigned int v2;
unsigned int v3;
unsigned char *v5;
__m64 *v4;
__m64 v6;
__m64 v7;
__m64 v8;
__m64 v9;
unsigned char *v11;
unsigned int v13;
unsigned char *v14;
__m64 *v16;
unsigned int v18;
__m64 v19;
v18 = datalen >> 3;
if (v18)
{
v11 = (unsigned char *)key;
v16 = (__m64 *)data;
v14 = (unsigned char *)key2;
v13 = *(unsigned int *)&v11[32];
v2 = v18;
v3 = 8 * (v13 & 0xD);
v4 = v16;
v5 = v14;
v6 = *(__m64 *)&v11[24];
do
{
v7 = _m_pxor(*(__m64 *)&v11[8 * (v3 & 0xF)], *(__m64 *)&v5[8 * (v3 & 0x7F)]);
v8 = _m_paddd(_m_pxor(v6, v7), v7);
v9 = _m_pxor(*v4, v8);
v19 = *v4;
*v4 = v9;
++v4;
v6 = _m_paddw(_m_pslld(_m_pxor(_m_paddb(v8, v19), v19), _mm_cvtsi32_si64(1u)), v19);
v3 = (v3 + 1) & 0x7F;
--v2;
} while (v2);
_m_empty();
}
}
//创建key
static void sub_4E9ECC_createkey(unsigned int *outkey, unsigned char *data, unsigned int datalen)
{
unsigned int v4;
unsigned int *v5;
unsigned int v6;
unsigned int v8;
unsigned char *v9;
unsigned int v10;
unsigned char v11;
unsigned int v13;
v5 = outkey;
v6 = 0;
do
{
if (v6 % 3)
{
v4 = (v6 + 7) * -((int)v6 + 3);
*v5 = v4;
}
else
{
v4 = (v6 + 7) * (v6 + 3);
*v5 = v4;
}
++v5;
++v6;
}
while (v6 != 256);
v8 = *(unsigned char *)(data + 49) % 0x49u + 128;
v13 = *(unsigned char *)(data + 79) % 7u + 7;
v9 = (unsigned char *)outkey;
v10 = 1024;
do
{
v8 = (unsigned int)(v13 + v8) % datalen;
v11 = *(unsigned char *)(v8 + data);
*v9++ ^= v11;
--v10;
} while (v10);
}
//计算hash
int sub_4E3178_get_hash(wchar_t *name, unsigned int namelen)
{
unsigned int v2;
unsigned int v3;
unsigned int v4;
unsigned char *v10;
v2 = 0;
v3 = namelen;
v10 = (unsigned char *)name;
if (v3 > 0)
{
v4 = 1;
do
{
v2 = ((*(unsigned short *)(v10 + 2 * v4 - 2) << (v4 & 7)) + v2) & 0x3FFFFFFF;
++v4;
--v3;
} while (v3);
}
return v2;
}
//得到hashver-data位置
int sub_4E760C_getpos(int hash, int count)
{
unsigned int v3;
int v4;
v3 = hash;
v4 = (unsigned short)v3 + (v3 >> 8) + (v3 >> 16);
return v4 % count;
}
////////////反编译出来的代码-结束//////////////////
////////////来自 exfp3 部分代码-开始//////////////////
static const unsigned char BPE_FLAG_SHORT_LENGTH = 0x01;
typedef struct bpe_hdr_t {
unsigned char signature[4]; // "1PC\xFF"
unsigned int flags;
unsigned int original_length;
} bpe_hdr_t;
typedef struct bpe_pair_t {
unsigned char left;
unsigned char right;
} bpe_pair_t;
// byte pair encoding algorithm
static void unbpe(unsigned char* buff, unsigned int len,
unsigned char* out_buff, unsigned int out_len)
{
unsigned char* end = buff + len;
bpe_hdr_t* hdr = (bpe_hdr_t*)buff;
buff += sizeof(*hdr);
bpe_pair_t table[256];
unsigned char stack[4096];
unsigned int stack_len = 0;
while (buff < end)
{
// Read/expand literal table
for (unsigned int i = 0; i < 256;)
{
unsigned int c = *buff++;
// Range of literals
if (c > 127)
{
for (c -= 127; c > 0; c--, i++) {
table[i].left = (unsigned char)i;
}
}
// Pairs of {left, right} unless left is a literal
for (c++; c > 0 && i < 256; c--, i++)
{
table[i].left = *buff++;
if (i != table[i].left) {
table[i].right = *buff++;
}
}
}
unsigned int block_len = 0;
// This optional int length is the only difference from the BPE reference
// implementation...
if (hdr->flags & BPE_FLAG_SHORT_LENGTH) {
block_len = *(unsigned short*)buff;
buff += sizeof(unsigned short);
}
else {
block_len = *(unsigned int *)buff;
buff += sizeof(unsigned int);
}
// Decompress block
while (block_len || stack_len)
{
unsigned char c = 0;
if (stack_len) {
c = stack[--stack_len];
}
else {
c = *buff++;
block_len--;
}
if (c == table[c].left) {
*out_buff++ = c;
}
else {
stack[stack_len++] = table[c].right;
stack[stack_len++] = table[c].left;
}
}
}
}
////////////来自 exfp3 部分代码-结束//////////////////
//读入文件
static unsigned char * get_file(wchar_t *file, unsigned int *len)
{
unsigned char *ret = NULL;
FILE *fp = _wfopen(file, L"rb");
if (fp)
{
fseek(fp, 0, SEEK_END);
unsigned int fplen = ftell(fp);
fseek(fp, 0, SEEK_SET);
ret = (unsigned char *)malloc(fplen);
fread(ret, 1, fplen, fp);
*len = fplen;
fclose(fp);
}
return ret;
}
//写出文件
static void put_file(wchar_t *file, unsigned char *data, unsigned int len)
{
wchar_t dir[1024];
wchar_t *pos = file;
while (1)
{
pos = wcschr(pos, '\\');
if (NULL == pos)
{
break;
}
wcsncpy(dir, file, pos - file);
dir[pos - file] = 0;
_wmkdir(dir);
pos++;
}
FILE *fp = _wfopen(file, L"wb");
fwrite(data, 1, len, fp);
fclose(fp);
}
//通配符比较
int match_with_asterisk(wchar_t* str1, wchar_t* pattern)
{
if (str1 == NULL) {
return -1;
}
if (pattern == NULL) {
return -1;
}
int len1 = wcslen(str1);
int len2 = wcslen(pattern);
int p1 = 0, p2 = 0;
//用于分段标记,'*'分隔的字符串
int mark = 0;
while (p1 < len1 && p2 < len2)
{
if (pattern[p2] == '?')
{
p1++; p2++;
continue;
}
if (pattern[p2] == '*')
{
/*如果当前是*号,则mark前面一部分已经获得匹配,
*从当前点开始继续下一个块的匹配
*/
p2++;
mark = p2;
continue;
}
if (str1[p1] != pattern[p2])
{
if (p1 == 0 && p2 == 0)
{
/*
* 如果是首字符,特殊处理,不相同即匹配失败
*/
return -1;
}
/*
* pattern: ...*bdef*...
* ^
* mark
* ^
* p2
* ^
* new p2
* str1:.....bdcf...
* ^
* p1
* ^
* new p1
* 如上示意图所示,在比到e和c处不想等
* p2返回到mark处,
* p1需要返回到下一个位置。
* 因为*前已经获得匹配,所以mark打标之前的不需要再比较
*/
p1 -= p2 - mark - 1;
p2 = mark;
continue;
}
/*
* 此处处理相等的情况
*/
p1++;
p2++;
}
if (p2 == len2)
{
if (p1 == len1)
{
/*
* 两个字符串都结束了,说明模式匹配成功
*/
return 0;
}
if (pattern[p2 - 1] == '*')
{
/*
* str1还没有结束,但pattern的最后一个字符是*,所以匹配成功
*
*/
return 0;
}
}
while (p2 < len2)
{
/*
* pattern多出的字符只要有一个不是*,匹配失败
*
*/
if (pattern[p2] != '*') {
return -1;
}
p2++;
}
return -1;
}
//枚举文件
static void enum_file(wchar_t *in_path, wchar_t *findname, wchar_t **out_filename[], int *out_size)
{
wchar_t filename[1024];
wcscat(wcscpy(filename, in_path), L"\\*");
struct _wfinddata64_t data;
intptr_t handle = _wfindfirst64(filename, &data);
if (-1 != handle)
{
do
{
if (0 == wcscmp(L".", data.name) || 0 == wcscmp(L"..", data.name)) {
continue;
}
if (_A_SUBDIR == (data.attrib & _A_SUBDIR)) {
wcscat(wcscat(wcscpy(filename, in_path), L"\\"), data.name);
enum_file(filename, findname, out_filename, out_size);
}
else
{
if (0 == match_with_asterisk(data.name, findname))
{
if (NULL == *out_filename) {
*out_filename = (wchar_t **)malloc(sizeof(wchar_t *) * (*out_size + 1));
}
else {
*out_filename = (wchar_t **)realloc(*out_filename, sizeof(wchar_t *) * (*out_size + 1));
}
(*out_filename)[*out_size] = (wchar_t *)malloc(wcslen(in_path) * 2 + 2 + wcslen(data.name) * 2 + 2);
wcscat(wcscat(wcscpy((*out_filename)[*out_size], in_path), L"\\"), data.name);
(*out_size)++;
}
}
}
while (0 == _tfindnext64(handle, &data));
_findclose(handle);
}
}
//链表成员
typedef struct _MLIST_ENTRY {
struct _MLIST_ENTRY *back;
struct _MLIST_ENTRY *next;
} MLIST_ENTRY;
//链表头
typedef struct _MLIST_HEADER {
MLIST_ENTRY *head;
int depth;
} MLIST_HEADER;
//链表初始化
void mlist_head_init(MLIST_HEADER *list_head)
{
list_head->depth = 0;
list_head->head = NULL;
}
//压入成员
int mlist_entry_push(MLIST_HEADER *list_head, MLIST_ENTRY *list_entry, int way)
{
if (NULL == list_head->head) {
list_entry->next = list_entry->back = list_entry;
list_head->head = list_entry;
}
else
{
if (way < 0)
{
list_head->head->back->next = list_entry;
list_entry->back = list_head->head->back;
list_entry->next = list_head->head;
list_head->head->back = list_entry;
list_head->head = list_entry;
}
else
{
list_head->head->back->next = list_entry;
list_entry->back = list_head->head->back;
list_entry->next = list_head->head;
list_head->head->back = list_entry;
}
}
return ++list_head->depth;
}
//弹出成员
MLIST_ENTRY * mlist_entry_pop(MLIST_HEADER *list_head, int way)
{
MLIST_ENTRY *ret = NULL;
if (list_head->depth > 0)
{
if (way < 0)
{
ret = list_head->head;
ret->back->next = ret->next;
ret->next->back = ret->back;
list_head->head = ret->next;
}
else
{
ret = list_head->head->back;
ret->back->next = ret->next;
list_head->head->back = ret->back;
}
--list_head->depth;
if (0 == list_head->depth) {
list_head->head = NULL;
}
}
return ret;
}
//清空
MLIST_ENTRY * mlist_flush(MLIST_HEADER *list_head)
{
MLIST_ENTRY *head;
list_head->depth = 0;
head = list_head->head;
list_head->head = NULL;
return head;
}
////////////下面是 解包 / 打包代码 //////////////////
//解包
static int file_unpack(wchar_t *in_file, wchar_t *out_path)
{
wprintf(L"unpack file %s...\r\n", in_file);
FILE *fp = _wfopen(in_file, L"rb");
if (fp)
{
fseek(fp, 0, SEEK_END);
unsigned int fplen = ftell(fp);
PACKHEAD pack_head;
fseek(fp, fplen - sizeof(pack_head), SEEK_SET);
fread(&pack_head, 1, sizeof(pack_head), fp);
if (0 == memcmp(pack_head.signature, "FilePackVer3.1\x00\x00", 16))
{
PACKKEY pack_key;
fseek(fp, fplen - sizeof(pack_key) - sizeof(pack_head), SEEK_SET);
fread(&pack_key, 1, sizeof(pack_key), fp);
PACKHASH *pack_hash = (PACKHASH *)malloc(pack_key.hash_size);
fseek(fp, fplen - sizeof(pack_key) - sizeof(pack_head) - pack_key.hash_size, SEEK_SET);
fread(pack_hash, 1, pack_key.hash_size, fp);
//解密hash结构内的数据
unsigned int hash_datalen = pack_hash->data_size;
unsigned char *hash_data = (unsigned char *)pack_hash + sizeof(PACKHASH);
sub_4E182C_uncrypt(0x0428, hash_data, hash_datalen);
//解压hash_data
if (pack_hash->is_compressed)
{
unsigned int temp_len = ((bpe_hdr_t *)hash_data)->original_length;
unsigned char *temp = (unsigned char *)malloc(temp_len);
unbpe(hash_data, hash_datalen, temp, temp_len);
free(hash_data);
hash_datalen = temp_len;
hash_data = temp;
}
//根据key算出解密用的值
int key = sub_4E2578_gethash(pack_key.key, 256) & 0x0FFFFFFF;
sub_4E182C_uncrypt(key, pack_key.signature, sizeof(pack_key.signature)); //解密
if (0 == strncmp((char *)pack_key.signature, "8hr48uky,8ugi8ewra4g8d5vbf5hb5s6", 32))
{
unsigned int commkey[256];
fseek(fp, pack_head.entry_offset, SEEK_SET);
for (unsigned int i = 0; i < pack_head.entry_count; i++)
{
//取得文件名
unsigned short namelen;
fread(&namelen, 1, sizeof(namelen), fp);
wchar_t *name = (wchar_t *)malloc(namelen * 2 + 2);
fread(name, 1, namelen * 2, fp);
name[namelen] = 0;
//取文件信息
PACKENTRY entry;
fread(&entry, 1, sizeof(entry), fp);
//解密文件名
sub_4E11C3_crypt(key, (unsigned char *)name, namelen);
wprintf(L"%s...", name);
unsigned int cur_pos = ftell(fp);
fseek(fp, entry.offset, SEEK_SET);
//读取文件
unsigned int datalen = entry.length;
unsigned char *data = (unsigned char *)malloc(datalen);
fread(data, 1, datalen, fp);
//校验
if (entry.hash == sub_4E2578_gethash(data, datalen))
{
if (entry.is_obfuscated > 0)
{
if (1 == entry.is_obfuscated)
{
//解密算法1
unsigned int filekey[64];
sub_4E8E64_createkey(filekey, 64, (unsigned short *)name, namelen, datalen, key);
sub_4E9014_uncrypt(data, datalen, filekey);
//特殊文件 pack_keyfile_kfueheish15538fa9or.key 算出一个key
if (0 == wcsncmp(L"pack_keyfile_kfueheish15538fa9or.key", name, namelen)) {
sub_4E9ECC_createkey(commkey, data, datalen);