1b39c5158Smillert 2b39c5158Smillertpackage Compress::Zlib; 3b39c5158Smillert 4898184e3Ssthenrequire 5.006 ; 5b39c5158Smillertrequire Exporter; 6b39c5158Smillertuse Carp ; 7b39c5158Smillertuse IO::Handle ; 8b39c5158Smillertuse Scalar::Util qw(dualvar); 9b39c5158Smillert 10*3d61058aSafresh1use IO::Compress::Base::Common 2.212 ; 11*3d61058aSafresh1use Compress::Raw::Zlib 2.212 ; 12*3d61058aSafresh1use IO::Compress::Gzip 2.212 ; 13*3d61058aSafresh1use IO::Uncompress::Gunzip 2.212 ; 14b39c5158Smillert 15b39c5158Smillertuse strict ; 16b39c5158Smillertuse warnings ; 17b39c5158Smillertuse bytes ; 18898184e3Ssthenour ($VERSION, $XS_VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS); 19b39c5158Smillert 20*3d61058aSafresh1$VERSION = '2.212'; 21b39c5158Smillert$XS_VERSION = $VERSION; 22b39c5158Smillert$VERSION = eval $VERSION; 23b39c5158Smillert 24b39c5158Smillert@ISA = qw(Exporter); 25b39c5158Smillert# Items to export into callers namespace by default. Note: do not export 26b39c5158Smillert# names by default without a very good reason. Use EXPORT_OK instead. 27b39c5158Smillert# Do not simply export all your public functions/methods/constants. 28b39c5158Smillert@EXPORT = qw( 29b39c5158Smillert deflateInit inflateInit 30b39c5158Smillert 31b39c5158Smillert compress uncompress 32b39c5158Smillert 33b39c5158Smillert gzopen $gzerrno 34b39c5158Smillert ); 35b39c5158Smillert 36b39c5158Smillertpush @EXPORT, @Compress::Raw::Zlib::EXPORT ; 37b39c5158Smillert 38b39c5158Smillert@EXPORT_OK = qw(memGunzip memGzip zlib_version); 39b39c5158Smillert%EXPORT_TAGS = ( 40b39c5158Smillert ALL => \@EXPORT 41b39c5158Smillert); 42b39c5158Smillert 43b39c5158SmillertBEGIN 44b39c5158Smillert{ 45b39c5158Smillert *zlib_version = \&Compress::Raw::Zlib::zlib_version; 46b39c5158Smillert} 47b39c5158Smillert 48b39c5158Smillertuse constant FLAG_APPEND => 1 ; 49b39c5158Smillertuse constant FLAG_CRC => 2 ; 50b39c5158Smillertuse constant FLAG_ADLER => 4 ; 51b39c5158Smillertuse constant FLAG_CONSUME_INPUT => 8 ; 52b39c5158Smillert 53b39c5158Smillertour (@my_z_errmsg); 54b39c5158Smillert 55b39c5158Smillert@my_z_errmsg = ( 56b39c5158Smillert "need dictionary", # Z_NEED_DICT 2 57b39c5158Smillert "stream end", # Z_STREAM_END 1 58b39c5158Smillert "", # Z_OK 0 59b39c5158Smillert "file error", # Z_ERRNO (-1) 60b39c5158Smillert "stream error", # Z_STREAM_ERROR (-2) 61b39c5158Smillert "data error", # Z_DATA_ERROR (-3) 62b39c5158Smillert "insufficient memory", # Z_MEM_ERROR (-4) 63b39c5158Smillert "buffer error", # Z_BUF_ERROR (-5) 64b39c5158Smillert "incompatible version",# Z_VERSION_ERROR(-6) 65b39c5158Smillert ); 66b39c5158Smillert 67b39c5158Smillert 68b39c5158Smillertsub _set_gzerr 69b39c5158Smillert{ 70b39c5158Smillert my $value = shift ; 71b39c5158Smillert 72b39c5158Smillert if ($value == 0) { 73b39c5158Smillert $Compress::Zlib::gzerrno = 0 ; 74b39c5158Smillert } 75b39c5158Smillert elsif ($value == Z_ERRNO() || $value > 2) { 76b39c5158Smillert $Compress::Zlib::gzerrno = $! ; 77b39c5158Smillert } 78b39c5158Smillert else { 79b39c5158Smillert $Compress::Zlib::gzerrno = dualvar($value+0, $my_z_errmsg[2 - $value]); 80b39c5158Smillert } 81b39c5158Smillert 82b39c5158Smillert return $value ; 83b39c5158Smillert} 84b39c5158Smillert 85b39c5158Smillertsub _set_gzerr_undef 86b39c5158Smillert{ 87b39c5158Smillert _set_gzerr(@_); 88b39c5158Smillert return undef; 89b39c5158Smillert} 90898184e3Ssthen 91b39c5158Smillertsub _save_gzerr 92b39c5158Smillert{ 93b39c5158Smillert my $gz = shift ; 94b39c5158Smillert my $test_eof = shift ; 95b39c5158Smillert 96b39c5158Smillert my $value = $gz->errorNo() || 0 ; 97898184e3Ssthen my $eof = $gz->eof() ; 98b39c5158Smillert 99b39c5158Smillert if ($test_eof) { 100b39c5158Smillert # gzread uses Z_STREAM_END to denote a successful end 101b39c5158Smillert $value = Z_STREAM_END() if $gz->eof() && $value == 0 ; 102b39c5158Smillert } 103b39c5158Smillert 104b39c5158Smillert _set_gzerr($value) ; 105b39c5158Smillert} 106b39c5158Smillert 107b39c5158Smillertsub gzopen($$) 108b39c5158Smillert{ 109b39c5158Smillert my ($file, $mode) = @_ ; 110b39c5158Smillert 111b39c5158Smillert my $gz ; 112b39c5158Smillert my %defOpts = (Level => Z_DEFAULT_COMPRESSION(), 113b39c5158Smillert Strategy => Z_DEFAULT_STRATEGY(), 114b39c5158Smillert ); 115b39c5158Smillert 116b39c5158Smillert my $writing ; 117b39c5158Smillert $writing = ! ($mode =~ /r/i) ; 118b39c5158Smillert $writing = ($mode =~ /[wa]/i) ; 119b39c5158Smillert 120b39c5158Smillert $defOpts{Level} = $1 if $mode =~ /(\d)/; 121b39c5158Smillert $defOpts{Strategy} = Z_FILTERED() if $mode =~ /f/i; 122b39c5158Smillert $defOpts{Strategy} = Z_HUFFMAN_ONLY() if $mode =~ /h/i; 123b39c5158Smillert $defOpts{Append} = 1 if $mode =~ /a/i; 124b39c5158Smillert 125b39c5158Smillert my $infDef = $writing ? 'deflate' : 'inflate'; 126b39c5158Smillert my @params = () ; 127b39c5158Smillert 128b39c5158Smillert croak "gzopen: file parameter is not a filehandle or filename" 129b39c5158Smillert unless isaFilehandle $file || isaFilename $file || 130b39c5158Smillert (ref $file && ref $file eq 'SCALAR'); 131b39c5158Smillert 132b39c5158Smillert return undef unless $mode =~ /[rwa]/i ; 133b39c5158Smillert 134b39c5158Smillert _set_gzerr(0) ; 135b39c5158Smillert 136b39c5158Smillert if ($writing) { 137eac174f2Safresh1 $gz = IO::Compress::Gzip->new($file, Minimal => 1, AutoClose => 1, 138b39c5158Smillert %defOpts) 139b39c5158Smillert or $Compress::Zlib::gzerrno = $IO::Compress::Gzip::GzipError; 140b39c5158Smillert } 141b39c5158Smillert else { 142eac174f2Safresh1 $gz = IO::Uncompress::Gunzip->new($file, 143b39c5158Smillert Transparent => 1, 144b39c5158Smillert Append => 0, 145b39c5158Smillert AutoClose => 1, 146b39c5158Smillert MultiStream => 1, 147b39c5158Smillert Strict => 0) 148b39c5158Smillert or $Compress::Zlib::gzerrno = $IO::Uncompress::Gunzip::GunzipError; 149b39c5158Smillert } 150b39c5158Smillert 151b39c5158Smillert return undef 152b39c5158Smillert if ! defined $gz ; 153b39c5158Smillert 154b39c5158Smillert bless [$gz, $infDef], 'Compress::Zlib::gzFile'; 155b39c5158Smillert} 156b39c5158Smillert 157b39c5158Smillertsub Compress::Zlib::gzFile::gzread 158b39c5158Smillert{ 159b39c5158Smillert my $self = shift ; 160b39c5158Smillert 161b39c5158Smillert return _set_gzerr(Z_STREAM_ERROR()) 162b39c5158Smillert if $self->[1] ne 'inflate'; 163b39c5158Smillert 164b39c5158Smillert my $len = defined $_[1] ? $_[1] : 4096 ; 165b39c5158Smillert 166898184e3Ssthen my $gz = $self->[0] ; 167b39c5158Smillert if ($self->gzeof() || $len == 0) { 168b39c5158Smillert # Zap the output buffer to match ver 1 behaviour. 169b39c5158Smillert $_[0] = "" ; 170898184e3Ssthen _save_gzerr($gz, 1); 171b39c5158Smillert return 0 ; 172b39c5158Smillert } 173b39c5158Smillert 174b39c5158Smillert my $status = $gz->read($_[0], $len) ; 175b39c5158Smillert _save_gzerr($gz, 1); 176b39c5158Smillert return $status ; 177b39c5158Smillert} 178b39c5158Smillert 179b39c5158Smillertsub Compress::Zlib::gzFile::gzreadline 180b39c5158Smillert{ 181b39c5158Smillert my $self = shift ; 182b39c5158Smillert 183b39c5158Smillert my $gz = $self->[0] ; 184b39c5158Smillert { 185b39c5158Smillert # Maintain backward compatibility with 1.x behaviour 186b39c5158Smillert # It didn't support $/, so this can't either. 187b39c5158Smillert local $/ = "\n" ; 188b39c5158Smillert $_[0] = $gz->getline() ; 189b39c5158Smillert } 190b39c5158Smillert _save_gzerr($gz, 1); 191b39c5158Smillert return defined $_[0] ? length $_[0] : 0 ; 192b39c5158Smillert} 193b39c5158Smillert 194b39c5158Smillertsub Compress::Zlib::gzFile::gzwrite 195b39c5158Smillert{ 196b39c5158Smillert my $self = shift ; 197b39c5158Smillert my $gz = $self->[0] ; 198b39c5158Smillert 199b39c5158Smillert return _set_gzerr(Z_STREAM_ERROR()) 200b39c5158Smillert if $self->[1] ne 'deflate'; 201b39c5158Smillert 202b39c5158Smillert $] >= 5.008 and (utf8::downgrade($_[0], 1) 203b39c5158Smillert or croak "Wide character in gzwrite"); 204b39c5158Smillert 205b39c5158Smillert my $status = $gz->write($_[0]) ; 206b39c5158Smillert _save_gzerr($gz); 207b39c5158Smillert return $status ; 208b39c5158Smillert} 209b39c5158Smillert 210b39c5158Smillertsub Compress::Zlib::gzFile::gztell 211b39c5158Smillert{ 212b39c5158Smillert my $self = shift ; 213b39c5158Smillert my $gz = $self->[0] ; 214b39c5158Smillert my $status = $gz->tell() ; 215b39c5158Smillert _save_gzerr($gz); 216b39c5158Smillert return $status ; 217b39c5158Smillert} 218b39c5158Smillert 219b39c5158Smillertsub Compress::Zlib::gzFile::gzseek 220b39c5158Smillert{ 221b39c5158Smillert my $self = shift ; 222b39c5158Smillert my $offset = shift ; 223b39c5158Smillert my $whence = shift ; 224b39c5158Smillert 225b39c5158Smillert my $gz = $self->[0] ; 226b39c5158Smillert my $status ; 22756d68f1eSafresh1 eval { local $SIG{__DIE__}; $status = $gz->seek($offset, $whence) ; }; 228b39c5158Smillert if ($@) 229b39c5158Smillert { 230b39c5158Smillert my $error = $@; 231b39c5158Smillert $error =~ s/^.*: /gzseek: /; 232b39c5158Smillert $error =~ s/ at .* line \d+\s*$//; 233b39c5158Smillert croak $error; 234b39c5158Smillert } 235b39c5158Smillert _save_gzerr($gz); 236b39c5158Smillert return $status ; 237b39c5158Smillert} 238b39c5158Smillert 239b39c5158Smillertsub Compress::Zlib::gzFile::gzflush 240b39c5158Smillert{ 241b39c5158Smillert my $self = shift ; 242b39c5158Smillert my $f = shift ; 243b39c5158Smillert 244b39c5158Smillert my $gz = $self->[0] ; 245b39c5158Smillert my $status = $gz->flush($f) ; 246b39c5158Smillert my $err = _save_gzerr($gz); 247b39c5158Smillert return $status ? 0 : $err; 248b39c5158Smillert} 249b39c5158Smillert 250b39c5158Smillertsub Compress::Zlib::gzFile::gzclose 251b39c5158Smillert{ 252b39c5158Smillert my $self = shift ; 253b39c5158Smillert my $gz = $self->[0] ; 254b39c5158Smillert 255b39c5158Smillert my $status = $gz->close() ; 256b39c5158Smillert my $err = _save_gzerr($gz); 257b39c5158Smillert return $status ? 0 : $err; 258b39c5158Smillert} 259b39c5158Smillert 260b39c5158Smillertsub Compress::Zlib::gzFile::gzeof 261b39c5158Smillert{ 262b39c5158Smillert my $self = shift ; 263b39c5158Smillert my $gz = $self->[0] ; 264b39c5158Smillert 265b39c5158Smillert return 0 266b39c5158Smillert if $self->[1] ne 'inflate'; 267b39c5158Smillert 268b39c5158Smillert my $status = $gz->eof() ; 269b39c5158Smillert _save_gzerr($gz); 270b39c5158Smillert return $status ; 271b39c5158Smillert} 272b39c5158Smillert 273b39c5158Smillertsub Compress::Zlib::gzFile::gzsetparams 274b39c5158Smillert{ 275b39c5158Smillert my $self = shift ; 276b39c5158Smillert croak "Usage: Compress::Zlib::gzFile::gzsetparams(file, level, strategy)" 277b39c5158Smillert unless @_ eq 2 ; 278b39c5158Smillert 279b39c5158Smillert my $gz = $self->[0] ; 280b39c5158Smillert my $level = shift ; 281b39c5158Smillert my $strategy = shift; 282b39c5158Smillert 283b39c5158Smillert return _set_gzerr(Z_STREAM_ERROR()) 284b39c5158Smillert if $self->[1] ne 'deflate'; 285b39c5158Smillert 286b39c5158Smillert my $status = *$gz->{Compress}->deflateParams(-Level => $level, 287b39c5158Smillert -Strategy => $strategy); 288b39c5158Smillert _save_gzerr($gz); 289b39c5158Smillert return $status ; 290b39c5158Smillert} 291b39c5158Smillert 292b39c5158Smillertsub Compress::Zlib::gzFile::gzerror 293b39c5158Smillert{ 294b39c5158Smillert my $self = shift ; 295b39c5158Smillert my $gz = $self->[0] ; 296b39c5158Smillert 297b39c5158Smillert return $Compress::Zlib::gzerrno ; 298b39c5158Smillert} 299b39c5158Smillert 300b39c5158Smillert 301b39c5158Smillertsub compress($;$) 302b39c5158Smillert{ 303b39c5158Smillert my ($x, $output, $err, $in) =('', '', '', '') ; 304b39c5158Smillert 305b39c5158Smillert if (ref $_[0] ) { 306b39c5158Smillert $in = $_[0] ; 307b39c5158Smillert croak "not a scalar reference" unless ref $in eq 'SCALAR' ; 308b39c5158Smillert } 309b39c5158Smillert else { 310b39c5158Smillert $in = \$_[0] ; 311b39c5158Smillert } 312b39c5158Smillert 313b39c5158Smillert $] >= 5.008 and (utf8::downgrade($$in, 1) 314b39c5158Smillert or croak "Wide character in compress"); 315b39c5158Smillert 316b39c5158Smillert my $level = (@_ == 2 ? $_[1] : Z_DEFAULT_COMPRESSION() ); 317b39c5158Smillert 31891f110e0Safresh1 $x = Compress::Raw::Zlib::_deflateInit(FLAG_APPEND, 31991f110e0Safresh1 $level, 32091f110e0Safresh1 Z_DEFLATED, 32191f110e0Safresh1 MAX_WBITS, 32291f110e0Safresh1 MAX_MEM_LEVEL, 32391f110e0Safresh1 Z_DEFAULT_STRATEGY, 32491f110e0Safresh1 4096, 32591f110e0Safresh1 '') 326b39c5158Smillert or return undef ; 327b39c5158Smillert 328b39c5158Smillert $err = $x->deflate($in, $output) ; 329b39c5158Smillert return undef unless $err == Z_OK() ; 330b39c5158Smillert 331b39c5158Smillert $err = $x->flush($output) ; 332b39c5158Smillert return undef unless $err == Z_OK() ; 333b39c5158Smillert 334b39c5158Smillert return $output ; 335b39c5158Smillert} 336b39c5158Smillert 337b39c5158Smillertsub uncompress($) 338b39c5158Smillert{ 33991f110e0Safresh1 my ($output, $in) =('', '') ; 340b39c5158Smillert 341b39c5158Smillert if (ref $_[0] ) { 342b39c5158Smillert $in = $_[0] ; 343b39c5158Smillert croak "not a scalar reference" unless ref $in eq 'SCALAR' ; 344b39c5158Smillert } 345b39c5158Smillert else { 346b39c5158Smillert $in = \$_[0] ; 347b39c5158Smillert } 348b39c5158Smillert 349b39c5158Smillert $] >= 5.008 and (utf8::downgrade($$in, 1) 350b39c5158Smillert or croak "Wide character in uncompress"); 351b39c5158Smillert 35291f110e0Safresh1 my ($obj, $status) = Compress::Raw::Zlib::_inflateInit(0, 35391f110e0Safresh1 MAX_WBITS, 4096, "") ; 354b39c5158Smillert 35591f110e0Safresh1 $status == Z_OK 35691f110e0Safresh1 or return undef; 35791f110e0Safresh1 35891f110e0Safresh1 $obj->inflate($in, $output) == Z_STREAM_END 35991f110e0Safresh1 or return undef; 360b39c5158Smillert 361b39c5158Smillert return $output; 362b39c5158Smillert} 363b39c5158Smillert 364b39c5158Smillertsub deflateInit(@) 365b39c5158Smillert{ 366b39c5158Smillert my ($got) = ParseParameters(0, 367b39c5158Smillert { 36891f110e0Safresh1 'bufsize' => [IO::Compress::Base::Common::Parse_unsigned, 4096], 36991f110e0Safresh1 'level' => [IO::Compress::Base::Common::Parse_signed, Z_DEFAULT_COMPRESSION()], 37091f110e0Safresh1 'method' => [IO::Compress::Base::Common::Parse_unsigned, Z_DEFLATED()], 37191f110e0Safresh1 'windowbits' => [IO::Compress::Base::Common::Parse_signed, MAX_WBITS()], 37291f110e0Safresh1 'memlevel' => [IO::Compress::Base::Common::Parse_unsigned, MAX_MEM_LEVEL()], 37391f110e0Safresh1 'strategy' => [IO::Compress::Base::Common::Parse_unsigned, Z_DEFAULT_STRATEGY()], 37491f110e0Safresh1 'dictionary' => [IO::Compress::Base::Common::Parse_any, ""], 375b39c5158Smillert }, @_ ) ; 376b39c5158Smillert 377b39c5158Smillert croak "Compress::Zlib::deflateInit: Bufsize must be >= 1, you specified " . 37891f110e0Safresh1 $got->getValue('bufsize') 37991f110e0Safresh1 unless $got->getValue('bufsize') >= 1; 380b39c5158Smillert 381b39c5158Smillert my $obj ; 382b39c5158Smillert 383b39c5158Smillert my $status = 0 ; 384b39c5158Smillert ($obj, $status) = 385b39c5158Smillert Compress::Raw::Zlib::_deflateInit(0, 38691f110e0Safresh1 $got->getValue('level'), 38791f110e0Safresh1 $got->getValue('method'), 38891f110e0Safresh1 $got->getValue('windowbits'), 38991f110e0Safresh1 $got->getValue('memlevel'), 39091f110e0Safresh1 $got->getValue('strategy'), 39191f110e0Safresh1 $got->getValue('bufsize'), 39291f110e0Safresh1 $got->getValue('dictionary')) ; 393b39c5158Smillert 394b39c5158Smillert my $x = ($status == Z_OK() ? bless $obj, "Zlib::OldDeflate" : undef) ; 395b39c5158Smillert return wantarray ? ($x, $status) : $x ; 396b39c5158Smillert} 397b39c5158Smillert 398b39c5158Smillertsub inflateInit(@) 399b39c5158Smillert{ 400b39c5158Smillert my ($got) = ParseParameters(0, 401b39c5158Smillert { 40291f110e0Safresh1 'bufsize' => [IO::Compress::Base::Common::Parse_unsigned, 4096], 40391f110e0Safresh1 'windowbits' => [IO::Compress::Base::Common::Parse_signed, MAX_WBITS()], 40491f110e0Safresh1 'dictionary' => [IO::Compress::Base::Common::Parse_any, ""], 405b39c5158Smillert }, @_) ; 406b39c5158Smillert 407b39c5158Smillert 408b39c5158Smillert croak "Compress::Zlib::inflateInit: Bufsize must be >= 1, you specified " . 40991f110e0Safresh1 $got->getValue('bufsize') 41091f110e0Safresh1 unless $got->getValue('bufsize') >= 1; 411b39c5158Smillert 412b39c5158Smillert my $status = 0 ; 413b39c5158Smillert my $obj ; 414b39c5158Smillert ($obj, $status) = Compress::Raw::Zlib::_inflateInit(FLAG_CONSUME_INPUT, 41591f110e0Safresh1 $got->getValue('windowbits'), 41691f110e0Safresh1 $got->getValue('bufsize'), 41791f110e0Safresh1 $got->getValue('dictionary')) ; 418b39c5158Smillert 419b39c5158Smillert my $x = ($status == Z_OK() ? bless $obj, "Zlib::OldInflate" : undef) ; 420b39c5158Smillert 421b39c5158Smillert wantarray ? ($x, $status) : $x ; 422b39c5158Smillert} 423b39c5158Smillert 424b39c5158Smillertpackage Zlib::OldDeflate ; 425b39c5158Smillert 426b39c5158Smillertour (@ISA); 427b39c5158Smillert@ISA = qw(Compress::Raw::Zlib::deflateStream); 428b39c5158Smillert 429b39c5158Smillert 430b39c5158Smillertsub deflate 431b39c5158Smillert{ 432b39c5158Smillert my $self = shift ; 433b39c5158Smillert my $output ; 434b39c5158Smillert 435b39c5158Smillert my $status = $self->SUPER::deflate($_[0], $output) ; 436b39c5158Smillert wantarray ? ($output, $status) : $output ; 437b39c5158Smillert} 438b39c5158Smillert 439b39c5158Smillertsub flush 440b39c5158Smillert{ 441b39c5158Smillert my $self = shift ; 442b39c5158Smillert my $output ; 443b39c5158Smillert my $flag = shift || Compress::Zlib::Z_FINISH(); 444b39c5158Smillert my $status = $self->SUPER::flush($output, $flag) ; 445b39c5158Smillert 446b39c5158Smillert wantarray ? ($output, $status) : $output ; 447b39c5158Smillert} 448b39c5158Smillert 449b39c5158Smillertpackage Zlib::OldInflate ; 450b39c5158Smillert 451b39c5158Smillertour (@ISA); 452b39c5158Smillert@ISA = qw(Compress::Raw::Zlib::inflateStream); 453b39c5158Smillert 454b39c5158Smillertsub inflate 455b39c5158Smillert{ 456b39c5158Smillert my $self = shift ; 457b39c5158Smillert my $output ; 458b39c5158Smillert my $status = $self->SUPER::inflate($_[0], $output) ; 459b39c5158Smillert wantarray ? ($output, $status) : $output ; 460b39c5158Smillert} 461b39c5158Smillert 462b39c5158Smillertpackage Compress::Zlib ; 463b39c5158Smillert 464*3d61058aSafresh1use IO::Compress::Gzip::Constants 2.212 ; 465b39c5158Smillert 466b39c5158Smillertsub memGzip($) 467b39c5158Smillert{ 46891f110e0Safresh1 _set_gzerr(0); 46991f110e0Safresh1 my $x = Compress::Raw::Zlib::_deflateInit(FLAG_APPEND|FLAG_CRC, 47091f110e0Safresh1 Z_BEST_COMPRESSION, 47191f110e0Safresh1 Z_DEFLATED, 47291f110e0Safresh1 -MAX_WBITS(), 47391f110e0Safresh1 MAX_MEM_LEVEL, 47491f110e0Safresh1 Z_DEFAULT_STRATEGY, 47591f110e0Safresh1 4096, 47691f110e0Safresh1 '') 47791f110e0Safresh1 or return undef ; 478b39c5158Smillert 479b39c5158Smillert # if the deflation buffer isn't a reference, make it one 480b39c5158Smillert my $string = (ref $_[0] ? $_[0] : \$_[0]) ; 481b39c5158Smillert 482b39c5158Smillert $] >= 5.008 and (utf8::downgrade($$string, 1) 483b39c5158Smillert or croak "Wide character in memGzip"); 484b39c5158Smillert 48591f110e0Safresh1 my $out; 48691f110e0Safresh1 my $status ; 48791f110e0Safresh1 48891f110e0Safresh1 $x->deflate($string, $out) == Z_OK 48991f110e0Safresh1 or return undef ; 49091f110e0Safresh1 49191f110e0Safresh1 $x->flush($out) == Z_OK 49291f110e0Safresh1 or return undef ; 49391f110e0Safresh1 49491f110e0Safresh1 return IO::Compress::Gzip::Constants::GZIP_MINIMUM_HEADER . 49591f110e0Safresh1 $out . 49691f110e0Safresh1 pack("V V", $x->crc32(), $x->total_in()); 497b39c5158Smillert} 498b39c5158Smillert 499b39c5158Smillert 500b39c5158Smillertsub _removeGzipHeader($) 501b39c5158Smillert{ 502b39c5158Smillert my $string = shift ; 503b39c5158Smillert 504b39c5158Smillert return Z_DATA_ERROR() 505b39c5158Smillert if length($$string) < GZIP_MIN_HEADER_SIZE ; 506b39c5158Smillert 507b39c5158Smillert my ($magic1, $magic2, $method, $flags, $time, $xflags, $oscode) = 508b39c5158Smillert unpack ('CCCCVCC', $$string); 509b39c5158Smillert 510b39c5158Smillert return Z_DATA_ERROR() 511b39c5158Smillert unless $magic1 == GZIP_ID1 and $magic2 == GZIP_ID2 and 512b39c5158Smillert $method == Z_DEFLATED() and !($flags & GZIP_FLG_RESERVED) ; 513b39c5158Smillert substr($$string, 0, GZIP_MIN_HEADER_SIZE) = '' ; 514b39c5158Smillert 515b39c5158Smillert # skip extra field 516b39c5158Smillert if ($flags & GZIP_FLG_FEXTRA) 517b39c5158Smillert { 518b39c5158Smillert return Z_DATA_ERROR() 519b39c5158Smillert if length($$string) < GZIP_FEXTRA_HEADER_SIZE ; 520b39c5158Smillert 521b39c5158Smillert my ($extra_len) = unpack ('v', $$string); 522b39c5158Smillert $extra_len += GZIP_FEXTRA_HEADER_SIZE; 523b39c5158Smillert return Z_DATA_ERROR() 524b39c5158Smillert if length($$string) < $extra_len ; 525b39c5158Smillert 526b39c5158Smillert substr($$string, 0, $extra_len) = ''; 527b39c5158Smillert } 528b39c5158Smillert 529b39c5158Smillert # skip orig name 530b39c5158Smillert if ($flags & GZIP_FLG_FNAME) 531b39c5158Smillert { 532b39c5158Smillert my $name_end = index ($$string, GZIP_NULL_BYTE); 533b39c5158Smillert return Z_DATA_ERROR() 534b39c5158Smillert if $name_end == -1 ; 535b39c5158Smillert substr($$string, 0, $name_end + 1) = ''; 536b39c5158Smillert } 537b39c5158Smillert 538b39c5158Smillert # skip comment 539b39c5158Smillert if ($flags & GZIP_FLG_FCOMMENT) 540b39c5158Smillert { 541b39c5158Smillert my $comment_end = index ($$string, GZIP_NULL_BYTE); 542b39c5158Smillert return Z_DATA_ERROR() 543b39c5158Smillert if $comment_end == -1 ; 544b39c5158Smillert substr($$string, 0, $comment_end + 1) = ''; 545b39c5158Smillert } 546b39c5158Smillert 547b39c5158Smillert # skip header crc 548b39c5158Smillert if ($flags & GZIP_FLG_FHCRC) 549b39c5158Smillert { 550b39c5158Smillert return Z_DATA_ERROR() 551b39c5158Smillert if length ($$string) < GZIP_FHCRC_SIZE ; 552b39c5158Smillert substr($$string, 0, GZIP_FHCRC_SIZE) = ''; 553b39c5158Smillert } 554b39c5158Smillert 555b39c5158Smillert return Z_OK(); 556b39c5158Smillert} 557b39c5158Smillert 558b39c5158Smillertsub _ret_gun_error 559b39c5158Smillert{ 560b39c5158Smillert $Compress::Zlib::gzerrno = $IO::Uncompress::Gunzip::GunzipError; 561b39c5158Smillert return undef; 562b39c5158Smillert} 563b39c5158Smillert 564b39c5158Smillert 565b39c5158Smillertsub memGunzip($) 566b39c5158Smillert{ 567b39c5158Smillert # if the buffer isn't a reference, make it one 568b39c5158Smillert my $string = (ref $_[0] ? $_[0] : \$_[0]); 569b39c5158Smillert 570b39c5158Smillert $] >= 5.008 and (utf8::downgrade($$string, 1) 571b39c5158Smillert or croak "Wide character in memGunzip"); 572b39c5158Smillert 573b39c5158Smillert _set_gzerr(0); 574b39c5158Smillert 575b39c5158Smillert my $status = _removeGzipHeader($string) ; 576b39c5158Smillert $status == Z_OK() 577b39c5158Smillert or return _set_gzerr_undef($status); 578b39c5158Smillert 579b39c5158Smillert my $bufsize = length $$string > 4096 ? length $$string : 4096 ; 58091f110e0Safresh1 my $x = Compress::Raw::Zlib::_inflateInit(FLAG_CRC | FLAG_CONSUME_INPUT, 58191f110e0Safresh1 -MAX_WBITS(), $bufsize, '') 582b39c5158Smillert or return _ret_gun_error(); 583b39c5158Smillert 58491f110e0Safresh1 my $output = '' ; 585b39c5158Smillert $status = $x->inflate($string, $output); 586b39c5158Smillert 587b39c5158Smillert if ( $status == Z_OK() ) 588b39c5158Smillert { 589b39c5158Smillert _set_gzerr(Z_DATA_ERROR()); 590b39c5158Smillert return undef; 591b39c5158Smillert } 592b39c5158Smillert 593b39c5158Smillert return _ret_gun_error() 594b39c5158Smillert if ($status != Z_STREAM_END()); 595b39c5158Smillert 596b39c5158Smillert if (length $$string >= 8) 597b39c5158Smillert { 598b39c5158Smillert my ($crc, $len) = unpack ("VV", substr($$string, 0, 8)); 599b39c5158Smillert substr($$string, 0, 8) = ''; 600b39c5158Smillert return _set_gzerr_undef(Z_DATA_ERROR()) 601b39c5158Smillert unless $len == length($output) and 602898184e3Ssthen $crc == Compress::Raw::Zlib::crc32($output); 603b39c5158Smillert } 604b39c5158Smillert else 605b39c5158Smillert { 606b39c5158Smillert $$string = ''; 607b39c5158Smillert } 608b39c5158Smillert 609b39c5158Smillert return $output; 610b39c5158Smillert} 611b39c5158Smillert 612b39c5158Smillert# Autoload methods go after __END__, and are processed by the autosplit program. 613b39c5158Smillert 614b39c5158Smillert1; 615b39c5158Smillert__END__ 616b39c5158Smillert 617b39c5158Smillert 618b39c5158Smillert=head1 NAME 619b39c5158Smillert 620b39c5158SmillertCompress::Zlib - Interface to zlib compression library 621b39c5158Smillert 622b39c5158Smillert=head1 SYNOPSIS 623b39c5158Smillert 624b39c5158Smillert use Compress::Zlib ; 625b39c5158Smillert 626b39c5158Smillert ($d, $status) = deflateInit( [OPT] ) ; 627b39c5158Smillert $status = $d->deflate($input, $output) ; 628b39c5158Smillert $status = $d->flush([$flush_type]) ; 629b39c5158Smillert $d->deflateParams(OPTS) ; 630b39c5158Smillert $d->deflateTune(OPTS) ; 631b39c5158Smillert $d->dict_adler() ; 632b39c5158Smillert $d->crc32() ; 633b39c5158Smillert $d->adler32() ; 634b39c5158Smillert $d->total_in() ; 635b39c5158Smillert $d->total_out() ; 636b39c5158Smillert $d->msg() ; 637b39c5158Smillert $d->get_Strategy(); 638b39c5158Smillert $d->get_Level(); 639b39c5158Smillert $d->get_BufSize(); 640b39c5158Smillert 641b39c5158Smillert ($i, $status) = inflateInit( [OPT] ) ; 642b39c5158Smillert $status = $i->inflate($input, $output [, $eof]) ; 643b39c5158Smillert $status = $i->inflateSync($input) ; 644b39c5158Smillert $i->dict_adler() ; 645b39c5158Smillert $d->crc32() ; 646b39c5158Smillert $d->adler32() ; 647b39c5158Smillert $i->total_in() ; 648b39c5158Smillert $i->total_out() ; 649b39c5158Smillert $i->msg() ; 650b39c5158Smillert $d->get_BufSize(); 651b39c5158Smillert 652b39c5158Smillert $dest = compress($source) ; 653b39c5158Smillert $dest = uncompress($source) ; 654b39c5158Smillert 655b39c5158Smillert $gz = gzopen($filename or filehandle, $mode) ; 656b39c5158Smillert $bytesread = $gz->gzread($buffer [,$size]) ; 657b39c5158Smillert $bytesread = $gz->gzreadline($line) ; 658b39c5158Smillert $byteswritten = $gz->gzwrite($buffer) ; 659b39c5158Smillert $status = $gz->gzflush($flush) ; 660b39c5158Smillert $offset = $gz->gztell() ; 661b39c5158Smillert $status = $gz->gzseek($offset, $whence) ; 662b39c5158Smillert $status = $gz->gzclose() ; 663b39c5158Smillert $status = $gz->gzeof() ; 664b39c5158Smillert $status = $gz->gzsetparams($level, $strategy) ; 665b39c5158Smillert $errstring = $gz->gzerror() ; 666b39c5158Smillert $gzerrno 667b39c5158Smillert 668b39c5158Smillert $dest = Compress::Zlib::memGzip($buffer) ; 669b39c5158Smillert $dest = Compress::Zlib::memGunzip($buffer) ; 670b39c5158Smillert 671b39c5158Smillert $crc = adler32($buffer [,$crc]) ; 672b39c5158Smillert $crc = crc32($buffer [,$crc]) ; 673b39c5158Smillert 6746fb12b70Safresh1 $crc = crc32_combine($crc1, $crc2, $len2); 6756fb12b70Safresh1 $adler = adler32_combine($adler1, $adler2, $len2); 676b39c5158Smillert 677b39c5158Smillert my $version = Compress::Raw::Zlib::zlib_version(); 678b39c5158Smillert 679b39c5158Smillert=head1 DESCRIPTION 680b39c5158Smillert 681b39c5158SmillertThe I<Compress::Zlib> module provides a Perl interface to the I<zlib> 682b39c5158Smillertcompression library (see L</AUTHOR> for details about where to get 683b39c5158SmillertI<zlib>). 684b39c5158Smillert 685b39c5158SmillertThe C<Compress::Zlib> module can be split into two general areas of 686b39c5158Smillertfunctionality, namely a simple read/write interface to I<gzip> files 687b39c5158Smillertand a low-level in-memory compression/decompression interface. 688b39c5158Smillert 689b39c5158SmillertEach of these areas will be discussed in the following sections. 690b39c5158Smillert 691b39c5158Smillert=head2 Notes for users of Compress::Zlib version 1 692b39c5158Smillert 693b39c5158SmillertThe main change in C<Compress::Zlib> version 2.x is that it does not now 694b39c5158Smillertinterface directly to the zlib library. Instead it uses the 695b39c5158SmillertC<IO::Compress::Gzip> and C<IO::Uncompress::Gunzip> modules for 696b39c5158Smillertreading/writing gzip files, and the C<Compress::Raw::Zlib> module for some 697b39c5158Smillertlow-level zlib access. 698b39c5158Smillert 699b39c5158SmillertThe interface provided by version 2 of this module should be 100% backward 700b39c5158Smillertcompatible with version 1. If you find a difference in the expected 701b39c5158Smillertbehaviour please contact the author (See L</AUTHOR>). See L<GZIP INTERFACE> 702b39c5158Smillert 703b39c5158SmillertWith the creation of the C<IO::Compress> and C<IO::Uncompress> modules no 704b39c5158Smillertnew features are planned for C<Compress::Zlib> - the new modules do 705b39c5158Smillerteverything that C<Compress::Zlib> does and then some. Development on 706b39c5158SmillertC<Compress::Zlib> will be limited to bug fixes only. 707b39c5158Smillert 708b39c5158SmillertIf you are writing new code, your first port of call should be one of the 709b39c5158Smillertnew C<IO::Compress> or C<IO::Uncompress> modules. 710b39c5158Smillert 711b39c5158Smillert=head1 GZIP INTERFACE 712b39c5158Smillert 713b39c5158SmillertA number of functions are supplied in I<zlib> for reading and writing 714b39c5158SmillertI<gzip> files that conform to RFC 1952. This module provides an interface 715b39c5158Smillertto most of them. 716b39c5158Smillert 717b39c5158SmillertIf you have previously used C<Compress::Zlib> 1.x, the following 718b39c5158Smillertenhancements/changes have been made to the C<gzopen> interface: 719b39c5158Smillert 720b39c5158Smillert=over 5 721b39c5158Smillert 722b39c5158Smillert=item 1 723b39c5158Smillert 724898184e3SsthenIf you want to open either STDIN or STDOUT with C<gzopen>, you can now 725b39c5158Smillertoptionally use the special filename "C<->" as a synonym for C<\*STDIN> and 726b39c5158SmillertC<\*STDOUT>. 727b39c5158Smillert 728b39c5158Smillert=item 2 729b39c5158Smillert 730b39c5158SmillertIn C<Compress::Zlib> version 1.x, C<gzopen> used the zlib library to open 731b39c5158Smillertthe underlying file. This made things especially tricky when a Perl 732b39c5158Smillertfilehandle was passed to C<gzopen>. Behind the scenes the numeric C file 733b39c5158Smillertdescriptor had to be extracted from the Perl filehandle and this passed to 734b39c5158Smillertthe zlib library. 735b39c5158Smillert 736b39c5158SmillertApart from being non-portable to some operating systems, this made it 737b39c5158Smillertdifficult to use C<gzopen> in situations where you wanted to extract/create 738b39c5158Smillerta gzip data stream that is embedded in a larger file, without having to 739b39c5158Smillertresort to opening and closing the file multiple times. 740b39c5158Smillert 741b39c5158SmillertIt also made it impossible to pass a perl filehandle that wasn't associated 742b39c5158Smillertwith a real filesystem file, like, say, an C<IO::String>. 743b39c5158Smillert 744b39c5158SmillertIn C<Compress::Zlib> version 2.x, the C<gzopen> interface has been 745b39c5158Smillertcompletely rewritten to use the L<IO::Compress::Gzip|IO::Compress::Gzip> 746b39c5158Smillertfor writing gzip files and L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip> 747b39c5158Smillertfor reading gzip files. None of the limitations mentioned above apply. 748b39c5158Smillert 749b39c5158Smillert=item 3 750b39c5158Smillert 751b39c5158SmillertAddition of C<gzseek> to provide a restricted C<seek> interface. 752b39c5158Smillert 753b39c5158Smillert=item 4. 754b39c5158Smillert 755b39c5158SmillertAdded C<gztell>. 756b39c5158Smillert 757b39c5158Smillert=back 758b39c5158Smillert 759b39c5158SmillertA more complete and flexible interface for reading/writing gzip 760b39c5158Smillertfiles/buffers is included with the module C<IO-Compress-Zlib>. See 761b39c5158SmillertL<IO::Compress::Gzip|IO::Compress::Gzip> and 762b39c5158SmillertL<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip> for more details. 763b39c5158Smillert 764b39c5158Smillert=over 5 765b39c5158Smillert 766b39c5158Smillert=item B<$gz = gzopen($filename, $mode)> 767b39c5158Smillert 768b39c5158Smillert=item B<$gz = gzopen($filehandle, $mode)> 769b39c5158Smillert 770b39c5158SmillertThis function opens either the I<gzip> file C<$filename> for reading or 771b39c5158Smillertwriting or attaches to the opened filehandle, C<$filehandle>. 772b39c5158SmillertIt returns an object on success and C<undef> on failure. 773b39c5158Smillert 774b39c5158SmillertWhen writing a gzip file this interface will I<always> create the smallest 775b39c5158Smillertpossible gzip header (exactly 10 bytes). If you want greater control over 776b39c5158Smillertwhat gets stored in the gzip header (like the original filename or a 777b39c5158Smillertcomment) use L<IO::Compress::Gzip|IO::Compress::Gzip> instead. Similarly if 778b39c5158Smillertyou want to read the contents of the gzip header use 779b39c5158SmillertL<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>. 780b39c5158Smillert 781b39c5158SmillertThe second parameter, C<$mode>, is used to specify whether the file is 782b39c5158Smillertopened for reading or writing and to optionally specify a compression 783b39c5158Smillertlevel and compression strategy when writing. The format of the C<$mode> 784b39c5158Smillertparameter is similar to the mode parameter to the 'C' function C<fopen>, 785b39c5158Smillertso "rb" is used to open for reading, "wb" for writing and "ab" for 786b39c5158Smillertappending (writing at the end of the file). 787b39c5158Smillert 788b39c5158SmillertTo specify a compression level when writing, append a digit between 0 789b39c5158Smillertand 9 to the mode string -- 0 means no compression and 9 means maximum 790b39c5158Smillertcompression. 791b39c5158SmillertIf no compression level is specified Z_DEFAULT_COMPRESSION is used. 792b39c5158Smillert 793b39c5158SmillertTo specify the compression strategy when writing, append 'f' for filtered 794b39c5158Smillertdata, 'h' for Huffman only compression, or 'R' for run-length encoding. 795b39c5158SmillertIf no strategy is specified Z_DEFAULT_STRATEGY is used. 796b39c5158Smillert 797b39c5158SmillertSo, for example, "wb9" means open for writing with the maximum compression 798b39c5158Smillertusing the default strategy and "wb4R" means open for writing with compression 799b39c5158Smillertlevel 4 and run-length encoding. 800b39c5158Smillert 801b39c5158SmillertRefer to the I<zlib> documentation for the exact format of the C<$mode> 802b39c5158Smillertparameter. 803b39c5158Smillert 804b39c5158Smillert=item B<$bytesread = $gz-E<gt>gzread($buffer [, $size]) ;> 805b39c5158Smillert 806b39c5158SmillertReads C<$size> bytes from the compressed file into C<$buffer>. If 807b39c5158SmillertC<$size> is not specified, it will default to 4096. If the scalar 808b39c5158SmillertC<$buffer> is not large enough, it will be extended automatically. 809b39c5158Smillert 810b39c5158SmillertReturns the number of bytes actually read. On EOF it returns 0 and in 811b39c5158Smillertthe case of an error, -1. 812b39c5158Smillert 813b39c5158Smillert=item B<$bytesread = $gz-E<gt>gzreadline($line) ;> 814b39c5158Smillert 815b39c5158SmillertReads the next line from the compressed file into C<$line>. 816b39c5158Smillert 817b39c5158SmillertReturns the number of bytes actually read. On EOF it returns 0 and in 818b39c5158Smillertthe case of an error, -1. 819b39c5158Smillert 820b39c5158SmillertIt is legal to intermix calls to C<gzread> and C<gzreadline>. 821b39c5158Smillert 822b39c5158SmillertTo maintain backward compatibility with version 1.x of this module 823b39c5158SmillertC<gzreadline> ignores the C<$/> variable - it I<always> uses the string 824b39c5158SmillertC<"\n"> as the line delimiter. 825b39c5158Smillert 826b39c5158SmillertIf you want to read a gzip file a line at a time and have it respect the 827b39c5158SmillertC<$/> variable (or C<$INPUT_RECORD_SEPARATOR>, or C<$RS> when C<English> is 828b39c5158Smillertin use) see L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip>. 829b39c5158Smillert 830b39c5158Smillert=item B<$byteswritten = $gz-E<gt>gzwrite($buffer) ;> 831b39c5158Smillert 832b39c5158SmillertWrites the contents of C<$buffer> to the compressed file. Returns the 833b39c5158Smillertnumber of bytes actually written, or 0 on error. 834b39c5158Smillert 835b39c5158Smillert=item B<$status = $gz-E<gt>gzflush($flush_type) ;> 836b39c5158Smillert 837b39c5158SmillertFlushes all pending output into the compressed file. 838b39c5158Smillert 839b39c5158SmillertThis method takes an optional parameter, C<$flush_type>, that controls 840b39c5158Smillerthow the flushing will be carried out. By default the C<$flush_type> 841b39c5158Smillertused is C<Z_FINISH>. Other valid values for C<$flush_type> are 842b39c5158SmillertC<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is 843b39c5158Smillertstrongly recommended that you only set the C<flush_type> parameter if 844b39c5158Smillertyou fully understand the implications of what it does - overuse of C<flush> 845b39c5158Smillertcan seriously degrade the level of compression achieved. See the C<zlib> 846b39c5158Smillertdocumentation for details. 847b39c5158Smillert 848b39c5158SmillertReturns 0 on success. 849b39c5158Smillert 850b39c5158Smillert=item B<$offset = $gz-E<gt>gztell() ;> 851b39c5158Smillert 852b39c5158SmillertReturns the uncompressed file offset. 853b39c5158Smillert 854b39c5158Smillert=item B<$status = $gz-E<gt>gzseek($offset, $whence) ;> 855b39c5158Smillert 856b39c5158SmillertProvides a sub-set of the C<seek> functionality, with the restriction 857b39c5158Smillertthat it is only legal to seek forward in the compressed file. 858b39c5158SmillertIt is a fatal error to attempt to seek backward. 859b39c5158Smillert 860b39c5158SmillertWhen opened for writing, empty parts of the file will have NULL (0x00) 861b39c5158Smillertbytes written to them. 862b39c5158Smillert 863b39c5158SmillertThe C<$whence> parameter should be one of SEEK_SET, SEEK_CUR or SEEK_END. 864b39c5158Smillert 865b39c5158SmillertReturns 1 on success, 0 on failure. 866b39c5158Smillert 867b39c5158Smillert=item B<$gz-E<gt>gzclose> 868b39c5158Smillert 869b39c5158SmillertCloses the compressed file. Any pending data is flushed to the file 870b39c5158Smillertbefore it is closed. 871b39c5158Smillert 872b39c5158SmillertReturns 0 on success. 873b39c5158Smillert 874b39c5158Smillert=item B<$gz-E<gt>gzsetparams($level, $strategy> 875b39c5158Smillert 876b39c5158SmillertChange settings for the deflate stream C<$gz>. 877b39c5158Smillert 878b39c5158SmillertThe list of the valid options is shown below. Options not specified 879b39c5158Smillertwill remain unchanged. 880b39c5158Smillert 881b39c5158SmillertNote: This method is only available if you are running zlib 1.0.6 or better. 882b39c5158Smillert 883b39c5158Smillert=over 5 884b39c5158Smillert 885b39c5158Smillert=item B<$level> 886b39c5158Smillert 887b39c5158SmillertDefines the compression level. Valid values are 0 through 9, 888b39c5158SmillertC<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and 889b39c5158SmillertC<Z_DEFAULT_COMPRESSION>. 890b39c5158Smillert 891b39c5158Smillert=item B<$strategy> 892b39c5158Smillert 893b39c5158SmillertDefines the strategy used to tune the compression. The valid values are 894b39c5158SmillertC<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. 895b39c5158Smillert 896b39c5158Smillert=back 897b39c5158Smillert 898b39c5158Smillert=item B<$gz-E<gt>gzerror> 899b39c5158Smillert 900b39c5158SmillertReturns the I<zlib> error message or number for the last operation 901b39c5158Smillertassociated with C<$gz>. The return value will be the I<zlib> error 902b39c5158Smillertnumber when used in a numeric context and the I<zlib> error message 903b39c5158Smillertwhen used in a string context. The I<zlib> error number constants, 904b39c5158Smillertshown below, are available for use. 905b39c5158Smillert 906b39c5158Smillert Z_OK 907b39c5158Smillert Z_STREAM_END 908b39c5158Smillert Z_ERRNO 909b39c5158Smillert Z_STREAM_ERROR 910b39c5158Smillert Z_DATA_ERROR 911b39c5158Smillert Z_MEM_ERROR 912b39c5158Smillert Z_BUF_ERROR 913b39c5158Smillert 914b39c5158Smillert=item B<$gzerrno> 915b39c5158Smillert 916b39c5158SmillertThe C<$gzerrno> scalar holds the error code associated with the most 917b39c5158Smillertrecent I<gzip> routine. Note that unlike C<gzerror()>, the error is 918b39c5158SmillertI<not> associated with a particular file. 919b39c5158Smillert 920b39c5158SmillertAs with C<gzerror()> it returns an error number in numeric context and 921b39c5158Smillertan error message in string context. Unlike C<gzerror()> though, the 922b39c5158Smillerterror message will correspond to the I<zlib> message when the error is 923b39c5158Smillertassociated with I<zlib> itself, or the UNIX error message when it is 924b39c5158Smillertnot (i.e. I<zlib> returned C<Z_ERRORNO>). 925b39c5158Smillert 926b39c5158SmillertAs there is an overlap between the error numbers used by I<zlib> and 927b39c5158SmillertUNIX, C<$gzerrno> should only be used to check for the presence of 928b39c5158SmillertI<an> error in numeric context. Use C<gzerror()> to check for specific 929b39c5158SmillertI<zlib> errors. The I<gzcat> example below shows how the variable can 930b39c5158Smillertbe used safely. 931b39c5158Smillert 932b39c5158Smillert=back 933b39c5158Smillert 934b39c5158Smillert=head2 Examples 935b39c5158Smillert 936b39c5158SmillertHere is an example script which uses the interface. It implements a 937b39c5158SmillertI<gzcat> function. 938b39c5158Smillert 939b39c5158Smillert use strict ; 940b39c5158Smillert use warnings ; 941b39c5158Smillert 942b39c5158Smillert use Compress::Zlib ; 943b39c5158Smillert 944b39c5158Smillert # use stdin if no files supplied 945b39c5158Smillert @ARGV = '-' unless @ARGV ; 946b39c5158Smillert 947b39c5158Smillert foreach my $file (@ARGV) { 948b39c5158Smillert my $buffer ; 949b39c5158Smillert 950b39c5158Smillert my $gz = gzopen($file, "rb") 951b39c5158Smillert or die "Cannot open $file: $gzerrno\n" ; 952b39c5158Smillert 953b39c5158Smillert print $buffer while $gz->gzread($buffer) > 0 ; 954b39c5158Smillert 955b39c5158Smillert die "Error reading from $file: $gzerrno" . ($gzerrno+0) . "\n" 956b39c5158Smillert if $gzerrno != Z_STREAM_END ; 957b39c5158Smillert 958b39c5158Smillert $gz->gzclose() ; 959b39c5158Smillert } 960b39c5158Smillert 961b39c5158SmillertBelow is a script which makes use of C<gzreadline>. It implements a 962b39c5158Smillertvery simple I<grep> like script. 963b39c5158Smillert 964b39c5158Smillert use strict ; 965b39c5158Smillert use warnings ; 966b39c5158Smillert 967b39c5158Smillert use Compress::Zlib ; 968b39c5158Smillert 969b39c5158Smillert die "Usage: gzgrep pattern [file...]\n" 970b39c5158Smillert unless @ARGV >= 1; 971b39c5158Smillert 972b39c5158Smillert my $pattern = shift ; 973b39c5158Smillert 974b39c5158Smillert # use stdin if no files supplied 975b39c5158Smillert @ARGV = '-' unless @ARGV ; 976b39c5158Smillert 977b39c5158Smillert foreach my $file (@ARGV) { 978b39c5158Smillert my $gz = gzopen($file, "rb") 979b39c5158Smillert or die "Cannot open $file: $gzerrno\n" ; 980b39c5158Smillert 981b39c5158Smillert while ($gz->gzreadline($_) > 0) { 982b39c5158Smillert print if /$pattern/ ; 983b39c5158Smillert } 984b39c5158Smillert 985b39c5158Smillert die "Error reading from $file: $gzerrno\n" 986b39c5158Smillert if $gzerrno != Z_STREAM_END ; 987b39c5158Smillert 988b39c5158Smillert $gz->gzclose() ; 989b39c5158Smillert } 990b39c5158Smillert 991b39c5158SmillertThis script, I<gzstream>, does the opposite of the I<gzcat> script 992b39c5158Smillertabove. It reads from standard input and writes a gzip data stream to 993b39c5158Smillertstandard output. 994b39c5158Smillert 995b39c5158Smillert use strict ; 996b39c5158Smillert use warnings ; 997b39c5158Smillert 998b39c5158Smillert use Compress::Zlib ; 999b39c5158Smillert 1000b39c5158Smillert binmode STDOUT; # gzopen only sets it on the fd 1001b39c5158Smillert 1002b39c5158Smillert my $gz = gzopen(\*STDOUT, "wb") 1003b39c5158Smillert or die "Cannot open stdout: $gzerrno\n" ; 1004b39c5158Smillert 1005b39c5158Smillert while (<>) { 1006b39c5158Smillert $gz->gzwrite($_) 1007b39c5158Smillert or die "error writing: $gzerrno\n" ; 1008b39c5158Smillert } 1009b39c5158Smillert 1010b39c5158Smillert $gz->gzclose ; 1011b39c5158Smillert 1012b39c5158Smillert=head2 Compress::Zlib::memGzip 1013b39c5158Smillert 1014b39c5158SmillertThis function is used to create an in-memory gzip file with the minimum 1015b39c5158Smillertpossible gzip header (exactly 10 bytes). 1016b39c5158Smillert 1017b39c5158Smillert $dest = Compress::Zlib::memGzip($buffer) 1018b39c5158Smillert or die "Cannot compress: $gzerrno\n"; 1019b39c5158Smillert 1020b39c5158SmillertIf successful, it returns the in-memory gzip file. Otherwise it returns 1021b39c5158SmillertC<undef> and the C<$gzerrno> variable will store the zlib error code. 1022b39c5158Smillert 1023b39c5158SmillertThe C<$buffer> parameter can either be a scalar or a scalar reference. 1024b39c5158Smillert 1025b39c5158SmillertSee L<IO::Compress::Gzip|IO::Compress::Gzip> for an alternative way to 1026b39c5158Smillertcarry out in-memory gzip compression. 1027b39c5158Smillert 1028b39c5158Smillert=head2 Compress::Zlib::memGunzip 1029b39c5158Smillert 1030b39c5158SmillertThis function is used to uncompress an in-memory gzip file. 1031b39c5158Smillert 1032b39c5158Smillert $dest = Compress::Zlib::memGunzip($buffer) 1033898184e3Ssthen or die "Cannot uncompress: $gzerrno\n"; 1034b39c5158Smillert 1035b39c5158SmillertIf successful, it returns the uncompressed gzip file. Otherwise it 1036b39c5158Smillertreturns C<undef> and the C<$gzerrno> variable will store the zlib error 1037b39c5158Smillertcode. 1038b39c5158Smillert 1039b39c5158SmillertThe C<$buffer> parameter can either be a scalar or a scalar reference. The 1040b39c5158Smillertcontents of the C<$buffer> parameter are destroyed after calling this function. 1041b39c5158Smillert 1042b39c5158SmillertIf C<$buffer> consists of multiple concatenated gzip data streams only the 1043b39c5158Smillertfirst will be uncompressed. Use C<gunzip> with the C<MultiStream> option in 1044b39c5158Smillertthe C<IO::Uncompress::Gunzip> module if you need to deal with concatenated 1045b39c5158Smillertdata streams. 1046b39c5158Smillert 1047b39c5158SmillertSee L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip> for an alternative way 1048b39c5158Smillertto carry out in-memory gzip uncompression. 1049b39c5158Smillert 1050b39c5158Smillert=head1 COMPRESS/UNCOMPRESS 1051b39c5158Smillert 1052b39c5158SmillertTwo functions are provided to perform in-memory compression/uncompression of 1053b39c5158SmillertRFC 1950 data streams. They are called C<compress> and C<uncompress>. 1054b39c5158Smillert 1055b39c5158Smillert=over 5 1056b39c5158Smillert 1057b39c5158Smillert=item B<$dest = compress($source [, $level] ) ;> 1058b39c5158Smillert 1059b39c5158SmillertCompresses C<$source>. If successful it returns the compressed 1060b39c5158Smillertdata. Otherwise it returns I<undef>. 1061b39c5158Smillert 1062b39c5158SmillertThe source buffer, C<$source>, can either be a scalar or a scalar 1063b39c5158Smillertreference. 1064b39c5158Smillert 1065b39c5158SmillertThe C<$level> parameter defines the compression level. Valid values are 1066b39c5158Smillert0 through 9, C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, 1067b39c5158SmillertC<Z_BEST_COMPRESSION>, and C<Z_DEFAULT_COMPRESSION>. 1068b39c5158SmillertIf C<$level> is not specified C<Z_DEFAULT_COMPRESSION> will be used. 1069b39c5158Smillert 1070b39c5158Smillert=item B<$dest = uncompress($source) ;> 1071b39c5158Smillert 1072b39c5158SmillertUncompresses C<$source>. If successful it returns the uncompressed 1073b39c5158Smillertdata. Otherwise it returns I<undef>. 1074b39c5158Smillert 1075b39c5158SmillertThe source buffer can either be a scalar or a scalar reference. 1076b39c5158Smillert 1077b39c5158Smillert=back 1078b39c5158Smillert 1079b39c5158SmillertPlease note: the two functions defined above are I<not> compatible with 1080b39c5158Smillertthe Unix commands of the same name. 1081b39c5158Smillert 1082b39c5158SmillertSee L<IO::Deflate|IO::Deflate> and L<IO::Inflate|IO::Inflate> included with 1083b39c5158Smillertthis distribution for an alternative interface for reading/writing RFC 1950 1084b39c5158Smillertfiles/buffers. 1085b39c5158Smillert 1086b39c5158Smillert=head1 Deflate Interface 1087b39c5158Smillert 1088b39c5158SmillertThis section defines an interface that allows in-memory compression using 1089b39c5158Smillertthe I<deflate> interface provided by zlib. 1090b39c5158Smillert 1091b39c5158SmillertHere is a definition of the interface available: 1092b39c5158Smillert 1093b39c5158Smillert=head2 B<($d, $status) = deflateInit( [OPT] )> 1094b39c5158Smillert 1095b39c5158SmillertInitialises a deflation stream. 1096b39c5158Smillert 1097b39c5158SmillertIt combines the features of the I<zlib> functions C<deflateInit>, 1098b39c5158SmillertC<deflateInit2> and C<deflateSetDictionary>. 1099b39c5158Smillert 1100b39c5158SmillertIf successful, it will return the initialised deflation stream, C<$d> 1101b39c5158Smillertand C<$status> of C<Z_OK> in a list context. In scalar context it 1102b39c5158Smillertreturns the deflation stream, C<$d>, only. 1103b39c5158Smillert 1104b39c5158SmillertIf not successful, the returned deflation stream (C<$d>) will be 1105b39c5158SmillertI<undef> and C<$status> will hold the exact I<zlib> error code. 1106b39c5158Smillert 1107b39c5158SmillertThe function optionally takes a number of named options specified as 1108b39c5158SmillertC<< -Name=>value >> pairs. This allows individual options to be 1109b39c5158Smillerttailored without having to specify them all in the parameter list. 1110b39c5158Smillert 1111b39c5158SmillertFor backward compatibility, it is also possible to pass the parameters 1112b39c5158Smillertas a reference to a hash containing the name=>value pairs. 1113b39c5158Smillert 1114b39c5158SmillertThe function takes one optional parameter, a reference to a hash. The 1115b39c5158Smillertcontents of the hash allow the deflation interface to be tailored. 1116b39c5158Smillert 1117b39c5158SmillertHere is a list of the valid options: 1118b39c5158Smillert 1119b39c5158Smillert=over 5 1120b39c5158Smillert 1121b39c5158Smillert=item B<-Level> 1122b39c5158Smillert 1123b39c5158SmillertDefines the compression level. Valid values are 0 through 9, 1124b39c5158SmillertC<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and 1125b39c5158SmillertC<Z_DEFAULT_COMPRESSION>. 1126b39c5158Smillert 1127b39c5158SmillertThe default is Z_DEFAULT_COMPRESSION. 1128b39c5158Smillert 1129b39c5158Smillert=item B<-Method> 1130b39c5158Smillert 1131b39c5158SmillertDefines the compression method. The only valid value at present (and 1132b39c5158Smillertthe default) is Z_DEFLATED. 1133b39c5158Smillert 1134b39c5158Smillert=item B<-WindowBits> 1135b39c5158Smillert 1136b39c5158SmillertTo create an RFC 1950 data stream, set C<WindowBits> to a positive number. 1137b39c5158Smillert 1138b39c5158SmillertTo create an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>. 1139b39c5158Smillert 1140b39c5158SmillertFor a full definition of the meaning and valid values for C<WindowBits> refer 1141b39c5158Smillertto the I<zlib> documentation for I<deflateInit2>. 1142b39c5158Smillert 1143b39c5158SmillertDefaults to MAX_WBITS. 1144b39c5158Smillert 1145b39c5158Smillert=item B<-MemLevel> 1146b39c5158Smillert 1147b39c5158SmillertFor a definition of the meaning and valid values for C<MemLevel> 1148b39c5158Smillertrefer to the I<zlib> documentation for I<deflateInit2>. 1149b39c5158Smillert 1150b39c5158SmillertDefaults to MAX_MEM_LEVEL. 1151b39c5158Smillert 1152b39c5158Smillert=item B<-Strategy> 1153b39c5158Smillert 1154b39c5158SmillertDefines the strategy used to tune the compression. The valid values are 1155b39c5158SmillertC<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. 1156b39c5158Smillert 1157b39c5158SmillertThe default is Z_DEFAULT_STRATEGY. 1158b39c5158Smillert 1159b39c5158Smillert=item B<-Dictionary> 1160b39c5158Smillert 1161b39c5158SmillertWhen a dictionary is specified I<Compress::Zlib> will automatically 1162b39c5158Smillertcall C<deflateSetDictionary> directly after calling C<deflateInit>. The 1163b39c5158SmillertAdler32 value for the dictionary can be obtained by calling the method 1164b46d8ef2Safresh1C<< $d->dict_adler() >>. 1165b39c5158Smillert 1166b39c5158SmillertThe default is no dictionary. 1167b39c5158Smillert 1168b39c5158Smillert=item B<-Bufsize> 1169b39c5158Smillert 1170b39c5158SmillertSets the initial size for the deflation buffer. If the buffer has to be 1171b39c5158Smillertreallocated to increase the size, it will grow in increments of 1172b39c5158SmillertC<Bufsize>. 1173b39c5158Smillert 1174b39c5158SmillertThe default is 4096. 1175b39c5158Smillert 1176b39c5158Smillert=back 1177b39c5158Smillert 1178b39c5158SmillertHere is an example of using the C<deflateInit> optional parameter list 1179b39c5158Smillertto override the default buffer size and compression level. All other 1180b39c5158Smillertoptions will take their default values. 1181b39c5158Smillert 1182b39c5158Smillert deflateInit( -Bufsize => 300, 1183b39c5158Smillert -Level => Z_BEST_SPEED ) ; 1184b39c5158Smillert 1185b39c5158Smillert=head2 B<($out, $status) = $d-E<gt>deflate($buffer)> 1186b39c5158Smillert 1187b39c5158SmillertDeflates the contents of C<$buffer>. The buffer can either be a scalar 1188b39c5158Smillertor a scalar reference. When finished, C<$buffer> will be 1189b39c5158Smillertcompletely processed (assuming there were no errors). If the deflation 1190b39c5158Smillertwas successful it returns the deflated output, C<$out>, and a status 1191b39c5158Smillertvalue, C<$status>, of C<Z_OK>. 1192b39c5158Smillert 1193b39c5158SmillertOn error, C<$out> will be I<undef> and C<$status> will contain the 1194b39c5158SmillertI<zlib> error code. 1195b39c5158Smillert 1196b39c5158SmillertIn a scalar context C<deflate> will return C<$out> only. 1197b39c5158Smillert 1198b39c5158SmillertAs with the I<deflate> function in I<zlib>, it is not necessarily the 1199b39c5158Smillertcase that any output will be produced by this method. So don't rely on 1200b39c5158Smillertthe fact that C<$out> is empty for an error test. 1201b39c5158Smillert 1202b39c5158Smillert=head2 B<($out, $status) = $d-E<gt>flush()> 1203b39c5158Smillert=head2 B<($out, $status) = $d-E<gt>flush($flush_type)> 1204b39c5158Smillert 1205b39c5158SmillertTypically used to finish the deflation. Any pending output will be 1206b39c5158Smillertreturned via C<$out>. 1207b39c5158SmillertC<$status> will have a value C<Z_OK> if successful. 1208b39c5158Smillert 1209b39c5158SmillertIn a scalar context C<flush> will return C<$out> only. 1210b39c5158Smillert 1211b39c5158SmillertNote that flushing can seriously degrade the compression ratio, so it 1212b39c5158Smillertshould only be used to terminate a decompression (using C<Z_FINISH>) or 1213b39c5158Smillertwhen you want to create a I<full flush point> (using C<Z_FULL_FLUSH>). 1214b39c5158Smillert 1215b39c5158SmillertBy default the C<flush_type> used is C<Z_FINISH>. Other valid values 1216b39c5158Smillertfor C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH> 1217b39c5158Smillertand C<Z_FULL_FLUSH>. It is strongly recommended that you only set the 1218b39c5158SmillertC<flush_type> parameter if you fully understand the implications of 1219b39c5158Smillertwhat it does. See the C<zlib> documentation for details. 1220b39c5158Smillert 1221b39c5158Smillert=head2 B<$status = $d-E<gt>deflateParams([OPT])> 1222b39c5158Smillert 1223b39c5158SmillertChange settings for the deflate stream C<$d>. 1224b39c5158Smillert 1225b39c5158SmillertThe list of the valid options is shown below. Options not specified 1226b39c5158Smillertwill remain unchanged. 1227b39c5158Smillert 1228b39c5158Smillert=over 5 1229b39c5158Smillert 1230b39c5158Smillert=item B<-Level> 1231b39c5158Smillert 1232b39c5158SmillertDefines the compression level. Valid values are 0 through 9, 1233b39c5158SmillertC<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and 1234b39c5158SmillertC<Z_DEFAULT_COMPRESSION>. 1235b39c5158Smillert 1236b39c5158Smillert=item B<-Strategy> 1237b39c5158Smillert 1238b39c5158SmillertDefines the strategy used to tune the compression. The valid values are 1239b39c5158SmillertC<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. 1240b39c5158Smillert 1241b39c5158Smillert=back 1242b39c5158Smillert 1243b39c5158Smillert=head2 B<$d-E<gt>dict_adler()> 1244b39c5158Smillert 1245b39c5158SmillertReturns the adler32 value for the dictionary. 1246b39c5158Smillert 1247b39c5158Smillert=head2 B<$d-E<gt>msg()> 1248b39c5158Smillert 1249b39c5158SmillertReturns the last error message generated by zlib. 1250b39c5158Smillert 1251b39c5158Smillert=head2 B<$d-E<gt>total_in()> 1252b39c5158Smillert 1253b39c5158SmillertReturns the total number of bytes uncompressed bytes input to deflate. 1254b39c5158Smillert 1255b39c5158Smillert=head2 B<$d-E<gt>total_out()> 1256b39c5158Smillert 1257b39c5158SmillertReturns the total number of compressed bytes output from deflate. 1258b39c5158Smillert 1259b39c5158Smillert=head2 Example 1260b39c5158Smillert 1261b39c5158SmillertHere is a trivial example of using C<deflate>. It simply reads standard 1262b39c5158Smillertinput, deflates it and writes it to standard output. 1263b39c5158Smillert 1264b39c5158Smillert use strict ; 1265b39c5158Smillert use warnings ; 1266b39c5158Smillert 1267b39c5158Smillert use Compress::Zlib ; 1268b39c5158Smillert 1269b39c5158Smillert binmode STDIN; 1270b39c5158Smillert binmode STDOUT; 1271b39c5158Smillert my $x = deflateInit() 1272b39c5158Smillert or die "Cannot create a deflation stream\n" ; 1273b39c5158Smillert 1274b39c5158Smillert my ($output, $status) ; 1275b39c5158Smillert while (<>) 1276b39c5158Smillert { 1277b39c5158Smillert ($output, $status) = $x->deflate($_) ; 1278b39c5158Smillert 1279b39c5158Smillert $status == Z_OK 1280b39c5158Smillert or die "deflation failed\n" ; 1281b39c5158Smillert 1282b39c5158Smillert print $output ; 1283b39c5158Smillert } 1284b39c5158Smillert 1285b39c5158Smillert ($output, $status) = $x->flush() ; 1286b39c5158Smillert 1287b39c5158Smillert $status == Z_OK 1288b39c5158Smillert or die "deflation failed\n" ; 1289b39c5158Smillert 1290b39c5158Smillert print $output ; 1291b39c5158Smillert 1292b39c5158Smillert=head1 Inflate Interface 1293b39c5158Smillert 1294b39c5158SmillertThis section defines the interface available that allows in-memory 1295b39c5158Smillertuncompression using the I<deflate> interface provided by zlib. 1296b39c5158Smillert 1297b39c5158SmillertHere is a definition of the interface: 1298b39c5158Smillert 1299b39c5158Smillert=head2 B<($i, $status) = inflateInit()> 1300b39c5158Smillert 1301b39c5158SmillertInitialises an inflation stream. 1302b39c5158Smillert 1303b39c5158SmillertIn a list context it returns the inflation stream, C<$i>, and the 1304b39c5158SmillertI<zlib> status code in C<$status>. In a scalar context it returns the 1305b39c5158Smillertinflation stream only. 1306b39c5158Smillert 1307b39c5158SmillertIf successful, C<$i> will hold the inflation stream and C<$status> will 1308b39c5158Smillertbe C<Z_OK>. 1309b39c5158Smillert 1310b39c5158SmillertIf not successful, C<$i> will be I<undef> and C<$status> will hold the 1311b39c5158SmillertI<zlib> error code. 1312b39c5158Smillert 1313b39c5158SmillertThe function optionally takes a number of named options specified as 1314b39c5158SmillertC<< -Name=>value >> pairs. This allows individual options to be 1315b39c5158Smillerttailored without having to specify them all in the parameter list. 1316b39c5158Smillert 1317b39c5158SmillertFor backward compatibility, it is also possible to pass the parameters 1318b39c5158Smillertas a reference to a hash containing the name=>value pairs. 1319b39c5158Smillert 1320b39c5158SmillertThe function takes one optional parameter, a reference to a hash. The 1321b39c5158Smillertcontents of the hash allow the deflation interface to be tailored. 1322b39c5158Smillert 1323b39c5158SmillertHere is a list of the valid options: 1324b39c5158Smillert 1325b39c5158Smillert=over 5 1326b39c5158Smillert 1327b39c5158Smillert=item B<-WindowBits> 1328b39c5158Smillert 1329b39c5158SmillertTo uncompress an RFC 1950 data stream, set C<WindowBits> to a positive number. 1330b39c5158Smillert 1331b39c5158SmillertTo uncompress an RFC 1951 data stream, set C<WindowBits> to C<-MAX_WBITS>. 1332b39c5158Smillert 1333b39c5158SmillertFor a full definition of the meaning and valid values for C<WindowBits> refer 1334b39c5158Smillertto the I<zlib> documentation for I<inflateInit2>. 1335b39c5158Smillert 1336b39c5158SmillertDefaults to MAX_WBITS. 1337b39c5158Smillert 1338b39c5158Smillert=item B<-Bufsize> 1339b39c5158Smillert 1340b39c5158SmillertSets the initial size for the inflation buffer. If the buffer has to be 1341b39c5158Smillertreallocated to increase the size, it will grow in increments of 1342b39c5158SmillertC<Bufsize>. 1343b39c5158Smillert 1344b39c5158SmillertDefault is 4096. 1345b39c5158Smillert 1346b39c5158Smillert=item B<-Dictionary> 1347b39c5158Smillert 1348b39c5158SmillertThe default is no dictionary. 1349b39c5158Smillert 1350b39c5158Smillert=back 1351b39c5158Smillert 1352b39c5158SmillertHere is an example of using the C<inflateInit> optional parameter to 1353b39c5158Smillertoverride the default buffer size. 1354b39c5158Smillert 1355b39c5158Smillert inflateInit( -Bufsize => 300 ) ; 1356b39c5158Smillert 1357b39c5158Smillert=head2 B<($out, $status) = $i-E<gt>inflate($buffer)> 1358b39c5158Smillert 1359b39c5158SmillertInflates the complete contents of C<$buffer>. The buffer can either be 1360b39c5158Smillerta scalar or a scalar reference. 1361b39c5158Smillert 1362b39c5158SmillertReturns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the 1363b39c5158Smillertcompressed data has been successfully reached. 1364b39c5158SmillertIf not successful, C<$out> will be I<undef> and C<$status> will hold 1365b39c5158Smillertthe I<zlib> error code. 1366b39c5158Smillert 1367b39c5158SmillertThe C<$buffer> parameter is modified by C<inflate>. On completion it 1368b39c5158Smillertwill contain what remains of the input buffer after inflation. This 1369b39c5158Smillertmeans that C<$buffer> will be an empty string when the return status is 1370b39c5158SmillertC<Z_OK>. When the return status is C<Z_STREAM_END> the C<$buffer> 1371b39c5158Smillertparameter will contains what (if anything) was stored in the input 1372b39c5158Smillertbuffer after the deflated data stream. 1373b39c5158Smillert 1374b39c5158SmillertThis feature is useful when processing a file format that encapsulates 1375b39c5158Smillerta compressed data stream (e.g. gzip, zip). 1376b39c5158Smillert 1377b39c5158Smillert=head2 B<$status = $i-E<gt>inflateSync($buffer)> 1378b39c5158Smillert 1379b39c5158SmillertScans C<$buffer> until it reaches either a I<full flush point> or the 1380b39c5158Smillertend of the buffer. 1381b39c5158Smillert 1382b39c5158SmillertIf a I<full flush point> is found, C<Z_OK> is returned and C<$buffer> 1383b39c5158Smillertwill be have all data up to the flush point removed. This can then be 1384b39c5158Smillertpassed to the C<deflate> method. 1385b39c5158Smillert 1386b39c5158SmillertAny other return code means that a flush point was not found. If more 1387b39c5158Smillertdata is available, C<inflateSync> can be called repeatedly with more 1388b39c5158Smillertcompressed data until the flush point is found. 1389b39c5158Smillert 1390b39c5158Smillert=head2 B<$i-E<gt>dict_adler()> 1391b39c5158Smillert 1392b39c5158SmillertReturns the adler32 value for the dictionary. 1393b39c5158Smillert 1394b39c5158Smillert=head2 B<$i-E<gt>msg()> 1395b39c5158Smillert 1396b39c5158SmillertReturns the last error message generated by zlib. 1397b39c5158Smillert 1398b39c5158Smillert=head2 B<$i-E<gt>total_in()> 1399b39c5158Smillert 1400b39c5158SmillertReturns the total number of bytes compressed bytes input to inflate. 1401b39c5158Smillert 1402b39c5158Smillert=head2 B<$i-E<gt>total_out()> 1403b39c5158Smillert 1404b39c5158SmillertReturns the total number of uncompressed bytes output from inflate. 1405b39c5158Smillert 1406b39c5158Smillert=head2 Example 1407b39c5158Smillert 1408b39c5158SmillertHere is an example of using C<inflate>. 1409b39c5158Smillert 1410b39c5158Smillert use strict ; 1411b39c5158Smillert use warnings ; 1412b39c5158Smillert 1413b39c5158Smillert use Compress::Zlib ; 1414b39c5158Smillert 1415b39c5158Smillert my $x = inflateInit() 1416b39c5158Smillert or die "Cannot create a inflation stream\n" ; 1417b39c5158Smillert 1418b39c5158Smillert my $input = '' ; 1419b39c5158Smillert binmode STDIN; 1420b39c5158Smillert binmode STDOUT; 1421b39c5158Smillert 1422b39c5158Smillert my ($output, $status) ; 1423b39c5158Smillert while (read(STDIN, $input, 4096)) 1424b39c5158Smillert { 1425b39c5158Smillert ($output, $status) = $x->inflate(\$input) ; 1426b39c5158Smillert 1427b39c5158Smillert print $output 1428b39c5158Smillert if $status == Z_OK or $status == Z_STREAM_END ; 1429b39c5158Smillert 1430b39c5158Smillert last if $status != Z_OK ; 1431b39c5158Smillert } 1432b39c5158Smillert 1433b39c5158Smillert die "inflation failed\n" 1434b39c5158Smillert unless $status == Z_STREAM_END ; 1435b39c5158Smillert 1436b39c5158Smillert=head1 CHECKSUM FUNCTIONS 1437b39c5158Smillert 1438b39c5158SmillertTwo functions are provided by I<zlib> to calculate checksums. For the 1439b39c5158SmillertPerl interface, the order of the two parameters in both functions has 1440b39c5158Smillertbeen reversed. This allows both running checksums and one off 1441b39c5158Smillertcalculations to be done. 1442b39c5158Smillert 1443b39c5158Smillert $crc = adler32($buffer [,$crc]) ; 1444b39c5158Smillert $crc = crc32($buffer [,$crc]) ; 1445b39c5158Smillert 1446b39c5158SmillertThe buffer parameters can either be a scalar or a scalar reference. 1447b39c5158Smillert 1448b39c5158SmillertIf the $crc parameters is C<undef>, the crc value will be reset. 1449b39c5158Smillert 1450b39c5158SmillertIf you have built this module with zlib 1.2.3 or better, two more 1451b39c5158SmillertCRC-related functions are available. 1452b39c5158Smillert 14536fb12b70Safresh1 $crc = crc32_combine($crc1, $crc2, $len2); 14546fb12b70Safresh1 $adler = adler32_combine($adler1, $adler2, $len2); 1455b39c5158Smillert 1456b39c5158SmillertThese functions allow checksums to be merged. 14576fb12b70Safresh1Refer to the I<zlib> documentation for more details. 1458b39c5158Smillert 1459b39c5158Smillert=head1 Misc 1460b39c5158Smillert 1461b39c5158Smillert=head2 my $version = Compress::Zlib::zlib_version(); 1462b39c5158Smillert 1463b39c5158SmillertReturns the version of the zlib library. 1464b39c5158Smillert 1465b39c5158Smillert=head1 CONSTANTS 1466b39c5158Smillert 1467b39c5158SmillertAll the I<zlib> constants are automatically imported when you make use 1468b39c5158Smillertof I<Compress::Zlib>. 1469b39c5158Smillert 147056d68f1eSafresh1=head1 SUPPORT 147156d68f1eSafresh1 147256d68f1eSafresh1General feedback/questions/bug reports should be sent to 147356d68f1eSafresh1L<https://github.com/pmqs/IO-Compress/issues> (preferred) or 147456d68f1eSafresh1L<https://rt.cpan.org/Public/Dist/Display.html?Name=IO-Compress>. 147556d68f1eSafresh1 1476b39c5158Smillert=head1 SEE ALSO 1477b39c5158Smillert 1478b46d8ef2Safresh1L<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> 1479b39c5158Smillert 1480898184e3SsthenL<IO::Compress::FAQ|IO::Compress::FAQ> 1481b39c5158Smillert 1482b39c5158SmillertL<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, 1483b39c5158SmillertL<Archive::Tar|Archive::Tar>, 1484b39c5158SmillertL<IO::Zlib|IO::Zlib> 1485b39c5158Smillert 1486b39c5158SmillertFor RFC 1950, 1951 and 1952 see 1487eac174f2Safresh1L<https://datatracker.ietf.org/doc/html/rfc1950>, 1488eac174f2Safresh1L<https://datatracker.ietf.org/doc/html/rfc1951> and 1489eac174f2Safresh1L<https://datatracker.ietf.org/doc/html/rfc1952> 1490b39c5158Smillert 1491b39c5158SmillertThe I<zlib> compression library was written by Jean-loup Gailly 14929f11ffb7Safresh1C<gzip@prep.ai.mit.edu> and Mark Adler C<madler@alumni.caltech.edu>. 1493b39c5158Smillert 1494b39c5158SmillertThe primary site for the I<zlib> compression library is 14959f11ffb7Safresh1L<http://www.zlib.org>. 1496b39c5158Smillert 1497e0680481Safresh1The primary site for the I<zlib-ng> compression library is 1498e0680481Safresh1L<https://github.com/zlib-ng/zlib-ng>. 1499e0680481Safresh1 15009f11ffb7Safresh1The primary site for gzip is L<http://www.gzip.org>. 1501b39c5158Smillert 1502b39c5158Smillert=head1 AUTHOR 1503b39c5158Smillert 15049f11ffb7Safresh1This module was written by Paul Marquess, C<pmqs@cpan.org>. 1505b39c5158Smillert 1506b39c5158Smillert=head1 MODIFICATION HISTORY 1507b39c5158Smillert 1508b39c5158SmillertSee the Changes file. 1509b39c5158Smillert 1510b39c5158Smillert=head1 COPYRIGHT AND LICENSE 1511b39c5158Smillert 1512*3d61058aSafresh1Copyright (c) 1995-2024 Paul Marquess. All rights reserved. 1513b39c5158Smillert 1514b39c5158SmillertThis program is free software; you can redistribute it and/or 1515b39c5158Smillertmodify it under the same terms as Perl itself. 1516