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_13';
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();
150 my $buf_len = length $buffer;
152 if ($status == STATUS_ENDSTREAM) {
153 if (*$self->{MultiStream}
154 && (length $temp_buf || ! $self->smartEof())){
155 *$self->{NewStream} = 1 ;
156 *$self->{EndStream} = 0 ;
157 $self->pushBack($temp_buf);
160 *$self->{EndStream} = 1 ;
161 $self->pushBack($temp_buf);
164 *$self->{HeaderPending} = $buffer ;
165 *$self->{InflatedBytesRead} = $buf_len ;
166 *$self->{TotalInflatedBytesRead} += $buf_len ;
167 *$self->{Type} = 'rfc1951';
169 $self->saveStatus(STATUS_OK);
174 'TrailerLength' => 0,
184 # inflateSync is a no-op in Plain mode
188 return 0 if *$self->{Closed} ;
189 #return G_EOF if !length *$self->{Pending} && *$self->{EndStream} ;
190 return 0 if ! length *$self->{Pending} && *$self->{EndStream} ;
193 *$self->{Strict} = 0 ;
200 if (length *$self->{Pending} )
202 $temp_buf = *$self->{Pending} ;
203 *$self->{Pending} = '';
207 $status = $self->smartRead(\$temp_buf, *$self->{BlockSize}) ;
208 return $self->saveErrorString(0, "Error Reading Data")
212 *$self->{EndStream} = 1 ;
213 return $self->saveErrorString(0, "unexpected end of file", STATUS_ERROR);
217 $status = *$self->{Uncomp}->sync($temp_buf) ;
219 if ($status == STATUS_OK)
221 *$self->{Pending} .= $temp_buf ;
225 last unless $status == STATUS_ERROR ;
236 # my $end_offset = 0;
238 # $status = $self->scan()
239 # #or return $self->saveErrorString(undef, "Error Scanning: $$error_ref", $self->errorNo) ;
240 # or return $self->saveErrorString(G_ERR, "Error Scanning: $status")
242 # $status = $self->zap($end_offset)
243 # or return $self->saveErrorString(G_ERR, "Error Zapping: $status");
244 # #or return $self->saveErrorString(undef, "Error Zapping: $$error_ref", $self->errorNo) ;
246 # #(*$obj->{Deflate}, $status) = $inf->createDeflate();
248 ## *$obj->{Header} = *$inf->{Info}{Header};
249 ## *$obj->{UnCompSize_32bit} =
250 ## *$obj->{BytesWritten} = *$inf->{UnCompSize_32bit} ;
251 ## *$obj->{CompSize_32bit} = *$inf->{CompSize_32bit} ;
254 ## if ( $outType eq 'buffer')
255 ## { substr( ${ *$self->{Buffer} }, $end_offset) = '' }
256 ## elsif ($outType eq 'handle' || $outType eq 'filename') {
257 ## *$self->{FH} = *$inf->{FH} ;
258 ## delete *$inf->{FH};
259 ## *$obj->{FH}->flush() ;
260 ## *$obj->{Handle} = 1 if $outType eq 'handle';
262 ## #seek(*$obj->{FH}, $end_offset, SEEK_SET)
263 ## *$obj->{FH}->seek($end_offset, SEEK_SET)
264 ## or return $obj->saveErrorString(undef, $!, $!) ;
273 return 1 if *$self->{Closed} ;
274 return 1 if !length *$self->{Pending} && *$self->{EndStream} ;
279 $len = $self->_raw_read(\$buffer, 1)
280 while ! *$self->{EndStream} && $len >= 0 ;
282 #return $len if $len < 0 ? $len : 0 ;
283 return $len < 0 ? 0 : 1 ;
290 my $headerLength = *$self->{Info}{HeaderLength};
291 my $block_offset = $headerLength + *$self->{Uncomp}->getLastBlockOffset();
292 $_[0] = $headerLength + *$self->{Uncomp}->getEndOffset();
293 #printf "# End $_[0], headerlen $headerLength \n";;
294 #printf "# block_offset $block_offset %x\n", $block_offset;
296 ( $self->smartSeek($block_offset) &&
297 $self->smartRead(\$byte, 1) )
298 or return $self->saveErrorString(0, $!, $!);
300 #printf "#byte is %x\n", unpack('C*',$byte);
301 *$self->{Uncomp}->resetLastBlockByte($byte);
302 #printf "#to byte is %x\n", unpack('C*',$byte);
304 ( $self->smartSeek($block_offset) &&
305 $self->smartWrite($byte) )
306 or return $self->saveErrorString(0, $!, $!);
308 #$self->smartSeek($end_offset, 1);
316 my ($def, $status) = *$self->{Uncomp}->createDeflateStream(
318 -WindowBits => - MAX_WBITS,
319 -CRC32 => *$self->{Params}->value('CRC32'),
320 -ADLER32 => *$self->{Params}->value('ADLER32'),
323 return wantarray ? ($status, $def) : $def ;
336 IO::Uncompress::RawInflate - Read RFC 1951 files/buffers
342 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
344 my $status = rawinflate $input => $output [,OPTS]
345 or die "rawinflate failed: $RawInflateError\n";
347 my $z = new IO::Uncompress::RawInflate $input [OPTS]
348 or die "rawinflate failed: $RawInflateError\n";
350 $status = $z->read($buffer)
351 $status = $z->read($buffer, $length)
352 $status = $z->read($buffer, $length, $offset)
353 $line = $z->getline()
358 $status = $z->inflateSync()
360 $data = $z->trailingData()
361 $status = $z->nextStream()
362 $data = $z->getHeaderInfo()
364 $z->seek($position, $whence)
376 read($z, $buffer, $length);
377 read($z, $buffer, $length, $offset);
379 seek($z, $position, $whence)
390 B<WARNING -- This is a Beta release>.
394 =item * DO NOT use in production code.
396 =item * The documentation is incomplete in places.
398 =item * Parts of the interface defined here are tentative.
400 =item * Please report any problems you find.
408 This module provides a Perl interface that allows the reading of
409 files/buffers that conform to RFC 1951.
411 For writing RFC 1951 files/buffers, see the companion module IO::Compress::RawDeflate.
418 =head1 Functional Interface
420 A top-level function, C<rawinflate>, is provided to carry out
421 "one-shot" uncompression between buffers and/or files. For finer
422 control over the uncompression process, see the L</"OO Interface">
425 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
427 rawinflate $input => $output [,OPTS]
428 or die "rawinflate failed: $RawInflateError\n";
432 The functional interface needs Perl5.005 or better.
435 =head2 rawinflate $input => $output [, OPTS]
438 C<rawinflate> expects at least two parameters, C<$input> and C<$output>.
440 =head3 The C<$input> parameter
442 The parameter, C<$input>, is used to define the source of
445 It can take one of the following forms:
451 If the C<$input> parameter is a simple scalar, it is assumed to be a
452 filename. This file will be opened for reading and the input data
453 will be read from it.
457 If the C<$input> parameter is a filehandle, the input data will be
459 The string '-' can be used as an alias for standard input.
461 =item A scalar reference
463 If C<$input> is a scalar reference, the input data will be read
466 =item An array reference
468 If C<$input> is an array reference, each element in the array must be a
471 The input data will be read from each file in turn.
473 The complete array will be walked to ensure that it only
474 contains valid filenames before any data is uncompressed.
478 =item An Input FileGlob string
480 If C<$input> is a string that is delimited by the characters "<" and ">"
481 C<rawinflate> will assume that it is an I<input fileglob string>. The
482 input is the list of files that match the fileglob.
484 If the fileglob does not match any files ...
486 See L<File::GlobMapper|File::GlobMapper> for more details.
491 If the C<$input> parameter is any other type, C<undef> will be returned.
495 =head3 The C<$output> parameter
497 The parameter C<$output> is used to control the destination of the
498 uncompressed data. This parameter can take one of these forms.
504 If the C<$output> parameter is a simple scalar, it is assumed to be a
505 filename. This file will be opened for writing and the uncompressed
506 data will be written to it.
510 If the C<$output> parameter is a filehandle, the uncompressed data
511 will be written to it.
512 The string '-' can be used as an alias for standard output.
515 =item A scalar reference
517 If C<$output> is a scalar reference, the uncompressed data will be
518 stored in C<$$output>.
522 =item An Array Reference
524 If C<$output> is an array reference, the uncompressed data will be
525 pushed onto the array.
527 =item An Output FileGlob
529 If C<$output> is a string that is delimited by the characters "<" and ">"
530 C<rawinflate> will assume that it is an I<output fileglob string>. The
531 output is the list of files that match the fileglob.
533 When C<$output> is an fileglob string, C<$input> must also be a fileglob
534 string. Anything else is an error.
538 If the C<$output> parameter is any other type, C<undef> will be returned.
545 When C<$input> maps to multiple compressed files/buffers and C<$output> is
546 a single file/buffer, after uncompression C<$output> will contain a
547 concatenation of all the uncompressed data from each of the input
554 =head2 Optional Parameters
556 Unless specified below, the optional parameters for C<rawinflate>,
557 C<OPTS>, are the same as those used with the OO interface defined in the
558 L</"Constructor Options"> section below.
562 =item C<< AutoClose => 0|1 >>
564 This option applies to any input or output data streams to
565 C<rawinflate> that are filehandles.
567 If C<AutoClose> is specified, and the value is true, it will result in all
568 input and/or output filehandles being closed once C<rawinflate> has
571 This parameter defaults to 0.
574 =item C<< BinModeOut => 0|1 >>
576 When writing to a file or filehandle, set C<binmode> before writing to the
585 =item C<< Append => 0|1 >>
589 =item C<< MultiStream => 0|1 >>
591 If the input file/buffer contains multiple compressed data streams, this
592 option will uncompress the whole lot as a single data stream.
605 To read the contents of the file C<file1.txt.1951> and write the
606 compressed data to the file C<file1.txt>.
610 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
612 my $input = "file1.txt.1951";
613 my $output = "file1.txt";
614 rawinflate $input => $output
615 or die "rawinflate failed: $RawInflateError\n";
618 To read from an existing Perl filehandle, C<$input>, and write the
619 uncompressed data to a buffer, C<$buffer>.
623 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
626 my $input = new IO::File "<file1.txt.1951"
627 or die "Cannot open 'file1.txt.1951': $!\n" ;
629 rawinflate $input => \$buffer
630 or die "rawinflate failed: $RawInflateError\n";
632 To uncompress all files in the directory "/my/home" that match "*.txt.1951" and store the compressed data in the same directory
636 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
638 rawinflate '</my/home/*.txt.1951>' => '</my/home/#1.txt>'
639 or die "rawinflate failed: $RawInflateError\n";
641 and if you want to compress each file one at a time, this will do the trick
645 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
647 for my $input ( glob "/my/home/*.txt.1951" )
650 $output =~ s/.1951// ;
651 rawinflate $input => $output
652 or die "Error compressing '$input': $RawInflateError\n";
659 The format of the constructor for IO::Uncompress::RawInflate is shown below
662 my $z = new IO::Uncompress::RawInflate $input [OPTS]
663 or die "IO::Uncompress::RawInflate failed: $RawInflateError\n";
665 Returns an C<IO::Uncompress::RawInflate> object on success and undef on failure.
666 The variable C<$RawInflateError> will contain an error message on failure.
668 If you are running Perl 5.005 or better the object, C<$z>, returned from
669 IO::Uncompress::RawInflate can be used exactly like an L<IO::File|IO::File> filehandle.
670 This means that all normal input file operations can be carried out with
671 C<$z>. For example, to read a line from a compressed file/buffer you can
672 use either of these forms
674 $line = $z->getline();
677 The mandatory parameter C<$input> is used to determine the source of the
678 compressed data. This parameter can take one of three forms.
684 If the C<$input> parameter is a scalar, it is assumed to be a filename. This
685 file will be opened for reading and the compressed data will be read from it.
689 If the C<$input> parameter is a filehandle, the compressed data will be
691 The string '-' can be used as an alias for standard input.
694 =item A scalar reference
696 If C<$input> is a scalar reference, the compressed data will be read from
701 =head2 Constructor Options
704 The option names defined below are case insensitive and can be optionally
705 prefixed by a '-'. So all of the following are valid
712 OPTS is a combination of the following options:
716 =item C<< AutoClose => 0|1 >>
718 This option is only valid when the C<$input> parameter is a filehandle. If
719 specified, and the value is true, it will result in the file being closed once
720 either the C<close> method is called or the IO::Uncompress::RawInflate object is
723 This parameter defaults to 0.
725 =item C<< MultiStream => 0|1 >>
729 This option is a no-op.
733 =item C<< Prime => $string >>
735 This option will uncompress the contents of C<$string> before processing the
738 This option can be useful when the compressed data is embedded in another
739 file/data structure and it is not possible to work out where the compressed
740 data begins without having to read the first few bytes. If this is the
741 case, the uncompression can be I<primed> with these bytes using this
744 =item C<< Transparent => 0|1 >>
746 If this option is set and the input file or buffer is not compressed data,
747 the module will allow reading of it anyway.
749 This option defaults to 1.
751 =item C<< BlockSize => $num >>
753 When reading the compressed input data, IO::Uncompress::RawInflate will read it in
754 blocks of C<$num> bytes.
756 This option defaults to 4096.
758 =item C<< InputLength => $size >>
760 When present this option will limit the number of compressed bytes read
761 from the input file/buffer to C<$size>. This option can be used in the
762 situation where there is useful data directly after the compressed data
763 stream and you know beforehand the exact length of the compressed data
766 This option is mostly used when reading from a filehandle, in which case
767 the file pointer will be left pointing to the first byte directly after the
768 compressed data stream.
772 This option defaults to off.
774 =item C<< Append => 0|1 >>
776 This option controls what the C<read> method does with uncompressed data.
778 If set to 1, all uncompressed data will be appended to the output parameter
779 of the C<read> method.
781 If set to 0, the contents of the output parameter of the C<read> method
782 will be overwritten by the uncompressed data.
786 =item C<< Strict => 0|1 >>
790 This option is a no-op.
809 $status = $z->read($buffer)
811 Reads a block of compressed data (the size the the compressed block is
812 determined by the C<Buffer> option in the constructor), uncompresses it and
813 writes any uncompressed data into C<$buffer>. If the C<Append> parameter is
814 set in the constructor, the uncompressed data will be appended to the
815 C<$buffer> parameter. Otherwise C<$buffer> will be overwritten.
817 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
818 or a negative number on error.
824 $status = $z->read($buffer, $length)
825 $status = $z->read($buffer, $length, $offset)
827 $status = read($z, $buffer, $length)
828 $status = read($z, $buffer, $length, $offset)
830 Attempt to read C<$length> bytes of uncompressed data into C<$buffer>.
832 The main difference between this form of the C<read> method and the
833 previous one, is that this one will attempt to return I<exactly> C<$length>
834 bytes. The only circumstances that this function will not is if end-of-file
835 or an IO error is encountered.
837 Returns the number of uncompressed bytes written to C<$buffer>, zero if eof
838 or a negative number on error.
845 $line = $z->getline()
850 This method fully supports the use of of the variable C<$/>
851 (or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
852 determine what constitutes an end of line. Both paragraph mode and file
853 slurp mode are supported.
862 Read a single character.
868 $char = $z->ungetc($string)
876 $status = $z->inflateSync()
885 $hdr = $z->getHeaderInfo();
886 @hdrs = $z->getHeaderInfo();
888 This method returns either a hash reference (in scalar context) or a list
889 or hash references (in array context) that contains information about each
890 of the header fields in the compressed data stream(s).
902 Returns the uncompressed file offset.
913 Returns true if the end of the compressed input stream has been reached.
919 $z->seek($position, $whence);
920 seek($z, $position, $whence);
925 Provides a sub-set of the C<seek> functionality, with the restriction
926 that it is only legal to seek forward in the input file/buffer.
927 It is a fatal error to attempt to seek backward.
931 The C<$whence> parameter takes one the usual values, namely SEEK_SET,
932 SEEK_CUR or SEEK_END.
934 Returns 1 on success, 0 on failure.
943 This is a noop provided for completeness.
949 Returns true if the object currently refers to a opened file/buffer.
953 my $prev = $z->autoflush()
954 my $prev = $z->autoflush(EXPR)
956 If the C<$z> object is associated with a file or a filehandle, this method
957 returns the current autoflush setting for the underlying filehandle. If
958 C<EXPR> is present, and is non-zero, it will enable flushing after every
959 write/print operation.
961 If C<$z> is associated with a buffer, this method has no effect and always
964 B<Note> that the special variable C<$|> B<cannot> be used to set or
965 retrieve the autoflush setting.
967 =head2 input_line_number
969 $z->input_line_number()
970 $z->input_line_number(EXPR)
974 Returns the current uncompressed line number. If C<EXPR> is present it has
975 the effect of setting the line number. Note that setting the line number
976 does not change the current position within the file/buffer being read.
978 The contents of C<$/> are used to to determine what constitutes a line
988 If the C<$z> object is associated with a file or a filehandle, this method
989 will return the underlying file descriptor.
991 If the C<$z> object is is associated with a buffer, this method will
1001 Closes the output file/buffer.
1005 For most versions of Perl this method will be automatically invoked if
1006 the IO::Uncompress::RawInflate object is destroyed (either explicitly or by the
1007 variable with the reference to the object going out of scope). The
1008 exceptions are Perl versions 5.005 through 5.00504 and 5.8.0. In
1009 these cases, the C<close> method will be called automatically, but
1010 not until global destruction of all live objects when the program is
1013 Therefore, if you want your scripts to be able to run on all versions
1014 of Perl, you should call C<close> explicitly and not rely on automatic
1017 Returns true on success, otherwise 0.
1019 If the C<AutoClose> option has been enabled when the IO::Uncompress::RawInflate
1020 object was created, and the object is associated with a file, the
1021 underlying file will also be closed.
1030 my $status = $z->nextStream();
1032 Skips to the next compressed data stream in the input file/buffer. If a new
1033 compressed data stream is found, the eof marker will be cleared, C<$.> will
1036 Returns 1 if a new stream was found, 0 if none was found, and -1 if an
1037 error was encountered.
1043 my $data = $z->trailingData();
1045 Returns any data that
1049 No symbolic constants are required by this IO::Uncompress::RawInflate at present.
1055 Imports C<rawinflate> and C<$RawInflateError>.
1058 use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;
1069 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>
1071 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>
1073 L<File::GlobMapper|File::GlobMapper>, L<Archive::Zip|Archive::Zip>,
1074 L<Archive::Tar|Archive::Tar>,
1075 L<IO::Zlib|IO::Zlib>
1078 For RFC 1950, 1951 and 1952 see
1079 F<http://www.faqs.org/rfcs/rfc1950.html>,
1080 F<http://www.faqs.org/rfcs/rfc1951.html> and
1081 F<http://www.faqs.org/rfcs/rfc1952.html>
1083 The I<zlib> compression library was written by Jean-loup Gailly
1084 F<gzip@prep.ai.mit.edu> and Mark Adler F<madler@alumni.caltech.edu>.
1086 The primary site for the I<zlib> compression library is
1087 F<http://www.zlib.org>.
1089 The primary site for gzip is F<http://www.gzip.org>.
1096 This module was written by Paul Marquess, F<pmqs@cpan.org>.
1100 =head1 MODIFICATION HISTORY
1102 See the Changes file.
1104 =head1 COPYRIGHT AND LICENSE
1106 Copyright (c) 2005-2006 Paul Marquess. All rights reserved.
1108 This program is free software; you can redistribute it and/or
1109 modify it under the same terms as Perl itself.