1b39c5158Smillert 2b39c5158Smillertpackage Compress::Raw::Zlib; 3b39c5158Smillert 4898184e3Ssthenrequire 5.006 ; 5b39c5158Smillertrequire Exporter; 6b39c5158Smillertuse Carp ; 7b39c5158Smillert 8b39c5158Smillertuse strict ; 9b39c5158Smillertuse warnings ; 10b39c5158Smillertuse bytes ; 11898184e3Ssthenour ($VERSION, $XS_VERSION, @ISA, @EXPORT, %EXPORT_TAGS, @EXPORT_OK, $AUTOLOAD, %DEFLATE_CONSTANTS, @DEFLATE_CONSTANTS); 12b39c5158Smillert 13*3d61058aSafresh1$VERSION = '2.212'; 14b39c5158Smillert$XS_VERSION = $VERSION; 15b39c5158Smillert$VERSION = eval $VERSION; 16b39c5158Smillert 17b39c5158Smillert@ISA = qw(Exporter); 18898184e3Ssthen%EXPORT_TAGS = ( flush => [qw{ 19898184e3Ssthen Z_NO_FLUSH 20898184e3Ssthen Z_PARTIAL_FLUSH 21898184e3Ssthen Z_SYNC_FLUSH 22898184e3Ssthen Z_FULL_FLUSH 23898184e3Ssthen Z_FINISH 24898184e3Ssthen Z_BLOCK 25898184e3Ssthen }], 26898184e3Ssthen level => [qw{ 27898184e3Ssthen Z_NO_COMPRESSION 28898184e3Ssthen Z_BEST_SPEED 29898184e3Ssthen Z_BEST_COMPRESSION 30898184e3Ssthen Z_DEFAULT_COMPRESSION 31898184e3Ssthen }], 32898184e3Ssthen strategy => [qw{ 33898184e3Ssthen Z_FILTERED 34898184e3Ssthen Z_HUFFMAN_ONLY 35898184e3Ssthen Z_RLE 36898184e3Ssthen Z_FIXED 37898184e3Ssthen Z_DEFAULT_STRATEGY 38898184e3Ssthen }], 39898184e3Ssthen status => [qw{ 40898184e3Ssthen Z_OK 41898184e3Ssthen Z_STREAM_END 42898184e3Ssthen Z_NEED_DICT 43898184e3Ssthen Z_ERRNO 44898184e3Ssthen Z_STREAM_ERROR 45898184e3Ssthen Z_DATA_ERROR 46898184e3Ssthen Z_MEM_ERROR 47898184e3Ssthen Z_BUF_ERROR 48898184e3Ssthen Z_VERSION_ERROR 49898184e3Ssthen }], 50898184e3Ssthen ); 51898184e3Ssthen 52898184e3Ssthen%DEFLATE_CONSTANTS = %EXPORT_TAGS; 53898184e3Ssthen 54b39c5158Smillert# Items to export into callers namespace by default. Note: do not export 55b39c5158Smillert# names by default without a very good reason. Use EXPORT_OK instead. 56b39c5158Smillert# Do not simply export all your public functions/methods/constants. 57898184e3Ssthen@DEFLATE_CONSTANTS = 58b39c5158Smillert@EXPORT = qw( 59b39c5158Smillert ZLIB_VERSION 60b39c5158Smillert ZLIB_VERNUM 61b39c5158Smillert 62898184e3Ssthen 63b39c5158Smillert OS_CODE 64b39c5158Smillert 65b39c5158Smillert MAX_MEM_LEVEL 66b39c5158Smillert MAX_WBITS 67b39c5158Smillert 68b39c5158Smillert Z_ASCII 69b39c5158Smillert Z_BEST_COMPRESSION 70b39c5158Smillert Z_BEST_SPEED 71b39c5158Smillert Z_BINARY 72b39c5158Smillert Z_BLOCK 73b39c5158Smillert Z_BUF_ERROR 74b39c5158Smillert Z_DATA_ERROR 75b39c5158Smillert Z_DEFAULT_COMPRESSION 76b39c5158Smillert Z_DEFAULT_STRATEGY 77b39c5158Smillert Z_DEFLATED 78b39c5158Smillert Z_ERRNO 79b39c5158Smillert Z_FILTERED 80b39c5158Smillert Z_FIXED 81b39c5158Smillert Z_FINISH 82b39c5158Smillert Z_FULL_FLUSH 83b39c5158Smillert Z_HUFFMAN_ONLY 84b39c5158Smillert Z_MEM_ERROR 85b39c5158Smillert Z_NEED_DICT 86b39c5158Smillert Z_NO_COMPRESSION 87b39c5158Smillert Z_NO_FLUSH 88b39c5158Smillert Z_NULL 89b39c5158Smillert Z_OK 90b39c5158Smillert Z_PARTIAL_FLUSH 91b39c5158Smillert Z_RLE 92b39c5158Smillert Z_STREAM_END 93b39c5158Smillert Z_STREAM_ERROR 94b39c5158Smillert Z_SYNC_FLUSH 95898184e3Ssthen Z_TREES 96b39c5158Smillert Z_UNKNOWN 97b39c5158Smillert Z_VERSION_ERROR 98b39c5158Smillert 99fdcd7346Safresh1 ZLIBNG_VERSION 100fdcd7346Safresh1 ZLIBNG_VERNUM 101fdcd7346Safresh1 ZLIBNG_VER_MAJOR 102fdcd7346Safresh1 ZLIBNG_VER_MINOR 103fdcd7346Safresh1 ZLIBNG_VER_REVISION 104fdcd7346Safresh1 ZLIBNG_VER_STATUS 105fdcd7346Safresh1 ZLIBNG_VER_MODIFIED 106fdcd7346Safresh1 107b39c5158Smillert WANT_GZIP 108b39c5158Smillert WANT_GZIP_OR_ZLIB 109b39c5158Smillert); 110b39c5158Smillert 111898184e3Ssthenpush @EXPORT, qw(crc32 adler32 DEF_WBITS); 112898184e3Ssthen 113b39c5158Smillertuse constant WANT_GZIP => 16; 114b39c5158Smillertuse constant WANT_GZIP_OR_ZLIB => 32; 115b39c5158Smillert 116b39c5158Smillertsub AUTOLOAD { 117b39c5158Smillert my($constname); 118b39c5158Smillert ($constname = $AUTOLOAD) =~ s/.*:://; 119b39c5158Smillert my ($error, $val) = constant($constname); 120b39c5158Smillert Carp::croak $error if $error; 121b39c5158Smillert no strict 'refs'; 122b39c5158Smillert *{$AUTOLOAD} = sub { $val }; 123b39c5158Smillert goto &{$AUTOLOAD}; 124b39c5158Smillert} 125b39c5158Smillert 126b39c5158Smillertuse constant FLAG_APPEND => 1 ; 127b39c5158Smillertuse constant FLAG_CRC => 2 ; 128b39c5158Smillertuse constant FLAG_ADLER => 4 ; 129b39c5158Smillertuse constant FLAG_CONSUME_INPUT => 8 ; 130b39c5158Smillertuse constant FLAG_LIMIT_OUTPUT => 16 ; 131b39c5158Smillert 132b39c5158Smillerteval { 133b39c5158Smillert require XSLoader; 134b39c5158Smillert XSLoader::load('Compress::Raw::Zlib', $XS_VERSION); 135b39c5158Smillert 1; 136b39c5158Smillert} 137b39c5158Smillertor do { 138b39c5158Smillert require DynaLoader; 139b39c5158Smillert local @ISA = qw(DynaLoader); 140b39c5158Smillert bootstrap Compress::Raw::Zlib $XS_VERSION ; 141b39c5158Smillert}; 142b39c5158Smillert 143b39c5158Smillert 144b39c5158Smillertuse constant Parse_any => 0x01; 145b39c5158Smillertuse constant Parse_unsigned => 0x02; 146b39c5158Smillertuse constant Parse_signed => 0x04; 147b39c5158Smillertuse constant Parse_boolean => 0x08; 14891f110e0Safresh1#use constant Parse_string => 0x10; 14991f110e0Safresh1#use constant Parse_custom => 0x12; 150b39c5158Smillert 15191f110e0Safresh1#use constant Parse_store_ref => 0x100 ; 152b39c5158Smillert 153b39c5158Smillertuse constant OFF_PARSED => 0 ; 154b39c5158Smillertuse constant OFF_TYPE => 1 ; 155b39c5158Smillertuse constant OFF_DEFAULT => 2 ; 156b39c5158Smillertuse constant OFF_FIXED => 3 ; 157b39c5158Smillertuse constant OFF_FIRST_ONLY => 4 ; 158b39c5158Smillertuse constant OFF_STICKY => 5 ; 159b39c5158Smillert 160b39c5158Smillert 161b39c5158Smillert 162b39c5158Smillertsub ParseParameters 163b39c5158Smillert{ 164b39c5158Smillert my $level = shift || 0 ; 165b39c5158Smillert 166b39c5158Smillert my $sub = (caller($level + 1))[3] ; 167b39c5158Smillert #local $Carp::CarpLevel = 1 ; 168b39c5158Smillert my $p = new Compress::Raw::Zlib::Parameters() ; 169b39c5158Smillert $p->parse(@_) 170b39c5158Smillert or croak "$sub: $p->{Error}" ; 171b39c5158Smillert 172b39c5158Smillert return $p; 173b39c5158Smillert} 174b39c5158Smillert 175b39c5158Smillert 176b39c5158Smillertsub Compress::Raw::Zlib::Parameters::new 177b39c5158Smillert{ 178b39c5158Smillert my $class = shift ; 179b39c5158Smillert 180b39c5158Smillert my $obj = { Error => '', 181b39c5158Smillert Got => {}, 182b39c5158Smillert } ; 183b39c5158Smillert 184b39c5158Smillert #return bless $obj, ref($class) || $class || __PACKAGE__ ; 185b39c5158Smillert return bless $obj, 'Compress::Raw::Zlib::Parameters' ; 186b39c5158Smillert} 187b39c5158Smillert 188b39c5158Smillertsub Compress::Raw::Zlib::Parameters::setError 189b39c5158Smillert{ 190b39c5158Smillert my $self = shift ; 191b39c5158Smillert my $error = shift ; 192b39c5158Smillert my $retval = @_ ? shift : undef ; 193b39c5158Smillert 194b39c5158Smillert $self->{Error} = $error ; 195b39c5158Smillert return $retval; 196b39c5158Smillert} 197b39c5158Smillert 198b39c5158Smillert#sub getError 199b39c5158Smillert#{ 200b39c5158Smillert# my $self = shift ; 201b39c5158Smillert# return $self->{Error} ; 202b39c5158Smillert#} 203b39c5158Smillert 204b39c5158Smillertsub Compress::Raw::Zlib::Parameters::parse 205b39c5158Smillert{ 206b39c5158Smillert my $self = shift ; 207b39c5158Smillert 208b39c5158Smillert my $default = shift ; 209b39c5158Smillert 210b39c5158Smillert my $got = $self->{Got} ; 211b39c5158Smillert my $firstTime = keys %{ $got } == 0 ; 212b39c5158Smillert 213b39c5158Smillert my (@Bad) ; 214b39c5158Smillert my @entered = () ; 215b39c5158Smillert 216b39c5158Smillert # Allow the options to be passed as a hash reference or 217b39c5158Smillert # as the complete hash. 218b39c5158Smillert if (@_ == 0) { 219b39c5158Smillert @entered = () ; 220b39c5158Smillert } 221b39c5158Smillert elsif (@_ == 1) { 222b39c5158Smillert my $href = $_[0] ; 223b39c5158Smillert return $self->setError("Expected even number of parameters, got 1") 224b39c5158Smillert if ! defined $href or ! ref $href or ref $href ne "HASH" ; 225b39c5158Smillert 226b39c5158Smillert foreach my $key (keys %$href) { 227b39c5158Smillert push @entered, $key ; 228b39c5158Smillert push @entered, \$href->{$key} ; 229b39c5158Smillert } 230b39c5158Smillert } 231b39c5158Smillert else { 232b39c5158Smillert my $count = @_; 233b39c5158Smillert return $self->setError("Expected even number of parameters, got $count") 234b39c5158Smillert if $count % 2 != 0 ; 235b39c5158Smillert 236b39c5158Smillert for my $i (0.. $count / 2 - 1) { 237b39c5158Smillert push @entered, $_[2* $i] ; 238b39c5158Smillert push @entered, \$_[2* $i+1] ; 239b39c5158Smillert } 240b39c5158Smillert } 241b39c5158Smillert 242b39c5158Smillert 243b39c5158Smillert while (my ($key, $v) = each %$default) 244b39c5158Smillert { 245b39c5158Smillert croak "need 4 params [@$v]" 246b39c5158Smillert if @$v != 4 ; 247b39c5158Smillert 248b39c5158Smillert my ($first_only, $sticky, $type, $value) = @$v ; 249b39c5158Smillert my $x ; 250b39c5158Smillert $self->_checkType($key, \$value, $type, 0, \$x) 251b39c5158Smillert or return undef ; 252b39c5158Smillert 253b39c5158Smillert $key = lc $key; 254b39c5158Smillert 255b39c5158Smillert if ($firstTime || ! $sticky) { 256b39c5158Smillert $got->{$key} = [0, $type, $value, $x, $first_only, $sticky] ; 257b39c5158Smillert } 258b39c5158Smillert 259b39c5158Smillert $got->{$key}[OFF_PARSED] = 0 ; 260b39c5158Smillert } 261b39c5158Smillert 262b39c5158Smillert for my $i (0.. @entered / 2 - 1) { 263b39c5158Smillert my $key = $entered[2* $i] ; 264b39c5158Smillert my $value = $entered[2* $i+1] ; 265b39c5158Smillert 266b39c5158Smillert #print "Key [$key] Value [$value]" ; 267b39c5158Smillert #print defined $$value ? "[$$value]\n" : "[undef]\n"; 268b39c5158Smillert 269b39c5158Smillert $key =~ s/^-// ; 270b39c5158Smillert my $canonkey = lc $key; 271b39c5158Smillert 272b39c5158Smillert if ($got->{$canonkey} && ($firstTime || 273b39c5158Smillert ! $got->{$canonkey}[OFF_FIRST_ONLY] )) 274b39c5158Smillert { 275b39c5158Smillert my $type = $got->{$canonkey}[OFF_TYPE] ; 276b39c5158Smillert my $s ; 277b39c5158Smillert $self->_checkType($key, $value, $type, 1, \$s) 278b39c5158Smillert or return undef ; 279b39c5158Smillert #$value = $$value unless $type & Parse_store_ref ; 280b39c5158Smillert $value = $$value ; 281b39c5158Smillert $got->{$canonkey} = [1, $type, $value, $s] ; 282b39c5158Smillert } 283b39c5158Smillert else 284b39c5158Smillert { push (@Bad, $key) } 285b39c5158Smillert } 286b39c5158Smillert 287b39c5158Smillert if (@Bad) { 288b39c5158Smillert my ($bad) = join(", ", @Bad) ; 289b39c5158Smillert return $self->setError("unknown key value(s) @Bad") ; 290b39c5158Smillert } 291b39c5158Smillert 292b39c5158Smillert return 1; 293b39c5158Smillert} 294b39c5158Smillert 295b39c5158Smillertsub Compress::Raw::Zlib::Parameters::_checkType 296b39c5158Smillert{ 297b39c5158Smillert my $self = shift ; 298b39c5158Smillert 299b39c5158Smillert my $key = shift ; 300b39c5158Smillert my $value = shift ; 301b39c5158Smillert my $type = shift ; 302b39c5158Smillert my $validate = shift ; 303b39c5158Smillert my $output = shift; 304b39c5158Smillert 305b39c5158Smillert #local $Carp::CarpLevel = $level ; 306b39c5158Smillert #print "PARSE $type $key $value $validate $sub\n" ; 30791f110e0Safresh1# if ( $type & Parse_store_ref) 30891f110e0Safresh1# { 30991f110e0Safresh1# #$value = $$value 31091f110e0Safresh1# # if ref ${ $value } ; 31191f110e0Safresh1# 31291f110e0Safresh1# $$output = $value ; 31391f110e0Safresh1# return 1; 31491f110e0Safresh1# } 315b39c5158Smillert 316b39c5158Smillert $value = $$value ; 317b39c5158Smillert 318b39c5158Smillert if ($type & Parse_any) 319b39c5158Smillert { 320b39c5158Smillert $$output = $value ; 321b39c5158Smillert return 1; 322b39c5158Smillert } 323b39c5158Smillert elsif ($type & Parse_unsigned) 324b39c5158Smillert { 325b39c5158Smillert return $self->setError("Parameter '$key' must be an unsigned int, got 'undef'") 326b39c5158Smillert if $validate && ! defined $value ; 327b39c5158Smillert return $self->setError("Parameter '$key' must be an unsigned int, got '$value'") 328b39c5158Smillert if $validate && $value !~ /^\d+$/; 329b39c5158Smillert 330b39c5158Smillert $$output = defined $value ? $value : 0 ; 331b39c5158Smillert return 1; 332b39c5158Smillert } 333b39c5158Smillert elsif ($type & Parse_signed) 334b39c5158Smillert { 335b39c5158Smillert return $self->setError("Parameter '$key' must be a signed int, got 'undef'") 336b39c5158Smillert if $validate && ! defined $value ; 337b39c5158Smillert return $self->setError("Parameter '$key' must be a signed int, got '$value'") 338b39c5158Smillert if $validate && $value !~ /^-?\d+$/; 339b39c5158Smillert 340b39c5158Smillert $$output = defined $value ? $value : 0 ; 341b39c5158Smillert return 1 ; 342b39c5158Smillert } 343b39c5158Smillert elsif ($type & Parse_boolean) 344b39c5158Smillert { 345b39c5158Smillert return $self->setError("Parameter '$key' must be an int, got '$value'") 346b39c5158Smillert if $validate && defined $value && $value !~ /^\d*$/; 347b39c5158Smillert $$output = defined $value ? $value != 0 : 0 ; 348b39c5158Smillert return 1; 349b39c5158Smillert } 35091f110e0Safresh1# elsif ($type & Parse_string) 35191f110e0Safresh1# { 35291f110e0Safresh1# $$output = defined $value ? $value : "" ; 35391f110e0Safresh1# return 1; 35491f110e0Safresh1# } 355b39c5158Smillert 356b39c5158Smillert $$output = $value ; 357b39c5158Smillert return 1; 358b39c5158Smillert} 359b39c5158Smillert 360b39c5158Smillert 361b39c5158Smillert 362b39c5158Smillertsub Compress::Raw::Zlib::Parameters::parsed 363b39c5158Smillert{ 364b39c5158Smillert my $self = shift ; 365b39c5158Smillert my $name = shift ; 366b39c5158Smillert 367b39c5158Smillert return $self->{Got}{lc $name}[OFF_PARSED] ; 368b39c5158Smillert} 369b39c5158Smillert 370b39c5158Smillertsub Compress::Raw::Zlib::Parameters::value 371b39c5158Smillert{ 372b39c5158Smillert my $self = shift ; 373b39c5158Smillert my $name = shift ; 374b39c5158Smillert 375b39c5158Smillert if (@_) 376b39c5158Smillert { 377b39c5158Smillert $self->{Got}{lc $name}[OFF_PARSED] = 1; 378b39c5158Smillert $self->{Got}{lc $name}[OFF_DEFAULT] = $_[0] ; 379b39c5158Smillert $self->{Got}{lc $name}[OFF_FIXED] = $_[0] ; 380b39c5158Smillert } 381b39c5158Smillert 382b39c5158Smillert return $self->{Got}{lc $name}[OFF_FIXED] ; 383b39c5158Smillert} 384b39c5158Smillert 38591f110e0Safresh1our $OPTIONS_deflate = 386b39c5158Smillert { 387b39c5158Smillert 'AppendOutput' => [1, 1, Parse_boolean, 0], 388b39c5158Smillert 'CRC32' => [1, 1, Parse_boolean, 0], 389b39c5158Smillert 'ADLER32' => [1, 1, Parse_boolean, 0], 390b39c5158Smillert 'Bufsize' => [1, 1, Parse_unsigned, 4096], 391b39c5158Smillert 392b39c5158Smillert 'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()], 393b39c5158Smillert 'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()], 394b39c5158Smillert 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()], 395b39c5158Smillert 'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()], 396b39c5158Smillert 'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()], 397b39c5158Smillert 'Dictionary' => [1, 1, Parse_any, ""], 39891f110e0Safresh1 }; 399b39c5158Smillert 40091f110e0Safresh1sub Compress::Raw::Zlib::Deflate::new 40191f110e0Safresh1{ 40291f110e0Safresh1 my $pkg = shift ; 40391f110e0Safresh1 my ($got) = ParseParameters(0, $OPTIONS_deflate, @_); 404b39c5158Smillert 405b39c5158Smillert croak "Compress::Raw::Zlib::Deflate::new: Bufsize must be >= 1, you specified " . 406b39c5158Smillert $got->value('Bufsize') 407b39c5158Smillert unless $got->value('Bufsize') >= 1; 408b39c5158Smillert 409b39c5158Smillert my $flags = 0 ; 410b39c5158Smillert $flags |= FLAG_APPEND if $got->value('AppendOutput') ; 411b39c5158Smillert $flags |= FLAG_CRC if $got->value('CRC32') ; 412b39c5158Smillert $flags |= FLAG_ADLER if $got->value('ADLER32') ; 413b39c5158Smillert 414b39c5158Smillert my $windowBits = $got->value('WindowBits'); 415b39c5158Smillert $windowBits += MAX_WBITS() 416b39c5158Smillert if ($windowBits & MAX_WBITS()) == 0 ; 417b39c5158Smillert 418b39c5158Smillert _deflateInit($flags, 419b39c5158Smillert $got->value('Level'), 420b39c5158Smillert $got->value('Method'), 421b39c5158Smillert $windowBits, 422b39c5158Smillert $got->value('MemLevel'), 423b39c5158Smillert $got->value('Strategy'), 424b39c5158Smillert $got->value('Bufsize'), 425b39c5158Smillert $got->value('Dictionary')) ; 426b39c5158Smillert 427b39c5158Smillert} 428b39c5158Smillert 429898184e3Ssthensub Compress::Raw::Zlib::deflateStream::STORABLE_freeze 430898184e3Ssthen{ 431898184e3Ssthen my $type = ref shift; 432898184e3Ssthen croak "Cannot freeze $type object\n"; 433898184e3Ssthen} 434898184e3Ssthen 435898184e3Ssthensub Compress::Raw::Zlib::deflateStream::STORABLE_thaw 436898184e3Ssthen{ 437898184e3Ssthen my $type = ref shift; 438898184e3Ssthen croak "Cannot thaw $type object\n"; 439898184e3Ssthen} 440898184e3Ssthen 441898184e3Ssthen 44291f110e0Safresh1our $OPTIONS_inflate = 443b39c5158Smillert { 444b39c5158Smillert 'AppendOutput' => [1, 1, Parse_boolean, 0], 445b39c5158Smillert 'LimitOutput' => [1, 1, Parse_boolean, 0], 446b39c5158Smillert 'CRC32' => [1, 1, Parse_boolean, 0], 447b39c5158Smillert 'ADLER32' => [1, 1, Parse_boolean, 0], 448b39c5158Smillert 'ConsumeInput' => [1, 1, Parse_boolean, 1], 449b39c5158Smillert 'Bufsize' => [1, 1, Parse_unsigned, 4096], 450b39c5158Smillert 451b39c5158Smillert 'WindowBits' => [1, 1, Parse_signed, MAX_WBITS()], 452b39c5158Smillert 'Dictionary' => [1, 1, Parse_any, ""], 45391f110e0Safresh1 } ; 454b39c5158Smillert 45591f110e0Safresh1sub Compress::Raw::Zlib::Inflate::new 45691f110e0Safresh1{ 45791f110e0Safresh1 my $pkg = shift ; 45891f110e0Safresh1 my ($got) = ParseParameters(0, $OPTIONS_inflate, @_); 459b39c5158Smillert 460b39c5158Smillert croak "Compress::Raw::Zlib::Inflate::new: Bufsize must be >= 1, you specified " . 461b39c5158Smillert $got->value('Bufsize') 462b39c5158Smillert unless $got->value('Bufsize') >= 1; 463b39c5158Smillert 464b39c5158Smillert my $flags = 0 ; 465b39c5158Smillert $flags |= FLAG_APPEND if $got->value('AppendOutput') ; 466b39c5158Smillert $flags |= FLAG_CRC if $got->value('CRC32') ; 467b39c5158Smillert $flags |= FLAG_ADLER if $got->value('ADLER32') ; 468b39c5158Smillert $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ; 469b39c5158Smillert $flags |= FLAG_LIMIT_OUTPUT if $got->value('LimitOutput') ; 470b39c5158Smillert 471b39c5158Smillert 472b39c5158Smillert my $windowBits = $got->value('WindowBits'); 473b39c5158Smillert $windowBits += MAX_WBITS() 474b39c5158Smillert if ($windowBits & MAX_WBITS()) == 0 ; 475b39c5158Smillert 476b39c5158Smillert _inflateInit($flags, $windowBits, $got->value('Bufsize'), 477b39c5158Smillert $got->value('Dictionary')) ; 478b39c5158Smillert} 479b39c5158Smillert 480898184e3Ssthensub Compress::Raw::Zlib::inflateStream::STORABLE_freeze 481898184e3Ssthen{ 482898184e3Ssthen my $type = ref shift; 483898184e3Ssthen croak "Cannot freeze $type object\n"; 484898184e3Ssthen} 485898184e3Ssthen 486898184e3Ssthensub Compress::Raw::Zlib::inflateStream::STORABLE_thaw 487898184e3Ssthen{ 488898184e3Ssthen my $type = ref shift; 489898184e3Ssthen croak "Cannot thaw $type object\n"; 490898184e3Ssthen} 491898184e3Ssthen 492b39c5158Smillertsub Compress::Raw::Zlib::InflateScan::new 493b39c5158Smillert{ 494b39c5158Smillert my $pkg = shift ; 495b39c5158Smillert my ($got) = ParseParameters(0, 496b39c5158Smillert { 497b39c5158Smillert 'CRC32' => [1, 1, Parse_boolean, 0], 498b39c5158Smillert 'ADLER32' => [1, 1, Parse_boolean, 0], 499b39c5158Smillert 'Bufsize' => [1, 1, Parse_unsigned, 4096], 500b39c5158Smillert 501b39c5158Smillert 'WindowBits' => [1, 1, Parse_signed, -MAX_WBITS()], 502b39c5158Smillert 'Dictionary' => [1, 1, Parse_any, ""], 503b39c5158Smillert }, @_) ; 504b39c5158Smillert 505b39c5158Smillert 506b39c5158Smillert croak "Compress::Raw::Zlib::InflateScan::new: Bufsize must be >= 1, you specified " . 507b39c5158Smillert $got->value('Bufsize') 508b39c5158Smillert unless $got->value('Bufsize') >= 1; 509b39c5158Smillert 510b39c5158Smillert my $flags = 0 ; 511b39c5158Smillert #$flags |= FLAG_APPEND if $got->value('AppendOutput') ; 512b39c5158Smillert $flags |= FLAG_CRC if $got->value('CRC32') ; 513b39c5158Smillert $flags |= FLAG_ADLER if $got->value('ADLER32') ; 514b39c5158Smillert #$flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ; 515b39c5158Smillert 516b39c5158Smillert _inflateScanInit($flags, $got->value('WindowBits'), $got->value('Bufsize'), 517b39c5158Smillert '') ; 518b39c5158Smillert} 519b39c5158Smillert 520b39c5158Smillertsub Compress::Raw::Zlib::inflateScanStream::createDeflateStream 521b39c5158Smillert{ 522b39c5158Smillert my $pkg = shift ; 523b39c5158Smillert my ($got) = ParseParameters(0, 524b39c5158Smillert { 525b39c5158Smillert 'AppendOutput' => [1, 1, Parse_boolean, 0], 526b39c5158Smillert 'CRC32' => [1, 1, Parse_boolean, 0], 527b39c5158Smillert 'ADLER32' => [1, 1, Parse_boolean, 0], 528b39c5158Smillert 'Bufsize' => [1, 1, Parse_unsigned, 4096], 529b39c5158Smillert 530b39c5158Smillert 'Level' => [1, 1, Parse_signed, Z_DEFAULT_COMPRESSION()], 531b39c5158Smillert 'Method' => [1, 1, Parse_unsigned, Z_DEFLATED()], 532b39c5158Smillert 'WindowBits' => [1, 1, Parse_signed, - MAX_WBITS()], 533b39c5158Smillert 'MemLevel' => [1, 1, Parse_unsigned, MAX_MEM_LEVEL()], 534b39c5158Smillert 'Strategy' => [1, 1, Parse_unsigned, Z_DEFAULT_STRATEGY()], 535b39c5158Smillert }, @_) ; 536b39c5158Smillert 537b39c5158Smillert croak "Compress::Raw::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified " . 538b39c5158Smillert $got->value('Bufsize') 539b39c5158Smillert unless $got->value('Bufsize') >= 1; 540b39c5158Smillert 541b39c5158Smillert my $flags = 0 ; 542b39c5158Smillert $flags |= FLAG_APPEND if $got->value('AppendOutput') ; 543b39c5158Smillert $flags |= FLAG_CRC if $got->value('CRC32') ; 544b39c5158Smillert $flags |= FLAG_ADLER if $got->value('ADLER32') ; 545b39c5158Smillert 546b39c5158Smillert $pkg->_createDeflateStream($flags, 547b39c5158Smillert $got->value('Level'), 548b39c5158Smillert $got->value('Method'), 549b39c5158Smillert $got->value('WindowBits'), 550b39c5158Smillert $got->value('MemLevel'), 551b39c5158Smillert $got->value('Strategy'), 552b39c5158Smillert $got->value('Bufsize'), 553b39c5158Smillert ) ; 554b39c5158Smillert 555b39c5158Smillert} 556b39c5158Smillert 557b39c5158Smillertsub Compress::Raw::Zlib::inflateScanStream::inflate 558b39c5158Smillert{ 559b39c5158Smillert my $self = shift ; 560b39c5158Smillert my $buffer = $_[1]; 561b39c5158Smillert my $eof = $_[2]; 562b39c5158Smillert 563b39c5158Smillert my $status = $self->scan(@_); 564b39c5158Smillert 565b39c5158Smillert if ($status == Z_OK() && $_[2]) { 566b39c5158Smillert my $byte = ' '; 567b39c5158Smillert 568b39c5158Smillert $status = $self->scan(\$byte, $_[1]) ; 569b39c5158Smillert } 570b39c5158Smillert 571b39c5158Smillert return $status ; 572b39c5158Smillert} 573b39c5158Smillert 574b39c5158Smillertsub Compress::Raw::Zlib::deflateStream::deflateParams 575b39c5158Smillert{ 576b39c5158Smillert my $self = shift ; 577b39c5158Smillert my ($got) = ParseParameters(0, { 578b39c5158Smillert 'Level' => [1, 1, Parse_signed, undef], 579b39c5158Smillert 'Strategy' => [1, 1, Parse_unsigned, undef], 580b39c5158Smillert 'Bufsize' => [1, 1, Parse_unsigned, undef], 581b39c5158Smillert }, 582b39c5158Smillert @_) ; 583b39c5158Smillert 584b39c5158Smillert croak "Compress::Raw::Zlib::deflateParams needs Level and/or Strategy" 585b39c5158Smillert unless $got->parsed('Level') + $got->parsed('Strategy') + 586b39c5158Smillert $got->parsed('Bufsize'); 587b39c5158Smillert 588b39c5158Smillert croak "Compress::Raw::Zlib::Inflate::deflateParams: Bufsize must be >= 1, you specified " . 589b39c5158Smillert $got->value('Bufsize') 590b39c5158Smillert if $got->parsed('Bufsize') && $got->value('Bufsize') <= 1; 591b39c5158Smillert 592b39c5158Smillert my $flags = 0; 593b39c5158Smillert $flags |= 1 if $got->parsed('Level') ; 594b39c5158Smillert $flags |= 2 if $got->parsed('Strategy') ; 595b39c5158Smillert $flags |= 4 if $got->parsed('Bufsize') ; 596b39c5158Smillert 597b39c5158Smillert $self->_deflateParams($flags, $got->value('Level'), 598b39c5158Smillert $got->value('Strategy'), $got->value('Bufsize')); 599b39c5158Smillert 600b39c5158Smillert} 601b39c5158Smillert 602b39c5158Smillert 603b39c5158Smillert1; 604b39c5158Smillert__END__ 605b39c5158Smillert 606b39c5158Smillert 607b39c5158Smillert=head1 NAME 608b39c5158Smillert 609fdcd7346Safresh1Compress::Raw::Zlib - Low-Level Interface to zlib or zlib-ng compression library 610b39c5158Smillert 611b39c5158Smillert=head1 SYNOPSIS 612b39c5158Smillert 613b39c5158Smillert use Compress::Raw::Zlib ; 614b39c5158Smillert 615b39c5158Smillert ($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) ; 616b39c5158Smillert $status = $d->deflate($input, $output) ; 617b39c5158Smillert $status = $d->flush($output [, $flush_type]) ; 618b39c5158Smillert $d->deflateReset() ; 619b39c5158Smillert $d->deflateParams(OPTS) ; 620b39c5158Smillert $d->deflateTune(OPTS) ; 621b39c5158Smillert $d->dict_adler() ; 622b39c5158Smillert $d->crc32() ; 623b39c5158Smillert $d->adler32() ; 624b39c5158Smillert $d->total_in() ; 625b39c5158Smillert $d->total_out() ; 626b39c5158Smillert $d->msg() ; 627b39c5158Smillert $d->get_Strategy(); 628b39c5158Smillert $d->get_Level(); 629b39c5158Smillert $d->get_BufSize(); 630b39c5158Smillert 631b39c5158Smillert ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) ; 632b39c5158Smillert $status = $i->inflate($input, $output [, $eof]) ; 633b39c5158Smillert $status = $i->inflateSync($input) ; 634898184e3Ssthen $i->inflateReset() ; 635b39c5158Smillert $i->dict_adler() ; 636b39c5158Smillert $d->crc32() ; 637b39c5158Smillert $d->adler32() ; 638b39c5158Smillert $i->total_in() ; 639b39c5158Smillert $i->total_out() ; 640b39c5158Smillert $i->msg() ; 641b39c5158Smillert $d->get_BufSize(); 642b39c5158Smillert 643b39c5158Smillert $crc = adler32($buffer [,$crc]) ; 644b39c5158Smillert $crc = crc32($buffer [,$crc]) ; 645b39c5158Smillert 6466fb12b70Safresh1 $crc = crc32_combine($crc1, $crc2, $len2); 6476fb12b70Safresh1 $adler = adler32_combine($adler1, $adler2, $len2); 648b39c5158Smillert 649b39c5158Smillert my $version = Compress::Raw::Zlib::zlib_version(); 650898184e3Ssthen my $flags = Compress::Raw::Zlib::zlibCompileFlags(); 651b39c5158Smillert 652fdcd7346Safresh1 is_zlib_native(); 653fdcd7346Safresh1 is_zlibng_native(); 654fdcd7346Safresh1 is_zlibng_compat(); 655fdcd7346Safresh1 is_zlibng(); 656fdcd7346Safresh1 657b39c5158Smillert=head1 DESCRIPTION 658b39c5158Smillert 659fdcd7346Safresh1The I<Compress::Raw::Zlib> module provides a Perl interface to the I<zlib> or I<zlib-ng> 660fdcd7346Safresh1compression libraries (see L</SEE ALSO> for details about where to get 661fdcd7346Safresh1I<zlib> or I<zlib-ng>). 662fdcd7346Safresh1 663fdcd7346Safresh1In the text below all references to I<zlib> are also applicable to I<zlib-ng> unless otherwise stated. 664b39c5158Smillert 665b39c5158Smillert=head1 Compress::Raw::Zlib::Deflate 666b39c5158Smillert 667b39c5158SmillertThis section defines an interface that allows in-memory compression using 668b39c5158Smillertthe I<deflate> interface provided by zlib. 669b39c5158Smillert 670b39c5158SmillertHere is a definition of the interface available: 671b39c5158Smillert 672b39c5158Smillert=head2 B<($d, $status) = new Compress::Raw::Zlib::Deflate( [OPT] ) > 673b39c5158Smillert 674b39c5158SmillertInitialises a deflation object. 675b39c5158Smillert 676b39c5158SmillertIf you are familiar with the I<zlib> library, it combines the 677b39c5158Smillertfeatures of the I<zlib> functions C<deflateInit>, C<deflateInit2> 678b39c5158Smillertand C<deflateSetDictionary>. 679b39c5158Smillert 680b39c5158SmillertIf successful, it will return the initialised deflation object, C<$d> 681b39c5158Smillertand a C<$status> of C<Z_OK> in a list context. In scalar context it 682b39c5158Smillertreturns the deflation object, C<$d>, only. 683b39c5158Smillert 684b39c5158SmillertIf not successful, the returned deflation object, C<$d>, will be 685b39c5158SmillertI<undef> and C<$status> will hold the a I<zlib> error code. 686b39c5158Smillert 687b39c5158SmillertThe function optionally takes a number of named options specified as 688b39c5158SmillertC<< Name => value >> pairs. This allows individual options to be 689b39c5158Smillerttailored without having to specify them all in the parameter list. 690b39c5158Smillert 691b39c5158SmillertFor backward compatibility, it is also possible to pass the parameters 692b39c5158Smillertas a reference to a hash containing the name=>value pairs. 693b39c5158Smillert 694b39c5158SmillertBelow is a list of the valid options: 695b39c5158Smillert 696b39c5158Smillert=over 5 697b39c5158Smillert 698b39c5158Smillert=item B<-Level> 699b39c5158Smillert 700b39c5158SmillertDefines the compression level. Valid values are 0 through 9, 701b39c5158SmillertC<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and 702b39c5158SmillertC<Z_DEFAULT_COMPRESSION>. 703b39c5158Smillert 704b39c5158SmillertThe default is C<Z_DEFAULT_COMPRESSION>. 705b39c5158Smillert 706b39c5158Smillert=item B<-Method> 707b39c5158Smillert 708b39c5158SmillertDefines the compression method. The only valid value at present (and 709898184e3Ssthenthe default) is C<Z_DEFLATED>. 710b39c5158Smillert 711b39c5158Smillert=item B<-WindowBits> 712b39c5158Smillert 713b39c5158SmillertTo compress an RFC 1950 data stream, set C<WindowBits> to a positive 714b39c5158Smillertnumber between 8 and 15. 715b39c5158Smillert 716b39c5158SmillertTo compress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>. 717b39c5158Smillert 718b39c5158SmillertTo compress an RFC 1952 data stream (i.e. gzip), set C<WindowBits> to 719b39c5158SmillertC<WANT_GZIP>. 720b39c5158Smillert 721b39c5158SmillertFor a definition of the meaning and valid values for C<WindowBits> 722b39c5158Smillertrefer to the I<zlib> documentation for I<deflateInit2>. 723b39c5158Smillert 724b39c5158SmillertDefaults to C<MAX_WBITS>. 725b39c5158Smillert 726b39c5158Smillert=item B<-MemLevel> 727b39c5158Smillert 728b39c5158SmillertFor a definition of the meaning and valid values for C<MemLevel> 729b39c5158Smillertrefer to the I<zlib> documentation for I<deflateInit2>. 730b39c5158Smillert 731b39c5158SmillertDefaults to MAX_MEM_LEVEL. 732b39c5158Smillert 733b39c5158Smillert=item B<-Strategy> 734b39c5158Smillert 735b39c5158SmillertDefines the strategy used to tune the compression. The valid values are 736b39c5158SmillertC<Z_DEFAULT_STRATEGY>, C<Z_FILTERED>, C<Z_RLE>, C<Z_FIXED> and 737b39c5158SmillertC<Z_HUFFMAN_ONLY>. 738b39c5158Smillert 739898184e3SsthenThe default is C<Z_DEFAULT_STRATEGY>. 740b39c5158Smillert 741b39c5158Smillert=item B<-Dictionary> 742b39c5158Smillert 743b39c5158SmillertWhen a dictionary is specified I<Compress::Raw::Zlib> will automatically 744b39c5158Smillertcall C<deflateSetDictionary> directly after calling C<deflateInit>. The 745b39c5158SmillertAdler32 value for the dictionary can be obtained by calling the method 746b39c5158SmillertC<$d-E<gt>dict_adler()>. 747b39c5158Smillert 748b39c5158SmillertThe default is no dictionary. 749b39c5158Smillert 750b39c5158Smillert=item B<-Bufsize> 751b39c5158Smillert 752b39c5158SmillertSets the initial size for the output buffer used by the C<$d-E<gt>deflate> 753b39c5158Smillertand C<$d-E<gt>flush> methods. If the buffer has to be 754b39c5158Smillertreallocated to increase the size, it will grow in increments of 755b39c5158SmillertC<Bufsize>. 756b39c5158Smillert 757b39c5158SmillertThe default buffer size is 4096. 758b39c5158Smillert 759b39c5158Smillert=item B<-AppendOutput> 760b39c5158Smillert 761b39c5158SmillertThis option controls how data is written to the output buffer by the 762b39c5158SmillertC<$d-E<gt>deflate> and C<$d-E<gt>flush> methods. 763b39c5158Smillert 764b39c5158SmillertIf the C<AppendOutput> option is set to false, the output buffers in the 765b39c5158SmillertC<$d-E<gt>deflate> and C<$d-E<gt>flush> methods will be truncated before 766b39c5158Smillertuncompressed data is written to them. 767b39c5158Smillert 768b39c5158SmillertIf the option is set to true, uncompressed data will be appended to the 769b39c5158Smillertoutput buffer in the C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods. 770b39c5158Smillert 771b39c5158SmillertThis option defaults to false. 772b39c5158Smillert 773b39c5158Smillert=item B<-CRC32> 774b39c5158Smillert 775b39c5158SmillertIf set to true, a crc32 checksum of the uncompressed data will be 776b39c5158Smillertcalculated. Use the C<$d-E<gt>crc32> method to retrieve this value. 777b39c5158Smillert 778b39c5158SmillertThis option defaults to false. 779b39c5158Smillert 780b39c5158Smillert=item B<-ADLER32> 781b39c5158Smillert 782b39c5158SmillertIf set to true, an adler32 checksum of the uncompressed data will be 783b39c5158Smillertcalculated. Use the C<$d-E<gt>adler32> method to retrieve this value. 784b39c5158Smillert 785b39c5158SmillertThis option defaults to false. 786b39c5158Smillert 787b39c5158Smillert=back 788b39c5158Smillert 789b39c5158SmillertHere is an example of using the C<Compress::Raw::Zlib::Deflate> optional 790b39c5158Smillertparameter list to override the default buffer size and compression 791b39c5158Smillertlevel. All other options will take their default values. 792b39c5158Smillert 793b39c5158Smillert my $d = new Compress::Raw::Zlib::Deflate ( -Bufsize => 300, 794b39c5158Smillert -Level => Z_BEST_SPEED ) ; 795b39c5158Smillert 796b39c5158Smillert=head2 B<$status = $d-E<gt>deflate($input, $output)> 797b39c5158Smillert 798b39c5158SmillertDeflates the contents of C<$input> and writes the compressed data to 799b39c5158SmillertC<$output>. 800b39c5158Smillert 801b39c5158SmillertThe C<$input> and C<$output> parameters can be either scalars or scalar 802b39c5158Smillertreferences. 803b39c5158Smillert 804b39c5158SmillertWhen finished, C<$input> will be completely processed (assuming there 805b39c5158Smillertwere no errors). If the deflation was successful it writes the deflated 806b39c5158Smillertdata to C<$output> and returns a status value of C<Z_OK>. 807b39c5158Smillert 808b39c5158SmillertOn error, it returns a I<zlib> error code. 809b39c5158Smillert 810b39c5158SmillertIf the C<AppendOutput> option is set to true in the constructor for 811b39c5158Smillertthe C<$d> object, the compressed data will be appended to C<$output>. If 812b39c5158Smillertit is false, C<$output> will be truncated before any compressed data is 813b39c5158Smillertwritten to it. 814b39c5158Smillert 815b39c5158SmillertB<Note>: This method will not necessarily write compressed data to 816b39c5158SmillertC<$output> every time it is called. So do not assume that there has been 817b39c5158Smillertan error if the contents of C<$output> is empty on returning from 818b39c5158Smillertthis method. As long as the return code from the method is C<Z_OK>, 819b39c5158Smillertthe deflate has succeeded. 820b39c5158Smillert 821b39c5158Smillert=head2 B<$status = $d-E<gt>flush($output [, $flush_type]) > 822b39c5158Smillert 823b39c5158SmillertTypically used to finish the deflation. Any pending output will be 824b39c5158Smillertwritten to C<$output>. 825b39c5158Smillert 826b39c5158SmillertReturns C<Z_OK> if successful. 827b39c5158Smillert 828b39c5158SmillertNote that flushing can seriously degrade the compression ratio, so it 829b39c5158Smillertshould only be used to terminate a decompression (using C<Z_FINISH>) or 830b39c5158Smillertwhen you want to create a I<full flush point> (using C<Z_FULL_FLUSH>). 831b39c5158Smillert 832b39c5158SmillertBy default the C<flush_type> used is C<Z_FINISH>. Other valid values 833b39c5158Smillertfor C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH> 834b39c5158Smillertand C<Z_FULL_FLUSH>. It is strongly recommended that you only set the 835b39c5158SmillertC<flush_type> parameter if you fully understand the implications of 836b39c5158Smillertwhat it does. See the C<zlib> documentation for details. 837b39c5158Smillert 838b39c5158SmillertIf the C<AppendOutput> option is set to true in the constructor for 839b39c5158Smillertthe C<$d> object, the compressed data will be appended to C<$output>. If 840b39c5158Smillertit is false, C<$output> will be truncated before any compressed data is 841b39c5158Smillertwritten to it. 842b39c5158Smillert 843b39c5158Smillert=head2 B<$status = $d-E<gt>deflateReset() > 844b39c5158Smillert 845b39c5158SmillertThis method will reset the deflation object C<$d>. It can be used when you 846b39c5158Smillertare compressing multiple data streams and want to use the same object to 847b39c5158Smillertcompress each of them. It should only be used once the previous data stream 848b39c5158Smillerthas been flushed successfully, i.e. a call to C<< $d->flush(Z_FINISH) >> has 849b39c5158Smillertreturned C<Z_OK>. 850b39c5158Smillert 851b39c5158SmillertReturns C<Z_OK> if successful. 852b39c5158Smillert 853b39c5158Smillert=head2 B<$status = $d-E<gt>deflateParams([OPT])> 854b39c5158Smillert 855b39c5158SmillertChange settings for the deflate object C<$d>. 856b39c5158Smillert 857b39c5158SmillertThe list of the valid options is shown below. Options not specified 858b39c5158Smillertwill remain unchanged. 859b39c5158Smillert 860b39c5158Smillert=over 5 861b39c5158Smillert 862b39c5158Smillert=item B<-Level> 863b39c5158Smillert 864b39c5158SmillertDefines the compression level. Valid values are 0 through 9, 865b39c5158SmillertC<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and 866b39c5158SmillertC<Z_DEFAULT_COMPRESSION>. 867b39c5158Smillert 868b39c5158Smillert=item B<-Strategy> 869b39c5158Smillert 870b39c5158SmillertDefines the strategy used to tune the compression. The valid values are 871b39c5158SmillertC<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. 872b39c5158Smillert 873b39c5158Smillert=item B<-BufSize> 874b39c5158Smillert 875b39c5158SmillertSets the initial size for the output buffer used by the C<$d-E<gt>deflate> 876b39c5158Smillertand C<$d-E<gt>flush> methods. If the buffer has to be 877b39c5158Smillertreallocated to increase the size, it will grow in increments of 878b39c5158SmillertC<Bufsize>. 879b39c5158Smillert 880b39c5158Smillert=back 881b39c5158Smillert 882b39c5158Smillert=head2 B<$status = $d-E<gt>deflateTune($good_length, $max_lazy, $nice_length, $max_chain)> 883b39c5158Smillert 884b39c5158SmillertTune the internal settings for the deflate object C<$d>. This option is 885b39c5158Smillertonly available if you are running zlib 1.2.2.3 or better. 886b39c5158Smillert 887b39c5158SmillertRefer to the documentation in zlib.h for instructions on how to fly 888b39c5158SmillertC<deflateTune>. 889b39c5158Smillert 890b39c5158Smillert=head2 B<$d-E<gt>dict_adler()> 891b39c5158Smillert 892b39c5158SmillertReturns the adler32 value for the dictionary. 893b39c5158Smillert 894b39c5158Smillert=head2 B<$d-E<gt>crc32()> 895b39c5158Smillert 896b39c5158SmillertReturns the crc32 value for the uncompressed data to date. 897b39c5158Smillert 898b39c5158SmillertIf the C<CRC32> option is not enabled in the constructor for this object, 899b39c5158Smillertthis method will always return 0; 900b39c5158Smillert 901b39c5158Smillert=head2 B<$d-E<gt>adler32()> 902b39c5158Smillert 903b39c5158SmillertReturns the adler32 value for the uncompressed data to date. 904b39c5158Smillert 905b39c5158Smillert=head2 B<$d-E<gt>msg()> 906b39c5158Smillert 907b39c5158SmillertReturns the last error message generated by zlib. 908b39c5158Smillert 909b39c5158Smillert=head2 B<$d-E<gt>total_in()> 910b39c5158Smillert 911b39c5158SmillertReturns the total number of bytes uncompressed bytes input to deflate. 912b39c5158Smillert 913b39c5158Smillert=head2 B<$d-E<gt>total_out()> 914b39c5158Smillert 915b39c5158SmillertReturns the total number of compressed bytes output from deflate. 916b39c5158Smillert 917b39c5158Smillert=head2 B<$d-E<gt>get_Strategy()> 918b39c5158Smillert 919b39c5158SmillertReturns the deflation strategy currently used. Valid values are 920b39c5158SmillertC<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. 921b39c5158Smillert 922b39c5158Smillert=head2 B<$d-E<gt>get_Level()> 923b39c5158Smillert 924b39c5158SmillertReturns the compression level being used. 925b39c5158Smillert 926b39c5158Smillert=head2 B<$d-E<gt>get_BufSize()> 927b39c5158Smillert 928b39c5158SmillertReturns the buffer size used to carry out the compression. 929b39c5158Smillert 930b39c5158Smillert=head2 Example 931b39c5158Smillert 932b39c5158SmillertHere is a trivial example of using C<deflate>. It simply reads standard 933b39c5158Smillertinput, deflates it and writes it to standard output. 934b39c5158Smillert 935b39c5158Smillert use strict ; 936b39c5158Smillert use warnings ; 937b39c5158Smillert 938b39c5158Smillert use Compress::Raw::Zlib ; 939b39c5158Smillert 940b39c5158Smillert binmode STDIN; 941b39c5158Smillert binmode STDOUT; 942b39c5158Smillert my $x = new Compress::Raw::Zlib::Deflate 943b39c5158Smillert or die "Cannot create a deflation stream\n" ; 944b39c5158Smillert 945b39c5158Smillert my ($output, $status) ; 946b39c5158Smillert while (<>) 947b39c5158Smillert { 948b39c5158Smillert $status = $x->deflate($_, $output) ; 949b39c5158Smillert 950b39c5158Smillert $status == Z_OK 951b39c5158Smillert or die "deflation failed\n" ; 952b39c5158Smillert 953b39c5158Smillert print $output ; 954b39c5158Smillert } 955b39c5158Smillert 956b39c5158Smillert $status = $x->flush($output) ; 957b39c5158Smillert 958b39c5158Smillert $status == Z_OK 959b39c5158Smillert or die "deflation failed\n" ; 960b39c5158Smillert 961b39c5158Smillert print $output ; 962b39c5158Smillert 963b39c5158Smillert=head1 Compress::Raw::Zlib::Inflate 964b39c5158Smillert 965b39c5158SmillertThis section defines an interface that allows in-memory uncompression using 966b39c5158Smillertthe I<inflate> interface provided by zlib. 967b39c5158Smillert 968b39c5158SmillertHere is a definition of the interface: 969b39c5158Smillert 970b39c5158Smillert=head2 B< ($i, $status) = new Compress::Raw::Zlib::Inflate( [OPT] ) > 971b39c5158Smillert 972b39c5158SmillertInitialises an inflation object. 973b39c5158Smillert 974b39c5158SmillertIn a list context it returns the inflation object, C<$i>, and the 975b39c5158SmillertI<zlib> status code (C<$status>). In a scalar context it returns the 976b39c5158Smillertinflation object only. 977b39c5158Smillert 978b39c5158SmillertIf successful, C<$i> will hold the inflation object and C<$status> will 979b39c5158Smillertbe C<Z_OK>. 980b39c5158Smillert 981b39c5158SmillertIf not successful, C<$i> will be I<undef> and C<$status> will hold the 982b39c5158SmillertI<zlib> error code. 983b39c5158Smillert 984b39c5158SmillertThe function optionally takes a number of named options specified as 985b39c5158SmillertC<< -Name => value >> pairs. This allows individual options to be 986b39c5158Smillerttailored without having to specify them all in the parameter list. 987b39c5158Smillert 988b39c5158SmillertFor backward compatibility, it is also possible to pass the parameters 989b39c5158Smillertas a reference to a hash containing the C<< name=>value >> pairs. 990b39c5158Smillert 991b39c5158SmillertHere is a list of the valid options: 992b39c5158Smillert 993b39c5158Smillert=over 5 994b39c5158Smillert 995b39c5158Smillert=item B<-WindowBits> 996b39c5158Smillert 997b39c5158SmillertTo uncompress an RFC 1950 data stream, set C<WindowBits> to a positive 998b39c5158Smillertnumber between 8 and 15. 999b39c5158Smillert 1000b39c5158SmillertTo uncompress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>. 1001b39c5158Smillert 1002b39c5158SmillertTo uncompress an RFC 1952 data stream (i.e. gzip), set C<WindowBits> to 1003b39c5158SmillertC<WANT_GZIP>. 1004b39c5158Smillert 1005b39c5158SmillertTo auto-detect and uncompress an RFC 1950 or RFC 1952 data stream (i.e. 1006b39c5158Smillertgzip), set C<WindowBits> to C<WANT_GZIP_OR_ZLIB>. 1007b39c5158Smillert 1008b39c5158SmillertFor a full definition of the meaning and valid values for C<WindowBits> 1009b39c5158Smillertrefer to the I<zlib> documentation for I<inflateInit2>. 1010b39c5158Smillert 1011b39c5158SmillertDefaults to C<MAX_WBITS>. 1012b39c5158Smillert 1013b39c5158Smillert=item B<-Bufsize> 1014b39c5158Smillert 1015b39c5158SmillertSets the initial size for the output buffer used by the C<$i-E<gt>inflate> 1016b39c5158Smillertmethod. If the output buffer in this method has to be reallocated to 1017b39c5158Smillertincrease the size, it will grow in increments of C<Bufsize>. 1018b39c5158Smillert 1019b39c5158SmillertDefault is 4096. 1020b39c5158Smillert 1021b39c5158Smillert=item B<-Dictionary> 1022b39c5158Smillert 1023b39c5158SmillertThe default is no dictionary. 1024b39c5158Smillert 1025b39c5158Smillert=item B<-AppendOutput> 1026b39c5158Smillert 1027b39c5158SmillertThis option controls how data is written to the output buffer by the 1028b39c5158SmillertC<$i-E<gt>inflate> method. 1029b39c5158Smillert 1030b39c5158SmillertIf the option is set to false, the output buffer in the C<$i-E<gt>inflate> 1031b39c5158Smillertmethod will be truncated before uncompressed data is written to it. 1032b39c5158Smillert 1033b39c5158SmillertIf the option is set to true, uncompressed data will be appended to the 1034b39c5158Smillertoutput buffer by the C<$i-E<gt>inflate> method. 1035b39c5158Smillert 1036b39c5158SmillertThis option defaults to false. 1037b39c5158Smillert 1038b39c5158Smillert=item B<-CRC32> 1039b39c5158Smillert 1040b39c5158SmillertIf set to true, a crc32 checksum of the uncompressed data will be 1041b39c5158Smillertcalculated. Use the C<$i-E<gt>crc32> method to retrieve this value. 1042b39c5158Smillert 1043b39c5158SmillertThis option defaults to false. 1044b39c5158Smillert 1045b39c5158Smillert=item B<-ADLER32> 1046b39c5158Smillert 1047b39c5158SmillertIf set to true, an adler32 checksum of the uncompressed data will be 1048b39c5158Smillertcalculated. Use the C<$i-E<gt>adler32> method to retrieve this value. 1049b39c5158Smillert 1050b39c5158SmillertThis option defaults to false. 1051b39c5158Smillert 1052b39c5158Smillert=item B<-ConsumeInput> 1053b39c5158Smillert 1054b39c5158SmillertIf set to true, this option will remove compressed data from the input 1055b39c5158Smillertbuffer of the C<< $i->inflate >> method as the inflate progresses. 1056b39c5158Smillert 1057b39c5158SmillertThis option can be useful when you are processing compressed data that is 1058b39c5158Smillertembedded in another file/buffer. In this case the data that immediately 1059b39c5158Smillertfollows the compressed stream will be left in the input buffer. 1060b39c5158Smillert 1061b39c5158SmillertThis option defaults to true. 1062b39c5158Smillert 1063b39c5158Smillert=item B<-LimitOutput> 1064b39c5158Smillert 1065b39c5158SmillertThe C<LimitOutput> option changes the behavior of the C<< $i->inflate >> 1066b39c5158Smillertmethod so that the amount of memory used by the output buffer can be 1067b39c5158Smillertlimited. 1068b39c5158Smillert 1069b39c5158SmillertWhen C<LimitOutput> is used the size of the output buffer used will either 1070b39c5158Smillertbe the value of the C<Bufsize> option or the amount of memory already 1071b39c5158Smillertallocated to C<$output>, whichever is larger. Predicting the output size 1072b39c5158Smillertavailable is tricky, so don't rely on getting an exact output buffer size. 1073b39c5158Smillert 1074b39c5158SmillertWhen C<LimitOutout> is not specified C<< $i->inflate >> will use as much 1075b39c5158Smillertmemory as it takes to write all the uncompressed data it creates by 1076b39c5158Smillertuncompressing the input buffer. 1077b39c5158Smillert 1078b39c5158SmillertIf C<LimitOutput> is enabled, the C<ConsumeInput> option will also be 1079b39c5158Smillertenabled. 1080b39c5158Smillert 1081b39c5158SmillertThis option defaults to false. 1082b39c5158Smillert 1083b39c5158SmillertSee L</The LimitOutput option> for a discussion on why C<LimitOutput> is 1084b39c5158Smillertneeded and how to use it. 1085b39c5158Smillert 1086b39c5158Smillert=back 1087b39c5158Smillert 1088b39c5158SmillertHere is an example of using an optional parameter to override the default 1089b39c5158Smillertbuffer size. 1090b39c5158Smillert 1091b39c5158Smillert my ($i, $status) = new Compress::Raw::Zlib::Inflate( -Bufsize => 300 ) ; 1092b39c5158Smillert 1093b39c5158Smillert=head2 B< $status = $i-E<gt>inflate($input, $output [,$eof]) > 1094b39c5158Smillert 1095b39c5158SmillertInflates the complete contents of C<$input> and writes the uncompressed 1096b39c5158Smillertdata to C<$output>. The C<$input> and C<$output> parameters can either be 1097b39c5158Smillertscalars or scalar references. 1098b39c5158Smillert 1099b39c5158SmillertReturns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the 1100b39c5158Smillertcompressed data has been successfully reached. 1101b39c5158Smillert 1102b39c5158SmillertIf not successful C<$status> will hold the I<zlib> error code. 1103b39c5158Smillert 1104b39c5158SmillertIf the C<ConsumeInput> option has been set to true when the 1105b39c5158SmillertC<Compress::Raw::Zlib::Inflate> object is created, the C<$input> parameter 1106b39c5158Smillertis modified by C<inflate>. On completion it will contain what remains 1107b39c5158Smillertof the input buffer after inflation. In practice, this means that when 1108b39c5158Smillertthe return status is C<Z_OK> the C<$input> parameter will contain an 1109b39c5158Smillertempty string, and when the return status is C<Z_STREAM_END> the C<$input> 1110b39c5158Smillertparameter will contains what (if anything) was stored in the input buffer 1111b39c5158Smillertafter the deflated data stream. 1112b39c5158Smillert 1113b39c5158SmillertThis feature is useful when processing a file format that encapsulates 1114b39c5158Smillerta compressed data stream (e.g. gzip, zip) and there is useful data 1115b39c5158Smillertimmediately after the deflation stream. 1116b39c5158Smillert 1117b39c5158SmillertIf the C<AppendOutput> option is set to true in the constructor for 1118b39c5158Smillertthis object, the uncompressed data will be appended to C<$output>. If 1119b39c5158Smillertit is false, C<$output> will be truncated before any uncompressed data 1120b39c5158Smillertis written to it. 1121b39c5158Smillert 1122b39c5158SmillertThe C<$eof> parameter needs a bit of explanation. 1123b39c5158Smillert 1124b39c5158SmillertPrior to version 1.2.0, zlib assumed that there was at least one trailing 1125b39c5158Smillertbyte immediately after the compressed data stream when it was carrying out 1126b39c5158Smillertdecompression. This normally isn't a problem because the majority of zlib 1127b39c5158Smillertapplications guarantee that there will be data directly after the 1128b39c5158Smillertcompressed data stream. For example, both gzip (RFC 1950) and zip both 1129b39c5158Smillertdefine trailing data that follows the compressed data stream. 1130b39c5158Smillert 1131b39c5158SmillertThe C<$eof> parameter only needs to be used if B<all> of the following 1132b39c5158Smillertconditions apply 1133b39c5158Smillert 1134b39c5158Smillert=over 5 1135b39c5158Smillert 1136b39c5158Smillert=item 1 1137b39c5158Smillert 1138b39c5158SmillertYou are either using a copy of zlib that is older than version 1.2.0 or you 1139b39c5158Smillertwant your application code to be able to run with as many different 1140b39c5158Smillertversions of zlib as possible. 1141b39c5158Smillert 1142b39c5158Smillert=item 2 1143b39c5158Smillert 1144b39c5158SmillertYou have set the C<WindowBits> parameter to C<-MAX_WBITS> in the constructor 1145b39c5158Smillertfor this object, i.e. you are uncompressing a raw deflated data stream 1146b39c5158Smillert(RFC 1951). 1147b39c5158Smillert 1148b39c5158Smillert=item 3 1149b39c5158Smillert 1150b39c5158SmillertThere is no data immediately after the compressed data stream. 1151b39c5158Smillert 1152b39c5158Smillert=back 1153b39c5158Smillert 1154b39c5158SmillertIf B<all> of these are the case, then you need to set the C<$eof> parameter 1155b39c5158Smillertto true on the final call (and only the final call) to C<$i-E<gt>inflate>. 1156b39c5158Smillert 1157b39c5158SmillertIf you have built this module with zlib >= 1.2.0, the C<$eof> parameter is 1158b39c5158Smillertignored. You can still set it if you want, but it won't be used behind the 1159b39c5158Smillertscenes. 1160b39c5158Smillert 1161b39c5158Smillert=head2 B<$status = $i-E<gt>inflateSync($input)> 1162b39c5158Smillert 1163b39c5158SmillertThis method can be used to attempt to recover good data from a compressed 1164b39c5158Smillertdata stream that is partially corrupt. 1165b39c5158SmillertIt scans C<$input> until it reaches either a I<full flush point> or the 1166b39c5158Smillertend of the buffer. 1167b39c5158Smillert 1168b39c5158SmillertIf a I<full flush point> is found, C<Z_OK> is returned and C<$input> 1169b39c5158Smillertwill be have all data up to the flush point removed. This data can then be 1170b39c5158Smillertpassed to the C<$i-E<gt>inflate> method to be uncompressed. 1171b39c5158Smillert 1172b39c5158SmillertAny other return code means that a flush point was not found. If more 1173b39c5158Smillertdata is available, C<inflateSync> can be called repeatedly with more 1174b39c5158Smillertcompressed data until the flush point is found. 1175b39c5158Smillert 1176b39c5158SmillertNote I<full flush points> are not present by default in compressed 1177b39c5158Smillertdata streams. They must have been added explicitly when the data stream 1178b39c5158Smillertwas created by calling C<Compress::Deflate::flush> with C<Z_FULL_FLUSH>. 1179b39c5158Smillert 1180898184e3Ssthen=head2 B<$status = $i-E<gt>inflateReset() > 1181898184e3Ssthen 1182898184e3SsthenThis method will reset the inflation object C<$i>. It can be used when you 1183898184e3Ssthenare uncompressing multiple data streams and want to use the same object to 1184898184e3Ssthenuncompress each of them. 1185898184e3Ssthen 1186898184e3SsthenReturns C<Z_OK> if successful. 1187898184e3Ssthen 1188b39c5158Smillert=head2 B<$i-E<gt>dict_adler()> 1189b39c5158Smillert 1190b39c5158SmillertReturns the adler32 value for the dictionary. 1191b39c5158Smillert 1192b39c5158Smillert=head2 B<$i-E<gt>crc32()> 1193b39c5158Smillert 1194b39c5158SmillertReturns the crc32 value for the uncompressed data to date. 1195b39c5158Smillert 1196b39c5158SmillertIf the C<CRC32> option is not enabled in the constructor for this object, 1197b39c5158Smillertthis method will always return 0; 1198b39c5158Smillert 1199b39c5158Smillert=head2 B<$i-E<gt>adler32()> 1200b39c5158Smillert 1201b39c5158SmillertReturns the adler32 value for the uncompressed data to date. 1202b39c5158Smillert 1203b39c5158SmillertIf the C<ADLER32> option is not enabled in the constructor for this object, 1204b39c5158Smillertthis method will always return 0; 1205b39c5158Smillert 1206b39c5158Smillert=head2 B<$i-E<gt>msg()> 1207b39c5158Smillert 1208b39c5158SmillertReturns the last error message generated by zlib. 1209b39c5158Smillert 1210b39c5158Smillert=head2 B<$i-E<gt>total_in()> 1211b39c5158Smillert 1212b39c5158SmillertReturns the total number of bytes compressed bytes input to inflate. 1213b39c5158Smillert 1214b39c5158Smillert=head2 B<$i-E<gt>total_out()> 1215b39c5158Smillert 1216b39c5158SmillertReturns the total number of uncompressed bytes output from inflate. 1217b39c5158Smillert 1218b39c5158Smillert=head2 B<$d-E<gt>get_BufSize()> 1219b39c5158Smillert 1220b39c5158SmillertReturns the buffer size used to carry out the decompression. 1221b39c5158Smillert 1222b39c5158Smillert=head2 Examples 1223b39c5158Smillert 1224b39c5158SmillertHere is an example of using C<inflate>. 1225b39c5158Smillert 1226b39c5158Smillert use strict ; 1227b39c5158Smillert use warnings ; 1228b39c5158Smillert 1229b39c5158Smillert use Compress::Raw::Zlib; 1230b39c5158Smillert 1231b39c5158Smillert my $x = new Compress::Raw::Zlib::Inflate() 1232b39c5158Smillert or die "Cannot create a inflation stream\n" ; 1233b39c5158Smillert 1234b39c5158Smillert my $input = '' ; 1235b39c5158Smillert binmode STDIN; 1236b39c5158Smillert binmode STDOUT; 1237b39c5158Smillert 1238b39c5158Smillert my ($output, $status) ; 1239b39c5158Smillert while (read(STDIN, $input, 4096)) 1240b39c5158Smillert { 1241b39c5158Smillert $status = $x->inflate($input, $output) ; 1242b39c5158Smillert 1243b39c5158Smillert print $output ; 1244b39c5158Smillert 1245b39c5158Smillert last if $status != Z_OK ; 1246b39c5158Smillert } 1247b39c5158Smillert 1248b39c5158Smillert die "inflation failed\n" 1249b39c5158Smillert unless $status == Z_STREAM_END ; 1250b39c5158Smillert 1251b39c5158SmillertThe next example show how to use the C<LimitOutput> option. Notice the use 1252b39c5158Smillertof two nested loops in this case. The outer loop reads the data from the 1253b39c5158Smillertinput source - STDIN and the inner loop repeatedly calls C<inflate> until 1254b39c5158SmillertC<$input> is exhausted, we get an error, or the end of the stream is 1255b39c5158Smillertreached. One point worth remembering is by using the C<LimitOutput> option 1256b39c5158Smillertyou also get C<ConsumeInput> set as well - this makes the code below much 1257b39c5158Smillertsimpler. 1258b39c5158Smillert 1259b39c5158Smillert use strict ; 1260b39c5158Smillert use warnings ; 1261b39c5158Smillert 1262b39c5158Smillert use Compress::Raw::Zlib; 1263b39c5158Smillert 1264b39c5158Smillert my $x = new Compress::Raw::Zlib::Inflate(LimitOutput => 1) 1265b39c5158Smillert or die "Cannot create a inflation stream\n" ; 1266b39c5158Smillert 1267b39c5158Smillert my $input = '' ; 1268b39c5158Smillert binmode STDIN; 1269b39c5158Smillert binmode STDOUT; 1270b39c5158Smillert 1271b39c5158Smillert my ($output, $status) ; 1272b39c5158Smillert 1273b39c5158Smillert OUTER: 1274b39c5158Smillert while (read(STDIN, $input, 4096)) 1275b39c5158Smillert { 1276b39c5158Smillert do 1277b39c5158Smillert { 1278b39c5158Smillert $status = $x->inflate($input, $output) ; 1279b39c5158Smillert 1280b39c5158Smillert print $output ; 1281b39c5158Smillert 1282b39c5158Smillert last OUTER 1283b39c5158Smillert unless $status == Z_OK || $status == Z_BUF_ERROR ; 1284b39c5158Smillert } 1285*3d61058aSafresh1 while length $input; 1286b39c5158Smillert } 1287b39c5158Smillert 1288b39c5158Smillert die "inflation failed\n" 1289b39c5158Smillert unless $status == Z_STREAM_END ; 1290b39c5158Smillert 1291b39c5158Smillert=head1 CHECKSUM FUNCTIONS 1292b39c5158Smillert 1293b39c5158SmillertTwo functions are provided by I<zlib> to calculate checksums. For the 1294b39c5158SmillertPerl interface, the order of the two parameters in both functions has 1295b39c5158Smillertbeen reversed. This allows both running checksums and one off 1296b39c5158Smillertcalculations to be done. 1297b39c5158Smillert 1298b39c5158Smillert $crc = adler32($buffer [,$crc]) ; 1299b39c5158Smillert $crc = crc32($buffer [,$crc]) ; 1300b39c5158Smillert 1301b39c5158SmillertThe buffer parameters can either be a scalar or a scalar reference. 1302b39c5158Smillert 1303b39c5158SmillertIf the $crc parameters is C<undef>, the crc value will be reset. 1304b39c5158Smillert 1305b39c5158SmillertIf you have built this module with zlib 1.2.3 or better, two more 1306b39c5158SmillertCRC-related functions are available. 1307b39c5158Smillert 13086fb12b70Safresh1 $crc = crc32_combine($crc1, $crc2, $len2); 13096fb12b70Safresh1 $adler = adler32_combine($adler1, $adler2, $len2); 1310b39c5158Smillert 1311b39c5158SmillertThese functions allow checksums to be merged. 13126fb12b70Safresh1Refer to the I<zlib> documentation for more details. 1313b39c5158Smillert 1314b39c5158Smillert=head1 Misc 1315b39c5158Smillert 1316b39c5158Smillert=head2 my $version = Compress::Raw::Zlib::zlib_version(); 1317b39c5158Smillert 1318fdcd7346Safresh1Returns the version of the I<zlib> library if this module has been built with the I<zlib> library. 1319fdcd7346Safresh1If this module has been built with I<zlib-ng> in native mode, this function will return a empty string. 1320fdcd7346Safresh1If this module has been built with I<zlib-ng> in compat mode, this function will return the Izlib> API 1321fdcd7346Safresh1verion that I<zlib-ng> is supporting. 1322fdcd7346Safresh1 1323fdcd7346Safresh1=head2 my $version = Compress::Raw::Zlib::zlibng_version(); 1324fdcd7346Safresh1 1325fdcd7346Safresh1Returns the version of the zlib-ng library if this module has been built with the I<zlib-ng> library. 1326fdcd7346Safresh1If this module has been built with I<zlib>, this function will return a empty string. 1327b39c5158Smillert 1328898184e3Ssthen=head2 my $flags = Compress::Raw::Zlib::zlibCompileFlags(); 1329898184e3Ssthen 1330898184e3SsthenReturns the flags indicating compile-time options that were used to build 1331fdcd7346Safresh1the zlib or zlib-ng library. See the zlib documentation for a description of the flags 1332898184e3Ssthenreturned by C<zlibCompileFlags>. 1333898184e3Ssthen 1334898184e3SsthenNote that when the zlib sources are built along with this module the 1335898184e3SsthenC<sprintf> flags (bits 24, 25 and 26) should be ignored. 1336898184e3Ssthen 1337898184e3SsthenIf you are using zlib 1.2.0 or older, C<zlibCompileFlags> will return 0. 1338898184e3Ssthen 1339fdcd7346Safresh1=head2 is_zlib_native(); 1340fdcd7346Safresh1=head2 is_zlibng_native(); 1341fdcd7346Safresh1=head2 is_zlibng_compat(); 1342fdcd7346Safresh1=head2 is_zlibng(); 1343fdcd7346Safresh1 1344fdcd7346Safresh1These function can use used to check if C<Compress::Raw::Zlib> was been built with I<zlib> or I<zlib-ng>. 1345fdcd7346Safresh1 1346fdcd7346Safresh1The function C<is_zlib_native> returns true if C<Compress::Raw::Zlib> was built with I<zlib>. 1347fdcd7346Safresh1The function C<is_zlibng> returns true if C<Compress::Raw::Zlib> was built with I<zlib-ng>. 1348fdcd7346Safresh1 1349fdcd7346Safresh1The I<zlib-ng> library has an option to build with a zlib-compataible API. 1350fdcd7346Safresh1The c<is_zlibng_compat> function retuens true if zlib-ng has ben built with this API. 1351fdcd7346Safresh1 1352fdcd7346Safresh1Finally, C<is_zlibng_native> returns true if I<zlib-ng> was built with its native API. 1353fdcd7346Safresh1 1354b39c5158Smillert=head1 The LimitOutput option. 1355b39c5158Smillert 1356b39c5158SmillertBy default C<< $i->inflate($input, $output) >> will uncompress I<all> data 1357b39c5158Smillertin C<$input> and write I<all> of the uncompressed data it has generated to 1358b39c5158SmillertC<$output>. This makes the interface to C<inflate> much simpler - if the 1359b39c5158Smillertmethod has uncompressed C<$input> successfully I<all> compressed data in 1360b39c5158SmillertC<$input> will have been dealt with. So if you are reading from an input 1361b39c5158Smillertsource and uncompressing as you go the code will look something like this 1362b39c5158Smillert 1363b39c5158Smillert use strict ; 1364b39c5158Smillert use warnings ; 1365b39c5158Smillert 1366b39c5158Smillert use Compress::Raw::Zlib; 1367b39c5158Smillert 1368b39c5158Smillert my $x = new Compress::Raw::Zlib::Inflate() 1369b39c5158Smillert or die "Cannot create a inflation stream\n" ; 1370b39c5158Smillert 1371b39c5158Smillert my $input = '' ; 1372b39c5158Smillert 1373b39c5158Smillert my ($output, $status) ; 1374b39c5158Smillert while (read(STDIN, $input, 4096)) 1375b39c5158Smillert { 1376b39c5158Smillert $status = $x->inflate($input, $output) ; 1377b39c5158Smillert 1378b39c5158Smillert print $output ; 1379b39c5158Smillert 1380b39c5158Smillert last if $status != Z_OK ; 1381b39c5158Smillert } 1382b39c5158Smillert 1383b39c5158Smillert die "inflation failed\n" 1384b39c5158Smillert unless $status == Z_STREAM_END ; 1385b39c5158Smillert 1386b39c5158SmillertThe points to note are 1387b39c5158Smillert 1388b39c5158Smillert=over 5 1389b39c5158Smillert 1390b39c5158Smillert=item * 1391b39c5158Smillert 1392b39c5158SmillertThe main processing loop in the code handles reading of compressed data 1393b39c5158Smillertfrom STDIN. 1394b39c5158Smillert 1395b39c5158Smillert=item * 1396b39c5158Smillert 1397b39c5158SmillertThe status code returned from C<inflate> will only trigger termination of 1398b39c5158Smillertthe main processing loop if it isn't C<Z_OK>. When C<LimitOutput> has not 13996fb12b70Safresh1been used the C<Z_OK> status means that the end of the compressed 1400b39c5158Smillertdata stream has been reached or there has been an error in uncompression. 1401b39c5158Smillert 1402b39c5158Smillert=item * 1403b39c5158Smillert 1404b39c5158SmillertAfter the call to C<inflate> I<all> of the uncompressed data in C<$input> 1405b39c5158Smillertwill have been processed. This means the subsequent call to C<read> can 1406b39c5158Smillertoverwrite it's contents without any problem. 1407b39c5158Smillert 1408b39c5158Smillert=back 1409b39c5158Smillert 1410b39c5158SmillertFor most use-cases the behavior described above is acceptable (this module 1411b39c5158Smillertand it's predecessor, C<Compress::Zlib>, have used it for over 10 years 1412b39c5158Smillertwithout an issue), but in a few very specific use-cases the amount of 1413b39c5158Smillertmemory required for C<$output> can prohibitively large. For example, if the 1414b39c5158Smillertcompressed data stream contains the same pattern repeated thousands of 1415b39c5158Smillerttimes, a relatively small compressed data stream can uncompress into 1416b39c5158Smillerthundreds of megabytes. Remember C<inflate> will keep allocating memory 1417b39c5158Smillertuntil I<all> the uncompressed data has been written to the output buffer - 1418b39c5158Smillertthe size of C<$output> is unbounded. 1419b39c5158Smillert 1420b39c5158SmillertThe C<LimitOutput> option is designed to help with this use-case. 1421b39c5158Smillert 1422b39c5158SmillertThe main difference in your code when using C<LimitOutput> is having to 1423b39c5158Smillertdeal with cases where the C<$input> parameter still contains some 1424b39c5158Smillertuncompressed data that C<inflate> hasn't processed yet. The status code 1425b39c5158Smillertreturned from C<inflate> will be C<Z_OK> if uncompression took place and 1426b39c5158SmillertC<Z_BUF_ERROR> if the output buffer is full. 1427b39c5158Smillert 1428b39c5158SmillertBelow is typical code that shows how to use C<LimitOutput>. 1429b39c5158Smillert 1430b39c5158Smillert use strict ; 1431b39c5158Smillert use warnings ; 1432b39c5158Smillert 1433b39c5158Smillert use Compress::Raw::Zlib; 1434b39c5158Smillert 1435b39c5158Smillert my $x = new Compress::Raw::Zlib::Inflate(LimitOutput => 1) 1436b39c5158Smillert or die "Cannot create a inflation stream\n" ; 1437b39c5158Smillert 1438b39c5158Smillert my $input = '' ; 1439b39c5158Smillert binmode STDIN; 1440b39c5158Smillert binmode STDOUT; 1441b39c5158Smillert 1442b39c5158Smillert my ($output, $status) ; 1443b39c5158Smillert 1444b39c5158Smillert OUTER: 1445b39c5158Smillert while (read(STDIN, $input, 4096)) 1446b39c5158Smillert { 1447b39c5158Smillert do 1448b39c5158Smillert { 1449b39c5158Smillert $status = $x->inflate($input, $output) ; 1450b39c5158Smillert 1451b39c5158Smillert print $output ; 1452b39c5158Smillert 1453b39c5158Smillert last OUTER 1454b39c5158Smillert unless $status == Z_OK || $status == Z_BUF_ERROR ; 1455b39c5158Smillert } 1456*3d61058aSafresh1 while length $input; 1457b39c5158Smillert } 1458b39c5158Smillert 1459b39c5158Smillert die "inflation failed\n" 1460b39c5158Smillert unless $status == Z_STREAM_END ; 1461b39c5158Smillert 1462b39c5158SmillertPoints to note this time: 1463b39c5158Smillert 1464b39c5158Smillert=over 5 1465b39c5158Smillert 1466b39c5158Smillert=item * 1467b39c5158Smillert 1468b39c5158SmillertThere are now two nested loops in the code: the outer loop for reading the 1469b39c5158Smillertcompressed data from STDIN, as before; and the inner loop to carry out the 1470b39c5158Smillertuncompression. 1471b39c5158Smillert 1472b39c5158Smillert=item * 1473b39c5158Smillert 1474b39c5158SmillertThere are two exit points from the inner uncompression loop. 1475b39c5158Smillert 1476b39c5158SmillertFirstly when C<inflate> has returned a status other than C<Z_OK> or 1477b39c5158SmillertC<Z_BUF_ERROR>. This means that either the end of the compressed data 1478b39c5158Smillertstream has been reached (C<Z_STREAM_END>) or there is an error in the 1479b39c5158Smillertcompressed data. In either of these cases there is no point in continuing 1480b39c5158Smillertwith reading the compressed data, so both loops are terminated. 1481b39c5158Smillert 1482b39c5158SmillertThe second exit point tests if there is any data left in the input buffer, 1483b39c5158SmillertC<$input> - remember that the C<ConsumeInput> option is automatically 1484b39c5158Smillertenabled when C<LimitOutput> is used. When the input buffer has been 1485b39c5158Smillertexhausted, the outer loop can run again and overwrite a now empty 1486b39c5158SmillertC<$input>. 1487b39c5158Smillert 1488b39c5158Smillert=back 1489b39c5158Smillert 1490b39c5158Smillert=head1 ACCESSING ZIP FILES 1491b39c5158Smillert 1492b39c5158SmillertAlthough it is possible (with some effort on your part) to use this module 149391f110e0Safresh1to access .zip files, there are other perl modules available that will do 149491f110e0Safresh1all the hard work for you. Check out C<Archive::Zip>, 149591f110e0Safresh1C<Archive::Zip::SimpleZip>, C<IO::Compress::Zip> and 149691f110e0Safresh1C<IO::Uncompress::Unzip>. 1497b39c5158Smillert 1498898184e3Ssthen=head1 FAQ 1499898184e3Ssthen 1500898184e3Ssthen=head2 Compatibility with Unix compress/uncompress. 1501898184e3Ssthen 1502898184e3SsthenThis module is not compatible with Unix C<compress>. 1503898184e3Ssthen 1504898184e3SsthenIf you have the C<uncompress> program available, you can use this to read 1505898184e3Ssthencompressed files 1506898184e3Ssthen 1507898184e3Ssthen open F, "uncompress -c $filename |"; 1508898184e3Ssthen while (<F>) 1509898184e3Ssthen { 1510898184e3Ssthen ... 1511898184e3Ssthen 1512898184e3SsthenAlternatively, if you have the C<gunzip> program available, you can use 1513898184e3Ssthenthis to read compressed files 1514898184e3Ssthen 1515898184e3Ssthen open F, "gunzip -c $filename |"; 1516898184e3Ssthen while (<F>) 1517898184e3Ssthen { 1518898184e3Ssthen ... 1519898184e3Ssthen 1520898184e3Ssthenand this to write compress files, if you have the C<compress> program 1521898184e3Ssthenavailable 1522898184e3Ssthen 1523898184e3Ssthen open F, "| compress -c $filename "; 1524898184e3Ssthen print F "data"; 1525898184e3Ssthen ... 1526898184e3Ssthen close F ; 1527898184e3Ssthen 1528898184e3Ssthen=head2 Accessing .tar.Z files 1529898184e3Ssthen 1530898184e3SsthenSee previous FAQ item. 1531898184e3Ssthen 1532898184e3SsthenIf the C<Archive::Tar> module is installed and either the C<uncompress> or 1533898184e3SsthenC<gunzip> programs are available, you can use one of these workarounds to 1534898184e3Ssthenread C<.tar.Z> files. 1535898184e3Ssthen 1536898184e3SsthenFirstly with C<uncompress> 1537898184e3Ssthen 1538898184e3Ssthen use strict; 1539898184e3Ssthen use warnings; 1540898184e3Ssthen use Archive::Tar; 1541898184e3Ssthen 1542898184e3Ssthen open F, "uncompress -c $filename |"; 1543898184e3Ssthen my $tar = Archive::Tar->new(*F); 1544898184e3Ssthen ... 1545898184e3Ssthen 1546898184e3Ssthenand this with C<gunzip> 1547898184e3Ssthen 1548898184e3Ssthen use strict; 1549898184e3Ssthen use warnings; 1550898184e3Ssthen use Archive::Tar; 1551898184e3Ssthen 1552898184e3Ssthen open F, "gunzip -c $filename |"; 1553898184e3Ssthen my $tar = Archive::Tar->new(*F); 1554898184e3Ssthen ... 1555898184e3Ssthen 1556898184e3SsthenSimilarly, if the C<compress> program is available, you can use this to 1557898184e3Ssthenwrite a C<.tar.Z> file 1558898184e3Ssthen 1559898184e3Ssthen use strict; 1560898184e3Ssthen use warnings; 1561898184e3Ssthen use Archive::Tar; 1562898184e3Ssthen use IO::File; 1563898184e3Ssthen 1564898184e3Ssthen my $fh = new IO::File "| compress -c >$filename"; 1565898184e3Ssthen my $tar = Archive::Tar->new(); 1566898184e3Ssthen ... 1567898184e3Ssthen $tar->write($fh); 1568898184e3Ssthen $fh->close ; 1569898184e3Ssthen 1570898184e3Ssthen=head2 Zlib Library Version Support 1571898184e3Ssthen 1572898184e3SsthenBy default C<Compress::Raw::Zlib> will build with a private copy of version 1573898184e3Ssthen1.2.5 of the zlib library. (See the F<README> file for details of 1574898184e3Ssthenhow to override this behaviour) 1575898184e3Ssthen 1576898184e3SsthenIf you decide to use a different version of the zlib library, you need to be 1577898184e3Ssthenaware of the following issues 1578898184e3Ssthen 1579898184e3Ssthen=over 5 1580898184e3Ssthen 1581898184e3Ssthen=item * 1582898184e3Ssthen 1583898184e3SsthenFirst off, you must have zlib 1.0.5 or better. 1584898184e3Ssthen 1585898184e3Ssthen=item * 1586898184e3Ssthen 1587898184e3SsthenYou need to have zlib 1.2.1 or better if you want to use the C<-Merge> 1588898184e3Ssthenoption with C<IO::Compress::Gzip>, C<IO::Compress::Deflate> and 1589898184e3SsthenC<IO::Compress::RawDeflate>. 1590898184e3Ssthen 1591898184e3Ssthen=back 1592898184e3Ssthen 1593b39c5158Smillert=head1 CONSTANTS 1594b39c5158Smillert 1595b39c5158SmillertAll the I<zlib> constants are automatically imported when you make use 1596b39c5158Smillertof I<Compress::Raw::Zlib>. 1597b39c5158Smillert 159856d68f1eSafresh1=head1 SUPPORT 159956d68f1eSafresh1 160056d68f1eSafresh1General feedback/questions/bug reports should be sent to 160156d68f1eSafresh1L<https://github.com/pmqs/Compress-Raw-Zlib/issues> (preferred) or 160256d68f1eSafresh1L<https://rt.cpan.org/Public/Dist/Display.html?Name=Compress-Raw-Zlib>. 160356d68f1eSafresh1 1604b39c5158Smillert=head1 SEE ALSO 1605b39c5158Smillert 1606b46d8ef2Safresh1L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Uncompress::Inflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzip>, L<IO::Uncompress::UnLzip>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Compress::Zstd>, L<IO::Uncompress::UnZstd>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress> 1607b39c5158Smillert 1608898184e3SsthenL<IO::Compress::FAQ|IO::Compress::FAQ> 1609b39c5158Smillert 1610b39c5158SmillertL<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, 1611b39c5158SmillertL<Archive::Tar|Archive::Tar>, 1612b39c5158SmillertL<IO::Zlib|IO::Zlib> 1613b39c5158Smillert 1614b39c5158SmillertFor RFC 1950, 1951 and 1952 see 1615eac174f2Safresh1L<https://datatracker.ietf.org/doc/html/rfc1950>, 1616eac174f2Safresh1L<https://datatracker.ietf.org/doc/html/rfc1951> and 1617eac174f2Safresh1L<https://datatracker.ietf.org/doc/html/rfc1952> 1618b39c5158Smillert 1619b39c5158SmillertThe I<zlib> compression library was written by Jean-loup Gailly 16209f11ffb7Safresh1C<gzip@prep.ai.mit.edu> and Mark Adler C<madler@alumni.caltech.edu>. 1621b39c5158Smillert 1622b39c5158SmillertThe primary site for the I<zlib> compression library is 16239f11ffb7Safresh1L<http://www.zlib.org>. 1624b39c5158Smillert 1625fdcd7346Safresh1The primary site for the I<zlib-ng> compression library is 1626fdcd7346Safresh1L<https://github.com/zlib-ng/zlib-ng>. 1627fdcd7346Safresh1 16289f11ffb7Safresh1The primary site for gzip is L<http://www.gzip.org>. 1629b39c5158Smillert 1630b39c5158Smillert=head1 AUTHOR 1631b39c5158Smillert 16329f11ffb7Safresh1This module was written by Paul Marquess, C<pmqs@cpan.org>. 1633b39c5158Smillert 1634b39c5158Smillert=head1 MODIFICATION HISTORY 1635b39c5158Smillert 1636b39c5158SmillertSee the Changes file. 1637b39c5158Smillert 1638b39c5158Smillert=head1 COPYRIGHT AND LICENSE 1639b39c5158Smillert 1640*3d61058aSafresh1Copyright (c) 2005-2024 Paul Marquess. All rights reserved. 1641b39c5158Smillert 1642b39c5158SmillertThis program is free software; you can redistribute it and/or 1643b39c5158Smillertmodify it under the same terms as Perl itself. 1644