2 package Compress::Raw::Zlib;
11 our ($VERSION, $XS_VERSION, @ISA, @EXPORT, %EXPORT_TAGS, @EXPORT_OK, $AUTOLOAD, %DEFLATE_CONSTANTS, @DEFLATE_CONSTANTS);
14 $XS_VERSION = $VERSION;
15 $VERSION = eval $VERSION;
18 %EXPORT_TAGS = ( flush => [qw{
52 %DEFLATE_CONSTANTS = %EXPORT_TAGS;
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.
103 push @EXPORT, qw(crc32 adler32 DEF_WBITS);
105 use constant WANT_GZIP => 16;
106 use constant WANT_GZIP_OR_ZLIB => 32;
110 ($constname = $AUTOLOAD) =~ s/.*:://;
111 my ($error, $val) = constant($constname);
112 Carp::croak $error if $error;
114 *{$AUTOLOAD} = sub { $val };
118 use constant FLAG_APPEND => 1 ;
119 use constant FLAG_CRC => 2 ;
120 use constant FLAG_ADLER => 4 ;
121 use constant FLAG_CONSUME_INPUT => 8 ;
122 use constant FLAG_LIMIT_OUTPUT => 16 ;
126 XSLoader::load('Compress::Raw::Zlib', $XS_VERSION);
131 local @ISA = qw(DynaLoader);
132 bootstrap Compress::Raw::Zlib $XS_VERSION ;
136 use constant Parse_any => 0x01;
137 use constant Parse_unsigned => 0x02;
138 use constant Parse_signed => 0x04;
139 use constant Parse_boolean => 0x08;
140 #use constant Parse_string => 0x10;
141 #use constant Parse_custom => 0x12;
143 #use constant Parse_store_ref => 0x100 ;
145 use constant OFF_PARSED => 0 ;
146 use constant OFF_TYPE => 1 ;
147 use constant OFF_DEFAULT => 2 ;
148 use constant OFF_FIXED => 3 ;
149 use constant OFF_FIRST_ONLY => 4 ;
150 use constant OFF_STICKY => 5 ;
156 my $level = shift || 0 ;
158 my $sub = (caller($level + 1))[3] ;
159 #local $Carp::CarpLevel = 1 ;
160 my $p = new Compress::Raw::Zlib::Parameters() ;
162 or croak "$sub: $p->{Error}" ;
168 sub Compress::Raw::Zlib::Parameters::new
172 my $obj = { Error => '',
176 #return bless $obj, ref($class) || $class || __PACKAGE__ ;
177 return bless $obj, 'Compress::Raw::Zlib::Parameters' ;
180 sub Compress::Raw::Zlib::Parameters::setError
184 my $retval = @_ ? shift : undef ;
186 $self->{Error} = $error ;
193 # return $self->{Error} ;
196 sub Compress::Raw::Zlib::Parameters::parse
200 my $default = shift ;
202 my $got = $self->{Got} ;
203 my $firstTime = keys %{ $got } == 0 ;
208 # Allow the options to be passed as a hash reference or
209 # as the complete hash.
215 return $self->setError("Expected even number of parameters, got 1")
216 if ! defined $href or ! ref $href or ref $href ne "HASH" ;
218 foreach my $key (keys %$href) {
219 push @entered, $key ;
220 push @entered, \$href->{$key} ;
225 return $self->setError("Expected even number of parameters, got $count")
228 for my $i (0.. $count / 2 - 1) {
229 push @entered, $_[2* $i] ;
230 push @entered, \$_[2* $i+1] ;
235 while (my ($key, $v) = each %$default)
237 croak "need 4 params [@$v]"
240 my ($first_only, $sticky, $type, $value) = @$v ;
242 $self->_checkType($key, \$value, $type, 0, \$x)
247 if ($firstTime || ! $sticky) {
248 $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
251 $got->{$key}[OFF_PARSED] = 0 ;
254 for my $i (0.. @entered / 2 - 1) {
255 my $key = $entered[2* $i] ;
256 my $value = $entered[2* $i+1] ;
258 #print "Key [$key] Value [$value]" ;
259 #print defined $$value ? "[$$value]\n" : "[undef]\n";
262 my $canonkey = lc $key;
264 if ($got->{$canonkey} && ($firstTime ||
265 ! $got->{$canonkey}[OFF_FIRST_ONLY] ))
267 my $type = $got->{$canonkey}[OFF_TYPE] ;
269 $self->_checkType($key, $value, $type, 1, \$s)
271 #$value = $$value unless $type & Parse_store_ref ;
273 $got->{$canonkey} = [1, $type, $value, $s] ;
276 { push (@Bad, $key) }
280 my ($bad) = join(", ", @Bad) ;
281 return $self->setError("unknown key value(s) @Bad") ;
287 sub Compress::Raw::Zlib::Parameters::_checkType
294 my $validate = shift ;
297 #local $Carp::CarpLevel = $level ;
298 #print "PARSE $type $key $value $validate $sub\n" ;
299 # if ( $type & Parse_store_ref)
302 # # if ref ${ $value } ;
304 # $$output = $value ;
310 if ($type & Parse_any)
315 elsif ($type & Parse_unsigned)
317 return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
318 if $validate && ! defined $value ;
319 return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
320 if $validate && $value !~ /^\d+$/;
322 $$output = defined $value ? $value : 0 ;
325 elsif ($type & Parse_signed)
327 return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
328 if $validate && ! defined $value ;
329 return $self->setError("Parameter '$key' must be a signed int, got '$value'")
330 if $validate && $value !~ /^-?\d+$/;
332 $$output = defined $value ? $value : 0 ;
335 elsif ($type & Parse_boolean)
337 return $self->setError("Parameter '$key' must be an int, got '$value'")
338 if $validate && defined $value && $value !~ /^\d*$/;
339 $$output = defined $value ? $value != 0 : 0 ;
342 # elsif ($type & Parse_string)
344 # $$output = defined $value ? $value : "" ;
354 sub Compress::Raw::Zlib::Parameters::parsed
359 return $self->{Got}{lc $name}[OFF_PARSED] ;
362 sub Compress::Raw::Zlib::Parameters::value
369 $self->{Got}{lc $name}[OFF_PARSED] = 1;
370 $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
371 $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ;
374 return $self->{Got}{lc $name}[OFF_FIXED] ;
377 our $OPTIONS_deflate =
379 'AppendOutput' => [1, 1, Parse_boolean, 0],
380 'CRC32' => [1, 1, Parse_boolean, 0],
381 'ADLER32' => [1, 1, Parse_boolean, 0],
382 'Bufsize' => [1, 1, Parse_unsigned, 4096],
384 'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()],
385 'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()],
386 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()],
387 'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()],
388 'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()],
389 'Dictionary' => [1, 1, Parse_any, ""],
392 sub Compress::Raw::Zlib::Deflate::new
395 my ($got) = ParseParameters(0, $OPTIONS_deflate, @_);
397 croak "Compress::Raw::Zlib::Deflate::new: Bufsize must be >= 1, you specified " .
398 $got->value('Bufsize')
399 unless $got->value('Bufsize') >= 1;
402 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
403 $flags |= FLAG_CRC if $got->value('CRC32') ;
404 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
406 my $windowBits = $got->value('WindowBits');
407 $windowBits += MAX_WBITS()
408 if ($windowBits & MAX_WBITS()) == 0 ;
411 $got->value('Level'),
412 $got->value('Method'),
414 $got->value('MemLevel'),
415 $got->value('Strategy'),
416 $got->value('Bufsize'),
417 $got->value('Dictionary')) ;
421 sub Compress::Raw::Zlib::deflateStream::STORABLE_freeze
423 my $type = ref shift;
424 croak "Cannot freeze $type object\n";
427 sub Compress::Raw::Zlib::deflateStream::STORABLE_thaw
429 my $type = ref shift;
430 croak "Cannot thaw $type object\n";
434 our $OPTIONS_inflate =
436 'AppendOutput' => [1, 1, Parse_boolean, 0],
437 'LimitOutput' => [1, 1, Parse_boolean, 0],
438 'CRC32' => [1, 1, Parse_boolean, 0],
439 'ADLER32' => [1, 1, Parse_boolean, 0],
440 'ConsumeInput' => [1, 1, Parse_boolean, 1],
441 'Bufsize' => [1, 1, Parse_unsigned, 4096],
443 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()],
444 'Dictionary' => [1, 1, Parse_any, ""],
447 sub Compress::Raw::Zlib::Inflate::new
450 my ($got) = ParseParameters(0, $OPTIONS_inflate, @_);
452 croak "Compress::Raw::Zlib::Inflate::new: Bufsize must be >= 1, you specified " .
453 $got->value('Bufsize')
454 unless $got->value('Bufsize') >= 1;
457 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
458 $flags |= FLAG_CRC if $got->value('CRC32') ;
459 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
460 $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
461 $flags |= FLAG_LIMIT_OUTPUT if $got->value('LimitOutput') ;
464 my $windowBits = $got->value('WindowBits');
465 $windowBits += MAX_WBITS()
466 if ($windowBits & MAX_WBITS()) == 0 ;
468 _inflateInit($flags, $windowBits, $got->value('Bufsize'),
469 $got->value('Dictionary')) ;
472 sub Compress::Raw::Zlib::inflateStream::STORABLE_freeze
474 my $type = ref shift;
475 croak "Cannot freeze $type object\n";
478 sub Compress::Raw::Zlib::inflateStream::STORABLE_thaw
480 my $type = ref shift;
481 croak "Cannot thaw $type object\n";
484 sub Compress::Raw::Zlib::InflateScan::new
487 my ($got) = ParseParameters(0,
489 'CRC32' => [1, 1, Parse_boolean, 0],
490 'ADLER32' => [1, 1, Parse_boolean, 0],
491 'Bufsize' => [1, 1, Parse_unsigned, 4096],
493 'WindowBits' => [1, 1, Parse_signed, -MAX_WBITS()],
494 'Dictionary' => [1, 1, Parse_any, ""],
498 croak "Compress::Raw::Zlib::InflateScan::new: Bufsize must be >= 1, you specified " .
499 $got->value('Bufsize')
500 unless $got->value('Bufsize') >= 1;
503 #$flags |= FLAG_APPEND if $got->value('AppendOutput') ;
504 $flags |= FLAG_CRC if $got->value('CRC32') ;
505 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
506 #$flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
508 _inflateScanInit($flags, $got->value('WindowBits'), $got->value('Bufsize'),
512 sub Compress::Raw::Zlib::inflateScanStream::createDeflateStream
515 my ($got) = ParseParameters(0,
517 'AppendOutput' => [1, 1, Parse_boolean, 0],
518 'CRC32' => [1, 1, Parse_boolean, 0],
519 'ADLER32' => [1, 1, Parse_boolean, 0],
520 'Bufsize' => [1, 1, Parse_unsigned, 4096],
522 'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()],
523 'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()],
524 'WindowBits' => [1, 1, Parse_signed, - MAX_WBITS()],
525 'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()],
526 'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()],
529 croak "Compress::Raw::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified " .
530 $got->value('Bufsize')
531 unless $got->value('Bufsize') >= 1;
534 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
535 $flags |= FLAG_CRC if $got->value('CRC32') ;
536 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
538 $pkg->_createDeflateStream($flags,
539 $got->value('Level'),
540 $got->value('Method'),
541 $got->value('WindowBits'),
542 $got->value('MemLevel'),
543 $got->value('Strategy'),
544 $got->value('Bufsize'),
549 sub Compress::Raw::Zlib::inflateScanStream::inflate
555 my $status = $self->scan(@_);
557 if ($status == Z_OK() && $_[2]) {
560 $status = $self->scan(\$byte, $_[1]) ;
566 sub Compress::Raw::Zlib::deflateStream::deflateParams
569 my ($got) = ParseParameters(0, {
570 'Level' => [1, 1, Parse_signed, undef],
571 'Strategy' => [1, 1, Parse_unsigned, undef],
572 'Bufsize' => [1, 1, Parse_unsigned, undef],
576 croak "Compress::Raw::Zlib::deflateParams needs Level and/or Strategy"
577 unless $got->parsed('Level') + $got->parsed('Strategy') +
578 $got->parsed('Bufsize');
580 croak "Compress::Raw::Zlib::Inflate::deflateParams: Bufsize must be >= 1, you specified " .
581 $got->value('Bufsize')
582 if $got->parsed('Bufsize') && $got->value('Bufsize') <= 1;
585 $flags |= 1 if $got->parsed('Level') ;
586 $flags |= 2 if $got->parsed('Strategy') ;
587 $flags |= 4 if $got->parsed('Bufsize') ;
589 $self->_deflateParams($flags, $got->value('Level'),
590 $got->value('Strategy'), $got->value('Bufsize'));
601 Compress::Raw::Zlib - Low-Level Interface to zlib compression library
605 use Compress::Raw::Zlib ;
607 ($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) ;
608 $status = $d->deflate($input, $output) ;
609 $status = $d->flush($output [, $flush_type]) ;
611 $d->deflateParams(OPTS) ;
612 $d->deflateTune(OPTS) ;
623 ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) ;
624 $status = $i->inflate($input, $output [, $eof]) ;
625 $status = $i->inflateSync($input) ;
635 $crc = adler32($buffer [,$crc]) ;
636 $crc = crc32($buffer [,$crc]) ;
638 $crc = crc32_combine($crc1, $crc2, $len2);
639 $adler = adler32_combine($adler1, $adler2, $len2);
641 my $version = Compress::Raw::Zlib::zlib_version();
642 my $flags = Compress::Raw::Zlib::zlibCompileFlags();
646 The I<Compress::Raw::Zlib> module provides a Perl interface to the I<zlib>
647 compression library (see L</AUTHOR> for details about where to get
650 =head1 Compress::Raw::Zlib::Deflate
652 This section defines an interface that allows in-memory compression using
653 the I<deflate> interface provided by zlib.
655 Here is a definition of the interface available:
657 =head2 B<($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) >
659 Initialises a deflation object.
661 If you are familiar with the I<zlib> library, it combines the
662 features of the I<zlib> functions C<deflateInit>, C<deflateInit2>
663 and C<deflateSetDictionary>.
665 If successful, it will return the initialised deflation object, C<$d>
666 and a C<$status> of C<Z_OK> in a list context. In scalar context it
667 returns the deflation object, C<$d>, only.
669 If not successful, the returned deflation object, C<$d>, will be
670 I<undef> and C<$status> will hold the a I<zlib> error code.
672 The function optionally takes a number of named options specified as
673 C<< Name => value >> pairs. This allows individual options to be
674 tailored without having to specify them all in the parameter list.
676 For backward compatibility, it is also possible to pass the parameters
677 as a reference to a hash containing the name=>value pairs.
679 Below is a list of the valid options:
685 Defines the compression level. Valid values are 0 through 9,
686 C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
687 C<Z_DEFAULT_COMPRESSION>.
689 The default is C<Z_DEFAULT_COMPRESSION>.
693 Defines the compression method. The only valid value at present (and
694 the default) is C<Z_DEFLATED>.
698 To compress an RFC 1950 data stream, set C<WindowBits> to a positive
699 number between 8 and 15.
701 To compress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>.
703 To compress an RFC 1952 data stream (i.e. gzip), set C<WindowBits> to
706 For a definition of the meaning and valid values for C<WindowBits>
707 refer to the I<zlib> documentation for I<deflateInit2>.
709 Defaults to C<MAX_WBITS>.
713 For a definition of the meaning and valid values for C<MemLevel>
714 refer to the I<zlib> documentation for I<deflateInit2>.
716 Defaults to MAX_MEM_LEVEL.
720 Defines the strategy used to tune the compression. The valid values are
721 C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED>, C<Z_RLE>, C<Z_FIXED> and
724 The default is C<Z_DEFAULT_STRATEGY>.
728 When a dictionary is specified I<Compress::Raw::Zlib> will automatically
729 call C<deflateSetDictionary> directly after calling C<deflateInit>. The
730 Adler32 value for the dictionary can be obtained by calling the method
731 C<$d-E<gt>dict_adler()>.
733 The default is no dictionary.
737 Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
738 and C<$d-E<gt>flush> methods. If the buffer has to be
739 reallocated to increase the size, it will grow in increments of
742 The default buffer size is 4096.
744 =item B<-AppendOutput>
746 This option controls how data is written to the output buffer by the
747 C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
749 If the C<AppendOutput> option is set to false, the output buffers in the
750 C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods will be truncated before
751 uncompressed data is written to them.
753 If the option is set to true, uncompressed data will be appended to the
754 output buffer in the C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
756 This option defaults to false.
760 If set to true, a crc32 checksum of the uncompressed data will be
761 calculated. Use the C<$d-E<gt>crc32> method to retrieve this value.
763 This option defaults to false.
767 If set to true, an adler32 checksum of the uncompressed data will be
768 calculated. Use the C<$d-E<gt>adler32> method to retrieve this value.
770 This option defaults to false.
774 Here is an example of using the C<Compress::Raw::Zlib::Deflate> optional
775 parameter list to override the default buffer size and compression
776 level. All other options will take their default values.
778 my $d = new Compress::Raw::Zlib::Deflate ( -Bufsize => 300,
779 -Level => Z_BEST_SPEED ) ;
781 =head2 B<$status = $d-E<gt>deflate($input, $output)>
783 Deflates the contents of C<$input> and writes the compressed data to
786 The C<$input> and C<$output> parameters can be either scalars or scalar
789 When finished, C<$input> will be completely processed (assuming there
790 were no errors). If the deflation was successful it writes the deflated
791 data to C<$output> and returns a status value of C<Z_OK>.
793 On error, it returns a I<zlib> error code.
795 If the C<AppendOutput> option is set to true in the constructor for
796 the C<$d> object, the compressed data will be appended to C<$output>. If
797 it is false, C<$output> will be truncated before any compressed data is
800 B<Note>: This method will not necessarily write compressed data to
801 C<$output> every time it is called. So do not assume that there has been
802 an error if the contents of C<$output> is empty on returning from
803 this method. As long as the return code from the method is C<Z_OK>,
804 the deflate has succeeded.
806 =head2 B<$status = $d-E<gt>flush($output [, $flush_type]) >
808 Typically used to finish the deflation. Any pending output will be
809 written to C<$output>.
811 Returns C<Z_OK> if successful.
813 Note that flushing can seriously degrade the compression ratio, so it
814 should only be used to terminate a decompression (using C<Z_FINISH>) or
815 when you want to create a I<full flush point> (using C<Z_FULL_FLUSH>).
817 By default the C<flush_type> used is C<Z_FINISH>. Other valid values
818 for C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH>
819 and C<Z_FULL_FLUSH>. It is strongly recommended that you only set the
820 C<flush_type> parameter if you fully understand the implications of
821 what it does. See the C<zlib> documentation for details.
823 If the C<AppendOutput> option is set to true in the constructor for
824 the C<$d> object, the compressed data will be appended to C<$output>. If
825 it is false, C<$output> will be truncated before any compressed data is
828 =head2 B<$status = $d-E<gt>deflateReset() >
830 This method will reset the deflation object C<$d>. It can be used when you
831 are compressing multiple data streams and want to use the same object to
832 compress each of them. It should only be used once the previous data stream
833 has been flushed successfully, i.e. a call to C<< $d->flush(Z_FINISH) >> has
836 Returns C<Z_OK> if successful.
838 =head2 B<$status = $d-E<gt>deflateParams([OPT])>
840 Change settings for the deflate object C<$d>.
842 The list of the valid options is shown below. Options not specified
843 will remain unchanged.
849 Defines the compression level. Valid values are 0 through 9,
850 C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
851 C<Z_DEFAULT_COMPRESSION>.
855 Defines the strategy used to tune the compression. The valid values are
856 C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
860 Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
861 and C<$d-E<gt>flush> methods. If the buffer has to be
862 reallocated to increase the size, it will grow in increments of
867 =head2 B<$status = $d-E<gt>deflateTune($good_length, $max_lazy, $nice_length, $max_chain)>
869 Tune the internal settings for the deflate object C<$d>. This option is
870 only available if you are running zlib 1.2.2.3 or better.
872 Refer to the documentation in zlib.h for instructions on how to fly
875 =head2 B<$d-E<gt>dict_adler()>
877 Returns the adler32 value for the dictionary.
879 =head2 B<$d-E<gt>crc32()>
881 Returns the crc32 value for the uncompressed data to date.
883 If the C<CRC32> option is not enabled in the constructor for this object,
884 this method will always return 0;
886 =head2 B<$d-E<gt>adler32()>
888 Returns the adler32 value for the uncompressed data to date.
890 =head2 B<$d-E<gt>msg()>
892 Returns the last error message generated by zlib.
894 =head2 B<$d-E<gt>total_in()>
896 Returns the total number of bytes uncompressed bytes input to deflate.
898 =head2 B<$d-E<gt>total_out()>
900 Returns the total number of compressed bytes output from deflate.
902 =head2 B<$d-E<gt>get_Strategy()>
904 Returns the deflation strategy currently used. Valid values are
905 C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
907 =head2 B<$d-E<gt>get_Level()>
909 Returns the compression level being used.
911 =head2 B<$d-E<gt>get_BufSize()>
913 Returns the buffer size used to carry out the compression.
917 Here is a trivial example of using C<deflate>. It simply reads standard
918 input, deflates it and writes it to standard output.
923 use Compress::Raw::Zlib ;
927 my $x = new Compress::Raw::Zlib::Deflate
928 or die "Cannot create a deflation stream\n" ;
930 my ($output, $status) ;
933 $status = $x->deflate($_, $output) ;
936 or die "deflation failed\n" ;
941 $status = $x->flush($output) ;
944 or die "deflation failed\n" ;
948 =head1 Compress::Raw::Zlib::Inflate
950 This section defines an interface that allows in-memory uncompression using
951 the I<inflate> interface provided by zlib.
953 Here is a definition of the interface:
955 =head2 B< ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) >
957 Initialises an inflation object.
959 In a list context it returns the inflation object, C<$i>, and the
960 I<zlib> status code (C<$status>). In a scalar context it returns the
961 inflation object only.
963 If successful, C<$i> will hold the inflation object and C<$status> will
966 If not successful, C<$i> will be I<undef> and C<$status> will hold the
969 The function optionally takes a number of named options specified as
970 C<< -Name => value >> pairs. This allows individual options to be
971 tailored without having to specify them all in the parameter list.
973 For backward compatibility, it is also possible to pass the parameters
974 as a reference to a hash containing the C<< name=>value >> pairs.
976 Here is a list of the valid options:
982 To uncompress an RFC 1950 data stream, set C<WindowBits> to a positive
983 number between 8 and 15.
985 To uncompress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>.
987 To uncompress an RFC 1952 data stream (i.e. gzip), set C<WindowBits> to
990 To auto-detect and uncompress an RFC 1950 or RFC 1952 data stream (i.e.
991 gzip), set C<WindowBits> to C<WANT_GZIP_OR_ZLIB>.
993 For a full definition of the meaning and valid values for C<WindowBits>
994 refer to the I<zlib> documentation for I<inflateInit2>.
996 Defaults to C<MAX_WBITS>.
1000 Sets the initial size for the output buffer used by the C<$i-E<gt>inflate>
1001 method. If the output buffer in this method has to be reallocated to
1002 increase the size, it will grow in increments of C<Bufsize>.
1006 =item B<-Dictionary>
1008 The default is no dictionary.
1010 =item B<-AppendOutput>
1012 This option controls how data is written to the output buffer by the
1013 C<$i-E<gt>inflate> method.
1015 If the option is set to false, the output buffer in the C<$i-E<gt>inflate>
1016 method will be truncated before uncompressed data is written to it.
1018 If the option is set to true, uncompressed data will be appended to the
1019 output buffer by the C<$i-E<gt>inflate> method.
1021 This option defaults to false.
1025 If set to true, a crc32 checksum of the uncompressed data will be
1026 calculated. Use the C<$i-E<gt>crc32> method to retrieve this value.
1028 This option defaults to false.
1032 If set to true, an adler32 checksum of the uncompressed data will be
1033 calculated. Use the C<$i-E<gt>adler32> method to retrieve this value.
1035 This option defaults to false.
1037 =item B<-ConsumeInput>
1039 If set to true, this option will remove compressed data from the input
1040 buffer of the C<< $i->inflate >> method as the inflate progresses.
1042 This option can be useful when you are processing compressed data that is
1043 embedded in another file/buffer. In this case the data that immediately
1044 follows the compressed stream will be left in the input buffer.
1046 This option defaults to true.
1048 =item B<-LimitOutput>
1050 The C<LimitOutput> option changes the behavior of the C<< $i->inflate >>
1051 method so that the amount of memory used by the output buffer can be
1054 When C<LimitOutput> is used the size of the output buffer used will either
1055 be the value of the C<Bufsize> option or the amount of memory already
1056 allocated to C<$output>, whichever is larger. Predicting the output size
1057 available is tricky, so don't rely on getting an exact output buffer size.
1059 When C<LimitOutout> is not specified C<< $i->inflate >> will use as much
1060 memory as it takes to write all the uncompressed data it creates by
1061 uncompressing the input buffer.
1063 If C<LimitOutput> is enabled, the C<ConsumeInput> option will also be
1066 This option defaults to false.
1068 See L</The LimitOutput option> for a discussion on why C<LimitOutput> is
1069 needed and how to use it.
1073 Here is an example of using an optional parameter to override the default
1076 my ($i, $status) = new Compress::Raw::Zlib::Inflate( -Bufsize => 300 ) ;
1078 =head2 B< $status = $i-E<gt>inflate($input, $output [,$eof]) >
1080 Inflates the complete contents of C<$input> and writes the uncompressed
1081 data to C<$output>. The C<$input> and C<$output> parameters can either be
1082 scalars or scalar references.
1084 Returns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the
1085 compressed data has been successfully reached.
1087 If not successful C<$status> will hold the I<zlib> error code.
1089 If the C<ConsumeInput> option has been set to true when the
1090 C<Compress::Raw::Zlib::Inflate> object is created, the C<$input> parameter
1091 is modified by C<inflate>. On completion it will contain what remains
1092 of the input buffer after inflation. In practice, this means that when
1093 the return status is C<Z_OK> the C<$input> parameter will contain an
1094 empty string, and when the return status is C<Z_STREAM_END> the C<$input>
1095 parameter will contains what (if anything) was stored in the input buffer
1096 after the deflated data stream.
1098 This feature is useful when processing a file format that encapsulates
1099 a compressed data stream (e.g. gzip, zip) and there is useful data
1100 immediately after the deflation stream.
1102 If the C<AppendOutput> option is set to true in the constructor for
1103 this object, the uncompressed data will be appended to C<$output>. If
1104 it is false, C<$output> will be truncated before any uncompressed data
1107 The C<$eof> parameter needs a bit of explanation.
1109 Prior to version 1.2.0, zlib assumed that there was at least one trailing
1110 byte immediately after the compressed data stream when it was carrying out
1111 decompression. This normally isn't a problem because the majority of zlib
1112 applications guarantee that there will be data directly after the
1113 compressed data stream. For example, both gzip (RFC 1950) and zip both
1114 define trailing data that follows the compressed data stream.
1116 The C<$eof> parameter only needs to be used if B<all> of the following
1123 You are either using a copy of zlib that is older than version 1.2.0 or you
1124 want your application code to be able to run with as many different
1125 versions of zlib as possible.
1129 You have set the C<WindowBits> parameter to C<-MAX_WBITS> in the constructor
1130 for this object, i.e. you are uncompressing a raw deflated data stream
1135 There is no data immediately after the compressed data stream.
1139 If B<all> of these are the case, then you need to set the C<$eof> parameter
1140 to true on the final call (and only the final call) to C<$i-E<gt>inflate>.
1142 If you have built this module with zlib >= 1.2.0, the C<$eof> parameter is
1143 ignored. You can still set it if you want, but it won't be used behind the
1146 =head2 B<$status = $i-E<gt>inflateSync($input)>
1148 This method can be used to attempt to recover good data from a compressed
1149 data stream that is partially corrupt.
1150 It scans C<$input> until it reaches either a I<full flush point> or the
1153 If a I<full flush point> is found, C<Z_OK> is returned and C<$input>
1154 will be have all data up to the flush point removed. This data can then be
1155 passed to the C<$i-E<gt>inflate> method to be uncompressed.
1157 Any other return code means that a flush point was not found. If more
1158 data is available, C<inflateSync> can be called repeatedly with more
1159 compressed data until the flush point is found.
1161 Note I<full flush points> are not present by default in compressed
1162 data streams. They must have been added explicitly when the data stream
1163 was created by calling C<Compress::Deflate::flush> with C<Z_FULL_FLUSH>.
1165 =head2 B<$status = $i-E<gt>inflateReset() >
1167 This method will reset the inflation object C<$i>. It can be used when you
1168 are uncompressing multiple data streams and want to use the same object to
1169 uncompress each of them.
1171 Returns C<Z_OK> if successful.
1173 =head2 B<$i-E<gt>dict_adler()>
1175 Returns the adler32 value for the dictionary.
1177 =head2 B<$i-E<gt>crc32()>
1179 Returns the crc32 value for the uncompressed data to date.
1181 If the C<CRC32> option is not enabled in the constructor for this object,
1182 this method will always return 0;
1184 =head2 B<$i-E<gt>adler32()>
1186 Returns the adler32 value for the uncompressed data to date.
1188 If the C<ADLER32> option is not enabled in the constructor for this object,
1189 this method will always return 0;
1191 =head2 B<$i-E<gt>msg()>
1193 Returns the last error message generated by zlib.
1195 =head2 B<$i-E<gt>total_in()>
1197 Returns the total number of bytes compressed bytes input to inflate.
1199 =head2 B<$i-E<gt>total_out()>
1201 Returns the total number of uncompressed bytes output from inflate.
1203 =head2 B<$d-E<gt>get_BufSize()>
1205 Returns the buffer size used to carry out the decompression.
1209 Here is an example of using C<inflate>.
1214 use Compress::Raw::Zlib;
1216 my $x = new Compress::Raw::Zlib::Inflate()
1217 or die "Cannot create a inflation stream\n" ;
1223 my ($output, $status) ;
1224 while (read(STDIN, $input, 4096))
1226 $status = $x->inflate($input, $output) ;
1230 last if $status != Z_OK ;
1233 die "inflation failed\n"
1234 unless $status == Z_STREAM_END ;
1236 The next example show how to use the C<LimitOutput> option. Notice the use
1237 of two nested loops in this case. The outer loop reads the data from the
1238 input source - STDIN and the inner loop repeatedly calls C<inflate> until
1239 C<$input> is exhausted, we get an error, or the end of the stream is
1240 reached. One point worth remembering is by using the C<LimitOutput> option
1241 you also get C<ConsumeInput> set as well - this makes the code below much
1247 use Compress::Raw::Zlib;
1249 my $x = new Compress::Raw::Zlib::Inflate(LimitOutput => 1)
1250 or die "Cannot create a inflation stream\n" ;
1256 my ($output, $status) ;
1259 while (read(STDIN, $input, 4096))
1263 $status = $x->inflate($input, $output) ;
1268 unless $status == Z_OK || $status == Z_BUF_ERROR ;
1270 while ($status == Z_OK && length $input);
1273 die "inflation failed\n"
1274 unless $status == Z_STREAM_END ;
1276 =head1 CHECKSUM FUNCTIONS
1278 Two functions are provided by I<zlib> to calculate checksums. For the
1279 Perl interface, the order of the two parameters in both functions has
1280 been reversed. This allows both running checksums and one off
1281 calculations to be done.
1283 $crc = adler32($buffer [,$crc]) ;
1284 $crc = crc32($buffer [,$crc]) ;
1286 The buffer parameters can either be a scalar or a scalar reference.
1288 If the $crc parameters is C<undef>, the crc value will be reset.
1290 If you have built this module with zlib 1.2.3 or better, two more
1291 CRC-related functions are available.
1293 $crc = crc32_combine($crc1, $crc2, $len2);
1294 $adler = adler32_combine($adler1, $adler2, $len2);
1296 These functions allow checksums to be merged.
1297 Refer to the I<zlib> documentation for more details.
1301 =head2 my $version = Compress::Raw::Zlib::zlib_version();
1303 Returns the version of the zlib library.
1305 =head2 my $flags = Compress::Raw::Zlib::zlibCompileFlags();
1307 Returns the flags indicating compile-time options that were used to build
1308 the zlib library. See the zlib documentation for a description of the flags
1309 returned by C<zlibCompileFlags>.
1311 Note that when the zlib sources are built along with this module the
1312 C<sprintf> flags (bits 24, 25 and 26) should be ignored.
1314 If you are using zlib 1.2.0 or older, C<zlibCompileFlags> will return 0.
1316 =head1 The LimitOutput option.
1318 By default C<< $i->inflate($input, $output) >> will uncompress I<all> data
1319 in C<$input> and write I<all> of the uncompressed data it has generated to
1320 C<$output>. This makes the interface to C<inflate> much simpler - if the
1321 method has uncompressed C<$input> successfully I<all> compressed data in
1322 C<$input> will have been dealt with. So if you are reading from an input
1323 source and uncompressing as you go the code will look something like this
1328 use Compress::Raw::Zlib;
1330 my $x = new Compress::Raw::Zlib::Inflate()
1331 or die "Cannot create a inflation stream\n" ;
1335 my ($output, $status) ;
1336 while (read(STDIN, $input, 4096))
1338 $status = $x->inflate($input, $output) ;
1342 last if $status != Z_OK ;
1345 die "inflation failed\n"
1346 unless $status == Z_STREAM_END ;
1348 The points to note are
1354 The main processing loop in the code handles reading of compressed data
1359 The status code returned from C<inflate> will only trigger termination of
1360 the main processing loop if it isn't C<Z_OK>. When C<LimitOutput> has not
1361 been used the C<Z_OK> status means that the end of the compressed
1362 data stream has been reached or there has been an error in uncompression.
1366 After the call to C<inflate> I<all> of the uncompressed data in C<$input>
1367 will have been processed. This means the subsequent call to C<read> can
1368 overwrite it's contents without any problem.
1372 For most use-cases the behavior described above is acceptable (this module
1373 and it's predecessor, C<Compress::Zlib>, have used it for over 10 years
1374 without an issue), but in a few very specific use-cases the amount of
1375 memory required for C<$output> can prohibitively large. For example, if the
1376 compressed data stream contains the same pattern repeated thousands of
1377 times, a relatively small compressed data stream can uncompress into
1378 hundreds of megabytes. Remember C<inflate> will keep allocating memory
1379 until I<all> the uncompressed data has been written to the output buffer -
1380 the size of C<$output> is unbounded.
1382 The C<LimitOutput> option is designed to help with this use-case.
1384 The main difference in your code when using C<LimitOutput> is having to
1385 deal with cases where the C<$input> parameter still contains some
1386 uncompressed data that C<inflate> hasn't processed yet. The status code
1387 returned from C<inflate> will be C<Z_OK> if uncompression took place and
1388 C<Z_BUF_ERROR> if the output buffer is full.
1390 Below is typical code that shows how to use C<LimitOutput>.
1395 use Compress::Raw::Zlib;
1397 my $x = new Compress::Raw::Zlib::Inflate(LimitOutput => 1)
1398 or die "Cannot create a inflation stream\n" ;
1404 my ($output, $status) ;
1407 while (read(STDIN, $input, 4096))
1411 $status = $x->inflate($input, $output) ;
1416 unless $status == Z_OK || $status == Z_BUF_ERROR ;
1418 while ($status == Z_OK && length $input);
1421 die "inflation failed\n"
1422 unless $status == Z_STREAM_END ;
1424 Points to note this time:
1430 There are now two nested loops in the code: the outer loop for reading the
1431 compressed data from STDIN, as before; and the inner loop to carry out the
1436 There are two exit points from the inner uncompression loop.
1438 Firstly when C<inflate> has returned a status other than C<Z_OK> or
1439 C<Z_BUF_ERROR>. This means that either the end of the compressed data
1440 stream has been reached (C<Z_STREAM_END>) or there is an error in the
1441 compressed data. In either of these cases there is no point in continuing
1442 with reading the compressed data, so both loops are terminated.
1444 The second exit point tests if there is any data left in the input buffer,
1445 C<$input> - remember that the C<ConsumeInput> option is automatically
1446 enabled when C<LimitOutput> is used. When the input buffer has been
1447 exhausted, the outer loop can run again and overwrite a now empty
1452 =head1 ACCESSING ZIP FILES
1454 Although it is possible (with some effort on your part) to use this module
1455 to access .zip files, there are other perl modules available that will do
1456 all the hard work for you. Check out C<Archive::Zip>,
1457 C<Archive::Zip::SimpleZip>, C<IO::Compress::Zip> and
1458 C<IO::Uncompress::Unzip>.
1462 =head2 Compatibility with Unix compress/uncompress.
1464 This module is not compatible with Unix C<compress>.
1466 If you have the C<uncompress> program available, you can use this to read
1469 open F, "uncompress -c $filename |";
1474 Alternatively, if you have the C<gunzip> program available, you can use
1475 this to read compressed files
1477 open F, "gunzip -c $filename |";
1482 and this to write compress files, if you have the C<compress> program
1485 open F, "| compress -c $filename ";
1490 =head2 Accessing .tar.Z files
1492 See previous FAQ item.
1494 If the C<Archive::Tar> module is installed and either the C<uncompress> or
1495 C<gunzip> programs are available, you can use one of these workarounds to
1496 read C<.tar.Z> files.
1498 Firstly with C<uncompress>
1504 open F, "uncompress -c $filename |";
1505 my $tar = Archive::Tar->new(*F);
1508 and this with C<gunzip>
1514 open F, "gunzip -c $filename |";
1515 my $tar = Archive::Tar->new(*F);
1518 Similarly, if the C<compress> program is available, you can use this to
1519 write a C<.tar.Z> file
1526 my $fh = new IO::File "| compress -c >$filename";
1527 my $tar = Archive::Tar->new();
1532 =head2 Zlib Library Version Support
1534 By default C<Compress::Raw::Zlib> will build with a private copy of version
1535 1.2.5 of the zlib library. (See the F<README> file for details of
1536 how to override this behaviour)
1538 If you decide to use a different version of the zlib library, you need to be
1539 aware of the following issues
1545 First off, you must have zlib 1.0.5 or better.
1549 You need to have zlib 1.2.1 or better if you want to use the C<-Merge>
1550 option with C<IO::Compress::Gzip>, C<IO::Compress::Deflate> and
1551 C<IO::Compress::RawDeflate>.
1557 All the I<zlib> constants are automatically imported when you make use
1558 of I<Compress::Raw::Zlib>.
1562 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::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
1564 L<IO::Compress::FAQ|IO::Compress::FAQ>
1566 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1567 L<Archive::Tar|Archive::Tar>,
1568 L<IO::Zlib|IO::Zlib>
1570 For RFC 1950, 1951 and 1952 see
1571 F<http://www.faqs.org/rfcs/rfc1950.html>,
1572 F<http://www.faqs.org/rfcs/rfc1951.html> and
1573 F<http://www.faqs.org/rfcs/rfc1952.html>
1575 The I<zlib> compression library was written by Jean-loup Gailly
1576 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1578 The primary site for the I<zlib> compression library is
1579 F<http://www.zlib.org>.
1581 The primary site for gzip is F<http://www.gzip.org>.
1585 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1587 =head1 MODIFICATION HISTORY
1589 See the Changes file.
1591 =head1 COPYRIGHT AND LICENSE
1593 Copyright (c) 2005-2014 Paul Marquess. All rights reserved.
1595 This program is free software; you can redistribute it and/or
1596 modify it under the same terms as Perl itself.