-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZMOprDll.pas
1859 lines (1719 loc) · 48.9 KB
/
ZMOprDll.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 ZMOprDLL;
// ZMDLLOpr.pas - DLL operations and functions
(* ***************************************************************************
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-05
interface
{$INCLUDE '.\ZipVers.inc'}
uses
{$IFDEF VERDXE2up}
System.Classes, WinApi.Windows,
{$ELSE}
Classes, Windows,
{$ENDIF}
ZMHandler;
// {$DEFINE ZDEBUG}
type
TZMDLL = class(TZMOperationRoot)
public
procedure AbortDLL;
end;
type
TZMOpAddStreamToFile = class(TZMDLL)
private
FFileAttr: Dword;
FFileDate: Dword;
FFileName: string;
public
constructor Create(const FileName: string; FileDate, FileAttr: Dword);
function Changes: TZMOperRes; override;
function Execute(TheBody: TZMHandler): Integer; override;
function Name: string; override;
function Needs: TZMOperRes; override;
end;
type
TZMOpAdd = class(TZMDLL)
public
constructor Create;
function Changes: TZMOperRes; override;
function Execute(TheBody: TZMHandler): Integer; override;
function Name: string; override;
function Needs: TZMOperRes; override;
end;
implementation
uses
{$IFDEF VERDXE2up}
System.SysUtils, VCL.Controls, VCL.Graphics, VCL.Dialogs,
{$ELSE}
SysUtils, Controls, Graphics, Dialogs,
{$IFNDEF VERD7up}ZMCompat, {$ENDIF}
{$ENDIF}
Forms, ZipMstr, ZMDelZip, ZMBody, ZMBaseOpr, ZMXcpt, ZMFileOpr, ZMLister,
ZMMsg, ZMUtils, ZMDrv, ZMStructs, ZMUTF8, ZMZipReader, ZMDLLLoad, ZMWinFuncs,
ZMZipBase, ZMZipWriter, ZMCore, ZMCommand;
const
__UNIT__ = 28;
type
TZMDLLOpr = class;
TDZCallback = class
private
FHoldSize: Integer;
PCB: PZCallBackStruct;
function GetActionCode: Integer;
function GetArg1: Cardinal;
function GetArg2: Cardinal;
function GetArg3: Integer;
function GetFile_Size: Int64;
function GetIsZip: Boolean;
function GetMsg: string;
function GetMsg2: string;
function GetOwner: TZMDLLOpr;
function GetWritten: Int64;
procedure SetArg1(const Value: Cardinal);
procedure SetArg2(const Value: Cardinal);
procedure SetArg3(const Value: Integer);
procedure SetFile_Size(const Value: Int64);
procedure SetMsg(const Value: string);
protected
FHeldData: PByte;
function Assign(ZCallBackRec: PZCallBackStruct): Integer;
function CopyData(Dst: PByte; MaxSize: Integer): Boolean;
function GetMsgStr(const Msg: PByte): string;
function HoldData(const Src: PByte; Size: Cardinal): PByte;
function HoldString(const Src: string): PByte;
procedure SetComment(const AStr: AnsiString);
procedure SetData(Src: PByte; Size: Integer);
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
procedure Clear;
property ActionCode: Integer read GetActionCode;
property Arg1: Cardinal read GetArg1 write SetArg1;
property Arg2: Cardinal read GetArg2 write SetArg2;
property Arg3: Integer read GetArg3 write SetArg3;
property File_Size: Int64 read GetFile_Size write SetFile_Size;
property IsZip: Boolean read GetIsZip;
property Msg: string read GetMsg write SetMsg;
property Msg2: string read GetMsg2;
property Owner: TZMDLLOpr read GetOwner;
property Written: Int64 read GetWritten;
end;
TZMDLLOpr = class(TZMFileOpr)
private
FAutoAttr: Cardinal;
FAutoDate: Cardinal;
FCB: TDZCallback;
FDLLOperKey: Cardinal;
FDLLTargetName: string;
FEventErr: string;
// 1 data for dll held until next callback or fini
FHeldData: Pointer;
FIsDestructing: Boolean;
Warnings: Integer;
function DLLStreamClose(ZStreamRec: PZStreamRec): Integer;
function DLLStreamCreate(ZStreamRec: PZStreamRec): Integer;
function DLLStreamIdentify(ZStreamRec: PZStreamRec): Integer;
function DLLToErrCode(DLL_error: Integer): Integer;
procedure DLL_Comment(var Result: Integer);
procedure DLL_Data(var Result: Integer);
procedure DLL_ExtName(var Result: Integer);
procedure DLL_Message(var Result: Integer);
procedure DLL_Password(var Result: Integer);
procedure DLL_Progress(Action: TActionCodes; var Result: Integer);
procedure DLL_SetAddName(var Result: Integer);
procedure DLL_Skipped(var Result: Integer);
function GetAddCompLevel: Integer;
function GetAddFrom: TDateTime;
function GetAddOptions: TZMAddOpts;
function GetAddStoreSuffixes: TZMAddStoreExts;
function GetDLL_Load: Boolean;
function GetExtAddStoreSuffixes: string;
function GetPassword: string;
function GetPasswordReqCount: Integer;
function GetRootDir: string;
function GetZipStream: TMemoryStream;
function IsDestWritable(const Fname: string; AllowEmpty: Boolean): Boolean;
function JoinMVArchive(var TmpZipName: string): Integer;
function RecreateMVArchive(const TmpZipName: string; Recreate: Boolean):
Boolean;
procedure SetAddOptions(const Value: TZMAddOpts);
procedure SetCB(const Value: TDZCallback);
procedure SetDLL_Load(const Value: Boolean);
procedure SetPasswordReqCount(const Value: Integer);
procedure SetRootDir(const Value: string);
protected
FAutoStream: TStream;
function Add: Integer;
function AddStoreExtStr(Options: TZMAddStoreExts): string;
function AddStreamToFile(const FileName: string;
FileDate, FileAttr: Dword): Integer;
function AllocDLLCommand(const FileName: string): PDLLCommands;
procedure CancelSet(Value: Integer);
procedure DestroyDLLCmd(var Rec: PDLLCommands);
function DLLCallback(ZCallBackRec: PZCallBackStruct): Integer;
function DLLStreamOp(Op: TZStreamActions; ZStreamRec: PZStreamRec): Integer;
procedure DLL_Arg(var Result: Integer);
procedure ExtAdd;
function SetupZipCmd(const Value: string): PDLLCommands;
property AddCompLevel: Integer read GetAddCompLevel;
property AddFrom: TDateTime read GetAddFrom;
property AddStoreSuffixes: TZMAddStoreExts read GetAddStoreSuffixes;
property CB: TDZCallback read FCB write SetCB;
property DLLTargetName: string read FDLLTargetName write FDLLTargetName;
property ExtAddStoreSuffixes: string read GetExtAddStoreSuffixes;
property Password: string read GetPassword;
property PasswordReqCount: Integer read GetPasswordReqCount
write SetPasswordReqCount;
property RootDir: string read GetRootDir write SetRootDir;
public
procedure AbortDLL;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
property AddOptions: TZMAddOpts read GetAddOptions write SetAddOptions;
property DLL_Load: Boolean read GetDLL_Load write SetDLL_Load;
property ZipStream: TMemoryStream read GetZipStream;
end;
function ZM_Error(Line, Error: Integer): Integer;
begin
Result := -((__UNIT__ shl ZERR_UNIT_SHIFTS) + (Line shl ZERR_LINE_SHIFTS) or
AbsErr(Error));
end;
(* ? ZCallback
1.76 01 May 2004 RP change return type and value to return flag for exception
1.76 24 April 2004 RP use DLLCallback
1.73 ( 1 June 2003) changed for new callback
{ Dennis Passmore (Compuserve: 71640,2464) contributed the idea of passing an
instance handle to the DLL, and, in turn, getting it back from the callback.
This lets us referance variables in the TZMDLLOpr class from within the
callback function. Way to go Dennis!
Modified by Russell Peters }
*)
function ZCallback(ZCallBackRec: PZCallBackStruct): Longint; stdcall;
begin
Result := CALLBACK_ERROR;
if ZCallBackRec^.Check = ZCallBack_Check then
begin
with TObject(ZCallBackRec^.Caller) as TZMDLLOpr do
Result := DLLCallback(ZCallBackRec);
end;
end;
function ZStreamCallback(ZStreamRec: PZStreamRec): Longint; stdcall;
var
Cnt: Integer;
Op: TZStreamActions;
Strm: TStream;
begin
Result := CALLBACK_ERROR;
try
if ZStreamRec^.Check = ZStream_Check then
begin
with ZStreamRec^ do
begin
Op := TZStreamActions(OpCode);
Result := 0;
case Op of
ZsaIdentify .. ZsaClose:
with TObject(ZStreamRec^.Caller) as TZMDLLOpr do
Result := DLLStreamOp(Op, ZStreamRec);
ZsaPosition: // reposition
begin
{$IFNDEF VERD6up}
if Integer(ArgLL) <> ArgLL then
begin
Strm := TObject(StrmP) as TStream;
ArgLL := Strm.Seek(ArgLL, Word(TSeekOrigin(ArgI)));
if ArgLL >= 0 then
Result := CALLBACK_TRUE;
end;
{$ELSE}
Strm := TObject(StrmP) as TStream;
ArgLL := Strm.Seek(ArgLL, TSeekOrigin(ArgI));
if ArgLL >= 0 then
Result := CALLBACK_TRUE;
{$ENDIF}
end;
ZsaRead: // read
begin
Strm := TObject(StrmP) as TStream;
Cnt := ArgI;
if (Strm.Position + Cnt) > Strm.Size then
Cnt := Integer(Strm.Size - Strm.Position);
ArgI := Strm.Read(BufP^, Cnt);
if ArgI = Cnt then
Result := CALLBACK_TRUE;
end;
ZsaWrite: // Write
begin
Strm := TObject(StrmP) as TStream;
Cnt := ArgI;
ArgI := Strm.Write(BufP^, Cnt);
if ArgI = Cnt then
Result := CALLBACK_TRUE;
end;
end;
end;
end;
except
on E: Exception do
begin
// clear any exceptions
Result := CALLBACK_ERROR;
end;
end;
end;
procedure TZMDLLOpr.AbortDLL;
begin
if FDLLOperKey <> 0 then
_DLL_Abort(Master, FDLLOperKey);
end;
function TZMDLLOpr.Add: Integer;
begin
if Body.Logging then
Body.LogSpecs('');
FAutoStream := nil;
ExtAdd;
Result := Errors.Code;
end;
function TZMDLLOpr.AddStoreExtStr(Options: TZMAddStoreExts): string;
const
SuffixStrings: array [TZMAddStoreSuffixEnum] of PChar = ('gif', 'png', 'z',
'zip', 'zoo', 'arc', 'lzh', 'arj', 'taz', 'tgz', 'lha', 'rar', 'ace', 'cab',
'gz', 'gzip', 'jar', 'exe', '', 'jpg', 'jpeg', '7zp', 'mp3', 'wmv', 'wma',
'dvr-ms', 'avi');
var
O: TZMAddStoreSuffixEnum;
begin
Result := '';
for O := low(TZMAddStoreSuffixEnum) to high(TZMAddStoreSuffixEnum) do
if (O <> AssEXT) and (O in Options) then
Result := Result + '.' + string(SuffixStrings[O]) + ':';
if AssEXT in Options then
Result := Result + ExtAddStoreSuffixes;
end;
(* ? TZMDLLOpr.AddStreamToFile
// 'FileName' is the name you want to use in the zip file to
// store the contents of the stream under.
*)
function TZMDLLOpr.AddStreamToFile(const FileName: string;
FileDate, FileAttr: Dword): Integer;
var
FatDate: Word;
FatTime: Word;
Fn: string;
Ft: TFileTime;
St: TSystemTime;
begin
Fn := Trim(FileName);
if (Length(Fn) = 0) and (IncludeSpecs.Count > 0) then
Fn := Trim(IncludeSpecs[0]);
// Edwin:
// - Fixed: Allow empty stream (Size = 0) to be added to the target .zip archive
// if (Fn = '') or (ZipStream.Size = 0) then
if Fn = '' then
// Edwin end
begin
Result := ZM_Error({_LINE_}376, ZE_NothingToZip);
Exit;
end;
// Edwin:
// - Fixed: AddStreamToFile should not call DriveFolders.ExpandPath, otherwise you won't be able to
// compress a file into the target zip archive into a relative path such as
// `MyFolder1\MyFile1.txt' (relative to the root of the zip file).
// Result := DriveFolders.ExpandPath(Fn, Fn);
// if Result < 0 then
// Exit;
Result := 0;
// Edwin end
// strip drive etc like 1.79
if ExtractFileDrive(Fn) <> '' then
Fn := Copy(Fn, 3, Length(Fn) - 2);
if (Fn <> '') and ((Fn[1] = '/') or (Fn[1] = '\')) then
Fn := Copy(Fn, 2, Length(Fn) - 1);
if NameIsBad(Fn, False) then
begin
Result := Body.PrepareErrMsg(ZE_BadFileName, [Fn], {_LINE_}385, __UNIT__);
ShowError(Result);
end;
if Result = 0 then
begin
Body.ClearIncludeSpecs;
IncludeSpecs.Add('0:' + Fn);
if FileDate = 0 then
begin
GetLocalTime(St);
SystemTimeToFileTime(St, Ft);
FileTimeToDosDateTime(Ft, FatDate, FatTime);
FileDate := (Dword(FatDate) shl 16) + FatTime;
end;
FAutoStream := ZipStream;
FAutoDate := FileDate;
FAutoAttr := FileAttr;
ExtAdd;
Result := -Errors.ExtCode; // ????
end;
end;
procedure TZMDLLOpr.AfterConstruction;
begin
inherited;
FDLLOperKey := 0;
FHeldData := nil;
FCB := TDZCallback.Create;
end;
function TZMDLLOpr.AllocDLLCommand(const FileName: string): PDLLCommands;
var
Opts: Cardinal;
begin
Result := AllocMem(SizeOf(TDLLCommands));
DLLTargetName := FileName;
ZeroMemory(Result, SizeOf(TDLLCommands));
Result^.FVersion := DELZIPVERSION; // version we expect the DLL to be
Result^.FCaller := Self; // point to our VCL instance; returned in Report
Result^.ZCallbackFunc := ZCallback;
// pass addr of function to be called from DLL
Result^.ZStreamFunc := ZStreamCallback;
Result^.FEncodedAs := Ord(Lister.Encoding); // how to interpret existing names
Result^.FFromPage := Lister.Encoding_CP;
if Verbosity >= ZvTrace then
Result^.FVerbosity := -1
else
if Verbosity >= ZvVerbose then
Result^.FVerbosity := 1
else
Result^.FVerbosity := 0;
{ if tracing, we want verbose also }
// used for dialogs (like the pwd dialogs)
if Unattended then
Result^.FHandle := 0
else
Result^.FHandle := Master.Handle;
Result^.FSS := nil;
Opts := DLL_OPT_Quiet; // no DLL error reporting
Result^.FOptions := Opts;
end;
procedure TZMDLLOpr.BeforeDestruction;
begin
FIsDestructing := True; // stop callbacks
AbortDLL;
if FHeldData <> nil then
begin
FreeMem(FHeldData); // release held data
FHeldData := nil;
end;
FreeAndNil(FCB);
inherited;
end;
procedure TZMDLLOpr.CancelSet(Value: Integer);
begin
AbortDLL; // is this too soon
end;
procedure TZMDLLOpr.DestroyDLLCmd(var Rec: PDLLCommands);
begin
if Rec <> nil then
begin
FreeMem(Rec);
Rec := nil;
end;
end;
(* ? TZMDLLOpr.DLLCallback
*)
function TZMDLLOpr.DLLCallback(ZCallBackRec: PZCallBackStruct): Integer;
var
Action: TActionCodes;
begin
Result := CALLBACK_UNHANDLED;
if FIsDestructing then // in destructor return
begin
Exit;
end;
CB.Assign(ZCallBackRec);
Action := TActionCodes(CB.ActionCode and 63);
try
case Action of
ZacMessage:
DLL_Message(Result);
ZacItem .. ZacXProgress:
DLL_Progress(Action, Result);
ZacNewName:
// request for a new path+name just before zipping or extracting
DLL_SetAddName(Result);
ZacPassword:
// New or other password needed during Extract()
DLL_Password(Result);
ZacCRCError:
;
ZacOverwrite:
;
ZacSkipped:
// Extract(UnZip) and Skipped
DLL_Skipped(Result);
ZacComment:
// Add(Zip) FileComments.
DLL_Comment(Result);
ZacData:
// Set Extra Data
DLL_Data(Result);
ZacExtName:
// request for a new path+name just before zipping or extracting
DLL_ExtName(Result);
ZacKey:
begin
FDLLOperKey := CB.Arg1;
Result := 0;
end;
ZacArg:
DLL_Arg(Result);
else
Result := CALLBACK_IGNORED; // unknown
end; { end case }
if (Action < ZacKey) and (Action > ZacMessage) then
begin
KeepAlive;
end;
if Cancel <> 0 then
begin
Result := CALLBACK_CANCEL;
if Body.Logging then
Body.Log(ZM_Error({_LINE_}539, 0), '[CANCEL sent]');
end;
except
on E: Exception do
begin
if FEventErr = '' then
// catch first exception only
FEventErr := ' #' + IntToStr(Ord(Action)) + ' "' + E.Message + '"';
Cancel := ZE_Except;
Result := CALLBACK_EXCEPTION;
if Body.Logging then
Body.Log(ZM_Error({_LINE_}550, 0), '[CALLBACK Exception sent] ' +
FEventErr);
end;
end;
end;
function TZMDLLOpr.DLLStreamClose(ZStreamRec: PZStreamRec): Integer;
var
Strm: TStream;
begin
Result := CALLBACK_UNHANDLED;
if TObject(ZStreamRec^.StrmP) is TStream then
begin
Strm := TStream(ZStreamRec^.StrmP);
if Strm = ZipStream then
begin
FAutoStream := nil;
ZStreamRec^.StrmP := nil;
Result := CALLBACK_TRUE;
end;
end;
end;
function TZMDLLOpr.DLLStreamCreate(ZStreamRec: PZStreamRec): Integer;
begin
Result := CALLBACK_UNHANDLED;
ZStreamRec^.StrmP := nil;
if Assigned(FAutoStream) then
begin
Result := CALLBACK_TRUE;
ZStreamRec^.StrmP := FAutoStream;
FAutoStream.Position := 0;
end;
end;
function TZMDLLOpr.DLLStreamIdentify(ZStreamRec: PZStreamRec): Integer;
begin
Result := CALLBACK_UNHANDLED;
if Assigned(FAutoStream) then
begin
Result := CALLBACK_TRUE;
ZStreamRec^.ArgLL := FAutoStream.Size;
ZStreamRec^.ArgD := FAutoDate;
ZStreamRec^.ArgA := FAutoAttr;
end;
end;
// ALL interface structures BYTE ALIGNED
(* stream operation arg usage
zacStIdentify,
// IN BufP = name
IN Number = number
OUT ArgLL = Size, ArgD = Date, ArgA = Attrs
zacStCreate,
// IN BufP = name
IN Number = number
OUT StrmP = stream
zacStClose,
IN Number = number
IN StrmP = stream
OUT StrmP = stream (= NULL)
zacStPosition,
IN Number = number
IN StrmP = stream, ArgLL = offset, ArgI = from
OUT ArgLL = position
zacStRead,
IN Number = number
IN StrmP = stream, BufP = buf, ArgI = count
OUT ArgI = bytes read
zacStWrite
IN Number = number
IN StrmP = stream, BufP = buf, ArgI = count
OUT ArgI = bytes written
*)
function TZMDLLOpr.DLLStreamOp(Op: TZStreamActions;
ZStreamRec: PZStreamRec): Integer;
begin
Result := CALLBACK_UNHANDLED;
case Op of
ZsaIdentify: // get details for named stream
Result := DLLStreamIdentify(ZStreamRec);
ZsaCreate: // Assign a stream
Result := DLLStreamCreate(ZStreamRec);
ZsaClose: // defaults to freeing stream if not ZipStream
Result := DLLStreamClose(ZStreamRec);
end;
Body.TraceFmt('Stream operation %d on %d returns %d',
[Ord(Op), ZStreamRec^.Number, Result], {_LINE_}637, __UNIT__);
end;
// return proper ErrCode for dll error
function TZMDLLOpr.DLLToErrCode(DLL_error: Integer): Integer;
begin
Result := DLL_error and $3F;
if Result <> 0 then
Result := ZD_GOOD + Result;
if Result > ZD_SKIPPED then
Result := ZD_ERROR;
end;
(* Arg1 = argument
0 = filename
1 = password
2 = RootDir
3 = ExtractDir
4 = Zip comment
5 = FSpecArgs Arg3 = Index
6 = FSpecArgsExcl Arg3 = Index
*)
procedure TZMDLLOpr.DLL_Arg(var Result: Integer);
var
Arg: TCBArgs;
Idx: Integer;
Sr: string;
begin
if CB.Arg1 <= Cardinal(Ord(high(TCBArgs))) then
begin
Arg := TCBArgs(CB.Arg1);
Idx := CB.Arg3;
Sr := '';
if (Arg in [ZcbFSpecArgs, ZcbFSpecArgsExcl]) and (Idx < 0) then
Result := CALLBACK_ERROR
else
if Arg = ZcbComment then
begin // always Ansi
CB.SetComment(Lister.ZipComment);
Result := CALLBACK_TRUE;
end
else
begin
Result := CALLBACK_TRUE;
case Arg of
ZcbFilename:
Sr := DLLTargetName;
ZcbPassword:
Sr := Password;
ZcbRootDir:
Sr := RootDir;
ZcbExtractDir:
; // sr := ExtrBaseDir;
ZcbFSpecArgs:
begin
if Idx >= IncludeSpecs.Count then
Result := CALLBACK_UNHANDLED
else
Sr := IncludeSpecs[Idx];
CB.Arg3 := IncludeSpecs.Count;
end;
ZcbFSpecArgsExcl:
begin
if Idx >= ExcludeSpecs.Count then
Result := CALLBACK_UNHANDLED
else
Sr := ExcludeSpecs[Idx];
CB.Arg3 := ExcludeSpecs.Count;
end;
ZcbSpecials:
Sr := AddStoreExtStr(AddStoreSuffixes);
ZcbTempPath:
Sr := Lister.TempDir;
end;
CB.Msg := Sr;
end;
end
else
Result := CALLBACK_ERROR;
end;
procedure TZMDLLOpr.DLL_Comment(var Result: Integer);
var
FileComment: string;
IsChanged: Boolean;
Ti: Integer;
TmpFileComment: TZMFileCommentEvent;
begin
TmpFileComment := Master.OnFileComment;
if Assigned(TmpFileComment) then
begin
FileComment := CB.Msg2;
IsChanged := False;
TmpFileComment(Master, CB.Msg, FileComment, IsChanged);
if IsChanged then
begin
Result := CALLBACK_TRUE;
Ti := Length(FileComment);
if Ti > 255 then
begin
Ti := 255;
FileComment := Copy(FileComment, 1, 255);
end;
CB.Msg := FileComment;
CB.Arg1 := Ti;
end;
end;
if (Cancel <> 0) and (Result >= CALLBACK_IGNORED) then
Result := CALLBACK_CANCEL;
end;
procedure TZMDLLOpr.DLL_Data(var Result: Integer);
var
Dat: TZMRawBytes;
DataChanged: Boolean;
DatSize: Int64;
IsChanged: Boolean;
LevelChanged: Boolean;
Lvl: Integer;
TmpFileExtra: TZMFileExtraEvent;
TmpSetCompLevel: TZMSetCompLevel;
Xlen: Integer;
begin
TmpFileExtra := Master.OnFileExtra;
TmpSetCompLevel := Master.OnSetCompLevel;
LevelChanged := False;
DataChanged := False;
if Assigned(TmpSetCompLevel) then
begin
IsChanged := False;
Lvl := Integer(CB.Arg2);
TmpSetCompLevel(Master, CB.Msg, Lvl, IsChanged);
if IsChanged and (Lvl in [0 .. 9]) then
begin
CB.Arg2 := Lvl;
LevelChanged := True;
end;
end;
if Assigned(TmpFileExtra) then
begin
DatSize := CB.Arg1; // old Size
SetLength(Dat, DatSize);
if DatSize > 0 then
CB.CopyData(PByte(@Dat[1]), DatSize);
IsChanged := False;
TmpFileExtra(Master, CB.Msg, Dat, IsChanged);
if IsChanged then
begin
DataChanged := True;
Xlen := Length(Dat);
if Xlen > 2047 then // limit
Xlen := 2047;
CB.SetData(PByte(@Dat[1]), Xlen);
end;
end;
if DataChanged then
begin
if LevelChanged then
Result := CALLBACK_3
else
Result := CALLBACK_TRUE;
end
else
begin
if LevelChanged then
Result := CALLBACK_2;
end;
end;
procedure TZMDLLOpr.DLL_ExtName(var Result: Integer);
var
BaseDir: string;
IsChanged: Boolean;
Msg: string;
OldFileName: string;
TmpSetExtName: TZMSetExtNameEvent;
function IsPathOnly(const F: string): Boolean;
var
C: Char;
begin
Result := False;
if F <> '' then
begin
C := F[Length(F)];
if (C = PathDelim) or (C = PathDelimAlt) then
Result := True;
end;
end;
begin
TmpSetExtName := Master.OnSetExtName;
if Assigned(TmpSetExtName) then
begin
Msg := CB.Msg2;
BaseDir := SetSlashW(Msg, PsdExternal);
Msg := CB.Msg;
OldFileName := Msg;
IsChanged := False;
TmpSetExtName(Master, OldFileName, BaseDir, IsChanged);
if IsChanged and (OldFileName <> Msg) and
(IsPathOnly(OldFileName) = IsPathOnly(Msg)) then
begin
CB.Msg := OldFileName;
Result := CALLBACK_TRUE;
end;
end;
end;
procedure TZMDLLOpr.DLL_Message(var Result: Integer);
var
ECode: Integer;
Erm: string;
ErrorCode: Integer;
EType: Integer;
ExtCode: Integer;
Show: Boolean;
TmpMessage: TZMMessageEvent;
begin
Erm := CB.Msg;
ErrorCode := CB.Arg1;
ExtCode := 0;
ECode := 0;
EType := 0;
if ErrorCode <> 0 then
begin
EType := ErrorCode and DZM_Type_Mask;
if EType = DZM_Warning then
Inc(Warnings);
ExtCode := ErrorCode or $40000000;
if ((ErrorCode and $FF) <> 0) and (EType >= DZM_Warning) and
(EType > (Errors.ExtCode and DZM_Type_Mask)) then
Errors.ExtCode := ExtCode; // remember last error
ECode := DLLToErrCode(ErrorCode);
if (EType >= DZM_Message) and ((ErrorCode and DZM_MessageBit) <> 0) then
Erm := ZipLoadStr(ECode) + Erm;
// W'll always keep the last ErrorCode
if (ECode <> 0) and (Errors.Code = 0) then
begin
if (FEventErr <> '') and (ECode = _DZ_ERR_ABORT) then
Erm := ZipFmtLoadStr(ZE_EventEx, [FEventErr]);
end;
Errors.ErrMessage := Erm;
end;
if Body.Logging then
Body.Log(ExtCode, Erm);
TmpMessage := Master.OnMessage;
if Assigned(TmpMessage) then
begin
Show := False;
case EType of
DZM_General, DZM_Error, DZM_Warning, DZM_Message:
Show := True;
DZM_Verbose:
if Verbosity >= ZvVerbose then
Show := True;
DZM_Trace:
if Verbosity >= ZvTrace then
Show := True;
end;
if Show then
begin
if ECode <> 0 then
ECode := ZM_Error({_LINE_}901, ECode);
TmpMessage(Master, ECode, Erm);
end;
end;
KeepAlive; // process messages or check terminate
end;
procedure TZMDLLOpr.DLL_Password(var Result: Integer);
var
IsZip: Boolean;
Pwd: string;
Response: TmsgDlgBtn;
RptCount: Longword;
TmpPasswordError: TZMPasswordErrorEvent;
begin
Pwd := '';
RptCount := CB.Arg1;
Response := MbOK;
IsZip := CB.IsZip;
TmpPasswordError := Master.OnPasswordError;
if Assigned(TmpPasswordError) then
begin
TmpPasswordError(Master, IsZip, Pwd, CB.Msg, RptCount, Response);
if Response <> MbOK then
Pwd := '';
end
else
if IsZip then
Pwd := Master.GetAddPassword(Response)
else
Pwd := Master.GetExtrPassword(Response);
if Pwd <> '' then
begin
CB.Msg := Pwd;
Result := CALLBACK_TRUE;
end
else
begin // no password
RptCount := 0;
Result := CALLBACK_2;
end;
if RptCount > 15 then
RptCount := 15;
CB.Arg1 := RptCount;
if Response = MbCancel then // Cancel
begin
Result := CALLBACK_2;
end
else
if Response = MbNoToAll then // Cancel all
begin
Result := CALLBACK_3;
end
else
if Response = MbAbort then // Abort
begin
Cancel := ZS_Abort;
Result := CALLBACK_ABORT;
end;
end;
procedure TZMDLLOpr.DLL_Progress(Action: TActionCodes; var Result: Integer);
begin
case Action of
ZacItem .. ZacEndOfBatch:
Progress.Written(CB.Written);
end;
case Action of
ZacTick:
KeepAlive;
ZacItem:
Progress.NewItem(CB.Msg, CB.File_Size);
ZacProgress:
Progress.Advance(CB.File_Size);
ZacEndOfBatch:
Progress.EndBatch;
ZacCount:
Progress.TotalCount := CB.Arg1;
ZacSize:
Progress.TotalSize := CB.File_Size;
ZacXItem:
Progress.NewXtraItem(CB.Msg, CB.File_Size);
ZacXProgress:
Progress.AdvanceXtra(CB.File_Size);
end;
Result := 0;
if (Action = ZacItem) and (CB.File_Size = -1) and (Progress.Stop) then