home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / perl / 5.10.0 / Compress / Raw / Zlib.pm
Encoding:
Perl POD Document  |  2009-06-26  |  33.9 KB  |  1,199 lines

  1.  
  2. package Compress::Raw::Zlib;
  3.  
  4. require 5.004 ;
  5. require Exporter;
  6. use AutoLoader;
  7. use Carp ;
  8.  
  9. #use Parse::Parameters;
  10.  
  11. use strict ;
  12. use warnings ;
  13. use bytes ;
  14. our ($VERSION, $XS_VERSION, @ISA, @EXPORT, $AUTOLOAD);
  15.  
  16. $VERSION = '2.008';
  17. $XS_VERSION = $VERSION; 
  18. $VERSION = eval $VERSION;
  19.  
  20. @ISA = qw(Exporter);
  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.
  24. @EXPORT = qw(
  25.         adler32 crc32
  26.  
  27.         ZLIB_VERSION
  28.         ZLIB_VERNUM
  29.  
  30.         DEF_WBITS
  31.         OS_CODE
  32.  
  33.         MAX_MEM_LEVEL
  34.         MAX_WBITS
  35.  
  36.         Z_ASCII
  37.         Z_BEST_COMPRESSION
  38.         Z_BEST_SPEED
  39.         Z_BINARY
  40.         Z_BLOCK
  41.         Z_BUF_ERROR
  42.         Z_DATA_ERROR
  43.         Z_DEFAULT_COMPRESSION
  44.         Z_DEFAULT_STRATEGY
  45.         Z_DEFLATED
  46.         Z_ERRNO
  47.         Z_FILTERED
  48.         Z_FIXED
  49.         Z_FINISH
  50.         Z_FULL_FLUSH
  51.         Z_HUFFMAN_ONLY
  52.         Z_MEM_ERROR
  53.         Z_NEED_DICT
  54.         Z_NO_COMPRESSION
  55.         Z_NO_FLUSH
  56.         Z_NULL
  57.         Z_OK
  58.         Z_PARTIAL_FLUSH
  59.         Z_RLE
  60.         Z_STREAM_END
  61.         Z_STREAM_ERROR
  62.         Z_SYNC_FLUSH
  63.         Z_UNKNOWN
  64.         Z_VERSION_ERROR
  65. );
  66.  
  67.  
  68. sub AUTOLOAD {
  69.     my($constname);
  70.     ($constname = $AUTOLOAD) =~ s/.*:://;
  71.     my ($error, $val) = constant($constname);
  72.     Carp::croak $error if $error;
  73.     no strict 'refs';
  74.     *{$AUTOLOAD} = sub { $val };
  75.     goto &{$AUTOLOAD};
  76. }
  77.  
  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 ;
  82.  
  83. eval {
  84.     require XSLoader;
  85.     XSLoader::load('Compress::Raw::Zlib', $XS_VERSION);
  86.     1;
  87. or do {
  88.     require DynaLoader;
  89.     local @ISA = qw(DynaLoader);
  90.     bootstrap Compress::Raw::Zlib $XS_VERSION ; 
  91. };
  92.  
  93.  
  94. use constant Parse_any      => 0x01;
  95. use constant Parse_unsigned => 0x02;
  96. use constant Parse_signed   => 0x04;
  97. use constant Parse_boolean  => 0x08;
  98. use constant Parse_string   => 0x10;
  99. use constant Parse_custom   => 0x12;
  100.  
  101. use constant Parse_store_ref => 0x100 ;
  102.  
  103. use constant OFF_PARSED     => 0 ;
  104. use constant OFF_TYPE       => 1 ;
  105. use constant OFF_DEFAULT    => 2 ;
  106. use constant OFF_FIXED      => 3 ;
  107. use constant OFF_FIRST_ONLY => 4 ;
  108. use constant OFF_STICKY     => 5 ;
  109.  
  110.  
  111.  
  112. sub ParseParameters
  113. {
  114.     my $level = shift || 0 ; 
  115.  
  116.     my $sub = (caller($level + 1))[3] ;
  117.     #local $Carp::CarpLevel = 1 ;
  118.     my $p = new Compress::Raw::Zlib::Parameters() ;
  119.     $p->parse(@_)
  120.         or croak "$sub: $p->{Error}" ;
  121.  
  122.     return $p;
  123. }
  124.  
  125.  
  126. sub Compress::Raw::Zlib::Parameters::new
  127. {
  128.     my $class = shift ;
  129.  
  130.     my $obj = { Error => '',
  131.                 Got   => {},
  132.               } ;
  133.  
  134.     #return bless $obj, ref($class) || $class || __PACKAGE__ ;
  135.     return bless $obj, 'Compress::Raw::Zlib::Parameters' ;
  136. }
  137.  
  138. sub Compress::Raw::Zlib::Parameters::setError
  139. {
  140.     my $self = shift ;
  141.     my $error = shift ;
  142.     my $retval = @_ ? shift : undef ;
  143.  
  144.     $self->{Error} = $error ;
  145.     return $retval;
  146. }
  147.           
  148. #sub getError
  149. #{
  150. #    my $self = shift ;
  151. #    return $self->{Error} ;
  152. #}
  153.           
  154. sub Compress::Raw::Zlib::Parameters::parse
  155. {
  156.     my $self = shift ;
  157.  
  158.     my $default = shift ;
  159.  
  160.     my $got = $self->{Got} ;
  161.     my $firstTime = keys %{ $got } == 0 ;
  162.  
  163.     my (@Bad) ;
  164.     my @entered = () ;
  165.  
  166.     # Allow the options to be passed as a hash reference or
  167.     # as the complete hash.
  168.     if (@_ == 0) {
  169.         @entered = () ;
  170.     }
  171.     elsif (@_ == 1) {
  172.         my $href = $_[0] ;    
  173.         return $self->setError("Expected even number of parameters, got 1")
  174.             if ! defined $href or ! ref $href or ref $href ne "HASH" ;
  175.  
  176.         foreach my $key (keys %$href) {
  177.             push @entered, $key ;
  178.             push @entered, \$href->{$key} ;
  179.         }
  180.     }
  181.     else {
  182.         my $count = @_;
  183.         return $self->setError("Expected even number of parameters, got $count")
  184.             if $count % 2 != 0 ;
  185.         
  186.         for my $i (0.. $count / 2 - 1) {
  187.             push @entered, $_[2* $i] ;
  188.             push @entered, \$_[2* $i+1] ;
  189.         }
  190.     }
  191.  
  192.  
  193.     while (my ($key, $v) = each %$default)
  194.     {
  195.         croak "need 4 params [@$v]"
  196.             if @$v != 4 ;
  197.  
  198.         my ($first_only, $sticky, $type, $value) = @$v ;
  199.         my $x ;
  200.         $self->_checkType($key, \$value, $type, 0, \$x) 
  201.             or return undef ;
  202.  
  203.         $key = lc $key;
  204.  
  205.         if ($firstTime || ! $sticky) {
  206.             $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ;
  207.         }
  208.  
  209.         $got->{$key}[OFF_PARSED] = 0 ;
  210.     }
  211.  
  212.     for my $i (0.. @entered / 2 - 1) {
  213.         my $key = $entered[2* $i] ;
  214.         my $value = $entered[2* $i+1] ;
  215.  
  216.         #print "Key [$key] Value [$value]" ;
  217.         #print defined $$value ? "[$$value]\n" : "[undef]\n";
  218.  
  219.         $key =~ s/^-// ;
  220.         my $canonkey = lc $key;
  221.  
  222.         if ($got->{$canonkey} && ($firstTime ||
  223.                                   ! $got->{$canonkey}[OFF_FIRST_ONLY]  ))
  224.         {
  225.             my $type = $got->{$canonkey}[OFF_TYPE] ;
  226.             my $s ;
  227.             $self->_checkType($key, $value, $type, 1, \$s)
  228.                 or return undef ;
  229.             #$value = $$value unless $type & Parse_store_ref ;
  230.             $value = $$value ;
  231.             $got->{$canonkey} = [1, $type, $value, $s] ;
  232.         }
  233.         else
  234.           { push (@Bad, $key) }
  235.     }
  236.  
  237.     if (@Bad) {
  238.         my ($bad) = join(", ", @Bad) ;
  239.         return $self->setError("unknown key value(s) @Bad") ;
  240.     }
  241.  
  242.     return 1;
  243. }
  244.  
  245. sub Compress::Raw::Zlib::Parameters::_checkType
  246. {
  247.     my $self = shift ;
  248.  
  249.     my $key   = shift ;
  250.     my $value = shift ;
  251.     my $type  = shift ;
  252.     my $validate  = shift ;
  253.     my $output  = shift;
  254.  
  255.     #local $Carp::CarpLevel = $level ;
  256.     #print "PARSE $type $key $value $validate $sub\n" ;
  257.     if ( $type & Parse_store_ref)
  258.     {
  259.         #$value = $$value
  260.         #    if ref ${ $value } ;
  261.  
  262.         $$output = $value ;
  263.         return 1;
  264.     }
  265.  
  266.     $value = $$value ;
  267.  
  268.     if ($type & Parse_any)
  269.     {
  270.         $$output = $value ;
  271.         return 1;
  272.     }
  273.     elsif ($type & Parse_unsigned)
  274.     {
  275.         return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'")
  276.             if $validate && ! defined $value ;
  277.         return $self->setError("Parameter '$key' must be an unsigned int, got '$value'")
  278.             if $validate && $value !~ /^\d+$/;
  279.  
  280.         $$output = defined $value ? $value : 0 ;    
  281.         return 1;
  282.     }
  283.     elsif ($type & Parse_signed)
  284.     {
  285.         return $self->setError("Parameter '$key' must be a signed int, got 'undef'")
  286.             if $validate && ! defined $value ;
  287.         return $self->setError("Parameter '$key' must be a signed int, got '$value'")
  288.             if $validate && $value !~ /^-?\d+$/;
  289.  
  290.         $$output = defined $value ? $value : 0 ;    
  291.         return 1 ;
  292.     }
  293.     elsif ($type & Parse_boolean)
  294.     {
  295.         return $self->setError("Parameter '$key' must be an int, got '$value'")
  296.             if $validate && defined $value && $value !~ /^\d*$/;
  297.         $$output =  defined $value ? $value != 0 : 0 ;    
  298.         return 1;
  299.     }
  300.     elsif ($type & Parse_string)
  301.     {
  302.         $$output = defined $value ? $value : "" ;    
  303.         return 1;
  304.     }
  305.  
  306.     $$output = $value ;
  307.     return 1;
  308. }
  309.  
  310.  
  311.  
  312. sub Compress::Raw::Zlib::Parameters::parsed
  313. {
  314.     my $self = shift ;
  315.     my $name = shift ;
  316.  
  317.     return $self->{Got}{lc $name}[OFF_PARSED] ;
  318. }
  319.  
  320. sub Compress::Raw::Zlib::Parameters::value
  321. {
  322.     my $self = shift ;
  323.     my $name = shift ;
  324.  
  325.     if (@_)
  326.     {
  327.         $self->{Got}{lc $name}[OFF_PARSED]  = 1;
  328.         $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ;
  329.         $self->{Got}{lc $name}[OFF_FIXED]   = $_[0] ;
  330.     }
  331.  
  332.     return $self->{Got}{lc $name}[OFF_FIXED] ;
  333. }
  334.  
  335. sub Compress::Raw::Zlib::Deflate::new
  336. {
  337.     my $pkg = shift ;
  338.     my ($got) = ParseParameters(0,
  339.             {
  340.                 'AppendOutput'  => [1, 1, Parse_boolean,  0],
  341.                 'CRC32'         => [1, 1, Parse_boolean,  0],
  342.                 'ADLER32'       => [1, 1, Parse_boolean,  0],
  343.                 'Bufsize'       => [1, 1, Parse_unsigned, 4096],
  344.  
  345.                 'Level'         => [1, 1, Parse_signed,   Z_DEFAULT_COMPRESSION()],
  346.                 'Method'        => [1, 1, Parse_unsigned, Z_DEFLATED()],
  347.                 'WindowBits'    => [1, 1, Parse_signed,   MAX_WBITS()],
  348.                 'MemLevel'      => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()],
  349.                 'Strategy'      => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()],
  350.                 'Dictionary'    => [1, 1, Parse_any,      ""],
  351.             }, @_) ;
  352.  
  353.  
  354.     croak "Compress::Raw::Zlib::Deflate::new: Bufsize must be >= 1, you specified " . 
  355.             $got->value('Bufsize')
  356.         unless $got->value('Bufsize') >= 1;
  357.  
  358.     my $flags = 0 ;
  359.     $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
  360.     $flags |= FLAG_CRC    if $got->value('CRC32') ;
  361.     $flags |= FLAG_ADLER  if $got->value('ADLER32') ;
  362.  
  363.     _deflateInit($flags,
  364.                 $got->value('Level'), 
  365.                 $got->value('Method'), 
  366.                 $got->value('WindowBits'), 
  367.                 $got->value('MemLevel'), 
  368.                 $got->value('Strategy'), 
  369.                 $got->value('Bufsize'),
  370.                 $got->value('Dictionary')) ;
  371.  
  372. }
  373.  
  374. sub Compress::Raw::Zlib::Inflate::new
  375. {
  376.     my $pkg = shift ;
  377.     my ($got) = ParseParameters(0,
  378.                     {
  379.                         'AppendOutput'  => [1, 1, Parse_boolean,  0],
  380.                         'CRC32'         => [1, 1, Parse_boolean,  0],
  381.                         'ADLER32'       => [1, 1, Parse_boolean,  0],
  382.                         'ConsumeInput'  => [1, 1, Parse_boolean,  1],
  383.                         'Bufsize'       => [1, 1, Parse_unsigned, 4096],
  384.                  
  385.                         'WindowBits'    => [1, 1, Parse_signed,   MAX_WBITS()],
  386.                         'Dictionary'    => [1, 1, Parse_any,      ""],
  387.             }, @_) ;
  388.  
  389.  
  390.     croak "Compress::Raw::Zlib::Inflate::new: Bufsize must be >= 1, you specified " . 
  391.             $got->value('Bufsize')
  392.         unless $got->value('Bufsize') >= 1;
  393.  
  394.     my $flags = 0 ;
  395.     $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
  396.     $flags |= FLAG_CRC    if $got->value('CRC32') ;
  397.     $flags |= FLAG_ADLER  if $got->value('ADLER32') ;
  398.     $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
  399.  
  400.     _inflateInit($flags, $got->value('WindowBits'), $got->value('Bufsize'), 
  401.                  $got->value('Dictionary')) ;
  402. }
  403.  
  404. sub Compress::Raw::Zlib::InflateScan::new
  405. {
  406.     my $pkg = shift ;
  407.     my ($got) = ParseParameters(0,
  408.                     {
  409.                         'CRC32'         => [1, 1, Parse_boolean,  0],
  410.                         'ADLER32'       => [1, 1, Parse_boolean,  0],
  411.                         'Bufsize'       => [1, 1, Parse_unsigned, 4096],
  412.                  
  413.                         'WindowBits'    => [1, 1, Parse_signed,   -MAX_WBITS()],
  414.                         'Dictionary'    => [1, 1, Parse_any,      ""],
  415.             }, @_) ;
  416.  
  417.  
  418.     croak "Compress::Raw::Zlib::InflateScan::new: Bufsize must be >= 1, you specified " . 
  419.             $got->value('Bufsize')
  420.         unless $got->value('Bufsize') >= 1;
  421.  
  422.     my $flags = 0 ;
  423.     #$flags |= FLAG_APPEND if $got->value('AppendOutput') ;
  424.     $flags |= FLAG_CRC    if $got->value('CRC32') ;
  425.     $flags |= FLAG_ADLER  if $got->value('ADLER32') ;
  426.     #$flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
  427.  
  428.     _inflateScanInit($flags, $got->value('WindowBits'), $got->value('Bufsize'), 
  429.                  '') ;
  430. }
  431.  
  432. sub Compress::Raw::Zlib::inflateScanStream::createDeflateStream
  433. {
  434.     my $pkg = shift ;
  435.     my ($got) = ParseParameters(0,
  436.             {
  437.                 'AppendOutput'  => [1, 1, Parse_boolean,  0],
  438.                 'CRC32'         => [1, 1, Parse_boolean,  0],
  439.                 'ADLER32'       => [1, 1, Parse_boolean,  0],
  440.                 'Bufsize'       => [1, 1, Parse_unsigned, 4096],
  441.  
  442.                 'Level'         => [1, 1, Parse_signed,   Z_DEFAULT_COMPRESSION()],
  443.                 'Method'        => [1, 1, Parse_unsigned, Z_DEFLATED()],
  444.                 'WindowBits'    => [1, 1, Parse_signed,   - MAX_WBITS()],
  445.                 'MemLevel'      => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()],
  446.                 'Strategy'      => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()],
  447.             }, @_) ;
  448.  
  449.     croak "Compress::Raw::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified " . 
  450.             $got->value('Bufsize')
  451.         unless $got->value('Bufsize') >= 1;
  452.  
  453.     my $flags = 0 ;
  454.     $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
  455.     $flags |= FLAG_CRC    if $got->value('CRC32') ;
  456.     $flags |= FLAG_ADLER  if $got->value('ADLER32') ;
  457.  
  458.     $pkg->_createDeflateStream($flags,
  459.                 $got->value('Level'), 
  460.                 $got->value('Method'), 
  461.                 $got->value('WindowBits'), 
  462.                 $got->value('MemLevel'), 
  463.                 $got->value('Strategy'), 
  464.                 $got->value('Bufsize'),
  465.                 ) ;
  466.  
  467. }
  468.  
  469. sub Compress::Raw::Zlib::inflateScanStream::inflate
  470. {
  471.     my $self = shift ;
  472.     my $buffer = $_[1];
  473.     my $eof = $_[2];
  474.  
  475.     my $status = $self->scan(@_);
  476.  
  477.     if ($status == Z_OK() && $_[2]) {
  478.         my $byte = ' ';
  479.         
  480.         $status = $self->scan(\$byte, $_[1]) ;
  481.     }
  482.     
  483.     return $status ;
  484. }
  485.  
  486. sub Compress::Raw::Zlib::deflateStream::deflateParams
  487. {
  488.     my $self = shift ;
  489.     my ($got) = ParseParameters(0, {
  490.                 'Level'      => [1, 1, Parse_signed,   undef],
  491.                 'Strategy'   => [1, 1, Parse_unsigned, undef],
  492.                 'Bufsize'    => [1, 1, Parse_unsigned, undef],
  493.                 }, 
  494.                 @_) ;
  495.  
  496.     croak "Compress::Raw::Zlib::deflateParams needs Level and/or Strategy"
  497.         unless $got->parsed('Level') + $got->parsed('Strategy') +
  498.             $got->parsed('Bufsize');
  499.  
  500.     croak "Compress::Raw::Zlib::Inflate::deflateParams: Bufsize must be >= 1, you specified " . 
  501.             $got->value('Bufsize')
  502.         if $got->parsed('Bufsize') && $got->value('Bufsize') <= 1;
  503.  
  504.     my $flags = 0;
  505.     $flags |= 1 if $got->parsed('Level') ;
  506.     $flags |= 2 if $got->parsed('Strategy') ;
  507.     $flags |= 4 if $got->parsed('Bufsize') ;
  508.  
  509.     $self->_deflateParams($flags, $got->value('Level'), 
  510.                           $got->value('Strategy'), $got->value('Bufsize'));
  511.  
  512. }
  513.  
  514.  
  515. # Autoload methods go after __END__, and are processed by the autosplit program.
  516.  
  517. 1;
  518. __END__
  519.  
  520.  
  521. =head1 NAME
  522.  
  523. Compress::Raw::Zlib - Low-Level Interface to zlib compression library
  524.  
  525. =head1 SYNOPSIS
  526.  
  527.     use Compress::Raw::Zlib ;
  528.  
  529.     ($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) ;
  530.     $status = $d->deflate($input, $output) ;
  531.     $status = $d->flush($output [, $flush_type]) ;
  532.     $d->deflateParams(OPTS) ;
  533.     $d->deflateTune(OPTS) ;
  534.     $d->dict_adler() ;
  535.     $d->crc32() ;
  536.     $d->adler32() ;
  537.     $d->total_in() ;
  538.     $d->total_out() ;
  539.     $d->msg() ;
  540.     $d->get_Strategy();
  541.     $d->get_Level();
  542.     $d->get_BufSize();
  543.  
  544.     ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) ;
  545.     $status = $i->inflate($input, $output [, $eof]) ;
  546.     $status = $i->inflateSync($input) ;
  547.     $i->dict_adler() ;
  548.     $d->crc32() ;
  549.     $d->adler32() ;
  550.     $i->total_in() ;
  551.     $i->total_out() ;
  552.     $i->msg() ;
  553.     $d->get_BufSize();
  554.  
  555.     $crc = adler32($buffer [,$crc]) ;
  556.     $crc = crc32($buffer [,$crc]) ;
  557.  
  558.     $crc = adler32_combine($crc1, $crc2, $len2)l
  559.     $crc = crc32_combine($adler1, $adler2, $len2)
  560.  
  561.     ZLIB_VERSION
  562.     ZLIB_VERNUM
  563.  
  564. =head1 DESCRIPTION
  565.  
  566. The I<Compress::Raw::Zlib> module provides a Perl interface to the I<zlib>
  567. compression library (see L</AUTHOR> for details about where to get
  568. I<zlib>). 
  569.  
  570.  
  571.  
  572. =head1 Compress::Raw::Zlib::Deflate
  573.  
  574. This section defines an interface that allows in-memory compression using
  575. the I<deflate> interface provided by zlib.
  576.  
  577. Here is a definition of the interface available:
  578.  
  579.  
  580. =head2 B<($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) >
  581.  
  582. Initialises a deflation object. 
  583.  
  584. If you are familiar with the I<zlib> library, it combines the
  585. features of the I<zlib> functions C<deflateInit>, C<deflateInit2>
  586. and C<deflateSetDictionary>.
  587.  
  588. If successful, it will return the initialised deflation object, C<$d>
  589. and a C<$status> of C<Z_OK> in a list context. In scalar context it
  590. returns the deflation object, C<$d>, only.
  591.  
  592. If not successful, the returned deflation object, C<$d>, will be
  593. I<undef> and C<$status> will hold the a I<zlib> error code.
  594.  
  595. The function optionally takes a number of named options specified as
  596. C<< Name => value >> pairs. This allows individual options to be
  597. tailored without having to specify them all in the parameter list.
  598.  
  599. For backward compatibility, it is also possible to pass the parameters
  600. as a reference to a hash containing the name=>value pairs.
  601.  
  602. Below is a list of the valid options:
  603.  
  604. =over 5
  605.  
  606. =item B<-Level>
  607.  
  608. Defines the compression level. Valid values are 0 through 9,
  609. C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
  610. C<Z_DEFAULT_COMPRESSION>.
  611.  
  612. The default is Z_DEFAULT_COMPRESSION.
  613.  
  614. =item B<-Method>
  615.  
  616. Defines the compression method. The only valid value at present (and
  617. the default) is Z_DEFLATED.
  618.  
  619. =item B<-WindowBits>
  620.  
  621. For a definition of the meaning and valid values for C<WindowBits>
  622. refer to the I<zlib> documentation for I<deflateInit2>.
  623.  
  624. Defaults to MAX_WBITS.
  625.  
  626. =item B<-MemLevel>
  627.  
  628. For a definition of the meaning and valid values for C<MemLevel>
  629. refer to the I<zlib> documentation for I<deflateInit2>.
  630.  
  631. Defaults to MAX_MEM_LEVEL.
  632.  
  633. =item B<-Strategy>
  634.  
  635. Defines the strategy used to tune the compression. The valid values are
  636. C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED>, C<Z_RLE>, C<Z_FIXED> and
  637. C<Z_HUFFMAN_ONLY>.
  638.  
  639. The default is Z_DEFAULT_STRATEGY.
  640.  
  641. =item B<-Dictionary>
  642.  
  643. When a dictionary is specified I<Compress::Raw::Zlib> will automatically
  644. call C<deflateSetDictionary> directly after calling C<deflateInit>. The
  645. Adler32 value for the dictionary can be obtained by calling the method 
  646. C<$d-E<gt>dict_adler()>.
  647.  
  648. The default is no dictionary.
  649.  
  650. =item B<-Bufsize>
  651.  
  652. Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
  653. and C<$d-E<gt>flush> methods. If the buffer has to be
  654. reallocated to increase the size, it will grow in increments of
  655. C<Bufsize>.
  656.  
  657. The default buffer size is 4096.
  658.  
  659. =item B<-AppendOutput>
  660.  
  661. This option controls how data is written to the output buffer by the
  662. C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
  663.  
  664. If the C<AppendOutput> option is set to false, the output buffers in the
  665. C<$d-E<gt>deflate> and C<$d-E<gt>flush>  methods will be truncated before
  666. uncompressed data is written to them.
  667.  
  668. If the option is set to true, uncompressed data will be appended to the
  669. output buffer in the C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
  670.  
  671. This option defaults to false.
  672.  
  673. =item B<-CRC32>
  674.  
  675. If set to true, a crc32 checksum of the uncompressed data will be
  676. calculated. Use the C<$d-E<gt>crc32> method to retrieve this value.
  677.  
  678. This option defaults to false.
  679.  
  680.  
  681. =item B<-ADLER32>
  682.  
  683. If set to true, an adler32 checksum of the uncompressed data will be
  684. calculated. Use the C<$d-E<gt>adler32> method to retrieve this value.
  685.  
  686. This option defaults to false.
  687.  
  688.  
  689. =back
  690.  
  691. Here is an example of using the C<Compress::Raw::Zlib::Deflate> optional
  692. parameter list to override the default buffer size and compression
  693. level. All other options will take their default values.
  694.  
  695.     my $d = new Compress::Raw::Zlib::Deflate ( -Bufsize => 300, 
  696.                                                -Level   => Z_BEST_SPEED ) ;
  697.  
  698.  
  699. =head2 B<$status = $d-E<gt>deflate($input, $output)>
  700.  
  701. Deflates the contents of C<$input> and writes the compressed data to
  702. C<$output>.
  703.  
  704. The C<$input> and C<$output> parameters can be either scalars or scalar
  705. references.
  706.  
  707. When finished, C<$input> will be completely processed (assuming there
  708. were no errors). If the deflation was successful it writes the deflated
  709. data to C<$output> and returns a status value of C<Z_OK>.
  710.  
  711. On error, it returns a I<zlib> error code.
  712.  
  713. If the C<AppendOutput> option is set to true in the constructor for
  714. the C<$d> object, the compressed data will be appended to C<$output>. If
  715. it is false, C<$output> will be truncated before any compressed data is
  716. written to it.
  717.  
  718. B<Note>: This method will not necessarily write compressed data to
  719. C<$output> every time it is called. So do not assume that there has been
  720. an error if the contents of C<$output> is empty on returning from
  721. this method. As long as the return code from the method is C<Z_OK>,
  722. the deflate has succeeded.
  723.  
  724. =head2 B<$status = $d-E<gt>flush($output [, $flush_type]) >
  725.  
  726. Typically used to finish the deflation. Any pending output will be
  727. written to C<$output>.
  728.  
  729. Returns C<Z_OK> if successful.
  730.  
  731. Note that flushing can seriously degrade the compression ratio, so it
  732. should only be used to terminate a decompression (using C<Z_FINISH>) or
  733. when you want to create a I<full flush point> (using C<Z_FULL_FLUSH>).
  734.  
  735. By default the C<flush_type> used is C<Z_FINISH>. Other valid values
  736. for C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH>
  737. and C<Z_FULL_FLUSH>. It is strongly recommended that you only set the
  738. C<flush_type> parameter if you fully understand the implications of
  739. what it does. See the C<zlib> documentation for details.
  740.  
  741. If the C<AppendOutput> option is set to true in the constructor for
  742. the C<$d> object, the compressed data will be appended to C<$output>. If
  743. it is false, C<$output> will be truncated before any compressed data is
  744. written to it.
  745.  
  746. =head2 B<$status = $d-E<gt>deflateParams([OPT])>
  747.  
  748. Change settings for the deflate object C<$d>.
  749.  
  750. The list of the valid options is shown below. Options not specified
  751. will remain unchanged.
  752.  
  753.  
  754. =over 5
  755.  
  756. =item B<-Level>
  757.  
  758. Defines the compression level. Valid values are 0 through 9,
  759. C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
  760. C<Z_DEFAULT_COMPRESSION>.
  761.  
  762. =item B<-Strategy>
  763.  
  764. Defines the strategy used to tune the compression. The valid values are
  765. C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. 
  766.  
  767. =item B<-BufSize>
  768.  
  769. Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
  770. and C<$d-E<gt>flush> methods. If the buffer has to be
  771. reallocated to increase the size, it will grow in increments of
  772. C<Bufsize>.
  773.  
  774.  
  775. =back
  776.  
  777. =head2 B<$status = $d-E<gt>deflateTune($good_length, $max_lazy, $nice_length, $max_chain)>
  778.  
  779. Tune the internal settings for the deflate object C<$d>. This option is
  780. only available if you are running zlib 1.2.2.3 or better.
  781.  
  782. Refer to the documentation in zlib.h for instructions on how to fly
  783. C<deflateTune>.
  784.  
  785. =head2 B<$d-E<gt>dict_adler()>
  786.  
  787. Returns the adler32 value for the dictionary.
  788.  
  789. =head2 B<$d-E<gt>crc32()>
  790.  
  791. Returns the crc32 value for the uncompressed data to date. 
  792.  
  793. If the C<CRC32> option is not enabled in the constructor for this object,
  794. this method will always return 0;
  795.  
  796. =head2 B<$d-E<gt>adler32()>
  797.  
  798. Returns the adler32 value for the uncompressed data to date. 
  799.  
  800. =head2 B<$d-E<gt>msg()>
  801.  
  802. Returns the last error message generated by zlib.
  803.  
  804. =head2 B<$d-E<gt>total_in()>
  805.  
  806. Returns the total number of bytes uncompressed bytes input to deflate.
  807.  
  808. =head2 B<$d-E<gt>total_out()>
  809.  
  810. Returns the total number of compressed bytes output from deflate.
  811.  
  812. =head2 B<$d-E<gt>get_Strategy()>
  813.  
  814. Returns the deflation strategy currently used. Valid values are
  815. C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. 
  816.  
  817.  
  818. =head2 B<$d-E<gt>get_Level()>
  819.  
  820. Returns the compression level being used. 
  821.  
  822. =head2 B<$d-E<gt>get_BufSize()>
  823.  
  824. Returns the buffer size used to carry out the compression.
  825.  
  826. =head2 Example
  827.  
  828.  
  829. Here is a trivial example of using C<deflate>. It simply reads standard
  830. input, deflates it and writes it to standard output.
  831.  
  832.     use strict ;
  833.     use warnings ;
  834.  
  835.     use Compress::Raw::Zlib ;
  836.  
  837.     binmode STDIN;
  838.     binmode STDOUT;
  839.     my $x = new Compress::Raw::Zlib::Deflate
  840.        or die "Cannot create a deflation stream\n" ;
  841.  
  842.     my ($output, $status) ;
  843.     while (<>)
  844.     {
  845.         $status = $x->deflate($_, $output) ;
  846.     
  847.         $status == Z_OK
  848.             or die "deflation failed\n" ;
  849.     
  850.         print $output ;
  851.     }
  852.     
  853.     $status = $x->flush($output) ;
  854.     
  855.     $status == Z_OK
  856.         or die "deflation failed\n" ;
  857.     
  858.     print $output ;
  859.  
  860. =head1 Compress::Raw::Zlib::Inflate
  861.  
  862. This section defines an interface that allows in-memory uncompression using
  863. the I<inflate> interface provided by zlib.
  864.  
  865. Here is a definition of the interface:
  866.  
  867.  
  868. =head2 B< ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) >
  869.  
  870. Initialises an inflation object. 
  871.  
  872. In a list context it returns the inflation object, C<$i>, and the
  873. I<zlib> status code (C<$status>). In a scalar context it returns the
  874. inflation object only.
  875.  
  876. If successful, C<$i> will hold the inflation object and C<$status> will
  877. be C<Z_OK>.
  878.  
  879. If not successful, C<$i> will be I<undef> and C<$status> will hold the
  880. I<zlib> error code.
  881.  
  882. The function optionally takes a number of named options specified as
  883. C<< -Name => value >> pairs. This allows individual options to be
  884. tailored without having to specify them all in the parameter list.
  885.  
  886. For backward compatibility, it is also possible to pass the parameters
  887. as a reference to a hash containing the C<< name=>value >> pairs.
  888.  
  889. Here is a list of the valid options:
  890.  
  891. =over 5
  892.  
  893. =item B<-WindowBits>
  894.  
  895. To uncompress an RFC 1950 data stream, set C<WindowBits> to a positive
  896. number.
  897.  
  898. To uncompress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>.
  899.  
  900. For a full definition of the meaning and valid values for C<WindowBits>
  901. refer to the I<zlib> documentation for I<inflateInit2>.
  902.  
  903. Defaults to MAX_WBITS.
  904.  
  905. =item B<-Bufsize>
  906.  
  907. Sets the initial size for the output buffer used by the C<$i-E<gt>inflate>
  908. method. If the output buffer in this method has to be reallocated to
  909. increase the size, it will grow in increments of C<Bufsize>.
  910.  
  911. Default is 4096.
  912.  
  913. =item B<-Dictionary>
  914.  
  915. The default is no dictionary.
  916.  
  917. =item B<-AppendOutput>
  918.  
  919. This option controls how data is written to the output buffer by the
  920. C<$i-E<gt>inflate> method.
  921.  
  922. If the option is set to false, the output buffer in the C<$i-E<gt>inflate>
  923. method will be truncated before uncompressed data is written to it.
  924.  
  925. If the option is set to true, uncompressed data will be appended to the
  926. output buffer by the C<$i-E<gt>inflate> method.
  927.  
  928. This option defaults to false.
  929.  
  930.  
  931. =item B<-CRC32>
  932.  
  933. If set to true, a crc32 checksum of the uncompressed data will be
  934. calculated. Use the C<$i-E<gt>crc32> method to retrieve this value.
  935.  
  936. This option defaults to false.
  937.  
  938. =item B<-ADLER32>
  939.  
  940. If set to true, an adler32 checksum of the uncompressed data will be
  941. calculated. Use the C<$i-E<gt>adler32> method to retrieve this value.
  942.  
  943. This option defaults to false.
  944.  
  945. =item B<-ConsumeInput>
  946.  
  947. If set to true, this option will remove compressed data from the input
  948. buffer of the the C< $i-E<gt>inflate > method as the inflate progresses.
  949.  
  950. This option can be useful when you are processing compressed data that is
  951. embedded in another file/buffer. In this case the data that immediately
  952. follows the compressed stream will be left in the input buffer.
  953.  
  954. This option defaults to true.
  955.  
  956. =back
  957.  
  958. Here is an example of using an optional parameter to override the default
  959. buffer size.
  960.  
  961.     my ($i, $status) = new Compress::Raw::Zlib::Inflate( -Bufsize => 300 ) ;
  962.  
  963. =head2 B< $status = $i-E<gt>inflate($input, $output [,$eof]) >
  964.  
  965. Inflates the complete contents of C<$input> and writes the uncompressed
  966. data to C<$output>. The C<$input> and C<$output> parameters can either be
  967. scalars or scalar references.
  968.  
  969. Returns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the
  970. compressed data has been successfully reached. 
  971.  
  972. If not successful C<$status> will hold the I<zlib> error code.
  973.  
  974. If the C<ConsumeInput> option has been set to true when the
  975. C<Compress::Raw::Zlib::Inflate> object is created, the C<$input> parameter
  976. is modified by C<inflate>. On completion it will contain what remains
  977. of the input buffer after inflation. In practice, this means that when
  978. the return status is C<Z_OK> the C<$input> parameter will contain an
  979. empty string, and when the return status is C<Z_STREAM_END> the C<$input>
  980. parameter will contains what (if anything) was stored in the input buffer
  981. after the deflated data stream.
  982.  
  983. This feature is useful when processing a file format that encapsulates
  984. a compressed data stream (e.g. gzip, zip) and there is useful data
  985. immediately after the deflation stream.
  986.  
  987. If the C<AppendOutput> option is set to true in the constructor for
  988. this object, the uncompressed data will be appended to C<$output>. If
  989. it is false, C<$output> will be truncated before any uncompressed data
  990. is written to it.
  991.  
  992. The C<$eof> parameter needs a bit of explanation. 
  993.  
  994. Prior to version 1.2.0, zlib assumed that there was at least one trailing
  995. byte immediately after the compressed data stream when it was carrying out
  996. decompression. This normally isn't a problem because the majority of zlib
  997. applications guarantee that there will be data directly after the
  998. compressed data stream.  For example, both gzip (RFC 1950) and zip both
  999. define trailing data that follows the compressed data stream.
  1000.  
  1001. The C<$eof> parameter only needs to be used if B<all> of the following
  1002. conditions apply
  1003.  
  1004. =over 5
  1005.  
  1006. =item 1 
  1007.  
  1008. You are either using a copy of zlib that is older than version 1.2.0 or you
  1009. want your application code to be able to run with as many different
  1010. versions of zlib as possible.
  1011.  
  1012. =item 2
  1013.  
  1014. You have set the C<WindowBits> parameter to C<-MAX_WBITS> in the constructor
  1015. for this object, i.e. you are uncompressing a raw deflated data stream
  1016. (RFC 1951).
  1017.  
  1018. =item 3
  1019.  
  1020. There is no data immediately after the compressed data stream.
  1021.  
  1022. =back
  1023.  
  1024. If B<all> of these are the case, then you need to set the C<$eof> parameter
  1025. to true on the final call (and only the final call) to C<$i-E<gt>inflate>. 
  1026.  
  1027. If you have built this module with zlib >= 1.2.0, the C<$eof> parameter is
  1028. ignored. You can still set it if you want, but it won't be used behind the
  1029. scenes.
  1030.  
  1031. =head2 B<$status = $i-E<gt>inflateSync($input)>
  1032.  
  1033. This method can be used to attempt to recover good data from a compressed
  1034. data stream that is partially corrupt.
  1035. It scans C<$input> until it reaches either a I<full flush point> or the
  1036. end of the buffer.
  1037.  
  1038. If a I<full flush point> is found, C<Z_OK> is returned and C<$input>
  1039. will be have all data up to the flush point removed. This data can then be
  1040. passed to the C<$i-E<gt>inflate> method to be uncompressed.
  1041.  
  1042. Any other return code means that a flush point was not found. If more
  1043. data is available, C<inflateSync> can be called repeatedly with more
  1044. compressed data until the flush point is found.
  1045.  
  1046. Note I<full flush points> are not present by default in compressed
  1047. data streams. They must have been added explicitly when the data stream
  1048. was created by calling C<Compress::Deflate::flush>  with C<Z_FULL_FLUSH>.
  1049.  
  1050.  
  1051. =head2 B<$i-E<gt>dict_adler()>
  1052.  
  1053. Returns the adler32 value for the dictionary.
  1054.  
  1055. =head2 B<$i-E<gt>crc32()>
  1056.  
  1057. Returns the crc32 value for the uncompressed data to date.
  1058.  
  1059. If the C<CRC32> option is not enabled in the constructor for this object,
  1060. this method will always return 0;
  1061.  
  1062. =head2 B<$i-E<gt>adler32()>
  1063.  
  1064. Returns the adler32 value for the uncompressed data to date.
  1065.  
  1066. If the C<ADLER32> option is not enabled in the constructor for this object,
  1067. this method will always return 0;
  1068.  
  1069. =head2 B<$i-E<gt>msg()>
  1070.  
  1071. Returns the last error message generated by zlib.
  1072.  
  1073. =head2 B<$i-E<gt>total_in()>
  1074.  
  1075. Returns the total number of bytes compressed bytes input to inflate.
  1076.  
  1077. =head2 B<$i-E<gt>total_out()>
  1078.  
  1079. Returns the total number of uncompressed bytes output from inflate.
  1080.  
  1081. =head2 B<$d-E<gt>get_BufSize()>
  1082.  
  1083. Returns the buffer size used to carry out the decompression.
  1084.  
  1085. =head2 Example
  1086.  
  1087. Here is an example of using C<inflate>.
  1088.  
  1089.     use strict ;
  1090.     use warnings ;
  1091.     
  1092.     use Compress::Raw::Zlib;
  1093.     
  1094.     my $x = new Compress::Raw::Zlib::Inflate()
  1095.        or die "Cannot create a inflation stream\n" ;
  1096.     
  1097.     my $input = '' ;
  1098.     binmode STDIN;
  1099.     binmode STDOUT;
  1100.     
  1101.     my ($output, $status) ;
  1102.     while (read(STDIN, $input, 4096))
  1103.     {
  1104.         $status = $x->inflate(\$input, $output) ;
  1105.     
  1106.         print $output 
  1107.             if $status == Z_OK or $status == Z_STREAM_END ;
  1108.     
  1109.         last if $status != Z_OK ;
  1110.     }
  1111.     
  1112.     die "inflation failed\n"
  1113.         unless $status == Z_STREAM_END ;
  1114.  
  1115. =head1 CHECKSUM FUNCTIONS
  1116.  
  1117. Two functions are provided by I<zlib> to calculate checksums. For the
  1118. Perl interface, the order of the two parameters in both functions has
  1119. been reversed. This allows both running checksums and one off
  1120. calculations to be done.
  1121.  
  1122.     $crc = adler32($buffer [,$crc]) ;
  1123.     $crc = crc32($buffer [,$crc]) ;
  1124.  
  1125. The buffer parameters can either be a scalar or a scalar reference.
  1126.  
  1127. If the $crc parameters is C<undef>, the crc value will be reset.
  1128.  
  1129. If you have built this module with zlib 1.2.3 or better, two more
  1130. CRC-related functions are available.
  1131.  
  1132.     $crc = adler32_combine($crc1, $crc2, $len2)l
  1133.     $crc = crc32_combine($adler1, $adler2, $len2)
  1134.  
  1135. These functions allow checksums to be merged.
  1136.  
  1137. =head1 ACCESSING ZIP FILES
  1138.  
  1139. Although it is possible (with some effort on your part) to use this
  1140. module to access .zip files, there is a module on CPAN that will do all
  1141. the hard work for you. Check out the C<Archive::Zip> module on CPAN at
  1142.  
  1143.     http://www.cpan.org/modules/by-module/Archive/Archive-Zip-*.tar.gz    
  1144.  
  1145.  
  1146. =head1 CONSTANTS
  1147.  
  1148. All the I<zlib> constants are automatically imported when you make use
  1149. of I<Compress::Raw::Zlib>.
  1150.  
  1151.  
  1152. =head1 SEE ALSO
  1153.  
  1154. 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>
  1155.  
  1156. L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
  1157.  
  1158. L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
  1159. L<Archive::Tar|Archive::Tar>,
  1160. L<IO::Zlib|IO::Zlib>
  1161.  
  1162.  
  1163. For RFC 1950, 1951 and 1952 see 
  1164. F<http://www.faqs.org/rfcs/rfc1950.html>,
  1165. F<http://www.faqs.org/rfcs/rfc1951.html> and
  1166. F<http://www.faqs.org/rfcs/rfc1952.html>
  1167.  
  1168. The I<zlib> compression library was written by Jean-loup Gailly
  1169. F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
  1170.  
  1171. The primary site for the I<zlib> compression library is
  1172. F<http://www.zlib.org>.
  1173.  
  1174. The primary site for gzip is F<http://www.gzip.org>.
  1175.  
  1176.  
  1177.  
  1178.  
  1179. =head1 AUTHOR
  1180.  
  1181. This module was written by Paul Marquess, F<pmqs@cpan.org>. 
  1182.  
  1183.  
  1184.  
  1185. =head1 MODIFICATION HISTORY
  1186.  
  1187. See the Changes file.
  1188.  
  1189. =head1 COPYRIGHT AND LICENSE
  1190.  
  1191. Copyright (c) 2005-2007 Paul Marquess. All rights reserved.
  1192.  
  1193. This program is free software; you can redistribute it and/or
  1194. modify it under the same terms as Perl itself.
  1195.  
  1196.  
  1197.  
  1198.