2 package Compress::Raw::Zlib;
9 #use Parse::Parameters;
14 our ($VERSION, $XS_VERSION, @ISA, @EXPORT, $AUTOLOAD);
17 $XS_VERSION = $VERSION;
18 $VERSION = eval $VERSION;
21 # Items to export into callers namespace by default. Note: do not export
22 # names by default without a very good reason. Use EXPORT_OK instead.
23 # Do not simply export all your public functions/methods/constants.
70 ($constname = $AUTOLOAD) =~ s/.*:://;
71 my ($error, $val) = constant($constname);
72 Carp::croak $error if $error;
74 *{$AUTOLOAD} = sub { $val };
78 use constant FLAG_APPEND => 1 ;
79 use constant FLAG_CRC => 2 ;
80 use constant FLAG_ADLER => 4 ;
81 use constant FLAG_CONSUME_INPUT => 8 ;
85 XSLoader::load('Compress::Raw::Zlib', $XS_VERSION);
90 local @ISA = qw(DynaLoader);
91 bootstrap Compress::Raw::Zlib $XS_VERSION ;
95 use constant Parse_any => 0x01;
96 use constant Parse_unsigned => 0x02;
97 use constant Parse_signed => 0x04;
98 use constant Parse_boolean => 0x08;
99 use constant Parse_string => 0x10;
100 use constant Parse_custom => 0x12;
102 use constant Parse_store_ref => 0x100 ;
104 use constant OFF_PARSED => 0 ;
105 use constant OFF_TYPE => 1 ;
106 use constant OFF_DEFAULT => 2 ;
107 use constant OFF_FIXED => 3 ;
108 use constant OFF_FIRST_ONLY => 4 ;
109 use constant OFF_STICKY => 5 ;
115 my $level = shift || 0 ;
117 my $sub = (caller($level + 1))[3] ;
118 #local $Carp::CarpLevel = 1 ;
119 my $p = new Compress::Raw::Zlib::Parameters() ;
121 or croak "$sub: $p->{Error}" ;
127 sub Compress::Raw::Zlib::Parameters::new
131 my $obj = { Error => '',
135 #return bless $obj, ref($class) || $class || __PACKAGE__ ;
136 return bless $obj, 'Compress::Raw::Zlib::Parameters' ;
139 sub Compress::Raw::Zlib::Parameters::setError
143 my $retval = @_ ? shift : undef ;
145 $self->{Error} = $error ;
152 # return $self->{Error} ;
155 sub Compress::Raw::Zlib::Parameters::parse
159 my $default = shift ;
161 my $got = $self->{Got} ;
162 my $firstTime = keys %{ $got } == 0 ;
167 # Allow the options to be passed as a hash reference or
168 # as the complete hash.
174 return $self->setError("Expected even number of parameters, got 1")
175 if ! defined $href or ! ref $href or ref $href ne "HASH" ;
177 foreach my $key (keys %$href) {
178 push @entered, $key ;
179 push @entered, \$href->{$key} ;
184 return $self->setError("Expected even number of parameters, got $count")
187 for my $i (0.. $count / 2 - 1) {
188 push @entered, $_[2* $i] ;
189 push @entered, \$_[2* $i+1] ;
194 while (my ($key, $v) = each %$default)
196 croak "need 4 params [@$v]"
199 my ($first_only, $sticky, $type, $value) = @$v ;
201 $self->_checkType($key, \$value, $type, 0, \$x)
206 if ($firstTime || ! $sticky) {
207 $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
210 $got->{$key}[OFF_PARSED] = 0 ;
213 for my $i (0.. @entered / 2 - 1) {
214 my $key = $entered[2* $i] ;
215 my $value = $entered[2* $i+1] ;
217 #print "Key [$key] Value [$value]" ;
218 #print defined $$value ? "[$$value]\n" : "[undef]\n";
221 my $canonkey = lc $key;
223 if ($got->{$canonkey} && ($firstTime ||
224 ! $got->{$canonkey}[OFF_FIRST_ONLY] ))
226 my $type = $got->{$canonkey}[OFF_TYPE] ;
228 $self->_checkType($key, $value, $type, 1, \$s)
230 #$value = $$value unless $type & Parse_store_ref ;
232 $got->{$canonkey} = [1, $type, $value, $s] ;
235 { push (@Bad, $key) }
239 my ($bad) = join(", ", @Bad) ;
240 return $self->setError("unknown key value(s) @Bad") ;
246 sub Compress::Raw::Zlib::Parameters::_checkType
253 my $validate = shift ;
256 #local $Carp::CarpLevel = $level ;
257 #print "PARSE $type $key $value $validate $sub\n" ;
258 if ( $type & Parse_store_ref)
261 # if ref ${ $value } ;
269 if ($type & Parse_any)
274 elsif ($type & Parse_unsigned)
276 return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
277 if $validate && ! defined $value ;
278 return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
279 if $validate && $value !~ /^\d+$/;
281 $$output = defined $value ? $value : 0 ;
284 elsif ($type & Parse_signed)
286 return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
287 if $validate && ! defined $value ;
288 return $self->setError("Parameter '$key' must be a signed int, got '$value'")
289 if $validate && $value !~ /^-?\d+$/;
291 $$output = defined $value ? $value : 0 ;
294 elsif ($type & Parse_boolean)
296 return $self->setError("Parameter '$key' must be an int, got '$value'")
297 if $validate && defined $value && $value !~ /^\d*$/;
298 $$output = defined $value ? $value != 0 : 0 ;
301 elsif ($type & Parse_string)
303 $$output = defined $value ? $value : "" ;
313 sub Compress::Raw::Zlib::Parameters::parsed
318 return $self->{Got}{lc $name}[OFF_PARSED] ;
321 sub Compress::Raw::Zlib::Parameters::value
328 $self->{Got}{lc $name}[OFF_PARSED] = 1;
329 $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
330 $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ;
333 return $self->{Got}{lc $name}[OFF_FIXED] ;
336 sub Compress::Raw::Zlib::Deflate::new
339 my ($got) = ParseParameters(0,
341 'AppendOutput' => [1, 1, Parse_boolean, 0],
342 'CRC32' => [1, 1, Parse_boolean, 0],
343 'ADLER32' => [1, 1, Parse_boolean, 0],
344 'Bufsize' => [1, 1, Parse_unsigned, 4096],
346 'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()],
347 'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()],
348 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()],
349 'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()],
350 'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()],
351 'Dictionary' => [1, 1, Parse_any, ""],
355 croak "Compress::Raw::Zlib::Deflate::new: Bufsize must be >= 1, you specified " .
356 $got->value('Bufsize')
357 unless $got->value('Bufsize') >= 1;
360 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
361 $flags |= FLAG_CRC if $got->value('CRC32') ;
362 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
365 $got->value('Level'),
366 $got->value('Method'),
367 $got->value('WindowBits'),
368 $got->value('MemLevel'),
369 $got->value('Strategy'),
370 $got->value('Bufsize'),
371 $got->value('Dictionary')) ;
375 sub Compress::Raw::Zlib::Inflate::new
378 my ($got) = ParseParameters(0,
380 'AppendOutput' => [1, 1, Parse_boolean, 0],
381 'CRC32' => [1, 1, Parse_boolean, 0],
382 'ADLER32' => [1, 1, Parse_boolean, 0],
383 'ConsumeInput' => [1, 1, Parse_boolean, 1],
384 'Bufsize' => [1, 1, Parse_unsigned, 4096],
386 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()],
387 'Dictionary' => [1, 1, Parse_any, ""],
391 croak "Compress::Raw::Zlib::Inflate::new: Bufsize must be >= 1, you specified " .
392 $got->value('Bufsize')
393 unless $got->value('Bufsize') >= 1;
396 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
397 $flags |= FLAG_CRC if $got->value('CRC32') ;
398 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
399 $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
401 _inflateInit($flags, $got->value('WindowBits'), $got->value('Bufsize'),
402 $got->value('Dictionary')) ;
405 sub Compress::Raw::Zlib::InflateScan::new
408 my ($got) = ParseParameters(0,
410 'CRC32' => [1, 1, Parse_boolean, 0],
411 'ADLER32' => [1, 1, Parse_boolean, 0],
412 'Bufsize' => [1, 1, Parse_unsigned, 4096],
414 'WindowBits' => [1, 1, Parse_signed, -MAX_WBITS()],
415 'Dictionary' => [1, 1, Parse_any, ""],
419 croak "Compress::Raw::Zlib::InflateScan::new: Bufsize must be >= 1, you specified " .
420 $got->value('Bufsize')
421 unless $got->value('Bufsize') >= 1;
424 #$flags |= FLAG_APPEND if $got->value('AppendOutput') ;
425 $flags |= FLAG_CRC if $got->value('CRC32') ;
426 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
427 #$flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
429 _inflateScanInit($flags, $got->value('WindowBits'), $got->value('Bufsize'),
433 sub Compress::Raw::Zlib::inflateScanStream::createDeflateStream
436 my ($got) = ParseParameters(0,
438 'AppendOutput' => [1, 1, Parse_boolean, 0],
439 'CRC32' => [1, 1, Parse_boolean, 0],
440 'ADLER32' => [1, 1, Parse_boolean, 0],
441 'Bufsize' => [1, 1, Parse_unsigned, 4096],
443 'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()],
444 'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()],
445 'WindowBits' => [1, 1, Parse_signed, - MAX_WBITS()],
446 'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()],
447 'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()],
450 croak "Compress::Raw::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified " .
451 $got->value('Bufsize')
452 unless $got->value('Bufsize') >= 1;
455 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
456 $flags |= FLAG_CRC if $got->value('CRC32') ;
457 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
459 $pkg->_createDeflateStream($flags,
460 $got->value('Level'),
461 $got->value('Method'),
462 $got->value('WindowBits'),
463 $got->value('MemLevel'),
464 $got->value('Strategy'),
465 $got->value('Bufsize'),
470 sub Compress::Raw::Zlib::inflateScanStream::inflate
476 my $status = $self->scan(@_);
478 if ($status == Z_OK() && $_[2]) {
481 $status = $self->scan(\$byte, $_[1]) ;
487 sub Compress::Raw::Zlib::deflateStream::deflateParams
490 my ($got) = ParseParameters(0, {
491 'Level' => [1, 1, Parse_signed, undef],
492 'Strategy' => [1, 1, Parse_unsigned, undef],
493 'Bufsize' => [1, 1, Parse_unsigned, undef],
497 croak "Compress::Raw::Zlib::deflateParams needs Level and/or Strategy"
498 unless $got->parsed('Level') + $got->parsed('Strategy') +
499 $got->parsed('Bufsize');
501 croak "Compress::Raw::Zlib::Inflate::deflateParams: Bufsize must be >= 1, you specified " .
502 $got->value('Bufsize')
503 if $got->parsed('Bufsize') && $got->value('Bufsize') <= 1;
506 $flags |= 1 if $got->parsed('Level') ;
507 $flags |= 2 if $got->parsed('Strategy') ;
508 $flags |= 4 if $got->parsed('Bufsize') ;
510 $self->_deflateParams($flags, $got->value('Level'),
511 $got->value('Strategy'), $got->value('Bufsize'));
516 # Autoload methods go after __END__, and are processed by the autosplit program.
524 Compress::Raw::Zlib - Low-Level Interface to zlib compression library
528 use Compress::Raw::Zlib ;
530 ($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) ;
531 $status = $d->deflate($input, $output) ;
532 $status = $d->flush($output [, $flush_type]) ;
533 $d->deflateParams(OPTS) ;
534 $d->deflateTune(OPTS) ;
545 ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) ;
546 $status = $i->inflate($input, $output [, $eof]) ;
547 $status = $i->inflateSync($input) ;
556 $crc = adler32($buffer [,$crc]) ;
557 $crc = crc32($buffer [,$crc]) ;
559 $crc = adler32_combine($crc1, $crc2, $len2)l
560 $crc = crc32_combine($adler1, $adler2, $len2)
567 The I<Compress::Raw::Zlib> module provides a Perl interface to the I<zlib>
568 compression library (see L</AUTHOR> for details about where to get
571 =head1 Compress::Raw::Zlib::Deflate
573 This section defines an interface that allows in-memory compression using
574 the I<deflate> interface provided by zlib.
576 Here is a definition of the interface available:
578 =head2 B<($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) >
580 Initialises a deflation object.
582 If you are familiar with the I<zlib> library, it combines the
583 features of the I<zlib> functions C<deflateInit>, C<deflateInit2>
584 and C<deflateSetDictionary>.
586 If successful, it will return the initialised deflation object, C<$d>
587 and a C<$status> of C<Z_OK> in a list context. In scalar context it
588 returns the deflation object, C<$d>, only.
590 If not successful, the returned deflation object, C<$d>, will be
591 I<undef> and C<$status> will hold the a I<zlib> error code.
593 The function optionally takes a number of named options specified as
594 C<< Name => value >> pairs. This allows individual options to be
595 tailored without having to specify them all in the parameter list.
597 For backward compatibility, it is also possible to pass the parameters
598 as a reference to a hash containing the name=>value pairs.
600 Below is a list of the valid options:
606 Defines the compression level. Valid values are 0 through 9,
607 C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
608 C<Z_DEFAULT_COMPRESSION>.
610 The default is Z_DEFAULT_COMPRESSION.
614 Defines the compression method. The only valid value at present (and
615 the default) is Z_DEFLATED.
619 For a definition of the meaning and valid values for C<WindowBits>
620 refer to the I<zlib> documentation for I<deflateInit2>.
622 Defaults to MAX_WBITS.
626 For a definition of the meaning and valid values for C<MemLevel>
627 refer to the I<zlib> documentation for I<deflateInit2>.
629 Defaults to MAX_MEM_LEVEL.
633 Defines the strategy used to tune the compression. The valid values are
634 C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED>, C<Z_RLE>, C<Z_FIXED> and
637 The default is Z_DEFAULT_STRATEGY.
641 When a dictionary is specified I<Compress::Raw::Zlib> will automatically
642 call C<deflateSetDictionary> directly after calling C<deflateInit>. The
643 Adler32 value for the dictionary can be obtained by calling the method
644 C<$d-E<gt>dict_adler()>.
646 The default is no dictionary.
650 Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
651 and C<$d-E<gt>flush> methods. If the buffer has to be
652 reallocated to increase the size, it will grow in increments of
655 The default buffer size is 4096.
657 =item B<-AppendOutput>
659 This option controls how data is written to the output buffer by the
660 C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
662 If the C<AppendOutput> option is set to false, the output buffers in the
663 C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods will be truncated before
664 uncompressed data is written to them.
666 If the option is set to true, uncompressed data will be appended to the
667 output buffer in the C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
669 This option defaults to false.
673 If set to true, a crc32 checksum of the uncompressed data will be
674 calculated. Use the C<$d-E<gt>crc32> method to retrieve this value.
676 This option defaults to false.
680 If set to true, an adler32 checksum of the uncompressed data will be
681 calculated. Use the C<$d-E<gt>adler32> method to retrieve this value.
683 This option defaults to false.
687 Here is an example of using the C<Compress::Raw::Zlib::Deflate> optional
688 parameter list to override the default buffer size and compression
689 level. All other options will take their default values.
691 my $d = new Compress::Raw::Zlib::Deflate ( -Bufsize => 300,
692 -Level => Z_BEST_SPEED ) ;
694 =head2 B<$status = $d-E<gt>deflate($input, $output)>
696 Deflates the contents of C<$input> and writes the compressed data to
699 The C<$input> and C<$output> parameters can be either scalars or scalar
702 When finished, C<$input> will be completely processed (assuming there
703 were no errors). If the deflation was successful it writes the deflated
704 data to C<$output> and returns a status value of C<Z_OK>.
706 On error, it returns a I<zlib> error code.
708 If the C<AppendOutput> option is set to true in the constructor for
709 the C<$d> object, the compressed data will be appended to C<$output>. If
710 it is false, C<$output> will be truncated before any compressed data is
713 B<Note>: This method will not necessarily write compressed data to
714 C<$output> every time it is called. So do not assume that there has been
715 an error if the contents of C<$output> is empty on returning from
716 this method. As long as the return code from the method is C<Z_OK>,
717 the deflate has succeeded.
719 =head2 B<$status = $d-E<gt>flush($output [, $flush_type]) >
721 Typically used to finish the deflation. Any pending output will be
722 written to C<$output>.
724 Returns C<Z_OK> if successful.
726 Note that flushing can seriously degrade the compression ratio, so it
727 should only be used to terminate a decompression (using C<Z_FINISH>) or
728 when you want to create a I<full flush point> (using C<Z_FULL_FLUSH>).
730 By default the C<flush_type> used is C<Z_FINISH>. Other valid values
731 for C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH>
732 and C<Z_FULL_FLUSH>. It is strongly recommended that you only set the
733 C<flush_type> parameter if you fully understand the implications of
734 what it does. See the C<zlib> documentation for details.
736 If the C<AppendOutput> option is set to true in the constructor for
737 the C<$d> object, the compressed data will be appended to C<$output>. If
738 it is false, C<$output> will be truncated before any compressed data is
741 =head2 B<$status = $d-E<gt>deflateParams([OPT])>
743 Change settings for the deflate object C<$d>.
745 The list of the valid options is shown below. Options not specified
746 will remain unchanged.
752 Defines the compression level. Valid values are 0 through 9,
753 C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
754 C<Z_DEFAULT_COMPRESSION>.
758 Defines the strategy used to tune the compression. The valid values are
759 C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
763 Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
764 and C<$d-E<gt>flush> methods. If the buffer has to be
765 reallocated to increase the size, it will grow in increments of
770 =head2 B<$status = $d-E<gt>deflateTune($good_length, $max_lazy, $nice_length, $max_chain)>
772 Tune the internal settings for the deflate object C<$d>. This option is
773 only available if you are running zlib 1.2.2.3 or better.
775 Refer to the documentation in zlib.h for instructions on how to fly
778 =head2 B<$d-E<gt>dict_adler()>
780 Returns the adler32 value for the dictionary.
782 =head2 B<$d-E<gt>crc32()>
784 Returns the crc32 value for the uncompressed data to date.
786 If the C<CRC32> option is not enabled in the constructor for this object,
787 this method will always return 0;
789 =head2 B<$d-E<gt>adler32()>
791 Returns the adler32 value for the uncompressed data to date.
793 =head2 B<$d-E<gt>msg()>
795 Returns the last error message generated by zlib.
797 =head2 B<$d-E<gt>total_in()>
799 Returns the total number of bytes uncompressed bytes input to deflate.
801 =head2 B<$d-E<gt>total_out()>
803 Returns the total number of compressed bytes output from deflate.
805 =head2 B<$d-E<gt>get_Strategy()>
807 Returns the deflation strategy currently used. Valid values are
808 C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
810 =head2 B<$d-E<gt>get_Level()>
812 Returns the compression level being used.
814 =head2 B<$d-E<gt>get_BufSize()>
816 Returns the buffer size used to carry out the compression.
820 Here is a trivial example of using C<deflate>. It simply reads standard
821 input, deflates it and writes it to standard output.
826 use Compress::Raw::Zlib ;
830 my $x = new Compress::Raw::Zlib::Deflate
831 or die "Cannot create a deflation stream\n" ;
833 my ($output, $status) ;
836 $status = $x->deflate($_, $output) ;
839 or die "deflation failed\n" ;
844 $status = $x->flush($output) ;
847 or die "deflation failed\n" ;
851 =head1 Compress::Raw::Zlib::Inflate
853 This section defines an interface that allows in-memory uncompression using
854 the I<inflate> interface provided by zlib.
856 Here is a definition of the interface:
858 =head2 B< ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) >
860 Initialises an inflation object.
862 In a list context it returns the inflation object, C<$i>, and the
863 I<zlib> status code (C<$status>). In a scalar context it returns the
864 inflation object only.
866 If successful, C<$i> will hold the inflation object and C<$status> will
869 If not successful, C<$i> will be I<undef> and C<$status> will hold the
872 The function optionally takes a number of named options specified as
873 C<< -Name => value >> pairs. This allows individual options to be
874 tailored without having to specify them all in the parameter list.
876 For backward compatibility, it is also possible to pass the parameters
877 as a reference to a hash containing the C<< name=>value >> pairs.
879 Here is a list of the valid options:
885 To uncompress an RFC 1950 data stream, set C<WindowBits> to a positive
888 To uncompress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>.
890 For a full definition of the meaning and valid values for C<WindowBits>
891 refer to the I<zlib> documentation for I<inflateInit2>.
893 Defaults to MAX_WBITS.
897 Sets the initial size for the output buffer used by the C<$i-E<gt>inflate>
898 method. If the output buffer in this method has to be reallocated to
899 increase the size, it will grow in increments of C<Bufsize>.
905 The default is no dictionary.
907 =item B<-AppendOutput>
909 This option controls how data is written to the output buffer by the
910 C<$i-E<gt>inflate> method.
912 If the option is set to false, the output buffer in the C<$i-E<gt>inflate>
913 method will be truncated before uncompressed data is written to it.
915 If the option is set to true, uncompressed data will be appended to the
916 output buffer by the C<$i-E<gt>inflate> method.
918 This option defaults to false.
922 If set to true, a crc32 checksum of the uncompressed data will be
923 calculated. Use the C<$i-E<gt>crc32> method to retrieve this value.
925 This option defaults to false.
929 If set to true, an adler32 checksum of the uncompressed data will be
930 calculated. Use the C<$i-E<gt>adler32> method to retrieve this value.
932 This option defaults to false.
934 =item B<-ConsumeInput>
936 If set to true, this option will remove compressed data from the input
937 buffer of the the C< $i-E<gt>inflate > method as the inflate progresses.
939 This option can be useful when you are processing compressed data that is
940 embedded in another file/buffer. In this case the data that immediately
941 follows the compressed stream will be left in the input buffer.
943 This option defaults to true.
947 Here is an example of using an optional parameter to override the default
950 my ($i, $status) = new Compress::Raw::Zlib::Inflate( -Bufsize => 300 ) ;
952 =head2 B< $status = $i-E<gt>inflate($input, $output [,$eof]) >
954 Inflates the complete contents of C<$input> and writes the uncompressed
955 data to C<$output>. The C<$input> and C<$output> parameters can either be
956 scalars or scalar references.
958 Returns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the
959 compressed data has been successfully reached.
961 If not successful C<$status> will hold the I<zlib> error code.
963 If the C<ConsumeInput> option has been set to true when the
964 C<Compress::Raw::Zlib::Inflate> object is created, the C<$input> parameter
965 is modified by C<inflate>. On completion it will contain what remains
966 of the input buffer after inflation. In practice, this means that when
967 the return status is C<Z_OK> the C<$input> parameter will contain an
968 empty string, and when the return status is C<Z_STREAM_END> the C<$input>
969 parameter will contains what (if anything) was stored in the input buffer
970 after the deflated data stream.
972 This feature is useful when processing a file format that encapsulates
973 a compressed data stream (e.g. gzip, zip) and there is useful data
974 immediately after the deflation stream.
976 If the C<AppendOutput> option is set to true in the constructor for
977 this object, the uncompressed data will be appended to C<$output>. If
978 it is false, C<$output> will be truncated before any uncompressed data
981 The C<$eof> parameter needs a bit of explanation.
983 Prior to version 1.2.0, zlib assumed that there was at least one trailing
984 byte immediately after the compressed data stream when it was carrying out
985 decompression. This normally isn't a problem because the majority of zlib
986 applications guarantee that there will be data directly after the
987 compressed data stream. For example, both gzip (RFC 1950) and zip both
988 define trailing data that follows the compressed data stream.
990 The C<$eof> parameter only needs to be used if B<all> of the following
997 You are either using a copy of zlib that is older than version 1.2.0 or you
998 want your application code to be able to run with as many different
999 versions of zlib as possible.
1003 You have set the C<WindowBits> parameter to C<-MAX_WBITS> in the constructor
1004 for this object, i.e. you are uncompressing a raw deflated data stream
1009 There is no data immediately after the compressed data stream.
1013 If B<all> of these are the case, then you need to set the C<$eof> parameter
1014 to true on the final call (and only the final call) to C<$i-E<gt>inflate>.
1016 If you have built this module with zlib >= 1.2.0, the C<$eof> parameter is
1017 ignored. You can still set it if you want, but it won't be used behind the
1020 =head2 B<$status = $i-E<gt>inflateSync($input)>
1022 This method can be used to attempt to recover good data from a compressed
1023 data stream that is partially corrupt.
1024 It scans C<$input> until it reaches either a I<full flush point> or the
1027 If a I<full flush point> is found, C<Z_OK> is returned and C<$input>
1028 will be have all data up to the flush point removed. This data can then be
1029 passed to the C<$i-E<gt>inflate> method to be uncompressed.
1031 Any other return code means that a flush point was not found. If more
1032 data is available, C<inflateSync> can be called repeatedly with more
1033 compressed data until the flush point is found.
1035 Note I<full flush points> are not present by default in compressed
1036 data streams. They must have been added explicitly when the data stream
1037 was created by calling C<Compress::Deflate::flush> with C<Z_FULL_FLUSH>.
1039 =head2 B<$i-E<gt>dict_adler()>
1041 Returns the adler32 value for the dictionary.
1043 =head2 B<$i-E<gt>crc32()>
1045 Returns the crc32 value for the uncompressed data to date.
1047 If the C<CRC32> option is not enabled in the constructor for this object,
1048 this method will always return 0;
1050 =head2 B<$i-E<gt>adler32()>
1052 Returns the adler32 value for the uncompressed data to date.
1054 If the C<ADLER32> option is not enabled in the constructor for this object,
1055 this method will always return 0;
1057 =head2 B<$i-E<gt>msg()>
1059 Returns the last error message generated by zlib.
1061 =head2 B<$i-E<gt>total_in()>
1063 Returns the total number of bytes compressed bytes input to inflate.
1065 =head2 B<$i-E<gt>total_out()>
1067 Returns the total number of uncompressed bytes output from inflate.
1069 =head2 B<$d-E<gt>get_BufSize()>
1071 Returns the buffer size used to carry out the decompression.
1075 Here is an example of using C<inflate>.
1080 use Compress::Raw::Zlib;
1082 my $x = new Compress::Raw::Zlib::Inflate()
1083 or die "Cannot create a inflation stream\n" ;
1089 my ($output, $status) ;
1090 while (read(STDIN, $input, 4096))
1092 $status = $x->inflate(\$input, $output) ;
1095 if $status == Z_OK or $status == Z_STREAM_END ;
1097 last if $status != Z_OK ;
1100 die "inflation failed\n"
1101 unless $status == Z_STREAM_END ;
1103 =head1 CHECKSUM FUNCTIONS
1105 Two functions are provided by I<zlib> to calculate checksums. For the
1106 Perl interface, the order of the two parameters in both functions has
1107 been reversed. This allows both running checksums and one off
1108 calculations to be done.
1110 $crc = adler32($buffer [,$crc]) ;
1111 $crc = crc32($buffer [,$crc]) ;
1113 The buffer parameters can either be a scalar or a scalar reference.
1115 If the $crc parameters is C<undef>, the crc value will be reset.
1117 If you have built this module with zlib 1.2.3 or better, two more
1118 CRC-related functions are available.
1120 $crc = adler32_combine($crc1, $crc2, $len2)l
1121 $crc = crc32_combine($adler1, $adler2, $len2)
1123 These functions allow checksums to be merged.
1125 =head1 ACCESSING ZIP FILES
1127 Although it is possible (with some effort on your part) to use this
1128 module to access .zip files, there is a module on CPAN that will do all
1129 the hard work for you. Check out the C<Archive::Zip> module on CPAN at
1131 http://www.cpan.org/modules/by-module/Archive/Archive-Zip-*.tar.gz
1135 All the I<zlib> constants are automatically imported when you make use
1136 of I<Compress::Raw::Zlib>.
1140 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::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
1142 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1144 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1145 L<Archive::Tar|Archive::Tar>,
1146 L<IO::Zlib|IO::Zlib>
1148 For RFC 1950, 1951 and 1952 see
1149 F<http://www.faqs.org/rfcs/rfc1950.html>,
1150 F<http://www.faqs.org/rfcs/rfc1951.html> and
1151 F<http://www.faqs.org/rfcs/rfc1952.html>
1153 The I<zlib> compression library was written by Jean-loup Gailly
1154 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1156 The primary site for the I<zlib> compression library is
1157 F<http://www.zlib.org>.
1159 The primary site for gzip is F<http://www.gzip.org>.
1163 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1165 =head1 MODIFICATION HISTORY
1167 See the Changes file.
1169 =head1 COPYRIGHT AND LICENSE
1171 Copyright (c) 2005-2008 Paul Marquess. All rights reserved.
1173 This program is free software; you can redistribute it and/or
1174 modify it under the same terms as Perl itself.