-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathUNZIP.PAS
2684 lines (2606 loc) · 58.8 KB
/
UNZIP.PAS
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
Unit Unzip;
{$I DEF.INC}
{$IFNDEF __Windows__}
{$Define assembler}
{$ENDIF}
{ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
INTERFACE
{ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
Uses
Systex;
Type
PLongint=^Longint;
Const
TFileBufferSize=High(Word)-16;
TFileNameSize=79;
Type
BufType=Array[0..TFileBufferSize]of Char;
TDirType=Array[0..TFileNameSize]of Char;
TZipRec=Record
Buf:^BufType;
BufSize,
LocalStart:Word;
Time:LongInt;
Size:LongInt;
CompressSize:LongInt;
HeaderOffset:LongInt;
FileName:TDirType;
PackMethod:Word;
Attr:Byte;
DirFile:File;
End;
PReportRec=^TReportRec;
TReportRec=Record
FileName:TDirType;
Time:LongInt;
Size:LongInt;
CompressSize:LongInt;
Attr:Integer;
PackMethod:Word;
Ratio:Byte;
Status:LongInt;
IsaDir:Boolean;
End;
Const
fileStarting=-1000;
fileUnzipping=-1001;
fileCompleted=-1002;
fileFailure=-1003;
Type
pUnzipReportProc=^UnzipReportProc;
UnzipReportProc=Procedure(Retcode:LongInt;Rec:PReportRec);
pUnzipQuestionProc=^UnzipQuestionProc;
UnzipQuestionProc=Function(Rec:PReportRec):Boolean;
Const
ezOk=0;
ezCRCErr=16001;
ezWriteErr=16002;
ezReadErr=16003;
ezZipFileErr=16004;
ezUserAbort=16005;
ezNotSupported=16006;
ezEncrypted=16007;
ezInUse=16008;
ezInternalError=16009;
ezNoMoreItems=16010;
ezFileError=16011;
ezNotZipfile=16012;
ezHeaderTooLarge=16013;
ezZipFileOpenError=16014;
ezSeriousError=16100;
ezMissingParameter=16500;
ezStarting=17004;
ezCompleted=17005;
huftComplete=0;
huftIncomplete=1;
huftError=2;
huftOutOfMem=3;
MaxMax=31*1024;
wsize=$8000;
INBUFSIZ=1024*4;
lbits:Integer=9;
dbits:Integer=6;
b_Max=16;
n_Max=288;
BMAX=16;
MaxCode=8192;
MaxStack=8192;
InitialCodeSize=9;
FinalCodeSize=13;
WriteMax=WSize-3*(MaxCode-256)-MaxStack-2;
Type
Prev=Array[257..MaxCode]of Integer;
PPrev=^Prev;
cds=Array[257..MaxCode]of Char;
pcds=^cds;
StackType=Array[0..MaxStack]of Char;
PStackType=^StackType;
WriteBufType=Array[0..WriteMax]of Char;
PWriteBufType=^WriteBufType;
Type
push=^Word;
PushList=^ushList;
ushlist=Array[0..MaxMax]of Word;
piobuf=^iobuf;
iobuf=Array[0..InBufSiz-1]of Byte;
pphuft=^phuft;
phuft=^huft;
phuftlist=^huftlist;
huft=Record
e,
b:Byte;
v_n:Word;
v_t:phuftList;
End;
HuftList=Array[0..8190]of Huft;
PLocalHeader=^TLocalHeader;
TLocalHeader=Packed Record
Signature:Array[0..3]of Char; {'PK'#1#2}
ExtractVer,
BitFlag,
ZipType:Word;
FileTimeDate,
CRC32:LongInt;
CompressSize,
UncompressSize:LongInt;
FilenameLen,
ExtraFieldLen:Word;
End;
Type
ZipClass=Record
Slide:PChar;
InBuf:IOBuf;
InPos,ReadPos:Integer;
DlgHandle:Word;
DlgNotify:Integer;
W:Word;
B:LongInt;
k:Byte;
InFile:Hdl;
OutFile:File;
CompSize:LongInt;
ReachedSize:LongInt;
UnCompSize:LongInt;
Crc32val:LongInt;
HuftType:Word;
TotalAbort:Boolean;
ZipEOF:Boolean;
InUse:Boolean;
ZipReport:UnzipReportProc;
ZipQuestion:UnzipQuestionProc;
ZipRec:TReportRec;
ShowPercent:Procedure(Pour:Byte);
NoRecurseDirs:Boolean;
LowcaseFileNames:Boolean;
PreviousCode:PPrev;
ActualCode:pcds;
Stack:PStackType;
WriteBuf:PWriteBufType;
NextFree:Integer;
WritePtr:Integer;
End;
Procedure UnzipInit(Var Q:ZipClass);
Function StripPath(Const S:String):String;
Function CalcRatio(NewSize,OrgSize:LongInt):LongInt;
Procedure ConvertPath(P:PChar);
Function FileUnzip(Var Q:ZipClass;Const SourceZipFile,TargetDirectory,FileSpecs:String;
Report:UnzipReportProc;Question:UnzipQuestionProc):Integer;
Function FileUnzipEx(Var Q:ZipClass;Const SourceZipFile,TargetDirectory,FileSpecs:String):Integer;
Function ViewZip(Var Q:ZipClass;Const SourceZipFile,FileSpecs:String;Report:UnzipReportProc):Integer;
Function SetUnZipReportProc(Var Q:ZipClass;aProc:UnzipReportProc):Pointer;
Function SetUnZipQuestionProc(Var Q:ZipClass;aProc:UnzipQuestionProc):Pointer;
Function UnZipSize(Var Q:ZipClass;Const SourceZipFile:String;Var Compressed:LongInt):LongInt;
Function SetNoRecurseDirs(Var Q:ZipClass;DontRecurse:Boolean):Boolean;
Function GetHeaderOffset(Const FileName:String;PEndoffSet:PLongint):Longint;
Function GetSupportedMethods:LongInt;
Function UnzipFile(Var Q:ZipClass;Const InName:String;OutName:String;
Offset:Longint;hFileAction:Word;CmIndex:Integer):Integer;
Function GetFirstInZip(Const ZipFileName:String;Var ZPRec:TZipRec):Integer;
Function GetNextInZip(Var Zprec:TZiprec):Integer;
Function IsZip(FileName:String;Var StartOffSet:Longint):Boolean;
Procedure CloseZipFile(Var Zprec:TZipRec);
Procedure UnzipDone(Var Q:ZipClass);
{ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
IMPLEMENTATION
{ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ}
Uses
Memories,Systems
{$IFDEF __Windows__}
,Windows
{$ENDIF};
Function StripPath(Const S:String):String;
Var
I,J:Integer;
Begin
StripPath:=S;
J:=Length(S);
For I:=J downto 1do Begin
If S[I]in['\',':','/']Then Begin
StripPath:=Copy(S,Succ(I),J-I);
Exit;
End;
End;
End;
Function CalcRatio(NewSize,OrgSize:LongInt):LongInt;Begin
If NewSize>MaxLongint div 100Then Begin
NewSize:=NewSize div 100;
OrgSize:=OrgSize div 100;
End;
If OrgSize=0Then CalcRatio:=0
Else CalcRatio:=100-((NewSize*100)div orgsize);
End;
Procedure ConvertPath(P:PChar);
Var
I,Len:LongInt;
Begin
Len:=StrLen(PChr(P));
For I:=1to(Len)do If P[I]='/'Then P[I]:='\';
End;
Const
fmOpenRead=$0000;
fmOpenWrite=$0001;
fmOpenReadWrite=$0002;
fmShareCompat=$0000;
fmShareExclusive=$0010;
fmShareDenyWrite=$0020;
fmShareDenyRead=$0030;
fmShareDenyNone=$0040;
FileModeCompat=fmOpenRead or fmShareDenyNone;
Function ExtractFileDir(Const aName:String):String;
Var
I:Word;
Begin
I:=Length(aName);
While Not(aName[I]in['\',':'])and(I<>0)do Dec(I);
If I=0Then ExtractFileDir:=''Else
If I=1Then ExtractFileDir:=Left(GetCurrentDir,3)
Else ExtractFileDir:=Left(aName,I)
End;
Function DirectoryExists(Const S:String):Boolean;
Var
Attr:Word;
Begin
Attr:=FileGetAttr(S);
DirectoryExists:=(Attr>=0)and(Attr and faDir<>0);
End;
Const
MaskBits:Array[0..16]of Word=(
$0000, $0001, $0003, $0007, $000F, $001F, $003F, $007F, $00FF,
$01FF, $03FF, $07FF, $0FFF, $1FFF, $3FFF, $7FFF, $FFFF
);
Border:Array[0..18]of Byte=(
16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15
);
cplens:Array[0..30]of Word=(
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
);
cplext:Array[0..30]of Word=(
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, 99, 99
);
cpdist:Array[0..29]of Word=(
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
);
cpdext:Array[0..29]of Word=(
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
);
cplen2:Array[0..63]of Word=(
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
);
cplen3:Array[0..63]of Word=(
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
);
Extra:Array[0..63]of Word=(
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
8
);
cpdist4:Array[0..63]of Word=(
1, 65, 129, 193, 257, 321, 385, 449, 513, 577, 641, 705,
769, 833, 897, 961, 1025, 1089, 1153, 1217, 1281, 1345, 1409, 1473,
1537, 1601, 1665, 1729, 1793, 1857, 1921, 1985, 2049, 2113, 2177,
2241, 2305, 2369, 2433, 2497, 2561, 2625, 2689, 2753, 2817, 2881,
2945, 3009, 3073, 3137, 3201, 3265, 3329, 3393, 3457, 3521, 3585,
3649, 3713, 3777, 3841, 3905, 3969, 4033
);
cpdist8:Array[0..63]of Word=(
1, 129, 257, 385, 513, 641, 769, 897, 1025, 1153, 1281,
1409, 1537, 1665, 1793, 1921, 2049, 2177, 2305, 2433, 2561, 2689,
2817, 2945, 3073, 3201, 3329, 3457, 3585, 3713, 3841, 3969, 4097,
4225, 4353, 4481, 4609, 4737, 4865, 4993, 5121, 5249, 5377, 5505,
5633, 5761, 5889, 6017, 6145, 6273, 6401, 6529, 6657, 6785, 6913,
7041, 7169, 7297, 7425, 7553, 7681, 7809, 7937, 8065
);
Procedure UpdateCRC32(Var CRC:Longint;Var InBuf;InLen:LongInt);
Const
CRC32Table:Array[0..255]of LongInt=(
$00000000, $77073096, $ee0e612c, $990951ba, $076dc419, $706af48f, $e963a535, $9e6495a3,
$0edb8832, $79dcb8a4, $e0d5e91e, $97d2d988, $09b64c2b, $7eb17cbd, $e7b82d07, $90bf1d91,
$1db71064, $6ab020f2, $f3b97148, $84be41de, $1adad47d, $6ddde4eb, $f4d4b551, $83d385c7,
$136c9856, $646ba8c0, $fd62f97a, $8a65c9ec, $14015c4f, $63066cd9, $fa0f3d63, $8d080df5,
$3b6e20c8, $4c69105e, $d56041e4, $a2677172, $3c03e4d1, $4b04d447, $d20d85fd, $a50ab56b,
$35b5a8fa, $42b2986c, $dbbbc9d6, $acbcf940, $32d86ce3, $45df5c75, $dcd60dcf, $abd13d59,
$26d930ac, $51de003a, $c8d75180, $bfd06116, $21b4f4b5, $56b3c423, $cfba9599, $b8bda50f,
$2802b89e, $5f058808, $c60cd9b2, $b10be924, $2f6f7c87, $58684c11, $c1611dab, $b6662d3d,
$76dc4190, $01db7106, $98d220bc, $efd5102a, $71b18589, $06b6b51f, $9fbfe4a5, $e8b8d433,
$7807c9a2, $0f00f934, $9609a88e, $e10e9818, $7f6a0dbb, $086d3d2d, $91646c97, $e6635c01,
$6b6b51f4, $1c6c6162, $856530d8, $f262004e, $6c0695ed, $1b01a57b, $8208f4c1, $f50fc457,
$65b0d9c6, $12b7e950, $8bbeb8ea, $fcb9887c, $62dd1ddf, $15da2d49, $8cd37cf3, $fbd44c65,
$4db26158, $3ab551ce, $a3bc0074, $d4bb30e2, $4adfa541, $3dd895d7, $a4d1c46d, $d3d6f4fb,
$4369e96a, $346ed9fc, $ad678846, $da60b8d0, $44042d73, $33031de5, $aa0a4c5f, $dd0d7cc9,
$5005713c, $270241aa, $be0b1010, $c90c2086, $5768b525, $206f85b3, $b966d409, $ce61e49f,
$5edef90e, $29d9c998, $b0d09822, $c7d7a8b4, $59b33d17, $2eb40d81, $b7bd5c3b, $c0ba6cad,
$edb88320, $9abfb3b6, $03b6e20c, $74b1d29a, $ead54739, $9dd277af, $04db2615, $73dc1683,
$e3630b12, $94643b84, $0d6d6a3e, $7a6a5aa8, $e40ecf0b, $9309ff9d, $0a00ae27, $7d079eb1,
$f00f9344, $8708a3d2, $1e01f268, $6906c2fe, $f762575d, $806567cb, $196c3671, $6e6b06e7,
$fed41b76, $89d32be0, $10da7a5a, $67dd4acc, $f9b9df6f, $8ebeeff9, $17b7be43, $60b08ed5,
$d6d6a3e8, $a1d1937e, $38d8c2c4, $4fdff252, $d1bb67f1, $a6bc5767, $3fb506dd, $48b2364b,
$d80d2bda, $af0a1b4c, $36034af6, $41047a60, $df60efc3, $a867df55, $316e8eef, $4669be79,
$cb61b38c, $bc66831a, $256fd2a0, $5268e236, $cc0c7795, $bb0b4703, $220216b9, $5505262f,
$c5ba3bbe, $b2bd0b28, $2bb45a92, $5cb36a04, $c2d7ffa7, $b5d0cf31, $2cd99e8b, $5bdeae1d,
$9b64c2b0, $ec63f226, $756aa39c, $026d930a, $9c0906a9, $eb0e363f, $72076785, $05005713,
$95bf4a82, $e2b87a14, $7bb12bae, $0cb61b38, $92d28e9b, $e5d5be0d, $7cdcefb7, $0bdbdf21,
$86d3d2d4, $f1d4e242, $68ddb3f8, $1fda836e, $81be16cd, $f6b9265b, $6fb077e1, $18b74777,
$88085ae6, $ff0f6a70, $66063bca, $11010b5c, $8f659eff, $f862ae69, $616bffd3, $166ccf45,
$a00ae278, $d70dd2ee, $4e048354, $3903b3c2, $a7672661, $d06016f7, $4969474d, $3e6e77db,
$aed16a4a, $d9d65adc, $40df0b66, $37d83bf0, $a9bcae53, $debb9ec5, $47b2cf7f, $30b5ffe9,
$bdbdf21c, $cabac28a, $53b39330, $24b4a3a6, $bad03605, $cdd70693, $54de5729, $23d967bf,
$b3667a2e, $c4614ab8, $5d681b02, $2a6f2b94, $b40bbe37, $c30c8ea1, $5a05df1b, $2d02ef8d
);
Var
BytePtr:^Byte;
WCount:Word;
aCRC:Longint;
Begin
aCRC:=CRC;
BytePtr:=Addr(InBuf);
For WCount:=1to(InLen)do Begin
aCRC:=CRC32Table[Byte(aCRC xor LongInt(BytePtr^))]xor((aCRC shr 8)and$00ffffff);
Inc(BytePtr);
End;
CRC:=aCRC;
End;
Procedure InitCRC32(Var CRC:Longint);Begin
CRC:=-1;
End;
Function FinalCRC32(CRC:Longint):Longint;Begin
FinalCRC32:=Not CRC;
End;
Procedure UpdateCRC(Var Q:ZipClass;Var s:iobuf;Len:Integer);Begin
UpdateCRC32(Q.Crc32val,s,len);
End;
Procedure MessageLoop(Var Q:ZipClass);
Var
K:Word;
Begin
If(KeyPress)Then Begin
K:=ReadKey;
If(K=Q.DlgNotify)Then Q.TotalAbort:=True;
End;
If(@Q.ShowPercent<>NIL)Then Begin
Q.ShowPercent(Q.ReachedSize*100 div Q.CompSize);
End;
End;
Procedure ReadBuf(Var Q:ZipClass);Begin
If Q.ReachedSize>Q.CompSize+2Then Begin
Q.ReadPos:=SizeOf(Q.InBuf);
Q.ZipEOF:=True
End
Else
Begin
MessageLoop(Q);
Q.ReadPos:=_GetRec(Q.InFile,SizeOf(Q.InBuf),Q.InBuf);
If(SysErr<>0)or(Q.ReadPos=0)Then Begin
Q.ReadPos:=SizeOf(Q.InBuf);
Q.ZipEOF:=True;
End;
Inc(Q.ReachedSize,Q.ReadPos);
Dec(Q.ReadPos);
End;
Q.Inpos:=0;
End;
Function ReadByte(Var Q:ZipClass):Byte;Begin
If(Q.Inpos>Q.ReadPos)Then ReadBuf(Q);
ReadByte:=Q.Inbuf[Q.Inpos];
Inc(Q.Inpos);
End;
Procedure NeedBits(Var Q:ZipClass;N:Byte);
Var
nb:LongInt;
D:Word;
Begin
{$IFNDEF Assembler}
While(Q.k<n)do Begin
If(Q.InPos>Q.ReadPos)Then ReadBuf(Q);
nb:=Q.InBuf[Q.InPos];
Inc(Q.Inpos);
Q.b:=Q.b OR nb SHL Q.k;
Inc(Q.k,8);
End;
{$ELSE}
ASM
CLD
LES DI,Q
MOV SI,Offset ZipClass.InBuf
ADD SI,DI
MOV CH,n
MOV CL,ES:[DI].ZipClass.K
MOV BX,ES:[DI].ZipClass.InPos
@again:
CMP CL,CH
JAE @Finished
CMP BX,ES:[DI].ZipClass.ReadPos
JG @ReadBuf
@FullBuf:
MOV AL,ES:[SI+BX] {DX:AX=NB}
XOR AH,AH
XOR DX,DX
CMP CL,8
JAE @Bigger8
SHL AX,CL
JMP @Continue
@Bigger8:
MOV D,CX
MOV AH,AL
XOR AL,AL
SUB CL,8
@Rotate:
OR CL,CL
JZ @Continue1
SHL AH,1
RCL DX,1
DEC CL
JMP @Rotate
@Continue1:
MOV CX,D
@Continue:
OR Word Ptr ES:[DI].ZipClass.B[2],DX
OR Word Ptr ES:[DI].ZipClass.B[0],AX
INC BX
ADD CL,8
JMP @Again
@ReadBuf:
PUSH ES
PUSH DI
PUSH SI
PUSH CX
PUSH ES
PUSH DI
CALL ReadBuf
POP CX
POP SI
POP DI
POP ES
MOV BX,ES:[DI].ZipClass.InPos
JMP @FullBuf
@Finished:
MOV ES:[DI].ZipClass.K,CL
MOV ES:[DI].ZipClass.InPos,BX
END;
{$ENDIF}
End;
Procedure DumpBits(Var Q:ZipClass;N:Byte);Begin
{$IFNDEF Assembler}
Q.b:=Q.b shr N;
Q.k:=Q.k-N;
{$ELSE}
ASM
LES DI,Q
MOV CL,N
MOV AX,Word Ptr ES:[DI].ZipClass.B[0]
MOV DX,Word Ptr ES:[DI].ZipClass.B[2]
MOV CH,CL
JCXZ @Finished
@Rotate:
SHR DX,1
RCR AX,1
DEC CH
JNZ @Rotate
@Finished:
MOV Word Ptr ES:[DI].ZipClass.B[0],AX
MOV Word Ptr ES:[DI].ZipClass.B[2],DX
SUB ES:[DI].ZipClass.K,CL
END;
{$ENDIF}
END;
Function Flush(Var Q:ZipClass;w:LongInt):Boolean;
Var
{$IFDEF __Windows__}
n:LongInt;
{$ELSE}
n:Word;
{$ENDIF}
b:Boolean;
Begin
BlockWrite(Q.OutFile,Q.Slide[0],w,n);
b:=(n=w)and(IOResult=0);
UpdateCRC(Q,IOBuf(Pointer(@Q.Slide[0])^),w);
If(b)and(@Q.ZipReport<>NIL)Then Begin
Q.ZipRec.Status:=fileUnzipping;
Q.ZipReport(n,@Q.ZipRec);
End;
Flush:=b;
End;
Var
_Token:PChar;
Function StrTok(Source:PChar;Token:Char):PChar;
Var
P:PChr;
Begin
If(Source<>NIL)Then _Token:=Source;
If(_Token=NIL)Then Begin
StrTok:=NIL;
Exit
End;
P:=StrScan(PChr(_Token),Token);
StrTok:=_Token;
If(P<>NIL)Then Begin
P^[0]:=#0;
Inc(P);
End;
_Token:=PChar(P);
End;
Procedure HuftFree(t:phuftlist);
Var
p,q:PHuftList;
z:Integer;
Begin
P:=Pointer(t);
While(P<>NIL)do Begin
Dec(LongInt(P),SizeOf(Huft));
q:=p^[0].v_t;
z:=p^[0].v_n;
FreeMemory(p,(z+1)*SizeOf(Huft));
p:=q;
End;
End;
Function HuftBuild(b:pword;n,s:Word;d,e:PushList;t:pphuft;Var m:Integer):Integer;
Var
a:Word;
c:Array[0..b_max+1]of Word;
f:Word;
g,
h:Integer;
i,
j:Word;
k:Integer;
p:PWord;
QP:PHuftList;
r:Huft;
u:Array[0..b_max]of PHuftList;
v:Array[0..n_max]of Word;
w:Integer;
x:Array[0..b_max+1]of Word;
l:Array[-1..b_max+1]of Word;
xp:PWord;
y:Integer;
z:Word;
TryAgain:Boolean;
pt:PHuft;
el:Word;
Begin
If n>256Then el:=PWord(LongInt(b)+256*SizeOf(Word))^
Else el:=BMAX;
FillClr(c,SizeOf(c));
p:=b;i:=n;
Repeat
If(p^>b_max)Then Begin
T^:=NIL;
m:=0;
HuftBuild:=huftError;
Exit
End;
Inc(c[p^]);
Inc(LongInt(p),SizeOf(Word));
Dec(i);
Until i=0;
If(c[0]=n)Then Begin
t^:=NIL;
m:=0;
HuftBuild:=huftComplete;
Exit
End;
j:=1;
While(j<=b_max)and(c[j]=0)do Inc(J);
k:=j;
If(m<j)Then m:=j;
i:=b_max;
While(I>0)and(c[i]=0)do Dec(i);
g:=i;
If(m>i)Then m:=i;
y:=1 SHL j;
While(j<i)do Begin
Dec(Y,C[J]);
If y<0Then Begin
HuftBuild:=huftError;
Exit
End;
y:=y shl 1;
Inc(j);
End;
Dec(y,c[i]);
If y<0Then Begin
HuftBuild:=huftError;
Exit
End;
Inc(c[i],y);
x[1]:=0;
j:=0;
p:=PWord(@c);
Inc(LongInt(p),SizeOf(Word));
xp:=pword(@x);
Inc(LongInt(xp),2*SizeOf(Word));
Dec(i);
While i<>0do Begin
Inc(j,p^);
xp^:=j;
Inc(LongInt(p),2);
Inc(LongInt(xp),2);
Dec(i);
End;
p:=b;i:=0;
Repeat
j:=p^;
Inc(LongInt(p),SizeOf(Word));
If j<>0Then Begin
v[x[J]]:=I;
Inc(x[J]);
End;
Inc(I);
Until I>=n;
x[0]:=0;I:=0;
p:=PWord(@v);
h:=-1;
l[-1]:=0;
w:=0;
u[0]:=NIL;
QP:=NIL;
z:=0;
For K:=K to(G)do Begin
For a:=c[k]downto 1do Begin
While k>w+l[h]do Begin
Inc(w,l[h]);
Inc(h);
z:=g-w;
If(z>m)Then z:=m;
j:=k-w;
f:=1 shl j;
If f>a+1Then Begin
Dec(f,a+1);
xp:=@c[k];
Inc(j);
TryAgain:=True;
While(j<z)and(TryAgain)do Begin
f:=f shl 1;
Inc(LongInt(xp),SizeOf(Word));
If f<=xp^Then TryAgain:=False
Else
Begin
Dec(f,xp^);
Inc(j);
End;
End;
End;
If(w+j>el)and(w<el)Then j:=el-w;
If w=0Then j:=m;
z:=1shl j;
l[h]:=j;
QP:=MemNew((Z+1)*SizeOf(Huft));
If(QP=NIL)Then Begin
If h<>0Then HuftFree(Pointer(u[0]));
HuftBuild:=huftOutOfMem;
Exit
End;
QP^[0].v_n:=z;
t^:=@QP^[1];
t:=pphuft(@QP^[0].v_t);
t^:=NIL;
QP:=phuftlist(@QP^[1]);
u[h]:=QP;
If h<>0Then Begin
x[h]:=i;
r.b:=l[h-1];
r.e:=16+j;
r.v_t:=QP;
j:=(i and((1 shl w)-1))shr(w-l[h-1]);
pt:=phuft(LongInt(u[h-1])-SizeOf(huft));
If(j>pt^.v_n)Then Begin
HuftFree(Pointer(u[0]));
HuftBuild:=huftError;
Exit
End;
pt:=@u[h-1]^[j];
pt^:=r;
End;
End;
r.b:=Word(k-w);
r.v_t:=NIL;
If LongInt(p)>=LongInt(@v[n])Then r.e:=99 Else
If(p^<s)Then Begin
If p^<256Then r.e:=16
Else r.e := 15;
r.v_n := p^;
Inc(LongInt(p),SizeOf(Word));
End
Else
Begin
If(d=NIL)or(e=NIL)Then Begin
HuftFree(Pointer(u[0]));
HuftBuild:=huftError;
Exit
End;
r.e:=Word(e^[p^-s]);
r.v_n:=d^[p^-s];
Inc(LongInt(p),SizeOf(Word));
End;
f:=1shl(k-w);
j:=i shr w;
While(j<z)do Begin
QP^[j]:=r;
Inc(j,f);
End;
j:=1shl(k-1);
While(i and j)<>0do Begin
i:=i xor j;
j:=j shr 1;
End;
i:=i xor j;
While((i and((1 shl w)-1))<>x[h])do Begin
Dec(h);
Dec(w,l[h]);
End;
End;
End;
If(y<>0)and(g<>1)Then HuftBuild:=huftIncomplete
Else HuftBuild:=huftComplete;
End;
Function InflateCodes(Var Q:ZipClass;tl,td:PHuftList;bl,bd:Integer):Integer;
Var
n,d,e1,
ml,md:Word;
t:phuft;
e:Byte;
Begin
ml:=MaskBits[bl];
md:=MaskBits[bd];
While Not(Q.TotalAbort or Q.ZipEOF)do Begin
NeedBits(Q,bl);
t:=@tl^[Q.b and ml];
e:=t^.e;
If e>16Then Repeat
If e=99Then Begin
InflateCodes:=ezZipFileErr;
Exit
End;
DumpBits(Q,t^.b);
Dec(e,16);
NeedBits(Q,e);
t:=@t^.v_t^[Q.b and MaskBits[e]];
e:=t^.e;
Until e<=16;
DumpBits(Q,t^.b);
If e=16Then Begin
Q.Slide[Q.W]:=Char(t^.v_n);
Inc(Q.W);
If(Q.W=WSize)Then Begin
If Not Flush(Q,Q.W)Then Begin
InflateCodes:=ezWriteErr;
Exit;
End;
Q.W:=0
End;
End
Else
Begin
If e=15Then Begin
InflateCodes:=ezOk;
Exit;
End;
NeedBits(Q,e);
n:=t^.v_n+(Q.b and MaskBits[e]);
DumpBits(Q,e);
NeedBits(Q,bd);
t:=@td^[Q.b and md];
e:=t^.e;
If e>16Then Repeat
If e=99Then Begin
InflateCodes:=ezZipFileErr;
Exit
End;
DumpBits(Q,t^.b);
Dec(e,16);
NeedBits(Q,e);
t:=@t^.v_t^[Q.b and MaskBits[e]];
e:=t^.e;
Until e<=16;
DumpBits(Q,t^.b);
NeedBits(Q,e);
d:=Q.W-t^.v_n-Q.b and MaskBits[e];
DumpBits(Q,e);
Repeat
d:=d and(WSize-1);
If(d>Q.W)Then e1:=WSize-d
Else e1:=WSize-Q.W;
If(e1>n)Then e1:=n;
Dec(n,e1);
If(Q.W-d>=e1)Then Begin
Move(Q.Slide[d],Q.Slide[Q.W],e1);
Inc(Q.W,e1);
Inc(d,e1);
End
Else
Repeat
Q.Slide[Q.W]:=Q.Slide[d];
Inc(Q.W);
Inc(d);
Dec(e1);
Until e1=0;
If(Q.W=WSize)Then Begin
If Not Flush(Q,Q.W)Then Begin
InflateCodes:=ezWriteErr;
Exit;
End;
Q.W:=0;
End;
Until n=0;
End;
End;
If(Q.TotalAbort)Then InflateCodes:=ezUserAbort
Else InflateCodes:=ezReadErr;
End;
Function InflateStored(Var Q:ZipClass):Integer;
Var
n:Word;
Begin
n:=Q.k and 7;
DumpBits(Q,n);
NeedBits(Q,16);
n:=Q.b and $FFFF;
DumpBits(Q,16);
NeedBits(Q,16);
If n<>(Not Q.b)and$FFFFThen Begin
InflateStored:=ezZipFileErr;
Exit
End;
DumpBits(Q,16);
While(n>0)and Not(Q.TotalAbort or Q.ZipEOF)do Begin
Dec(n);
NeedBits(Q,8);
Q.Slide[Q.W]:=Char(Q.b);
Inc(Q.W);
If(Q.W=WSize)Then Begin
If Not Flush(Q,Q.W)Then Begin
InflateStored:=ezWriteErr;
Exit
End;
Q.W:=0;
End;
DumpBits(Q,8);
End;
If(Q.TotalAbort)Then InflateStored:=ezUserAbort Else
If(Q.ZipEOF)Then InflateStored:=ezReadErr
Else InflateStored:=ezOk;
End;
Function InflateFixed(Var Q:ZipClass):Integer;
Var
i:Integer;
tl,td:phuftlist;
bl,bd:Integer;
L:Array[0..287]of Word;
Begin
FillWord(L,SizeOf(L)shr 1,8);
FillWord(L[144],112,9);
FillWord(L[256],24,7);
bl:=7;
i:=HuftBuild(PWord(@l),288,257,PushList(@cplens),PushList(@cplext),pphuft(@tl ),bl);
If(i<>huftComplete)Then Begin
InflateFixed:=i;
Exit
End;
FillWord(L,30,5);
bd:=5;
i:=HuftBuild(PWord(@l),30,0,PushList(@cpdist),PushList(@cpdext),pphuft(@td),bd);
If(i>huftIncomplete)Then Begin
HuftFree(tl);
InflateFixed:=ezZipFileErr;
Exit
End;
InflateFixed:=InflateCodes(Q,tl,td,bl,bd);
HuftFree(tl);
HuftFree(td);
End;
Function InflateDynamic(Var Q:ZipClass):Integer;
Var
i:Integer;
j,
l,
m,
n:Word;
tl,
td:PHuftList;
bl,bd:Integer;
nb,nl,nd:Word;
ll:Array[0..288+32-1]of Word;
Begin
NeedBits(Q,5);
nl:=257+Word(Q.b)and$1F;
DumpBits(Q,5);
NeedBits(Q,5);
nd:=1+Word(Q.b)and$1F;
DumpBits(Q,5);
NeedBits(Q,4);
nb:=4+Word(Q.b)and$F;
DumpBits(Q,4);
If(nl>288)or(nd>32)Then Begin
InflateDynamic:=1;
Exit
End;
FillClr(ll,SizeOf(ll));
For j:=0to nb-1do Begin
NeedBits(Q,3);
ll[Border[j]]:=Q.b and 7;
DumpBits(Q,3);
End;
For J:=nb to 18do ll[Border[J]]:=0;
bl:=7;
i:=HuftBuild(PWord(@ll),19,19,NIL,NIL,pphuft(@tl),bl);
If(i<>huftComplete)Then Begin
If(i=HuftIncomplete)Then HuftFree(tl);
InflateDynamic:=ezZipFileErr;
Exit
End;
n:=nl+nd;
m:=MaskBits[bl];
i:=0;l:=0;
While Word(i)<n do Begin
NeedBits(Q,bl);
td:=PHuftList(@tl^[Q.b and m]);
j:=PHuft(td)^.b;
DumpBits(Q,j);
j:=PHuft(td)^.v_n;
If j<16Then Begin
l:=j;
ll[i]:=l;
Inc(i)
End