32
32
interface
33
33
34
34
uses
35
- SysUtils, ZBase, ZInflate, ZDeflate;
35
+ ZBase, ZInflate, ZDeflate;
36
36
37
37
type
38
38
tzflate = record
@@ -56,7 +56,7 @@ tgzipinfo = record
56
56
footerlen: dword;
57
57
end ;
58
58
59
- Tristate = (tNull, tTrue, tFalse) ;
59
+ TBytes = array of byte ;
60
60
61
61
const
62
62
ZFLATE_ZLIB = 1 ;
@@ -107,10 +107,14 @@ function zfindstream(data: pointer; size: dword; var streamtype: dword; var star
107
107
function gzdeflate (data: pointer; size: dword; var output: pointer; var outputsize: dword; level: dword=9 ): boolean;
108
108
// compress whole string to DEFLATE at once
109
109
function gzdeflate (str: string; level: dword=9 ): string;
110
+ // compress whole bytes to DEFLATE at once
111
+ function gzdeflate (bytes : TBytes; level: dword=9 ): TBytes;
110
112
// decompress whole DEFLATE buffer at once
111
113
function gzinflate (data: pointer; size: dword; var output: pointer; var outputsize: dword): boolean;
112
114
// decompress whole DEFLATE string at once
113
115
function gzinflate (str: string): string;
116
+ // decompress whole DEFLATE bytes at once
117
+ function gzinflate (bytes : TBytes): TBytes;
114
118
115
119
// make ZLIB header
116
120
function makezlibheader (compressionlevel: integer): string;
@@ -120,14 +124,14 @@ function makezlibfooter(adler: dword): string;
120
124
function gzcompress (data: pointer; size: dword; var output: pointer; var outputsize: dword; level: dword=9 ): boolean;
121
125
// compress whole string to ZLIB at once
122
126
function gzcompress (str: string; level: dword=9 ): string;
127
+ // compress whole buffer to ZLIB at once
128
+ function gzcompress (bytes : TBytes; level: dword=9 ) : TBytes;
123
129
// dempress whole ZLIB buffer at once !
124
- function gzuncompress (data: pointer; size: dword; readHeader : Tristate; var output: pointer; var outputsize: dword): boolean;
130
+ function gzuncompress (data: pointer; size: dword; var output: pointer; var outputsize: dword): boolean;
125
131
// dempress whole ZLIB string at once
126
132
function gzuncompress (str: string): string;
127
- // compress whole buffer to ZLIB at once
128
- function gzcompress (bytes : TBytes; level: dword=9 ) : TBytes;
129
133
// dempress whole ZLIB buffer at once
130
- function gzuncompress (bytes : TBytes; readHeader : Tristate = tNull ) : TBytes;
134
+ function gzuncompress (bytes : TBytes) : TBytes;
131
135
132
136
// make GZIP header
133
137
function makegzipheader (compressionlevel: integer; filename: string=' ' ; comment: string=' ' ): string;
@@ -137,15 +141,21 @@ function makegzipfooter(originalsize: dword; crc: dword): string;
137
141
function gzencode (data: pointer; size: dword; var output: pointer; var outputsize: dword; level: dword=9 ; filename: string=' ' ; comment: string=' ' ): boolean;
138
142
// compress whole string to GZIP at once
139
143
function gzencode (str: string; level: dword=9 ; filename: string=' ' ; comment: string=' ' ): string;
144
+ // compress whole string to GZIP at once
145
+ function gzencode (bytes: TBytes; level: dword=9 ; filename: string=' ' ; comment: string=' ' ): TBytes;
140
146
// decompress whole GZIP buffer at once
141
147
function gzdecode (data: pointer; size: dword; var output: pointer; var outputsize: dword): boolean;
142
148
// decompress whole GZIP string at once
143
149
function gzdecode (str: string): string;
150
+ // decompress whole GZIP string at once
151
+ function gzdecode (bytes: TBytes): TBytes;
144
152
145
153
// try to detect buffer format and decompress it at once
146
154
function zdecompress (data: pointer; size: dword; var output: pointer; var outputsize: dword): boolean;
147
155
// try to detect string format and decompress it at once
148
156
function zdecompress (str: string): string;
157
+ // try to detect bytes format and decompress it at once
158
+ function zdecompress (bytes: TBytes): TBytes;
149
159
150
160
// transalte error code to message
151
161
function zflatetranslatecode (code: integer): string;
@@ -412,6 +422,18 @@ function gzdeflate(str: string; level: dword=9): string;
412
422
freemem(p);
413
423
end ;
414
424
425
+ function gzdeflate (bytes: TBytes; level: dword=9 ): TBytes;
426
+ var
427
+ p: pointer;
428
+ d: dword;
429
+ begin
430
+ result := nil ;
431
+ if not gzdeflate(@bytes[0 ], length(bytes), p, d, level) then exit;
432
+ setlength(result, d);
433
+ move(p^, result[0 ], d);
434
+ freemem(p);
435
+ end ;
436
+
415
437
// -- inflate -----------------------------
416
438
417
439
function gzinflate (data: pointer; size: dword; var output: pointer; var outputsize: dword): boolean;
@@ -458,7 +480,19 @@ function gzinflate(str: string): string;
458
480
result := ' ' ;
459
481
if not gzinflate(@str[1 ], length(str), p, d) then exit;
460
482
setlength(result, d);
461
- move(p^, result[1 ], d);
483
+ move(p^, result[1 ], d);
484
+ freemem(p);
485
+ end ;
486
+
487
+ function gzinflate (bytes: TBytes): TBytes;
488
+ var
489
+ p: pointer;
490
+ d: dword;
491
+ begin
492
+ result := nil ;
493
+ if not gzinflate(@bytes[0 ], length(bytes), p, d) then exit;
494
+ setlength(result, d);
495
+ move(p^, result[0 ], d);
462
496
freemem(p);
463
497
end ;
464
498
@@ -544,29 +578,22 @@ function gzcompress(bytes : TBytes; level: dword=9) : TBytes;
544
578
545
579
// -- ZLIB decompress ---------------------
546
580
547
- function gzuncompress (data: pointer; size: dword; readHeader : Tristate; var output: pointer; var outputsize: dword): boolean;
581
+ function gzuncompress (data: pointer; size: dword; var output: pointer; var outputsize: dword): boolean;
548
582
var
549
583
zlib: tzlibinfo;
550
584
z: tzflate;
551
585
checksum: dword;
552
- header : boolean;
553
586
begin
554
587
result := false;
555
- header := false;
556
- if readHeader <> tFalse then
557
- if zreadzlibheader(data, zlib) then
558
- header := true
559
- else if readHeader = tTrue then
560
- exit(zerror(z, ZFLATE_EZLIBINVALID));
588
+ if not zreadzlibheader(data, zlib) then exit(zerror(z, ZFLATE_EZLIBINVALID));
561
589
562
590
checksum := swapendian(pdword(data+size-4 )^);
563
591
564
592
data += zlib.streamat;
565
593
size -= zlib.streamat+zlib.footerlen;
566
594
if not gzinflate(data, size, output, outputsize) then exit;
567
595
568
- if header and (adler32(adler32(0 , nil , 0 ), output, outputsize) <> checksum) then
569
- exit(zerror(z, ZFLATE_ECHECKSUM));
596
+ if (adler32(adler32(0 , nil , 0 ), output, outputsize) <> checksum) then exit(zerror(z, ZFLATE_ECHECKSUM));
570
597
571
598
result := true;
572
599
end ;
@@ -577,19 +604,19 @@ function gzuncompress(str: string): string;
577
604
d: dword;
578
605
begin
579
606
result := ' ' ;
580
- if not gzuncompress(@str[1 ], length(str), tTrue, p, d) then exit;
607
+ if not gzuncompress(@str[1 ], length(str), p, d) then exit;
581
608
setlength(result, d);
582
609
move(p^, result[1 ], d);
583
610
freemem(p);
584
611
end ;
585
612
586
- function gzuncompress (bytes : TBytes; readHeader : Tristate = tNull ) : TBytes;
613
+ function gzuncompress (bytes : TBytes) : TBytes;
587
614
var
588
615
p: pointer;
589
616
d: dword;
590
617
begin
591
618
result := nil ;
592
- if not gzuncompress(@bytes[0 ], length(bytes), readHeader, p, d) then exit;
619
+ if not gzuncompress(@bytes[0 ], length(bytes), p, d) then exit;
593
620
try
594
621
setlength(result, d);
595
622
move(p^, result[0 ], d);
@@ -686,6 +713,18 @@ function gzencode(str: string; level: dword=9; filename: string=''; comment: str
686
713
freemem(p);
687
714
end ;
688
715
716
+ function gzencode (bytes: TBytes; level: dword=9 ; filename: string=' ' ; comment: string=' ' ): TBytes;
717
+ var
718
+ p: pointer;
719
+ d: dword;
720
+ begin
721
+ result := nil ;
722
+ if not gzencode(@bytes[0 ], length(bytes), p, d, level, filename, comment) then exit;
723
+ setlength(result, d);
724
+ move(p^, result[0 ], d);
725
+ freemem(p);
726
+ end ;
727
+
689
728
// -- GZIP decompress ---------------------
690
729
691
730
function gzdecode (data: pointer; size: dword; var output: pointer; var outputsize: dword): boolean;
@@ -722,6 +761,18 @@ function gzdecode(str: string): string;
722
761
freemem(p);
723
762
end ;
724
763
764
+ function gzdecode (bytes: TBytes): TBytes;
765
+ var
766
+ p: pointer;
767
+ d: dword;
768
+ begin
769
+ result := nil ;
770
+ if not gzdecode(@bytes[0 ], length(bytes), p, d) then exit;
771
+ setlength(result, d);
772
+ move(p^, result[0 ], d);
773
+ freemem(p);
774
+ end ;
775
+
725
776
// -- decompress anything -----------------
726
777
727
778
function zdecompress (data: pointer; size: dword; var output: pointer; var outputsize: dword): boolean;
@@ -753,6 +804,18 @@ function zdecompress(str: string): string;
753
804
freemem(p);
754
805
end ;
755
806
807
+ function zdecompress (bytes: TBytes): TBytes;
808
+ var
809
+ p: pointer;
810
+ d: dword;
811
+ begin
812
+ result := nil ;
813
+ if not zdecompress(@bytes[0 ], length(bytes), p, d) then exit;
814
+ setlength(result, d);
815
+ move(p^, result[0 ], d);
816
+ freemem(p);
817
+ end ;
818
+
756
819
// -- error translation -------------------
757
820
758
821
function zflatetranslatecode (code: integer): string;
0 commit comments