-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathZMBaseOpr.pas
1338 lines (1252 loc) · 37.5 KB
/
ZMBaseOpr.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 ZMBaseOpr;
(*
Derived from
* SFX for DelZip v1.7
* Copyright 2002-2005
* written by Markus Stephany
*)
// ZMBaseOpr.pas - base of operation classes
(* ***************************************************************************
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 2013-12-06
{$I '.\ZipVers.inc'}
interface
uses
{$IFDEF VERDXE2up}
WinApi.Windows, System.SysUtils, System.Classes, Vcl.Forms, Vcl.Graphics,
Vcl.Dialogs, Vcl.Controls,
{$ELSE}
Windows, SysUtils, Classes, Forms, Graphics, Dialogs, Controls,
{$IFNDEF VERD7up}ZMCompat, {$ENDIF}
{$ENDIF}
ZipMstr, ZMBody, ZMLister, ZMMisc, ZMZipReader, ZMZipWriter;
type
TZMBaseOpr = class(TZMBase)
private
FDriveFolders: TZMDriveFolders;
FInterimZip: TZMZipWriter;
FLister: TZMLister;
FSFXBinStream: TMemoryStream;
function CreateStubStream: Boolean;
function GetCurrent: TZMZipReader;
function GetReload: TZMReloads;
function GetSuccessCnt: Integer;
function GetZipFileName: string;
function LoadFromBinFile(var Stub: TStream; var Specified: Boolean)
: Integer;
function LoadFromBinFile1(var Stub: TStream; var Specified: Boolean;
const DefaultName: string): Integer;
function LoadFromResource(var Stub: TStream; const Sfxtyp: string): Integer;
function MapOptionsToStub(Opts: TZMSFXOpts): Word;
function MapOverwriteModeToStub(Mode: TZMOvrOpts): Word;
function NewSFXStub: TMemoryStream;
function RecreateSingle(Intermed, TheZip: TZMZipReader): Integer;
procedure ReplaceIcon(Str: TMemoryStream; OIcon: TIcon);
procedure SetCurrent(const Value: TZMZipReader);
procedure SetInterimZip(const Value: TZMZipWriter);
procedure SetReload(const Value: TZMReloads);
procedure SetSuccessCnt(const Value: Integer);
function WriteIconToStream(Stream: TStream; Icon: HICON;
Width, Height, Depth: Integer): Integer;
protected
procedure CreateInterimZip; virtual;
function CurrentZip(MustExist: Boolean; SafePart: Boolean = False)
: TZMZipReader;
function DetachedSize(Zf: TZMZipReader): Integer;
function FinalizeInterimZip(OrigZip: TZMZipReader): Integer; virtual;
procedure PrepareInterimZip;
function PrepareStub: Integer;
function PrepareZip(Zip: TZMZipReader): Integer;
function Recreate(Intermed, TheZip: TZMZipReader): Integer;
function ReleaseSFXBin: TMemoryStream;
// 1 Rewrite via an intermediate
function Remake(CurZip: TZMZipReader; ReqCnt: Integer;
All: Boolean): Integer;
procedure VerifySource(SrcZip: TZMZipReader);
function WriteDetached(Zf: TZMZipReader): Integer;
function ZipMessageDlgEx(const Title, Msg: string; Context: Integer;
Btns: TMsgDlgButtons): TModalResult;
property Current: TZMZipReader read GetCurrent write SetCurrent;
property DriveFolders: TZMDriveFolders read FDriveFolders;
property Lister: TZMLister read FLister;
property Reload: TZMReloads read GetReload write SetReload;
property SFXBinStream: TMemoryStream read FSFXBinStream write FSFXBinStream;
property SuccessCnt: Integer read GetSuccessCnt write SetSuccessCnt;
property ZipFileName: string read GetZipFileName;
public
constructor Create(TheLister: TZMLister); overload;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
property InterimZip: TZMZipWriter read FInterimZip write SetInterimZip;
end;
implementation
uses
{$IFDEF VERDXE2up}
System.UITypes, Winapi.ShellAPI,
{$ELSE}
ShellAPI,
{$ENDIF}
ZMMsg, ZMDrv, ZMUtils, ZMZipBase, ZMZipDirectory, ZMUTF8, ZMWinFuncs,
ZMEngine, ZMStructs, ZMSFXInt, ZMXcpt;
const
__UNIT__ = 3;
const
SFXBinDefault: string = 'ZMSFX192.bin';
SFXBinUDefault: string = 'ZMSFXU192.bin';
SFXBufSize: Word = $2000;
ExtExe = 'exe';
DotExtExe = '.' + ExtExe;
const
MinStubSize = 12000;
MaxStubSize = 80000;
BufSize = 10240;
function ZM_Error(Line, Error: Integer): Integer;
begin
Result := -((__UNIT__ shl ZERR_UNIT_SHIFTS) + (Line shl ZERR_LINE_SHIFTS) or
AbsErr(Error));
end;
type
TZMLoader = class(TZMZipWriter)
private
FForZip: TZMZipReader;
FOpr: TZMBaseOpr;
procedure SetForZip(const Value: TZMZipReader);
protected
function AddStripped(const Rec: TZMEntryBase): Integer;
function BeforeCommit: Integer; override;
function FixHeaderNames: Integer; override;
function PrepareDetached: Integer;
function StripEntries: Integer;
property Opr: TZMBaseOpr read FOpr;
public
constructor Create(TheLister: TZMLister; TheOpr: TZMBaseOpr);
procedure AfterConstruction; override;
property ForZip: TZMZipReader read FForZip write SetForZip;
end;
type
TZMEntryDetached = class(TZMEntryWriter)
public
function Process: Int64; override;
function ProcessSize: Int64; override;
end;
function WriteCommand(Dest: TMemoryStream; const Cmd: string; Ident: Integer)
: Integer; forward;
function WriteCommand(Dest: TMemoryStream; const Cmd: string;
Ident: Integer): Integer;
var
Ucmd: UTF8String;
Z: Byte;
begin
Result := 0;
if Length(Cmd) > 0 then
begin
Ucmd := AsUTF8Str(Cmd);
Dest.Write(Ident, 1);
Result := Dest.Write(PAnsiChar(Ucmd)^, Length(Ucmd)) + 2;
Z := 0;
Dest.Write(Z, 1);
end;
end;
{ TZMBaseOpr }
constructor TZMBaseOpr.Create(TheLister: TZMLister);
begin
inherited Create(TZMBody(TheLister));
FLister := TheLister;
end;
procedure TZMBaseOpr.AfterConstruction;
begin
inherited;
FDriveFolders := TZMDriveFolders.Create(Body);
FSFXBinStream := nil;
end;
procedure TZMBaseOpr.BeforeDestruction;
begin
FDriveFolders.Free;
if FInterimZip <> nil then
FInterimZip.Free;
FreeAndNil(FSFXBinStream);
inherited;
end;
procedure TZMBaseOpr.CreateInterimZip;
begin
InterimZip := nil;
end;
function TZMBaseOpr.CreateStubStream: Boolean;
const
MinVers = 1900000;
var
BinStub: TStream;
BinVers: Integer;
Err: Boolean;
ResStub: TStream;
ResVers: Integer;
Stub: TStream;
Stubname: string;
UseBin: Boolean;
XPath: string;
begin
// what type of bin will be used
Stub := nil;
ResStub := nil;
BinStub := nil;
BinVers := -1;
FreeAndNil(FSFXBinStream); // dispose of existing (if any)
try
// load it either from resource (if bcsfx##.res has been linked to the executable)
// or by loading from file in SFXPath and check both versions if available
Stubname := DZRES_SFX;
Err := False; // resource stub not found
XPath := Lister.SFX.Path;
if (Length(XPath) > 1) and (XPath[1] = '>') and (XPath[Length(XPath)] = '<')
then
begin
// must use from resource
Stubname := Copy(XPath, 2, Length(XPath) - 2);
if Stubname = '' then
Stubname := DZRES_SFX;
ResVers := LoadFromResource(ResStub, Stubname);
if ResVers < MinVers then
Err := True;
end
else
begin
// get from resource if it exists
ResVers := LoadFromResource(ResStub, DZRES_SFX);
// load if exists from file
BinVers := LoadFromBinFile(BinStub, UseBin);
if UseBin then
ResVers := 0;
end;
if not Err then
begin
// decide which will be used
if (BinVers >= MinVers) and (BinVers >= ResVers) then
Stub := BinStub
else
begin
if ResVers >= MinVers then
Stub := ResStub
else
Err := True;
end;
end;
if Stub <> nil then
begin
FSFXBinStream := TMemoryStream.Create();
try
if FSFXBinStream.CopyFrom(Stub, Stub.Size - Sizeof(Integer)) <>
(Stub.Size - Sizeof(Integer)) then
raise EZipMaster.CreateMsg(Body, ZE_CopyError, {_LINE_}301, __UNIT__);
FSFXBinStream.Position := 0;
if Assigned(Lister.SFX.Icon) then
ReplaceIcon(FSFXBinStream, SFX.Icon);
FSFXBinStream.Position := 0;
except
FreeAndNil(FSFXBinStream);
end;
end;
finally
FreeAndNil(ResStub);
FreeAndNil(BinStub);
end;
if Err then
raise EZipMaster.CreateMsgFmt(Body, ZE_NoZipSFXBin, [Stubname], {_LINE_}315,
__UNIT__);
Result := FSFXBinStream <> nil;
end;
function TZMBaseOpr.CurrentZip(MustExist: Boolean; SafePart: Boolean = False)
: TZMZipReader;
begin
Result := Lister.CurrentZip(MustExist, SafePart);
end;
function TZMBaseOpr.DetachedSize(Zf: TZMZipReader): Integer;
var
Data: TZMRawBytes;
Has64: Boolean;
Ix: Integer;
Rec: TZMEntryBase;
Sz: Integer;
begin
Result := -1;
ASSERT(Assigned(Zf), 'no input');
// Diag('Write file');
if not Assigned(Zf) then
Exit;
if FSFXBinStream = nil then
begin
Result := PrepareStub;
if Result < 0 then
Exit;
end;
Result := FSFXBinStream.Size;
Has64 := False;
// add approximate central directory size
Rec := Zf.FirstRec;
while Rec <> nil do
begin
Result := Result + Sizeof(TZipCentralHeader) + Rec.FileNameLen;
if Rec.ExtraFieldLength > 4 then
begin
Ix := 0;
Sz := 0;
Data := Rec.ExtraField;
if XData(Data, Zip64_data_tag, Ix, Sz) then
begin
Result := Result + Sz;
Has64 := True;
end;
if XData(Data, UPath_Data_Tag, Ix, Sz) then
Result := Result + Sz;
if XData(Data, NTFS_data_tag, Ix, Sz) and (Sz >= 36) then
Result := Result + Sz;
end;
Rec := Rec.Next;
end;
Result := Result + Sizeof(TZipEndOfCentral);
if Has64 then
begin
// also has EOC64
Inc(Result, Sizeof(TZip64EOCLocator));
Inc(Result, Zf.Z64VSize);
end;
end;
function TZMBaseOpr.FinalizeInterimZip(OrigZip: TZMZipReader): Integer;
begin
if OrigZip = nil then
OrigZip := Lister.Current;
OrigZip.File_Reopen(FmOpenRead or FmShareDenyWrite);
Result := InterimZip.Commit(ZwoZipTime in WriteOptions);
OrigZip.File_Close;
InterimZip.File_Close;
if Result < 0 then
raise EZipMaster.CreateMsg(Body, Result, {_LINE_}388, __UNIT__);
PrepareZip(OrigZip);
// Recreate like orig
Result := Recreate(InterimZip, OrigZip);
if Result < 0 then
raise EZipMaster.CreateMsg(Body, Result, {_LINE_}393, __UNIT__);
end;
function TZMBaseOpr.GetCurrent: TZMZipReader;
begin
Result := Lister.Current;
end;
function TZMBaseOpr.GetReload: TZMReloads;
begin
Result := Lister.Reload;
end;
function TZMBaseOpr.GetSuccessCnt: Integer;
begin
Result := Lister.SuccessCnt;
end;
function TZMBaseOpr.GetZipFileName: string;
begin
Result := Lister.ZipFileName;
end;
function TZMBaseOpr.LoadFromBinFile(var Stub: TStream;
var Specified: Boolean): Integer;
begin
{$IFDEF UNICODE}
Result := LoadFromBinFile1(Stub, Specified, SFXBinUDefault);
if Result <= 0 then
Result := LoadFromBinFile1(Stub, Specified, SFXBinDefault);
{$ELSE}
Result := LoadFromBinFile1(Stub, Specified, SFXBinDefault);
if Result <= 0 then
Result := LoadFromBinFile1(Stub, Specified, SFXBinUDefault);
{$ENDIF}
end;
function TZMBaseOpr.LoadFromBinFile1(var Stub: TStream; var Specified: Boolean;
const DefaultName: string): Integer;
var
BinExists: Boolean;
Binpath: string;
Path: string;
XPath: string;
begin
Result := -1;
Specified := False;
XPath := SFX.Path;
Path := XPath;
// if no name specified use default
if ExtractFileName(XPath) = '' then
Path := Path + DefaultName;
Binpath := Path;
if (Length(XPath) > 1) and ((XPath[1] = '.') or (ExtractFilePath(XPath) <> ''))
then
begin
// use specified
Specified := True;
if XPath[1] = '.' then // relative to program
Binpath := PathConcat(ExtractFilePath(ParamStr(0)), Path);
BinExists := FileExists(Binpath);
end
else
begin
// Try the application directory.
Binpath := DelimitPath(ExtractFilePath(ParamStr(0)), True) + Path;
BinExists := FileExists(Binpath);
if not BinExists then
begin
// Try the current directory.
Binpath := Path;
BinExists := FileExists(Binpath);
end;
end;
if BinExists then
begin
try
Stub := TFileStream.Create(Binpath, FmOpenRead or FmShareDenyWrite);
if (Stub.Size > MinStubSize) and (Stub.Size < MaxStubSize) then
begin
Stub.ReadBuffer(Result, Sizeof(Integer));
end;
Body.TraceFmt('found stub: %s %s', [XPath, VersStr(Result)], {_LINE_}475,
__UNIT__);
except
Result := -5;
end;
end;
end;
function TZMBaseOpr.LoadFromResource(var Stub: TStream;
const Sfxtyp: string): Integer;
var
Rname: string;
begin
Result := -2;
Rname := Sfxtyp;
Stub := OpenResStream(Rname, RT_RCDATA);
if (Stub <> nil) and (Stub.Size > MinStubSize) and (Stub.Size < MaxStubSize)
then
begin
Stub.ReadBuffer(Result, Sizeof(Integer));
Body.TraceFmt('resource stub: %s', [VersStr(Result)], {_LINE_}495,
__UNIT__);
end;
end;
function TZMBaseOpr.MapOptionsToStub(Opts: TZMSFXOpts): Word;
begin
Result := 0;
if SoAskCmdLine in Opts then
Result := Result or So_AskCmdLine;
if SoAskFiles in Opts then
Result := Result or So_AskFiles;
if SoHideOverWriteBox in Opts then
Result := Result or So_HideOverWriteBox;
if SoAutoRun in Opts then
Result := Result or So_AutoRun;
if SoNoSuccessMsg in Opts then
Result := Result or So_NoSuccessMsg;
if SoExpandVariables in Opts then
Result := Result or So_ExpandVariables;
if SoInitiallyHideFiles in Opts then
Result := Result or So_InitiallyHideFiles;
if SoForceHideFiles in Opts then
Result := Result or So_ForceHideFiles;
if SoCheckAutoRunFileName in Opts then
Result := Result or So_CheckAutoRunFileName;
if SoCanBeCancelled in Opts then
Result := Result or So_CanBeCancelled;
if SoCreateEmptyDirs in Opts then
Result := Result or So_CreateEmptyDirs;
if SoSuccessAlways in Opts then
Result := Result or So_SuccessAlways;
end;
function TZMBaseOpr.MapOverwriteModeToStub(Mode: TZMOvrOpts): Word;
begin
case Mode of
OvrAlways:
Result := Som_Overwrite;
OvrNever:
Result := Som_Skip;
else
Result := Som_Ask;
end;
end;
function TZMBaseOpr.NewSFXStub: TMemoryStream;
begin
Result := nil;
if PrepareStub = 0 then
Result := ReleaseSFXBin;
end;
procedure TZMBaseOpr.PrepareInterimZip;
var
CurZip: TZMZipReader;
Err: Integer;
begin
CurZip := Lister.Current;
CreateInterimZip;
ShowProgress := ZspFull;
InterimZip.ZipComment := CurZip.ZipComment;
if (CurZip.OpenRet >= 0) and CurZip.MultiDisk then
begin
Err := InterimZip.RefuseWriteSplit;
if Err <> 0 then
raise EZipMaster.CreateMsg(Body, Err, {_LINE_}561, __UNIT__);
end;
if (not(ZwoDiskSpan in Body.WriteOptions)) and
(UpperCase(ExtractFileExt(CurZip.ReqFileName)) = EXT_EXE) then
begin
InterimZip.UseSFX := True;
InterimZip.Stub := NewSFXStub;
end;
end;
function TZMBaseOpr.PrepareStub: Integer;
var
Cdata: TSFXStringsData;
Deflater: TZMCompressor;
Ds: TMemoryStream;
Err: Integer;
I: Integer;
L: Integer;
Ms: TMemoryStream;
SFXBlkSize: Integer;
SFXHead: TSFXFileHeader;
SFXMsg: string;
SFXMsgFlags: Word;
Want: Integer;
begin
Result := -ZE_Unknown;
if not CreateStubStream then
begin
Result := ZM_Error({_LINE_}589, ZE_Unknown);
Exit;
end;
SFXMsg := SFX.Message;
SFXMsgFlags := MB_OK;
if (Length(SFXMsg) >= 1) then
begin
Want := 1; // want the lot
if (Length(SFXMsg) > 1) and (SFXMsg[2] = '|') then
begin
case SFXMsg[1] of
'1':
SFXMsgFlags := MB_OKCANCEL or MB_ICONINFORMATION;
'2':
SFXMsgFlags := MB_YESNO or MB_ICONQUESTION;
'|':
Want := 2;
end;
if SFXMsgFlags <> MB_OK then
Want := 3;
end;
if Want > 1 then
SFXMsg := Copy(SFXMsg, Want, 2048);
end;
try
// create header
SFXHead.Signature := SFX_HEADER_SIG;
SFXHead.Options := MapOptionsToStub(SFX.Options);
SFXHead.DefOVW := MapOverwriteModeToStub(SFX.OverwriteMode);
SFXHead.StartMsgType := SFXMsgFlags;
Ds := nil;
Ms := TMemoryStream.Create;
try
WriteCommand(Ms, SFX.Caption, Sc_Caption);
WriteCommand(Ms, SFX.CommandLine, Sc_CmdLine);
WriteCommand(Ms, SFX.DefaultDir, Sc_Path);
WriteCommand(Ms, SFX.Message, Sc_StartMsg);
WriteCommand(Ms, SFX.RegFailPath, Sc_RegFailPath);
L := 0;
Ms.WriteBuffer(L, 1);
// check string lengths
if Ms.Size > 4000 then
raise EZipMaster.CreateMsg(Body, ZE_StringTooLong, {_LINE_}631,
__UNIT__);
if Ms.Size > 100 then
begin
Cdata.USize := Ms.Size;
Ms.Position := 0;
Ds := TMemoryStream.Create;
Deflater := TZMCompressor.Create;
Deflater.OutStream := Ds;
Deflater.InStream := Ms;
Deflater.InSize := Ms.Size;
Err := Deflater.Prepare(METHOD_DEFLATED);
if Err = 0 then
Err := Deflater.Compress; // (8);
if Err = 0 then
begin
Cdata.CSize := Ds.Size;
if (Ms.Size > (Cdata.CSize + Sizeof(Cdata))) then
begin
// use compressed
Ms.Size := 0;
Ds.Position := 0;
Cdata.CRC := Deflater.CRC;
Ms.WriteBuffer(Cdata, Sizeof(Cdata));
Ms.CopyFrom(Ds, Ds.Size);
SFXHead.Options := SFXHead.Options or So_CompressedCmd;
end;
end;
end;
// DWord Alignment.
I := Ms.Size and 3;
if I <> 0 then
Ms.WriteBuffer(L, 4 - I); // dword align
SFXBlkSize := Sizeof(TSFXFileHeader) + Ms.Size;
// // create header
SFXHead.Size := Word(SFXBlkSize);
FSFXBinStream.Seek(0, SoFromEnd);
FSFXBinStream.WriteBuffer(SFXHead, Sizeof(SFXHead));
L := SFXBlkSize - Sizeof(SFXHead);
I := Ms.Size;
if I > 0 then
begin
Ms.Position := 0;
FSFXBinStream.CopyFrom(Ms, I);
Dec(L, I);
end;
// check DWORD align
if L <> 0 then
raise EZipMaster.CreateMsg(Body, ZE_InternalError, {_LINE_}681,
__UNIT__);
Result := 0;
finally
Ms.Free;
Ds.Free;
end;
except
on E: EZipMaster do
begin
FreeAndNil(FSFXBinStream);
ShowExceptionError(E);
Result := E.ExtErr;
end
else
begin
FreeAndNil(FSFXBinStream);
Result := ZM_Error({_LINE_}699, ZE_Unknown);
end;
end;
end;
function TZMBaseOpr.PrepareZip(Zip: TZMZipReader): Integer;
begin
Result := Zip.RefuseWriteSplit;
if Result <> 0 then
begin
Result := ZM_Error({_LINE_}709, Result);
Exit;
end;
if (UpperCase(ExtractFileExt(Zip.ReqFileName)) = EXT_EXE) then
begin
Zip.UseSFX := True;
Zip.Stub := NewSFXStub;
end;
Result := 0;
end;
(* ? TZMBaseOpr.Recreate
recreate the 'theZip' file from the intermediate result
to make as SFX
- theZip.UseSFX is set
- theZip.Stub must hold the stub to use
*)
function TZMBaseOpr.Recreate(Intermed, TheZip: TZMZipReader): Integer;
var
Czip: TZMZipReader;
DestZip: TZMZipCopier;
DetchSFX: Boolean;
Detchsz: Integer;
Existed: Boolean;
OrigKeepFreeDisk1: Cardinal;
R: Integer;
Tmp: string;
WantNewDisk: Boolean;
begin
Detchsz := 0;
DetchSFX := False;
Existed := (Zfi_Loaded and TheZip.Info) <> 0;
if TheZip.MultiDisk or ((not Existed) and (ZwoDiskSpan in Lister.WriteOptions))
then
begin
Body.TraceFmt('Recreate multi-part: %s', [TheZip.ReqFileName], {_LINE_}744,
__UNIT__);
if TheZip.UseSFX then
DetchSFX := True;
Result := ZM_Error({_LINE_}748, ZE_Unknown);
Intermed.File_Close;
Czip := TheZip;
// theZip must have proper stub
if DetchSFX and not Assigned(Czip.Stub) then
begin
Result := ZM_Error({_LINE_}754, ZE_SFXCopyError);
// no stub available - cannot convert
Exit;
end;
WantNewDisk := True; // assume Require to ask for new disk
if Existed then
begin
Czip.SeekDisk(0, True); // ask to enter the first disk again
Czip.File_Close; ////****
WantNewDisk := False;
end;
Tmp := TheZip.ReqFileName;
if DetchSFX then
begin
Body.Trace('Recreate detached SFX', {_LINE_}768, __UNIT__);
// allow room detchSFX stub
Detchsz := DetachedSize(Intermed);
Tmp := ChangeFileExt(Tmp, EXT_ZIP); // name of the zip files
end;
// now create the spanned archive similar to theZip from Intermed
OrigKeepFreeDisk1 := Span.KeepFreeOnDisk1;
DestZip := TZMZipCopier.Create(Lister);
try
DestZip.ArchiveName := Tmp;
DestZip.WorkDrive.DriveStr := Tmp;
DestZip.ReqFileName := TheZip.ReqFileName;
Span.KeepFreeOnDisk1 := Span.KeepFreeOnDisk1 + Cardinal(Detchsz);
ShowProgress := ZspExtra;
DestZip.TotalDisks := 0;
if DetchSFX and (DestZip.Numbering = ZnsExt) then
DestZip.Numbering := ZnsName
else
DestZip.Numbering := TheZip.Numbering; // number same as source
DestZip.PrepareWrite(ZwMultiple);
DestZip.NewDisk := WantNewDisk;
DestZip.File_Size := Intermed.File_Size; // to calc TotalDisks
Intermed.File_Open('', FmOpenRead or FmShareDenyWrite);
DestZip.StampDate := Intermed.FileDate;
AnswerAll := True;
R := DestZip.WriteFile(Intermed, True);
DestZip.File_Close;
if R < 0 then
raise EZipMaster.CreateMsg(Body, R, {_LINE_}796, __UNIT__);
if DetchSFX then
begin
DestZip.DiskNr := 0;
if DestZip.WorkDrive.DriveIsFloppy then
DestZip.ArchiveName := Tmp
else
DestZip.ArchiveName := DestZip.CreateMVFileNameEx(Tmp, False, False);
DestZip.SeekDisk(0, False);
DestZip.AssignStub(Czip);
DestZip.ArchiveName := Tmp; // restore base name
if WriteDetached(DestZip) >= 0 then
Result := 0;
end
else
Result := 0;
finally
Intermed.File_Close;
Span.KeepFreeOnDisk1 := OrigKeepFreeDisk1;
DestZip.Free;
end;
TheZip.Invalidate; // must reload
end
else
// not split
Result := RecreateSingle(Intermed, TheZip); // just copy it
end;
(* ? TZMBaseOpr.RecreateSingle
Recreate the 'current' file from the intermediate result
to make as SFX
- Current.UseSFX is set
- Current.Stub must hold the stub to use
*)
function TZMBaseOpr.RecreateSingle(Intermed, TheZip: TZMZipReader): Integer;
var
DestZip: TZMZipCopier;
begin
TheZip.File_Close;
Body.TraceFmt('Replacing: %s', [TheZip.ReqFileName], {_LINE_}835, __UNIT__);
Result := _Z_EraseFile(TheZip.ReqFileName, HowToDelete = HtdAllowUndo);
if Result > 0 then // ignore file does not exist
begin
Body.InformFmt('EraseFile failed for: %s', [TheZip.ReqFileName],
{_LINE_}840, __UNIT__);
raise EZipMaster.CreateMsgFmt(Body, ZE_FileError, [TheZip.ReqFileName],
{_LINE_}842, __UNIT__);
end;
// rename/copy Intermed
AnswerAll := True;
if Assigned(TheZip.Stub) and TheZip.UseSFX and (Intermed.Sig <> ZfsDOS) then
begin // rebuild with sfx
Body.Trace('Rebuild with SFX', {_LINE_}848, __UNIT__);
Intermed.File_Close;
Intermed.File_Open('', FmOpenRead or FmShareDenyWrite);
Result := Intermed.OpenZip(False, False);
if Result < 0 then
Exit;
DestZip := TZMZipCopier.Create(Lister);
try
DestZip.AssignStub(TheZip);
DestZip.UseSFX := True;
DestZip.StampDate := Intermed.StampDate; // will be 'orig' or now
DestZip.DiskNr := 0;
DestZip.ZipComment := TheZip.ZipComment; // keep orig
ShowProgress := ZspExtra;
DestZip.File_Create(TheZip.ReqFileName);
Result := DestZip.WriteFile(Intermed, True);
Intermed.File_Close;
DestZip.File_Close;
if Result < 0 then
raise EZipMaster.CreateMsg(Body, Result, {_LINE_}867, __UNIT__)
finally
DestZip.Free;
end;
end
else
begin
TheZip.File_Close;
Result := Body.PrepareErrMsg(ZE_FileError, [TheZip.ReqFileName],
{_LINE_}876, __UNIT__);
if Intermed.File_Rename(TheZip.ReqFileName) then
Result := 0;
end;
TheZip.Invalidate; // changed - must reload
end;
function TZMBaseOpr.ReleaseSFXBin: TMemoryStream;
begin
Result := FSFXBinStream;
FSFXBinStream := nil;
end;
// write to intermediate then recreate as original
function TZMBaseOpr.Remake(CurZip: TZMZipReader; ReqCnt: Integer;
All: Boolean): Integer;
var
Intermed: TZMZipCopier;
Res: Integer;
begin
Result := 0;
Intermed := TZMZipCopier.Create(Lister);
try
if not Intermed.File_CreateTemp(PRE_INTER, '') then
raise EZipMaster.CreateMsg(Body, ZE_NoOutFile, {_LINE_}900, __UNIT__);
ShowProgress := ZspFull;
Intermed.ZipComment := CurZip.ZipComment;
CurZip.File_Reopen(FmOpenRead or FmShareDenyWrite);
Res := Intermed.WriteFile(CurZip, All);
CurZip.File_Close;
Intermed.File_Close;
if Res < 0 then
raise EZipMaster.CreateMsg(Body, Res, {_LINE_}908, __UNIT__);
Result := Intermed.Count; // number of remaining files
if (ReqCnt >= 0) and (Result <> ReqCnt) then
raise EZipMaster.CreateMsg(Body, ZE_InternalError, {_LINE_}911, __UNIT__);
// Recreate like orig
Res := Recreate(Intermed, CurZip);
if Res < 0 then
raise EZipMaster.CreateMsg(Body, Res, {_LINE_}915, __UNIT__);
finally
Intermed.Free; // also delete temp file
end;
end;
// replaces an icon in an executable file (stream)
procedure TZMBaseOpr.ReplaceIcon(Str: TMemoryStream; OIcon: TIcon);
var
Bad: Boolean;
HdrSection: TImageSectionHeader;
I: Integer;
OriInfo: BitmapInfoHeader;
PIDE: PIconDirEntry;
RecIcon: TImageResourceDataEntry;
StrIco: TMemoryStream;
begin
Bad := True;
Lister.LocateFirstIconHeader(Str, HdrSection, RecIcon);
Str.Seek(Integer(HdrSection.PointerToRawData) -
Integer(HdrSection.VirtualAddress) + Integer(RecIcon.OffsetToData),
SoFromBeginning);
if (Str.Read(OriInfo, Sizeof(BitmapInfoHeader)) <> Sizeof(BitmapInfoHeader))
then
raise EZipMaster.CreateMsg(Body, ZE_NoCopyIcon, {_LINE_}939, __UNIT__);
// now check the icon
StrIco := TMemoryStream.Create;
try
if WriteIconToStream(StrIco, OIcon.Handle, OriInfo.BiWidth,
OriInfo.BiHeight div 2, OriInfo.BiBitCount) <= 0 then
raise EZipMaster.CreateMsg(Body, ZE_NoIcon, {_LINE_}946, __UNIT__);
// now search for matching icon
with PIconDir(StrIco.Memory)^ do
begin
if (ResType <> RES_ICON) or (ResCount < 1) or (Reserved <> 0) then
raise EZipMaster.CreateMsg(Body, ZE_NoIcon, {_LINE_}952, __UNIT__);
for I := 0 to Pred(ResCount) do
begin
PIDE := PIconDirEntry(PAnsiChar(StrIco.Memory) + Sizeof(TIconDir) +
(I * Sizeof(TIconDirEntry)));
if (PIDE^.DwBytesInRes = RecIcon.Size) and (PIDE^.BReserved = 0) then
begin
// matching icon found, replace
StrIco.Seek(PIDE^.DwImageOffset,
{$IFDEF VERPre6}Word{$ENDIF}(TSeekOrigin(SoFromBeginning)));
Str.Seek(Integer(HdrSection.PointerToRawData) -
Integer(HdrSection.VirtualAddress) + Integer(RecIcon.OffsetToData),
SoFromBeginning);
if Str.CopyFrom(StrIco, RecIcon.Size) <> Integer(RecIcon.Size) then
raise EZipMaster.CreateMsg(Body, ZE_NoCopyIcon, {_LINE_}967,
__UNIT__);
// ok and out
Bad := False;
end;
end;
end;
finally
StrIco.Free;
end;
if Bad then
// no icon copied, so none of matching size found
raise EZipMaster.CreateMsg(Body, ZE_NoIconFound, {_LINE_}980, __UNIT__);
end;
procedure TZMBaseOpr.SetCurrent(const Value: TZMZipReader);
begin
Lister.Current := Value;
end;
procedure TZMBaseOpr.SetInterimZip(const Value: TZMZipWriter);
begin
if Value <> FInterimZip then
begin
FInterimZip.Free;
FInterimZip := Value;
end;
end;
procedure TZMBaseOpr.SetReload(const Value: TZMReloads);
begin
Lister.Reload := Value;
end;
procedure TZMBaseOpr.SetSuccessCnt(const Value: Integer);
begin