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