File Coverage

blib/lib/Compress/Raw/Zlib.pm
Criterion Covered Total %
statement 198 227 87.2
branch 73 102 71.5
condition 25 41 60.9
subroutine 35 41 85.3
pod 0 1 0.0
total 331 412 80.3


line stmt bran cond sub pod time code
1              
2             package Compress::Raw::Zlib;
3              
4             require 5.006 ;
5             require Exporter;
6 6     6   257975 use Carp ;
  6         15  
  6         636  
7              
8 6     6   57 use strict ;
  6         12  
  6         247  
9 6     6   34 use warnings ;
  6         14  
  6         451  
10 6     6   1461 use bytes ;
  6         1566  
  6         34  
11             our ($VERSION, $XS_VERSION, @ISA, @EXPORT, %EXPORT_TAGS, @EXPORT_OK, $AUTOLOAD, %DEFLATE_CONSTANTS, @DEFLATE_CONSTANTS);
12              
13             $VERSION = '2.222';
14             $XS_VERSION = $VERSION;
15             $VERSION = eval $VERSION;
16              
17             @ISA = qw(Exporter);
18             %EXPORT_TAGS = ( flush => [qw{
19             Z_NO_FLUSH
20             Z_PARTIAL_FLUSH
21             Z_SYNC_FLUSH
22             Z_FULL_FLUSH
23             Z_FINISH
24             Z_BLOCK
25             }],
26             level => [qw{
27             Z_NO_COMPRESSION
28             Z_BEST_SPEED
29             Z_BEST_COMPRESSION
30             Z_DEFAULT_COMPRESSION
31             }],
32             strategy => [qw{
33             Z_FILTERED
34             Z_HUFFMAN_ONLY
35             Z_RLE
36             Z_FIXED
37             Z_DEFAULT_STRATEGY
38             }],
39             status => [qw{
40             Z_OK
41             Z_STREAM_END
42             Z_NEED_DICT
43             Z_ERRNO
44             Z_STREAM_ERROR
45             Z_DATA_ERROR
46             Z_MEM_ERROR
47             Z_BUF_ERROR
48             Z_VERSION_ERROR
49             }],
50             );
51              
52             %DEFLATE_CONSTANTS = %EXPORT_TAGS;
53              
54             # Items to export into callers namespace by default. Note: do not export
55             # names by default without a very good reason. Use EXPORT_OK instead.
56             # Do not simply export all your public functions/methods/constants.
57             @DEFLATE_CONSTANTS =
58             @EXPORT = qw(
59             ZLIB_VERSION
60             ZLIB_VERNUM
61              
62              
63             OS_CODE
64              
65             MAX_MEM_LEVEL
66             MAX_WBITS
67              
68             Z_ASCII
69             Z_BEST_COMPRESSION
70             Z_BEST_SPEED
71             Z_BINARY
72             Z_BLOCK
73             Z_BUF_ERROR
74             Z_DATA_ERROR
75             Z_DEFAULT_COMPRESSION
76             Z_DEFAULT_STRATEGY
77             Z_DEFLATED
78             Z_ERRNO
79             Z_FILTERED
80             Z_FIXED
81             Z_FINISH
82             Z_FULL_FLUSH
83             Z_HUFFMAN_ONLY
84             Z_MEM_ERROR
85             Z_NEED_DICT
86             Z_NO_COMPRESSION
87             Z_NO_FLUSH
88             Z_NULL
89             Z_OK
90             Z_PARTIAL_FLUSH
91             Z_RLE
92             Z_STREAM_END
93             Z_STREAM_ERROR
94             Z_SYNC_FLUSH
95             Z_TREES
96             Z_UNKNOWN
97             Z_VERSION_ERROR
98              
99             ZLIBNG_VERSION
100             ZLIBNG_VERNUM
101             ZLIBNG_VER_MAJOR
102             ZLIBNG_VER_MINOR
103             ZLIBNG_VER_REVISION
104             ZLIBNG_VER_STATUS
105             ZLIBNG_VER_MODIFIED
106              
107             WANT_GZIP
108             WANT_GZIP_OR_ZLIB
109             );
110              
111             push @EXPORT, qw(crc32 adler32 DEF_WBITS);
112              
113 6     6   2037 use constant WANT_GZIP => 16;
  6         13  
  6         708  
114 6     6   40 use constant WANT_GZIP_OR_ZLIB => 32;
  6         25  
  6         981  
115              
116             sub AUTOLOAD {
117 57     57   1812521 my($constname);
118 57         315 ($constname = $AUTOLOAD) =~ s/.*:://;
119 57         625 my ($error, $val) = constant($constname);
120 57 50       193 Carp::croak $error if $error;
121 6     6   40 no strict 'refs';
  6         17  
  6         697  
122 57     1833   266 *{$AUTOLOAD} = sub { $val };
  57         285  
  1833         21865  
123 57         102 goto &{$AUTOLOAD};
  57         207  
124             }
125              
126 6     6   51 use constant FLAG_APPEND => 1 ;
  6         28  
  6         426  
127 6     6   36 use constant FLAG_CRC => 2 ;
  6         9  
  6         338  
128 6     6   47 use constant FLAG_ADLER => 4 ;
  6         11  
  6         338  
129 6     6   35 use constant FLAG_CONSUME_INPUT => 8 ;
  6         12  
  6         423  
130 6     6   34 use constant FLAG_LIMIT_OUTPUT => 16 ;
  6         13  
  6         826  
131              
132             eval {
133             require XSLoader;
134             XSLoader::load('Compress::Raw::Zlib', $XS_VERSION);
135             1;
136             }
137             or do {
138             require DynaLoader;
139             local @ISA = qw(DynaLoader);
140             bootstrap Compress::Raw::Zlib $XS_VERSION ;
141             };
142              
143              
144 6     6   56 use constant Parse_any => 0x01;
  6         10  
  6         349  
145 6     6   31 use constant Parse_unsigned => 0x02;
  6         10  
  6         314  
146 6     6   75 use constant Parse_signed => 0x04;
  6         16  
  6         327  
147 6     6   452 use constant Parse_boolean => 0x08;
  6         11  
  6         293  
148             #use constant Parse_string => 0x10;
149             #use constant Parse_custom => 0x12;
150              
151             #use constant Parse_store_ref => 0x100 ;
152              
153 6     6   29 use constant OFF_PARSED => 0 ;
  6         10  
  6         329  
154 6     6   30 use constant OFF_TYPE => 1 ;
  6         10  
  6         284  
155 6     6   31 use constant OFF_DEFAULT => 2 ;
  6         11  
  6         325  
156 6     6   30 use constant OFF_FIXED => 3 ;
  6         10  
  6         584  
157 6     6   32 use constant OFF_FIRST_ONLY => 4 ;
  6         18  
  6         281  
158 6     6   29 use constant OFF_STICKY => 5 ;
  6         12  
  6         20826  
159              
160              
161              
162             sub ParseParameters
163             {
164 97   50 97 0 389 my $level = shift || 0 ;
165              
166 97         464 my $sub = (caller($level + 1))[3] ;
167             #local $Carp::CarpLevel = 1 ;
168 97         276 my $p = new Compress::Raw::Zlib::Parameters() ;
169 97 100       214 $p->parse(@_)
170             or croak "$sub: $p->{Error}" ;
171              
172 86         176 return $p;
173             }
174              
175              
176             sub Compress::Raw::Zlib::Parameters::new
177             {
178 97     97   147 my $class = shift ;
179              
180 97         280 my $obj = { Error => '',
181             Got => {},
182             } ;
183              
184             #return bless $obj, ref($class) || $class || __PACKAGE__ ;
185 97         241 return bless $obj, 'Compress::Raw::Zlib::Parameters' ;
186             }
187              
188             sub Compress::Raw::Zlib::Parameters::setError
189             {
190 11     11   12 my $self = shift ;
191 11         13 my $error = shift ;
192 11 50       15 my $retval = @_ ? shift : undef ;
193              
194 11         35 $self->{Error} = $error ;
195 11         1290 return $retval;
196             }
197              
198             #sub getError
199             #{
200             # my $self = shift ;
201             # return $self->{Error} ;
202             #}
203              
204             sub Compress::Raw::Zlib::Parameters::parse
205             {
206 97     97   130 my $self = shift ;
207              
208 97         119 my $default = shift ;
209              
210 97         163 my $got = $self->{Got} ;
211 97         120 my $firstTime = keys %{ $got } == 0 ;
  97         224  
212              
213 97         182 my (@Bad) ;
214 97         135 my @entered = () ;
215              
216             # Allow the options to be passed as a hash reference or
217             # as the complete hash.
218 97 100       246 if (@_ == 0) {
    100          
219 11         26 @entered = () ;
220             }
221             elsif (@_ == 1) {
222 9         16 my $href = $_[0] ;
223 9 100 66     69 return $self->setError("Expected even number of parameters, got 1")
      66        
224             if ! defined $href or ! ref $href or ref $href ne "HASH" ;
225              
226 7         25 foreach my $key (keys %$href) {
227 14         25 push @entered, $key ;
228 14         27 push @entered, \$href->{$key} ;
229             }
230             }
231             else {
232 77         123 my $count = @_;
233 77 100       246 return $self->setError("Expected even number of parameters, got $count")
234             if $count % 2 != 0 ;
235              
236 75         211 for my $i (0.. $count / 2 - 1) {
237 120         200 push @entered, $_[2* $i] ;
238 120         263 push @entered, \$_[2* $i+1] ;
239             }
240             }
241              
242              
243 93         319 while (my ($key, $v) = each %$default)
244             {
245 780 50       1254 croak "need 4 params [@$v]"
246             if @$v != 4 ;
247              
248 780         1141 my ($first_only, $sticky, $type, $value) = @$v ;
249 780         823 my $x ;
250 780 50       1193 $self->_checkType($key, \$value, $type, 0, \$x)
251             or return undef ;
252              
253 780         1232 $key = lc $key;
254              
255 780 50 33     1283 if ($firstTime || ! $sticky) {
256 780         1857 $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
257             }
258              
259 780         2046 $got->{$key}[OFF_PARSED] = 0 ;
260             }
261              
262 93         244 for my $i (0.. @entered / 2 - 1) {
263 134         239 my $key = $entered[2* $i] ;
264 134         179 my $value = $entered[2* $i+1] ;
265              
266             #print "Key [$key] Value [$value]" ;
267             #print defined $$value ? "[$$value]\n" : "[undef]\n";
268              
269 134         306 $key =~ s/^-// ;
270 134         204 my $canonkey = lc $key;
271              
272 134 100 33     488 if ($got->{$canonkey} && ($firstTime ||
      66        
273             ! $got->{$canonkey}[OFF_FIRST_ONLY] ))
274             {
275 131         181 my $type = $got->{$canonkey}[OFF_TYPE] ;
276 131         218 my $s ;
277 131 100       276 $self->_checkType($key, $value, $type, 1, \$s)
278             or return undef ;
279             #$value = $$value unless $type & Parse_store_ref ;
280 127         199 $value = $$value ;
281 127         460 $got->{$canonkey} = [1, $type, $value, $s] ;
282             }
283             else
284 3         7 { push (@Bad, $key) }
285             }
286              
287 89 100       166 if (@Bad) {
288 3         8 my ($bad) = join(", ", @Bad) ;
289 3         10 return $self->setError("unknown key value(s) @Bad") ;
290             }
291              
292 86         239 return 1;
293             }
294              
295             sub Compress::Raw::Zlib::Parameters::_checkType
296             {
297 911     911   1023 my $self = shift ;
298              
299 911         977 my $key = shift ;
300 911         1016 my $value = shift ;
301 911         939 my $type = shift ;
302 911         913 my $validate = shift ;
303 911         954 my $output = shift;
304              
305             #local $Carp::CarpLevel = $level ;
306             #print "PARSE $type $key $value $validate $sub\n" ;
307             # if ( $type & Parse_store_ref)
308             # {
309             # #$value = $$value
310             # # if ref ${ $value } ;
311             #
312             # $$output = $value ;
313             # return 1;
314             # }
315              
316 911         1036 $value = $$value ;
317              
318 911 100       1894 if ($type & Parse_any)
    100          
    100          
    50          
319             {
320 90         121 $$output = $value ;
321 90         181 return 1;
322             }
323             elsif ($type & Parse_unsigned)
324             {
325 246 50 66     513 return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
326             if $validate && ! defined $value ;
327 246 100 100     521 return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
328             if $validate && $value !~ /^\d+$/;
329              
330 242 100       354 $$output = defined $value ? $value : 0 ;
331 242         469 return 1;
332             }
333             elsif ($type & Parse_signed)
334             {
335 150 50 66     327 return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
336             if $validate && ! defined $value ;
337 150 50 66     328 return $self->setError("Parameter '$key' must be a signed int, got '$value'")
338             if $validate && $value !~ /^-?\d+$/;
339              
340 150 100       247 $$output = defined $value ? $value : 0 ;
341 150         299 return 1 ;
342             }
343             elsif ($type & Parse_boolean)
344             {
345 425 50 66     1072 return $self->setError("Parameter '$key' must be an int, got '$value'")
      66        
346             if $validate && defined $value && $value !~ /^\d*$/;
347 425 50       679 $$output = defined $value ? $value != 0 : 0 ;
348 425         833 return 1;
349             }
350             # elsif ($type & Parse_string)
351             # {
352             # $$output = defined $value ? $value : "" ;
353             # return 1;
354             # }
355              
356 0         0 $$output = $value ;
357 0         0 return 1;
358             }
359              
360              
361              
362             sub Compress::Raw::Zlib::Parameters::parsed
363             {
364 35     35   47 my $self = shift ;
365 35         29 my $name = shift ;
366              
367 35         285 return $self->{Got}{lc $name}[OFF_PARSED] ;
368             }
369              
370             sub Compress::Raw::Zlib::Parameters::value
371             {
372 784     784   886 my $self = shift ;
373 784         851 my $name = shift ;
374              
375 784 50       1058 if (@_)
376             {
377 0         0 $self->{Got}{lc $name}[OFF_PARSED] = 1;
378 0         0 $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
379 0         0 $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ;
380             }
381              
382 784         27200 return $self->{Got}{lc $name}[OFF_FIXED] ;
383             }
384              
385             our $OPTIONS_deflate =
386             {
387             'AppendOutput' => [1, 1, Parse_boolean, 0],
388             'CRC32' => [1, 1, Parse_boolean, 0],
389             'ADLER32' => [1, 1, Parse_boolean, 0],
390             'Bufsize' => [1, 1, Parse_unsigned, 4096],
391              
392             'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()],
393             'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()],
394             'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()],
395             'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()],
396             'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()],
397             'Dictionary' => [1, 1, Parse_any, ""],
398             };
399              
400             sub Compress::Raw::Zlib::Deflate::new
401             {
402 39     39   55643 my $pkg = shift ;
403 39         143 my ($got) = ParseParameters(0, $OPTIONS_deflate, @_);
404              
405 34 100       90 croak "Compress::Raw::Zlib::Deflate::new: Bufsize must be >= 1, you specified " .
406             $got->value('Bufsize')
407             unless $got->value('Bufsize') >= 1;
408              
409 33         53 my $flags = 0 ;
410 33 100       69 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
411 33 50       70 $flags |= FLAG_CRC if $got->value('CRC32') ;
412 33 50       61 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
413              
414 33         59 my $windowBits = $got->value('WindowBits');
415 33 100       105 $windowBits += MAX_WBITS()
416             if ($windowBits & MAX_WBITS()) == 0 ;
417              
418 33         71 _deflateInit($flags,
419             $got->value('Level'),
420             $got->value('Method'),
421             $windowBits,
422             $got->value('MemLevel'),
423             $got->value('Strategy'),
424             $got->value('Bufsize'),
425             $got->value('Dictionary')) ;
426              
427             }
428              
429             sub Compress::Raw::Zlib::deflateStream::STORABLE_freeze
430             {
431 0     0   0 my $type = ref shift;
432 0         0 croak "Cannot freeze $type object\n";
433             }
434              
435             sub Compress::Raw::Zlib::deflateStream::STORABLE_thaw
436             {
437 0     0   0 my $type = ref shift;
438 0         0 croak "Cannot thaw $type object\n";
439             }
440              
441              
442             our $OPTIONS_inflate =
443             {
444             'AppendOutput' => [1, 1, Parse_boolean, 0],
445             'LimitOutput' => [1, 1, Parse_boolean, 0],
446             'CRC32' => [1, 1, Parse_boolean, 0],
447             'ADLER32' => [1, 1, Parse_boolean, 0],
448             'ConsumeInput' => [1, 1, Parse_boolean, 1],
449             'Bufsize' => [1, 1, Parse_unsigned, 4096],
450              
451             'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()],
452             'Dictionary' => [1, 1, Parse_any, ""],
453             } ;
454              
455             sub Compress::Raw::Zlib::Inflate::new
456             {
457 50     50   4468 my $pkg = shift ;
458 50         137 my ($got) = ParseParameters(0, $OPTIONS_inflate, @_);
459              
460 45 100       123 croak "Compress::Raw::Zlib::Inflate::new: Bufsize must be >= 1, you specified " .
461             $got->value('Bufsize')
462             unless $got->value('Bufsize') >= 1;
463              
464 44         70 my $flags = 0 ;
465 44 100       102 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
466 44 50       92 $flags |= FLAG_CRC if $got->value('CRC32') ;
467 44 50       85 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
468 44 100       80 $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
469 44 100       160 $flags |= FLAG_LIMIT_OUTPUT if $got->value('LimitOutput') ;
470              
471              
472 44         75 my $windowBits = $got->value('WindowBits');
473 44 100       93 $windowBits += MAX_WBITS()
474             if ($windowBits & MAX_WBITS()) == 0 ;
475              
476 44         91 _inflateInit($flags, $windowBits, $got->value('Bufsize'),
477             $got->value('Dictionary')) ;
478             }
479              
480             sub Compress::Raw::Zlib::inflateStream::STORABLE_freeze
481             {
482 0     0   0 my $type = ref shift;
483 0         0 croak "Cannot freeze $type object\n";
484             }
485              
486             sub Compress::Raw::Zlib::inflateStream::STORABLE_thaw
487             {
488 0     0   0 my $type = ref shift;
489 0         0 croak "Cannot thaw $type object\n";
490             }
491              
492             sub Compress::Raw::Zlib::InflateScan::new
493             {
494 1     1   162 my $pkg = shift ;
495 1         6 my ($got) = ParseParameters(0,
496             {
497             'CRC32' => [1, 1, Parse_boolean, 0],
498             'ADLER32' => [1, 1, Parse_boolean, 0],
499             'Bufsize' => [1, 1, Parse_unsigned, 4096],
500              
501             'WindowBits' => [1, 1, Parse_signed, -MAX_WBITS()],
502             'Dictionary' => [1, 1, Parse_any, ""],
503             }, @_) ;
504              
505              
506 1 50       6 croak "Compress::Raw::Zlib::InflateScan::new: Bufsize must be >= 1, you specified " .
507             $got->value('Bufsize')
508             unless $got->value('Bufsize') >= 1;
509              
510 1         2 my $flags = 0 ;
511             #$flags |= FLAG_APPEND if $got->value('AppendOutput') ;
512 1 50       3 $flags |= FLAG_CRC if $got->value('CRC32') ;
513 1 50       3 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
514             #$flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
515              
516 1         4 _inflateScanInit($flags, $got->value('WindowBits'), $got->value('Bufsize'),
517             '') ;
518             }
519              
520             sub Compress::Raw::Zlib::inflateScanStream::createDeflateStream
521             {
522 0     0   0 my $pkg = shift ;
523 0         0 my ($got) = ParseParameters(0,
524             {
525             'AppendOutput' => [1, 1, Parse_boolean, 0],
526             'CRC32' => [1, 1, Parse_boolean, 0],
527             'ADLER32' => [1, 1, Parse_boolean, 0],
528             'Bufsize' => [1, 1, Parse_unsigned, 4096],
529              
530             'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()],
531             'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()],
532             'WindowBits' => [1, 1, Parse_signed, - MAX_WBITS()],
533             'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()],
534             'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()],
535             }, @_) ;
536              
537 0 0       0 croak "Compress::Raw::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified " .
538             $got->value('Bufsize')
539             unless $got->value('Bufsize') >= 1;
540              
541 0         0 my $flags = 0 ;
542 0 0       0 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
543 0 0       0 $flags |= FLAG_CRC if $got->value('CRC32') ;
544 0 0       0 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
545              
546 0         0 $pkg->_createDeflateStream($flags,
547             $got->value('Level'),
548             $got->value('Method'),
549             $got->value('WindowBits'),
550             $got->value('MemLevel'),
551             $got->value('Strategy'),
552             $got->value('Bufsize'),
553             ) ;
554              
555             }
556              
557             sub Compress::Raw::Zlib::inflateScanStream::inflate
558             {
559 0     0   0 my $self = shift ;
560 0         0 my $buffer = $_[1];
561 0         0 my $eof = $_[2];
562              
563 0         0 my $status = $self->scan(@_);
564              
565 0 0 0     0 if ($status == Z_OK() && $_[2]) {
566 0         0 my $byte = ' ';
567              
568 0         0 $status = $self->scan(\$byte, $_[1]) ;
569             }
570              
571 0         0 return $status ;
572             }
573              
574             sub Compress::Raw::Zlib::deflateStream::deflateParams
575             {
576 7     7   537 my $self = shift ;
577 7         54 my ($got) = ParseParameters(0, {
578             'Level' => [1, 1, Parse_signed, undef],
579             'Strategy' => [1, 1, Parse_unsigned, undef],
580             'Bufsize' => [1, 1, Parse_unsigned, undef],
581             },
582             @_) ;
583              
584 6 100       25 croak "Compress::Raw::Zlib::deflateParams needs Level and/or Strategy"
585             unless $got->parsed('Level') + $got->parsed('Strategy') +
586             $got->parsed('Bufsize');
587              
588 5 100 100     9 croak "Compress::Raw::Zlib::Inflate::deflateParams: Bufsize must be >= 1, you specified " .
589             $got->value('Bufsize')
590             if $got->parsed('Bufsize') && $got->value('Bufsize') <= 1;
591              
592 4         4 my $flags = 0;
593 4 100       7 $flags |= 1 if $got->parsed('Level') ;
594 4 100       6 $flags |= 2 if $got->parsed('Strategy') ;
595 4 100       7 $flags |= 4 if $got->parsed('Bufsize') ;
596              
597 4         8 $self->_deflateParams($flags, $got->value('Level'),
598             $got->value('Strategy'), $got->value('Bufsize'));
599              
600             }
601              
602              
603             1;
604             __END__
605              
606              
607             =head1 NAME
608              
609             Compress::Raw::Zlib - Low-Level Interface to zlib or zlib-ng compression library
610              
611             =head1 SYNOPSIS
612              
613             use Compress::Raw::Zlib ;
614              
615             ($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) ;
616             $status = $d->deflate($input, $output) ;
617             $status = $d->flush($output [, $flush_type]) ;
618             $d->deflateReset() ;
619             $d->deflateParams(OPTS) ;
620             $d->deflateTune(OPTS) ;
621             $d->dict_adler() ;
622             $d->crc32() ;
623             $d->adler32() ;
624             $d->total_in() ;
625             $d->total_out() ;
626             $d->msg() ;
627             $d->get_Strategy();
628             $d->get_Level();
629             $d->get_Bufsize();
630              
631             ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) ;
632             $status = $i->inflate($input, $output [, $eof]) ;
633             $status = $i->inflateSync($input) ;
634             $i->inflateReset() ;
635             $i->dict_adler() ;
636             $d->crc32() ;
637             $d->adler32() ;
638             $i->total_in() ;
639             $i->total_out() ;
640             $i->msg() ;
641             $d->get_Bufsize();
642              
643             $crc = adler32($buffer [,$crc]) ;
644             $crc = crc32($buffer [,$crc]) ;
645              
646             $crc = crc32_combine($crc1, $crc2, $len2);
647             $adler = adler32_combine($adler1, $adler2, $len2);
648              
649             my $version = Compress::Raw::Zlib::zlib_version();
650             my $flags = Compress::Raw::Zlib::zlibCompileFlags();
651              
652             is_zlib_native();
653             is_zlibng_native();
654             is_zlibng_compat();
655             is_zlibng();
656              
657             =head1 DESCRIPTION
658              
659             The I<Compress::Raw::Zlib> module provides a Perl interface to the I<zlib> or I<zlib-ng>
660             compression libraries (see L</SEE ALSO> for details about where to get
661             I<zlib> or I<zlib-ng>).
662              
663             In the text below all references to I<zlib> are also applicable to I<zlib-ng> unless otherwise stated.
664              
665             =head1 Compress::Raw::Zlib::Deflate
666              
667             This section defines an interface that allows in-memory compression using
668             the I<deflate> interface provided by zlib.
669              
670             Here is a definition of the interface available:
671              
672             =head2 B<($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) >
673              
674             Initialises a deflation object.
675              
676             If you are familiar with the I<zlib> library, it combines the
677             features of the I<zlib> functions C<deflateInit>, C<deflateInit2>
678             and C<deflateSetDictionary>.
679              
680             If successful, it will return the initialised deflation object, C<$d>
681             and a C<$status> of C<Z_OK> in a list context. In scalar context it
682             returns the deflation object, C<$d>, only.
683              
684             If not successful, the returned deflation object, C<$d>, will be
685             I<undef> and C<$status> will hold the a I<zlib> error code.
686              
687             The function optionally takes a number of named options specified as
688             C<< Name => value >> pairs. This allows individual options to be
689             tailored without having to specify them all in the parameter list.
690              
691             For backward compatibility, it is also possible to pass the parameters
692             as a reference to a hash containing the name=>value pairs.
693              
694             Below is a list of the valid options:
695              
696             =over 5
697              
698             =item B<-Level>
699              
700             Defines the compression level. Valid values are 0 through 9,
701             C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
702             C<Z_DEFAULT_COMPRESSION>.
703              
704             The default is C<Z_DEFAULT_COMPRESSION>.
705              
706             =item B<-Method>
707              
708             Defines the compression method. The only valid value at present (and
709             the default) is C<Z_DEFLATED>.
710              
711             =item B<-WindowBits>
712              
713             To compress an RFC 1950 data stream, set C<WindowBits> to a positive
714             number between 8 and 15.
715              
716             To compress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>.
717              
718             To compress an RFC 1952 data stream (i.e. gzip), set C<WindowBits> to
719             C<WANT_GZIP>.
720              
721             For a definition of the meaning and valid values for C<WindowBits>
722             refer to the I<zlib> documentation for I<deflateInit2>.
723              
724             Defaults to C<MAX_WBITS>.
725              
726             =item B<-MemLevel>
727              
728             For a definition of the meaning and valid values for C<MemLevel>
729             refer to the I<zlib> documentation for I<deflateInit2>.
730              
731             Defaults to MAX_MEM_LEVEL.
732              
733             =item B<-Strategy>
734              
735             Defines the strategy used to tune the compression. The valid values are
736             C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED>, C<Z_RLE>, C<Z_FIXED> and
737             C<Z_HUFFMAN_ONLY>.
738              
739             The default is C<Z_DEFAULT_STRATEGY>.
740              
741             =item B<-Dictionary>
742              
743             When a dictionary is specified I<Compress::Raw::Zlib> will automatically
744             call C<deflateSetDictionary> directly after calling C<deflateInit>. The
745             Adler32 value for the dictionary can be obtained by calling the method
746             C<$d-E<gt>dict_adler()>.
747              
748             The default is no dictionary.
749              
750             =item B<-Bufsize>
751              
752             Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
753             and C<$d-E<gt>flush> methods. If the buffer has to be
754             reallocated to increase the size, it will grow in increments of
755             C<Bufsize>.
756              
757             The default buffer size is 4096.
758              
759             =item B<-AppendOutput>
760              
761             This option controls how data is written to the output buffer by the
762             C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
763              
764             If the C<AppendOutput> option is set to false, the output buffers in the
765             C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods will be truncated before
766             uncompressed data is written to them.
767              
768             If the option is set to true, uncompressed data will be appended to the
769             output buffer in the C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
770              
771             This option defaults to false.
772              
773             =item B<-CRC32>
774              
775             If set to true, a crc32 checksum of the uncompressed data will be
776             calculated. Use the C<$d-E<gt>crc32> method to retrieve this value.
777              
778             This option defaults to false.
779              
780             =item B<-ADLER32>
781              
782             If set to true, an adler32 checksum of the uncompressed data will be
783             calculated. Use the C<$d-E<gt>adler32> method to retrieve this value.
784              
785             This option defaults to false.
786              
787             =back
788              
789             Here is an example of using the C<Compress::Raw::Zlib::Deflate> optional
790             parameter list to override the default buffer size and compression
791             level. All other options will take their default values.
792              
793             my $d = new Compress::Raw::Zlib::Deflate ( -Bufsize => 300,
794             -Level => Z_BEST_SPEED ) ;
795              
796             =head2 B<$status = $d-E<gt>deflate($input, $output)>
797              
798             Deflates the contents of C<$input> and writes the compressed data to
799             C<$output>.
800              
801             The C<$input> and C<$output> parameters can be either scalars or scalar
802             references.
803              
804             When finished, C<$input> will be completely processed (assuming there
805             were no errors). If the deflation was successful it writes the deflated
806             data to C<$output> and returns a status value of C<Z_OK>.
807              
808             On error, it returns a I<zlib> error code.
809              
810             If the C<AppendOutput> option is set to true in the constructor for
811             the C<$d> object, the compressed data will be appended to C<$output>. If
812             it is false, C<$output> will be truncated before any compressed data is
813             written to it.
814              
815             B<Note>: This method will not necessarily write compressed data to
816             C<$output> every time it is called. So do not assume that there has been
817             an error if the contents of C<$output> is empty on returning from
818             this method. As long as the return code from the method is C<Z_OK>,
819             the deflate has succeeded.
820              
821             =head2 B<$status = $d-E<gt>flush($output [, $flush_type]) >
822              
823             Typically used to finish the deflation. Any pending output will be
824             written to C<$output>.
825              
826             Returns C<Z_OK> if successful.
827              
828             Note that flushing can seriously degrade the compression ratio, so it
829             should only be used to terminate a decompression (using C<Z_FINISH>) or
830             when you want to create a I<full flush point> (using C<Z_FULL_FLUSH>).
831              
832             By default the C<flush_type> used is C<Z_FINISH>. Other valid values
833             for C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH>
834             and C<Z_FULL_FLUSH>. It is strongly recommended that you only set the
835             C<flush_type> parameter if you fully understand the implications of
836             what it does. See the C<zlib> documentation for details.
837              
838             If the C<AppendOutput> option is set to true in the constructor for
839             the C<$d> object, the compressed data will be appended to C<$output>. If
840             it is false, C<$output> will be truncated before any compressed data is
841             written to it.
842              
843             =head2 B<$status = $d-E<gt>deflateReset() >
844              
845             This method will reset the deflation object C<$d>. It can be used when you
846             are compressing multiple data streams and want to use the same object to
847             compress each of them. It should only be used once the previous data stream
848             has been flushed successfully, i.e. a call to C<< $d->flush(Z_FINISH) >> has
849             returned C<Z_OK>.
850              
851             Returns C<Z_OK> if successful.
852              
853             =head2 B<$status = $d-E<gt>deflateParams([OPT])>
854              
855             Change settings for the deflate object C<$d>.
856              
857             The list of the valid options is shown below. Options not specified
858             will remain unchanged.
859              
860             =over 5
861              
862             =item B<-Level>
863              
864             Defines the compression level. Valid values are 0 through 9,
865             C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
866             C<Z_DEFAULT_COMPRESSION>.
867              
868             =item B<-Strategy>
869              
870             Defines the strategy used to tune the compression. The valid values are
871             C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
872              
873             =item B<-BufSize>
874              
875             Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
876             and C<$d-E<gt>flush> methods. If the buffer has to be
877             reallocated to increase the size, it will grow in increments of
878             C<Bufsize>.
879              
880             =back
881              
882             =head2 B<$status = $d-E<gt>deflateTune($good_length, $max_lazy, $nice_length, $max_chain)>
883              
884             Tune the internal settings for the deflate object C<$d>. This option is
885             only available if you are running zlib 1.2.2.3 or better.
886              
887             Refer to the documentation in zlib.h for instructions on how to fly
888             C<deflateTune>.
889              
890             =head2 B<$d-E<gt>dict_adler()>
891              
892             Returns the adler32 value for the dictionary.
893              
894             =head2 B<$d-E<gt>crc32()>
895              
896             Returns the crc32 value for the uncompressed data to date.
897              
898             If the C<CRC32> option is not enabled in the constructor for this object,
899             this method will always return 0;
900              
901             =head2 B<$d-E<gt>adler32()>
902              
903             Returns the adler32 value for the uncompressed data to date.
904              
905             =head2 B<$d-E<gt>msg()>
906              
907             Returns the last error message generated by zlib.
908              
909             =head2 B<$d-E<gt>total_in()>
910              
911             Returns the total number of bytes uncompressed bytes input to deflate.
912              
913             =head2 B<$d-E<gt>total_out()>
914              
915             Returns the total number of compressed bytes output from deflate.
916              
917             =head2 B<$d-E<gt>get_Strategy()>
918              
919             Returns the deflation strategy currently used. Valid values are
920             C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
921              
922             =head2 B<$d-E<gt>get_Level()>
923              
924             Returns the compression level being used.
925              
926             =head2 B<$d-E<gt>get_Bufsize()>
927              
928             Returns the buffer size used to carry out the compression.
929              
930             =head2 Example
931              
932             Here is a trivial example of using C<deflate>. It simply reads standard
933             input, deflates it and writes it to standard output.
934              
935             use strict ;
936             use warnings ;
937              
938             use Compress::Raw::Zlib ;
939              
940             binmode STDIN;
941             binmode STDOUT;
942             my $x = new Compress::Raw::Zlib::Deflate
943             or die "Cannot create a deflation stream\n" ;
944              
945             my ($output, $status) ;
946             while (<>)
947             {
948             $status = $x->deflate($_, $output) ;
949              
950             $status == Z_OK
951             or die "deflation failed\n" ;
952              
953             print $output ;
954             }
955              
956             $status = $x->flush($output) ;
957              
958             $status == Z_OK
959             or die "deflation failed\n" ;
960              
961             print $output ;
962              
963             =head1 Compress::Raw::Zlib::Inflate
964              
965             This section defines an interface that allows in-memory uncompression using
966             the I<inflate> interface provided by zlib.
967              
968             Here is a definition of the interface:
969              
970             =head2 B< ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) >
971              
972             Initialises an inflation object.
973              
974             In a list context it returns the inflation object, C<$i>, and the
975             I<zlib> status code (C<$status>). In a scalar context it returns the
976             inflation object only.
977              
978             If successful, C<$i> will hold the inflation object and C<$status> will
979             be C<Z_OK>.
980              
981             If not successful, C<$i> will be I<undef> and C<$status> will hold the
982             I<zlib> error code.
983              
984             The function optionally takes a number of named options specified as
985             C<< -Name => value >> pairs. This allows individual options to be
986             tailored without having to specify them all in the parameter list.
987              
988             For backward compatibility, it is also possible to pass the parameters
989             as a reference to a hash containing the C<< name=>value >> pairs.
990              
991             Here is a list of the valid options:
992              
993             =over 5
994              
995             =item B<-WindowBits>
996              
997             To uncompress an RFC 1950 data stream, set C<WindowBits> to a positive
998             number between 8 and 15.
999              
1000             To uncompress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>.
1001              
1002             To uncompress an RFC 1952 data stream (i.e. gzip), set C<WindowBits> to
1003             C<WANT_GZIP>.
1004              
1005             To auto-detect and uncompress an RFC 1950 or RFC 1952 data stream (i.e.
1006             gzip), set C<WindowBits> to C<WANT_GZIP_OR_ZLIB>.
1007              
1008             For a full definition of the meaning and valid values for C<WindowBits>
1009             refer to the I<zlib> documentation for I<inflateInit2>.
1010              
1011             Defaults to C<MAX_WBITS>.
1012              
1013             =item B<-Bufsize>
1014              
1015             Sets the initial size for the output buffer used by the C<$i-E<gt>inflate>
1016             method. If the output buffer in this method has to be reallocated to
1017             increase the size, it will grow in increments of C<Bufsize>.
1018              
1019             Default is 4096.
1020              
1021             =item B<-Dictionary>
1022              
1023             The default is no dictionary.
1024              
1025             =item B<-AppendOutput>
1026              
1027             This option controls how data is written to the output buffer by the
1028             C<$i-E<gt>inflate> method.
1029              
1030             If the option is set to false, the output buffer in the C<$i-E<gt>inflate>
1031             method will be truncated before uncompressed data is written to it.
1032              
1033             If the option is set to true, uncompressed data will be appended to the
1034             output buffer by the C<$i-E<gt>inflate> method.
1035              
1036             This option defaults to false.
1037              
1038             =item B<-CRC32>
1039              
1040             If set to true, a crc32 checksum of the uncompressed data will be
1041             calculated. Use the C<$i-E<gt>crc32> method to retrieve this value.
1042              
1043             This option defaults to false.
1044              
1045             =item B<-ADLER32>
1046              
1047             If set to true, an adler32 checksum of the uncompressed data will be
1048             calculated. Use the C<$i-E<gt>adler32> method to retrieve this value.
1049              
1050             This option defaults to false.
1051              
1052             =item B<-ConsumeInput>
1053              
1054             If set to true, this option will remove compressed data from the input
1055             buffer of the C<< $i->inflate >> method as the inflate progresses.
1056              
1057             This option can be useful when you are processing compressed data that is
1058             embedded in another file/buffer. In this case the data that immediately
1059             follows the compressed stream will be left in the input buffer.
1060              
1061             This option defaults to true.
1062              
1063             =item B<-LimitOutput>
1064              
1065             The C<LimitOutput> option changes the behavior of the C<< $i->inflate >>
1066             method so that the amount of memory used by the output buffer can be
1067             limited.
1068              
1069             When C<LimitOutput> is used the size of the output buffer used will either
1070             be the value of the C<Bufsize> option or the amount of memory already
1071             allocated to C<$output>, whichever is larger. Predicting the output size
1072             available is tricky, so don't rely on getting an exact output buffer size.
1073              
1074             When C<LimitOutout> is not specified C<< $i->inflate >> will use as much
1075             memory as it takes to write all the uncompressed data it creates by
1076             uncompressing the input buffer.
1077              
1078             If C<LimitOutput> is enabled, the C<ConsumeInput> option will also be
1079             enabled.
1080              
1081             This option defaults to false.
1082              
1083             See L</The LimitOutput option> for a discussion on why C<LimitOutput> is
1084             needed and how to use it.
1085              
1086             =back
1087              
1088             Here is an example of using an optional parameter to override the default
1089             buffer size.
1090              
1091             my ($i, $status) = new Compress::Raw::Zlib::Inflate( -Bufsize => 300 ) ;
1092              
1093             =head2 B< $status = $i-E<gt>inflate($input, $output [,$eof]) >
1094              
1095             Inflates the complete contents of C<$input> and writes the uncompressed
1096             data to C<$output>. The C<$input> and C<$output> parameters can either be
1097             scalars or scalar references.
1098              
1099             Returns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the
1100             compressed data has been successfully reached.
1101              
1102             If not successful C<$status> will hold the I<zlib> error code.
1103              
1104             If the C<ConsumeInput> option has been set to true when the
1105             C<Compress::Raw::Zlib::Inflate> object is created, the C<$input> parameter
1106             is modified by C<inflate>. On completion it will contain what remains
1107             of the input buffer after inflation. In practice, this means that when
1108             the return status is C<Z_OK> the C<$input> parameter will contain an
1109             empty string, and when the return status is C<Z_STREAM_END> the C<$input>
1110             parameter will contains what (if anything) was stored in the input buffer
1111             after the deflated data stream.
1112              
1113             This feature is useful when processing a file format that encapsulates
1114             a compressed data stream (e.g. gzip, zip) and there is useful data
1115             immediately after the deflation stream.
1116              
1117             If the C<AppendOutput> option is set to true in the constructor for
1118             this object, the uncompressed data will be appended to C<$output>. If
1119             it is false, C<$output> will be truncated before any uncompressed data
1120             is written to it.
1121              
1122             The C<$eof> parameter needs a bit of explanation.
1123              
1124             Prior to version 1.2.0, zlib assumed that there was at least one trailing
1125             byte immediately after the compressed data stream when it was carrying out
1126             decompression. This normally isn't a problem because the majority of zlib
1127             applications guarantee that there will be data directly after the
1128             compressed data stream. For example, both gzip (RFC 1950) and zip both
1129             define trailing data that follows the compressed data stream.
1130              
1131             The C<$eof> parameter only needs to be used if B<all> of the following
1132             conditions apply
1133              
1134             =over 5
1135              
1136             =item 1
1137              
1138             You are either using a copy of zlib that is older than version 1.2.0 or you
1139             want your application code to be able to run with as many different
1140             versions of zlib as possible.
1141              
1142             =item 2
1143              
1144             You have set the C<WindowBits> parameter to C<-MAX_WBITS> in the constructor
1145             for this object, i.e. you are uncompressing a raw deflated data stream
1146             (RFC 1951).
1147              
1148             =item 3
1149              
1150             There is no data immediately after the compressed data stream.
1151              
1152             =back
1153              
1154             If B<all> of these are the case, then you need to set the C<$eof> parameter
1155             to true on the final call (and only the final call) to C<$i-E<gt>inflate>.
1156              
1157             If you have built this module with zlib >= 1.2.0, the C<$eof> parameter is
1158             ignored. You can still set it if you want, but it won't be used behind the
1159             scenes.
1160              
1161             =head2 B<$status = $i-E<gt>inflateSync($input)>
1162              
1163             This method can be used to attempt to recover good data from a compressed
1164             data stream that is partially corrupt.
1165             It scans C<$input> until it reaches either a I<full flush point> or the
1166             end of the buffer.
1167              
1168             If a I<full flush point> is found, C<Z_OK> is returned and C<$input>
1169             will be have all data up to the flush point removed. This data can then be
1170             passed to the C<$i-E<gt>inflate> method to be uncompressed.
1171              
1172             Any other return code means that a flush point was not found. If more
1173             data is available, C<inflateSync> can be called repeatedly with more
1174             compressed data until the flush point is found.
1175              
1176             Note I<full flush points> are not present by default in compressed
1177             data streams. They must have been added explicitly when the data stream
1178             was created by calling C<Compress::Deflate::flush> with C<Z_FULL_FLUSH>.
1179              
1180             =head2 B<$status = $i-E<gt>inflateReset() >
1181              
1182             This method will reset the inflation object C<$i>. It can be used when you
1183             are uncompressing multiple data streams and want to use the same object to
1184             uncompress each of them.
1185              
1186             Returns C<Z_OK> if successful.
1187              
1188             =head2 B<$i-E<gt>dict_adler()>
1189              
1190             Returns the adler32 value for the dictionary.
1191              
1192             =head2 B<$i-E<gt>crc32()>
1193              
1194             Returns the crc32 value for the uncompressed data to date.
1195              
1196             If the C<CRC32> option is not enabled in the constructor for this object,
1197             this method will always return 0;
1198              
1199             =head2 B<$i-E<gt>adler32()>
1200              
1201             Returns the adler32 value for the uncompressed data to date.
1202              
1203             If the C<ADLER32> option is not enabled in the constructor for this object,
1204             this method will always return 0;
1205              
1206             =head2 B<$i-E<gt>msg()>
1207              
1208             Returns the last error message generated by zlib.
1209              
1210             =head2 B<$i-E<gt>total_in()>
1211              
1212             Returns the total number of bytes compressed bytes input to inflate.
1213              
1214             =head2 B<$i-E<gt>total_out()>
1215              
1216             Returns the total number of uncompressed bytes output from inflate.
1217              
1218             =head2 B<$d-E<gt>get_Bufsize()>
1219              
1220             Returns the buffer size used to carry out the decompression.
1221              
1222             =head2 Examples
1223              
1224             Here is an example of using C<inflate>.
1225              
1226             use strict ;
1227             use warnings ;
1228              
1229             use Compress::Raw::Zlib;
1230              
1231             my $x = new Compress::Raw::Zlib::Inflate()
1232             or die "Cannot create a inflation stream\n" ;
1233              
1234             my $input = '' ;
1235             binmode STDIN;
1236             binmode STDOUT;
1237              
1238             my ($output, $status) ;
1239             while (read(STDIN, $input, 4096))
1240             {
1241             $status = $x->inflate($input, $output) ;
1242              
1243             print $output ;
1244              
1245             last if $status != Z_OK ;
1246             }
1247              
1248             die "inflation failed\n"
1249             unless $status == Z_STREAM_END ;
1250              
1251             The next example show how to use the C<LimitOutput> option. Notice the use
1252             of two nested loops in this case. The outer loop reads the data from the
1253             input source - STDIN and the inner loop repeatedly calls C<inflate> until
1254             C<$input> is exhausted, we get an error, or the end of the stream is
1255             reached. One point worth remembering is by using the C<LimitOutput> option
1256             you also get C<ConsumeInput> set as well - this makes the code below much
1257             simpler.
1258              
1259             use strict ;
1260             use warnings ;
1261              
1262             use Compress::Raw::Zlib;
1263              
1264             my $x = new Compress::Raw::Zlib::Inflate(LimitOutput => 1)
1265             or die "Cannot create a inflation stream\n" ;
1266              
1267             my $input = '' ;
1268             binmode STDIN;
1269             binmode STDOUT;
1270              
1271             my ($output, $status) ;
1272              
1273             OUTER:
1274             while (read(STDIN, $input, 4096))
1275             {
1276             do
1277             {
1278             $status = $x->inflate($input, $output) ;
1279              
1280             print $output ;
1281              
1282             last OUTER
1283             unless $status == Z_OK || $status == Z_BUF_ERROR ;
1284             }
1285             while length $input;
1286             }
1287              
1288             die "inflation failed\n"
1289             unless $status == Z_STREAM_END ;
1290              
1291             =head1 CHECKSUM FUNCTIONS
1292              
1293             Two functions are provided by I<zlib> to calculate checksums. For the
1294             Perl interface, the order of the two parameters in both functions has
1295             been reversed. This allows both running checksums and one off
1296             calculations to be done.
1297              
1298             $crc = adler32($buffer [,$crc]) ;
1299             $crc = crc32($buffer [,$crc]) ;
1300              
1301             The buffer parameters can either be a scalar or a scalar reference.
1302              
1303             If the $crc parameters is C<undef>, the crc value will be reset.
1304              
1305             If you have built this module with zlib 1.2.3 or better, two more
1306             CRC-related functions are available.
1307              
1308             $crc = crc32_combine($crc1, $crc2, $len2);
1309             $adler = adler32_combine($adler1, $adler2, $len2);
1310              
1311             These functions allow checksums to be merged.
1312             Refer to the I<zlib> documentation for more details.
1313              
1314             =head1 Misc
1315              
1316             =head2 my $version = Compress::Raw::Zlib::zlib_version();
1317              
1318             Returns the version of the I<zlib> library if this module has been built with the I<zlib> library.
1319             If this module has been built with I<zlib-ng> in native mode, this function will return a empty string.
1320             If this module has been built with I<zlib-ng> in compat mode, this function will return the Izlib> API
1321             version that I<zlib-ng> is supporting.
1322              
1323             =head2 my $version = Compress::Raw::Zlib::zlibng_version();
1324              
1325             Returns the version of the zlib-ng library if this module has been built with the I<zlib-ng> library.
1326             If this module has been built with I<zlib>, this function will return a empty string.
1327              
1328             =head2 my $flags = Compress::Raw::Zlib::zlibCompileFlags();
1329              
1330             Returns the flags indicating compile-time options that were used to build
1331             the zlib or zlib-ng library. See the zlib documentation for a description of the flags
1332             returned by C<zlibCompileFlags>.
1333              
1334             Note that when the zlib sources are built along with this module the
1335             C<sprintf> flags (bits 24, 25 and 26) should be ignored.
1336              
1337             If you are using zlib 1.2.0 or older, C<zlibCompileFlags> will return 0.
1338              
1339             =head2 is_zlib_native();
1340             =head2 is_zlibng_native();
1341             =head2 is_zlibng_compat();
1342             =head2 is_zlibng();
1343              
1344             These function can use used to check if C<Compress::Raw::Zlib> was been built with I<zlib> or I<zlib-ng>.
1345              
1346             The function C<is_zlib_native> returns true if C<Compress::Raw::Zlib> was built with I<zlib>.
1347             The function C<is_zlibng> returns true if C<Compress::Raw::Zlib> was built with I<zlib-ng>.
1348              
1349             The I<zlib-ng> library has an option to build with a zlib-compataible API.
1350             The c<is_zlibng_compat> function retuens true if zlib-ng has ben built with this API.
1351              
1352             Finally, C<is_zlibng_native> returns true if I<zlib-ng> was built with its native API.
1353              
1354             =head1 The LimitOutput option.
1355              
1356             By default C<< $i->inflate($input, $output) >> will uncompress I<all> data
1357             in C<$input> and write I<all> of the uncompressed data it has generated to
1358             C<$output>. This makes the interface to C<inflate> much simpler - if the
1359             method has uncompressed C<$input> successfully I<all> compressed data in
1360             C<$input> will have been dealt with. So if you are reading from an input
1361             source and uncompressing as you go the code will look something like this
1362              
1363             use strict ;
1364             use warnings ;
1365              
1366             use Compress::Raw::Zlib;
1367              
1368             my $x = new Compress::Raw::Zlib::Inflate()
1369             or die "Cannot create a inflation stream\n" ;
1370              
1371             my $input = '' ;
1372              
1373             my ($output, $status) ;
1374             while (read(STDIN, $input, 4096))
1375             {
1376             $status = $x->inflate($input, $output) ;
1377              
1378             print $output ;
1379              
1380             last if $status != Z_OK ;
1381             }
1382              
1383             die "inflation failed\n"
1384             unless $status == Z_STREAM_END ;
1385              
1386             The points to note are
1387              
1388             =over 5
1389              
1390             =item *
1391              
1392             The main processing loop in the code handles reading of compressed data
1393             from STDIN.
1394              
1395             =item *
1396              
1397             The status code returned from C<inflate> will only trigger termination of
1398             the main processing loop if it isn't C<Z_OK>. When C<LimitOutput> has not
1399             been used the C<Z_OK> status means that the end of the compressed
1400             data stream has been reached or there has been an error in uncompression.
1401              
1402             =item *
1403              
1404             After the call to C<inflate> I<all> of the uncompressed data in C<$input>
1405             will have been processed. This means the subsequent call to C<read> can
1406             overwrite it's contents without any problem.
1407              
1408             =back
1409              
1410             For most use-cases the behavior described above is acceptable (this module
1411             and it's predecessor, C<Compress::Zlib>, have used it for over 10 years
1412             without an issue), but in a few very specific use-cases the amount of
1413             memory required for C<$output> can prohibitively large. For example, if the
1414             compressed data stream contains the same pattern repeated thousands of
1415             times, a relatively small compressed data stream can uncompress into
1416             hundreds of megabytes. Remember C<inflate> will keep allocating memory
1417             until I<all> the uncompressed data has been written to the output buffer -
1418             the size of C<$output> is unbounded.
1419              
1420             The C<LimitOutput> option is designed to help with this use-case.
1421              
1422             The main difference in your code when using C<LimitOutput> is having to
1423             deal with cases where the C<$input> parameter still contains some
1424             uncompressed data that C<inflate> hasn't processed yet. The status code
1425             returned from C<inflate> will be C<Z_OK> if uncompression took place and
1426             C<Z_BUF_ERROR> if the output buffer is full.
1427              
1428             Below is typical code that shows how to use C<LimitOutput>.
1429              
1430             use strict ;
1431             use warnings ;
1432              
1433             use Compress::Raw::Zlib;
1434              
1435             my $x = new Compress::Raw::Zlib::Inflate(LimitOutput => 1)
1436             or die "Cannot create a inflation stream\n" ;
1437              
1438             my $input = '' ;
1439             binmode STDIN;
1440             binmode STDOUT;
1441              
1442             my ($output, $status) ;
1443              
1444             OUTER:
1445             while (read(STDIN, $input, 4096))
1446             {
1447             do
1448             {
1449             $status = $x->inflate($input, $output) ;
1450              
1451             print $output ;
1452              
1453             last OUTER
1454             unless $status == Z_OK || $status == Z_BUF_ERROR ;
1455             }
1456             while length $input;
1457             }
1458              
1459             die "inflation failed\n"
1460             unless $status == Z_STREAM_END ;
1461              
1462             Points to note this time:
1463              
1464             =over 5
1465              
1466             =item *
1467              
1468             There are now two nested loops in the code: the outer loop for reading the
1469             compressed data from STDIN, as before; and the inner loop to carry out the
1470             uncompression.
1471              
1472             =item *
1473              
1474             There are two exit points from the inner uncompression loop.
1475              
1476             Firstly when C<inflate> has returned a status other than C<Z_OK> or
1477             C<Z_BUF_ERROR>. This means that either the end of the compressed data
1478             stream has been reached (C<Z_STREAM_END>) or there is an error in the
1479             compressed data. In either of these cases there is no point in continuing
1480             with reading the compressed data, so both loops are terminated.
1481              
1482             The second exit point tests if there is any data left in the input buffer,
1483             C<$input> - remember that the C<ConsumeInput> option is automatically
1484             enabled when C<LimitOutput> is used. When the input buffer has been
1485             exhausted, the outer loop can run again and overwrite a now empty
1486             C<$input>.
1487              
1488             =back
1489              
1490             =head1 ACCESSING ZIP FILES
1491              
1492             Although it is possible (with some effort on your part) to use this module
1493             to access .zip files, there are other perl modules available that will do
1494             all the hard work for you. Check out C<Archive::Zip>,
1495             C<Archive::Zip::SimpleZip>, C<IO::Compress::Zip> and
1496             C<IO::Uncompress::Unzip>.
1497              
1498             =head1 FAQ
1499              
1500             =head2 Compatibility with Unix compress/uncompress.
1501              
1502             This module is not compatible with Unix C<compress>.
1503              
1504             If you have the C<uncompress> program available, you can use this to read
1505             compressed files
1506              
1507             open F, "uncompress -c $filename |";
1508             while (<F>)
1509             {
1510             ...
1511              
1512             Alternatively, if you have the C<gunzip> program available, you can use
1513             this to read compressed files
1514              
1515             open F, "gunzip -c $filename |";
1516             while (<F>)
1517             {
1518             ...
1519              
1520             and this to write compress files, if you have the C<compress> program
1521             available
1522              
1523             open F, "| compress -c $filename ";
1524             print F "data";
1525             ...
1526             close F ;
1527              
1528             =head2 Accessing .tar.Z files
1529              
1530             See previous FAQ item.
1531              
1532             If the C<Archive::Tar> module is installed and either the C<uncompress> or
1533             C<gunzip> programs are available, you can use one of these workarounds to
1534             read C<.tar.Z> files.
1535              
1536             Firstly with C<uncompress>
1537              
1538             use strict;
1539             use warnings;
1540             use Archive::Tar;
1541              
1542             open F, "uncompress -c $filename |";
1543             my $tar = Archive::Tar->new(*F);
1544             ...
1545              
1546             and this with C<gunzip>
1547              
1548             use strict;
1549             use warnings;
1550             use Archive::Tar;
1551              
1552             open F, "gunzip -c $filename |";
1553             my $tar = Archive::Tar->new(*F);
1554             ...
1555              
1556             Similarly, if the C<compress> program is available, you can use this to
1557             write a C<.tar.Z> file
1558              
1559             use strict;
1560             use warnings;
1561             use Archive::Tar;
1562             use IO::File;
1563              
1564             my $fh = new IO::File "| compress -c >$filename";
1565             my $tar = Archive::Tar->new();
1566             ...
1567             $tar->write($fh);
1568             $fh->close ;
1569              
1570             =head2 Zlib Library Version Support
1571              
1572             By default C<Compress::Raw::Zlib> will build with a private copy of version
1573             1.2.5 of the zlib library. (See the F<README> file for details of
1574             how to override this behaviour)
1575              
1576             If you decide to use a different version of the zlib library, you need to be
1577             aware of the following issues
1578              
1579             =over 5
1580              
1581             =item *
1582              
1583             First off, you must have zlib 1.0.5 or better.
1584              
1585             =item *
1586              
1587             You need to have zlib 1.2.1 or better if you want to use the C<-Merge>
1588             option with C<IO::Compress::Gzip>, C<IO::Compress::Deflate> and
1589             C<IO::Compress::RawDeflate>.
1590              
1591             =back
1592              
1593             =head1 CONSTANTS
1594              
1595             All the I<zlib> constants are automatically imported when you make use
1596             of I<Compress::Raw::Zlib>.
1597              
1598             =head1 SUPPORT
1599              
1600             General feedback/questions/bug reports should be sent to
1601             L<https://github.com/pmqs/Compress-Raw-Zlib/issues> (preferred) or
1602             L<https://rt.cpan.org/Public/Dist/Display.html?Name=Compress-Raw-Zlib>.
1603              
1604             =head1 SEE ALSO
1605              
1606             L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzip>, L<IO::Uncompress::UnLzip>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Compress::Zstd>, L<IO::Uncompress::UnZstd>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
1607              
1608             L<IO::Compress::FAQ|IO::Compress::FAQ>
1609              
1610             L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1611             L<Archive::Tar|Archive::Tar>,
1612             L<IO::Zlib|IO::Zlib>
1613              
1614             For RFC 1950, 1951 and 1952 see
1615             L<https://datatracker.ietf.org/doc/html/rfc1950>,
1616             L<https://datatracker.ietf.org/doc/html/rfc1951> and
1617             L<https://datatracker.ietf.org/doc/html/rfc1952>
1618              
1619             The I<zlib> compression library was written by Jean-loup Gailly
1620             C<gzip@prep.ai.mit.edu> and Mark Adler C<madler@alumni.caltech.edu>.
1621              
1622             The primary site for the I<zlib> compression library is
1623             L<http://www.zlib.org>.
1624              
1625             The primary site for the I<zlib-ng> compression library is
1626             L<https://github.com/zlib-ng/zlib-ng>.
1627              
1628             The primary site for gzip is L<http://www.gzip.org>.
1629              
1630             =head1 AUTHOR
1631              
1632             This module was written by Paul Marquess, C<pmqs@cpan.org>.
1633              
1634             =head1 MODIFICATION HISTORY
1635              
1636             See the Changes file.
1637              
1638             =head1 COPYRIGHT AND LICENSE
1639              
1640             Copyright (c) 2005-2026 Paul Marquess. All rights reserved.
1641              
1642             This program is free software; you can redistribute it and/or
1643             modify it under the same terms as Perl itself.