-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZMEngine.pas
678 lines (614 loc) · 17.6 KB
/
ZMEngine.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
unit ZMEngine;
// ZMEngine.pas - Compress/decompress and crypt engines interface
(* ***************************************************************************
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
{$INCLUDE '.\ZipVers.inc'}
{$Q-} // range check off
interface
uses
{$IFDEF VERDXE2up}
System.Classes, WinApi.Windows, System.SysUtils;
{$ELSE}
Windows, Classes, SysUtils;
{$ENDIF}
type
TZM_ProgressEvent = procedure(Sender: TObject; const Count: Integer;
IsRead: Boolean) of object;
type
TZMCryptTypes = (ZcNone, ZcZip);
type
TZMBuffer = class
private
FActualSize: Integer;
FBufPtr: PByte;
FMyBuffer: array of Byte;
FSize: Integer;
procedure Resize(const Value: Integer);
procedure SetSize(const Value: Integer);
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
function CalcBufSize(Value: Int64): Integer;
procedure Clean;
property BufPtr: PByte read FBufPtr;
property Size: Integer read FSize write SetSize;
end;
type
TZMCryptorBase = class
protected
function GetState: Integer; virtual; abstract;
public
procedure Clear; virtual; abstract;
function Supports(CryptType: TZMCryptTypes): Boolean; virtual; abstract;
property State: Integer read GetState;
end;
type
TZMDecryptorBase = class(TZMCryptorBase)
public
function DecodeInit(RefValue: Cardinal): Integer; virtual; abstract;
function ReadIn(Buf: PByte; const Count: Integer): Integer;
virtual; abstract;
// 1 Return <0 _ error, 0 _ match, >0 _ no match
function Unlock(const PassPhrase: AnsiString): Integer; virtual; abstract;
end;
type
TZMEncryptorBase = class(TZMCryptorBase)
public
function EncodeInit(const PassPhrase: AnsiString; RefValue: DWORD): Integer;
virtual; abstract;
function WriteOut(const Buf: PByte; const Count: Integer): Integer;
virtual; abstract;
end;
type
TZMCoDecBase = class
public
function Execute: Integer; virtual; abstract;
function Initial(CompressionType: Integer): Integer; virtual; abstract;
function Supports(CompressionType: Integer): Boolean; virtual; abstract;
end;
type
TZMEngine = class
private
FCompLevel: Integer;
FCRC: Cardinal;
FEncrypted: Boolean;
FInBuffer: TZMBuffer;
FInSize: Int64;
FInStrm: TStream;
FOnProgress: TZM_ProgressEvent;
FOutBuffer: TZMBuffer;
FOutSize: Int64;
FOutStrm: TStream;
FState: Integer;
FTheCryptor: TZMCryptorBase;
FToBeWritten: Int64;
FWrote: Int64;
FZBufferSize: Cardinal;
function CalcBufSize(Value: Int64): Integer;
procedure MakeBuffers;
private
FCoDec: TZMCoDecBase;
procedure SetTheCryptor(const Value: TZMCryptorBase);
protected
procedure Finish; virtual;
function InitialCoDec(CompressionType: Integer): Integer; virtual; abstract;
procedure SetCoDec(ACoDec: TZMCoDecBase);
function SetCryptor(CryptType: TZMCryptTypes): Integer; virtual; abstract;
property State: Integer read FState;
property TheCryptor: TZMCryptorBase read FTheCryptor write SetTheCryptor;
public
constructor Create; overload;
constructor Create(BufSize: Int64); overload;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
function DoReadIn(Buf: PByte; const Count: Integer): Integer;
function DoWriteOut(const Buf: PByte; const Count: Integer): Integer;
function Prepare(CompressionType: Integer): Integer;
function PrepareCrypt(CryptType: TZMCryptTypes): Integer;
function ReadIn(Buf: PByte; const Count: Integer): Integer; virtual;
function WriteOut(const Buf: PByte; const Count: Integer): Integer; virtual;
property CompLevel: Integer read FCompLevel write FCompLevel;
property CRC: Cardinal read FCRC write FCRC;
property Encrypted: Boolean read FEncrypted;
property InBuffer: TZMBuffer read FInBuffer;
property InSize: Int64 read FInSize write FInSize;
// max to read, amount read
property InStream: TStream read FInStrm write FInStrm;
property OutBuffer: TZMBuffer read FOutBuffer;
property OutSize: Int64 read FOutSize write FOutSize; // max to write
property OutStream: TStream read FOutStrm write FOutStrm;
property ToBeWritten: Int64 read FToBeWritten write FToBeWritten;
property Wrote: Int64 read FWrote write FWrote;
property OnProgress: TZM_ProgressEvent read FOnProgress write FOnProgress;
end;
type
TZMCompressor = class(TZMEngine)
private
FEncryptor: TZMEncryptorBase;
protected
procedure Finish; override;
function InitialCoDec(CompressionType: Integer): Integer; override;
function SetCryptor(CryptType: TZMCryptTypes): Integer; override;
public
function Compress: Integer;
function WriteOut(const Buf: PByte; const Count: Integer): Integer;
override;
property Encryptor: TZMEncryptorBase read FEncryptor;
end;
type
TZMDecompressor = class(TZMEngine)
private
FDecryptor: TZMDecryptorBase;
protected
procedure Finish; override;
function InitialCoDec(CompressionType: Integer): Integer; override;
function SetCryptor(CryptType: TZMCryptTypes): Integer; override;
public
function Decompress: Integer;
function ReadIn(Buf: PByte; const Count: Integer): Integer; override;
property Decryptor: TZMDecryptorBase read FDecryptor;
end;
function DeflateZStream(OutStream, InStream: TStream; Length: Int64): Integer;
function ExtractZStream(OutStream, InStream: TStream; Length: Int64): Integer;
function UndeflateQ(OutStream, InStream: TStream; Length: Int64; Method: Word;
var CRC: Cardinal): Integer;
implementation
uses
ZMMsg, ZMUtils, ZMStructs, ZMCoDec;
const
__UNIT__ = 15;
{$IFDEF VERPre6}
type
UInt64 = Int64;
{$ENDIF}
function ZM_Error(Line, Error: Integer): Integer;
begin
Result := -((__UNIT__ shl ZERR_UNIT_SHIFTS) + (Line shl ZERR_LINE_SHIFTS) or
AbsErr(Error));
end;
function UndeflateQ(OutStream, InStream: TStream; Length: Int64; Method: Word;
var CRC: Cardinal): Integer;
var
Inflater: TZMDecompressor;
begin
if (Method = METHOD_DEFLATED) or (Method = METHOD_STORED) then
begin
Inflater := TZMDecompressor.Create(InStream.Size);
try
Inflater.InStream := InStream;
Inflater.OutStream := OutStream;
Inflater.InSize := Length;
Result := Inflater.Prepare(Method);
if Result = 0 then
Result := Inflater.Decompress;
if (Result = 0) and (Inflater.CRC <> CRC) then
begin
Result := ZM_Error({_LINE_}251, ZE_BadCRC);
CRC := Inflater.CRC;
end;
finally
Inflater.Free;
end;
end
else
Result := ZM_Error({_LINE_}259, ZE_Unsupported);
end;
function ExtractZStream(OutStream, InStream: TStream; Length: Int64): Integer;
var
Heder: TZM_StreamHeader;
begin
if InStream.Read(Heder, SizeOf(TZM_StreamHeader)) = SizeOf(TZM_StreamHeader)
then
Result := UndeflateQ(OutStream, InStream, Length - SizeOf(TZM_StreamHeader),
Heder.Method, Heder.CRC)
else
Result := ZM_Error({_LINE_}271, ZE_ReadError);
end;
function DeflateZStream(OutStream, InStream: TStream; Length: Int64): Integer;
var
Deflater: TZMCompressor;
Heder: TZM_StreamHeader;
Position1: Int64;
Position2: Int64;
begin
Heder.Method := 8;
Heder.CRC := 0;
Position1 := OutStream.Position;
if OutStream.Write(Heder, Sizeof(Heder)) = SizeOf(Heder) then
begin
Deflater := TZMCompressor.Create(InStream.Size);
try
Deflater.InStream := InStream;
if Length > 0 then
Deflater.InSize := Length
else
Deflater.InSize := -1; // assume want the lot
Deflater.OutStream := OutStream;
Deflater.CompLevel := 9;
Result := Deflater.Prepare(METHOD_DEFLATED);
if Result = 0 then
Result := Deflater.Compress;
if Result >= 0 then
Heder.CRC := Deflater.CRC; // good
finally
Deflater.Free;
end;
if Result >= 0 then
begin
// rewrite Heder
Position2 := OutStream.Position;
OutStream.Position := Position1; // reposition to heder
if OutStream.Write(Heder, Sizeof(Heder)) = SizeOf(Heder) then
OutStream.Position := Position2; // back at end
end;
end
else
Result := ZM_Error({_LINE_}313, ZE_WriteError);
end;
procedure TZMBuffer.AfterConstruction;
begin
inherited;
FActualSize := 0;
FSize := 0;
FBufPtr := nil;
end;
procedure TZMBuffer.BeforeDestruction;
begin
FMyBuffer := nil;
inherited;
end;
function TZMBuffer.CalcBufSize(Value: Int64): Integer;
begin
if Value < 0 then
Result := -Integer(Value)
else
begin
Result := Value div 1024;
if Result > 63 then
Result := 63
else
if Result < 4 then
Result := 3;
Result := (1 + Result) * 1024;
end;
end;
// fill with zeros
procedure TZMBuffer.Clean;
begin
ZeroMemory(BufPtr, Size);
end;
procedure TZMBuffer.Resize(const Value: Integer);
begin
SetLength(FMyBuffer, Value);
FActualSize := Value;
FSize := Value;
FBufPtr := PByte(@FMyBuffer[0]);
end;
procedure TZMBuffer.SetSize(const Value: Integer);
begin
if FSize <> Value then
begin
if FActualSize < Value then
Resize(Value)
else
FSize := Value;
end;
end;
constructor TZMEngine.Create;
begin
inherited;
FZBufferSize := 8192;
end;
constructor TZMEngine.Create(BufSize: Int64);
begin
inherited Create;
FZBufferSize := CalcBufSize(BufSize);
end;
procedure TZMEngine.AfterConstruction;
begin
inherited;
MakeBuffers;
FInSize := -1;
FOutSize := -1;
FCompLevel := 9;
end;
procedure TZMEngine.BeforeDestruction;
begin
FInBuffer.Free;
OutBuffer.Free;
FTheCryptor.Free;
FCoDec.Free;
inherited;
end;
function TZMEngine.CalcBufSize(Value: Int64): Integer;
begin
if Value < 0 then
Result := {-}Integer(-Value)
else
begin
Result := Value div 1024;
if Result > 63 then
Result := 63
else
if Result < 4 then
Result := 3;
Result := (1 + Result) * 1024;
end;
end;
function TZMEngine.DoReadIn(Buf: PByte; const Count: Integer): Integer;
var
ToRead: Integer;
begin
Result := 0;
ToRead := Count;
if (InSize >= 0) and (ToRead > InSize) then
ToRead := InSize;
if ToRead > 0 then
Result := InStream.Read(Buf^, ToRead);
if Result > 0 then
begin
if InSize >= 0 then
InSize := InSize - Result;
if Assigned(OnProgress) then
OnProgress(Self, Result, True); // amount read
end;
end;
function TZMEngine.DoWriteOut(const Buf: PByte; const Count: Integer): Integer;
var
DidWrite: Integer;
begin
Result := 0;
if Count > 0 then
begin
if ToBeWritten > 0 then
begin
OutSize := OutSize + Count;
if OutSize > ToBeWritten then
begin
Result := ZM_Error({_LINE_}448, ZE_WrongLength);
Exit;
end;
end;
if Assigned(OutStream) then
begin
DidWrite := OutStream.Write(Buf^, Count);
if DidWrite <> Count then
begin
if DidWrite < 0 then
Result := DidWrite
else
Result := ZM_Error({_LINE_}460, ZE_WriteError);
Exit;
end;
end
else
DidWrite := Count;
if DidWrite > 0 then
begin
Wrote := Wrote + DidWrite;
if Assigned(OnProgress) then
OnProgress(Self, DidWrite, False); // amount written
end;
end;
end;
procedure TZMEngine.Finish;
begin
FEncrypted := False;
FState := 1; // finished
end;
procedure TZMEngine.MakeBuffers;
begin
FInBuffer := TZMBuffer.Create;
FOutBuffer := TZMBuffer.Create;
FInBuffer.Size := FZBufferSize;
OutBuffer.Size := FZBufferSize;
end;
function TZMEngine.Prepare(CompressionType: Integer): Integer;
begin
FState := -1; // not prepared
FEncrypted := False;
ToBeWritten := -1;
FCRC := 0;
FWrote := 0;
Result := InitialCoDec(CompressionType);
if Result >= 0 then
FState := 0;
end;
function TZMEngine.PrepareCrypt(CryptType: TZMCryptTypes): Integer;
begin
if State = 0 then
begin
Result := SetCryptor(CryptType);
FEncrypted := Result = 1;
end
else
Result := ZM_Error({_LINE_}509, ZE_LogicError);
end;
function TZMEngine.ReadIn(Buf: PByte; const Count: Integer): Integer;
begin
Result := DoReadIn(Buf, Count);
end;
procedure TZMEngine.SetCoDec(ACoDec: TZMCoDecBase);
begin
if FCoDec <> ACoDec then
begin
FCoDec.Free;
FCoDec := ACoDec;
end;
end;
procedure TZMEngine.SetTheCryptor(const Value: TZMCryptorBase);
begin
if FTheCryptor <> Value then
begin
FTheCryptor.Free;
FTheCryptor := Value;
end;
end;
function TZMEngine.WriteOut(const Buf: PByte; const Count: Integer): Integer;
begin
Result := DoWriteOut(Buf, Count);
end;
{ TZMCompressor }
function TZMCompressor.Compress: Integer;
begin
if @InStream = @OutStream then
begin
Result := ZM_Error({_LINE_}546, ZE_InIsOutStream);
Exit;
end;
if State = 0 then
begin
Result := FCoDec.Execute;
Finish;
end
else
Result := ZM_Error({_LINE_}555, ZE_LogicError);
end;
function TZMCompressor.InitialCoDec(CompressionType: Integer): Integer;
begin
Result := 0;
FEncryptor := nil;
if CompLevel < 1 then
CompressionType := METHOD_STORED;
if not(Assigned(FCoDec) and FCoDec.Supports(CompressionType)) then
begin
// need new CoDec
case CompressionType of
METHOD_STORED:
SetCoDec(TZMCoDecStore.Create(Self));
METHOD_DEFLATED:
SetCoDec(TZMCoDecDeflate.Create(Self));
else
Result := -ZE_Unsupported;
end;
end;
if Result = 0 then
Result := FCoDec.Initial(CompressionType);
end;
procedure TZMCompressor.Finish;
begin
if Assigned(Encryptor) then
Encryptor.Clear;
FEncryptor := nil;
inherited;
end;
function TZMCompressor.SetCryptor(CryptType: TZMCryptTypes): Integer;
begin
Result := 0;
if CryptType = ZcNone then
FEncryptor := nil
else
begin
if not Assigned(TheCryptor) then
TheCryptor := TZMZipEncryptor.Create(Self);
FEncryptor := TZMZipEncryptor(TheCryptor);
Result := 1;
end;
end;
function TZMCompressor.WriteOut(const Buf: PByte; const Count: Integer)
: Integer;
begin
if Encrypted then
Result := Encryptor.WriteOut(Buf, Count) // encode and write
else
Result := DoWriteOut(Buf, Count);
end;
{ TZMDecompressor }
function TZMDecompressor.Decompress: Integer;
begin
if @InStream = @OutStream then
begin
Result := ZM_Error({_LINE_}617, ZE_InIsOutStream);
Exit;
end;
if State = 0 then
begin
Result := FCoDec.Execute;
Finish;
end
else
Result := ZM_Error({_LINE_}626, ZE_LogicError);
end;
procedure TZMDecompressor.Finish;
begin
if Assigned(Decryptor) then
Decryptor.Clear;
FDecryptor := nil;
inherited;
end;
function TZMDecompressor.InitialCoDec(CompressionType: Integer): Integer;
begin
Result := 0;
FDecryptor := nil;
if not(Assigned(FCoDec) and FCoDec.Supports(CompressionType)) then
begin
// need new CoDec
case CompressionType of
METHOD_STORED:
SetCoDec(TZMCoDecStore.Create(Self));
METHOD_DEFLATED:
SetCoDec(TZMCoDecInflate.Create(Self));
else
Result := -ZE_Unsupported;
end;
end;
if Result = 0 then
Result := FCoDec.Initial(CompressionType);
end;
function TZMDecompressor.ReadIn(Buf: PByte; const Count: Integer): Integer;
begin
if Encrypted then
Result := Decryptor.ReadIn(Buf, Count) // readin and decode it
else
Result := DoReadIn(Buf, Count);
end;
function TZMDecompressor.SetCryptor(CryptType: TZMCryptTypes): Integer;
begin
Result := 0;
if CryptType = ZcNone then
FDecryptor := nil
else
begin
if not Assigned(TheCryptor) then
TheCryptor := TZMZipDecryptor.Create(Self);
FDecryptor := TZMZipDecryptor(TheCryptor);
Result := 1;
end;
end;
end.