Compress::Zlib
[p5sagit/p5-mst-13.2.git] / ext / Compress / Zlib / Zlib.pm
CommitLineData
f4c6fd49 1
2package Compress::Zlib;
3
4require 5.004 ;
5require Exporter;
f4c6fd49 6use AutoLoader;
7use Carp ;
8use IO::Handle ;
642e522c 9use Scalar::Util qw(dualvar);
10
11use Compress::Zlib::Common;
12use Compress::Zlib::ParseParameters;
f4c6fd49 13
14use strict ;
8aa25532 15use warnings ;
642e522c 16use bytes ;
17our ($VERSION, $XS_VERSION, @ISA, @EXPORT, $AUTOLOAD);
f4c6fd49 18
68a72f3d 19$VERSION = '2.000_06';
642e522c 20$XS_VERSION = $VERSION;
21$VERSION = eval $VERSION;
f4c6fd49 22
06edba15 23@ISA = qw(Exporter);
f4c6fd49 24# Items to export into callers namespace by default. Note: do not export
25# names by default without a very good reason. Use EXPORT_OK instead.
26# Do not simply export all your public functions/methods/constants.
27@EXPORT = qw(
642e522c 28 deflateInit inflateInit
f4c6fd49 29
642e522c 30 compress uncompress
f4c6fd49 31
642e522c 32 gzopen $gzerrno
f4c6fd49 33
642e522c 34 adler32 crc32
f4c6fd49 35
642e522c 36 ZLIB_VERSION
37 ZLIB_VERNUM
f4c6fd49 38
642e522c 39 DEF_WBITS
40 OS_CODE
f4c6fd49 41
42 MAX_MEM_LEVEL
642e522c 43 MAX_WBITS
44
45 Z_ASCII
46 Z_BEST_COMPRESSION
47 Z_BEST_SPEED
48 Z_BINARY
49 Z_BLOCK
50 Z_BUF_ERROR
51 Z_DATA_ERROR
52 Z_DEFAULT_COMPRESSION
53 Z_DEFAULT_STRATEGY
f4c6fd49 54 Z_DEFLATED
642e522c 55 Z_ERRNO
56 Z_FILTERED
57 Z_FIXED
58 Z_FINISH
59 Z_FULL_FLUSH
60 Z_HUFFMAN_ONLY
61 Z_MEM_ERROR
62 Z_NEED_DICT
63 Z_NO_COMPRESSION
64 Z_NO_FLUSH
65 Z_NULL
66 Z_OK
67 Z_PARTIAL_FLUSH
68 Z_RLE
69 Z_STREAM_END
70 Z_STREAM_ERROR
71 Z_SYNC_FLUSH
72 Z_UNKNOWN
73 Z_VERSION_ERROR
f4c6fd49 74);
75
f4c6fd49 76sub AUTOLOAD {
77 my($constname);
78 ($constname = $AUTOLOAD) =~ s/.*:://;
79 my ($error, $val) = constant($constname);
80 Carp::croak $error if $error;
81 no strict 'refs';
82 *{$AUTOLOAD} = sub { $val };
83 goto &{$AUTOLOAD};
84}
85
642e522c 86use constant FLAG_APPEND => 1 ;
87use constant FLAG_CRC => 2 ;
88use constant FLAG_ADLER => 4 ;
89use constant FLAG_CONSUME_INPUT => 8 ;
90
06edba15 91eval {
92 require XSLoader;
642e522c 93 XSLoader::load('Compress::Zlib', $XS_VERSION);
94 1;
95}
96or do {
06edba15 97 require DynaLoader;
98 local @ISA = qw(DynaLoader);
642e522c 99 bootstrap Compress::Zlib $XS_VERSION ;
100};
101
f4c6fd49 102# Preloaded methods go here.
103
642e522c 104require IO::Compress::Gzip;
105require IO::Uncompress::Gunzip;
106
107our (@my_z_errmsg);
108
109@my_z_errmsg = (
110 "need dictionary", # Z_NEED_DICT 2
111 "stream end", # Z_STREAM_END 1
112 "", # Z_OK 0
113 "file error", # Z_ERRNO (-1)
114 "stream error", # Z_STREAM_ERROR (-2)
115 "data error", # Z_DATA_ERROR (-3)
116 "insufficient memory", # Z_MEM_ERROR (-4)
117 "buffer error", # Z_BUF_ERROR (-5)
118 "incompatible version",# Z_VERSION_ERROR(-6)
119 );
120
121
122sub _set_gzerr
f4c6fd49 123{
642e522c 124 my $value = shift ;
f4c6fd49 125
642e522c 126 if ($value == 0) {
127 $Compress::Zlib::gzerrno = 0 ;
128 }
129 elsif ($value == Z_ERRNO() || $value > 2) {
130 $Compress::Zlib::gzerrno = $! ;
131 }
132 else {
133 $Compress::Zlib::gzerrno = dualvar($value+0, $my_z_errmsg[2 - $value]);
134 }
f4c6fd49 135
642e522c 136 return $value ;
f4c6fd49 137}
138
642e522c 139sub _save_gzerr
f4c6fd49 140{
642e522c 141 my $gz = shift ;
142 my $test_eof = shift ;
f4c6fd49 143
642e522c 144 my $value = $gz->errorNo() || 0 ;
145
146 if ($test_eof) {
147 #my $gz = $self->[0] ;
148 # gzread uses Z_STREAM_END to denote a successful end
149 $value = Z_STREAM_END() if $gz->eof() && $value == 0 ;
150 }
151
152 _set_gzerr($value) ;
f4c6fd49 153}
154
155sub gzopen($$)
156{
157 my ($file, $mode) = @_ ;
642e522c 158
159 my $gz ;
160 my %defOpts = (Level => Z_DEFAULT_COMPRESSION(),
161 Strategy => Z_DEFAULT_STRATEGY(),
162 );
163
164 my $writing ;
165 $writing = ! ($mode =~ /r/i) ;
166 $writing = ($mode =~ /[wa]/i) ;
167
168 $defOpts{Level} = $1 if $mode =~ /(\d)/;
169 $defOpts{Strategy} = Z_FILTERED() if $mode =~ /f/i;
170 $defOpts{Strategy} = Z_HUFFMAN_ONLY() if $mode =~ /h/i;
171
172 my $infDef = $writing ? 'deflate' : 'inflate';
173 my @params = () ;
174
175 croak "gzopen: file parameter is not a filehandle or filename"
176 unless isaFilehandle $file || isaFilename $file ;
177
178 return undef unless $mode =~ /[rwa]/i ;
179
180 _set_gzerr(0) ;
181
182 if ($writing) {
07a53161 183 $gz = new IO::Compress::Gzip($file, Minimal => 1, AutoClose => 1,
7581d28c 184 %defOpts)
642e522c 185 or $Compress::Zlib::gzerrno = $IO::Compress::Gzip::GzipError;
f4c6fd49 186 }
187 else {
07a53161 188 $gz = new IO::Uncompress::Gunzip($file,
189 Transparent => 1,
7581d28c 190 Append => 0,
07a53161 191 AutoClose => 1, Strict => 0)
642e522c 192 or $Compress::Zlib::gzerrno = $IO::Uncompress::Gunzip::GunzipError;
f4c6fd49 193 }
642e522c 194
195 return undef
196 if ! defined $gz ;
197
198 bless [$gz, $infDef], 'Compress::Zlib::gzFile';
199}
200
201sub Compress::Zlib::gzFile::gzread
202{
203 my $self = shift ;
204
205 return _set_gzerr(Z_STREAM_ERROR())
206 if $self->[1] ne 'inflate';
207
208 return 0 if $self->gzeof();
209
210 my $gz = $self->[0] ;
211 my $status = $gz->read($_[0], defined $_[1] ? $_[1] : 4096) ;
212 $_[0] = "" if ! defined $_[0] ;
213 _save_gzerr($gz, 1);
214 return $status ;
f4c6fd49 215}
216
642e522c 217sub Compress::Zlib::gzFile::gzreadline
f4c6fd49 218{
642e522c 219 my $self = shift ;
f4c6fd49 220
642e522c 221 my $gz = $self->[0] ;
222 $_[0] = $gz->getline() ;
223 _save_gzerr($gz, 1);
224 return defined $_[0] ? length $_[0] : 0 ;
225}
f4c6fd49 226
642e522c 227sub Compress::Zlib::gzFile::gzwrite
228{
229 my $self = shift ;
230 my $gz = $self->[0] ;
f4c6fd49 231
642e522c 232 return _set_gzerr(Z_STREAM_ERROR())
233 if $self->[1] ne 'deflate';
f4c6fd49 234
642e522c 235 my $status = $gz->write($_[0]) ;
236 _save_gzerr($gz);
237 return $status ;
238}
f4c6fd49 239
642e522c 240sub Compress::Zlib::gzFile::gztell
241{
242 my $self = shift ;
243 my $gz = $self->[0] ;
244 my $status = $gz->tell() ;
245 _save_gzerr($gz);
246 return $status ;
247}
248
249sub Compress::Zlib::gzFile::gzseek
250{
251 my $self = shift ;
252 my $offset = shift ;
253 my $whence = shift ;
254
255 my $gz = $self->[0] ;
256 my $status ;
257 eval { $status = $gz->seek($offset, $whence) ; };
258 if ($@)
259 {
260 my $error = $@;
261 $error =~ s/^.*: /gzseek: /;
262 $error =~ s/ at .* line \d+\s*$//;
263 croak $error;
f4c6fd49 264 }
642e522c 265 _save_gzerr($gz);
266 return $status ;
267}
268
269sub Compress::Zlib::gzFile::gzflush
270{
271 my $self = shift ;
272 my $f = shift ;
f4c6fd49 273
642e522c 274 my $gz = $self->[0] ;
275 my $status = $gz->flush($f) ;
276 _save_gzerr($gz);
277 return $status ;
f4c6fd49 278}
279
642e522c 280sub Compress::Zlib::gzFile::gzclose
281{
282 my $self = shift ;
283 my $gz = $self->[0] ;
f4c6fd49 284
642e522c 285 my $status = $gz->close() ;
286 _save_gzerr($gz);
287 return ! $status ;
288}
f4c6fd49 289
642e522c 290sub Compress::Zlib::gzFile::gzeof
291{
292 my $self = shift ;
293 my $gz = $self->[0] ;
294
295 return 0
296 if $self->[1] ne 'inflate';
297
298 my $status = $gz->eof() ;
299 _save_gzerr($gz);
300 return $status ;
301}
302
303sub Compress::Zlib::gzFile::gzsetparams
304{
305 my $self = shift ;
306 croak "Usage: Compress::Zlib::gzFile::gzsetparams(file, level, strategy)"
307 unless @_ eq 2 ;
308
309 my $gz = $self->[0] ;
310 my $level = shift ;
311 my $strategy = shift;
312
313 return _set_gzerr(Z_STREAM_ERROR())
314 if $self->[1] ne 'deflate';
315
316 my $status = *$gz->{Deflate}->deflateParams(-Level => $level,
317 -Strategy => $strategy);
318 _save_gzerr($gz);
319 return $status ;
320}
321
322sub Compress::Zlib::gzFile::gzerror
f4c6fd49 323{
642e522c 324 my $self = shift ;
325 my $gz = $self->[0] ;
326
327 return $Compress::Zlib::gzerrno ;
f4c6fd49 328}
329
642e522c 330sub Compress::Zlib::Deflate::new
331{
332 my $pkg = shift ;
333 my ($got) = ParseParameters(0,
334 {
335 'AppendOutput' => [Parse_boolean, 0],
336 'CRC32' => [Parse_boolean, 0],
337 'ADLER32' => [Parse_boolean, 0],
338 'Bufsize' => [Parse_unsigned, 4096],
339
340 'Level' => [Parse_signed, Z_DEFAULT_COMPRESSION()],
341 'Method' => [Parse_unsigned, Z_DEFLATED()],
342 'WindowBits' => [Parse_signed, MAX_WBITS()],
343 'MemLevel' => [Parse_unsigned, MAX_MEM_LEVEL()],
344 'Strategy' => [Parse_unsigned, Z_DEFAULT_STRATEGY()],
345 'Dictionary' => [Parse_any, ""],
346 }, @_) ;
347
348
349 croak "Compress::Zlib::Deflate::new: Bufsize must be >= 1, you specified " .
350 $got->value('Bufsize')
351 unless $got->value('Bufsize') >= 1;
352
353 my $flags = 0 ;
354 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
355 $flags |= FLAG_CRC if $got->value('CRC32') ;
356 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
357
358 _deflateInit($flags,
359 $got->value('Level'),
360 $got->value('Method'),
361 $got->value('WindowBits'),
362 $got->value('MemLevel'),
363 $got->value('Strategy'),
364 $got->value('Bufsize'),
365 $got->value('Dictionary')) ;
366
367}
368
369sub Compress::Zlib::Inflate::new
370{
371 my $pkg = shift ;
372 my ($got) = ParseParameters(0,
373 {
374 'AppendOutput' => [Parse_boolean, 0],
375 'CRC32' => [Parse_boolean, 0],
376 'ADLER32' => [Parse_boolean, 0],
377 'ConsumeInput' => [Parse_boolean, 1],
378 'Bufsize' => [Parse_unsigned, 4096],
379
380 'WindowBits' => [Parse_signed, MAX_WBITS()],
381 'Dictionary' => [Parse_any, ""],
382 }, @_) ;
383
384
385 croak "Compress::Zlib::Inflate::new: Bufsize must be >= 1, you specified " .
386 $got->value('Bufsize')
387 unless $got->value('Bufsize') >= 1;
388
389 my $flags = 0 ;
390 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
391 $flags |= FLAG_CRC if $got->value('CRC32') ;
392 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
393 $flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
394
395 _inflateInit($flags, $got->value('WindowBits'), $got->value('Bufsize'),
396 $got->value('Dictionary')) ;
397}
398
399sub Compress::Zlib::InflateScan::new
400{
401 my $pkg = shift ;
402 my ($got) = ParseParameters(0,
403 {
404 'CRC32' => [Parse_boolean, 0],
405 'ADLER32' => [Parse_boolean, 0],
406 'Bufsize' => [Parse_unsigned, 4096],
407
408 'WindowBits' => [Parse_signed, -MAX_WBITS()],
409 'Dictionary' => [Parse_any, ""],
410 }, @_) ;
411
412
413 croak "Compress::Zlib::InflateScan::new: Bufsize must be >= 1, you specified " .
414 $got->value('Bufsize')
415 unless $got->value('Bufsize') >= 1;
416
417 my $flags = 0 ;
418 #$flags |= FLAG_APPEND if $got->value('AppendOutput') ;
419 $flags |= FLAG_CRC if $got->value('CRC32') ;
420 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
421 #$flags |= FLAG_CONSUME_INPUT if $got->value('ConsumeInput') ;
422
423 _inflateScanInit($flags, $got->value('WindowBits'), $got->value('Bufsize'),
424 '') ;
425}
426
427sub Compress::Zlib::inflateScanStream::createDeflateStream
f4c6fd49 428{
642e522c 429 my $pkg = shift ;
430 my ($got) = ParseParameters(0,
431 {
432 'AppendOutput' => [Parse_boolean, 0],
433 'CRC32' => [Parse_boolean, 0],
434 'ADLER32' => [Parse_boolean, 0],
435 'Bufsize' => [Parse_unsigned, 4096],
f4c6fd49 436
642e522c 437 'Level' => [Parse_signed, Z_DEFAULT_COMPRESSION()],
438 'Method' => [Parse_unsigned, Z_DEFLATED()],
439 'WindowBits' => [Parse_signed, - MAX_WBITS()],
440 'MemLevel' => [Parse_unsigned, MAX_MEM_LEVEL()],
441 'Strategy' => [Parse_unsigned, Z_DEFAULT_STRATEGY()],
442 }, @_) ;
443
444 croak "Compress::Zlib::InflateScan::createDeflateStream: Bufsize must be >= 1, you specified " .
445 $got->value('Bufsize')
446 unless $got->value('Bufsize') >= 1;
447
448 my $flags = 0 ;
449 $flags |= FLAG_APPEND if $got->value('AppendOutput') ;
450 $flags |= FLAG_CRC if $got->value('CRC32') ;
451 $flags |= FLAG_ADLER if $got->value('ADLER32') ;
452
453 $pkg->_createDeflateStream($flags,
454 $got->value('Level'),
455 $got->value('Method'),
456 $got->value('WindowBits'),
457 $got->value('MemLevel'),
458 $got->value('Strategy'),
459 $got->value('Bufsize'),
460 ) ;
461
f4c6fd49 462}
463
642e522c 464
f4c6fd49 465sub Compress::Zlib::deflateStream::deflateParams
466{
467 my $self = shift ;
642e522c 468 my ($got) = ParseParameters(0, {
469 'Level' => [Parse_signed, undef],
470 'Strategy' => [Parse_unsigned, undef],
471 'Bufsize' => [Parse_unsigned, undef],
472 },
473 @_) ;
474
475 croak "Compress::Zlib::deflateParams needs Level and/or Strategy"
476 unless $got->parsed('Level') + $got->parsed('Strategy') +
477 $got->parsed('Bufsize');
478
479 croak "Compress::Zlib::Inflate::deflateParams: Bufsize must be >= 1, you specified " .
480 $got->value('Bufsize')
481 if $got->parsed('Bufsize') && $got->value('Bufsize') <= 1;
f4c6fd49 482
483 my $flags = 0;
642e522c 484 $flags |= 1 if $got->parsed('Level') ;
485 $flags |= 2 if $got->parsed('Strategy') ;
486 $flags |= 4 if $got->parsed('Bufsize') ;
487
488 $self->_deflateParams($flags, $got->value('Level'),
489 $got->value('Strategy'), $got->value('Bufsize'));
490
f4c6fd49 491}
492
493sub compress($;$)
494{
642e522c 495 my ($x, $output, $err, $in) =('', '', '', '') ;
f4c6fd49 496
497 if (ref $_[0] ) {
498 $in = $_[0] ;
642e522c 499 croak "not a scalar reference" unless ref $in eq 'SCALAR' ;
f4c6fd49 500 }
501 else {
502 $in = \$_[0] ;
503 }
504
505 my $level = (@_ == 2 ? $_[1] : Z_DEFAULT_COMPRESSION() );
506
642e522c 507 $x = new Compress::Zlib::Deflate -AppendOutput => 1, -Level => $level
508 or return undef ;
f4c6fd49 509
642e522c 510 $err = $x->deflate($in, $output) ;
511 return undef unless $err == Z_OK() ;
f4c6fd49 512
642e522c 513 $err = $x->flush($output) ;
514 return undef unless $err == Z_OK() ;
f4c6fd49 515
642e522c 516 return $output ;
f4c6fd49 517
f4c6fd49 518}
519
f4c6fd49 520sub uncompress($)
521{
642e522c 522 my ($x, $output, $err, $in) =('', '', '', '') ;
f4c6fd49 523
524 if (ref $_[0] ) {
525 $in = $_[0] ;
642e522c 526 croak "not a scalar reference" unless ref $in eq 'SCALAR' ;
f4c6fd49 527 }
528 else {
529 $in = \$_[0] ;
530 }
531
642e522c 532 $x = new Compress::Zlib::Inflate -ConsumeInput => 0 or return undef ;
f4c6fd49 533
642e522c 534 $err = $x->inflate($in, $output) ;
535 return undef unless $err == Z_STREAM_END() ;
f4c6fd49 536
642e522c 537 return $output ;
538}
539
540
541### This stuff is for backward compat. with Compress::Zlib 1.x
542
f4c6fd49 543
642e522c 544sub deflateInit(@)
545{
546 my ($got) = ParseParameters(0,
547 {
548 'Bufsize' => [Parse_unsigned, 4096],
549 'Level' => [Parse_signed, Z_DEFAULT_COMPRESSION()],
550 'Method' => [Parse_unsigned, Z_DEFLATED()],
551 'WindowBits' => [Parse_signed, MAX_WBITS()],
552 'MemLevel' => [Parse_unsigned, MAX_MEM_LEVEL()],
553 'Strategy' => [Parse_unsigned, Z_DEFAULT_STRATEGY()],
554 'Dictionary' => [Parse_any, ""],
555 }, @_ ) ;
556
557 croak "Compress::Zlib::deflateInit: Bufsize must be >= 1, you specified " .
558 $got->value('Bufsize')
559 unless $got->value('Bufsize') >= 1;
560
561 my (%obj) = () ;
562
563 my $status = 0 ;
564 ($obj{def}, $status) =
565 _deflateInit(0,
566 $got->value('Level'),
567 $got->value('Method'),
568 $got->value('WindowBits'),
569 $got->value('MemLevel'),
570 $got->value('Strategy'),
571 $got->value('Bufsize'),
572 $got->value('Dictionary')) ;
573
574 my $x = ($status == Z_OK() ? bless \%obj, "Zlib::OldDeflate" : undef) ;
575 return wantarray ? ($x, $status) : $x ;
f4c6fd49 576}
642e522c 577
578sub inflateInit(@)
579{
580 my ($got) = ParseParameters(0,
581 {
582 'Bufsize' => [Parse_unsigned, 4096],
583 'WindowBits' => [Parse_signed, MAX_WBITS()],
584 'Dictionary' => [Parse_any, ""],
585 }, @_) ;
f4c6fd49 586
587
642e522c 588 croak "Compress::Zlib::inflateInit: Bufsize must be >= 1, you specified " .
589 $got->value('Bufsize')
590 unless $got->value('Bufsize') >= 1;
f4c6fd49 591
642e522c 592 my $status = 0 ;
593 my (%obj) = () ;
594 ($obj{def}, $status) = _inflateInit(FLAG_CONSUME_INPUT,
595 $got->value('WindowBits'),
596 $got->value('Bufsize'),
597 $got->value('Dictionary')) ;
598
599 my $x = ($status == Z_OK() ? bless \%obj, "Zlib::OldInflate" : undef) ;
600
601 wantarray ? ($x, $status) : $x ;
602}
603
604package Zlib::OldDeflate ;
605
606sub deflate
607{
608 my $self = shift ;
609 my $output ;
610 #my (@rest) = @_ ;
611
612 my $status = $self->{def}->deflate($_[0], $output) ;
613
614 wantarray ? ($output, $status) : $output ;
615}
616
617sub flush
618{
619 my $self = shift ;
620 my $output ;
621 my $flag = shift || Compress::Zlib::Z_FINISH();
622 my $status = $self->{def}->flush($output, $flag) ;
623
624 wantarray ? ($output, $status) : $output ;
625}
626
627sub deflateParams
628{
629 my $self = shift ;
630 $self->{def}->deflateParams(@_) ;
631}
632
633sub msg
634{
635 my $self = shift ;
636 $self->{def}->msg() ;
637}
638
639sub total_in
640{
641 my $self = shift ;
642 $self->{def}->total_in() ;
643}
644
645sub total_out
646{
647 my $self = shift ;
648 $self->{def}->total_out() ;
649}
650
651sub dict_adler
652{
653 my $self = shift ;
654 $self->{def}->dict_adler() ;
655}
656
657sub get_Level
658{
659 my $self = shift ;
660 $self->{def}->get_Level() ;
661}
662
663sub get_Strategy
664{
665 my $self = shift ;
666 $self->{def}->get_Strategy() ;
667}
668
669#sub DispStream
670#{
671# my $self = shift ;
672# $self->{def}->DispStream($_[0]) ;
673#}
674
675package Zlib::OldInflate ;
676
677sub inflate
678{
679 my $self = shift ;
680 my $output ;
681 my $status = $self->{def}->inflate($_[0], $output) ;
682 wantarray ? ($output, $status) : $output ;
683}
684
685sub inflateSync
686{
687 my $self = shift ;
688 $self->{def}->inflateSync($_[0]) ;
689}
690
691sub msg
692{
693 my $self = shift ;
694 $self->{def}->msg() ;
695}
696
697sub total_in
698{
699 my $self = shift ;
700 $self->{def}->total_in() ;
701}
702
703sub total_out
704{
705 my $self = shift ;
706 $self->{def}->total_out() ;
707}
708
709sub dict_adler
710{
711 my $self = shift ;
712 $self->{def}->dict_adler() ;
713}
714
715#sub DispStream
716#{
717# my $self = shift ;
718# $self->{def}->DispStream($_[0]) ;
719#}
720
721package Compress::Zlib ;
722
723use Compress::Gzip::Constants;
f4c6fd49 724
f4c6fd49 725sub memGzip($)
726{
642e522c 727 my $x = new Compress::Zlib::Deflate(
728 -AppendOutput => 1,
729 -CRC32 => 1,
730 -ADLER32 => 0,
f4c6fd49 731 -Level => Z_BEST_COMPRESSION(),
642e522c 732 -WindowBits => - MAX_WBITS(),
f4c6fd49 733 )
734 or return undef ;
735
736 # write a minimal gzip header
642e522c 737 my $output = GZIP_MINIMUM_HEADER ;
f4c6fd49 738
739 # if the deflation buffer isn't a reference, make it one
740 my $string = (ref $_[0] ? $_[0] : \$_[0]) ;
741
642e522c 742 my $status = $x->deflate($string, \$output) ;
f4c6fd49 743 $status == Z_OK()
744 or return undef ;
745
642e522c 746 $status = $x->flush(\$output) ;
f4c6fd49 747 $status == Z_OK()
748 or return undef ;
749
642e522c 750 return $output . pack("V V", $x->crc32(), $x->total_in()) ;
f4c6fd49 751
f4c6fd49 752}
753
642e522c 754
f4c6fd49 755sub _removeGzipHeader($)
756{
757 my $string = shift ;
758
759 return Z_DATA_ERROR()
642e522c 760 if length($$string) < GZIP_MIN_HEADER_SIZE ;
f4c6fd49 761
762 my ($magic1, $magic2, $method, $flags, $time, $xflags, $oscode) =
763 unpack ('CCCCVCC', $$string);
764
765 return Z_DATA_ERROR()
642e522c 766 unless $magic1 == GZIP_ID1 and $magic2 == GZIP_ID2 and
767 $method == Z_DEFLATED() and !($flags & GZIP_FLG_RESERVED) ;
768 substr($$string, 0, GZIP_MIN_HEADER_SIZE) = '' ;
f4c6fd49 769
770 # skip extra field
642e522c 771 if ($flags & GZIP_FLG_FEXTRA)
f4c6fd49 772 {
773 return Z_DATA_ERROR()
642e522c 774 if length($$string) < GZIP_FEXTRA_HEADER_SIZE ;
f4c6fd49 775
776 my ($extra_len) = unpack ('v', $$string);
642e522c 777 $extra_len += GZIP_FEXTRA_HEADER_SIZE;
f4c6fd49 778 return Z_DATA_ERROR()
779 if length($$string) < $extra_len ;
780
781 substr($$string, 0, $extra_len) = '';
782 }
783
784 # skip orig name
642e522c 785 if ($flags & GZIP_FLG_FNAME)
f4c6fd49 786 {
642e522c 787 my $name_end = index ($$string, GZIP_NULL_BYTE);
f4c6fd49 788 return Z_DATA_ERROR()
789 if $name_end == -1 ;
790 substr($$string, 0, $name_end + 1) = '';
791 }
792
793 # skip comment
642e522c 794 if ($flags & GZIP_FLG_FCOMMENT)
f4c6fd49 795 {
642e522c 796 my $comment_end = index ($$string, GZIP_NULL_BYTE);
f4c6fd49 797 return Z_DATA_ERROR()
798 if $comment_end == -1 ;
799 substr($$string, 0, $comment_end + 1) = '';
800 }
801
802 # skip header crc
642e522c 803 if ($flags & GZIP_FLG_FHCRC)
f4c6fd49 804 {
805 return Z_DATA_ERROR()
642e522c 806 if length ($$string) < GZIP_FHCRC_SIZE ;
807 substr($$string, 0, GZIP_FHCRC_SIZE) = '';
f4c6fd49 808 }
809
810 return Z_OK();
811}
812
813
814sub memGunzip($)
815{
816 # if the buffer isn't a reference, make it one
817 my $string = (ref $_[0] ? $_[0] : \$_[0]);
818
819 _removeGzipHeader($string) == Z_OK()
820 or return undef;
821
822 my $bufsize = length $$string > 4096 ? length $$string : 4096 ;
642e522c 823 my $x = new Compress::Zlib::Inflate({-WindowBits => - MAX_WBITS(),
824 -Bufsize => $bufsize})
825
f4c6fd49 826 or return undef;
642e522c 827
828 my $output = "" ;
829 my $status = $x->inflate($string, $output);
f4c6fd49 830 return undef
831 unless $status == Z_STREAM_END();
832
833 if (length $$string >= 8)
834 {
835 my ($crc, $len) = unpack ("VV", substr($$string, 0, 8));
836 substr($$string, 0, 8) = '';
837 return undef
838 unless $len == length($output) and
839 $crc == crc32($output);
840 }
841 else
842 {
843 $$string = '';
844 }
f4c6fd49 845 return $output;
846}
847
848# Autoload methods go after __END__, and are processed by the autosplit program.
849
8501;
851__END__
852
f4c6fd49 853
854=head1 NAME
855
856Compress::Zlib - Interface to zlib compression library
857
858=head1 SYNOPSIS
859
642e522c 860 use Compress::Zlib 2 ;
f4c6fd49 861
642e522c 862 ($d, $status) = new Compress::Zlib::Deflate( [OPT] ) ;
863 $status = $d->deflate($input, $output) ;
864 $status = $d->flush($output [, $flush_type]) ;
865 $d->deflateParams(OPTS) ;
866 $d->deflateTune(OPTS) ;
f4c6fd49 867 $d->dict_adler() ;
642e522c 868 $d->crc32() ;
869 $d->adler32() ;
f4c6fd49 870 $d->total_in() ;
871 $d->total_out() ;
872 $d->msg() ;
642e522c 873 $d->get_Strategy();
874 $d->get_Level();
875 $d->get_BufSize();
f4c6fd49 876
642e522c 877 ($i, $status) = new Compress::Zlib::Inflate( [OPT] ) ;
878 $status = $i->inflate($input, $output) ;
879 $status = $i->inflateSync($input) ;
f4c6fd49 880 $i->dict_adler() ;
642e522c 881 $d->crc32() ;
882 $d->adler32() ;
f4c6fd49 883 $i->total_in() ;
884 $i->total_out() ;
885 $i->msg() ;
642e522c 886 $d->get_BufSize();
f4c6fd49 887
642e522c 888 $dest = compress($source) ;
f4c6fd49 889 $dest = uncompress($source) ;
890
891 $gz = gzopen($filename or filehandle, $mode) ;
892 $bytesread = $gz->gzread($buffer [,$size]) ;
893 $bytesread = $gz->gzreadline($line) ;
894 $byteswritten = $gz->gzwrite($buffer) ;
895 $status = $gz->gzflush($flush) ;
642e522c 896 $offset = $gz->gztell() ;
897 $status = $gz->gzseek($offset, $whence) ;
f4c6fd49 898 $status = $gz->gzclose() ;
899 $status = $gz->gzeof() ;
900 $status = $gz->gzsetparams($level, $strategy) ;
901 $errstring = $gz->gzerror() ;
902 $gzerrno
903
904 $dest = Compress::Zlib::memGzip($buffer) ;
905 $dest = Compress::Zlib::memGunzip($buffer) ;
906
907 $crc = adler32($buffer [,$crc]) ;
908 $crc = crc32($buffer [,$crc]) ;
909
642e522c 910 $crc = adler32_combine($crc1, $crc2, $len2)l
911 $crc = crc32_combine($adler1, $adler2, $len2)
912
f4c6fd49 913 ZLIB_VERSION
642e522c 914 ZLIB_VERNUM
915
916 # Compress::Zlib 1.x legacy interface
917
918 ($d, $status) = deflateInit( [OPT] ) ;
919 ($out, $status) = $d->deflate($buffer) ;
920 $status = $d->deflateParams([OPT]) ;
921 ($out, $status) = $d->flush() ;
922 $d->dict_adler() ;
923 $d->total_in() ;
924 $d->total_out() ;
925 $d->msg() ;
926
927 ($i, $status) = inflateInit( [OPT] ) ;
928 ($out, $status) = $i->inflate($buffer) ;
929 $status = $i->inflateSync($buffer) ;
930 $i->dict_adler() ;
931 $i->total_in() ;
932 $i->total_out() ;
933 $i->msg() ;
934
f4c6fd49 935
936=head1 DESCRIPTION
937
938The I<Compress::Zlib> module provides a Perl interface to the I<zlib>
939compression library (see L</AUTHOR> for details about where to get
642e522c 940I<zlib>).
941The I<zlib> library allows reading and writing of
942compressed data streams that conform to RFC1950, RFC1951 and RFC1952
943(aka gzip).
944Most of the I<zlib> functionality is available in I<Compress::Zlib>.
f4c6fd49 945
642e522c 946Unless you are working with legacy code, or you need to work directly
947with the low-level zlib interface, it is recommended that applications
948use one of the newer C<IO::*> interfaces provided with this module.
f4c6fd49 949
642e522c 950The C<Compress::Zlib> module can be split into two general areas of
951functionality, namely a low-level in-memory compression/decompression
952interface and a simple read/write interface to I<gzip> files.
f4c6fd49 953
642e522c 954Each of these areas will be discussed separately below.
f4c6fd49 955
f4c6fd49 956
642e522c 957=head1 GZIP INTERFACE
f4c6fd49 958
642e522c 959A number of functions are supplied in I<zlib> for reading and writing
960I<gzip> files that conform to RFC1952. This module provides an interface
961to most of them.
f4c6fd49 962
642e522c 963If you are upgrading from C<Compress::Zlib> 1.x, the following enhancements
964have been made to the C<gzopen> interface:
f4c6fd49 965
642e522c 966=over 5
f4c6fd49 967
642e522c 968=item 1
f4c6fd49 969
642e522c 970If you want to to open either STDIN or STDOUT with C<gzopen>, you can
971optionally use the special filename "C<->" as a synonym for C<\*STDIN> and
972C<\*STDOUT>.
f4c6fd49 973
642e522c 974=item 2
f4c6fd49 975
642e522c 976In C<Compress::Zlib> version 1.x, C<gzopen> used the zlib library to open the
977underlying file. This made things especially tricky when a Perl filehandle was
978passed to C<gzopen>. Behind the scenes the numeric C file descriptor had to be
979extracted from the Perl filehandle and this passed to the zlib library.
f4c6fd49 980
642e522c 981Apart from being non-portable to some operating systems, this made it difficult
982to use C<gzopen> in situations where you wanted to extract/create a gzip data
983stream that is embedded in a larger file, without having to resort to opening
984and closing the file multiple times.
f4c6fd49 985
642e522c 986In C<Compress::Zlib> version 2.x, the C<gzopen> interface has been completely
987rewritten to use the L<IO::Compress::Gzip|IO::Compress::Gzip> for writing gzip files and
988L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip> for reading gzip files.
f4c6fd49 989
642e522c 990=item 3
f4c6fd49 991
642e522c 992Addition of C<gzseek> to provide a restricted C<seek> interface.
f4c6fd49 993
642e522c 994=item 4.
f4c6fd49 995
642e522c 996Added C<gztell>.
f4c6fd49 997
642e522c 998=back
f4c6fd49 999
642e522c 1000A more complete and flexible interface for reading/writing gzip files/buffers
1001is included with this module. See L<IO::Compress::Gzip|IO::Compress::Gzip> and
1002L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip> for more details.
f4c6fd49 1003
642e522c 1004=over 5
f4c6fd49 1005
642e522c 1006=item B<$gz = gzopen($filename, $mode)>
f4c6fd49 1007
642e522c 1008=item B<$gz = gzopen($filehandle, $mode)>
f4c6fd49 1009
642e522c 1010This function opens either the I<gzip> file C<$filename> for reading or writing
1011or attaches to the opened filehandle, C<$filehandle>. It returns an object on
1012success and C<undef> on failure.
f4c6fd49 1013
642e522c 1014When writing a gzip file this interface will always create the smallest
1015possible gzip header (exactly 10 bytes). If you want control over the
1016information stored in the gzip header (like the original filename or a comment)
1017use L<IO::Compress::Gzip|IO::Compress::Gzip> instead.
f4c6fd49 1018
642e522c 1019The second parameter, C<$mode>, is used to specify whether the file is
1020opened for reading or writing and to optionally specify a compression
1021level and compression strategy when writing. The format of the C<$mode>
1022parameter is similar to the mode parameter to the 'C' function C<fopen>,
1023so "rb" is used to open for reading and "wb" for writing.
f4c6fd49 1024
642e522c 1025To specify a compression level when writing, append a digit between 0
1026and 9 to the mode string -- 0 means no compression and 9 means maximum
1027compression.
1028If no compression level is specified Z_DEFAULT_COMPRESSION is used.
f4c6fd49 1029
642e522c 1030To specify the compression strategy when writing, append 'f' for filtered
1031data, 'h' for Huffman only compression, or 'R' for run-length encoding.
1032If no strategy is specified Z_DEFAULT_STRATEGY is used.
f4c6fd49 1033
642e522c 1034So, for example, "wb9" means open for writing with the maximum compression
1035using the default strategy and "wb4R" means open for writing with compression
1036level 4 and run-length encoding.
f4c6fd49 1037
642e522c 1038Refer to the I<zlib> documentation for the exact format of the C<$mode>
1039parameter.
f4c6fd49 1040
f4c6fd49 1041
642e522c 1042=item B<$bytesread = $gz-E<gt>gzread($buffer [, $size]) ;>
1043
1044Reads C<$size> bytes from the compressed file into C<$buffer>. If
1045C<$size> is not specified, it will default to 4096. If the scalar
1046C<$buffer> is not large enough, it will be extended automatically.
1047
1048Returns the number of bytes actually read. On EOF it returns 0 and in
1049the case of an error, -1.
1050
1051=item B<$bytesread = $gz-E<gt>gzreadline($line) ;>
1052
1053Reads the next line from the compressed file into C<$line>.
1054
1055Returns the number of bytes actually read. On EOF it returns 0 and in
1056the case of an error, -1.
1057
1058It is legal to intermix calls to C<gzread> and C<gzreadline>.
1059
1060In addition, C<gzreadline> fully supports the use of of the variable C<$/>
1061(C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
1062determine what constitutes an end of line. Both paragraph mode and file
1063slurp mode are supported.
1064
1065
1066=item B<$byteswritten = $gz-E<gt>gzwrite($buffer) ;>
1067
1068Writes the contents of C<$buffer> to the compressed file. Returns the
1069number of bytes actually written, or 0 on error.
1070
1071=item B<$status = $gz-E<gt>gzflush($flush_type) ;>
1072
1073Flushes all pending output into the compressed file.
1074
1075This method takes an optional parameter, C<$flush_type>, that controls
1076how the flushing will be carried out. By default the C<$flush_type>
1077used is C<Z_FINISH>. Other valid values for C<$flush_type> are
1078C<Z_NO_FLUSH>, C<Z_SYNC_FLUSH>, C<Z_FULL_FLUSH> and C<Z_BLOCK>. It is
1079strongly recommended that you only set the C<flush_type> parameter if
1080you fully understand the implications of what it does - overuse of C<flush>
1081can seriously degrade the level of compression achieved. See the C<zlib>
1082documentation for details.
1083
1084Returns 1 on success, 0 on failure.
1085
1086
1087=item B<$offset = $gz-E<gt>gztell() ;>
1088
1089Returns the uncompressed file offset.
1090
1091=item B<$status = $gz-E<gt>gzseek($offset, $whence) ;>
1092
1093Sets the file position of the
1094
1095Provides a sub-set of the C<seek> functionality, with the restriction
1096that it is only legal to seek forward in the compressed file.
1097It is a fatal error to attempt to seek backward.
1098
1099When opened for writing, empty parts of the file will have NULL (0x00)
1100bytes written to them.
1101
1102The C<$whence> parameter should be one of SEEK_SET, SEEK_CUR or SEEK_END.
1103
1104Returns 1 on success, 0 on failure.
1105
1106=item B<$gz-E<gt>gzclose>
1107
1108Closes the compressed file. Any pending data is flushed to the file
1109before it is closed.
1110
1111Returns 1 on success, 0 on failure.
1112
1113=item B<$gz-E<gt>gzsetparams($level, $strategy>
1114
1115Change settings for the deflate stream C<$gz>.
1116
1117The list of the valid options is shown below. Options not specified
1118will remain unchanged.
1119
1120Note: This method is only available if you are running zlib 1.0.6 or better.
1121
1122=over 5
1123
1124=item B<$level>
1125
1126Defines the compression level. Valid values are 0 through 9,
1127C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
1128C<Z_DEFAULT_COMPRESSION>.
1129
1130=item B<$strategy>
1131
1132Defines the strategy used to tune the compression. The valid values are
1133C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
1134
1135=back
1136
1137=item B<$gz-E<gt>gzerror>
1138
1139Returns the I<zlib> error message or number for the last operation
1140associated with C<$gz>. The return value will be the I<zlib> error
1141number when used in a numeric context and the I<zlib> error message
1142when used in a string context. The I<zlib> error number constants,
1143shown below, are available for use.
1144
1145 Z_OK
1146 Z_STREAM_END
1147 Z_ERRNO
1148 Z_STREAM_ERROR
1149 Z_DATA_ERROR
1150 Z_MEM_ERROR
1151 Z_BUF_ERROR
1152
1153=item B<$gzerrno>
1154
1155The C<$gzerrno> scalar holds the error code associated with the most
1156recent I<gzip> routine. Note that unlike C<gzerror()>, the error is
1157I<not> associated with a particular file.
1158
1159As with C<gzerror()> it returns an error number in numeric context and
1160an error message in string context. Unlike C<gzerror()> though, the
1161error message will correspond to the I<zlib> message when the error is
1162associated with I<zlib> itself, or the UNIX error message when it is
1163not (i.e. I<zlib> returned C<Z_ERRORNO>).
1164
1165As there is an overlap between the error numbers used by I<zlib> and
1166UNIX, C<$gzerrno> should only be used to check for the presence of
1167I<an> error in numeric context. Use C<gzerror()> to check for specific
1168I<zlib> errors. The I<gzcat> example below shows how the variable can
1169be used safely.
1170
1171=back
1172
1173
1174=head2 Examples
1175
1176Here is an example script which uses the interface. It implements a
1177I<gzcat> function.
1178
1179 use strict ;
1180 use warnings ;
1181
1182 use Compress::Zlib ;
1183
1184 # use stdin if no files supplied
1185 @ARGV = '-' unless @ARGV ;
1186
1187 foreach my $file (@ARGV) {
1188 my $buffer ;
1189
1190 my $gz = gzopen($file, "rb")
1191 or die "Cannot open $file: $gzerrno\n" ;
1192
1193 print $buffer while $gz->gzread($buffer) > 0 ;
1194
1195 die "Error reading from $file: $gzerrno" . ($gzerrno+0) . "\n"
1196 if $gzerrno != Z_STREAM_END ;
1197
1198 $gz->gzclose() ;
1199 }
1200
1201Below is a script which makes use of C<gzreadline>. It implements a
1202very simple I<grep> like script.
1203
1204 use strict ;
1205 use warnings ;
1206
1207 use Compress::Zlib ;
1208
1209 die "Usage: gzgrep pattern [file...]\n"
1210 unless @ARGV >= 1;
1211
1212 my $pattern = shift ;
1213
1214 # use stdin if no files supplied
1215 @ARGV = '-' unless @ARGV ;
1216
1217 foreach my $file (@ARGV) {
1218 my $gz = gzopen($file, "rb")
1219 or die "Cannot open $file: $gzerrno\n" ;
1220
1221 while ($gz->gzreadline($_) > 0) {
1222 print if /$pattern/ ;
1223 }
1224
1225 die "Error reading from $file: $gzerrno\n"
1226 if $gzerrno != Z_STREAM_END ;
1227
1228 $gz->gzclose() ;
1229 }
1230
1231This script, I<gzstream>, does the opposite of the I<gzcat> script
1232above. It reads from standard input and writes a gzip data stream to
1233standard output.
1234
1235 use strict ;
1236 use warnings ;
1237
1238 use Compress::Zlib ;
1239
1240 binmode STDOUT; # gzopen only sets it on the fd
1241
1242 my $gz = gzopen(\*STDOUT, "wb")
1243 or die "Cannot open stdout: $gzerrno\n" ;
1244
1245 while (<>) {
1246 $gz->gzwrite($_)
1247 or die "error writing: $gzerrno\n" ;
1248 }
1249
1250 $gz->gzclose ;
1251
1252=head2 Compress::Zlib::memGzip
1253
1254This function is used to create an in-memory gzip file with the minimum
1255possible gzip header (exactly 10 bytes).
1256
1257 $dest = Compress::Zlib::memGzip($buffer) ;
1258
1259If successful, it returns the in-memory gzip file, otherwise it returns
1260undef.
1261
1262The C<$buffer> parameter can either be a scalar or a scalar reference.
1263
1264See L<IO::Compress::Gzip|IO::Compress::Gzip> for an alternative way to carry out in-memory gzip
1265compression.
1266
1267=head2 Compress::Zlib::memGunzip
1268
1269This function is used to uncompress an in-memory gzip file.
1270
1271 $dest = Compress::Zlib::memGunzip($buffer) ;
1272
1273If successful, it returns the uncompressed gzip file, otherwise it
1274returns undef.
1275
1276The C<$buffer> parameter can either be a scalar or a scalar reference. The
1277contents of the C<$buffer> parameter are destroyed after calling this function.
1278
1279See L<IO::Uncompress::Gunzip|IO::Uncompress::Gunzip> for an alternative way to carry out in-memory gzip
1280uncompression.
1281
1282=head1 COMPRESS/UNCOMPRESS
1283
1284Two functions are provided to perform in-memory compression/uncompression of
1285RFC 1950 data streams. They are called C<compress> and C<uncompress>.
1286
1287=over 5
1288
1289=item B<$dest = compress($source [, $level] ) ;>
1290
1291Compresses C<$source>. If successful it returns the compressed
1292data. Otherwise it returns I<undef>.
1293
1294The source buffer, C<$source>, can either be a scalar or a scalar
1295reference.
1296
1297The C<$level> parameter defines the compression level. Valid values are
12980 through 9, C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>,
1299C<Z_BEST_COMPRESSION>, and C<Z_DEFAULT_COMPRESSION>.
1300If C<$level> is not specified C<Z_DEFAULT_COMPRESSION> will be used.
1301
1302
1303=item B<$dest = uncompress($source) ;>
1304
1305Uncompresses C<$source>. If successful it returns the uncompressed
1306data. Otherwise it returns I<undef>.
1307
1308The source buffer can either be a scalar or a scalar reference.
1309
1310=back
1311
1312Please note: the two functions defined above are I<not> compatible with
1313the Unix commands of the same name.
1314
1315See L<IO::Compress::Deflate|IO::Compress::Deflate> and L<IO::Uncompress::Inflate|IO::Uncompress::Inflate> included with
1316this distribution for an alternative interface for reading/writing RFC 1950
1317files/buffers.
1318
1319=head1 CHECKSUM FUNCTIONS
1320
1321Two functions are provided by I<zlib> to calculate checksums. For the
1322Perl interface, the order of the two parameters in both functions has
1323been reversed. This allows both running checksums and one off
1324calculations to be done.
1325
1326 $crc = adler32($buffer [,$crc]) ;
1327 $crc = crc32($buffer [,$crc]) ;
1328
1329The buffer parameters can either be a scalar or a scalar reference.
1330
1331If the $crc parameters is C<undef>, the crc value will be reset.
1332
1333If you have built this module with zlib 1.2.3 or better, two more
1334CRC-related functions are available.
1335
1336 $crc = adler32_combine($crc1, $crc2, $len2)l
1337 $crc = crc32_combine($adler1, $adler2, $len2)
1338
1339These functions allow checksums to be merged.
1340
1341=head1 Compress::Zlib::Deflate
1342
1343This section defines an interface that allows in-memory compression using
1344the I<deflate> interface provided by zlib.
1345
1346Note: The interface defined in this section is different from version
13471.x of this module. The original deflate interface is still available
1348for backward compatibility and is documented in the section
1349L<Compress::Zlib 1.x Deflate Interface>.
1350
1351Here is a definition of the interface available:
1352
1353
1354=head2 B<($d, $status) = new Compress::Zlib::Deflate( [OPT] ) >
1355
1356Initialises a deflation object.
1357
1358If you are familiar with the I<zlib> library, it combines the
1359features of the I<zlib> functions C<deflateInit>, C<deflateInit2>
1360and C<deflateSetDictionary>.
1361
1362If successful, it will return the initialised deflation object, C<$d>
1363and a C<$status> of C<Z_OK> in a list context. In scalar context it
1364returns the deflation object, C<$d>, only.
1365
1366If not successful, the returned deflation object, C<$d>, will be
1367I<undef> and C<$status> will hold the a I<zlib> error code.
1368
1369The function optionally takes a number of named options specified as
1370C<-Name =E<gt> value> pairs. This allows individual options to be
1371tailored without having to specify them all in the parameter list.
1372
1373For backward compatibility, it is also possible to pass the parameters
1374as a reference to a hash containing the name=>value pairs.
1375
1376Below is a list of the valid options:
1377
1378=over 5
1379
1380=item B<-Level>
1381
1382Defines the compression level. Valid values are 0 through 9,
1383C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
1384C<Z_DEFAULT_COMPRESSION>.
1385
1386The default is C<-Level =E<gt> Z_DEFAULT_COMPRESSION>.
1387
1388=item B<-Method>
1389
1390Defines the compression method. The only valid value at present (and
1391the default) is C<-Method =E<gt> Z_DEFLATED>.
1392
1393=item B<-WindowBits>
1394
1395For a definition of the meaning and valid values for C<WindowBits>
1396refer to the I<zlib> documentation for I<deflateInit2>.
1397
1398Defaults to C<-WindowBits =E<gt> MAX_WBITS>.
1399
1400=item B<-MemLevel>
1401
1402For a definition of the meaning and valid values for C<MemLevel>
1403refer to the I<zlib> documentation for I<deflateInit2>.
1404
1405Defaults to C<-MemLevel =E<gt> MAX_MEM_LEVEL>.
1406
1407=item B<-Strategy>
1408
1409Defines the strategy used to tune the compression. The valid values are
1410C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED>, C<Z_RLE>, C<Z_FIXED> and
1411C<Z_HUFFMAN_ONLY>.
1412
1413The default is C<-Strategy =E<gt>Z_DEFAULT_STRATEGY>.
1414
1415=item B<-Dictionary>
1416
1417When a dictionary is specified I<Compress::Zlib> will automatically
1418call C<deflateSetDictionary> directly after calling C<deflateInit>. The
1419Adler32 value for the dictionary can be obtained by calling the method
1420C<$d-E<gt>dict_adler()>.
f4c6fd49 1421
1422The default is no dictionary.
1423
1424=item B<-Bufsize>
1425
642e522c 1426Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
1427and C<$d-E<gt>flush> methods. If the buffer has to be
f4c6fd49 1428reallocated to increase the size, it will grow in increments of
642e522c 1429C<Bufsize>.
1430
1431The default buffer size is 4096.
1432
1433=item B<-AppendOutput>
1434
1435This option controls how data is written to the output buffer by the
1436C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
1437
1438If the C<AppendOutput> option is set to false, the output buffers in the
1439C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods will be truncated before
1440uncompressed data is written to them.
1441
1442If the option is set to true, uncompressed data will be appended to the
1443output buffer in the C<$d-E<gt>deflate> and C<$d-E<gt>flush> methods.
1444
1445This option defaults to false.
1446
1447=item B<-CRC32>
1448
1449If set to true, a crc32 checksum of the uncompressed data will be
1450calculated. Use the C<$d-E<gt>crc32> method to retrieve this value.
1451
1452This option defaults to false.
1453
1454
1455=item B<-ADLER32>
1456
1457If set to true, an adler32 checksum of the uncompressed data will be
1458calculated. Use the C<$d-E<gt>adler32> method to retrieve this value.
1459
1460This option defaults to false.
f4c6fd49 1461
f4c6fd49 1462
1463=back
1464
642e522c 1465Here is an example of using the C<Compress::Zlib::Deflate> optional
1466parameter list to override the default buffer size and compression
1467level. All other options will take their default values.
f4c6fd49 1468
642e522c 1469 my $d = new Compress::Zlib::Deflate ( -Bufsize => 300,
1470 -Level => Z_BEST_SPEED ) ;
f4c6fd49 1471
1472
642e522c 1473=head2 B<$status = $d-E<gt>deflate($input, $output)>
f4c6fd49 1474
642e522c 1475Deflates the contents of C<$input> and writes the compressed data to
1476C<$output>.
f4c6fd49 1477
642e522c 1478The C<$input> and C<$output> parameters can be either scalars or scalar
1479references.
f4c6fd49 1480
642e522c 1481When finished, C<$input> will be completely processed (assuming there
1482were no errors). If the deflation was successful it writes the deflated
1483data to C<$output> and returns a status value of C<Z_OK>.
f4c6fd49 1484
642e522c 1485On error, it returns a I<zlib> error code.
f4c6fd49 1486
642e522c 1487If the C<AppendOutput> option is set to true in the constructor for
1488the C<$d> object, the compressed data will be appended to C<$output>. If
1489it is false, C<$output> will be truncated before any compressed data is
1490written to it.
f4c6fd49 1491
642e522c 1492B<Note>: This method will not necessarily write compressed data to
1493C<$output> every time it is called. So do not assume that there has been
1494an error if the contents of C<$output> is empty on returning from
1495this method. As long as the return code from the method is C<Z_OK>,
1496the deflate has succeeded.
f4c6fd49 1497
642e522c 1498=head2 B<$status = $d-E<gt>flush($output [, $flush_type]) >
f4c6fd49 1499
1500Typically used to finish the deflation. Any pending output will be
642e522c 1501written to C<$output>.
f4c6fd49 1502
642e522c 1503Returns C<Z_OK> if successful.
f4c6fd49 1504
1505Note that flushing can seriously degrade the compression ratio, so it
1506should only be used to terminate a decompression (using C<Z_FINISH>) or
1507when you want to create a I<full flush point> (using C<Z_FULL_FLUSH>).
1508
1509By default the C<flush_type> used is C<Z_FINISH>. Other valid values
1510for C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH>
1511and C<Z_FULL_FLUSH>. It is strongly recommended that you only set the
1512C<flush_type> parameter if you fully understand the implications of
1513what it does. See the C<zlib> documentation for details.
1514
642e522c 1515If the C<AppendOutput> option is set to true in the constructor for
1516the C<$d> object, the compressed data will be appended to C<$output>. If
1517it is false, C<$output> will be truncated before any compressed data is
1518written to it.
1519
f4c6fd49 1520=head2 B<$status = $d-E<gt>deflateParams([OPT])>
1521
642e522c 1522Change settings for the deflate object C<$d>.
f4c6fd49 1523
1524The list of the valid options is shown below. Options not specified
1525will remain unchanged.
1526
642e522c 1527
f4c6fd49 1528=over 5
1529
1530=item B<-Level>
1531
1532Defines the compression level. Valid values are 0 through 9,
1533C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
1534C<Z_DEFAULT_COMPRESSION>.
1535
1536=item B<-Strategy>
1537
1538Defines the strategy used to tune the compression. The valid values are
1539C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
1540
642e522c 1541=item B<-BufSize>
1542
1543Sets the initial size for the output buffer used by the C<$d-E<gt>deflate>
1544and C<$d-E<gt>flush> methods. If the buffer has to be
1545reallocated to increase the size, it will grow in increments of
1546C<Bufsize>.
1547
1548
f4c6fd49 1549=back
1550
642e522c 1551=head2 B<$status = $d-E<gt>deflateTune($good_length, $max_lazy, $nice_length, $max_chain)>
1552
1553Tune the internal settings for the deflate object C<$d>. This option is
1554only available if you are running zlib 1.2.2.3 or better.
1555
1556Refer to the documentation in zlib.h for instructions on how to fly
1557C<deflateTune>.
1558
f4c6fd49 1559=head2 B<$d-E<gt>dict_adler()>
1560
1561Returns the adler32 value for the dictionary.
1562
642e522c 1563=head2 B<$d-E<gt>crc32()>
1564
1565Returns the crc32 value for the uncompressed data to date.
1566
1567If the C<CRC32> option is not enabled in the constructor for this object,
1568this method will always return 0;
1569
1570=head2 B<$d-E<gt>adler32()>
1571
1572Returns the adler32 value for the uncompressed data to date.
1573
f4c6fd49 1574=head2 B<$d-E<gt>msg()>
1575
1576Returns the last error message generated by zlib.
1577
1578=head2 B<$d-E<gt>total_in()>
1579
1580Returns the total number of bytes uncompressed bytes input to deflate.
1581
1582=head2 B<$d-E<gt>total_out()>
1583
1584Returns the total number of compressed bytes output from deflate.
1585
642e522c 1586=head2 B<$d-E<gt>get_Strategy()>
1587
1588Returns the deflation strategy currently used. Valid values are
1589C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
1590
1591
1592=head2 B<$d-E<gt>get_Level()>
1593
1594Returns the compression level being used.
1595
1596=head2 B<$d-E<gt>get_BufSize()>
1597
1598Returns the buffer size used to carry out the compression.
1599
f4c6fd49 1600=head2 Example
1601
1602
642e522c 1603Here is a trivial example of using C<deflate>. It simply reads standard
f4c6fd49 1604input, deflates it and writes it to standard output.
1605
1606 use strict ;
1607 use warnings ;
1608
642e522c 1609 use Compress::Zlib 2 ;
f4c6fd49 1610
1611 binmode STDIN;
1612 binmode STDOUT;
642e522c 1613 my $x = new Compress::Zlib::Deflate
f4c6fd49 1614 or die "Cannot create a deflation stream\n" ;
1615
1616 my ($output, $status) ;
1617 while (<>)
1618 {
642e522c 1619 $status = $x->deflate($_, $output) ;
f4c6fd49 1620
1621 $status == Z_OK
1622 or die "deflation failed\n" ;
1623
1624 print $output ;
1625 }
1626
642e522c 1627 $status = $x->flush($output) ;
f4c6fd49 1628
1629 $status == Z_OK
1630 or die "deflation failed\n" ;
1631
1632 print $output ;
1633
642e522c 1634=head1 Compress::Zlib::Inflate
1635
1636This section defines an interface that allows in-memory uncompression using
1637the I<inflate> interface provided by zlib.
1638
1639Note: The interface defined in this section is different from version
16401.x of this module. The original inflate interface is still available
1641for backward compatibility and is documented in the section
1642L<Compress::Zlib 1.x Inflate Interface>.
f4c6fd49 1643
1644Here is a definition of the interface:
1645
1646
642e522c 1647=head2 B< ($i, $status) = new Compress::Zlib::Inflate( [OPT] ) >
f4c6fd49 1648
642e522c 1649Initialises an inflation object.
f4c6fd49 1650
642e522c 1651In a list context it returns the inflation object, C<$i>, and the
1652I<zlib> status code (C<$status>). In a scalar context it returns the
1653inflation object only.
f4c6fd49 1654
642e522c 1655If successful, C<$i> will hold the inflation object and C<$status> will
f4c6fd49 1656be C<Z_OK>.
1657
642e522c 1658If not successful, C<$i> will be I<undef> and C<$status> will hold the
f4c6fd49 1659I<zlib> error code.
1660
1661The function optionally takes a number of named options specified as
642e522c 1662C<-Name =E<gt> value> pairs. This allows individual options to be
f4c6fd49 1663tailored without having to specify them all in the parameter list.
642e522c 1664
f4c6fd49 1665For backward compatibility, it is also possible to pass the parameters
642e522c 1666as a reference to a hash containing the name=E<gt>value pairs.
1667
f4c6fd49 1668Here is a list of the valid options:
1669
1670=over 5
1671
1672=item B<-WindowBits>
1673
642e522c 1674For a definition of the meaning and valid values for C<WindowBits>
f4c6fd49 1675refer to the I<zlib> documentation for I<inflateInit2>.
1676
1677Defaults to C<-WindowBits =E<gt>MAX_WBITS>.
1678
1679=item B<-Bufsize>
1680
642e522c 1681Sets the initial size for the output buffer used by the C<$i-E<gt>inflate>
1682method. If the output buffer in this method has to be reallocated to
1683increase the size, it will grow in increments of C<Bufsize>.
f4c6fd49 1684
1685Default is 4096.
1686
1687=item B<-Dictionary>
1688
1689The default is no dictionary.
1690
642e522c 1691=item B<-AppendOutput>
1692
1693This option controls how data is written to the output buffer by the
1694C<$i-E<gt>inflate> method.
1695
1696If the option is set to false, the output buffer in the C<$i-E<gt>inflate>
1697method will be truncated before uncompressed data is written to it.
1698
1699If the option is set to true, uncompressed data will be appended to the
1700output buffer by the C<$i-E<gt>inflate> method.
1701
1702This option defaults to false.
1703
1704
1705=item B<-CRC32>
1706
1707If set to true, a crc32 checksum of the uncompressed data will be
1708calculated. Use the C<$i-E<gt>crc32> method to retrieve this value.
1709
1710This option defaults to false.
1711
1712=item B<-ADLER32>
1713
1714If set to true, an adler32 checksum of the uncompressed data will be
1715calculated. Use the C<$i-E<gt>adler32> method to retrieve this value.
1716
1717This option defaults to false.
1718
1719=item B<-ConsumeInput>
1720
1721If set to true, this option will remove compressed data from the input
1722buffer of the the C< $i-E<gt>inflate > method as the inflate progresses.
1723
1724This option can be useful when you are processing compressed data that is
1725embedded in another file/buffer. In this case the data that immediately
1726follows the compressed stream will be left in the input buffer.
1727
1728This option defaults to true.
1729
f4c6fd49 1730=back
1731
642e522c 1732Here is an example of using an optional parameter to override the default
1733buffer size.
f4c6fd49 1734
642e522c 1735 my ($i, $status) = new Compress::Zlib::Inflate( -Bufsize => 300 ) ;
f4c6fd49 1736
642e522c 1737=head2 B< $status = $i-E<gt>inflate($input, $output) >
f4c6fd49 1738
642e522c 1739Inflates the complete contents of C<$input> and writes the uncompressed
1740data to C<$output>. The C<$input> and C<$output> parameters can either be
1741scalars or scalar references.
f4c6fd49 1742
1743Returns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the
1744compressed data has been successfully reached.
f4c6fd49 1745
642e522c 1746If not successful C<$status> will hold the I<zlib> error code.
1747
1748If the C<ConsumeInput> option has been set to true when the
1749C<Compress::Zlib::Inflate> object is created, the C<$input> parameter
1750is modified by C<inflate>. On completion it will contain what remains
1751of the input buffer after inflation. In practice, this means that when
1752the return status is C<Z_OK> the C<$input> parameter will contain an
1753empty string, and when the return status is C<Z_STREAM_END> the C<$input>
1754parameter will contains what (if anything) was stored in the input buffer
1755after the deflated data stream.
f4c6fd49 1756
1757This feature is useful when processing a file format that encapsulates
642e522c 1758a compressed data stream (e.g. gzip, zip) and there is useful data
1759immediately after the deflation stream.
f4c6fd49 1760
642e522c 1761If the C<AppendOutput> option is set to true in the constructor for
1762this object, the uncompressed data will be appended to C<$output>. If
1763it is false, C<$output> will be truncated before any uncompressed data
1764is written to it.
f4c6fd49 1765
642e522c 1766=head2 B<$status = $i-E<gt>inflateSync($input)>
1767
1768This method can be used to attempt to recover good data from a compressed
1769data stream that is partially corrupt.
1770It scans C<$input> until it reaches either a I<full flush point> or the
f4c6fd49 1771end of the buffer.
1772
642e522c 1773If a I<full flush point> is found, C<Z_OK> is returned and C<$input>
1774will be have all data up to the flush point removed. This data can then be
1775passed to the C<$i-E<gt>inflate> method to be uncompressed.
f4c6fd49 1776
1777Any other return code means that a flush point was not found. If more
1778data is available, C<inflateSync> can be called repeatedly with more
1779compressed data until the flush point is found.
1780
642e522c 1781Note I<full flush points> are not present by default in compressed
1782data streams. They must have been added explicitly when the data stream
1783was created by calling C<Compress::Deflate::flush> with C<Z_FULL_FLUSH>.
1784
f4c6fd49 1785
1786=head2 B<$i-E<gt>dict_adler()>
1787
1788Returns the adler32 value for the dictionary.
1789
642e522c 1790=head2 B<$i-E<gt>crc32()>
1791
1792Returns the crc32 value for the uncompressed data to date.
1793
1794If the C<CRC32> option is not enabled in the constructor for this object,
1795this method will always return 0;
1796
1797=head2 B<$i-E<gt>adler32()>
1798
1799Returns the adler32 value for the uncompressed data to date.
1800
1801If the C<ADLER32> option is not enabled in the constructor for this object,
1802this method will always return 0;
1803
f4c6fd49 1804=head2 B<$i-E<gt>msg()>
1805
1806Returns the last error message generated by zlib.
1807
1808=head2 B<$i-E<gt>total_in()>
1809
1810Returns the total number of bytes compressed bytes input to inflate.
1811
1812=head2 B<$i-E<gt>total_out()>
1813
1814Returns the total number of uncompressed bytes output from inflate.
1815
642e522c 1816=head2 B<$d-E<gt>get_BufSize()>
1817
1818Returns the buffer size used to carry out the decompression.
1819
f4c6fd49 1820=head2 Example
1821
642e522c 1822Here is an example of using C<inflate>.
f4c6fd49 1823
1824 use strict ;
1825 use warnings ;
1826
642e522c 1827 use Compress::Zlib 2 ;
f4c6fd49 1828
642e522c 1829 my $x = new Compress::Zlib::Inflate()
f4c6fd49 1830 or die "Cannot create a inflation stream\n" ;
1831
1832 my $input = '' ;
1833 binmode STDIN;
1834 binmode STDOUT;
1835
1836 my ($output, $status) ;
1837 while (read(STDIN, $input, 4096))
1838 {
642e522c 1839 $status = $x->inflate(\$input, $output) ;
f4c6fd49 1840
1841 print $output
1842 if $status == Z_OK or $status == Z_STREAM_END ;
1843
1844 last if $status != Z_OK ;
1845 }
1846
1847 die "inflation failed\n"
1848 unless $status == Z_STREAM_END ;
1849
642e522c 1850=head1 Compress::Zlib 1.x Deflate Interface
1851
1852This section defines the interface available in C<Compress::Zlib> version
18531.x that allows in-memory compression using the I<deflate> interface
1854provided by zlib.
1855
1856Here is a definition of the interface available:
1857
1858
1859=head2 B<($d, $status) = deflateInit( [OPT] )>
f4c6fd49 1860
642e522c 1861Initialises a deflation stream.
1862
1863It combines the features of the I<zlib> functions C<deflateInit>,
1864C<deflateInit2> and C<deflateSetDictionary>.
1865
1866If successful, it will return the initialised deflation stream, C<$d>
1867and C<$status> of C<Z_OK> in a list context. In scalar context it
1868returns the deflation stream, C<$d>, only.
1869
1870If not successful, the returned deflation stream (C<$d>) will be
1871I<undef> and C<$status> will hold the exact I<zlib> error code.
bdd93743 1872
642e522c 1873The function optionally takes a number of named options specified as
1874C<-Name=E<gt>value> pairs. This allows individual options to be
1875tailored without having to specify them all in the parameter list.
1876
1877For backward compatibility, it is also possible to pass the parameters
1878as a reference to a hash containing the name=>value pairs.
1879
1880The function takes one optional parameter, a reference to a hash. The
1881contents of the hash allow the deflation interface to be tailored.
1882
1883Here is a list of the valid options:
f4c6fd49 1884
1885=over 5
1886
642e522c 1887=item B<-Level>
f4c6fd49 1888
642e522c 1889Defines the compression level. Valid values are 0 through 9,
1890C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
1891C<Z_DEFAULT_COMPRESSION>.
f4c6fd49 1892
642e522c 1893The default is C<-Level =E<gt>Z_DEFAULT_COMPRESSION>.
f4c6fd49 1894
642e522c 1895=item B<-Method>
f4c6fd49 1896
642e522c 1897Defines the compression method. The only valid value at present (and
1898the default) is C<-Method =E<gt>Z_DEFLATED>.
f4c6fd49 1899
642e522c 1900=item B<-WindowBits>
f4c6fd49 1901
642e522c 1902For a definition of the meaning and valid values for C<WindowBits>
1903refer to the I<zlib> documentation for I<deflateInit2>.
f4c6fd49 1904
642e522c 1905Defaults to C<-WindowBits =E<gt>MAX_WBITS>.
f4c6fd49 1906
642e522c 1907=item B<-MemLevel>
f4c6fd49 1908
642e522c 1909For a definition of the meaning and valid values for C<MemLevel>
1910refer to the I<zlib> documentation for I<deflateInit2>.
bdd93743 1911
642e522c 1912Defaults to C<-MemLevel =E<gt>MAX_MEM_LEVEL>.
f4c6fd49 1913
642e522c 1914=item B<-Strategy>
f4c6fd49 1915
642e522c 1916Defines the strategy used to tune the compression. The valid values are
1917C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
f4c6fd49 1918
642e522c 1919The default is C<-Strategy =E<gt>Z_DEFAULT_STRATEGY>.
f4c6fd49 1920
642e522c 1921=item B<-Dictionary>
1922
1923When a dictionary is specified I<Compress::Zlib> will automatically
1924call C<deflateSetDictionary> directly after calling C<deflateInit>. The
1925Adler32 value for the dictionary can be obtained by calling the method
1926C<$d->dict_adler()>.
f4c6fd49 1927
642e522c 1928The default is no dictionary.
f4c6fd49 1929
642e522c 1930=item B<-Bufsize>
f4c6fd49 1931
642e522c 1932Sets the initial size for the deflation buffer. If the buffer has to be
1933reallocated to increase the size, it will grow in increments of
1934C<Bufsize>.
f4c6fd49 1935
642e522c 1936The default is 4096.
f4c6fd49 1937
642e522c 1938=back
f4c6fd49 1939
642e522c 1940Here is an example of using the C<deflateInit> optional parameter list
1941to override the default buffer size and compression level. All other
1942options will take their default values.
f4c6fd49 1943
642e522c 1944 deflateInit( -Bufsize => 300,
1945 -Level => Z_BEST_SPEED ) ;
f4c6fd49 1946
f4c6fd49 1947
642e522c 1948=head2 B<($out, $status) = $d-E<gt>deflate($buffer)>
f4c6fd49 1949
f4c6fd49 1950
642e522c 1951Deflates the contents of C<$buffer>. The buffer can either be a scalar
1952or a scalar reference. When finished, C<$buffer> will be
1953completely processed (assuming there were no errors). If the deflation
1954was successful it returns the deflated output, C<$out>, and a status
1955value, C<$status>, of C<Z_OK>.
f4c6fd49 1956
642e522c 1957On error, C<$out> will be I<undef> and C<$status> will contain the
1958I<zlib> error code.
f4c6fd49 1959
642e522c 1960In a scalar context C<deflate> will return C<$out> only.
f4c6fd49 1961
642e522c 1962As with the I<deflate> function in I<zlib>, it is not necessarily the
1963case that any output will be produced by this method. So don't rely on
1964the fact that C<$out> is empty for an error test.
f4c6fd49 1965
f4c6fd49 1966
642e522c 1967=head2 B<($out, $status) = $d-E<gt>flush([flush_type])>
f4c6fd49 1968
642e522c 1969Typically used to finish the deflation. Any pending output will be
1970returned via C<$out>.
1971C<$status> will have a value C<Z_OK> if successful.
f4c6fd49 1972
642e522c 1973In a scalar context C<flush> will return C<$out> only.
f4c6fd49 1974
642e522c 1975Note that flushing can seriously degrade the compression ratio, so it
1976should only be used to terminate a decompression (using C<Z_FINISH>) or
1977when you want to create a I<full flush point> (using C<Z_FULL_FLUSH>).
f4c6fd49 1978
642e522c 1979By default the C<flush_type> used is C<Z_FINISH>. Other valid values
1980for C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH>
1981and C<Z_FULL_FLUSH>. It is strongly recommended that you only set the
1982C<flush_type> parameter if you fully understand the implications of
1983what it does. See the C<zlib> documentation for details.
f4c6fd49 1984
642e522c 1985=head2 B<$status = $d-E<gt>deflateParams([OPT])>
f4c6fd49 1986
642e522c 1987Change settings for the deflate stream C<$d>.
f4c6fd49 1988
1989The list of the valid options is shown below. Options not specified
1990will remain unchanged.
1991
f4c6fd49 1992=over 5
1993
642e522c 1994=item B<-Level>
f4c6fd49 1995
1996Defines the compression level. Valid values are 0 through 9,
1997C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and
1998C<Z_DEFAULT_COMPRESSION>.
1999
642e522c 2000=item B<-Strategy>
f4c6fd49 2001
2002Defines the strategy used to tune the compression. The valid values are
2003C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>.
2004
2005=back
2006
642e522c 2007=head2 B<$d-E<gt>dict_adler()>
f4c6fd49 2008
642e522c 2009Returns the adler32 value for the dictionary.
f4c6fd49 2010
642e522c 2011=head2 B<$d-E<gt>msg()>
f4c6fd49 2012
642e522c 2013Returns the last error message generated by zlib.
f4c6fd49 2014
642e522c 2015=head2 B<$d-E<gt>total_in()>
f4c6fd49 2016
642e522c 2017Returns the total number of bytes uncompressed bytes input to deflate.
f4c6fd49 2018
642e522c 2019=head2 B<$d-E<gt>total_out()>
f4c6fd49 2020
642e522c 2021Returns the total number of compressed bytes output from deflate.
f4c6fd49 2022
642e522c 2023=head2 Example
f4c6fd49 2024
f4c6fd49 2025
642e522c 2026Here is a trivial example of using C<deflate>. It simply reads standard
2027input, deflates it and writes it to standard output.
f4c6fd49 2028
2029 use strict ;
2030 use warnings ;
642e522c 2031
f4c6fd49 2032 use Compress::Zlib ;
f4c6fd49 2033
642e522c 2034 binmode STDIN;
2035 binmode STDOUT;
2036 my $x = deflateInit()
2037 or die "Cannot create a deflation stream\n" ;
f4c6fd49 2038
642e522c 2039 my ($output, $status) ;
2040 while (<>)
2041 {
2042 ($output, $status) = $x->deflate($_) ;
f4c6fd49 2043
642e522c 2044 $status == Z_OK
2045 or die "deflation failed\n" ;
f4c6fd49 2046
642e522c 2047 print $output ;
f4c6fd49 2048 }
f4c6fd49 2049
642e522c 2050 ($output, $status) = $x->flush() ;
f4c6fd49 2051
642e522c 2052 $status == Z_OK
2053 or die "deflation failed\n" ;
f4c6fd49 2054
642e522c 2055 print $output ;
f4c6fd49 2056
642e522c 2057=head1 Compress::Zlib 1.x Inflate Interface
f4c6fd49 2058
642e522c 2059This section defines the interface available in C<Compress::Zlib> version
20601.x that allows in-memory uncompression using the I<deflate> interface
2061provided by zlib.
f4c6fd49 2062
642e522c 2063Here is a definition of the interface:
f4c6fd49 2064
f4c6fd49 2065
642e522c 2066=head2 B<($i, $status) = inflateInit()>
f4c6fd49 2067
642e522c 2068Initialises an inflation stream.
f4c6fd49 2069
642e522c 2070In a list context it returns the inflation stream, C<$i>, and the
2071I<zlib> status code (C<$status>). In a scalar context it returns the
2072inflation stream only.
f4c6fd49 2073
642e522c 2074If successful, C<$i> will hold the inflation stream and C<$status> will
2075be C<Z_OK>.
f4c6fd49 2076
642e522c 2077If not successful, C<$i> will be I<undef> and C<$status> will hold the
2078I<zlib> error code.
f4c6fd49 2079
642e522c 2080The function optionally takes a number of named options specified as
2081C<-Name=E<gt>value> pairs. This allows individual options to be
2082tailored without having to specify them all in the parameter list.
2083
2084For backward compatibility, it is also possible to pass the parameters
2085as a reference to a hash containing the name=>value pairs.
2086
2087The function takes one optional parameter, a reference to a hash. The
2088contents of the hash allow the deflation interface to be tailored.
2089
2090Here is a list of the valid options:
f4c6fd49 2091
642e522c 2092=over 5
f4c6fd49 2093
642e522c 2094=item B<-WindowBits>
f4c6fd49 2095
642e522c 2096For a definition of the meaning and valid values for C<WindowBits>
2097refer to the I<zlib> documentation for I<inflateInit2>.
f4c6fd49 2098
642e522c 2099Defaults to C<-WindowBits =E<gt>MAX_WBITS>.
f4c6fd49 2100
642e522c 2101=item B<-Bufsize>
f4c6fd49 2102
642e522c 2103Sets the initial size for the inflation buffer. If the buffer has to be
2104reallocated to increase the size, it will grow in increments of
2105C<Bufsize>.
f4c6fd49 2106
642e522c 2107Default is 4096.
5993747c 2108
642e522c 2109=item B<-Dictionary>
5993747c 2110
642e522c 2111The default is no dictionary.
5993747c 2112
642e522c 2113=back
5993747c 2114
642e522c 2115Here is an example of using the C<inflateInit> optional parameter to
2116override the default buffer size.
5993747c 2117
642e522c 2118 inflateInit( -Bufsize => 300 ) ;
5993747c 2119
642e522c 2120=head2 B<($out, $status) = $i-E<gt>inflate($buffer)>
5993747c 2121
642e522c 2122Inflates the complete contents of C<$buffer>. The buffer can either be
2123a scalar or a scalar reference.
5993747c 2124
642e522c 2125Returns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the
2126compressed data has been successfully reached.
2127If not successful, C<$out> will be I<undef> and C<$status> will hold
2128the I<zlib> error code.
5993747c 2129
642e522c 2130The C<$buffer> parameter is modified by C<inflate>. On completion it
2131will contain what remains of the input buffer after inflation. This
2132means that C<$buffer> will be an empty string when the return status is
2133C<Z_OK>. When the return status is C<Z_STREAM_END> the C<$buffer>
2134parameter will contains what (if anything) was stored in the input
2135buffer after the deflated data stream.
5993747c 2136
642e522c 2137This feature is useful when processing a file format that encapsulates
2138a compressed data stream (e.g. gzip, zip).
5993747c 2139
642e522c 2140=head2 B<$status = $i-E<gt>inflateSync($buffer)>
5993747c 2141
642e522c 2142Scans C<$buffer> until it reaches either a I<full flush point> or the
2143end of the buffer.
5993747c 2144
642e522c 2145If a I<full flush point> is found, C<Z_OK> is returned and C<$buffer>
2146will be have all data up to the flush point removed. This can then be
2147passed to the C<deflate> method.
5993747c 2148
642e522c 2149Any other return code means that a flush point was not found. If more
2150data is available, C<inflateSync> can be called repeatedly with more
2151compressed data until the flush point is found.
5993747c 2152
5993747c 2153
642e522c 2154=head2 B<$i-E<gt>dict_adler()>
5993747c 2155
642e522c 2156Returns the adler32 value for the dictionary.
5993747c 2157
642e522c 2158=head2 B<$i-E<gt>msg()>
5993747c 2159
642e522c 2160Returns the last error message generated by zlib.
5993747c 2161
642e522c 2162=head2 B<$i-E<gt>total_in()>
5993747c 2163
642e522c 2164Returns the total number of bytes compressed bytes input to inflate.
f4c6fd49 2165
642e522c 2166=head2 B<$i-E<gt>total_out()>
f4c6fd49 2167
642e522c 2168Returns the total number of uncompressed bytes output from inflate.
f4c6fd49 2169
642e522c 2170=head2 Example
f4c6fd49 2171
642e522c 2172Here is an example of using C<inflate>.
f4c6fd49 2173
642e522c 2174 use strict ;
2175 use warnings ;
2176
2177 use Compress::Zlib ;
2178
2179 my $x = inflateInit()
2180 or die "Cannot create a inflation stream\n" ;
2181
2182 my $input = '' ;
2183 binmode STDIN;
2184 binmode STDOUT;
2185
2186 my ($output, $status) ;
2187 while (read(STDIN, $input, 4096))
2188 {
2189 ($output, $status) = $x->inflate(\$input) ;
2190
2191 print $output
2192 if $status == Z_OK or $status == Z_STREAM_END ;
2193
2194 last if $status != Z_OK ;
2195 }
2196
2197 die "inflation failed\n"
2198 unless $status == Z_STREAM_END ;
f4c6fd49 2199
642e522c 2200=head1 ACCESSING ZIP FILES
f4c6fd49 2201
642e522c 2202Although it is possible (with some effort on your part) to use this
2203module to access .zip files, there is a module on CPAN that will do all
2204the hard work for you. Check out the C<Archive::Zip> module on CPAN at
f4c6fd49 2205
642e522c 2206 http://www.cpan.org/modules/by-module/Archive/Archive-Zip-*.tar.gz
f4c6fd49 2207
f4c6fd49 2208
2209=head1 CONSTANTS
2210
2211All the I<zlib> constants are automatically imported when you make use
2212of I<Compress::Zlib>.
2213
642e522c 2214
2215=head1 SEE ALSO
2216
2217L<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::Uncompress::AnyInflate>
2218
2219L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
2220
2221L<File::GlobMapper|File::GlobMapper>, L<Archive::Tar|Archive::Zip>,
2222L<IO::Zlib|IO::Zlib>
2223
2224For RFC 1950, 1951 and 1952 see
2225F<http://www.faqs.org/rfcs/rfc1950.html>,
2226F<http://www.faqs.org/rfcs/rfc1951.html> and
2227F<http://www.faqs.org/rfcs/rfc1952.html>
2228
2229The primary site for the gzip program is F<http://www.gzip.org>.
2230
f4c6fd49 2231=head1 AUTHOR
2232
2233The I<Compress::Zlib> module was written by Paul Marquess,
2234F<pmqs@cpan.org>. The latest copy of the module can be
2235found on CPAN in F<modules/by-module/Compress/Compress-Zlib-x.x.tar.gz>.
2236
642e522c 2237The I<zlib> compression library was written by Jean-loup Gailly
2238F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
2239
f4c6fd49 2240The primary site for the I<zlib> compression library is
2241F<http://www.zlib.org>.
2242
2243=head1 MODIFICATION HISTORY
2244
2245See the Changes file.
642e522c 2246
2247=head1 COPYRIGHT AND LICENSE
2248
2249
2250Copyright (c) 1995-2005 Paul Marquess. All rights reserved.
2251This program is free software; you can redistribute it and/or
2252modify it under the same terms as Perl itself.
2253
2254
2255
2256
2257