-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZLibEx.pas
2252 lines (1752 loc) · 73.4 KB
/
ZLibEx.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
{*************************************************************************************************
* ZLibEx.pas *
* *
* copyright (c) 2000-2013 base2 technologies *
* copyright (c) 1995-2002 Borland Software Corporation *
* *
* revision history *
* 2012.05.21 updated for win64 (delphi xe2) *
* added NativeInt type for delphi 2007- *
* added NativeUInt type for delphi 2007- *
* 2011.07.21 fixed routines to validate size before calling Move *
* 2010.07.01 hide overloaded Z*String* routines for delphi 5 *
* 2010.05.02 added ZDelfateEx and ZInflateEx *
* 2010.04.20 added TZ*Buffer classes *
* 2010.04.15 moved core zlib routines to separate unit (ZLibExApi.pas) *
* added ZDeflate* and ZInflate* *
* 2010.04.14 fixed ZInternalCompress loops *
* fixed ZInternalDecompress loops *
* updated ZInternalCompressStream loops *
* updated ZInternalDecompressStream loops *
* 2010.01.27 updated for delphi 2010 *
* 2009.04.14 added overloaded string routines for AnsiString and UnicodeString *
* 2009.01.28 updated for delphi 2009 String (UnicodeString) *
* 2008.05.15 added TStreamPos type for Stream.Position variants *
* added TCustomZStream.Stream* methods *
* 2007.08.17 modified TZCompressionStream.Write to use Write instead of WriteBuffer *
* 2007.03.15 moved gzip routines to separate unit (ZLibExGZ.pas) *
* 2006.10.07 fixed EZLibError constructor for c++ builder compatibility *
* 2006.03.28 moved Z_DEFLATED to interface section *
* added custom compression levels zcLevel1 thru zcLevel9 *
* 2006.03.27 added ZCompressStreamWeb *
* 2006.03.24 added ZAdler32 and ZCrc32 *
* 2005.11.29 changed FStreamPos to Int64 for delphi 6+ *
* 2005.03.04 modified ZInternalCompressStream loops *
* modified ZInternalDecompressStream loops *
* 2005.02.07 fixed ZInternalCompressStream loop conditions *
* fixed ZInternalDecompressStream loop conditions *
* 2005.01.11 added ZCompressStrWeb *
* 2003.04.14 added ZCompress2 and ZDecompress2 *
* added ZCompressStr2 and ZDecompressStr2 *
* added ZCompressStream2 and ZDecompressStream2 *
* added overloaded T*Stream constructors to support InflateInit2 *
* and DeflateInit2 *
* fixed ZDecompressStream to use ZDecompressCheck instead of ZCompressCheck *
* 2001.11.27 enhanced TZDecompressionStream.Read to adjust source stream position upon end *
* of compression data *
* fixed endless loop in TZDecompressionStream.Read when destination count was *
* greater than uncompressed data *
* 2001.10.26 renamed unit to integrate "nicely" with delphi 6 *
* 2000.11.24 added soFromEnd condition to TZDecompressionStream.Seek *
* added ZCompressStream and ZDecompressStream *
* 2000.06.13 optimized, fixed, rewrote, and enhanced the zlib.pas unit included on the *
* delphi cd (zlib version 1.1.3) *
* *
* acknowledgments *
* erik turner *
* 2001.10.26 Z*Stream routines *
* *
* david bennion *
* 2001.11.27 finding the nasty little endless loop quirk with the *
* TZDecompressionStream.Read method *
* *
* luigi sandon *
* 2005.02.07 pointing out the missing loop condition (Z_STREAM_END) in *
* ZInternalCompressStream and ZInternalDecompressStream *
* *
* ferry van genderen *
* 2005.03.04 assisting me fine tune and beta test ZInternalCompressStream and *
* ZInternalDecompressStream *
* *
* j. rathlev *
* 2005.11.28 pointing out the FStreamPos and TStream.Position type inconsistency *
* *
* anders johansen *
* 2006.10.07 pointing out the ELibError constructor incompatibility with c++ builder *
* *
* marcin szafranski *
* 2009.01.28 beta testing the delphi 2009 changes *
* *
* iztok kacin *
* 2009.04.14 assisting me design and further improve support for delphi 2009 *
* *
* oleg matrozov *
* 2010.04.14 pointing out the missing loop condition (avail_in > 0) in ZInternalCompress *
* and ZInternalDecompress *
* 2010.04.20 prototyping and assisting with the TZ*Buffer classes *
* *
* edward koo *
* 2010.07.01 pointing out the delphi 5 incompatibility with the overloaded Z*String* *
* routines *
* *
* egron elbra *
* 2011.07.20 pointing out the range exception when moving empty strings *
* *
* marian pascalau *
* 2012.05.21 providing their win64 modifications *
* *
* donations *
* 2011.05.06 farshad mohajeri *
* 2012.06.07 marat safin *
* 2012.12.14 moacir schmidt *
* 2013.05.23 roman ganz *
*************************************************************************************************}
unit ZLibEx;
{$MODE Delphi}
interface
uses
SysUtils, Classes, PasZLib;
const
Z_RLE = 3;
Z_FIXED = 4;
Z_BLOCK = 5;
Z_TREES = 6;
z_errmsg: Array [0..9] of String = (
'Need dictionary', // Z_NEED_DICT (2)
'Stream end', // Z_STREAM_END (1)
'OK', // Z_OK (0)
'File error', // Z_ERRNO (-1)
'Stream error', // Z_STREAM_ERROR (-2)
'Data error', // Z_DATA_ERROR (-3)
'Insufficient memory', // Z_MEM_ERROR (-4)
'Buffer error', // Z_BUF_ERROR (-5)
'Incompatible version', // Z_VERSION_ERROR (-6)
''
);
type
{$ifndef UNICODE}
RawByteString = AnsiString;
UnicodeString = WideString;
UnicodeChar = WideChar;
{$else ifdef Version2010Plus}
UnicodeChar = WideChar;
{$endif}
{$ifndef Version2009Plus}
NativeInt = Integer;
NativeUInt = Cardinal;
{$endif}
TStreamPos = {$ifdef Version6Plus} Int64 {$else} Longint {$endif};
TZCompressionLevel = (
zcNone,
zcFastest,
zcDefault,
zcMax,
zcLevel1,
zcLevel2,
zcLevel3,
zcLevel4,
zcLevel5,
zcLevel6,
zcLevel7,
zcLevel8,
zcLevel9
);
TZStrategy = (
zsDefault,
zsFiltered,
zsHuffman,
zsRLE,
zsFixed
);
TZError = (
zeError,
zeStreamError,
zeDataError,
zeMemoryError,
zeBufferError,
zeVersionError
);
TZFlush = (
zfNoFlush,
zfPartialFlush,
zfSyncFlush,
zfFullFlush,
zfFinish,
zfBlock,
zfTrees
);
const
ZLevels: Array [TZCompressionLevel] of Integer = (
Z_NO_COMPRESSION, // zcNone
Z_BEST_SPEED, // zcFastest
Z_DEFAULT_COMPRESSION, // zcDefault
Z_BEST_COMPRESSION, // zcMax
1, // zcLevel1
2, // zcLevel2
3, // zcLevel3
4, // zcLevel4
5, // zcLevel5
6, // zcLevel6
7, // zcLevel7
8, // zcLevel8
9 // zcLevel9
);
ZStrategies: Array [TZStrategy] of Integer = (
Z_DEFAULT_STRATEGY, // zsDefault
Z_FILTERED, // zsFiltered
Z_HUFFMAN_ONLY, // zsHuffman
Z_RLE, // zsRLE
Z_FIXED // zsFixed
);
ZErrors: Array [TZError] of Integer = (
Z_ERRNO, // zeError
Z_STREAM_ERROR, // zeStreamError
Z_DATA_ERROR, // zeDataError
Z_MEM_ERROR, // zeMemoryError
Z_BUF_ERROR, // zeBufferError
Z_VERSION_ERROR // zeVersionError
);
ZFlushes: Array [TZFlush] of Integer = (
Z_NO_FLUSH, // zfNoFlush
Z_PARTIAL_FLUSH, // zfPartialFlush
Z_SYNC_FLUSH, // zfSyncFlush
Z_FULL_FLUSH, // zfFullFlush
Z_FINISH, // zfFinish
Z_BLOCK, // zfBlock
Z_TREES // zfTrees
);
type
{** TZ*Function *******************************************************************************}
TZReadFunction = function (param: Pointer; var buffer;
size: Integer): Integer;
TZWriteFunction = function (param: Pointer; const buffer;
size: Integer): Integer;
{** TZInformation *****************************************************************************}
TZInformation = packed record
CompressedFlags : Longint;
CompressedSize : TStreamPos;
CompressedCrc : Longint;
CompressedAdler : Longint;
UncompressedFlags: Longint;
UncompressedSize : TStreamPos;
UncompressedCrc : Longint;
UncompressedAdler: Longint;
end;
{** TCustomZStream ****************************************************************************}
TCustomZStream = class(TStream)
private
FStream : TStream;
FStreamPos : TStreamPos;
FOnProgress: TNotifyEvent;
FZStream : TZStream;
FBuffer : Array [Word] of Byte;
function GetStreamPosition: TStreamPos;
procedure SetStreamPosition(value: TStreamPos);
protected
constructor Create(stream: TStream);
function StreamRead(var buffer; count: Longint): Longint;
function StreamWrite(const buffer; count: Longint): Longint;
function StreamSeek(offset: Longint; origin: Word): Longint;
procedure StreamReadBuffer(var buffer; count: Longint);
procedure StreamWriteBuffer(const buffer; count: Longint);
procedure DoProgress; dynamic;
property StreamPosition: TStreamPos read GetStreamPosition write SetStreamPosition;
property OnProgress: TNotifyEvent read FOnProgress write FOnProgress;
end;
{** TZCompressionStream ***********************************************************************}
TZCompressionStream = class(TCustomZStream)
private
function GetCompressionRate: Single;
public
constructor Create(dest: TStream;
compressionLevel: TZCompressionLevel = zcDefault); overload;
constructor Create(dest: TStream; compressionLevel: TZCompressionLevel;
windowBits, memLevel: Integer; strategy: TZStrategy); overload;
destructor Destroy; override;
function Read(var buffer; count: Longint): Longint; override;
function Write(const buffer; count: Longint): Longint; override;
function Seek(offset: Longint; origin: Word): Longint; override;
property CompressionRate: Single read GetCompressionRate;
property OnProgress;
end;
{** TZDecompressionStream *********************************************************************}
TZDecompressionStream = class(TCustomZStream)
public
constructor Create(source: TStream); overload;
constructor Create(source: TStream; windowBits: Integer); overload;
destructor Destroy; override;
function Read(var buffer; count: Longint): Longint; override;
function Write(const buffer; count: Longint): Longint; override;
function Seek(offset: Longint; origin: Word): Longint; override;
property OnProgress;
end;
{** TZCustomBuffer ****************************************************************************}
TZCustomBuffer = class(TObject)
private
FBuffer : Pointer;
FBufferCapacity: Integer;
FBufferSize : Integer;
protected
FZStream: TZStream;
procedure BufferWrite(const buffer: Pointer; size: Integer);
procedure BufferRead(var buffer: Pointer; size: Integer);
procedure BufferCapacity(capacity: Integer);
property BufferSize: Integer read FBufferSize;
public
constructor Create;
destructor Destroy; override;
procedure Clear; virtual;
procedure Flush(flush: TZFlush); virtual;
function Write(const buffer: Pointer; size: Integer): Integer; overload;
virtual; abstract;
function Write(const s: AnsiString): Integer; overload;
function Read(var buffer: Pointer; size: Integer): Integer; overload;
function Read(var s: AnsiString): Integer; overload;
end;
{** TZCompressionBuffer ***********************************************************************}
TZCompressionBuffer = class(TZCustomBuffer)
public
constructor Create(level: TZCompressionLevel = zcDefault); overload;
constructor Create(level: TZCompressionLevel;
windowBits, memLevel: Integer; strategy: TZStrategy); overload;
destructor Destroy; override;
procedure Clear; override;
procedure Flush(flush: TZFlush); override;
function Write(const buffer: Pointer; size: Integer): Integer;
override;
end;
{** TZDecompressionBuffer *********************************************************************}
TZDecompressionBuffer = class(TZCustomBuffer)
public
constructor Create; overload;
constructor Create(windowBits: Integer); overload;
destructor Destroy; override;
procedure Clear; override;
function Write(const buffer: Pointer; size: Integer): Integer; override;
end;
{** zlib deflate routines ***********************************************************************}
function ZDeflateInit(var stream: TZStream;
level: TZCompressionLevel): Integer;
{$ifdef Version2005Plus} inline; {$endif}
function ZDeflateInit2(var stream: TZStream;
level: TZCompressionLevel; windowBits, memLevel: Integer;
strategy: TZStrategy): Integer;
{$ifdef Version2005Plus} inline; {$endif}
function ZDeflate(var stream: TZStream; flush: TZFlush): Integer;
{$ifdef Version2005Plus} inline; {$endif}
function ZDeflateEnd(var stream: TZStream): Integer;
{$ifdef Version2005Plus} inline; {$endif}
function ZDeflateReset(var stream: TZStream): Integer;
{$ifdef Version2005Plus} inline; {$endif}
{** zlib inflate routines ***********************************************************************}
function ZInflateInit(var stream: TZStream): Integer;
{$ifdef Version2005Plus} inline; {$endif}
function ZInflateInit2(var stream: TZStream;
windowBits: Integer): Integer;
{$ifdef Version2005Plus} inline; {$endif}
function ZInflate(var stream: TZStream; flush: TZFlush): Integer;
{$ifdef Version2005Plus} inline; {$endif}
function ZInflateEnd(var stream: TZStream): Integer;
{$ifdef Version2005Plus} inline; {$endif}
function ZInflateReset(var stream: TZStream): Integer;
{$ifdef Version2005Plus} inline; {$endif}
{** zlib checksum routines **********************************************************************}
function ZAdler32(adler: Longint; const buffer:pchar; size: Integer): Longint;
{$ifdef Version2005Plus} inline; {$endif}
function ZCrc32(crc: Longint; const buffer:pchar; size: Integer): Longint;
{$ifdef Version2005Plus} inline; {$endif}
{** zlib custom routines ************************************************************************}
procedure ZDeflateEx(var stream: TZStream; param: Pointer;
read: TZReadFunction; write: TZWriteFunction; flush: TZFlush);
procedure ZInflateEx(var stream: TZStream; param: Pointer;
read: TZReadFunction; write: TZWriteFunction; flush: TZFlush);
{*************************************************************************************************
* ZCompress *
* *
* pre-conditions *
* inBuffer = pointer to uncompressed data *
* inSize = size of inBuffer (bytes) *
* outBuffer = pointer (unallocated) *
* level = compression level *
* *
* post-conditions *
* outBuffer = pointer to compressed data (allocated) *
* outSize = size of outBuffer (bytes) *
*************************************************************************************************}
procedure ZCompress(const inBuffer: Pointer; inSize: Integer;
out outBuffer: Pointer; out outSize: Integer;
level: TZCompressionLevel = zcDefault);
{*************************************************************************************************
* ZCompress2 *
* *
* pre-conditions *
* inBuffer = pointer to uncompressed data *
* inSize = size of inBuffer (bytes) *
* outBuffer = pointer (unallocated) *
* level = compression level *
* method = compression method *
* windowBits = window bits *
* memLevel = memory level *
* strategy = compression strategy *
* *
* post-conditions *
* outBuffer = pointer to compressed data (allocated) *
* outSize = size of outBuffer (bytes) *
*************************************************************************************************}
procedure ZCompress2(const inBuffer: Pointer; inSize: Integer;
out outBuffer: Pointer; out outSize: Integer; level: TZCompressionLevel;
windowBits, memLevel: Integer; strategy: TZStrategy);
{*************************************************************************************************
* ZDecompress *
* *
* pre-conditions *
* inBuffer = pointer to compressed data *
* inSize = size of inBuffer (bytes) *
* outBuffer = pointer (unallocated) *
* outEstimate = estimated size of uncompressed data (bytes) *
* *
* post-conditions *
* outBuffer = pointer to decompressed data (allocated) *
* outSize = size of outBuffer (bytes) *
*************************************************************************************************}
procedure ZDecompress(const inBuffer: Pointer; inSize: Integer;
out outBuffer: Pointer; out outSize: Integer; outEstimate: Integer = 0);
{*************************************************************************************************
* ZDecompress2 *
* *
* pre-conditions *
* inBuffer = pointer to compressed data *
* inSize = size of inBuffer (bytes) *
* outBuffer = pointer (unallocated) *
* windowBits = window bits *
* outEstimate = estimated size of uncompressed data (bytes) *
* *
* post-conditions *
* outBuffer = pointer to decompressed data (allocated) *
* outSize = size of outBuffer (bytes) *
*************************************************************************************************}
procedure ZDecompress2(const inBuffer: Pointer; inSize: Integer;
out outBuffer: Pointer; out outSize: Integer; windowBits: Integer;
outEstimate: Integer = 0);
{** string routines *****************************************************************************}
{*************************************************************************************************
* ZCompressStr *
* *
* pre-conditions *
* s = uncompressed data string *
* level = compression level *
* *
* return *
* compressed data string *
*************************************************************************************************}
function ZCompressStr(const s: AnsiString;
level: TZCompressionLevel = zcDefault): RawByteString;
procedure ZCompressString(var result: RawByteString; const s: AnsiString;
level: TZCompressionLevel = zcDefault); overload;
{$ifdef Version6Plus}
procedure ZCompressString(var result: RawByteString; const s: UnicodeString;
level: TZCompressionLevel = zcDefault); overload;
{$endif}
{*************************************************************************************************
* ZCompressStrEx *
* *
* pre-conditions *
* s = uncompressed data string *
* level = compression level *
* *
* return *
* compressed data string with 4 byte (integer) header indicating *
* original uncompressed data length *
*************************************************************************************************}
function ZCompressStrEx(const s: AnsiString;
level: TZCompressionLevel = zcDefault): RawByteString;
procedure ZCompressStringEx(var result: RawByteString; const s: AnsiString;
level: TZCompressionLevel = zcDefault); overload;
{$ifdef Version6Plus}
procedure ZCompressStringEx(var result: RawByteString; const s: UnicodeString;
level: TZCompressionLevel = zcDefault); overload;
{$endif}
{*************************************************************************************************
* ZCompressStr2 *
* *
* pre-conditions *
* s = uncompressed data string *
* level = compression level *
* windowBits = window bits *
* memLevel = memory level *
* strategy = compression strategy *
* *
* return *
* compressed data string *
*************************************************************************************************}
function ZCompressStr2(const s: AnsiString; level: TZCompressionLevel;
windowBits, memLevel: Integer; strategy: TZStrategy): RawByteString;
procedure ZCompressString2(var result: RawByteString; const s: AnsiString;
level: TZCompressionLevel; windowBits, memLevel: Integer;
strategy: TZStrategy); overload;
{$ifdef Version6Plus}
procedure ZCompressString2(var result: RawByteString; const s: UnicodeString;
level: TZCompressionLevel; windowBits, memLevel: Integer;
strategy: TZStrategy); overload;
{$endif}
{*************************************************************************************************
* ZCompressStrWeb *
* *
* pre-conditions *
* s = uncompressed data string *
* *
* return *
* compressed data string *
*************************************************************************************************}
function ZCompressStrWeb(const s: AnsiString): RawByteString;
procedure ZCompressStringWeb(var result: RawByteString; const s: AnsiString);
overload;
{$ifdef Version6Plus}
procedure ZCompressStringWeb(var result: RawByteString;
const s: UnicodeString); overload;
{$endif}
{*************************************************************************************************
* ZDecompressStr *
* *
* pre-conditions *
* s = compressed data string *
* *
* return *
* uncompressed data string *
*************************************************************************************************}
function ZDecompressStr(const s: RawByteString): AnsiString;
procedure ZDecompressString(var result: AnsiString; const s: RawByteString);
overload;
{$ifdef Version6Plus}
procedure ZDecompressString(var result: UnicodeString;
const s: RawByteString); overload;
{$endif}
{*************************************************************************************************
* ZDecompressStrEx *
* *
* pre-conditions *
* s = compressed data string with 4 byte (integer) header indicating *
* original uncompressed data length *
* *
* return *
* uncompressed data string *
*************************************************************************************************}
function ZDecompressStrEx(const s: RawByteString): AnsiString;
procedure ZDecompressStringEx(var result: AnsiString; const s: RawByteString);
overload;
{$ifdef Version6Plus}
procedure ZDecompressStringEx(var result: UnicodeString;
const s: RawByteString); overload;
{$endif}
{*************************************************************************************************
* ZDecompressStr2 *
* *
* pre-conditions *
* s = compressed data string *
* windowBits = window bits *
* *
* return *
* uncompressed data string *
*************************************************************************************************}
function ZDecompressStr2(const s: RawByteString;
windowBits: Integer): AnsiString;
procedure ZDecompressString2(var result: AnsiString; const s: RawByteString;
windowBits: Integer); overload;
{$ifdef Version6Plus}
procedure ZDecompressString2(var result: UnicodeString;
const s: RawByteString; windowBits: Integer); overload;
{$endif}
{** stream routines *****************************************************************************}
procedure ZCompressStream(inStream, outStream: TStream;
level: TZCompressionLevel = zcDefault);
procedure ZCompressStream2(inStream, outStream: TStream;
level: TZCompressionLevel; windowBits, memLevel: Integer;
strategy: TZStrategy);
procedure ZCompressStreamWeb(inStream, outStream: TStream);
procedure ZDecompressStream(inStream, outStream: TStream);
procedure ZDecompressStream2(inStream, outStream: TStream;
windowBits: Integer);
{************************************************************************************************}
type
EZLibErrorClass = class of EZlibError;
EZLibError = class(Exception)
private
FErrorCode: Integer;
public
constructor Create(code: Integer; const dummy: String = ''); overload;
constructor Create(error: TZError; const dummy: String = ''); overload;
property ErrorCode: Integer read FErrorCode write FErrorCode;
end;
EZCompressionError = class(EZLibError);
EZDecompressionError = class(EZLibError);
implementation
const
SZInvalid = 'Invalid ZStream operation!';
{************************************************************************************************}
function ZCompressCheck(code: Integer): Integer;
begin
result := code;
if code < 0 then
begin
raise EZCompressionError.Create(code);
end;
end;
function ZDecompressCheck(code: Integer; raiseBufferError: Boolean = True): Integer;
begin
Result := code;
if code < 0 then
begin
if (code <> Z_BUF_ERROR) or raiseBufferError then
begin
raise EZDecompressionError.Create(code);
end;
end;
end;
{** zlib deflate routines ***********************************************************************}
function ZDeflateInit(var stream: TZStream;
level: TZCompressionLevel): Integer;
begin
result := deflateInit_(stream, ZLevels[level], ZLIB_VERSION,
SizeOf(TZStream));
end;
function ZDeflateInit2(var stream: TZStream;
level: TZCompressionLevel; windowBits, memLevel: Integer;
strategy: TZStrategy): Integer;
begin
result := deflateInit2_(stream, ZLevels[level], Z_DEFLATED, windowBits,
memLevel, ZStrategies[strategy], ZLIB_VERSION, SizeOf(TZStream));
end;
function ZDeflate(var stream: TZStream; flush: TZFlush): Integer;
begin
result := deflate(stream, ZFlushes[flush]);
end;
function ZDeflateEnd(var stream: TZStream): Integer;
begin
result := deflateEnd(stream);
end;
function ZDeflateReset(var stream: TZStream): Integer;
begin
result := deflateReset(stream);
end;
{** zlib inflate routines ***********************************************************************}
function ZInflateInit(var stream: TZStream): Integer;
begin
result := inflateInit_(stream, ZLIB_VERSION, SizeOf(TZStream));
end;
function ZInflateInit2(var stream: TZStream;
windowBits: Integer): Integer;
begin
result := inflateInit2_(stream, windowBits, ZLIB_VERSION,
SizeOf(TZStream));
end;
function ZInflate(var stream: TZStream; flush: TZFlush): Integer;
begin
result := inflate(stream, ZFlushes[flush]);
end;
function ZInflateEnd(var stream: TZStream): Integer;
begin
result := inflateEnd(stream);
end;
function ZInflateReset(var stream: TZStream): Integer;
begin
result := inflateReset(stream);
end;
{** zlib checksum routines **********************************************************************}
function ZAdler32(adler: Longint; const buffer:pchar; size: Integer): Longint;
begin
result := adler32(adler,buffer,size);
end;
function ZCrc32(crc: Longint; const buffer:pchar; size: Integer): Longint;
begin
result := crc32(crc,buffer,size);
end;
{** zlib extended routines **********************************************************************}
procedure ZDeflateEx(var stream: TZStream; param: Pointer;
read: TZReadFunction; write: TZWriteFunction; flush: TZFlush);
const
bufferSize = 8192;
var
zresult : Integer;
readBuffer : Array [0..bufferSize - 1] of Byte;
writeBuffer: Array [0..bufferSize - 1] of Byte;
writeSize : Integer;
flushEx : TZFlush;
begin
if Assigned(read) then
begin
stream.avail_in := read(param, readBuffer, bufferSize);
end
else stream.avail_in := 0;
repeat
stream.next_in := @readBuffer;
repeat
stream.avail_out := bufferSize;
stream.next_out := @writeBuffer;
flushEx := flush;
if (flushEx = zfFinish) and (stream.avail_in = bufferSize) then
begin
flushEx := zfNoFlush;
end;
zresult := ZCompressCheck(ZDeflate(stream, flushEx));
writeSize := bufferSize - stream.avail_out;
write(param, writeBuffer, writeSize);
until stream.avail_out > 0;
//assert: stream.avail_in = 0
if (zresult <> Z_STREAM_END) and Assigned(read) then
begin
stream.avail_in := read(param, readBuffer, bufferSize);
end;
until (stream.avail_in = 0) and (flush = flushEx);
end;
procedure ZInflateEx(var stream: TZStream; param: Pointer;
read: TZReadFunction; write: TZWriteFunction; flush: TZFlush);
const
bufferSize = 8192;
var
zresult : Integer;
readBuffer : Array [0..bufferSize - 1] of Byte;
writeBuffer: Array [0..bufferSize - 1] of Byte;
writeSize : Integer;
begin
if Assigned(read) then
begin
stream.avail_in := read(param, readBuffer, bufferSize);
end
else stream.avail_in := 0;
zresult := Z_OK;
while (zresult <> Z_STREAM_END) and (stream.avail_in > 0) do
begin
stream.next_in := @readBuffer;
repeat
stream.avail_out := bufferSize;
stream.next_out := @writeBuffer;
zresult := ZDecompressCheck(ZInflate(stream, flush), False);
writeSize := bufferSize - stream.avail_out;
write(param, writeBuffer, writeSize);
until stream.avail_out > 0;
if (zresult <> Z_STREAM_END) and Assigned(read) then
begin
stream.avail_in := read(param, readBuffer, bufferSize);
end;
end;
end;
{** private buffer routines *********************************************************************}
type
PZBufferParam = ^TZBufferParam;
TZBufferParam = packed record
InBuffer : Pointer;
InPosition : Integer;
InSize : Integer;
OutBuffer : Pointer;
OutPosition: Integer;
OutSize : Integer;
end;
function ZBufferRead(p: Pointer; var buffer; size: Integer): Integer;
var
param: PZBufferParam;
begin
param := PZBufferParam(p);
result := param^.InSize - param^.InPosition;
if result > size then result := size;
Move(Pointer(Integer(param^.InBuffer) + param^.InPosition)^, buffer, result);
Inc(param^.InPosition, result);
end;
function ZBufferWrite(p: Pointer; const buffer; size: Integer): Integer;
var
param: PZBufferParam;
begin
param := PZBufferParam(p);
if param^.OutPosition + size > param^.OutSize then
begin
param^.OutSize := param^.OutPosition + size;
ReallocMem(Pointer(param^.OutBuffer), param^.OutSize);
end;
Move(buffer, Pointer(Integer(param^.OutBuffer) + param^.OutPosition)^, size);
Inc(param^.OutPosition, size);
result := size;
end;
procedure ZInternalCompressEx(var zstream: TZStream; const inBuffer: Pointer;
inSize: Integer; out outBuffer: Pointer; out outSize: Integer);
var
param: TZBufferParam;
begin
FillChar(param, SizeOf(TZBufferParam), 0);
outBuffer := Nil;
outSize := 0;
param.InBuffer := inBuffer;
param.InSize := inSize;
try
ZDeflateEx(zstream, @param, @ZBufferRead, @ZBufferWrite, zfFinish);
ZCompressCheck(ZDeflateEnd(zstream));
outBuffer := param.OutBuffer;
outSize := param.OutSize;
except
FreeMem(param.OutBuffer);
raise;
end;
end;
procedure ZInternalDecompressEx(zstream: TZStream; const inBuffer: Pointer;
inSize: Integer; out outBuffer: Pointer; out outSize: Integer;
outEstimate: Integer);
var
param: TZBufferParam;
begin
FillChar(param, SizeOf(TZBufferParam), 0);
outBuffer := Nil;
outSize := 0;
param.InBuffer := inBuffer;
param.InSize := inSize;
if outEstimate > 0 then
begin