IO::Compress modules
[p5sagit/p5-mst-13.2.git] / ext / Compress / IO / Zlib / lib / IO / Uncompress / Inflate.pm
index d3efef6..2ec0a49 100644 (file)
@@ -13,7 +13,7 @@ use IO::Uncompress::RawInflate ;
 require Exporter ;
 our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $InflateError);
 
-$VERSION = '2.000_11';
+$VERSION = '2.000_14';
 $InflateError = '';
 
 @ISA    = qw( Exporter IO::Uncompress::RawInflate );
@@ -66,8 +66,9 @@ sub ckMagic
                                         ZLIB_HEADER_SIZE . " bytes") 
         if length $magic != ZLIB_HEADER_SIZE;
 
-    return $self->HeaderError("CRC mismatch.")
-        if ! isZlibMagic($magic) ;
+    #return $self->HeaderError("CRC mismatch.")
+    return undef
+        if ! $self->isZlibMagic($magic) ;
                       
     *$self->{Type} = 'rfc1950';
     return $magic;
@@ -98,10 +99,31 @@ sub chkTrailer
 
 sub isZlibMagic
 {
+    my $self = shift;
     my $buffer = shift ;
-    return 0 if length $buffer < ZLIB_HEADER_SIZE ;
+
+    return 0 
+        if length $buffer < ZLIB_HEADER_SIZE ;
+
     my $hdr = unpack("n", $buffer) ;
-    return $hdr % 31 == 0 ;
+    #return 0 if $hdr % 31 != 0 ;
+    return $self->HeaderError("CRC mismatch.")
+        if $hdr % 31 != 0 ;
+
+    my ($CMF, $FLG) = unpack "C C", $buffer;
+    my $cm =    bits($CMF, ZLIB_CMF_CM_OFFSET,    ZLIB_CMF_CM_BITS) ;
+
+    # Only Deflate supported
+    return $self->HeaderError("Not Deflate (CM is $cm)") 
+        if $cm != ZLIB_CMF_CM_DEFLATED ;
+
+    # Max window value is 7 for Deflate.
+    my $cinfo = bits($CMF, ZLIB_CMF_CINFO_OFFSET, ZLIB_CMF_CINFO_BITS) ;
+    return $self->HeaderError("CINFO > " . ZLIB_CMF_CINFO_MAX . 
+                              " (CINFO is $cinfo)") 
+        if $cinfo > ZLIB_CMF_CINFO_MAX ;
+
+    return 1;    
 }
 
 sub bits
@@ -203,7 +225,8 @@ IO::Uncompress::Inflate - Read RFC 1950 files/buffers
 
     $status = $z->inflateSync()
 
-    $z->trailingData()
+    $data = $z->trailingData()
+    $status = $z->nextStream()
     $data = $z->getHeaderInfo()
     $z->tell()
     $z->seek($position, $whence)
@@ -259,7 +282,6 @@ For writing RFC 1950 files/buffers, see the companion module IO::Compress::Defla
 
 
 
-
 =head1 Functional Interface
 
 A top-level function, C<inflate>, is provided to carry out
@@ -386,9 +408,13 @@ If the C<$output> parameter is any other type, C<undef> will be returned.
 
 =head2 Notes
 
-When C<$input> maps to multiple files/buffers and C<$output> is a single
-file/buffer the uncompressed input files/buffers will all be stored
-in C<$output> as a single uncompressed stream.
+
+When C<$input> maps to multiple compressed files/buffers and C<$output> is
+a single file/buffer, after uncompression C<$output> will contain a
+concatenation of all the uncompressed data from each of the input
+files/buffers.
+
+
 
 
 
@@ -400,7 +426,7 @@ L</"Constructor Options"> section below.
 
 =over 5
 
-=item AutoClose =E<gt> 0|1
+=item C<< AutoClose => 0|1 >>
 
 This option applies to any input or output data streams to 
 C<inflate> that are filehandles.
@@ -412,8 +438,7 @@ completed.
 This parameter defaults to 0.
 
 
-
-=item BinModeOut =E<gt> 0|1
+=item C<< BinModeOut => 0|1 >>
 
 When writing to a file or filehandle, set C<binmode> before writing to the
 file.
@@ -424,15 +449,46 @@ Defaults to 0.
 
 
 
-=item -Append =E<gt> 0|1
+=item C<< Append => 0|1 >>
 
 TODO
 
-=item -MultiStream =E<gt> 0|1
+=item C<< MultiStream => 0|1 >>
+
+
+If the input file/buffer contains multiple compressed data streams, this
+option will uncompress the whole lot as a single data stream.
+
+Defaults to 0.
+
+
+
+
+
+=item C<< TrailingData => $scalar >>
+
+Returns the data, if any, that is present immediately after the compressed
+data stream once uncompression is complete. 
+
+This option can be used when there is useful information immediately
+following the compressed data stream, and you don't know the length of the
+compressed data stream.
+
+If the input is a buffer, C<trailingData> will return everything from the
+end of the compressed data stream to the end of the buffer.
+
+If the input is a filehandle, C<trailingData> will return the data that is
+left in the filehandle input buffer once the end of the compressed data
+stream has been reached. You can then use the filehandle to read the rest
+of the input file. 
+
+Don't bother using C<trailingData> if the input is a filename.
 
-Creates a new stream after each file.
 
-Defaults to 1.
+
+If you know the length of the compressed data stream before you start
+uncompressing, you can avoid having to use C<trailingData> by setting the
+C<InputLength> option.
 
 
 
@@ -554,7 +610,7 @@ OPTS is a combination of the following options:
 
 =over 5
 
-=item -AutoClose =E<gt> 0|1
+=item C<< AutoClose => 0|1 >>
 
 This option is only valid when the C<$input> parameter is a filehandle. If
 specified, and the value is true, it will result in the file being closed once
@@ -563,7 +619,7 @@ destroyed.
 
 This parameter defaults to 0.
 
-=item -MultiStream =E<gt> 0|1
+=item C<< MultiStream => 0|1 >>
 
 
 
@@ -576,8 +632,7 @@ start of another stream.
 This parameter defaults to 0.
 
 
-
-=item -Prime =E<gt> $string
+=item C<< Prime => $string >>
 
 This option will uncompress the contents of C<$string> before processing the
 input file/buffer.
@@ -588,21 +643,21 @@ data begins without having to read the first few bytes. If this is the
 case, the uncompression can be I<primed> with these bytes using this
 option.
 
-=item -Transparent =E<gt> 0|1
+=item C<< Transparent => 0|1 >>
 
 If this option is set and the input file or buffer is not compressed data,
 the module will allow reading of it anyway.
 
 This option defaults to 1.
 
-=item -BlockSize =E<gt> $num
+=item C<< BlockSize => $num >>
 
 When reading the compressed input data, IO::Uncompress::Inflate will read it in
 blocks of C<$num> bytes.
 
 This option defaults to 4096.
 
-=item -InputLength =E<gt> $size
+=item C<< InputLength => $size >>
 
 When present this option will limit the number of compressed bytes read
 from the input file/buffer to C<$size>. This option can be used in the
@@ -618,7 +673,7 @@ compressed data stream.
 
 This option defaults to off.
 
-=item -Append =E<gt> 0|1
+=item C<< Append => 0|1 >>
 
 This option controls what the C<read> method does with uncompressed data.
 
@@ -630,7 +685,7 @@ will be overwritten by the uncompressed data.
 
 Defaults to 0.
 
-=item -Strict =E<gt> 0|1
+=item C<< Strict => 0|1 >>
 
 
 
@@ -667,6 +722,7 @@ uncompressed data actually contained in the file.
 
 
 
+
 =back
 
 =head2 Examples
@@ -720,10 +776,10 @@ Usage is
 
 Reads a single line. 
 
-This method fully supports the use of of the variable C<$/>
-(or C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
-determine what constitutes an end of line. Both paragraph mode and file
-slurp mode are supported. 
+This method fully supports the use of of the variable C<$/> (or
+C<$INPUT_RECORD_SEPARATOR> or C<$RS> when C<English> is in use) to
+determine what constitutes an end of line. Paragraph mode, record mode and
+file slurp mode are all supported. 
 
 
 =head2 getc
@@ -896,6 +952,50 @@ underlying file will also be closed.
 
 
 
+=head2 nextStream
+
+Usage is
+
+    my $status = $z->nextStream();
+
+Skips to the next compressed data stream in the input file/buffer. If a new
+compressed data stream is found, the eof marker will be cleared and C<$.>
+will be reset to 0.
+
+Returns 1 if a new stream was found, 0 if none was found, and -1 if an
+error was encountered.
+
+=head2 trailingData
+
+Usage is
+
+    my $data = $z->trailingData();
+
+Returns the data, if any, that is present immediately after the compressed
+data stream once uncompression is complete. It only makes sense to call
+this method once the end of the compressed data stream has been
+encountered.
+
+This option can be used when there is useful information immediately
+following the compressed data stream, and you don't know the length of the
+compressed data stream.
+
+If the input is a buffer, C<trailingData> will return everything from the
+end of the compressed data stream to the end of the buffer.
+
+If the input is a filehandle, C<trailingData> will return the data that is
+left in the filehandle input buffer once the end of the compressed data
+stream has been reached. You can then use the filehandle to read the rest
+of the input file. 
+
+Don't bother using C<trailingData> if the input is a filename.
+
+
+
+If you know the length of the compressed data stream before you start
+uncompressing, you can avoid having to use C<trailingData> by setting the
+C<InputLength> option in the constructor.
+
 =head1 Importing 
 
 No symbolic constants are required by this IO::Uncompress::Inflate at present. 
@@ -918,7 +1018,7 @@ Same as doing this
 
 =head1 SEE ALSO
 
-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::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
+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::Lzop>, L<IO::Uncompress::UnLzop>, L<IO::Compress::Lzf>, L<IO::Uncompress::UnLzf>, L<IO::Uncompress::AnyInflate>, L<IO::Uncompress::AnyUncompress>
 
 L<Compress::Zlib::FAQ|Compress::Zlib::FAQ>