-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathZMLister.pas
1548 lines (1435 loc) · 40 KB
/
ZMLister.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 ZMLister;
// ZMLister.pas - Loads and 'lists' zips
(* ***************************************************************************
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, 2015, 2016, 2017 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 2013-12-05
{$I '.\ZipVers.inc'}
interface
uses
{$IFDEF VERDXE2up}
System.Classes, WinApi.Windows, System.SysUtils, VCL.Graphics,
{$ELSE}
Windows, SysUtils, Classes, Graphics, {$IFNDEF VERD7up}ZMCompat, {$ENDIF}
{$ENDIF}
ZipMstr, ZMStructs, ZMBody, ZMZipReader, ZMZipDirectory;
type
// None = no reload, Reload = reload non-detached, Any = reload any, Clear = clear
TZMReloads = (ZlrNone, ZlrClear, ZlrLoaded, ZlrReload, ZlrAny);
type
TZMExtEntry = class;
TZMLister = class(TZMBody)
private
FCurrent: TZMZipReader;
FDirOnlyCount: Integer;
FExtStream: TStream;
FLockKey: Integer;
FRecList: TList;
FReload: TZMReloads;
FStatusBits: Cardinal;
FUseDirOnlyEntries: Boolean;
FZipComment: AnsiString;
FZipFileName: string;
function AddRecord(Rec: TZMEntryBase): Boolean;
procedure ClearEntries;
function FindFirstIcon(var Rec: TImageResourceDataEntry;
const ILevel: Integer; const PointerToRawData: Cardinal;
Str: TStream): Boolean;
function GetCount: Integer;
function GetCurrent: TZMZipReader;
function GetCurrentIsValid: Boolean;
function GetDirEntry(Idx: Integer): TZMDirEntry;
function GetDirOnlyCnt: Integer;
function GetFirstIcon(Str: TMemoryStream): TIcon;
function GetIsSpanned: Boolean;
function GetSFXOffset: Integer;
function GetTotalDisks: Integer;
function GetUseDirOnlyEntries: Boolean;
function GetZipComment: AnsiString;
function GetZipEOC: Int64;
function GetZipFileSize: Int64;
function GetZipSOC: Int64;
function MapSFXSettings17(Pheder: PByte; Stub: TMemoryStream): Integer;
function MapSFXSettings19(Pheder: PByte; Stub: TMemoryStream): Integer;
function ReadSFXStr17(var P: PByte; Len: Byte): AnsiString;
procedure SetCurrent(const Value: TZMZipReader);
procedure SetUseDirOnlyEntries(const Value: Boolean);
procedure SetZipFileName(const Value: string);
procedure ZipChange(Sender: TObject; Idx: Integer; Chng: TZCChanges);
protected
function IsDetachSFX(Zfile: TZMZipReader): Boolean;
function LoadSFXStr(Ptbl: PByte; Ident: Byte): string;
function MapOptionsFrom17(Opts: Word): TZMSFXOpts;
function MapOptionsFromStub(Opts: Word): TZMSFXOpts;
function MapOverwriteModeFromStub(Ovr: Word): TZMOvrOpts;
// 1 return true if it was there
function MapSFXSettings(Stub: TMemoryStream): Integer;
procedure Started; override;
function TrimDetached(Stub: TMemoryStream): Boolean;
procedure UpdateAuxProperties;
procedure UpdateCurrent(WasGood: Boolean);
property StatusBits: Cardinal read FStatusBits write FStatusBits;
procedure SetEncoding(const Value: TZMEncodingOpts); override;
procedure SetEncoding_CP(const Value: Cardinal); override;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
procedure Clear; override;
procedure ClearCachedNames;
function CurrentZip(MustExist: Boolean; SafePart: Boolean = False)
: TZMZipReader;
function Find(const Fspec: string; var Idx: Integer): TZMDirEntry;
function ForEach(Func: TZMForEachFunction; var Data): Integer;
function IsDetachedSFX(const FileName: string): Boolean;
function List: Integer;
function LoadZip(const ZipName: string; NoEvent: Boolean): Integer;
procedure LocateFirstIconHeader(Str: TStream;
var HdrSection: TImageSectionHeader;
var RecIcon: TImageResourceDataEntry);
procedure OnDirUpdate;
procedure OnNewName(Idx: Integer);
procedure Refresh;
procedure Set_ExtStream(const Value: TStream);
procedure Set_ZipFileName(const Zname: string; Load: TZMLoadOpts);
property Count: Integer read GetCount;
property Current: TZMZipReader read GetCurrent write SetCurrent;
property CurrentIsValid: Boolean read GetCurrentIsValid;
property DirEntry[Idx: Integer]: TZMDirEntry read GetDirEntry; default;
property DirOnlyCnt: Integer read GetDirOnlyCnt;
property ExtStream: TStream read FExtStream;
property IsSpanned: Boolean read GetIsSpanned;
property LockKey: Integer read FLockKey;
property Reload: TZMReloads read FReload write FReload;
property SFXOffset: Integer read GetSFXOffset;
property TotalDisks: Integer read GetTotalDisks;
property UseDirOnlyEntries: Boolean read GetUseDirOnlyEntries
write SetUseDirOnlyEntries;
property ZipComment: AnsiString read GetZipComment write FZipComment;
property ZipEOC: Int64 read GetZipEOC;
property ZipFileName: string read FZipFileName write SetZipFileName;
property ZipFileSize: Int64 read GetZipFileSize;
property ZipSOC: Int64 read GetZipSOC;
end;
TZMExtEntry = class(TZMDirEntry)
private
FMyKey: Integer;
FMyLister: TZMLister;
FMyRec: TZMEntryBase;
function Fetch(var Rec: TZMEntryBase): Boolean;
protected
function GetCompressedSize: Int64; override;
function GetCompressionMethod: Word; override;
function GetCRC32: Cardinal; override;
function GetDateTime: Cardinal; override;
function GetEncoded: TZMEncodingOpts; override;
function GetEncrypted: Boolean; override;
function GetExtFileAttrib: Longword; override;
function GetExtraField: TZMRawBytes; override;
function GetExtraFieldLength: Word; override;
function GetFileComment: string; override;
function GetFileCommentLen: Word; override;
function GetFileName: string; override;
function GetFileNameLength: Word; override;
function GetFlag: Word; override;
function GetHeaderName: TZMRawBytes; override;
function GetIntFileAttrib: Word; override;
function GetMaster: TComponent; override;
function GetRelOffLocalHdr: Int64; override;
function GetStartOnDisk: Word; override;
function GetStatusBits: Cardinal; override;
function GetUncompressedSize: Int64; override;
function GetVersionMadeBy: Word; override;
function GetVersionNeeded: Word; override;
public
constructor Create(Owner: TZMLister; TheRec: TZMEntryBase); overload;
property MyRec: TZMEntryBase read FMyRec;
end;
implementation
uses
{$IFDEF VERDXE2up}
VCL.Dialogs,
{$ELSE}
Dialogs,
{$ENDIF}
ZMMsg, ZMUtils, ZMXcpt, ZMHandler, ZMUTF8, ZMZipBase, ZMEngine,
ZMCore, ZMSFXInt;
const
__UNIT__ = 20;
const
MinStubSize = 12000;
function ZM_Error(Line, Error: Integer): Integer;
begin
Result := -((__UNIT__ shl ZERR_UNIT_SHIFTS) + (Line shl ZERR_LINE_SHIFTS) or
AbsErr(Error));
end;
{ TZMLister }
function TZMLister.AddRecord(Rec: TZMEntryBase): Boolean;
var
Idx: Integer;
XRec: TZMExtEntry;
begin
Result := False;
if (not UseDirOnlyEntries) and Rec.TestStatusBit(ZsbDirOnly) then
begin
Inc(FDirOnlyCount);
Rec.ExtIndex := -1; // not indexed
end
else
begin
XRec := TZMExtEntry.Create(Self, Rec);
Idx := FRecList.Add(XRec);
Rec.ExtIndex := Idx;
FCurrent.HTAdd(Rec, True);
Result := True;
end;
StatusBits := StatusBits or Rec.StatusBits;
end;
procedure TZMLister.AfterConstruction;
begin
inherited;
FRecList := TList.Create;
FUseDirOnlyEntries := UseDirOnlyEntries;
end;
procedure TZMLister.BeforeDestruction;
begin
Clear;
FRecList.Free;
FreeAndNil(FCurrent);
inherited;
end;
procedure TZMLister.Clear;
begin
inherited;
ClearEntries;
FExtStream := nil;
end;
procedure TZMLister.ClearCachedNames;
var
Cz: TZMZipReader;
begin
Cz := FCurrent;
if Cz <> nil then
Cz.ClearCachedNames; // update for any changed settings
end;
procedure TZMLister.ClearEntries;
var
I: Integer;
Tmp: TObject;
begin
FDirOnlyCount := 0;
for I := 0 to Pred(FRecList.Count) do
begin
Tmp := FRecList.Items[I];
if Tmp <> nil then
begin
FRecList.Items[I] := nil;
Tmp.Free;
end;
end;
FRecList.Clear;
StatusBits := 0;
end;
function TZMLister.CurrentZip(MustExist: Boolean; SafePart: Boolean = False)
: TZMZipReader;
begin
if ZipFileName = '' then
raise EZipMaster.CreateMsg(Self, ZE_NoZipSpecified, {_LINE_}294, __UNIT__);
Result := Current;
if MustExist and ((Zfi_Loaded and Result.Info) = 0) then
raise EZipMaster.CreateMsg(Self, ZE_NoValidZip, {_LINE_}297, __UNIT__);
if SafePart and ((Zfi_Cancelled and Result.Info) <> 0) then
begin
if Result.AskAnotherDisk(ZipFileName) = IdCancel then
raise EZipMaster.CreateMsg(Self, ZS_Abort, {_LINE_}301, __UNIT__);
Result.Info := 0; // clear error
end;
if Result.ArchiveName = '' then
begin
// creating new file
Result.ArchiveName := ZipFileName;
Result.ReqFileName := ZipFileName;
end;
end;
function TZMLister.Find(const Fspec: string; var Idx: Integer): TZMDirEntry;
var
Rec: TZMEntryBase;
begin
if Idx < -1 then
Idx := -1;
Result := nil;
if (Idx + 1) < Count then
begin
Rec := FCurrent.SearchName(Fspec, IsWild(Fspec), Idx);
if Rec <> nil then
begin
Idx := Rec.ExtIndex;
Result := TZMDirEntry(FRecList[Idx]);
end;
end;
if Result = nil then
Idx := -1;
end;
function TZMLister.FindFirstIcon(var Rec: TImageResourceDataEntry;
const ILevel: Integer; const PointerToRawData: Cardinal;
Str: TStream): Boolean;
var
I: Integer;
IPos: Integer;
RecDir: TImageResourceDirectory;
RecEnt: TImageResourceDirectoryEntry;
begin
// position must be correct
Result := False;
if (Str.Read(RecDir, Sizeof(RecDir)) <> Sizeof(RecDir)) then
raise EZipMaster.CreateMsg(Self, ZE_BrowseError, {_LINE_}345, __UNIT__);
for I := 0 to Pred(RecDir.NumberOfNamedEntries + RecDir.NumberOfIdEntries) do
begin
if (Str.Read(RecEnt, Sizeof(RecEnt)) <> Sizeof(RecEnt)) then
raise EZipMaster.CreateMsg(Self, ZE_BrowseError, {_LINE_}350, __UNIT__);
// check if a directory or a resource
IPos := Str.Position;
try
if (RecEnt.Un2.DataIsDirectory and IMAGE_RESOURCE_DATA_IS_DIRECTORY) = IMAGE_RESOURCE_DATA_IS_DIRECTORY
then
begin
if ((ILevel = 0) and (MakeIntResource(RecEnt.Un1.Name) <> RT_ICON)) or
((ILevel = 1) and (RecEnt.Un1.Id <> 1)) then
Continue; // not an icon of id 1
Str.Seek(RecEnt.Un2.OffsetToDirectory and
(not IMAGE_RESOURCE_DATA_IS_DIRECTORY) + PointerToRawData,
{$IFDEF VERPre6}Word{$ENDIF}(TSeekOrigin(SoFromBeginning)));
Result := FindFirstIcon(Rec, ILevel + 1, PointerToRawData, Str);
if Result then
Break;
end
else
begin
// is resource bin data
Str.Seek(RecEnt.Un2.OffsetToData + PointerToRawData,
{$IFDEF VERPre6}Word{$ENDIF}(TSeekOrigin(SoFromBeginning)));
if Str.Read(Rec, Sizeof(Rec)) <> Sizeof(Rec) then
raise EZipMaster.CreateMsg(Self, ZE_BrowseError, {_LINE_}375,
__UNIT__);
Result := True;
Break;
end;
finally
Str.Position := IPos;
end;
end;
end;
function TZMLister.ForEach(Func: TZMForEachFunction; var Data): Integer;
var
CurZip: TZMZipReader;
Good: Integer;
Idx: Integer;
// i: Integer;
Rec: TZMEntryBase;
SelCnt: Integer;
XRec: TZMDirEntry;
begin
Result := 0;
Good := 0;
if Logging then
LogSpecs('');
if IncludeSpecs.Count < 1 then
raise EZipMaster.CreateMsg(Self, ZE_InvalidArguments, {_LINE_}401,
__UNIT__);
Trace('StartUp ForEach', {_LINE_}403, __UNIT__);
CurZip := CurrentZip(True);
SelCnt := CurZip.SelectFiles(IncludeSpecs, ExcludeSpecs);
if SelCnt < 0 then
begin
Result := SelCnt;
ShowError(Result);
Exit;
end;
ClearIncludeSpecs; // will contain files processed
ShowProgress := ZspFull;
CurZip.Guage.SetCount(SelCnt);
CurZip.Guage.SetSize(SelCnt);
Rec := CurZip.FirstSelected;
while Rec <> nil do
begin
if (not UseDirOnlyEntries) and Rec.IsDirOnly then
begin
// ignore DirOnly entries
Rec := CurZip.NextSelected(Rec);
Continue;
end;
if Verbosity > ZvVerbose then
TraceFmt('Processing: %s', [Rec.FileName], {_LINE_}426, __UNIT__);
CurZip.Guage.NewItem(Rec.FileName, 1);
Idx := Rec.ExtIndex;
if Idx >= 0 then
begin
XRec := TZMDirEntry(FRecList[Idx]);
Result := Func(XRec, Data);
if Result <> 0 then
begin
ProblemList.Add(Rec.FileName);
Break;
end;
Inc(Good);
IncludeSpecs.Add(Rec.FileName);
CurZip.Guage.Advance(1);
end;
CheckCancel;
Rec := CurZip.NextSelected(Rec);
end;
CurZip.Guage.EndBatch;
SuccessCnt := Good;
Trace('finished ForEach', {_LINE_}447, __UNIT__);
if Logging then
LogSpecs('');
end;
function TZMLister.GetCount: Integer;
begin
Result := FRecList.Count;
end;
function TZMLister.GetCurrent: TZMZipReader;
begin
if Assigned(FCurrent) then
begin
if (FCurrent.Info and Zfi_Invalid) <> 0 then
Current := TZMZipReader.Create(Self); // force reload
end
else
Current := TZMZipReader.Create(Self);
Result := FCurrent;
end;
function TZMLister.GetCurrentIsValid: Boolean;
begin
Result := (ZipFileName <> '') and (Current <> nil) and
((Current.Info and Zfi_Loaded) <> 0);
end;
function TZMLister.GetDirEntry(Idx: Integer): TZMDirEntry;
begin
if (Idx >= 0) and (Idx < FRecList.Count) then
Result := TZMDirEntry(FRecList[Idx])
else
begin
ReportMsg(ZE_RangeError, [Idx, FRecList.Count - 1]);
Result := nil;
end;
end;
function TZMLister.GetDirOnlyCnt: Integer;
begin
Result := FDirOnlyCount;
end;
// replaces an icon in an executable file (stream)
function TZMLister.GetFirstIcon(Str: TMemoryStream): TIcon;
var
Bad: Boolean;
Delta: Cardinal;
Handle: HIcon;
HdrSection: TImageSectionHeader;
IcoData: PByte;
IcoSize: Cardinal;
RecIcon: TImageResourceDataEntry;
begin
Bad := True;
Result := nil;
LocateFirstIconHeader(Str, HdrSection, RecIcon);
Delta := Integer(HdrSection.PointerToRawData) -
Integer(HdrSection.VirtualAddress) + Integer(RecIcon.OffsetToData);
IcoData := PByte(Str.Memory);
Inc(IcoData, Delta);
IcoSize := HdrSection.SizeOfRawData;
Handle := CreateIconFromResource(IcoData, IcoSize, True, $30000);
if Handle <> 0 then
begin
Result := TIcon.Create;
Result.Handle := Handle;
Bad := False;
end;
if Bad then
// no icon copied, so none of matching size found
raise EZipMaster.CreateMsg(Self, ZE_NoIconFound, {_LINE_}519, __UNIT__);
end;
function TZMLister.GetIsSpanned: Boolean;
begin
if FCurrent <> nil then
Result := FCurrent.MultiDisk
else
Result := False;
end;
function TZMLister.GetSFXOffset: Integer;
begin
if FCurrent <> nil then
Result := FCurrent.CentralOffset
else
Result := 0;
end;
function TZMLister.GetTotalDisks: Integer;
begin
Result := 0;
if Assigned(FCurrent) then
Result := FCurrent.TotalDisks;
end;
function TZMLister.GetUseDirOnlyEntries: Boolean;
begin
Result := FUseDirOnlyEntries;
end;
function TZMLister.GetZipComment: AnsiString;
begin
if CurrentIsValid then
Result := Current.ZipComment
else
Result := FZipComment;
end;
function TZMLister.GetZipEOC: Int64;
begin
if FCurrent <> nil then
Result := FCurrent.EOCOffset
else
Result := 0;
end;
function TZMLister.GetZipFileSize: Int64;
begin
if FCurrent <> nil then
Result := FCurrent.File_Size
else
Result := 0;
end;
function TZMLister.GetZipSOC: Int64;
begin
if FCurrent <> nil then
Result := FCurrent.CentralOffset
else
Result := 0;
end;
function TZMLister.IsDetachedSFX(const FileName: string): Boolean;
var
Ext: string;
FN: string;
Wz: TZMZipReader;
begin
Result := False;
FN := FileName;
if FN = '' then
FN := ZipFileName;
Ext := ExtractFileExt(FN);
if AnsiSameText(Ext, '.exe') then
begin
Wz := TZMZipReader.Create(Self);
try
Wz.ArchiveName := FN;
if (Wz.OpenEOC(FN, True) >= 0) and IsDetachSFX(Wz) then
Result := True;
finally
Wz.Free;
end;
end;
end;
// if is detached sfx - set stub excluding the detached header
function TZMLister.IsDetachSFX(Zfile: TZMZipReader): Boolean;
const
MaxStubSize = 80000;
var
Cstt: Integer;
Ms: TMemoryStream;
begin
Result := False;
try
Zfile.Stub := nil; // remove old
Ms := nil;
if (Zfile.IsOpen) and (Zfile.DiskNr = 0) and (Zfile.Sig = ZfsDOS) then
begin
// check invalid values
if (Zfile.EOCOffset <= Zfile.CentralSize) or
(Zfile.CentralSize < Sizeof(TZipCentralHeader)) then
Exit;
Cstt := Zfile.EOCOffset - Zfile.CentralSize;
// must have SFX stub but we only check for biggest practical header
if (Cstt < MinStubSize) or (Cstt > MaxStubSize) then
Exit;
if Zfile.Seek(0, SoBeginning) <> 0 then
Exit;
Ms := TMemoryStream.Create;
try
if Zfile.ReadTo(Ms, Cstt + 4) = (Cstt + 4) then
begin
Result := TrimDetached(Ms);
end;
finally
Ms.Free;
end;
end;
except
Result := False;
FreeAndNil(Ms);
end;
end;
function TZMLister.List: Integer;
begin
Result := LoadZip(ZipFileName, False);
end;
// table format - ident: byte, strng[]: byte, 0: byte; ...;0
function TZMLister.LoadSFXStr(Ptbl: PByte; Ident: Byte): string;
var
Id: Byte;
begin
Result := '';
if (Ptbl = nil) or (Ident = 0) then
Exit;
Id := Ptbl^;
while (Id <> 0) and (Id <> Ident) do
begin
while Ptbl^ <> 0 do
Inc(Ptbl);
Inc(Ptbl);
Id := Ptbl^;
end;
if Id = Ident then
begin
Inc(Ptbl);
{$IFDEF UNICODE}
Result := PUTF8ToStr(PAnsiChar(Ptbl), -1);
{$ELSE}
if IsUtf8 then
Result := UTF8String(PAnsiChar(Ptbl))
else
Result := PUTF8ToStr(PAnsiChar(Ptbl), -1);
{$ENDIF}
end;
end;
function TZMLister.LoadZip(const ZipName: string; NoEvent: Boolean): Integer;
var
TmpDirUpdate: TNotifyEvent;
begin
Result := 0;
Current := nil; // close and remove any old file
if (ZipName <> '') or (FExtStream <> nil) then
begin
// make new ZipFile
if FExtStream <> nil then
Current.Stream := ExtStream
else
Current.ArchiveName := ZipName;
Result := Current.OpenZip(False, False);
if Result >= 0 then
begin
Current.File_Close;
ZipComment := '';
// FZipComment := Current.ZipComment;
Reload := ZlrLoaded; // update Aux properties
end
else
begin
if AbsErr(Result) = ZE_NoInFile then
begin
// just report no file - may be intentional
Errors.ExtCode := ZM_Error({_LINE_}707, ZE_NoInFile);
Errors.ErrMessage := ZipLoadStr(ZE_NoInFile);
Result := 0;
end;
end;
end;
if not NoEvent then
begin
TmpDirUpdate := Master.OnDirUpdate;
if Assigned(TmpDirUpdate) then
TmpDirUpdate(Master);
end;
end;
procedure TZMLister.LocateFirstIconHeader(Str: TStream;
var HdrSection: TImageSectionHeader; var RecIcon: TImageResourceDataEntry);
var
BFound: Boolean;
CAddress: Cardinal;
DataDir: PImageDataDirectory;
HdrDos: TImageDosHeader;
HdrNT: TImageNTHeaders;
I: Integer;
begin
BFound := False;
// check if we have an executable
Str.Seek(0, SoFromBeginning);
if (Str.Read(HdrDos, Sizeof(HdrDos)) <> Sizeof(HdrDos)) or
(HdrDos.E_magic <> IMAGE_DOS_SIGNATURE) then
raise EZipMaster.CreateMsg(Self, ZE_InputNotExe, {_LINE_}736, __UNIT__);
Str.Seek(HdrDos._lfanew, SoFromBeginning);
if (Str.Read(HdrNT, Sizeof(HdrNT)) <> Sizeof(HdrNT)) or
(HdrNT.Signature <> IMAGE_NT_SIGNATURE) then
raise EZipMaster.CreateMsg(Self, ZE_InputNotExe, {_LINE_}741, __UNIT__);
// check if we have a resource section
DataDir := @(HdrNT.OptionalHeader.DataDirectory
[IMAGE_DIRECTORY_ENTRY_RESOURCE]);
if (DataDir^.VirtualAddress = 0) or (DataDir^.Size = 0) then
raise EZipMaster.CreateMsg(Self, ZE_NoExeResource, {_LINE_}747, __UNIT__)
else
CAddress := DataDir^.VirtualAddress; // store address
// iterate over sections
for I := 0 to Pred(HdrNT.FileHeader.NumberOfSections) do
begin
if (Str.Read(HdrSection, Sizeof(HdrSection)) <> Sizeof(HdrSection)) then
raise EZipMaster.CreateMsg(Self, ZE_ExeSections, {_LINE_}755, __UNIT__);
// with hdrSection do
if HdrSection.VirtualAddress = CAddress then
begin
BFound := True;
Break;
end;
end;
if not BFound then
raise EZipMaster.CreateMsg(Self, ZE_NoExeResource, {_LINE_}766, __UNIT__);
// go to resource data
Str.Seek(HdrSection.PointerToRawData,
{$IFDEF VERPre6}Word{$ENDIF}(TSeekOrigin(SoFromBeginning)));
// recurse through the resource dirs to find an icon
if not FindFirstIcon(RecIcon, 0, HdrSection.PointerToRawData, Str) then
raise EZipMaster.CreateMsg(Self, ZE_NoExeIcon, {_LINE_}774, __UNIT__);
end;
function TZMLister.MapOptionsFrom17(Opts: Word): TZMSFXOpts;
begin
Result := [];
if (So_AskCmdLine_17 and Opts) <> 0 then
Result := Result + [SoAskCmdLine];
if (So_AskFiles_17 and Opts) <> 0 then
Result := Result + [SoAskFiles];
if (So_HideOverWriteBox_17 and Opts) <> 0 then
Result := Result + [SoHideOverWriteBox];
if (So_AutoRun_17 and Opts) <> 0 then
Result := Result + [SoAutoRun];
if (So_NoSuccessMsg_17 and Opts) <> 0 then
Result := Result + [SoNoSuccessMsg];
if (So_ExpandVariables_17 and Opts) <> 0 then
Result := Result + [SoExpandVariables];
if (So_InitiallyHideFiles_17 and Opts) <> 0 then
Result := Result + [SoInitiallyHideFiles];
if (So_ForceHideFiles_17 and Opts) <> 0 then
Result := Result + [SoForceHideFiles];
if (So_CheckAutoRunFileName_17 and Opts) <> 0 then
Result := Result + [SoCheckAutoRunFileName];
if (So_CanBeCancelled_17 and Opts) <> 0 then
Result := Result + [SoCanBeCancelled];
if (So_CreateEmptyDirs_17 and Opts) <> 0 then
Result := Result + [SoCreateEmptyDirs];
end;
function TZMLister.MapOptionsFromStub(Opts: Word): TZMSFXOpts;
begin
Result := [];
if (So_AskCmdLine and Opts) <> 0 then
Result := Result + [SoAskCmdLine];
if (So_AskFiles and Opts) <> 0 then
Result := Result + [SoAskFiles];
if (So_HideOverWriteBox and Opts) <> 0 then
Result := Result + [SoHideOverWriteBox];
if (So_AutoRun and Opts) <> 0 then
Result := Result + [SoAutoRun];
if (So_NoSuccessMsg and Opts) <> 0 then
Result := Result + [SoNoSuccessMsg];
if (So_ExpandVariables and Opts) <> 0 then
Result := Result + [SoExpandVariables];
if (So_InitiallyHideFiles and Opts) <> 0 then
Result := Result + [SoInitiallyHideFiles];
if (So_ForceHideFiles and Opts) <> 0 then
Result := Result + [SoForceHideFiles];
if (So_CheckAutoRunFileName and Opts) <> 0 then
Result := Result + [SoCheckAutoRunFileName];
if (So_CanBeCancelled and Opts) <> 0 then
Result := Result + [SoCanBeCancelled];
if (So_CreateEmptyDirs and Opts) <> 0 then
Result := Result + [SoCreateEmptyDirs];
if (So_SuccessAlways and Opts) <> 0 then
Result := Result + [SoSuccessAlways];
end;
function TZMLister.MapOverwriteModeFromStub(Ovr: Word): TZMOvrOpts;
begin
case Ovr of
Som_Overwrite:
Result := OvrAlways;
Som_Skip:
Result := OvrNever;
else
Result := OvrConfirm;
end;
end;
function TZMLister.MapSFXSettings(Stub: TMemoryStream): Integer;
type
T_header = packed record
Sig: DWORD;
Size: Word;
X: Word;
end;
P_header = ^T_header;
var
I: Integer;
NumSections: Integer;
P: PByte;
Phed: P_header;
Sz: Cardinal;
Tmp: Cardinal;
begin
Result := 0;
if (Stub <> nil) and (Stub.Size > MinStubSize) then
begin
Sz := 0;
P := Stub.Memory;
if (PImageDosHeader(P).E_magic <> IMAGE_DOS_SIGNATURE) then
Exit;
Result := ZM_Error({_LINE_}869, ZE_SFXBadRead); // 'unknown sfx'
Inc(P, PImageDosHeader(P)._lfanew);
if PCardinal(P)^ <> IMAGE_PE_SIGNATURE then
Exit; // not exe
Inc(P, Sizeof(Cardinal));
NumSections := PImageFileHeader(P).NumberOfSections;
Inc(P, Sizeof(TImageFileHeader) + Sizeof(TImageOptionalHeader));
for I := 1 to NumSections do
begin
Tmp := PImageSectionHeader(P)^.PointerToRawData + PImageSectionHeader(P)
^.SizeOfRawData;
if Tmp > Sz then
Sz := Tmp;
Inc(P, Sizeof(TImageSectionHeader));
end;
// sz = end of stub
P := Stub.Memory;
Inc(P, Sz);
Phed := P_header(P);
if Phed.Sig = SFX_HEADER_SIG then
Result := MapSFXSettings19(P, Stub)
else
if Phed.Sig = SFX_HEADER_SIG_17 then
Result := MapSFXSettings17(P, Stub);
end;
end;
function TZMLister.MapSFXSettings17(Pheder: PByte; Stub: TMemoryStream)
: Integer;
type
T_header = packed record
Sig: DWORD;
Size: Word;
X: Word;
end;
P_header = ^T_header;
var
Ico: TIcon;
P: PByte;
PSFXHeader: PSFXFileHeader_17;
X_Caption, X_Path, X_CmdLine, X_RegFailPath, X_StartMsg: AnsiString;
begin
Result := ZM_Error({_LINE_}912, ZE_SFXBadRead);
PSFXHeader := PSFXFileHeader_17(Pheder);
P := Pheder;
Inc(P, Sizeof(TSFXFileHeader_17)); // point to strings
X_Caption := ReadSFXStr17(P, PSFXHeader^.CaptionSize);
X_Path := ReadSFXStr17(P, PSFXHeader^.PathSize);
X_CmdLine := ReadSFXStr17(P, PSFXHeader^.CmdLineSize);
X_RegFailPath := ReadSFXStr17(P, PSFXHeader^.RegFailPathSize);
X_StartMsg := ReadSFXStr17(P, PSFXHeader^.StartMsgSize);
// read icon
try
Ico := GetFirstIcon(Stub);
// should test valid
SFX.Icon := Ico;
Ico.Free;
except
on E: EZipMaster do
begin
Result := E.ExtErr;
Exit;
end
else
Exit;
end;
SFX.Options := MapOptionsFrom17(PSFXHeader^.Options);
SFX.OverwriteMode := MapOverwriteModeFromStub(PSFXHeader^.DefOVW);
if (PSFXHeader^.StartMsgType and (MB_OKCANCEL or MB_YESNO)) <> 0 then
begin
if (PSFXHeader^.StartMsgType and MB_OKCANCEL) <> 0 then
X_StartMsg := '1|' + X_StartMsg
else
if (PSFXHeader^.StartMsgType and MB_YESNO) <> 0 then
X_StartMsg := '2|' + X_StartMsg;
end;
SFX.Message := string(X_StartMsg);
SFX.Caption := string(X_Caption);
SFX.DefaultDir := string(X_Path);
SFX.CommandLine := string(X_CmdLine);
SFX.RegFailPath := string(X_RegFailPath);
Result := 0; // all is well
end;
function TZMLister.MapSFXSettings19(Pheder: PByte; Stub: TMemoryStream)
: Integer;
var
Cmnds: PByte;
CStream: TMemoryStream;
Ico: TIcon;
Msg: string;
Delta: Integer;
P: PByte;
PHed: PSFXFileHeader;
Psdat: PSFXStringsData;
begin
Result := ZM_Error({_LINE_}967, ZE_SFXBadRead);
PHed := PSFXFileHeader(Pheder);
CStream := nil;
Cmnds := PByte(@PHed^.StartMsgType);
Inc(Cmnds, Sizeof(Word));
try
// get command strings
if (So_CompressedCmd and PHed^.Options) <> 0 then
begin
P := Cmnds;
Cmnds := nil;
Psdat := PSFXStringsData(P);
Inc(P, Sizeof(TSFXStringsData)); // point to compressed data
Delta := Cardinal(P) - Cardinal(Stub.Memory);
if Stub.Seek(Delta, Word(SoFromBeginning)) = Delta then
begin
CStream := TMemoryStream.Create;
if (UndeflateQ(CStream, Stub, Psdat.CSize, METHOD_DEFLATED, Psdat.CRC)
= 0) and (CStream.Size = Psdat.USize) then
Cmnds := CStream.Memory // ok
else
begin
Trace('Undeflate Error: sfx strings', {_LINE_}989, __UNIT__);
{$IFDEF _DEBUG}
ShowMessage(ZE_InternalError, 'Undeflate error');
{$ENDIF}
end;
end;
end;
if Cmnds <> nil then
begin
// read icon
try