-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZMZipDirectory.pas
1831 lines (1718 loc) · 48.1 KB
/
ZMZipDirectory.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 ZMZipDirectory;
// ZMZipReader.pas - Represents the readable Directory of a Zip file
(* ***************************************************************************
TZipMaster VCL originally by Chris Vleghert, Eric W. Engler.
Present Maintainers and Authors Roger Aelbrecht and Russell Peters.
Copyright (C) 1997-2002 Chris Vleghert and Eric W. Engler
Copyright (C) 1992-2008 Eric W. Engler
Copyright (C) 2009, 2010, 2011, 2012, 2013 Russell Peters and Roger Aelbrecht
Copyright (C) 2014 Russell Peters and Roger Aelbrecht
All rights reserved.
For the purposes of Copyright and this license "DelphiZip" is the current
authors, maintainers and developers of its code:
Russell Peters and Roger Aelbrecht.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* DelphiZip reserves the names "DelphiZip", "ZipMaster", "ZipBuilder",
"DelZip" and derivatives of those names for the use in or about this
code and neither those names nor the names of its authors or
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL DELPHIZIP, IT'S AUTHORS OR CONTRIBUTERS BE
LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
contact: problems AT delphizip DOT org
updates: http://www.delphizip.org
*************************************************************************** *)
// modified 2014-01-02
{$INCLUDE '.\ZipVers.inc'}
interface
uses
{$IFDEF VERDXE2up}
System.Classes, WinApi.Windows,
{$ELSE}
Classes, Windows,
{$ENDIF}
ZipMstr, ZMBody, ZMZipEOC, ZMStructs;
type
TZMRecStrings = (ZrsName, ZrsComment, ZrsIName);
TZMSelects = (ZzsClear, ZzsSet, ZzsToggle);
TZMStrEncOpts = (ZseDOS, ZseXName, ZseXComment);
TZMStrEncodes = set of TZMStrEncOpts;
// ZipDirEntry status bit constants
const
ZsbHashed = $100; // hash calculated
ZsbLocalDone = $200; // local data prepared
ZsbLocal64 = $400; // local header required zip64
ZsbEncMask = $70000; // mask for bits holding how entry is encoded
ZsbExtName = $200000; // filename has extended characters
ZsbExtCmnt = $400000; // filename has extended characters
type
TZCChanges = (ZccBegin, ZccAdd, ZccEnd);
TZCChangeEvent = procedure(Sender: TObject; Idx: Integer; Change: TZCChanges)
of object;
type
TZMZipDirectory = class;
TZMSelectArgs = class;
TZMEntryBase = class
private
FBody: TZMBody;
FExtIndex: Integer;
FHash: Cardinal;
FHTNext: TZMEntryBase;
FMyFile: TZMZipDirectory;
FNext: TZMEntryBase;
FSelectArgs: TZMSelectArgs;
FStatusBits: Cardinal;
FXName: string;
function GetDataString(Cmnt: Boolean): UTF8String;
function GetHash: Cardinal;
function GetIsDirOnly: Boolean;
function GetSelected: Boolean;
function GetStatus(Bit: Cardinal): Boolean;
function GetStatusBit(Mask: Cardinal): Cardinal;
function GetXName: string;
procedure SetSelected(const Value: Boolean);
procedure SetStatus(Bit: Cardinal; const Value: Boolean);
function VerifyLocalName(const LocalHeader: TZipLocalHeader; const HName:
TZMRawBytes): Integer;
protected
function FindDataTag(Tag: Word; var Idx, Siz: Integer): Boolean;
function GetCompressedSize: Int64; virtual; abstract;
function GetCompressionMethod: Word; virtual; abstract;
function GetCRC32: Cardinal; virtual; abstract;
function GetDateStamp: TDateTime;
function GetDateTime: Cardinal; virtual; abstract;
function GetDirty: Boolean;
function GetEncoded: TZMEncodingOpts; virtual;
function GetEncrypted: Boolean; virtual;
function GetExtFileAttrib: Longword; virtual; abstract;
function GetExtraData(Tag: Word): TZMRawBytes; virtual; abstract;
function GetExtraField: TZMRawBytes; virtual; abstract;
function GetExtraFieldLength: Word; virtual; abstract;
function GetFileComment: string; virtual; abstract;
function GetFileCommentLen: Word; virtual; abstract;
function GetFileName: string; virtual; abstract;
function GetFileNameLen: Word; virtual; abstract;
function GetFlag: Word; virtual; abstract;
function GetHTFileName: string; virtual;
function GetIntFileAttrib: Word; virtual; abstract;
function GetIsEncoded: TZMEncodingOpts; virtual;
function GetMaster: TCustomZipMaster;
function GetModifDateTime: Longword; virtual; abstract;
function GetRelOffLocalHdr: Int64; virtual; abstract;
function GetStartOnDisk: Cardinal; virtual; abstract;
function GetStatusBits: Cardinal;
function GetTitle: string; virtual;
function GetUncompressedSize: Int64; virtual; abstract;
function GetVersionMadeBy: Word; virtual; abstract;
function GetVersionNeeded: Word; virtual; abstract;
function Get_CompressedSize: Cardinal; virtual; abstract;
function Get_FileComment: TZMRawBytes; virtual; abstract;
function Get_FileName: TZMRawBytes; virtual; abstract;
function Get_RelOffLocalHdr: Cardinal; virtual; abstract;
function Get_StartOnDisk: Word; virtual; abstract;
function Get_UncompressedSize: Cardinal; virtual; abstract;
function IsZip64: Boolean;
procedure MarkDirty;
procedure SetIsEncoded(const Value: TZMEncodingOpts);
property Master: TCustomZipMaster read GetMaster;
public
constructor Create(TheOwner: TZMZipDirectory);
procedure AfterConstruction; override;
procedure AssignFrom(const Zr: TZMEntryBase); virtual;
procedure BeforeDestruction; override;
function CentralSize: Cardinal;
procedure ClearCachedName; virtual; abstract;
function ClearStatusBit(const Values: Cardinal): Cardinal;
function FetchNTFSTimes(var Times: TNTFS_Times): Integer;
function HasChanges: Boolean;
function HasDataDesc: Boolean;
function HTIsSame(const AnEntry: TZMEntryBase): Boolean; virtual;
function HTIsSameStr(StrHash: Cardinal; const Str: string)
: Boolean; virtual;
function SeekLocalData(var LocalHeader: TZipLocalHeader;
const HName: TZMRawBytes): Integer;
function Select(How: TZMSelects): Boolean;
function SetStatusBit(const Value: Cardinal): Cardinal;
function TestStatusBit(const Mask: Cardinal): Boolean;
property Body: TZMBody read FBody;
property CompressedSize: Int64 read GetCompressedSize;
property CompressionMethod: Word read GetCompressionMethod;
property CRC32: Cardinal read GetCRC32;
property DateStamp: TDateTime read GetDateStamp;
property DateTime: Cardinal read GetDateTime;
property Encoded: TZMEncodingOpts read GetEncoded;
property Encrypted: Boolean read GetEncrypted;
property ExtFileAttrib: Longword read GetExtFileAttrib;
property ExtIndex: Integer read FExtIndex write FExtIndex;
property ExtraField: TZMRawBytes read GetExtraField;
property ExtraFieldLength: Word read GetExtraFieldLength;
property FileComment: string read GetFileComment;
property FileCommentLen: Word read GetFileCommentLen;
property FileName: string read GetFileName;
property FileNameLen: Word read GetFileNameLen;
property Flag: Word read GetFlag;
property Hash: Cardinal read GetHash;
property HTFileName: string read GetHTFileName;
property HTNext: TZMEntryBase read FHTNext write FHTNext;
property IntFileAttrib: Word read GetIntFileAttrib;
property IsDirOnly: Boolean read GetIsDirOnly;
property IsEncoded: TZMEncodingOpts read GetIsEncoded write SetIsEncoded;
property ModifDateTime: Longword read GetModifDateTime;
property MyFile: TZMZipDirectory read FMyFile write FMyFile;
property Next: TZMEntryBase read FNext write FNext;
property RelOffLocalHdr: Int64 read GetRelOffLocalHdr;
property SelectArgs: TZMSelectArgs read FSelectArgs write FSelectArgs;
property Selected: Boolean read GetSelected write SetSelected;
property StartOnDisk: Cardinal read GetStartOnDisk;
property Status[Bit: Cardinal]: Boolean read GetStatus write SetStatus;
property StatusBit[Mask: Cardinal]: Cardinal read GetStatusBit;
property StatusBits: Cardinal read FStatusBits write FStatusBits;
property Title: string read GetTitle;
property UncompressedSize: Int64 read GetUncompressedSize;
property VersionMadeBy: Word read GetVersionMadeBy;
property VersionNeeded: Word read GetVersionNeeded;
property XName: string read GetXName write FXName;
property _CompressedSize: Cardinal read Get_CompressedSize;
// prefix '_' header properties that are overriden
property _FileComment: TZMRawBytes read Get_FileComment;
property _FileName: TZMRawBytes read Get_FileName;
property _RelOffLocalHdr: Cardinal read Get_RelOffLocalHdr;
property _StartOnDisk: Word read Get_StartOnDisk;
property _UncompressedSize: Cardinal read Get_UncompressedSize;
end;
TZMSelectArgs = class
private
Cnts: Integer;
FNext: TZMSelectArgs;
public
function Accept(Rec: TZMEntryBase): Boolean; virtual; abstract;
procedure AfterConstruction; override;
procedure Assign(Other: TZMSelectArgs); virtual;
property Next: TZMSelectArgs read FNext write FNext;
end;
TZMZipDirectory = class(TZMZipEOC)
private
FArgsList: TZMSelectArgs;
FCount: Integer;
FFirstRec: TZMEntryBase;
FHTCount: Integer;
FHTSize: Integer;
FLastRec: TZMEntryBase;
FOnChange: TZCChangeEvent;
FOpenRet: Integer;
FSelCount: Integer;
FSFXOfs: Cardinal;
FSOCOfs: Int64;
FStub: TMemoryStream;
FUseSFX: Boolean;
function GetFirstSelected: TZMEntryBase;
procedure LoadEntries;
procedure SetHTSize(Value: Integer);
procedure SetStub(const Value: TMemoryStream);
protected
FHashTable: array of TZMEntryBase;
procedure ClearEntries;
function HTFind(const FileName: string): TZMEntryBase;
procedure InferNumbering;
procedure MarkDirty;
procedure RemoveEntry(Entry: TZMEntryBase);
function SelectEntry(Rec: TZMEntryBase; How: TZMSelects): Boolean;
property HTSize: Integer read FHTSize write SetHTSize;
public
function Add(Rec: TZMEntryBase): Integer;
function AddSelectArgs(Args: TZMSelectArgs): TZMSelectArgs;
procedure AfterConstruction; override;
procedure AssignStub(From: TZMZipDirectory);
procedure BeforeDestruction; override;
procedure ClearArgsList;
procedure ClearCachedNames;
procedure ClearSelection;
function FindName(const Name: string; const NotMe: TZMEntryBase = nil)
: TZMEntryBase;
procedure FreeSelectArgs(Args: TZMSelectArgs);
function HasDupName(const Rec: TZMEntryBase): TZMEntryBase;
function HTAdd(AnEntry: TZMEntryBase; AllowDuplicate: Boolean)
: TZMEntryBase;
procedure HTAutoSize(Req: Cardinal);
procedure HTClear;
function HTFirstDup(const AnEntry: TZMEntryBase): TZMEntryBase;
function HTNextDup(const AnEntry: TZMEntryBase): TZMEntryBase;
procedure HTRemove(AnEntry: TZMEntryBase);
function InsertAfter(NewRec, ARec: TZMEntryBase): Integer;
// 1 Mark as Contents Invalid
procedure Invalidate;
function LoadZip: Integer;
function NextSelected(CurRec: TZMEntryBase): TZMEntryBase;
function RecAtN(N: Integer): TZMEntryBase;
function SearchName(const Pattern: string; IsWild: Boolean; After: Integer)
: TZMEntryBase;
function SearchNameEx(const Pattern: string; IsWild: Boolean;
After: TZMEntryBase): TZMEntryBase;
function Select(const Pattern: string; How: TZMSelects): Integer;
function SelectFile(const Pattern, Reject: string; How: TZMSelects)
: Integer;
function SelectFiles(const Want, Reject: TStrings): Integer;
function SelectRec(const Pattern, Reject: string; How: TZMSelects;
SelArgs: TZMSelectArgs): Integer;
property ArgsList: TZMSelectArgs read FArgsList;
property Count: Integer read FCount;
property FirstRec: TZMEntryBase read FFirstRec write FFirstRec;
property FirstSelected: TZMEntryBase read GetFirstSelected;
property HTCount: Integer read FHTCount;
property LastRec: TZMEntryBase read FLastRec;
property OpenRet: Integer read FOpenRet write FOpenRet;
property SelCount: Integer read FSelCount write FSelCount;
property SFXOfs: Cardinal read FSFXOfs write FSFXOfs;
property SOCOfs: Int64 read FSOCOfs write FSOCOfs;
property Stub: TMemoryStream read FStub write SetStub;
property UseSFX: Boolean read FUseSFX write FUseSFX;
property OnChange: TZCChangeEvent read FOnChange write FOnChange;
end;
implementation
uses
{$IFDEF VERDXE2up}
System.SysUtils,
{$ELSE}
SysUtils, {$IFNDEF UNICODE}ZMCompat, {$ENDIF}
{$ENDIF}
ZMCore, ZMZipBase, ZMMsg, ZMXcpt, ZMUtils, ZMMatch, ZMWinFuncs,
ZMEntryReader, ZMUTF8, ZMCRC;
const
__UNIT__ = 43;
const
HTChainsMax = 65537;
HTChainsMin = 61;
const
AllSpec: string = '*.*';
AnySpec: string = '*';
function ZM_Error(Line, Error: Integer): Integer;
begin
Result := -((__UNIT__ shl ZERR_UNIT_SHIFTS) + (Line shl ZERR_LINE_SHIFTS) or
AbsErr(Error));
end;
type
Txdat64 = packed record
Tag: Word;
Siz: Word;
Vals: array [0 .. 4] of Int64; // last only cardinal
end;
function _XDataP(const Xdat: PByte; Len, Tag: Word; var Data: PByte;
var Size: Integer): Boolean;
var
I: Integer;
P: PAnsiChar;
Wsz: Word;
Wtg: Word;
begin
Result := False;
Data := nil;
Size := 0;
I := 1;
P := PAnsiChar(Xdat);
while I < Len - (2 * SizeOf(Word)) do
begin
Wtg := PWord(P)^;
Wsz := PWord(P + 2)^;
if Wtg = Tag then
begin
Result := (I + Wsz + (2 * SizeOf(Word))) <= Len + 1;
if Result then
begin
Data := PByte(P);
Size := Wsz;
end;
Break;
end;
I := I + Wsz + (2 * SizeOf(Word));
Inc(P, Wsz + (2 * SizeOf(Word)));
end;
end;
// returns index
function TZMZipDirectory.Add(Rec: TZMEntryBase): Integer;
begin
if FirstRec = nil then
begin
// will be first
FFirstRec := Rec;
Rec.Next := nil;
Rec.ExtIndex := 0;
FCount := 1;
FLastRec := Rec;
Result := 0;
end
else
begin
Assert(LastRec <> nil, 'invalid last pointer');
LastRec.Next := Rec;
FLastRec := Rec;
Rec.ExtIndex := FCount;
Result := FCount;
Inc(FCount);
end;
end;
function TZMZipDirectory.AddSelectArgs(Args: TZMSelectArgs): TZMSelectArgs;
begin
Result := Args;
if Args = nil then
Exit;
Args.Next := FArgsList;
FArgsList := Args; // chain it
end;
procedure TZMZipDirectory.AfterConstruction;
begin
inherited;
FCount := 0;
FFirstRec := nil;
FLastRec := nil;
end;
procedure TZMZipDirectory.AssignStub(From: TZMZipDirectory);
begin
FreeAndNil(FStub);
FStub := From.Stub;
From.FStub := nil;
end;
procedure TZMZipDirectory.BeforeDestruction;
begin
ClearEntries;
ClearArgsList;
FreeAndNil(FStub);
inherited;
end;
procedure TZMZipDirectory.ClearArgsList;
var
Node: TZMEntryBase;
Tmp: TZMSelectArgs;
begin
Node := FirstRec;
while Node <> nil do
begin
Node.SelectArgs := nil; // clear all
Node := Node.Next;
end;
// delete list
while FArgsList <> nil do
begin
Tmp := FArgsList;
FArgsList := Tmp.Next;
Tmp.Free;
end;
end;
procedure TZMZipDirectory.ClearCachedNames;
var
Node: TZMEntryBase;
begin
HTClear;
Node := FirstRec;
while Node <> nil do
begin
Node.ClearCachedName;
Node := Node.Next;
end;
end;
procedure TZMZipDirectory.ClearEntries;
var
Node: TZMEntryBase;
Tmp: TObject;
begin
HTClear;
Node := FirstRec;
while Node <> nil do
begin
Tmp := Node;
Node := Node.Next;
Tmp.Free;
end;
FFirstRec := nil;
FLastRec := nil;
FCount := 0;
FSelCount := 0;
end;
procedure TZMZipDirectory.ClearSelection;
var
Node: TZMEntryBase;
begin
Node := FirstRec;
while Node <> nil do
begin
Node.Selected := False;
Node := Node.Next;
end;
FSelCount := 0;
end;
function TZMZipDirectory.FindName(const Name: string;
const NotMe: TZMEntryBase = nil): TZMEntryBase;
var
Hash: Cardinal;
begin
if HTSize > 0 then
begin
Result := HTFind(Name);
while (Result <> nil) do
begin
if (Result <> NotMe) and (Result.StatusBit[ZsbDiscard] = 0) then
Break;
Result := HTNextDup(Result);
end;
end
else
begin
Hash := HashFuncNoCase(Name);
Result := FirstRec; // from beginning
while Result <> nil do
begin
if (Result <> NotMe) and (Result.StatusBit[ZsbDiscard] = 0) and
(Result.Hash = Hash) and FileNameMatch(Name, Result.FileName) then
Break;
Result := Result.Next;
end;
end;
end;
procedure TZMZipDirectory.FreeSelectArgs(Args: TZMSelectArgs);
var
Arg: TZMSelectArgs;
begin
if Args = nil then
Exit;
Arg := ArgsList;
if Arg = Args then
begin
// first entry
FArgsList := Arg.Next; // remove from list
Arg.Free;
end
else
begin
// find it
while (Arg <> nil) and (Arg.Next <> Args) do
Arg := Arg.Next;
if Arg <> nil then
begin
// found
Arg.Next := Args.Next; // remove from list
Arg.Free;
end;
end;
end;
function TZMZipDirectory.GetFirstSelected: TZMEntryBase;
begin
Result := FirstRec;
if (Result <> nil) and (Result.StatusBit[ZsbSkipped or ZsbSelected] <>
ZsbSelected) then
Result := NextSelected(Result);
end;
// searches for record with same Name
function TZMZipDirectory.HasDupName(const Rec: TZMEntryBase): TZMEntryBase;
var
Name: string;
Hash: Cardinal;
begin
Result := nil;
if Rec <> nil then
begin
Name := Rec.FileName;
Hash := HashFuncNoCase(Name);
Result := FirstRec; // from beginning
while Result <> nil do
begin
if (Result <> Rec) and (Result.Hash = Hash) and
FileNameMatch(Name, Result.FileName) then
Break;
Result := Result.Next;
end;
end;
end;
// returns existing entry (if any) or nil
function TZMZipDirectory.HTAdd(AnEntry: TZMEntryBase; AllowDuplicate: Boolean)
: TZMEntryBase;
var
Entry: TZMEntryBase;
Hash: Cardinal;
Idx: Integer;
Smaller: TZMEntryBase;
begin
Assert(AnEntry <> nil, 'nil ZipDirEntry');
if FHashTable = nil then
HTSize := 1283;
Result := nil;
Hash := AnEntry.Hash;
Idx := Hash mod Cardinal(Length(FHashTable));
Entry := FHashTable[Idx];
if Entry = nil then
begin
AnEntry.HTNext := nil; // only one in linked-list
FHashTable[Idx] := AnEntry;
Inc(FHTCount);
end
else
begin
// search linked-list
Smaller := nil;
// Parent := Entry; // keep compiler happy
while Entry <> nil do
begin
if Entry.HTIsSame(AnEntry) then
begin
if not AllowDuplicate then
begin
Result := Entry;
Exit;
end;
if Result = nil then
Result := Entry; // catch first
if Entry = AnEntry then
begin
Result := nil;
Exit; // already added
end;
end;
// step to next
if Entry.ExtIndex < AnEntry.ExtIndex then
Smaller := Entry; // remember last smaller index
Entry := Entry.HTNext;
end;
if Smaller = nil then
begin
// new first node
AnEntry.HTNext := FHashTable[Idx];
FHashTable[Idx] := AnEntry;
end
else
begin
// insert into list after smaller
AnEntry.HTNext := Smaller.HTNext;
Smaller.HTNext := AnEntry;
end;
Inc(FHTCount);
end;
end;
// set size to a reasonable prime number
procedure TZMZipDirectory.HTAutoSize(Req: Cardinal);
const
PrimeSizes: array [0 .. 29] of Cardinal = (61, 131, 257, 389, 521, 641, 769,
1031, 1283, 1543, 2053, 2579, 3593, 4099, 5147, 6151, 7177, 8209, 10243,
12289, 14341, 16411, 18433, 20483, 22521, 24593, 28687, 32771,
40961, 65537);
var
I: Integer;
begin
if Req < 12000 then
begin
// use next higher size
for I := 0 to high(PrimeSizes) do
if PrimeSizes[I] >= Req then
begin
Req := PrimeSizes[I];
Break;
end;
end
else
begin
// use highest smaller size
for I := high(PrimeSizes) downto 0 do
if PrimeSizes[I] < Req then
begin
Req := PrimeSizes[I];
Break;
end;
end;
SetHTSize(Req);
end;
procedure TZMZipDirectory.HTClear;
begin
FHashTable := nil;
FHTSize := 0;
FHTCount := 0;
end;
// not needed for HeaderName ?
function TZMZipDirectory.HTFind(const FileName: string): TZMEntryBase;
var
Hash: Cardinal;
Idx: Cardinal;
begin
if FHashTable = nil then
HTAutoSize(Count);
Hash := HashFuncNoCase(FileName);
Idx := Hash mod Cardinal(Length(FHashTable));
Result := FHashTable[Idx];
// check entries in this chain
while Result <> nil do
begin
if Result.HTIsSameStr(Hash, FileName) then
Break; // exists
Result := Result.HTNext;
end;
end;
// finds the first duplicate in chain
// assumes HashTable filled and links are valid
function TZMZipDirectory.HTFirstDup(const AnEntry: TZMEntryBase): TZMEntryBase;
var
Hash: Cardinal;
Idx: Cardinal;
begin
Hash := AnEntry.Hash;
Idx := Hash mod Cardinal(Length(FHashTable));
Result := FHashTable[Idx]; // first entry in linked-list
// check entries in this chain
while Result <> nil do
begin
if Result.HTIsSame(AnEntry) then
Break; // exists
Result := Result.HTNext;
end;
end;
// find next duplicate in linked-list
// assumes HashTable filled and links are valid
function TZMZipDirectory.HTNextDup(const AnEntry: TZMEntryBase): TZMEntryBase;
begin
Result := AnEntry.HTNext; // next entry in linked-list
// check entries in this chain
while Result <> nil do
begin
if Result.HTIsSame(AnEntry) then
Break; // exists
Result := Result.HTNext;
end;
end;
procedure TZMZipDirectory.HTRemove(AnEntry: TZMEntryBase);
var
Entry: TZMEntryBase;
Hash: Cardinal;
Idx: Cardinal;
Prev: TZMEntryBase;
begin
Prev := nil;
if FHashTable = nil then
Exit; // nothing to delete from
Hash := AnEntry.Hash;
Idx := Hash mod Cardinal(Length(FHashTable));
Entry := FHashTable[Idx]; // first entry in linked-list
// check entries in this chain
while (Entry <> nil) and (Entry <> AnEntry) do
begin
Prev := Entry;
Entry := Entry.HTNext;
end;
if Entry <> nil then
begin
// found
if Prev = nil then
FHashTable[Idx] := Entry.HTNext // is first
else
Prev.HTNext := Entry.HTNext; // remove from chain
Entry.HTNext := nil;
Dec(FHTCount);
end;
end;
// Use after EOC found and FileName is last part
// if removable has proper numbered volume name we assume it is numbered volume
procedure TZMZipDirectory.InferNumbering;
var
Fname: string;
Num: Integer;
NumStr: string;
begin
if IsExtStream then
Numbering := ZnsNone
else
if (Numbering = ZnsNone) and (TotalDisks > 1) then
begin
// only if unknown
WorkDrive.DriveStr := ArchiveName;
if WorkDrive.DriveIsFloppy and SameText(WorkDrive.DiskName,
VolName(DiskNr)) then
Numbering := ZnsVolume
else
begin
NumStr := '';
Fname := ExtractNameOfFile(ArchiveName);
Numbering := ZnsExt;
if Length(Fname) > 3 then
begin
NumStr := Copy(Fname, Length(Fname) - 2, 3);
Num := StrToIntDef(NumStr, -1);
if Num = (DiskNr + 1) then
begin
// ambiguous conflict
if not HasSpanSig(ExtractFilePath(ArchiveName) + Fname + '.z01')
then
Numbering := ZnsName;
end;
end;
end;
end;
end;
function TZMZipDirectory.InsertAfter(NewRec, ARec: TZMEntryBase): Integer;
begin
Assert(NewRec <> nil, 'Tried to add empty TZMEntryBase');
Assert(NewRec.MyFile <> nil, 'Owned by different list');
if ARec = nil then
ARec := FirstRec;
if ARec = nil then
begin
FFirstRec := NewRec;
FLastRec := FirstRec;
LastRec.Next := nil;
FCount := 1;
end
else
begin
Assert(ARec.MyFile = Self, 'Tried to add to different list');
NewRec.Next := ARec.Next;
if LastRec = ARec then
FLastRec := NewRec; // new end
ARec.Next := NewRec;
Inc(FCount);
end;
NewRec.MyFile := Self;
Result := Count;
end;
procedure TZMZipDirectory.Invalidate;
begin
Info := Info or Zfi_Invalid;
end;
function TZMZipDirectory.LoadZip: Integer;
var
LiE: Integer;
OffsetDiff: Int64;
Sgn: Cardinal;
begin
if not IsOpen then
begin
Result := Body.PrepareErrMsg(ZE_FileOpen, [ArchiveName],
{_LINE_}884, __UNIT__);
Exit;
end;
Result := ZM_Error({_LINE_}887, ZE_UnknownError);
if (Info and Zfi_EOC) = 0 then
Exit; // should not get here if eoc has not been read
LiE := 1;
OffsetDiff := 0;
ClearEntries;
if Assigned(OnChange) then
OnChange(Self, 0, ZccBegin);
SOCOfs := CentralOffset;
try
OffsetDiff := CentralOffset;
// Do we have to request for a previous disk first?
if DiskNr <> CentralDiskNo then
begin
SeekDisk(CentralDiskNo, False);
File_Size := Seek(0, SoEnd);
end
else
if not Z64 then
begin
// Due to the fact that v1.3 and v1.4x programs do not change the archives
// EOC and CEH records in case of a SFX conversion (and back) we have to
// make this extra check.
OffsetDiff := File_Size -
(Integer(CentralSize) + SizeOf(TZipEndOfCentral) + ZipCommentLen);
end;
SOCOfs := OffsetDiff;
// save the location of the Start Of Central dir
SFXOfs := Cardinal(OffsetDiff);
if SFXOfs <> SOCOfs then
SFXOfs := 0;
// initialize this - we will reduce it later
if File_Size = 22 then
SFXOfs := 0;
if CentralOffset <> OffsetDiff then
begin
// We need this in the ConvertXxx functions.
ShowErrorEx(ZW_WrongZipStruct, {_LINE_}925, __UNIT__);
CheckSeek(CentralOffset, SoBeginning,
ZM_Error({_LINE_}927, ZE_ReadZipError));
CheckRead(Sgn, 4, ZM_Error({_LINE_}928, ZE_CEHBadRead));
if Sgn = CentralFileHeaderSig then
begin
SOCOfs := CentralOffset;
// TODO warn - central size error
end;
end;
// Now we can go to the start of the Central directory.
CheckSeek(SOCOfs, SoBeginning, ZM_Error({_LINE_}937, ZE_ReadZipError));
Guage.NewItem(ZP_Loading, TotalEntries);
// Read every entry: The central header and save the information.
{$IFDEF DEBUG}
Body.TraceFmt('List - expecting %d files', [TotalEntries],
{_LINE_}942, __UNIT__);
{$ENDIF}
LoadEntries;
LiE := 0; // finished ok
Result := 0;
Info := (Info and not(Zfi_MakeMask)) or Zfi_Loaded;
finally
Guage.EndBatch;
if LiE = 1 then
begin
ArchiveName := '';
SFXOfs := 0;
File_Close;
end
else
begin
CentralOffset := SOCOfs; // corrected
// Correct the offset for v1.3 and 1.4x
SFXOfs := SFXOfs + Cardinal(OffsetDiff - CentralOffset);
end;
// Let the user's program know we just refreshed the zip dir contents.
if Assigned(OnChange) then
OnChange(Self, Count, ZccEnd);
end;
end;
procedure TZMZipDirectory.LoadEntries;
var
I: Integer;
R: Integer;
begin
for I := 0 to (TotalEntries - 1) do
begin
if LastRec = nil then
begin
FFirstRec := TZMEntryReader.Create(Self); // is first
FLastRec := FirstRec;
end
else
begin
LastRec.Next := TZMEntryReader.Create(Self); // append
FLastRec := LastRec.Next;
end;
R := TZMEntryReader(LastRec).Read;
if R < 0 then
raise EZipMaster.CreateMsg(Body, R, 0, 0);
if R > 0 then
Z64 := True;
{$IFDEF DEBUG}
if Verbosity >= ZvTrace then
Body.TraceFmt('List - [%d] "%s"', [I, LastRec.FileName], {_LINE}995,
__UNIT__);
{$ENDIF}
Inc(FCount);
// Notify user, when needed, of the NextSelected entry in the ZipDir.
if Assigned(OnChange) then
OnChange(Self, I, ZccAdd); // change event to give TZipDirEntry
// Calculate the earliest Local Header start
if SFXOfs > LastRec.RelOffLocalHdr then
SFXOfs := LastRec.RelOffLocalHdr;
Guage.Advance(1);
CheckCancel;
end; // for;
end;
procedure TZMZipDirectory.MarkDirty;
begin
Info := Info or Zfi_Dirty;
end;
// return BadIndex when no more
function TZMZipDirectory.NextSelected(CurRec: TZMEntryBase): TZMEntryBase;
begin
Result := CurRec.Next;
while Result <> nil do
begin
if Result.StatusBit[ZsbSkipped or ZsbSelected] = ZsbSelected then
Break;
Result := Result.Next;
end;
end;
function TZMZipDirectory.RecAtN(N: Integer): TZMEntryBase;
begin
Result := FirstRec;
while (Result <> nil) and (N > 0) do
begin
Result := Result.Next;
Dec(N);
end;
end;
procedure TZMZipDirectory.RemoveEntry(Entry: TZMEntryBase);