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