Commit | Line | Data |
---|---|---|
f4c6fd49 RGS |
1 | # File : Zlib.pm |
2 | # Author : Paul Marquess | |
3 | # Created : 30 January 2005 | |
4 | # Version : 1.34 | |
5 | # | |
6 | # Copyright (c) 1995-2005 Paul Marquess. All rights reserved. | |
7 | # This program is free software; you can redistribute it and/or | |
8 | # modify it under the same terms as Perl itself. | |
9 | # | |
10 | ||
11 | package Compress::Zlib; | |
12 | ||
13 | require 5.004 ; | |
14 | require Exporter; | |
15 | require DynaLoader; | |
16 | use AutoLoader; | |
17 | use Carp ; | |
18 | use IO::Handle ; | |
19 | ||
20 | use strict ; | |
21 | local ($^W) = 1; #use warnings ; | |
22 | use vars qw($VERSION @ISA @EXPORT $AUTOLOAD); | |
23 | use vars qw($deflateDefault $deflateParamsDefault $inflateDefault); | |
24 | ||
213b85e9 | 25 | $VERSION = "1.34_01" ; |
f4c6fd49 RGS |
26 | |
27 | @ISA = qw(Exporter DynaLoader); | |
28 | # Items to export into callers namespace by default. Note: do not export | |
29 | # names by default without a very good reason. Use EXPORT_OK instead. | |
30 | # Do not simply export all your public functions/methods/constants. | |
31 | @EXPORT = qw( | |
32 | deflateInit | |
33 | inflateInit | |
34 | ||
35 | compress | |
36 | uncompress | |
37 | ||
38 | gzip gunzip | |
39 | ||
40 | gzopen | |
41 | $gzerrno | |
42 | ||
43 | adler32 | |
44 | crc32 | |
45 | ||
46 | ZLIB_VERSION | |
47 | ||
48 | DEF_WBITS | |
49 | OS_CODE | |
50 | ||
51 | MAX_MEM_LEVEL | |
52 | MAX_WBITS | |
53 | ||
54 | Z_ASCII | |
55 | Z_BEST_COMPRESSION | |
56 | Z_BEST_SPEED | |
57 | Z_BINARY | |
58 | Z_BUF_ERROR | |
59 | Z_DATA_ERROR | |
60 | Z_DEFAULT_COMPRESSION | |
61 | Z_DEFAULT_STRATEGY | |
62 | Z_DEFLATED | |
63 | Z_ERRNO | |
64 | Z_FILTERED | |
65 | Z_FINISH | |
66 | Z_FULL_FLUSH | |
67 | Z_HUFFMAN_ONLY | |
68 | Z_MEM_ERROR | |
69 | Z_NEED_DICT | |
70 | Z_NO_COMPRESSION | |
71 | Z_NO_FLUSH | |
72 | Z_NULL | |
73 | Z_OK | |
74 | Z_PARTIAL_FLUSH | |
75 | Z_STREAM_END | |
76 | Z_STREAM_ERROR | |
77 | Z_SYNC_FLUSH | |
78 | Z_UNKNOWN | |
79 | Z_VERSION_ERROR | |
80 | ); | |
81 | ||
82 | ||
83 | ||
84 | sub AUTOLOAD { | |
85 | my($constname); | |
86 | ($constname = $AUTOLOAD) =~ s/.*:://; | |
87 | my ($error, $val) = constant($constname); | |
88 | Carp::croak $error if $error; | |
89 | no strict 'refs'; | |
90 | *{$AUTOLOAD} = sub { $val }; | |
91 | goto &{$AUTOLOAD}; | |
92 | } | |
93 | ||
94 | bootstrap Compress::Zlib $VERSION ; | |
95 | ||
96 | # Preloaded methods go here. | |
97 | ||
98 | sub isaFilehandle($) | |
99 | { | |
100 | my $fh = shift ; | |
101 | ||
102 | return ((UNIVERSAL::isa($fh,'GLOB') or UNIVERSAL::isa(\$fh,'GLOB')) | |
103 | and defined fileno($fh) ) | |
104 | ||
105 | } | |
106 | ||
107 | sub isaFilename($) | |
108 | { | |
109 | my $name = shift ; | |
110 | ||
111 | return (! ref $name and UNIVERSAL::isa(\$name, 'SCALAR')) ; | |
112 | } | |
113 | ||
114 | sub gzopen($$) | |
115 | { | |
116 | my ($file, $mode) = @_ ; | |
117 | ||
118 | if (isaFilehandle $file) { | |
119 | IO::Handle::flush($file) ; | |
120 | my $offset = tell($file) ; | |
121 | gzdopen_(fileno($file), $mode, $offset) ; | |
122 | } | |
123 | elsif (isaFilename $file) { | |
124 | gzopen_($file, $mode) | |
125 | } | |
126 | else { | |
127 | croak "gzopen: file parameter is not a filehandle or filename" | |
128 | } | |
129 | } | |
130 | ||
131 | sub ParseParameters($@) | |
132 | { | |
133 | my ($default, @rest) = @_ ; | |
134 | my (%got) = %$default ; | |
135 | my (@Bad) ; | |
136 | my ($key, $value) ; | |
137 | my $sub = (caller(1))[3] ; | |
138 | my %options = () ; | |
139 | ||
140 | # allow the options to be passed as a hash reference or | |
141 | # as the complete hash. | |
142 | if (@rest == 1) { | |
143 | ||
144 | croak "$sub: parameter is not a reference to a hash" | |
145 | if ref $rest[0] ne "HASH" ; | |
146 | ||
147 | %options = %{ $rest[0] } ; | |
148 | } | |
149 | elsif (@rest >= 2) { | |
150 | my $count = @rest; | |
151 | croak "$sub: Expected even number of parameters, got $count" | |
152 | if @rest % 2 != 0 ; | |
153 | %options = @rest ; | |
154 | } | |
155 | ||
156 | while (($key, $value) = each %options) | |
157 | { | |
158 | $key =~ s/^-// ; | |
159 | ||
160 | if (exists $default->{$key}) | |
161 | { $got{$key} = $value } | |
162 | else | |
163 | { push (@Bad, $key) } | |
164 | } | |
165 | ||
166 | if (@Bad) { | |
167 | my ($bad) = join(", ", @Bad) ; | |
168 | croak "unknown key value(s) @Bad" ; | |
169 | } | |
170 | ||
171 | return \%got ; | |
172 | } | |
173 | ||
174 | $deflateDefault = { | |
175 | 'Level' => Z_DEFAULT_COMPRESSION(), | |
176 | 'Method' => Z_DEFLATED(), | |
177 | 'WindowBits' => MAX_WBITS(), | |
178 | 'MemLevel' => MAX_MEM_LEVEL(), | |
179 | 'Strategy' => Z_DEFAULT_STRATEGY(), | |
180 | 'Bufsize' => 4096, | |
181 | 'Dictionary' => "", | |
182 | } ; | |
183 | ||
184 | $deflateParamsDefault = { | |
185 | 'Level' => undef, | |
186 | 'Strategy' => undef, | |
187 | 'Bufsize' => undef, | |
188 | } ; | |
189 | ||
190 | $inflateDefault = { | |
191 | 'WindowBits' => MAX_WBITS(), | |
192 | 'Bufsize' => 4096, | |
193 | 'Dictionary' => "", | |
194 | } ; | |
195 | ||
196 | ||
197 | sub deflateInit(@) | |
198 | { | |
199 | my ($got) = ParseParameters($deflateDefault, @_) ; | |
200 | local ($^W) = 0; #no warnings; | |
201 | croak "deflateInit: Bufsize must be >= 1, you specified $got->{Bufsize}" | |
202 | unless $got->{Bufsize} >= 1; | |
203 | _deflateInit($got->{Level}, $got->{Method}, $got->{WindowBits}, | |
204 | $got->{MemLevel}, $got->{Strategy}, $got->{Bufsize}, | |
205 | $got->{Dictionary}) ; | |
206 | ||
207 | } | |
208 | ||
209 | sub inflateInit(@) | |
210 | { | |
211 | my ($got) = ParseParameters($inflateDefault, @_) ; | |
212 | local ($^W) = 0; #no warnings; | |
213 | croak "inflateInit: Bufsize must be >= 1, you specified $got->{Bufsize}" | |
214 | unless $got->{Bufsize} >= 1; | |
215 | _inflateInit($got->{WindowBits}, $got->{Bufsize}, $got->{Dictionary}); | |
216 | ||
217 | } | |
218 | ||
219 | sub Compress::Zlib::deflateStream::deflateParams | |
220 | { | |
221 | my $self = shift ; | |
222 | my ($got) = ParseParameters($deflateParamsDefault, @_) ; | |
223 | croak "deflateParams needs Level and/or Strategy" | |
224 | unless defined $got->{Level} || defined $got->{Strategy}; | |
225 | local ($^W) = 0; #no warnings; | |
226 | croak "deflateParams: Bufsize must be >= 1, you specified $got->{Bufsize}" | |
227 | unless !defined $got->{Bufsize} || $got->{Bufsize} >= 1; | |
228 | ||
229 | my $flags = 0; | |
230 | if (defined $got->{Level}) | |
231 | { $flags |= 1 } | |
232 | else | |
233 | { $got->{Level} = 0 } | |
234 | ||
235 | if (defined $got->{Strategy}) | |
236 | { $flags |= 2 } | |
237 | else | |
238 | { $got->{Strategy} = 0 } | |
239 | ||
240 | $got->{Bufsize} = 0 | |
241 | if !defined $got->{Bufsize}; | |
242 | ||
243 | $self->_deflateParams($flags, $got->{Level}, $got->{Strategy}, | |
244 | $got->{Bufsize}); | |
245 | ||
246 | } | |
247 | ||
248 | sub compress($;$) | |
249 | { | |
250 | my ($x, $output, $out, $err, $in) ; | |
251 | ||
252 | if (ref $_[0] ) { | |
253 | $in = $_[0] ; | |
254 | croak "not a scalar reference" unless ref $in eq 'SCALAR' ; | |
255 | } | |
256 | else { | |
257 | $in = \$_[0] ; | |
258 | } | |
259 | ||
260 | my $level = (@_ == 2 ? $_[1] : Z_DEFAULT_COMPRESSION() ); | |
261 | ||
262 | ||
263 | if ( (($x, $err) = deflateInit(Level => $level))[1] == Z_OK()) { | |
264 | ||
265 | ($output, $err) = $x->deflate($in) ; | |
266 | return undef unless $err == Z_OK() ; | |
267 | ||
268 | ($out, $err) = $x->flush() ; | |
269 | return undef unless $err == Z_OK() ; | |
270 | ||
271 | return ($output . $out) ; | |
272 | ||
273 | } | |
274 | ||
275 | return undef ; | |
276 | } | |
277 | ||
278 | ||
279 | sub uncompress($) | |
280 | { | |
281 | my ($x, $output, $err, $in) ; | |
282 | ||
283 | if (ref $_[0] ) { | |
284 | $in = $_[0] ; | |
285 | croak "not a scalar reference" unless ref $in eq 'SCALAR' ; | |
286 | } | |
287 | else { | |
288 | $in = \$_[0] ; | |
289 | } | |
290 | ||
291 | if ( (($x, $err) = inflateInit())[1] == Z_OK()) { | |
292 | ||
293 | ($output, $err) = $x->__unc_inflate($in) ; | |
294 | return undef unless $err == Z_STREAM_END() ; | |
295 | ||
296 | return $output ; | |
297 | } | |
298 | ||
299 | return undef ; | |
300 | } | |
301 | ||
302 | ||
303 | # Constants | |
304 | use constant MAGIC1 => 0x1f ; | |
305 | use constant MAGIC2 => 0x8b ; | |
306 | use constant OSCODE => 3 ; | |
307 | ||
308 | use constant FTEXT => 1 ; | |
309 | use constant FHCRC => 2 ; | |
310 | use constant FEXTRA => 4 ; | |
311 | use constant FNAME => 8 ; | |
312 | use constant FCOMMENT => 16 ; | |
313 | use constant NULL => pack("C", 0) ; | |
314 | use constant RESERVED => 0xE0 ; | |
315 | ||
316 | use constant MIN_HDR_SIZE => 10 ; # minimum gzip header size | |
317 | ||
318 | sub memGzip($) | |
319 | { | |
320 | my $x = deflateInit( | |
321 | -Level => Z_BEST_COMPRESSION(), | |
322 | -WindowBits => - MAX_WBITS(), | |
323 | ) | |
324 | or return undef ; | |
325 | ||
326 | # write a minimal gzip header | |
327 | my(@m); | |
328 | push @m, pack("C" . MIN_HDR_SIZE, | |
329 | MAGIC1, MAGIC2, Z_DEFLATED(), 0,0,0,0,0,0, OSCODE) ; | |
330 | ||
331 | # if the deflation buffer isn't a reference, make it one | |
332 | my $string = (ref $_[0] ? $_[0] : \$_[0]) ; | |
333 | ||
334 | my ($output, $status) = $x->deflate($string) ; | |
335 | push @m, $output ; | |
336 | $status == Z_OK() | |
337 | or return undef ; | |
338 | ||
339 | ($output, $status) = $x->flush() ; | |
340 | push @m, $output ; | |
341 | $status == Z_OK() | |
342 | or return undef ; | |
343 | ||
344 | push @m, pack("V V", crc32($string), $x->total_in()); | |
345 | ||
346 | return join "", @m; | |
347 | } | |
348 | ||
349 | sub _removeGzipHeader($) | |
350 | { | |
351 | my $string = shift ; | |
352 | ||
353 | return Z_DATA_ERROR() | |
354 | if length($$string) < MIN_HDR_SIZE ; | |
355 | ||
356 | my ($magic1, $magic2, $method, $flags, $time, $xflags, $oscode) = | |
357 | unpack ('CCCCVCC', $$string); | |
358 | ||
359 | return Z_DATA_ERROR() | |
360 | unless $magic1 == MAGIC1 and $magic2 == MAGIC2 and | |
361 | $method == Z_DEFLATED() and !($flags & RESERVED()) ; | |
362 | substr($$string, 0, MIN_HDR_SIZE) = '' ; | |
363 | ||
364 | # skip extra field | |
365 | if ($flags & FEXTRA) | |
366 | { | |
367 | return Z_DATA_ERROR() | |
368 | if length($$string) < 2 ; | |
369 | ||
370 | my ($extra_len) = unpack ('v', $$string); | |
371 | $extra_len += 2; | |
372 | return Z_DATA_ERROR() | |
373 | if length($$string) < $extra_len ; | |
374 | ||
375 | substr($$string, 0, $extra_len) = ''; | |
376 | } | |
377 | ||
378 | # skip orig name | |
379 | if ($flags & FNAME) | |
380 | { | |
381 | my $name_end = index ($$string, NULL); | |
382 | return Z_DATA_ERROR() | |
383 | if $name_end == -1 ; | |
384 | substr($$string, 0, $name_end + 1) = ''; | |
385 | } | |
386 | ||
387 | # skip comment | |
388 | if ($flags & FCOMMENT) | |
389 | { | |
390 | my $comment_end = index ($$string, NULL); | |
391 | return Z_DATA_ERROR() | |
392 | if $comment_end == -1 ; | |
393 | substr($$string, 0, $comment_end + 1) = ''; | |
394 | } | |
395 | ||
396 | # skip header crc | |
397 | if ($flags & FHCRC) | |
398 | { | |
399 | return Z_DATA_ERROR() | |
400 | if length ($$string) < 2 ; | |
401 | substr($$string, 0, 2) = ''; | |
402 | } | |
403 | ||
404 | return Z_OK(); | |
405 | } | |
406 | ||
407 | ||
408 | sub memGunzip($) | |
409 | { | |
410 | # if the buffer isn't a reference, make it one | |
411 | my $string = (ref $_[0] ? $_[0] : \$_[0]); | |
412 | ||
413 | _removeGzipHeader($string) == Z_OK() | |
414 | or return undef; | |
415 | ||
416 | my $bufsize = length $$string > 4096 ? length $$string : 4096 ; | |
417 | my $x = inflateInit( -WindowBits => - MAX_WBITS(), | |
418 | -Bufsize => $bufsize) | |
419 | or return undef; | |
420 | my ($output, $status) = $x->inflate($string); | |
421 | return undef | |
422 | unless $status == Z_STREAM_END(); | |
423 | ||
424 | if (length $$string >= 8) | |
425 | { | |
426 | my ($crc, $len) = unpack ("VV", substr($$string, 0, 8)); | |
427 | substr($$string, 0, 8) = ''; | |
428 | return undef | |
429 | unless $len == length($output) and | |
430 | $crc == crc32($output); | |
431 | } | |
432 | else | |
433 | { | |
434 | $$string = ''; | |
435 | } | |
436 | ||
437 | return $output; | |
438 | } | |
439 | ||
440 | # Autoload methods go after __END__, and are processed by the autosplit program. | |
441 | ||
442 | 1; | |
443 | __END__ | |
444 | ||
445 | =cut | |
446 | ||
447 | =head1 NAME | |
448 | ||
449 | Compress::Zlib - Interface to zlib compression library | |
450 | ||
451 | =head1 SYNOPSIS | |
452 | ||
453 | use Compress::Zlib ; | |
454 | ||
455 | ($d, $status) = deflateInit( [OPT] ) ; | |
456 | ($out, $status) = $d->deflate($buffer) ; | |
457 | $status = $d->deflateParams([OPT]) ; | |
458 | ($out, $status) = $d->flush() ; | |
459 | $d->dict_adler() ; | |
460 | $d->total_in() ; | |
461 | $d->total_out() ; | |
462 | $d->msg() ; | |
463 | ||
464 | ($i, $status) = inflateInit( [OPT] ) ; | |
465 | ($out, $status) = $i->inflate($buffer) ; | |
466 | $status = $i->inflateSync($buffer) ; | |
467 | $i->dict_adler() ; | |
468 | $i->total_in() ; | |
469 | $i->total_out() ; | |
470 | $i->msg() ; | |
471 | ||
472 | $dest = compress($source, [$level]) ; | |
473 | $dest = uncompress($source) ; | |
474 | ||
475 | $gz = gzopen($filename or filehandle, $mode) ; | |
476 | $bytesread = $gz->gzread($buffer [,$size]) ; | |
477 | $bytesread = $gz->gzreadline($line) ; | |
478 | $byteswritten = $gz->gzwrite($buffer) ; | |
479 | $status = $gz->gzflush($flush) ; | |
480 | $status = $gz->gzclose() ; | |
481 | $status = $gz->gzeof() ; | |
482 | $status = $gz->gzsetparams($level, $strategy) ; | |
483 | $errstring = $gz->gzerror() ; | |
484 | $gzerrno | |
485 | ||
486 | $dest = Compress::Zlib::memGzip($buffer) ; | |
487 | $dest = Compress::Zlib::memGunzip($buffer) ; | |
488 | ||
489 | $crc = adler32($buffer [,$crc]) ; | |
490 | $crc = crc32($buffer [,$crc]) ; | |
491 | ||
492 | ZLIB_VERSION | |
493 | ||
494 | =head1 DESCRIPTION | |
495 | ||
496 | The I<Compress::Zlib> module provides a Perl interface to the I<zlib> | |
497 | compression library (see L</AUTHOR> for details about where to get | |
498 | I<zlib>). Most of the functionality provided by I<zlib> is available | |
499 | in I<Compress::Zlib>. | |
500 | ||
501 | The module can be split into two general areas of functionality, namely | |
502 | in-memory compression/decompression and read/write access to I<gzip> | |
503 | files. Each of these areas will be discussed separately below. | |
504 | ||
505 | =head1 DEFLATE | |
506 | ||
507 | The interface I<Compress::Zlib> provides to the in-memory I<deflate> | |
508 | (and I<inflate>) functions has been modified to fit into a Perl model. | |
509 | ||
510 | The main difference is that for both inflation and deflation, the Perl | |
511 | interface will I<always> consume the complete input buffer before | |
512 | returning. Also the output buffer returned will be automatically grown | |
513 | to fit the amount of output available. | |
514 | ||
515 | Here is a definition of the interface available: | |
516 | ||
517 | ||
518 | =head2 B<($d, $status) = deflateInit( [OPT] )> | |
519 | ||
520 | Initialises a deflation stream. | |
521 | ||
522 | It combines the features of the I<zlib> functions B<deflateInit>, | |
523 | B<deflateInit2> and B<deflateSetDictionary>. | |
524 | ||
525 | If successful, it will return the initialised deflation stream, B<$d> | |
526 | and B<$status> of C<Z_OK> in a list context. In scalar context it | |
527 | returns the deflation stream, B<$d>, only. | |
528 | ||
529 | If not successful, the returned deflation stream (B<$d>) will be | |
530 | I<undef> and B<$status> will hold the exact I<zlib> error code. | |
531 | ||
532 | The function optionally takes a number of named options specified as | |
533 | C<-Name=E<gt>value> pairs. This allows individual options to be | |
534 | tailored without having to specify them all in the parameter list. | |
535 | ||
536 | For backward compatibility, it is also possible to pass the parameters | |
537 | as a reference to a hash containing the name=>value pairs. | |
538 | ||
539 | The function takes one optional parameter, a reference to a hash. The | |
540 | contents of the hash allow the deflation interface to be tailored. | |
541 | ||
542 | Here is a list of the valid options: | |
543 | ||
544 | =over 5 | |
545 | ||
546 | =item B<-Level> | |
547 | ||
548 | Defines the compression level. Valid values are 0 through 9, | |
549 | C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and | |
550 | C<Z_DEFAULT_COMPRESSION>. | |
551 | ||
552 | The default is C<-Level =E<gt>Z_DEFAULT_COMPRESSION>. | |
553 | ||
554 | =item B<-Method> | |
555 | ||
556 | Defines the compression method. The only valid value at present (and | |
557 | the default) is C<-Method =E<gt>Z_DEFLATED>. | |
558 | ||
559 | =item B<-WindowBits> | |
560 | ||
561 | For a definition of the meaning and valid values for B<WindowBits> | |
562 | refer to the I<zlib> documentation for I<deflateInit2>. | |
563 | ||
564 | Defaults to C<-WindowBits =E<gt>MAX_WBITS>. | |
565 | ||
566 | =item B<-MemLevel> | |
567 | ||
568 | For a definition of the meaning and valid values for B<MemLevel> | |
569 | refer to the I<zlib> documentation for I<deflateInit2>. | |
570 | ||
571 | Defaults to C<-MemLevel =E<gt>MAX_MEM_LEVEL>. | |
572 | ||
573 | =item B<-Strategy> | |
574 | ||
575 | Defines the strategy used to tune the compression. The valid values are | |
576 | C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. | |
577 | ||
578 | The default is C<-Strategy =E<gt>Z_DEFAULT_STRATEGY>. | |
579 | ||
580 | =item B<-Dictionary> | |
581 | ||
582 | When a dictionary is specified I<Compress::Zlib> will automatically | |
583 | call B<deflateSetDictionary> directly after calling B<deflateInit>. The | |
584 | Adler32 value for the dictionary can be obtained by calling the method | |
585 | C<$d->dict_adler()>. | |
586 | ||
587 | The default is no dictionary. | |
588 | ||
589 | =item B<-Bufsize> | |
590 | ||
591 | Sets the initial size for the deflation buffer. If the buffer has to be | |
592 | reallocated to increase the size, it will grow in increments of | |
593 | B<Bufsize>. | |
594 | ||
595 | The default is 4096. | |
596 | ||
597 | =back | |
598 | ||
599 | Here is an example of using the B<deflateInit> optional parameter list | |
600 | to override the default buffer size and compression level. All other | |
601 | options will take their default values. | |
602 | ||
603 | deflateInit( -Bufsize => 300, | |
604 | -Level => Z_BEST_SPEED ) ; | |
605 | ||
606 | ||
607 | =head2 B<($out, $status) = $d-E<gt>deflate($buffer)> | |
608 | ||
609 | ||
610 | Deflates the contents of B<$buffer>. The buffer can either be a scalar | |
611 | or a scalar reference. When finished, B<$buffer> will be | |
612 | completely processed (assuming there were no errors). If the deflation | |
613 | was successful it returns the deflated output, B<$out>, and a status | |
614 | value, B<$status>, of C<Z_OK>. | |
615 | ||
616 | On error, B<$out> will be I<undef> and B<$status> will contain the | |
617 | I<zlib> error code. | |
618 | ||
619 | In a scalar context B<deflate> will return B<$out> only. | |
620 | ||
621 | As with the I<deflate> function in I<zlib>, it is not necessarily the | |
622 | case that any output will be produced by this method. So don't rely on | |
623 | the fact that B<$out> is empty for an error test. | |
624 | ||
625 | ||
626 | =head2 B<($out, $status) = $d-E<gt>flush([flush_type])> | |
627 | ||
628 | Typically used to finish the deflation. Any pending output will be | |
629 | returned via B<$out>. | |
630 | B<$status> will have a value C<Z_OK> if successful. | |
631 | ||
632 | In a scalar context B<flush> will return B<$out> only. | |
633 | ||
634 | Note that flushing can seriously degrade the compression ratio, so it | |
635 | should only be used to terminate a decompression (using C<Z_FINISH>) or | |
636 | when you want to create a I<full flush point> (using C<Z_FULL_FLUSH>). | |
637 | ||
638 | By default the C<flush_type> used is C<Z_FINISH>. Other valid values | |
639 | for C<flush_type> are C<Z_NO_FLUSH>, C<Z_PARTIAL_FLUSH>, C<Z_SYNC_FLUSH> | |
640 | and C<Z_FULL_FLUSH>. It is strongly recommended that you only set the | |
641 | C<flush_type> parameter if you fully understand the implications of | |
642 | what it does. See the C<zlib> documentation for details. | |
643 | ||
644 | =head2 B<$status = $d-E<gt>deflateParams([OPT])> | |
645 | ||
646 | Change settings for the deflate stream C<$d>. | |
647 | ||
648 | The list of the valid options is shown below. Options not specified | |
649 | will remain unchanged. | |
650 | ||
651 | =over 5 | |
652 | ||
653 | =item B<-Level> | |
654 | ||
655 | Defines the compression level. Valid values are 0 through 9, | |
656 | C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and | |
657 | C<Z_DEFAULT_COMPRESSION>. | |
658 | ||
659 | =item B<-Strategy> | |
660 | ||
661 | Defines the strategy used to tune the compression. The valid values are | |
662 | C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. | |
663 | ||
664 | =back | |
665 | ||
666 | =head2 B<$d-E<gt>dict_adler()> | |
667 | ||
668 | Returns the adler32 value for the dictionary. | |
669 | ||
670 | =head2 B<$d-E<gt>msg()> | |
671 | ||
672 | Returns the last error message generated by zlib. | |
673 | ||
674 | =head2 B<$d-E<gt>total_in()> | |
675 | ||
676 | Returns the total number of bytes uncompressed bytes input to deflate. | |
677 | ||
678 | =head2 B<$d-E<gt>total_out()> | |
679 | ||
680 | Returns the total number of compressed bytes output from deflate. | |
681 | ||
682 | =head2 Example | |
683 | ||
684 | ||
685 | Here is a trivial example of using B<deflate>. It simply reads standard | |
686 | input, deflates it and writes it to standard output. | |
687 | ||
688 | use strict ; | |
689 | use warnings ; | |
690 | ||
691 | use Compress::Zlib ; | |
692 | ||
693 | binmode STDIN; | |
694 | binmode STDOUT; | |
695 | my $x = deflateInit() | |
696 | or die "Cannot create a deflation stream\n" ; | |
697 | ||
698 | my ($output, $status) ; | |
699 | while (<>) | |
700 | { | |
701 | ($output, $status) = $x->deflate($_) ; | |
702 | ||
703 | $status == Z_OK | |
704 | or die "deflation failed\n" ; | |
705 | ||
706 | print $output ; | |
707 | } | |
708 | ||
709 | ($output, $status) = $x->flush() ; | |
710 | ||
711 | $status == Z_OK | |
712 | or die "deflation failed\n" ; | |
713 | ||
714 | print $output ; | |
715 | ||
716 | =head1 INFLATE | |
717 | ||
718 | Here is a definition of the interface: | |
719 | ||
720 | ||
721 | =head2 B<($i, $status) = inflateInit()> | |
722 | ||
723 | Initialises an inflation stream. | |
724 | ||
725 | In a list context it returns the inflation stream, B<$i>, and the | |
726 | I<zlib> status code (B<$status>). In a scalar context it returns the | |
727 | inflation stream only. | |
728 | ||
729 | If successful, B<$i> will hold the inflation stream and B<$status> will | |
730 | be C<Z_OK>. | |
731 | ||
732 | If not successful, B<$i> will be I<undef> and B<$status> will hold the | |
733 | I<zlib> error code. | |
734 | ||
735 | The function optionally takes a number of named options specified as | |
736 | C<-Name=E<gt>value> pairs. This allows individual options to be | |
737 | tailored without having to specify them all in the parameter list. | |
738 | ||
739 | For backward compatibility, it is also possible to pass the parameters | |
740 | as a reference to a hash containing the name=>value pairs. | |
741 | ||
742 | The function takes one optional parameter, a reference to a hash. The | |
743 | contents of the hash allow the deflation interface to be tailored. | |
744 | ||
745 | Here is a list of the valid options: | |
746 | ||
747 | =over 5 | |
748 | ||
749 | =item B<-WindowBits> | |
750 | ||
751 | For a definition of the meaning and valid values for B<WindowBits> | |
752 | refer to the I<zlib> documentation for I<inflateInit2>. | |
753 | ||
754 | Defaults to C<-WindowBits =E<gt>MAX_WBITS>. | |
755 | ||
756 | =item B<-Bufsize> | |
757 | ||
758 | Sets the initial size for the inflation buffer. If the buffer has to be | |
759 | reallocated to increase the size, it will grow in increments of | |
760 | B<Bufsize>. | |
761 | ||
762 | Default is 4096. | |
763 | ||
764 | =item B<-Dictionary> | |
765 | ||
766 | The default is no dictionary. | |
767 | ||
768 | =back | |
769 | ||
770 | Here is an example of using the B<inflateInit> optional parameter to | |
771 | override the default buffer size. | |
772 | ||
773 | inflateInit( -Bufsize => 300 ) ; | |
774 | ||
775 | =head2 B<($out, $status) = $i-E<gt>inflate($buffer)> | |
776 | ||
777 | Inflates the complete contents of B<$buffer>. The buffer can either be | |
778 | a scalar or a scalar reference. | |
779 | ||
780 | Returns C<Z_OK> if successful and C<Z_STREAM_END> if the end of the | |
781 | compressed data has been successfully reached. | |
782 | If not successful, B<$out> will be I<undef> and B<$status> will hold | |
783 | the I<zlib> error code. | |
784 | ||
785 | The C<$buffer> parameter is modified by C<inflate>. On completion it | |
786 | will contain what remains of the input buffer after inflation. This | |
787 | means that C<$buffer> will be an empty string when the return status is | |
788 | C<Z_OK>. When the return status is C<Z_STREAM_END> the C<$buffer> | |
789 | parameter will contains what (if anything) was stored in the input | |
790 | buffer after the deflated data stream. | |
791 | ||
792 | This feature is useful when processing a file format that encapsulates | |
793 | a compressed data stream (e.g. gzip, zip). | |
794 | ||
795 | =head2 B<$status = $i-E<gt>inflateSync($buffer)> | |
796 | ||
797 | Scans C<$buffer> until it reaches either a I<full flush point> or the | |
798 | end of the buffer. | |
799 | ||
800 | If a I<full flush point> is found, C<Z_OK> is returned and C<$buffer> | |
801 | will be have all data up to the flush point removed. This can then be | |
802 | passed to the C<deflate> method. | |
803 | ||
804 | Any other return code means that a flush point was not found. If more | |
805 | data is available, C<inflateSync> can be called repeatedly with more | |
806 | compressed data until the flush point is found. | |
807 | ||
808 | ||
809 | =head2 B<$i-E<gt>dict_adler()> | |
810 | ||
811 | Returns the adler32 value for the dictionary. | |
812 | ||
813 | =head2 B<$i-E<gt>msg()> | |
814 | ||
815 | Returns the last error message generated by zlib. | |
816 | ||
817 | =head2 B<$i-E<gt>total_in()> | |
818 | ||
819 | Returns the total number of bytes compressed bytes input to inflate. | |
820 | ||
821 | =head2 B<$i-E<gt>total_out()> | |
822 | ||
823 | Returns the total number of uncompressed bytes output from inflate. | |
824 | ||
825 | =head2 Example | |
826 | ||
827 | Here is an example of using B<inflate>. | |
828 | ||
829 | use strict ; | |
830 | use warnings ; | |
831 | ||
832 | use Compress::Zlib ; | |
833 | ||
834 | my $x = inflateInit() | |
835 | or die "Cannot create a inflation stream\n" ; | |
836 | ||
837 | my $input = '' ; | |
838 | binmode STDIN; | |
839 | binmode STDOUT; | |
840 | ||
841 | my ($output, $status) ; | |
842 | while (read(STDIN, $input, 4096)) | |
843 | { | |
844 | ($output, $status) = $x->inflate(\$input) ; | |
845 | ||
846 | print $output | |
847 | if $status == Z_OK or $status == Z_STREAM_END ; | |
848 | ||
849 | last if $status != Z_OK ; | |
850 | } | |
851 | ||
852 | die "inflation failed\n" | |
853 | unless $status == Z_STREAM_END ; | |
854 | ||
855 | =head1 COMPRESS/UNCOMPRESS | |
856 | ||
857 | Two high-level functions are provided by I<zlib> to perform in-memory | |
858 | compression. They are B<compress> and B<uncompress>. Two Perl subs are | |
859 | provided which provide similar functionality. | |
860 | ||
861 | =over 5 | |
862 | ||
863 | =item B<$dest = compress($source [, $level] ) ;> | |
864 | ||
865 | Compresses B<$source>. If successful it returns the | |
866 | compressed data. Otherwise it returns I<undef>. | |
867 | ||
868 | The source buffer can either be a scalar or a scalar reference. | |
869 | ||
870 | The B<$level> paramter defines the compression level. Valid values are | |
871 | 0 through 9, C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, | |
872 | C<Z_BEST_COMPRESSION>, and C<Z_DEFAULT_COMPRESSION>. | |
873 | If B<$level> is not specified C<Z_DEFAULT_COMPRESSION> will be used. | |
874 | ||
875 | ||
876 | =item B<$dest = uncompress($source) ;> | |
877 | ||
878 | Uncompresses B<$source>. If successful it returns the uncompressed | |
879 | data. Otherwise it returns I<undef>. | |
880 | ||
881 | The source buffer can either be a scalar or a scalar reference. | |
882 | ||
883 | =back | |
884 | ||
885 | =head1 GZIP INTERFACE | |
886 | ||
887 | A number of functions are supplied in I<zlib> for reading and writing | |
888 | I<gzip> files. This module provides an interface to most of them. In | |
889 | general the interface provided by this module operates identically to | |
890 | the functions provided by I<zlib>. Any differences are explained | |
891 | below. | |
892 | ||
893 | =over 5 | |
894 | ||
895 | =item B<$gz = gzopen(filename or filehandle, mode)> | |
896 | ||
897 | This function operates identically to the I<zlib> equivalent except | |
898 | that it returns an object which is used to access the other I<gzip> | |
899 | methods. | |
900 | ||
901 | As with the I<zlib> equivalent, the B<mode> parameter is used to | |
902 | specify both whether the file is opened for reading or writing and to | |
903 | optionally specify a a compression level. Refer to the I<zlib> | |
904 | documentation for the exact format of the B<mode> parameter. | |
905 | ||
906 | If a reference to an open filehandle is passed in place of the | |
907 | filename, gzdopen will be called behind the scenes. The third example | |
908 | at the end of this section, I<gzstream>, uses this feature. | |
909 | ||
910 | =item B<$bytesread = $gz-E<gt>gzread($buffer [, $size]) ;> | |
911 | ||
912 | Reads B<$size> bytes from the compressed file into B<$buffer>. If | |
913 | B<$size> is not specified, it will default to 4096. If the scalar | |
914 | B<$buffer> is not large enough, it will be extended automatically. | |
915 | ||
916 | Returns the number of bytes actually read. On EOF it returns 0 and in | |
917 | the case of an error, -1. | |
918 | ||
919 | =item B<$bytesread = $gz-E<gt>gzreadline($line) ;> | |
920 | ||
921 | Reads the next line from the compressed file into B<$line>. | |
922 | ||
923 | Returns the number of bytes actually read. On EOF it returns 0 and in | |
924 | the case of an error, -1. | |
925 | ||
926 | It is legal to intermix calls to B<gzread> and B<gzreadline>. | |
927 | ||
928 | At this time B<gzreadline> ignores the variable C<$/> | |
929 | (C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use). The | |
930 | end of a line is denoted by the C character C<'\n'>. | |
931 | ||
932 | =item B<$byteswritten = $gz-E<gt>gzwrite($buffer) ;> | |
933 | ||
934 | Writes the contents of B<$buffer> to the compressed file. Returns the | |
935 | number of bytes actually written, or 0 on error. | |
936 | ||
937 | =item B<$status = $gz-E<gt>gzflush($flush) ;> | |
938 | ||
939 | Flushes all pending output to the compressed file. | |
940 | Works identically to the I<zlib> function it interfaces to. Note that | |
941 | the use of B<gzflush> can degrade compression. | |
942 | ||
943 | Returns C<Z_OK> if B<$flush> is C<Z_FINISH> and all output could be | |
944 | flushed. Otherwise the zlib error code is returned. | |
945 | ||
946 | Refer to the I<zlib> documentation for the valid values of B<$flush>. | |
947 | ||
948 | =item B<$status = $gz-E<gt>gzeof() ;> | |
949 | ||
950 | Returns 1 if the end of file has been detected while reading the input | |
951 | file, otherwise returns 0. | |
952 | ||
953 | =item B<$gz-E<gt>gzclose> | |
954 | ||
955 | Closes the compressed file. Any pending data is flushed to the file | |
956 | before it is closed. | |
957 | ||
958 | =item B<$gz-E<gt>gzsetparams($level, $strategy> | |
959 | ||
960 | Change settings for the deflate stream C<$gz>. | |
961 | ||
962 | The list of the valid options is shown below. Options not specified | |
963 | will remain unchanged. | |
964 | ||
965 | Note: This method is only available if you are running zlib 1.0.6 or better. | |
966 | ||
967 | =over 5 | |
968 | ||
969 | =item B<$level> | |
970 | ||
971 | Defines the compression level. Valid values are 0 through 9, | |
972 | C<Z_NO_COMPRESSION>, C<Z_BEST_SPEED>, C<Z_BEST_COMPRESSION>, and | |
973 | C<Z_DEFAULT_COMPRESSION>. | |
974 | ||
975 | =item B<$strategy> | |
976 | ||
977 | Defines the strategy used to tune the compression. The valid values are | |
978 | C<Z_DEFAULT_STRATEGY>, C<Z_FILTERED> and C<Z_HUFFMAN_ONLY>. | |
979 | ||
980 | =back | |
981 | ||
982 | =item B<$gz-E<gt>gzerror> | |
983 | ||
984 | Returns the I<zlib> error message or number for the last operation | |
985 | associated with B<$gz>. The return value will be the I<zlib> error | |
986 | number when used in a numeric context and the I<zlib> error message | |
987 | when used in a string context. The I<zlib> error number constants, | |
988 | shown below, are available for use. | |
989 | ||
990 | Z_OK | |
991 | Z_STREAM_END | |
992 | Z_ERRNO | |
993 | Z_STREAM_ERROR | |
994 | Z_DATA_ERROR | |
995 | Z_MEM_ERROR | |
996 | Z_BUF_ERROR | |
997 | ||
998 | =item B<$gzerrno> | |
999 | ||
1000 | The B<$gzerrno> scalar holds the error code associated with the most | |
1001 | recent I<gzip> routine. Note that unlike B<gzerror()>, the error is | |
1002 | I<not> associated with a particular file. | |
1003 | ||
1004 | As with B<gzerror()> it returns an error number in numeric context and | |
1005 | an error message in string context. Unlike B<gzerror()> though, the | |
1006 | error message will correspond to the I<zlib> message when the error is | |
1007 | associated with I<zlib> itself, or the UNIX error message when it is | |
1008 | not (i.e. I<zlib> returned C<Z_ERRORNO>). | |
1009 | ||
1010 | As there is an overlap between the error numbers used by I<zlib> and | |
1011 | UNIX, B<$gzerrno> should only be used to check for the presence of | |
1012 | I<an> error in numeric context. Use B<gzerror()> to check for specific | |
1013 | I<zlib> errors. The I<gzcat> example below shows how the variable can | |
1014 | be used safely. | |
1015 | ||
1016 | =back | |
1017 | ||
1018 | ||
1019 | =head2 Examples | |
1020 | ||
1021 | Here is an example script which uses the interface. It implements a | |
1022 | I<gzcat> function. | |
1023 | ||
1024 | use strict ; | |
1025 | use warnings ; | |
1026 | ||
1027 | use Compress::Zlib ; | |
1028 | ||
1029 | die "Usage: gzcat file...\n" | |
1030 | unless @ARGV ; | |
1031 | ||
1032 | my $file ; | |
1033 | ||
1034 | foreach $file (@ARGV) { | |
1035 | my $buffer ; | |
1036 | ||
1037 | my $gz = gzopen($file, "rb") | |
1038 | or die "Cannot open $file: $gzerrno\n" ; | |
1039 | ||
1040 | print $buffer while $gz->gzread($buffer) > 0 ; | |
1041 | ||
1042 | die "Error reading from $file: $gzerrno" . ($gzerrno+0) . "\n" | |
1043 | if $gzerrno != Z_STREAM_END ; | |
1044 | ||
1045 | $gz->gzclose() ; | |
1046 | } | |
1047 | ||
1048 | Below is a script which makes use of B<gzreadline>. It implements a | |
1049 | very simple I<grep> like script. | |
1050 | ||
1051 | use strict ; | |
1052 | use warnings ; | |
1053 | ||
1054 | use Compress::Zlib ; | |
1055 | ||
1056 | die "Usage: gzgrep pattern file...\n" | |
1057 | unless @ARGV >= 2; | |
1058 | ||
1059 | my $pattern = shift ; | |
1060 | ||
1061 | my $file ; | |
1062 | ||
1063 | foreach $file (@ARGV) { | |
1064 | my $gz = gzopen($file, "rb") | |
1065 | or die "Cannot open $file: $gzerrno\n" ; | |
1066 | ||
1067 | while ($gz->gzreadline($_) > 0) { | |
1068 | print if /$pattern/ ; | |
1069 | } | |
1070 | ||
1071 | die "Error reading from $file: $gzerrno\n" | |
1072 | if $gzerrno != Z_STREAM_END ; | |
1073 | ||
1074 | $gz->gzclose() ; | |
1075 | } | |
1076 | ||
1077 | This script, I<gzstream>, does the opposite of the I<gzcat> script | |
1078 | above. It reads from standard input and writes a gzip file to standard | |
1079 | output. | |
1080 | ||
1081 | use strict ; | |
1082 | use warnings ; | |
1083 | ||
1084 | use Compress::Zlib ; | |
1085 | ||
1086 | binmode STDOUT; # gzopen only sets it on the fd | |
1087 | ||
1088 | my $gz = gzopen(\*STDOUT, "wb") | |
1089 | or die "Cannot open stdout: $gzerrno\n" ; | |
1090 | ||
1091 | while (<>) { | |
1092 | $gz->gzwrite($_) | |
1093 | or die "error writing: $gzerrno\n" ; | |
1094 | } | |
1095 | ||
1096 | $gz->gzclose ; | |
1097 | ||
1098 | =head2 Compress::Zlib::memGzip | |
1099 | ||
1100 | This function is used to create an in-memory gzip file. | |
1101 | It creates a minimal gzip header. | |
1102 | ||
1103 | $dest = Compress::Zlib::memGzip($buffer) ; | |
1104 | ||
1105 | If successful, it returns the in-memory gzip file, otherwise it returns | |
1106 | undef. | |
1107 | ||
1108 | The buffer parameter can either be a scalar or a scalar reference. | |
1109 | ||
1110 | =head2 Compress::Zlib::memGunzip | |
1111 | ||
1112 | This function is used to uncompress an in-memory gzip file. | |
1113 | ||
1114 | $dest = Compress::Zlib::memGunzip($buffer) ; | |
1115 | ||
1116 | If successful, it returns the uncompressed gzip file, otherwise it | |
1117 | returns undef. | |
1118 | ||
1119 | The buffer parameter can either be a scalar or a scalar reference. The | |
1120 | contents of the buffer parameter are destroyed after calling this | |
1121 | function. | |
1122 | ||
1123 | =head1 CHECKSUM FUNCTIONS | |
1124 | ||
1125 | Two functions are provided by I<zlib> to calculate a checksum. For the | |
1126 | Perl interface, the order of the two parameters in both functions has | |
1127 | been reversed. This allows both running checksums and one off | |
1128 | calculations to be done. | |
1129 | ||
1130 | $crc = adler32($buffer [,$crc]) ; | |
1131 | $crc = crc32($buffer [,$crc]) ; | |
1132 | ||
1133 | The buffer parameters can either be a scalar or a scalar reference. | |
1134 | ||
1135 | If the $crc parameters is C<undef>, the crc value will be reset. | |
1136 | ||
1137 | =head1 ACCESSING ZIP FILES | |
1138 | ||
1139 | Although it is possible to use this module to access .zip files, there | |
1140 | is a module on CPAN that will do all the hard work for you. Check out | |
1141 | ||
1142 | http://www.cpan.org/modules/by-module/Archive/Archive-Zip-*.tar.gz | |
1143 | ||
1144 | Assuming you don't want to use this module to access zip files there | |
1145 | are a number of undocumented features in the zlib library you need to | |
1146 | be aware of. | |
1147 | ||
1148 | =over 5 | |
1149 | ||
1150 | =item 1. | |
1151 | ||
1152 | When calling B<inflateInit> or B<deflateInit> the B<WindowBits> parameter | |
1153 | must be set to C<-MAX_WBITS>. This disables the creation of the zlib | |
1154 | header. | |
1155 | ||
1156 | =item 2. | |
1157 | ||
1158 | The zlib function B<inflate>, and so the B<inflate> method supplied in | |
1159 | this module, assume that there is at least one trailing byte after the | |
1160 | compressed data stream. Normally this isn't a problem because both | |
1161 | the gzip and zip file formats will guarantee that there is data directly | |
1162 | after the compressed data stream. | |
1163 | ||
1164 | =back | |
1165 | ||
1166 | =head1 CONSTANTS | |
1167 | ||
1168 | All the I<zlib> constants are automatically imported when you make use | |
1169 | of I<Compress::Zlib>. | |
1170 | ||
1171 | =head1 AUTHOR | |
1172 | ||
1173 | The I<Compress::Zlib> module was written by Paul Marquess, | |
1174 | F<pmqs@cpan.org>. The latest copy of the module can be | |
1175 | found on CPAN in F<modules/by-module/Compress/Compress-Zlib-x.x.tar.gz>. | |
1176 | ||
1177 | The primary site for the I<zlib> compression library is | |
1178 | F<http://www.zlib.org>. | |
1179 | ||
1180 | =head1 MODIFICATION HISTORY | |
1181 | ||
1182 | See the Changes file. |