-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZMCoDec.pas
834 lines (734 loc) · 21.7 KB
/
ZMCoDec.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
unit ZMCoDec;
// ZMCoDec.pas - 'zip' Compress/decompress and crypt engine
(* ***************************************************************************
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}
{ZipMstr,} ZMEngine, ZMCRC;
const
DEFLATE_MAX = 9;
const
RAND_HEAD_LEN = 12; // Length of encryption random header.
type
TZMCryptKeys = array [0 .. 2] of DWORD;
PZMCryptKeys = ^TZMCryptKeys;
TZMCryptHeader = packed array [0 .. RAND_HEAD_LEN - 1] of Byte;
PZMCryptHeader = ^TZMCryptHeader;
type
TZMZipCryptor = class
private
FCRC32Table: PZCRC32Table;
FCryptHeader: TZMCryptHeader;
FEngine: TZMEngine;
FMyReference: Cardinal;
FReady: Integer;
Keys: TZMCryptKeys;
function DecodeByte(C: Byte): Byte;
function EncodeByte(C: Byte): Byte;
procedure Init_keys(const Passwd: AnsiString);
function MakeHeader(const PassPhrase: AnsiString; RefValue: DWORD): Integer;
procedure Update_keys(C: Byte);
protected
function DecodeBuffer(Buf: PByte; Count: Longint): Integer;
function EncodeBuffer(Buf: PByte; Count: Longint): Integer;
function ReadHeader: Integer;
function WriteHeader: Integer;
property CRC32Table: PZCRC32Table read FCRC32Table;
property Ready: Integer read FReady write FReady;
public
constructor Create(MyBase: TZMEngine);
procedure AfterConstruction; override;
procedure Clear;
function DecodeInit(RefValue: Cardinal): Integer;
function EncodeInit(const PassPhrase: AnsiString; RefValue: DWORD): Integer;
function Supports(CryptType: TZMCryptTypes): Boolean;
function Unlock(const PassPhrase: AnsiString): Integer;
end;
type
TZMZipDecryptor = class(TZMDecryptorBase)
private
FCryptor: TZMZipCryptor;
FEngine: TZMEngine;
protected
function GetState: Integer; override;
public
constructor Create(MyBase: TZMEngine);
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
procedure Clear; override;
function DecodeInit(RefValue: Cardinal): Integer; override;
function ReadIn(Buf: PByte; const Count: Integer): Integer; override;
function Supports(CryptType: TZMCryptTypes): Boolean; override;
function Unlock(const PassPhrase: AnsiString): Integer; override;
end;
type
TZMZipEncryptor = class(TZMEncryptorBase)
private
FCryptor: TZMZipCryptor;
FEngine: TZMEngine;
protected
function GetState: Integer; override;
public
constructor Create(MyBase: TZMEngine);
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
procedure Clear; override;
function EncodeInit(const PassPhrase: AnsiString; RefValue: DWORD)
: Integer; override;
function Supports(CryptType: TZMCryptTypes): Boolean; override;
function WriteOut(const Buf: PByte; const Count: Integer): Integer;
override;
end;
type
TZMCoDecs = class(TZMCoDecBase)
private
FMyEngine: TZMEngine;
protected
property MyEngine: TZMEngine read FMyEngine;
public
constructor Create(MyBase: TZMEngine);
function Initial(CompressionType: Integer): Integer; override;
end;
type
TZMCoDecStore = class(TZMCoDecs)
public
function Execute: Integer; override;
function Supports(CompressionType: Integer): Boolean; override;
end;
type
TZMCoDecInflate = class(TZMCoDecs)
public
function Supports(CompressionType: Integer): Boolean; override;
function Execute: Integer; override;
end;
type
TZMCoDecDeflate = class(TZMCoDecs)
private
FCompLevel: Integer;
protected
property CompLevel: Integer read FCompLevel write FCompLevel;
public
function Supports(CompressionType: Integer): Boolean; override;
function Initial(CompressionType: Integer): Integer; override;
function Execute: Integer; override;
end;
implementation
uses
ZMMsg, ZMStructs, ZMZLibExApi, ZMUtils;
const
__UNIT__ = 5;
const
CRYPT_BUFFER_SIZE = 8 * 1024;
ZipMagic = 134775813;
MinInterval = 8096;
{$IFDEF VERPre6}
type
UInt64 = Int64;
{$ENDIF}
var
XSSeed: UInt64 = 0;
XSInitialed: Boolean = False;
function ZM_Error(Line, Error: Integer): Integer;
begin
Result := -((__UNIT__ shl ZERR_UNIT_SHIFTS) + (Line shl ZERR_LINE_SHIFTS) or
AbsErr(Error));
end;
function ZLibToZMError(Code: Integer): Integer;
begin
Result := 0;
if Code < 0 then
begin
Result := (ZZ_ZLibFile - 1) + (-Code);
if Result > ZZ_ZLibUnknown then
Result := ZZ_ZLibUnknown;
end;
end;
{ ** zlib deflate routines *********************************************************************** }
function ZDeflateInit2(var Stream: TZStreamRec; Level: Integer): Integer;
begin
Result := deflateInit2(Stream, Level, Z_DEFLATED, -15, 8, Z_DEFAULT_STRATEGY);
end;
function ZDeflate(var Stream: TZStreamRec; Flush: Integer): Integer;
begin
Result := deflate(Stream, Flush);
end;
function ZDeflateEnd(var Stream: TZStreamRec): Integer;
begin
Result := deflateEnd(Stream);
end;
{ ** zlib inflate routines *********************************************************************** }
function ZInflateInit2(var Stream: TZStreamRec; WindowBits: Integer): Integer;
begin
Result := inflateInit2_(Stream, WindowBits, ZLIB_VERSION,
SizeOf(TZStreamRec));
end;
function ZInflate(var Stream: TZStreamRec; Flush: Integer): Integer;
begin
Result := inflate(Stream, Flush);
end;
function ZInflateEnd(var Stream: TZStreamRec): Integer;
begin
Result := inflateEnd(Stream);
end;
// 2463534242;
function XSRandom: Cardinal;
// Marsaglia, George (July 2003). "Xorshift RNGs".
// Journal of Statistical Software Vol. 8 (Issue 14).
var
Temp: UInt64;
begin
Temp := XSSeed xor (XSSeed shl 13);
Temp := Temp xor (Temp shr 17);
Temp := Temp xor (Temp shl 5);
XSSeed := Temp;
Result := (Temp shr 1) and $FFFFFFFF;
end;
procedure InitXSSeed;
var
Temp: FILETIME;
SysTime: SYSTEMTIME;
begin
GetSystemTime(SysTime);
SystemTimeToFileTime(SysTime, Temp);
XSSeed := (GetTickCount shr 1) xor UInt64(Temp);
XSInitialed := True;
end;
{ TZMZipCryptor }
constructor TZMZipCryptor.Create(MyBase: TZMEngine);
begin
inherited Create;
FEngine := MyBase;
end;
procedure TZMZipCryptor.AfterConstruction;
begin
inherited;
FCRC32Table := PZCRC32Table(GetCRCTable);
Clear;
if not XSInitialed then
InitXSSeed;
end;
procedure TZMZipCryptor.Clear;
begin
FReady := 0; // not verified
end;
// returns >0 _ output count, <0 _ error
// decrypts inplace
function TZMZipCryptor.DecodeBuffer(Buf: PByte; Count: Longint): Integer;
var
B: Byte;
I: Integer;
T: Byte;
Temp: Cardinal;
begin
for I := 0 to Count - 1 do
begin
// decode byte
Temp := (Keys[2] and $FFFF) or 2;
T := (((Temp * (Temp xor 1)) shr 8) and $FF);
B := Byte(Buf^ xor T);
Buf^ := B;
// update keys
Keys[0] := CRC32Table^[DWORD((Keys[0] xor B) and $0FF)
] xor DWORD((Keys[0] shr 8) and $0FFFFFF);
Keys[1] := Keys[1] + (Keys[0] and $FF);
Keys[1] := (Keys[1] * ZipMagic) + 1;
Keys[2] := CRC32Table^[DWORD(Keys[2] xor Keys[1] shr 24) and $0FF]
xor (DWORD(Keys[2] shr 8) and $0FFFFFF);
Inc(Buf);
end;
Result := Count;
end;
function TZMZipCryptor.DecodeByte(C: Byte): Byte;
var
Temp: Cardinal;
T: Byte;
begin
// decode byte
Temp := (Keys[2] and $FFFF) or 2;
T := (((Temp * (Temp xor 1)) shr 8) and $0FF);
Result := Byte(C xor T);
// update keys
Keys[0] := CRC32Table^[DWORD((Keys[0] xor Result) and $0FF)
] xor DWORD((Keys[0] shr 8) and $0FFFFFF);
Keys[1] := Keys[1] + (Keys[0] and $FF);
Keys[1] := (Keys[1] * ZipMagic) + 1;
Keys[2] := CRC32Table^[DWORD(Keys[2] xor Keys[1] shr 24) and $0FF]
xor (DWORD(Keys[2] shr 8) and $0FFFFFF);
end;
function TZMZipCryptor.DecodeInit(RefValue: Cardinal): Integer;
begin
FMyReference := RefValue;
if ReadHeader <> RAND_HEAD_LEN then
FReady := -1; // bad
Result := Ready;
end;
// returns >0 _ output count, <0 _ error
// encrypts inplace
function TZMZipCryptor.EncodeBuffer(Buf: PByte; Count: Longint): Integer;
var
B: Byte;
I: Integer;
T: Byte;
Temp: Cardinal;
begin
for I := 0 to Count - 1 do
begin
B := Buf^;
Temp := (Keys[2] and $FFFF) or 2;
T := ((Temp * (Temp xor 1)) shr 8) and $FF;
Buf^ := Byte(B xor T);
// update keys
Keys[0] := CRC32Table[DWORD((Keys[0] xor B) and $0FF)
] xor DWORD((Keys[0] shr 8) and $0FFFFFF);
Keys[1] := Keys[1] + (Keys[0] and $FF);
Keys[1] := (Keys[1] * ZipMagic) + 1;
Keys[2] := CRC32Table[DWORD(Keys[2] xor Keys[1] shr 24) and $0FF]
xor (DWORD(Keys[2] shr 8) and $0FFFFFF);
Inc(Buf);
end;
Result := Count;
end;
function TZMZipCryptor.EncodeByte(C: Byte): Byte;
var
T: Byte;
Temp: DWORD;
begin
// decrypt byte
Temp := (Keys[2] and $FFFF) or 2;
T := (((Temp * (Temp xor 1)) shr 8) and $FF);
Result := Byte(C xor T);
// update keys
Keys[0] := CRC32Table[DWORD((Keys[0] xor C) and $0FF)
] xor DWORD((Keys[0] shr 8) and $0FFFFFF);
Keys[1] := Keys[1] + (Keys[0] and $FF);
Keys[1] := (Keys[1] * ZipMagic) + 1;
Keys[2] := CRC32Table[DWORD(Keys[2] xor Keys[1] shr 24) and $0FF]
xor (DWORD(Keys[2] shr 8) and $0FFFFFF);
end;
function TZMZipCryptor.EncodeInit(const PassPhrase: AnsiString;
RefValue: DWORD): Integer;
begin
Result := MakeHeader(PassPhrase, RefValue);
if Result < 0 then
FReady := -1
else
FReady := 2;
end;
procedure TZMZipCryptor.Init_keys(const Passwd: AnsiString);
var
I: Integer;
begin
Keys[0] := 305419896;
Keys[1] := 591751049;
Keys[2] := 878082192;
for I := 1 to Length(Passwd) do
Update_keys(Ord(Passwd[I]));
end;
function TZMZipCryptor.MakeHeader(const PassPhrase: AnsiString;
RefValue: DWORD): Integer;
var
Header: array [0 .. RAND_HEAD_LEN - 3] of Byte;
I: Integer;
begin
// First generate RAND_HEAD_LEN - 2 random bytes.
// We encrypt the output of rand() to get less predictability,
// since rand() is often poorly implemented.
Init_keys(PassPhrase);
for I := 0 to RAND_HEAD_LEN - 3 do
begin
Header[I] := EncodeByte(XSRandom and $0FF);
end;
Init_keys(PassPhrase);
// Encrypt random header (last two bytes is high word of crc)
for I := 0 to RAND_HEAD_LEN - 3 do
FCryptHeader[I] := EncodeByte(Header[I]);
FCryptHeader[RAND_HEAD_LEN - 2] := EncodeByte((RefValue shr 16) and $FF);
FCryptHeader[RAND_HEAD_LEN - 1] := EncodeByte((RefValue shr 24) and $FF);
Result := RAND_HEAD_LEN;
end;
function TZMZipCryptor.ReadHeader: Integer;
begin
Result := FEngine.DoReadIn(PByte(@FCryptHeader), RAND_HEAD_LEN);
end;
function TZMZipCryptor.Supports(CryptType: TZMCryptTypes): Boolean;
begin
Result := CryptType = ZcZip;
end;
// Return <0 _ error, 0 _ match, >0 _ no match
function TZMZipCryptor.Unlock(const PassPhrase: AnsiString): Integer;
var
Hh: array [0 .. RAND_HEAD_LEN - 1] of Byte; // decrypted header copy
B: Word;
N: Integer;
begin
if FReady < 0 then
begin
Result := ZM_Error({_LINE_}461, ZE_LogicError); // not initialised
Exit;
end;
if (FReady = 0) and (PassPhrase <> '') then
begin
// still locked
// set DecryptKeys and save the encrypted header
Init_keys(PassPhrase);
Move(FCryptHeader, Hh, RAND_HEAD_LEN);
// check password
for N := 0 to RAND_HEAD_LEN - 1 do
Hh[N] := Byte(DecodeByte(Hh[N]));
B := Hh[RAND_HEAD_LEN - 1];
if B = (FMyReference and $0FF) then
FReady := 1; // unlocked
end;
if FReady = 1 then
Result := 0 // matched
else
Result := 1; // failed
end;
procedure TZMZipCryptor.Update_keys(C: Byte);
begin
Keys[0] := CRC32Table[(C xor Keys[0]) and $0FF] xor (Keys[0] shr 8);
Keys[1] := Keys[1] + (Keys[0] and $0FF);
Keys[1] := (Keys[1] * ZipMagic) + 1;
Keys[2] := CRC32Table[DWORD(Keys[2] xor Keys[1] shr 24) and $0FF]
xor (DWORD(Keys[2] shr 8) and $0FFFFFF);
end;
function TZMZipCryptor.WriteHeader: Integer;
begin
Result := 0;
if FReady = 2 then
begin
Result := FEngine.DoWriteOut(PByte(@FCryptHeader), RAND_HEAD_LEN);
if Result <> RAND_HEAD_LEN then
FReady := -1
else
FReady := 1;
end
else
FReady := -1;
if (Result >= 0) and (Result <> RAND_HEAD_LEN) then
Result := ZM_Error({_LINE_}508, ZE_CryptError);
end;
function TZMCoDecStore.Execute: Integer;
var
CRC: DWORD;
InBuf: PByte;
InBufSize: Integer;
begin
FMyEngine.CRC := 0;
CRC := 0;
InBuf := FMyEngine.InBuffer.BufPtr;
InBufSize := FMyEngine.InBuffer.Size;
FMyEngine.ToBeWritten := FMyEngine.OutSize; // max to write
FMyEngine.OutSize := 0; // nothing written yet
repeat
Result := FMyEngine.ReadIn(InBuf, InBufSize);
if Result <= 0 then
Break; // error
CRC := ZCRC32(CRC, InBuf^, Result);
Result := FMyEngine.WriteOut(InBuf, Result);
if Result < 0 then
Break;
until (Result < 0);
FMyEngine.CRC := CRC;
end;
function TZMCoDecStore.Supports(CompressionType: Integer): Boolean;
begin
Result := CompressionType = METHOD_STORED;
end;
{ TZMCoDecInflate }
function TZMCoDecInflate.Execute: Integer;
var
CRC: DWORD;
Have: Integer;
InBuf: PByte;
InBufSize: Integer;
OutBuf: PByte;
OutBufSize: Integer;
ZStream: TZStreamRec;
begin
MyEngine.CRC := 0;
CRC := 0;
InBuf := MyEngine.InBuffer.BufPtr;
InBufSize := MyEngine.InBuffer.Size;
OutBuf := MyEngine.OutBuffer.BufPtr;
OutBufSize := MyEngine.OutBuffer.Size;
MyEngine.ToBeWritten := MyEngine.OutSize; // max to write
MyEngine.OutSize := 0; // nothing written yet
// allocate deflate state
ZStream.Zalloc := nil;
ZStream.Zfree := nil;
ZStream.Opaque := nil;
ZStream.Avail_in := 0;
ZStream.Next_in := nil;
Result := ZInflateInit2(ZStream, -15);
if (Result < 0) and (Result <> Z_BUF_ERROR) then
begin
Result := ZM_Error({_LINE_}574, ZLibToZMError(Result));
Exit;
end;
try
// Inflate until deflate stream ends or end of file
repeat
Have := MyEngine.ReadIn(InBuf, InBufSize);
if Have <= 0 then
begin
Result := Have;
Break;
end;
ZStream.Avail_in := Have;
ZStream.Next_in := InBuf;
// run inflate() on input until output buffer not full
repeat
ZStream.Avail_out := OutBufSize;
ZStream.Next_out := OutBuf;
Result := ZInflate(ZStream, Z_NO_FLUSH);
if (Result < 0) and (Result <> Z_BUF_ERROR) then
begin
Result := ZM_Error({_LINE_}596, ZLibToZMError(Result));
Break;
end;
Result := 0;
Have := OutBufSize - Integer(ZStream.Avail_out);
if Have > 0 then
begin
CRC := ZCRC32(CRC, OutBuf^, Have);
Result := MyEngine.WriteOut(OutBuf, Have);
if Result < 0 then
Break;
end;
until (ZStream.Avail_out <> 0);
// done when inflate() says it's done or error
until (Result < 0) or (Result = Z_STREAM_END);
finally
// clean up
ZInflateEnd(ZStream);
MyEngine.CRC := CRC;
end;
end;
function TZMCoDecInflate.Supports(CompressionType: Integer): Boolean;
begin
Result := CompressionType = METHOD_DEFLATED;
end;
{ TZMCoDecDeflate }
function TZMCoDecDeflate.Execute: Integer;
var
CRC: DWORD;
Flush: Integer;
Have: Integer;
InBuf: PByte;
InBufSize: Integer;
OutBuf: PByte;
OutBufSize: Integer;
ZStream: TZStreamRec;
begin
MyEngine.CRC := 0;
CRC := 0;
InBuf := MyEngine.InBuffer.BufPtr;
InBufSize := MyEngine.InBuffer.Size;
OutBuf := MyEngine.OutBuffer.BufPtr;
OutBufSize := MyEngine.OutBuffer.Size;
MyEngine.ToBeWritten := MyEngine.OutSize; // max to write
MyEngine.OutSize := 0; // nothing written yet
// allocate deflate state
ZStream.Zalloc := nil;
ZStream.Zfree := nil;
ZStream.Opaque := nil;
Result := ZDeflateInit2(ZStream, MyEngine.CompLevel);
if Result < 0 then
begin
Result := ZM_Error({_LINE_}655, ZLibToZMError(Result));
Exit;
end;
try
Result := 0;
Flush := Z_NO_FLUSH;
while (Flush <> Z_FINISH) and (Result = 0) do
begin
Result := MyEngine.ReadIn(InBuf, InBufSize);
if Result < 0 then
Break; // error
if Result < InBufSize{1} then
Flush := Z_FINISH // end of input
else
Flush := Z_NO_FLUSH;
if Result > 0 then
CRC := ZCRC32(CRC, InBuf^, Result);
ZStream.Avail_in := Result;
ZStream.Next_in := InBuf;
{ run deflate() on input until output buffer not full, finish
compression if all of source has been read in }
repeat
ZStream.Avail_out := OutBufSize;
ZStream.Next_out := OutBuf;
Result := ZDeflate(ZStream, Flush);
if Result < 0 then
begin
Result := ZM_Error({_LINE_}684, ZLibToZMError(Result));
Break;
end;
Have := OutBufSize - Integer(ZStream.Avail_out);
if Have > 0 then
begin
Result := MyEngine.WriteOut(OutBuf, Have);
if Result < 0 then
Break;
end;
until ZStream.Avail_out <> 0;
end;
finally
ZDeflateEnd(ZStream);
MyEngine.CRC := CRC;
end;
if Result > 0 then
Result := 0;
end;
function TZMCoDecDeflate.Initial(CompressionType: Integer): Integer;
begin
Result := 0;
if Supports(CompressionType) then
FCompLevel := CompressionType
else
Result := ZM_Error(735, ZE_LogicError);
end;
function TZMCoDecDeflate.Supports(CompressionType: Integer): Boolean;
begin
Result := (CompressionType > 0) and (CompressionType <= DEFLATE_MAX);
end;
constructor TZMCoDecs.Create(MyBase: TZMEngine);
begin
inherited Create;
FMyEngine := MyBase;
end;
function TZMCoDecs.Initial(CompressionType: Integer): Integer;
begin
if Supports(CompressionType) then
Result := 0
else
Result := ZM_Error(648, ZE_LogicError);
end;
constructor TZMZipDecryptor.Create(MyBase: TZMEngine);
begin
inherited Create;
FEngine := MyBase;
end;
{ TZMZipDecryptor }
procedure TZMZipDecryptor.AfterConstruction;
begin
inherited;
FCryptor := TZMZipCryptor.Create(FEngine);
end;
procedure TZMZipDecryptor.BeforeDestruction;
begin
FCryptor.Free;
inherited;
end;
procedure TZMZipDecryptor.Clear;
begin
FCryptor.Clear;
end;
function TZMZipDecryptor.DecodeInit(RefValue: Cardinal): Integer;
begin
Result := FCryptor.DecodeInit(RefValue);
end;
function TZMZipDecryptor.GetState: Integer;
begin
Result := FCryptor.Ready;
end;
function TZMZipDecryptor.ReadIn(Buf: PByte; const Count: Integer): Integer;
begin
Result := FEngine.DoReadIn(Buf, Count);
if Result > 0 then
Result := FCryptor.DecodeBuffer(Buf, Result);
end;
function TZMZipDecryptor.Supports(CryptType: TZMCryptTypes): Boolean;
begin
Result := FCryptor.Supports(CryptType);
end;
function TZMZipDecryptor.Unlock(const PassPhrase: AnsiString): Integer;
begin
Result := FCryptor.Unlock(PassPhrase);
end;
{ TZMZipEncryptor }
procedure TZMZipEncryptor.AfterConstruction;
begin
inherited;
FCryptor := TZMZipCryptor.Create(FEngine);
end;
procedure TZMZipEncryptor.BeforeDestruction;
begin
FCryptor.Free;
inherited;
end;
procedure TZMZipEncryptor.Clear;
begin
FCryptor.Clear;
end;
constructor TZMZipEncryptor.Create(MyBase: TZMEngine);
begin
inherited Create;
FEngine := MyBase;
end;
function TZMZipEncryptor.EncodeInit(const PassPhrase: AnsiString;
RefValue: DWORD): Integer;
begin
Result := FCryptor.EncodeInit(PassPhrase, RefValue);
end;
function TZMZipEncryptor.GetState: Integer;
begin
Result := FCryptor.Ready;
end;
function TZMZipEncryptor.Supports(CryptType: TZMCryptTypes): Boolean;
begin
Result := FCryptor.Supports(CryptType);
end;
function TZMZipEncryptor.WriteOut(const Buf: PByte;
const Count: Integer): Integer;
begin
Result := FCryptor.EncodeBuffer(Buf, Count);
if Result >= 0 then
Result := FEngine.DoWriteOut(Buf, Count);
end;
end.