1 package IO::Uncompress::RawInflate ;
8 use Compress::Raw::Zlib ;
9 use IO::Compress::Base::Common qw(:Status createSelfTiedObject);
11 use IO::Uncompress::Base ;
12 use IO::Uncompress::Adapter::Inflate ;
18 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, %DEFLATE_CONSTANTS, $RawInflateError);
20 $VERSION = '2.000_11';
21 $RawInflateError = '';
23 @ISA = qw( Exporter IO::Uncompress::Base );
24 @EXPORT_OK = qw( $RawInflateError rawinflate ) ;
25 %DEFLATE_CONSTANTS = ();
26 %EXPORT_TAGS = %IO::Uncompress::Base::EXPORT_TAGS ;
27 push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;
28 Exporter::export_ok_tags('all');
35 my $obj = createSelfTiedObject($class, \$RawInflateError);
36 $obj->_create(undef, 0, @_);
41 my $obj = createSelfTiedObject(undef, \$RawInflateError);
42 return $obj->_inf(@_);
64 my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject(
66 $got->value('ADLER32'),
70 return $self->saveErrorString(undef, $errstr, $errno)
73 *$self->{Uncomp} = $obj;
75 my $magic = $self->ckMagic()
78 *$self->{Info} = $self->readHeader($magic)
90 return $self->_isRaw() ;
100 'FingerprintLength' => 0,
102 'TrailerLength' => 0,
116 my $got = $self->_isRawx(@_);
119 *$self->{Pending} = *$self->{HeaderPending} ;
122 $self->pushBack(*$self->{HeaderPending});
123 *$self->{Uncomp}->reset();
125 *$self->{HeaderPending} = '';
135 $magic = '' unless defined $magic ;
139 $self->smartRead(\$buffer, *$self->{BlockSize}) >= 0
140 or return $self->saveErrorString(undef, "No data to read");
142 my $temp_buf = $magic . $buffer ;
143 *$self->{HeaderPending} = $temp_buf ;
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;
149 my $buf_len = *$self->{Uncomp}->uncompressedBytes();
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);
159 *$self->{EndStream} = 1 ;
160 $self->pushBack($temp_buf);
163 *$self->{HeaderPending} = $buffer ;
164 *$self->{InflatedBytesRead} = $buf_len ;
165 *$self->{TotalInflatedBytesRead} += $buf_len ;
166 *$self->{Type} = 'rfc1951';
168 $self->saveStatus(STATUS_OK);
173 'TrailerLength' => 0,
183 # inflateSync is a no-op in Plain mode
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} ;
192 *$self->{Strict} = 0 ;
199 if (length *$self->{Pending} )
201 $temp_buf = *$self->{Pending} ;
202 *$self->{Pending} = '';
206 $status = $self->smartRead(\$temp_buf, *$self->{BlockSize}) ;
207 return $self->saveErrorString(0, "Error Reading Data")
211 *$self->{EndStream} = 1 ;
212 return $self->saveErrorString(0, "unexpected end of file", STATUS_ERROR);
216 $status = *$self->{Uncomp}->sync($temp_buf) ;
218 if ($status == STATUS_OK)
220 *$self->{Pending} .= $temp_buf ;
224 last unless $status == STATUS_ERROR ;
235 # my $end_offset = 0;
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")
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) ;
245 # #(*$obj->{Deflate}, $status) = $inf->createDeflate();
247 ## *$obj->{Header} = *$inf->{Info}{Header};
248 ## *$obj->{UnCompSize_32bit} =
249 ## *$obj->{BytesWritten} = *$inf->{UnCompSize_32bit} ;
250 ## *$obj->{CompSize_32bit} = *$inf->{CompSize_32bit} ;
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';
261 ## #seek(*$obj->{FH}, $end_offset, SEEK_SET)
262 ## *$obj->{FH}->seek($end_offset, SEEK_SET)
263 ## or return $obj->saveErrorString(undef, $!, $!) ;
272 return 1 if *$self->{Closed} ;
273 return 1 if !length *$self->{Pending} && *$self->{EndStream} ;
278 $len = $self->_raw_read(\$buffer, 1)
279 while ! *$self->{EndStream} && $len >= 0 ;
281 #return $len if $len < 0 ? $len : 0 ;
282 return $len < 0 ? 0 : 1 ;
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;
295 ( $self->smartSeek($block_offset) &&
296 $self->smartRead(\$byte, 1) )
297 or return $self->saveErrorString(0, $!, $!);
299 #printf "#byte is %x\n", unpack('C*',$byte);
300 *$self->{Uncomp}->resetLastBlockByte($byte);
301 #printf "#to byte is %x\n", unpack('C*',$byte);
303 ( $self->smartSeek($block_offset) &&
304 $self->smartWrite($byte) )
305 or return $self->saveErrorString(0, $!, $!);
307 #$self->smartSeek($end_offset, 1);
315 my ($def, $status) = *$self->{Uncomp}->createDeflateStream(
317 -WindowBits => - MAX_WBITS,
318 -CRC32 => *$self->{Params}->value('CRC32'),
319 -ADLER32 => *$self->{Params}->value('ADLER32'),
322 return wantarray ? ($status, $def) : $def ;
335 IO::Uncompress::RawInflate - Read RFC 1951 files/buffers
341 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
343 my $status = rawinflate $input => $output [,OPTS]
344 or die "rawinflate failed: $RawInflateError\n";
346 my $z = new IO::Uncompress::RawInflate $input [OPTS]
347 or die "rawinflate failed: $RawInflateError\n";
349 $status = $z->read($buffer)
350 $status = $z->read($buffer, $length)
351 $status = $z->read($buffer, $length, $offset)
352 $line = $z->getline()
357 $status = $z->inflateSync()
360 $data = $z->getHeaderInfo()
362 $z->seek($position, $whence)
374 read($z, $buffer, $length);
375 read($z, $buffer, $length, $offset);
377 seek($z, $position, $whence)
388 B<WARNING -- This is a Beta release>.
392 =item * DO NOT use in production code.
394 =item * The documentation is incomplete in places.
396 =item * Parts of the interface defined here are tentative.
398 =item * Please report any problems you find.
406 This module provides a Perl interface that allows the reading of
407 files/buffers that conform to RFC 1951.
409 For writing RFC 1951 files/buffers, see the companion module IO::Compress::RawDeflate.
416 =head1 Functional Interface
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">
423 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
425 rawinflate $input => $output [,OPTS]
426 or die "rawinflate failed: $RawInflateError\n";
430 The functional interface needs Perl5.005 or better.
433 =head2 rawinflate $input => $output [, OPTS]
436 C<rawinflate> expects at least two parameters, C<$input> and C<$output>.
438 =head3 The C<$input> parameter
440 The parameter, C<$input>, is used to define the source of
443 It can take one of the following forms:
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.
455 If the C<$input> parameter is a filehandle, the input data will be
457 The string '-' can be used as an alias for standard input.
459 =item A scalar reference
461 If C<$input> is a scalar reference, the input data will be read
464 =item An array reference
466 If C<$input> is an array reference, each element in the array must be a
469 The input data will be read from each file in turn.
471 The complete array will be walked to ensure that it only
472 contains valid filenames before any data is uncompressed.
476 =item An Input FileGlob string
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.
482 If the fileglob does not match any files ...
484 See L<File::GlobMapper|File::GlobMapper> for more details.
489 If the C<$input> parameter is any other type, C<undef> will be returned.
493 =head3 The C<$output> parameter
495 The parameter C<$output> is used to control the destination of the
496 uncompressed data. This parameter can take one of these forms.
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.
508 If the C<$output> parameter is a filehandle, the uncompressed data
509 will be written to it.
510 The string '-' can be used as an alias for standard output.
513 =item A scalar reference
515 If C<$output> is a scalar reference, the uncompressed data will be
516 stored in C<$$output>.
520 =item An Array Reference
522 If C<$output> is an array reference, the uncompressed data will be
523 pushed onto the array.
525 =item An Output FileGlob
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.
531 When C<$output> is an fileglob string, C<$input> must also be a fileglob
532 string. Anything else is an error.
536 If the C<$output> parameter is any other type, C<undef> will be returned.
542 When C<$input> maps to multiple files/buffers and C<$output> is a single
543 file/buffer the uncompressed input files/buffers will all be stored
544 in C<$output> as a single uncompressed stream.
548 =head2 Optional Parameters
550 Unless specified below, the optional parameters for C<rawinflate>,
551 C<OPTS>, are the same as those used with the OO interface defined in the
552 L</"Constructor Options"> section below.
556 =item AutoClose =E<gt> 0|1
558 This option applies to any input or output data streams to
559 C<rawinflate> that are filehandles.
561 If C<AutoClose> is specified, and the value is true, it will result in all
562 input and/or output filehandles being closed once C<rawinflate> has
565 This parameter defaults to 0.
569 =item BinModeOut =E<gt> 0|1
571 When writing to a file or filehandle, set C<binmode> before writing to the
580 =item -Append =E<gt> 0|1
584 =item -MultiStream =E<gt> 0|1
586 Creates a new stream after each file.
599 To read the contents of the file C<file1.txt.1951> and write the
600 compressed data to the file C<file1.txt>.
604 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
606 my $input = "file1.txt.1951";
607 my $output = "file1.txt";
608 rawinflate $input => $output
609 or die "rawinflate failed: $RawInflateError\n";
612 To read from an existing Perl filehandle, C<$input>, and write the
613 uncompressed data to a buffer, C<$buffer>.
617 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
620 my $input = new IO::File "<file1.txt.1951"
621 or die "Cannot open 'file1.txt.1951': $!\n" ;
623 rawinflate $input => \$buffer
624 or die "rawinflate failed: $RawInflateError\n";
626 To uncompress all files in the directory "/my/home" that match "*.txt.1951" and store the compressed data in the same directory
630 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
632 rawinflate '</my/home/*.txt.1951>' => '</my/home/#1.txt>'
633 or die "rawinflate failed: $RawInflateError\n";
635 and if you want to compress each file one at a time, this will do the trick
639 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
641 for my $input ( glob "/my/home/*.txt.1951" )
644 $output =~ s/.1951// ;
645 rawinflate $input => $output
646 or die "Error compressing '$input': $RawInflateError\n";
653 The format of the constructor for IO::Uncompress::RawInflate is shown below
656 my $z = new IO::Uncompress::RawInflate $input [OPTS]
657 or die "IO::Uncompress::RawInflate failed: $RawInflateError\n";
659 Returns an C<IO::Uncompress::RawInflate> object on success and undef on failure.
660 The variable C<$RawInflateError> will contain an error message on failure.
662 If you are running Perl 5.005 or better the object, C<$z>, returned from
663 IO::Uncompress::RawInflate can be used exactly like an L<IO::File|IO::File> filehandle.
664 This means that all normal input file operations can be carried out with
665 C<$z>. For example, to read a line from a compressed file/buffer you can
666 use either of these forms
668 $line = $z->getline();
671 The mandatory parameter C<$input> is used to determine the source of the
672 compressed data. This parameter can take one of three forms.
678 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
679 file will be opened for reading and the compressed data will be read from it.
683 If the C<$input> parameter is a filehandle, the compressed data will be
685 The string '-' can be used as an alias for standard input.
688 =item A scalar reference
690 If C<$input> is a scalar reference, the compressed data will be read from
695 =head2 Constructor Options
698 The option names defined below are case insensitive and can be optionally
699 prefixed by a '-'. So all of the following are valid
706 OPTS is a combination of the following options:
710 =item -AutoClose =E<gt> 0|1
712 This option is only valid when the C<$input> parameter is a filehandle. If
713 specified, and the value is true, it will result in the file being closed once
714 either the C<close> method is called or the IO::Uncompress::RawInflate object is
717 This parameter defaults to 0.
719 =item -MultiStream =E<gt> 0|1
723 This option is a no-op.
727 =item -Prime =E<gt> $string
729 This option will uncompress the contents of C<$string> before processing the
732 This option can be useful when the compressed data is embedded in another
733 file/data structure and it is not possible to work out where the compressed
734 data begins without having to read the first few bytes. If this is the
735 case, the uncompression can be I<primed> with these bytes using this
738 =item -Transparent =E<gt> 0|1
740 If this option is set and the input file or buffer is not compressed data,
741 the module will allow reading of it anyway.
743 This option defaults to 1.
745 =item -BlockSize =E<gt> $num
747 When reading the compressed input data, IO::Uncompress::RawInflate will read it in
748 blocks of C<$num> bytes.
750 This option defaults to 4096.
752 =item -InputLength =E<gt> $size
754 When present this option will limit the number of compressed bytes read
755 from the input file/buffer to C<$size>. This option can be used in the
756 situation where there is useful data directly after the compressed data
757 stream and you know beforehand the exact length of the compressed data
760 This option is mostly used when reading from a filehandle, in which case
761 the file pointer will be left pointing to the first byte directly after the
762 compressed data stream.
766 This option defaults to off.
768 =item -Append =E<gt> 0|1
770 This option controls what the C<read> method does with uncompressed data.
772 If set to 1, all uncompressed data will be appended to the output parameter
773 of the C<read> method.
775 If set to 0, the contents of the output parameter of the C<read> method
776 will be overwritten by the uncompressed data.
780 =item -Strict =E<gt> 0|1
784 This option is a no-op.
803 $status = $z->read($buffer)
805 Reads a block of compressed data (the size the the compressed block is
806 determined by the C<Buffer> option in the constructor), uncompresses it and
807 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
808 set in the constructor, the uncompressed data will be appended to the
809 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
811 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
812 or a negative number on error.
818 $status = $z->read($buffer, $length)
819 $status = $z->read($buffer, $length, $offset)
821 $status = read($z, $buffer, $length)
822 $status = read($z, $buffer, $length, $offset)
824 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
826 The main difference between this form of the C<read> method and the
827 previous one, is that this one will attempt to return I<exactly> C<$length>
828 bytes. The only circumstances that this function will not is if end-of-file
829 or an IO error is encountered.
831 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
832 or a negative number on error.
839 $line = $z->getline()
844 This method fully supports the use of of the variable C<$/>
845 (or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
846 determine what constitutes an end of line. Both paragraph mode and file
847 slurp mode are supported.
856 Read a single character.
862 $char = $z->ungetc($string)
870 $status = $z->inflateSync()
879 $hdr = $z->getHeaderInfo();
880 @hdrs = $z->getHeaderInfo();
882 This method returns either a hash reference (in scalar context) or a list
883 or hash references (in array context) that contains information about each
884 of the header fields in the compressed data stream(s).
896 Returns the uncompressed file offset.
907 Returns true if the end of the compressed input stream has been reached.
913 $z->seek($position, $whence);
914 seek($z, $position, $whence);
919 Provides a sub-set of the C<seek> functionality, with the restriction
920 that it is only legal to seek forward in the input file/buffer.
921 It is a fatal error to attempt to seek backward.
925 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
926 SEEK_CUR or SEEK_END.
928 Returns 1 on success, 0 on failure.
937 This is a noop provided for completeness.
943 Returns true if the object currently refers to a opened file/buffer.
947 my $prev = $z->autoflush()
948 my $prev = $z->autoflush(EXPR)
950 If the C<$z> object is associated with a file or a filehandle, this method
951 returns the current autoflush setting for the underlying filehandle. If
952 C<EXPR> is present, and is non-zero, it will enable flushing after every
953 write/print operation.
955 If C<$z> is associated with a buffer, this method has no effect and always
958 B<Note> that the special variable C<$|> B<cannot> be used to set or
959 retrieve the autoflush setting.
961 =head2 input_line_number
963 $z->input_line_number()
964 $z->input_line_number(EXPR)
968 Returns the current uncompressed line number. If C<EXPR> is present it has
969 the effect of setting the line number. Note that setting the line number
970 does not change the current position within the file/buffer being read.
972 The contents of C<$/> are used to to determine what constitutes a line
982 If the C<$z> object is associated with a file or a filehandle, this method
983 will return the underlying file descriptor.
985 If the C<$z> object is is associated with a buffer, this method will
995 Closes the output file/buffer.
999 For most versions of Perl this method will be automatically invoked if
1000 the IO::Uncompress::RawInflate object is destroyed (either explicitly or by the
1001 variable with the reference to the object going out of scope). The
1002 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
1003 these cases, the C<close> method will be called automatically, but
1004 not until global destruction of all live objects when the program is
1007 Therefore, if you want your scripts to be able to run on all versions
1008 of Perl, you should call C<close> explicitly and not rely on automatic
1011 Returns true on success, otherwise 0.
1013 If the C<AutoClose> option has been enabled when the IO::Uncompress::RawInflate
1014 object was created, and the object is associated with a file, the
1015 underlying file will also be closed.
1022 No symbolic constants are required by this IO::Uncompress::RawInflate at present.
1028 Imports C<rawinflate> and C<$RawInflateError>.
1031 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
1042 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>
1044 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1046 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1047 L<Archive::Tar|Archive::Tar>,
1048 L<IO::Zlib|IO::Zlib>
1051 For RFC 1950, 1951 and 1952 see
1052 F<http://www.faqs.org/rfcs/rfc1950.html>,
1053 F<http://www.faqs.org/rfcs/rfc1951.html> and
1054 F<http://www.faqs.org/rfcs/rfc1952.html>
1056 The I<zlib> compression library was written by Jean-loup Gailly
1057 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1059 The primary site for the I<zlib> compression library is
1060 F<http://www.zlib.org>.
1062 The primary site for gzip is F<http://www.gzip.org>.
1069 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1073 =head1 MODIFICATION HISTORY
1075 See the Changes file.
1077 =head1 COPYRIGHT AND LICENSE
1079 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
1081 This program is free software; you can redistribute it and/or
1082 modify it under the same terms as Perl itself.