Commit | Line | Data |
642e522c |
1 | package IO::Uncompress::Inflate ; |
2 | # for RFC1950 |
3 | |
4 | use strict ; |
5 | use warnings; |
a02d0f6f |
6 | use bytes; |
642e522c |
7 | |
9b5fd1d4 |
8 | use IO::Compress::Base::Common 2.024 qw(:Status createSelfTiedObject); |
9 | use IO::Compress::Zlib::Constants 2.024 ; |
1a6a8453 |
10 | |
9b5fd1d4 |
11 | use IO::Uncompress::RawInflate 2.024 ; |
642e522c |
12 | |
13 | require Exporter ; |
14 | our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $InflateError); |
15 | |
9b5fd1d4 |
16 | $VERSION = '2.024'; |
642e522c |
17 | $InflateError = ''; |
18 | |
1a6a8453 |
19 | @ISA = qw( Exporter IO::Uncompress::RawInflate ); |
642e522c |
20 | @EXPORT_OK = qw( $InflateError inflate ) ; |
1a6a8453 |
21 | %EXPORT_TAGS = %IO::Uncompress::RawInflate::DEFLATE_CONSTANTS ; |
642e522c |
22 | push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; |
23 | Exporter::export_ok_tags('all'); |
24 | |
25 | |
26 | sub new |
27 | { |
1a6a8453 |
28 | my $class = shift ; |
29 | my $obj = createSelfTiedObject($class, \$InflateError); |
30 | |
31 | $obj->_create(undef, 0, @_); |
642e522c |
32 | } |
33 | |
34 | sub inflate |
35 | { |
1a6a8453 |
36 | my $obj = createSelfTiedObject(undef, \$InflateError); |
37 | return $obj->_inf(@_); |
38 | } |
39 | |
40 | sub getExtraParams |
41 | { |
42 | return (); |
43 | } |
44 | |
45 | sub ckParams |
46 | { |
47 | my $self = shift ; |
48 | my $got = shift ; |
49 | |
50 | # gunzip always needs adler32 |
51 | $got->value('ADLER32' => 1); |
52 | |
53 | return 1; |
54 | } |
55 | |
56 | sub ckMagic |
57 | { |
58 | my $self = shift; |
59 | |
60 | my $magic ; |
61 | $self->smartReadExact(\$magic, ZLIB_HEADER_SIZE); |
62 | |
63 | *$self->{HeaderPending} = $magic ; |
64 | |
65 | return $self->HeaderError("Header size is " . |
66 | ZLIB_HEADER_SIZE . " bytes") |
67 | if length $magic != ZLIB_HEADER_SIZE; |
68 | |
258133d1 |
69 | #return $self->HeaderError("CRC mismatch.") |
70 | return undef |
71 | if ! $self->isZlibMagic($magic) ; |
1a6a8453 |
72 | |
73 | *$self->{Type} = 'rfc1950'; |
74 | return $magic; |
75 | } |
76 | |
77 | sub readHeader |
78 | { |
79 | my $self = shift; |
80 | my $magic = shift ; |
81 | |
82 | return $self->_readDeflateHeader($magic) ; |
83 | } |
84 | |
85 | sub chkTrailer |
86 | { |
87 | my $self = shift; |
88 | my $trailer = shift; |
89 | |
90 | my $ADLER32 = unpack("N", $trailer) ; |
91 | *$self->{Info}{ADLER32} = $ADLER32; |
92 | return $self->TrailerError("CRC mismatch") |
93 | if *$self->{Strict} && $ADLER32 != *$self->{Uncomp}->adler32() ; |
94 | |
a02d0f6f |
95 | return STATUS_OK; |
1a6a8453 |
96 | } |
97 | |
98 | |
99 | |
100 | sub isZlibMagic |
101 | { |
258133d1 |
102 | my $self = shift; |
1a6a8453 |
103 | my $buffer = shift ; |
258133d1 |
104 | |
105 | return 0 |
106 | if length $buffer < ZLIB_HEADER_SIZE ; |
107 | |
1a6a8453 |
108 | my $hdr = unpack("n", $buffer) ; |
258133d1 |
109 | #return 0 if $hdr % 31 != 0 ; |
110 | return $self->HeaderError("CRC mismatch.") |
111 | if $hdr % 31 != 0 ; |
112 | |
113 | my ($CMF, $FLG) = unpack "C C", $buffer; |
114 | my $cm = bits($CMF, ZLIB_CMF_CM_OFFSET, ZLIB_CMF_CM_BITS) ; |
115 | |
116 | # Only Deflate supported |
117 | return $self->HeaderError("Not Deflate (CM is $cm)") |
118 | if $cm != ZLIB_CMF_CM_DEFLATED ; |
119 | |
120 | # Max window value is 7 for Deflate. |
121 | my $cinfo = bits($CMF, ZLIB_CMF_CINFO_OFFSET, ZLIB_CMF_CINFO_BITS) ; |
122 | return $self->HeaderError("CINFO > " . ZLIB_CMF_CINFO_MAX . |
123 | " (CINFO is $cinfo)") |
124 | if $cinfo > ZLIB_CMF_CINFO_MAX ; |
125 | |
126 | return 1; |
642e522c |
127 | } |
128 | |
1a6a8453 |
129 | sub bits |
130 | { |
131 | my $data = shift ; |
132 | my $offset = shift ; |
133 | my $mask = shift ; |
134 | |
135 | ($data >> $offset ) & $mask & 0xFF ; |
136 | } |
137 | |
138 | |
139 | sub _readDeflateHeader |
140 | { |
141 | my ($self, $buffer) = @_ ; |
142 | |
143 | # if (! $buffer) { |
144 | # $self->smartReadExact(\$buffer, ZLIB_HEADER_SIZE); |
145 | # |
146 | # *$self->{HeaderPending} = $buffer ; |
147 | # |
148 | # return $self->HeaderError("Header size is " . |
149 | # ZLIB_HEADER_SIZE . " bytes") |
150 | # if length $buffer != ZLIB_HEADER_SIZE; |
151 | # |
152 | # return $self->HeaderError("CRC mismatch.") |
153 | # if ! isZlibMagic($buffer) ; |
154 | # } |
155 | |
156 | my ($CMF, $FLG) = unpack "C C", $buffer; |
157 | my $FDICT = bits($FLG, ZLIB_FLG_FDICT_OFFSET, ZLIB_FLG_FDICT_BITS ), |
158 | |
159 | my $cm = bits($CMF, ZLIB_CMF_CM_OFFSET, ZLIB_CMF_CM_BITS) ; |
160 | $cm == ZLIB_CMF_CM_DEFLATED |
161 | or return $self->HeaderError("Not Deflate (CM is $cm)") ; |
162 | |
163 | my $DICTID; |
164 | if ($FDICT) { |
165 | $self->smartReadExact(\$buffer, ZLIB_FDICT_SIZE) |
166 | or return $self->TruncatedHeader("FDICT"); |
167 | |
168 | $DICTID = unpack("N", $buffer) ; |
169 | } |
170 | |
171 | *$self->{Type} = 'rfc1950'; |
172 | |
173 | return { |
174 | 'Type' => 'rfc1950', |
175 | 'FingerprintLength' => ZLIB_HEADER_SIZE, |
176 | 'HeaderLength' => ZLIB_HEADER_SIZE, |
177 | 'TrailerLength' => ZLIB_TRAILER_SIZE, |
178 | 'Header' => $buffer, |
179 | |
180 | CMF => $CMF , |
181 | CM => bits($CMF, ZLIB_CMF_CM_OFFSET, ZLIB_CMF_CM_BITS ), |
182 | CINFO => bits($CMF, ZLIB_CMF_CINFO_OFFSET, ZLIB_CMF_CINFO_BITS ), |
183 | FLG => $FLG , |
184 | FCHECK => bits($FLG, ZLIB_FLG_FCHECK_OFFSET, ZLIB_FLG_FCHECK_BITS), |
185 | FDICT => bits($FLG, ZLIB_FLG_FDICT_OFFSET, ZLIB_FLG_FDICT_BITS ), |
186 | FLEVEL => bits($FLG, ZLIB_FLG_LEVEL_OFFSET, ZLIB_FLG_LEVEL_BITS ), |
187 | DICTID => $DICTID , |
188 | |
189 | }; |
190 | } |
191 | |
192 | |
193 | |
194 | |
642e522c |
195 | 1 ; |
196 | |
197 | __END__ |
198 | |
199 | |
200 | =head1 NAME |
201 | |
cb7abd7f |
202 | IO::Uncompress::Inflate - Read RFC 1950 files/buffers |
203 | |
642e522c |
204 | =head1 SYNOPSIS |
205 | |
206 | use IO::Uncompress::Inflate qw(inflate $InflateError) ; |
207 | |
208 | my $status = inflate $input => $output [,OPTS] |
209 | or die "inflate failed: $InflateError\n"; |
210 | |
211 | my $z = new IO::Uncompress::Inflate $input [OPTS] |
212 | or die "inflate failed: $InflateError\n"; |
213 | |
214 | $status = $z->read($buffer) |
215 | $status = $z->read($buffer, $length) |
216 | $status = $z->read($buffer, $length, $offset) |
217 | $line = $z->getline() |
218 | $char = $z->getc() |
219 | $char = $z->ungetc() |
a02d0f6f |
220 | $char = $z->opened() |
221 | |
642e522c |
222 | $status = $z->inflateSync() |
a02d0f6f |
223 | |
e7d45986 |
224 | $data = $z->trailingData() |
225 | $status = $z->nextStream() |
642e522c |
226 | $data = $z->getHeaderInfo() |
227 | $z->tell() |
228 | $z->seek($position, $whence) |
229 | $z->binmode() |
230 | $z->fileno() |
231 | $z->eof() |
232 | $z->close() |
233 | |
234 | $InflateError ; |
235 | |
236 | # IO::File mode |
237 | |
238 | <$z> |
239 | read($z, $buffer); |
240 | read($z, $buffer, $length); |
241 | read($z, $buffer, $length, $offset); |
242 | tell($z) |
243 | seek($z, $position, $whence) |
244 | binmode($z) |
245 | fileno($z) |
246 | eof($z) |
247 | close($z) |
248 | |
642e522c |
249 | =head1 DESCRIPTION |
250 | |
1a6a8453 |
251 | This module provides a Perl interface that allows the reading of |
642e522c |
252 | files/buffers that conform to RFC 1950. |
253 | |
1a6a8453 |
254 | For writing RFC 1950 files/buffers, see the companion module IO::Compress::Deflate. |
642e522c |
255 | |
642e522c |
256 | =head1 Functional Interface |
257 | |
1a6a8453 |
258 | A top-level function, C<inflate>, is provided to carry out |
259 | "one-shot" uncompression between buffers and/or files. For finer |
260 | control over the uncompression process, see the L</"OO Interface"> |
261 | section. |
642e522c |
262 | |
263 | use IO::Uncompress::Inflate qw(inflate $InflateError) ; |
264 | |
265 | inflate $input => $output [,OPTS] |
266 | or die "inflate failed: $InflateError\n"; |
267 | |
642e522c |
268 | The functional interface needs Perl5.005 or better. |
269 | |
642e522c |
270 | =head2 inflate $input => $output [, OPTS] |
271 | |
1a6a8453 |
272 | C<inflate> expects at least two parameters, C<$input> and C<$output>. |
642e522c |
273 | |
274 | =head3 The C<$input> parameter |
275 | |
276 | The parameter, C<$input>, is used to define the source of |
277 | the compressed data. |
278 | |
279 | It can take one of the following forms: |
280 | |
281 | =over 5 |
282 | |
283 | =item A filename |
284 | |
285 | If the C<$input> parameter is a simple scalar, it is assumed to be a |
286 | filename. This file will be opened for reading and the input data |
287 | will be read from it. |
288 | |
289 | =item A filehandle |
290 | |
291 | If the C<$input> parameter is a filehandle, the input data will be |
292 | read from it. |
293 | The string '-' can be used as an alias for standard input. |
294 | |
295 | =item A scalar reference |
296 | |
297 | If C<$input> is a scalar reference, the input data will be read |
298 | from C<$$input>. |
299 | |
300 | =item An array reference |
301 | |
1a6a8453 |
302 | If C<$input> is an array reference, each element in the array must be a |
303 | filename. |
304 | |
305 | The input data will be read from each file in turn. |
306 | |
642e522c |
307 | The complete array will be walked to ensure that it only |
1a6a8453 |
308 | contains valid filenames before any data is uncompressed. |
309 | |
642e522c |
310 | =item An Input FileGlob string |
311 | |
312 | If C<$input> is a string that is delimited by the characters "<" and ">" |
313 | C<inflate> will assume that it is an I<input fileglob string>. The |
314 | input is the list of files that match the fileglob. |
315 | |
316 | If the fileglob does not match any files ... |
317 | |
318 | See L<File::GlobMapper|File::GlobMapper> for more details. |
319 | |
642e522c |
320 | =back |
321 | |
322 | If the C<$input> parameter is any other type, C<undef> will be returned. |
323 | |
642e522c |
324 | =head3 The C<$output> parameter |
325 | |
326 | The parameter C<$output> is used to control the destination of the |
327 | uncompressed data. This parameter can take one of these forms. |
328 | |
329 | =over 5 |
330 | |
331 | =item A filename |
332 | |
1a6a8453 |
333 | If the C<$output> parameter is a simple scalar, it is assumed to be a |
334 | filename. This file will be opened for writing and the uncompressed |
335 | data will be written to it. |
642e522c |
336 | |
337 | =item A filehandle |
338 | |
1a6a8453 |
339 | If the C<$output> parameter is a filehandle, the uncompressed data |
340 | will be written to it. |
642e522c |
341 | The string '-' can be used as an alias for standard output. |
342 | |
642e522c |
343 | =item A scalar reference |
344 | |
1a6a8453 |
345 | If C<$output> is a scalar reference, the uncompressed data will be |
346 | stored in C<$$output>. |
642e522c |
347 | |
642e522c |
348 | =item An Array Reference |
349 | |
1a6a8453 |
350 | If C<$output> is an array reference, the uncompressed data will be |
351 | pushed onto the array. |
642e522c |
352 | |
353 | =item An Output FileGlob |
354 | |
355 | If C<$output> is a string that is delimited by the characters "<" and ">" |
356 | C<inflate> will assume that it is an I<output fileglob string>. The |
357 | output is the list of files that match the fileglob. |
358 | |
359 | When C<$output> is an fileglob string, C<$input> must also be a fileglob |
360 | string. Anything else is an error. |
361 | |
362 | =back |
363 | |
364 | If the C<$output> parameter is any other type, C<undef> will be returned. |
365 | |
642e522c |
366 | =head2 Notes |
367 | |
c70c1701 |
368 | When C<$input> maps to multiple compressed files/buffers and C<$output> is |
369 | a single file/buffer, after uncompression C<$output> will contain a |
370 | concatenation of all the uncompressed data from each of the input |
371 | files/buffers. |
372 | |
642e522c |
373 | =head2 Optional Parameters |
374 | |
375 | Unless specified below, the optional parameters for C<inflate>, |
376 | C<OPTS>, are the same as those used with the OO interface defined in the |
377 | L</"Constructor Options"> section below. |
378 | |
379 | =over 5 |
380 | |
e7d45986 |
381 | =item C<< AutoClose => 0|1 >> |
642e522c |
382 | |
1a6a8453 |
383 | This option applies to any input or output data streams to |
384 | C<inflate> that are filehandles. |
642e522c |
385 | |
386 | If C<AutoClose> is specified, and the value is true, it will result in all |
387 | input and/or output filehandles being closed once C<inflate> has |
388 | completed. |
389 | |
390 | This parameter defaults to 0. |
391 | |
e7d45986 |
392 | =item C<< BinModeOut => 0|1 >> |
1a6a8453 |
393 | |
394 | When writing to a file or filehandle, set C<binmode> before writing to the |
395 | file. |
396 | |
397 | Defaults to 0. |
398 | |
e7d45986 |
399 | =item C<< Append => 0|1 >> |
642e522c |
400 | |
9b5fd1d4 |
401 | The behaviour of this option is dependent on the type of output data |
402 | stream. |
403 | |
404 | =over 5 |
405 | |
406 | =item * A Buffer |
407 | |
408 | If C<Append> is enabled, all uncompressed data will be append to the end of |
409 | the output buffer. Otherwise the output buffer will be cleared before any |
410 | uncompressed data is written to it. |
411 | |
412 | =item * A Filename |
413 | |
414 | If C<Append> is enabled, the file will be opened in append mode. Otherwise |
415 | the contents of the file, if any, will be truncated before any uncompressed |
416 | data is written to it. |
417 | |
418 | =item * A Filehandle |
419 | |
420 | If C<Append> is enabled, the filehandle will be positioned to the end of |
421 | the file via a call to C<seek> before any uncompressed data is |
422 | written to it. Otherwise the file pointer will not be moved. |
423 | |
424 | =back |
425 | |
426 | When C<Append> is specified, and set to true, it will I<append> all uncompressed |
427 | data to the output data stream. |
428 | |
429 | So when the output is a filehandle it will carry out a seek to the eof |
430 | before writing any uncompressed data. If the output is a filename, it will be opened for |
431 | appending. If the output is a buffer, all uncompressed data will be appened to |
432 | the existing buffer. |
433 | |
434 | Conversely when C<Append> is not specified, or it is present and is set to |
435 | false, it will operate as follows. |
436 | |
437 | When the output is a filename, it will truncate the contents of the file |
438 | before writing any uncompressed data. If the output is a filehandle |
439 | its position will not be changed. If the output is a buffer, it will be |
440 | wiped before any uncompressed data is output. |
441 | |
442 | Defaults to 0. |
642e522c |
443 | |
e7d45986 |
444 | =item C<< MultiStream => 0|1 >> |
1a6a8453 |
445 | |
e7d45986 |
446 | If the input file/buffer contains multiple compressed data streams, this |
447 | option will uncompress the whole lot as a single data stream. |
1a6a8453 |
448 | |
e7d45986 |
449 | Defaults to 0. |
1a6a8453 |
450 | |
258133d1 |
451 | =item C<< TrailingData => $scalar >> |
452 | |
453 | Returns the data, if any, that is present immediately after the compressed |
454 | data stream once uncompression is complete. |
455 | |
456 | This option can be used when there is useful information immediately |
457 | following the compressed data stream, and you don't know the length of the |
458 | compressed data stream. |
459 | |
460 | If the input is a buffer, C<trailingData> will return everything from the |
461 | end of the compressed data stream to the end of the buffer. |
462 | |
463 | If the input is a filehandle, C<trailingData> will return the data that is |
464 | left in the filehandle input buffer once the end of the compressed data |
465 | stream has been reached. You can then use the filehandle to read the rest |
466 | of the input file. |
467 | |
468 | Don't bother using C<trailingData> if the input is a filename. |
469 | |
258133d1 |
470 | If you know the length of the compressed data stream before you start |
471 | uncompressing, you can avoid having to use C<trailingData> by setting the |
472 | C<InputLength> option. |
473 | |
642e522c |
474 | =back |
475 | |
642e522c |
476 | =head2 Examples |
477 | |
478 | To read the contents of the file C<file1.txt.1950> and write the |
10c2b2bb |
479 | uncompressed data to the file C<file1.txt>. |
642e522c |
480 | |
481 | use strict ; |
482 | use warnings ; |
483 | use IO::Uncompress::Inflate qw(inflate $InflateError) ; |
484 | |
485 | my $input = "file1.txt.1950"; |
486 | my $output = "file1.txt"; |
487 | inflate $input => $output |
488 | or die "inflate failed: $InflateError\n"; |
489 | |
642e522c |
490 | To read from an existing Perl filehandle, C<$input>, and write the |
491 | uncompressed data to a buffer, C<$buffer>. |
492 | |
493 | use strict ; |
494 | use warnings ; |
495 | use IO::Uncompress::Inflate qw(inflate $InflateError) ; |
496 | use IO::File ; |
497 | |
498 | my $input = new IO::File "<file1.txt.1950" |
499 | or die "Cannot open 'file1.txt.1950': $!\n" ; |
500 | my $buffer ; |
501 | inflate $input => \$buffer |
502 | or die "inflate failed: $InflateError\n"; |
503 | |
504 | To uncompress all files in the directory "/my/home" that match "*.txt.1950" and store the compressed data in the same directory |
505 | |
506 | use strict ; |
507 | use warnings ; |
508 | use IO::Uncompress::Inflate qw(inflate $InflateError) ; |
509 | |
510 | inflate '</my/home/*.txt.1950>' => '</my/home/#1.txt>' |
511 | or die "inflate failed: $InflateError\n"; |
512 | |
513 | and if you want to compress each file one at a time, this will do the trick |
514 | |
515 | use strict ; |
516 | use warnings ; |
517 | use IO::Uncompress::Inflate qw(inflate $InflateError) ; |
518 | |
519 | for my $input ( glob "/my/home/*.txt.1950" ) |
520 | { |
521 | my $output = $input; |
522 | $output =~ s/.1950// ; |
523 | inflate $input => $output |
524 | or die "Error compressing '$input': $InflateError\n"; |
525 | } |
526 | |
527 | =head1 OO Interface |
528 | |
529 | =head2 Constructor |
530 | |
531 | The format of the constructor for IO::Uncompress::Inflate is shown below |
532 | |
642e522c |
533 | my $z = new IO::Uncompress::Inflate $input [OPTS] |
534 | or die "IO::Uncompress::Inflate failed: $InflateError\n"; |
535 | |
536 | Returns an C<IO::Uncompress::Inflate> object on success and undef on failure. |
537 | The variable C<$InflateError> will contain an error message on failure. |
538 | |
1a6a8453 |
539 | If you are running Perl 5.005 or better the object, C<$z>, returned from |
540 | IO::Uncompress::Inflate can be used exactly like an L<IO::File|IO::File> filehandle. |
541 | This means that all normal input file operations can be carried out with |
542 | C<$z>. For example, to read a line from a compressed file/buffer you can |
543 | use either of these forms |
642e522c |
544 | |
545 | $line = $z->getline(); |
546 | $line = <$z>; |
547 | |
548 | The mandatory parameter C<$input> is used to determine the source of the |
549 | compressed data. This parameter can take one of three forms. |
550 | |
551 | =over 5 |
552 | |
553 | =item A filename |
554 | |
555 | If the C<$input> parameter is a scalar, it is assumed to be a filename. This |
556 | file will be opened for reading and the compressed data will be read from it. |
557 | |
558 | =item A filehandle |
559 | |
560 | If the C<$input> parameter is a filehandle, the compressed data will be |
561 | read from it. |
562 | The string '-' can be used as an alias for standard input. |
563 | |
642e522c |
564 | =item A scalar reference |
565 | |
566 | If C<$input> is a scalar reference, the compressed data will be read from |
567 | C<$$output>. |
568 | |
569 | =back |
570 | |
571 | =head2 Constructor Options |
572 | |
642e522c |
573 | The option names defined below are case insensitive and can be optionally |
574 | prefixed by a '-'. So all of the following are valid |
575 | |
576 | -AutoClose |
577 | -autoclose |
578 | AUTOCLOSE |
579 | autoclose |
580 | |
581 | OPTS is a combination of the following options: |
582 | |
583 | =over 5 |
584 | |
e7d45986 |
585 | =item C<< AutoClose => 0|1 >> |
642e522c |
586 | |
587 | This option is only valid when the C<$input> parameter is a filehandle. If |
588 | specified, and the value is true, it will result in the file being closed once |
589 | either the C<close> method is called or the IO::Uncompress::Inflate object is |
590 | destroyed. |
591 | |
592 | This parameter defaults to 0. |
593 | |
e7d45986 |
594 | =item C<< MultiStream => 0|1 >> |
642e522c |
595 | |
642e522c |
596 | Allows multiple concatenated compressed streams to be treated as a single |
597 | compressed stream. Decompression will stop once either the end of the |
598 | file/buffer is reached, an error is encountered (premature eof, corrupt |
599 | compressed data) or the end of a stream is not immediately followed by the |
600 | start of another stream. |
601 | |
602 | This parameter defaults to 0. |
603 | |
e7d45986 |
604 | =item C<< Prime => $string >> |
642e522c |
605 | |
606 | This option will uncompress the contents of C<$string> before processing the |
607 | input file/buffer. |
608 | |
609 | This option can be useful when the compressed data is embedded in another |
610 | file/data structure and it is not possible to work out where the compressed |
1a6a8453 |
611 | data begins without having to read the first few bytes. If this is the |
612 | case, the uncompression can be I<primed> with these bytes using this |
613 | option. |
642e522c |
614 | |
e7d45986 |
615 | =item C<< Transparent => 0|1 >> |
642e522c |
616 | |
f6fd7794 |
617 | If this option is set and the input file/buffer is not compressed data, |
642e522c |
618 | the module will allow reading of it anyway. |
619 | |
f6fd7794 |
620 | In addition, if the input file/buffer does contain compressed data and |
621 | there is non-compressed data immediately following it, setting this option |
622 | will make this module treat the whole file/bufffer as a single data stream. |
623 | |
642e522c |
624 | This option defaults to 1. |
625 | |
e7d45986 |
626 | =item C<< BlockSize => $num >> |
642e522c |
627 | |
1a6a8453 |
628 | When reading the compressed input data, IO::Uncompress::Inflate will read it in |
629 | blocks of C<$num> bytes. |
642e522c |
630 | |
631 | This option defaults to 4096. |
632 | |
e7d45986 |
633 | =item C<< InputLength => $size >> |
642e522c |
634 | |
1a6a8453 |
635 | When present this option will limit the number of compressed bytes read |
636 | from the input file/buffer to C<$size>. This option can be used in the |
637 | situation where there is useful data directly after the compressed data |
638 | stream and you know beforehand the exact length of the compressed data |
639 | stream. |
642e522c |
640 | |
1a6a8453 |
641 | This option is mostly used when reading from a filehandle, in which case |
642 | the file pointer will be left pointing to the first byte directly after the |
642e522c |
643 | compressed data stream. |
644 | |
642e522c |
645 | This option defaults to off. |
646 | |
e7d45986 |
647 | =item C<< Append => 0|1 >> |
642e522c |
648 | |
649 | This option controls what the C<read> method does with uncompressed data. |
650 | |
1a6a8453 |
651 | If set to 1, all uncompressed data will be appended to the output parameter |
652 | of the C<read> method. |
642e522c |
653 | |
1a6a8453 |
654 | If set to 0, the contents of the output parameter of the C<read> method |
655 | will be overwritten by the uncompressed data. |
642e522c |
656 | |
657 | Defaults to 0. |
658 | |
e7d45986 |
659 | =item C<< Strict => 0|1 >> |
642e522c |
660 | |
642e522c |
661 | This option controls whether the extra checks defined below are used when |
1a6a8453 |
662 | carrying out the decompression. When Strict is on, the extra tests are |
663 | carried out, when Strict is off they are not. |
642e522c |
664 | |
665 | The default for this option is off. |
666 | |
642e522c |
667 | =over 5 |
668 | |
669 | =item 1 |
670 | |
671 | The ADLER32 checksum field must be present. |
672 | |
673 | =item 2 |
674 | |
675 | The value of the ADLER32 field read must match the adler32 value of the |
676 | uncompressed data actually contained in the file. |
677 | |
678 | =back |
679 | |
642e522c |
680 | =back |
681 | |
682 | =head2 Examples |
683 | |
684 | TODO |
685 | |
686 | =head1 Methods |
687 | |
688 | =head2 read |
689 | |
690 | Usage is |
691 | |
692 | $status = $z->read($buffer) |
693 | |
694 | Reads a block of compressed data (the size the the compressed block is |
695 | determined by the C<Buffer> option in the constructor), uncompresses it and |
1a6a8453 |
696 | writes any uncompressed data into C<$buffer>. If the C<Append> parameter is |
697 | set in the constructor, the uncompressed data will be appended to the |
698 | C<$buffer> parameter. Otherwise C<$buffer> will be overwritten. |
642e522c |
699 | |
1a6a8453 |
700 | Returns the number of uncompressed bytes written to C<$buffer>, zero if eof |
701 | or a negative number on error. |
642e522c |
702 | |
703 | =head2 read |
704 | |
705 | Usage is |
706 | |
707 | $status = $z->read($buffer, $length) |
708 | $status = $z->read($buffer, $length, $offset) |
709 | |
710 | $status = read($z, $buffer, $length) |
711 | $status = read($z, $buffer, $length, $offset) |
712 | |
713 | Attempt to read C<$length> bytes of uncompressed data into C<$buffer>. |
714 | |
1a6a8453 |
715 | The main difference between this form of the C<read> method and the |
716 | previous one, is that this one will attempt to return I<exactly> C<$length> |
717 | bytes. The only circumstances that this function will not is if end-of-file |
718 | or an IO error is encountered. |
642e522c |
719 | |
1a6a8453 |
720 | Returns the number of uncompressed bytes written to C<$buffer>, zero if eof |
721 | or a negative number on error. |
642e522c |
722 | |
642e522c |
723 | =head2 getline |
724 | |
725 | Usage is |
726 | |
727 | $line = $z->getline() |
728 | $line = <$z> |
729 | |
730 | Reads a single line. |
731 | |
258133d1 |
732 | This method fully supports the use of of the variable C<$/> (or |
733 | C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to |
734 | determine what constitutes an end of line. Paragraph mode, record mode and |
735 | file slurp mode are all supported. |
642e522c |
736 | |
642e522c |
737 | =head2 getc |
738 | |
739 | Usage is |
740 | |
741 | $char = $z->getc() |
742 | |
743 | Read a single character. |
744 | |
745 | =head2 ungetc |
746 | |
747 | Usage is |
748 | |
749 | $char = $z->ungetc($string) |
750 | |
642e522c |
751 | =head2 inflateSync |
752 | |
753 | Usage is |
754 | |
755 | $status = $z->inflateSync() |
756 | |
757 | TODO |
758 | |
759 | =head2 getHeaderInfo |
760 | |
761 | Usage is |
762 | |
1a6a8453 |
763 | $hdr = $z->getHeaderInfo(); |
764 | @hdrs = $z->getHeaderInfo(); |
642e522c |
765 | |
1a6a8453 |
766 | This method returns either a hash reference (in scalar context) or a list |
767 | or hash references (in array context) that contains information about each |
768 | of the header fields in the compressed data stream(s). |
642e522c |
769 | |
642e522c |
770 | =head2 tell |
771 | |
772 | Usage is |
773 | |
774 | $z->tell() |
775 | tell $z |
776 | |
777 | Returns the uncompressed file offset. |
778 | |
779 | =head2 eof |
780 | |
781 | Usage is |
782 | |
783 | $z->eof(); |
784 | eof($z); |
785 | |
642e522c |
786 | Returns true if the end of the compressed input stream has been reached. |
787 | |
642e522c |
788 | =head2 seek |
789 | |
790 | $z->seek($position, $whence); |
791 | seek($z, $position, $whence); |
792 | |
642e522c |
793 | Provides a sub-set of the C<seek> functionality, with the restriction |
794 | that it is only legal to seek forward in the input file/buffer. |
795 | It is a fatal error to attempt to seek backward. |
796 | |
642e522c |
797 | The C<$whence> parameter takes one the usual values, namely SEEK_SET, |
798 | SEEK_CUR or SEEK_END. |
799 | |
800 | Returns 1 on success, 0 on failure. |
801 | |
802 | =head2 binmode |
803 | |
804 | Usage is |
805 | |
806 | $z->binmode |
807 | binmode $z ; |
808 | |
809 | This is a noop provided for completeness. |
810 | |
a02d0f6f |
811 | =head2 opened |
812 | |
813 | $z->opened() |
814 | |
815 | Returns true if the object currently refers to a opened file/buffer. |
816 | |
817 | =head2 autoflush |
818 | |
819 | my $prev = $z->autoflush() |
820 | my $prev = $z->autoflush(EXPR) |
821 | |
822 | If the C<$z> object is associated with a file or a filehandle, this method |
823 | returns the current autoflush setting for the underlying filehandle. If |
824 | C<EXPR> is present, and is non-zero, it will enable flushing after every |
825 | write/print operation. |
826 | |
827 | If C<$z> is associated with a buffer, this method has no effect and always |
828 | returns C<undef>. |
829 | |
830 | B<Note> that the special variable C<$|> B<cannot> be used to set or |
831 | retrieve the autoflush setting. |
832 | |
833 | =head2 input_line_number |
834 | |
835 | $z->input_line_number() |
836 | $z->input_line_number(EXPR) |
837 | |
a02d0f6f |
838 | Returns the current uncompressed line number. If C<EXPR> is present it has |
839 | the effect of setting the line number. Note that setting the line number |
840 | does not change the current position within the file/buffer being read. |
841 | |
842 | The contents of C<$/> are used to to determine what constitutes a line |
843 | terminator. |
844 | |
642e522c |
845 | =head2 fileno |
846 | |
847 | $z->fileno() |
848 | fileno($z) |
849 | |
d54256af |
850 | If the C<$z> object is associated with a file or a filehandle, C<fileno> |
851 | will return the underlying file descriptor. Once the C<close> method is |
852 | called C<fileno> will return C<undef>. |
642e522c |
853 | |
d54256af |
854 | If the C<$z> object is is associated with a buffer, this method will return |
855 | C<undef>. |
642e522c |
856 | |
857 | =head2 close |
858 | |
859 | $z->close() ; |
860 | close $z ; |
861 | |
642e522c |
862 | Closes the output file/buffer. |
863 | |
642e522c |
864 | For most versions of Perl this method will be automatically invoked if |
865 | the IO::Uncompress::Inflate object is destroyed (either explicitly or by the |
866 | variable with the reference to the object going out of scope). The |
867 | exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In |
868 | these cases, the C<close> method will be called automatically, but |
869 | not until global destruction of all live objects when the program is |
870 | terminating. |
871 | |
872 | Therefore, if you want your scripts to be able to run on all versions |
873 | of Perl, you should call C<close> explicitly and not rely on automatic |
874 | closing. |
875 | |
876 | Returns true on success, otherwise 0. |
877 | |
878 | If the C<AutoClose> option has been enabled when the IO::Uncompress::Inflate |
879 | object was created, and the object is associated with a file, the |
880 | underlying file will also be closed. |
881 | |
e7d45986 |
882 | =head2 nextStream |
883 | |
884 | Usage is |
885 | |
886 | my $status = $z->nextStream(); |
887 | |
888 | Skips to the next compressed data stream in the input file/buffer. If a new |
258133d1 |
889 | compressed data stream is found, the eof marker will be cleared and C<$.> |
890 | will be reset to 0. |
e7d45986 |
891 | |
892 | Returns 1 if a new stream was found, 0 if none was found, and -1 if an |
893 | error was encountered. |
894 | |
895 | =head2 trailingData |
896 | |
897 | Usage is |
898 | |
899 | my $data = $z->trailingData(); |
900 | |
258133d1 |
901 | Returns the data, if any, that is present immediately after the compressed |
902 | data stream once uncompression is complete. It only makes sense to call |
903 | this method once the end of the compressed data stream has been |
904 | encountered. |
905 | |
906 | This option can be used when there is useful information immediately |
907 | following the compressed data stream, and you don't know the length of the |
908 | compressed data stream. |
909 | |
910 | If the input is a buffer, C<trailingData> will return everything from the |
911 | end of the compressed data stream to the end of the buffer. |
912 | |
913 | If the input is a filehandle, C<trailingData> will return the data that is |
914 | left in the filehandle input buffer once the end of the compressed data |
915 | stream has been reached. You can then use the filehandle to read the rest |
916 | of the input file. |
917 | |
918 | Don't bother using C<trailingData> if the input is a filename. |
919 | |
258133d1 |
920 | If you know the length of the compressed data stream before you start |
921 | uncompressing, you can avoid having to use C<trailingData> by setting the |
922 | C<InputLength> option in the constructor. |
e7d45986 |
923 | |
642e522c |
924 | =head1 Importing |
925 | |
926 | No symbolic constants are required by this IO::Uncompress::Inflate at present. |
927 | |
928 | =over 5 |
929 | |
930 | =item :all |
931 | |
932 | Imports C<inflate> and C<$InflateError>. |
933 | Same as doing this |
934 | |
935 | use IO::Uncompress::Inflate qw(inflate $InflateError) ; |
936 | |
937 | =back |
938 | |
939 | =head1 EXAMPLES |
940 | |
d54256af |
941 | =head2 Working with Net::FTP |
642e522c |
942 | |
d54256af |
943 | See L<IO::Uncompress::Inflate::FAQ|IO::Uncompress::Inflate::FAQ/"Compressed files and Net::FTP"> |
642e522c |
944 | |
945 | =head1 SEE ALSO |
946 | |
9b5fd1d4 |
947 | L<Compress::Zlib>, L<IO::Compress::Gzip>, L<IO::Uncompress::Gunzip>, L<IO::Compress::Deflate>, L<IO::Compress::RawDeflate>, L<IO::Uncompress::RawInflate>, L<IO::Compress::Bzip2>, L<IO::Uncompress::Bunzip2>, L<IO::Compress::Lzma>, L<IO::Uncompress::UnLzma>, L<IO::Compress::Xz>, L<IO::Uncompress::UnXz>, L<IO::Compress::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress> |
642e522c |
948 | |
949 | L<Compress::Zlib::FAQ|Compress::Zlib::FAQ> |
950 | |
a02d0f6f |
951 | L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>, |
952 | L<Archive::Tar|Archive::Tar>, |
642e522c |
953 | L<IO::Zlib|IO::Zlib> |
954 | |
955 | For RFC 1950, 1951 and 1952 see |
956 | F<http://www.faqs.org/rfcs/rfc1950.html>, |
957 | F<http://www.faqs.org/rfcs/rfc1951.html> and |
958 | F<http://www.faqs.org/rfcs/rfc1952.html> |
959 | |
a02d0f6f |
960 | The I<zlib> compression library was written by Jean-loup Gailly |
961 | F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>. |
962 | |
963 | The primary site for the I<zlib> compression library is |
964 | F<http://www.zlib.org>. |
965 | |
966 | The primary site for gzip is F<http://www.gzip.org>. |
967 | |
642e522c |
968 | =head1 AUTHOR |
969 | |
cb7abd7f |
970 | This module was written by Paul Marquess, F<pmqs@cpan.org>. |
642e522c |
971 | |
642e522c |
972 | =head1 MODIFICATION HISTORY |
973 | |
974 | See the Changes file. |
975 | |
976 | =head1 COPYRIGHT AND LICENSE |
642e522c |
977 | |
9b5fd1d4 |
978 | Copyright (c) 2005-2010 Paul Marquess. All rights reserved. |
a02d0f6f |
979 | |
642e522c |
980 | This program is free software; you can redistribute it and/or |
981 | modify it under the same terms as Perl itself. |
982 | |