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