⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gunzip.pm

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 PM
📖 第 1 页 / 共 2 页
字号:
package IO::Uncompress::Gunzip ;require 5.004 ;# for RFC1952use strict ;use warnings;use bytes;use IO::Uncompress::RawInflate 2.008 ;use Compress::Raw::Zlib 2.008 qw( crc32 ) ;use IO::Compress::Base::Common 2.008 qw(:Status createSelfTiedObject);use IO::Compress::Gzip::Constants 2.008 ;use IO::Compress::Zlib::Extra 2.008 ;require Exporter ;our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $GunzipError);@ISA = qw( Exporter IO::Uncompress::RawInflate );@EXPORT_OK = qw( $GunzipError gunzip );%EXPORT_TAGS = %IO::Uncompress::RawInflate::DEFLATE_CONSTANTS ;push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;Exporter::export_ok_tags('all');$GunzipError = '';$VERSION = '2.008';sub new{    my $class = shift ;    $GunzipError = '';    my $obj = createSelfTiedObject($class, \$GunzipError);    $obj->_create(undef, 0, @_);}sub gunzip{    my $obj = createSelfTiedObject(undef, \$GunzipError);    return $obj->_inf(@_) ;}sub getExtraParams{    use IO::Compress::Base::Common  2.008 qw(:Parse);    return ( 'ParseExtra' => [1, 1, Parse_boolean,  0] ) ;}sub ckParams{    my $self = shift ;    my $got = shift ;    # gunzip always needs crc32    $got->value('CRC32' => 1);    return 1;}sub ckMagic{    my $self = shift;    my $magic ;    $self->smartReadExact(\$magic, GZIP_ID_SIZE);    *$self->{HeaderPending} = $magic ;    return $self->HeaderError("Minimum header size is " .                               GZIP_MIN_HEADER_SIZE . " bytes")         if length $magic != GZIP_ID_SIZE ;                                        return $self->HeaderError("Bad Magic")        if ! isGzipMagic($magic) ;    *$self->{Type} = 'rfc1952';    return $magic ;}sub readHeader{    my $self = shift;    my $magic = shift;    return $self->_readGzipHeader($magic);}sub chkTrailer{    my $self = shift;    my $trailer = shift;    # Check CRC & ISIZE     my ($CRC32, $ISIZE) = unpack("V V", $trailer) ;    *$self->{Info}{CRC32} = $CRC32;        *$self->{Info}{ISIZE} = $ISIZE;        if (*$self->{Strict}) {        return $self->TrailerError("CRC mismatch")            if $CRC32 != *$self->{Uncomp}->crc32() ;        my $exp_isize = *$self->{UnCompSize}->get32bit();        return $self->TrailerError("ISIZE mismatch. Got $ISIZE"                                  . ", expected $exp_isize")            if $ISIZE != $exp_isize ;    }    return STATUS_OK;}sub isGzipMagic{    my $buffer = shift ;    return 0 if length $buffer < GZIP_ID_SIZE ;    my ($id1, $id2) = unpack("C C", $buffer) ;    return $id1 == GZIP_ID1 && $id2 == GZIP_ID2 ;}sub _readFullGzipHeader($){    my ($self) = @_ ;    my $magic = '' ;    $self->smartReadExact(\$magic, GZIP_ID_SIZE);    *$self->{HeaderPending} = $magic ;    return $self->HeaderError("Minimum header size is " .                               GZIP_MIN_HEADER_SIZE . " bytes")         if length $magic != GZIP_ID_SIZE ;                                        return $self->HeaderError("Bad Magic")        if ! isGzipMagic($magic) ;    my $status = $self->_readGzipHeader($magic);    delete *$self->{Transparent} if ! defined $status ;    return $status ;}sub _readGzipHeader($){    my ($self, $magic) = @_ ;    my ($HeaderCRC) ;    my ($buffer) = '' ;    $self->smartReadExact(\$buffer, GZIP_MIN_HEADER_SIZE - GZIP_ID_SIZE)        or return $self->HeaderError("Minimum header size is " .                                      GZIP_MIN_HEADER_SIZE . " bytes") ;    my $keep = $magic . $buffer ;    *$self->{HeaderPending} = $keep ;    # now split out the various parts    my ($cm, $flag, $mtime, $xfl, $os) = unpack("C C V C C", $buffer) ;    $cm == GZIP_CM_DEFLATED         or return $self->HeaderError("Not Deflate (CM is $cm)") ;    # check for use of reserved bits    return $self->HeaderError("Use of Reserved Bits in FLG field.")        if $flag & GZIP_FLG_RESERVED ;     my $EXTRA ;    my @EXTRA = () ;    if ($flag & GZIP_FLG_FEXTRA) {        $EXTRA = "" ;        $self->smartReadExact(\$buffer, GZIP_FEXTRA_HEADER_SIZE)             or return $self->TruncatedHeader("FEXTRA Length") ;        my ($XLEN) = unpack("v", $buffer) ;        $self->smartReadExact(\$EXTRA, $XLEN)             or return $self->TruncatedHeader("FEXTRA Body");        $keep .= $buffer . $EXTRA ;        if ($XLEN && *$self->{'ParseExtra'}) {            my $bad = IO::Compress::Zlib::Extra::parseRawExtra($EXTRA,                                                \@EXTRA, 1, 1);            return $self->HeaderError($bad)                if defined $bad;        }    }    my $origname ;    if ($flag & GZIP_FLG_FNAME) {        $origname = "" ;        while (1) {            $self->smartReadExact(\$buffer, 1)                 or return $self->TruncatedHeader("FNAME");            last if $buffer eq GZIP_NULL_BYTE ;            $origname .= $buffer         }        $keep .= $origname . GZIP_NULL_BYTE ;        return $self->HeaderError("Non ISO 8859-1 Character found in Name")            if *$self->{Strict} && $origname =~ /$GZIP_FNAME_INVALID_CHAR_RE/o ;    }    my $comment ;    if ($flag & GZIP_FLG_FCOMMENT) {        $comment = "";        while (1) {            $self->smartReadExact(\$buffer, 1)                 or return $self->TruncatedHeader("FCOMMENT");            last if $buffer eq GZIP_NULL_BYTE ;            $comment .= $buffer         }        $keep .= $comment . GZIP_NULL_BYTE ;        return $self->HeaderError("Non ISO 8859-1 Character found in Comment")            if *$self->{Strict} && $comment =~ /$GZIP_FCOMMENT_INVALID_CHAR_RE/o ;    }    if ($flag & GZIP_FLG_FHCRC) {        $self->smartReadExact(\$buffer, GZIP_FHCRC_SIZE)             or return $self->TruncatedHeader("FHCRC");        $HeaderCRC = unpack("v", $buffer) ;        my $crc16 = crc32($keep) & 0xFF ;        return $self->HeaderError("CRC16 mismatch.")            if *$self->{Strict} && $crc16 != $HeaderCRC;        $keep .= $buffer ;    }    # Assume compression method is deflated for xfl tests    #if ($xfl) {    #}    *$self->{Type} = 'rfc1952';    return {        'Type'          => 'rfc1952',        'FingerprintLength'  => 2,        'HeaderLength'  => length $keep,        'TrailerLength' => GZIP_TRAILER_SIZE,        'Header'        => $keep,        'isMinimalHeader' => $keep eq GZIP_MINIMUM_HEADER ? 1 : 0,        'MethodID'      => $cm,        'MethodName'    => $cm == GZIP_CM_DEFLATED ? "Deflated" : "Unknown" ,        'TextFlag'      => $flag & GZIP_FLG_FTEXT ? 1 : 0,        'HeaderCRCFlag' => $flag & GZIP_FLG_FHCRC ? 1 : 0,        'NameFlag'      => $flag & GZIP_FLG_FNAME ? 1 : 0,        'CommentFlag'   => $flag & GZIP_FLG_FCOMMENT ? 1 : 0,        'ExtraFlag'     => $flag & GZIP_FLG_FEXTRA ? 1 : 0,        'Name'          => $origname,        'Comment'       => $comment,        'Time'          => $mtime,        'OsID'          => $os,        'OsName'        => defined $GZIP_OS_Names{$os}                                  ? $GZIP_OS_Names{$os} : "Unknown",        'HeaderCRC'     => $HeaderCRC,        'Flags'         => $flag,        'ExtraFlags'    => $xfl,        'ExtraFieldRaw' => $EXTRA,        'ExtraField'    => [ @EXTRA ],        #'CompSize'=> $compsize,        #'CRC32'=> $CRC32,        #'OrigSize'=> $ISIZE,      }}1;__END__=head1 NAMEIO::Uncompress::Gunzip - Read RFC 1952 files/buffers=head1 SYNOPSIS    use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;    my $status = gunzip $input => $output [,OPTS]        or die "gunzip failed: $GunzipError\n";    my $z = new IO::Uncompress::Gunzip $input [OPTS]         or die "gunzip failed: $GunzipError\n";    $status = $z->read($buffer)    $status = $z->read($buffer, $length)    $status = $z->read($buffer, $length, $offset)    $line = $z->getline()    $char = $z->getc()    $char = $z->ungetc()    $char = $z->opened()    $status = $z->inflateSync()    $data = $z->trailingData()    $status = $z->nextStream()    $data = $z->getHeaderInfo()    $z->tell()    $z->seek($position, $whence)    $z->binmode()    $z->fileno()    $z->eof()    $z->close()    $GunzipError ;    # IO::File mode    <$z>    read($z, $buffer);    read($z, $buffer, $length);    read($z, $buffer, $length, $offset);    tell($z)    seek($z, $position, $whence)    binmode($z)    fileno($z)    eof($z)    close($z)=head1 DESCRIPTIONThis module provides a Perl interface that allows the reading offiles/buffers that conform to RFC 1952.For writing RFC 1952 files/buffers, see the companion module IO::Compress::Gzip.=head1 Functional InterfaceA top-level function, C<gunzip>, is provided to carry out"one-shot" uncompression between buffers and/or files. For finercontrol over the uncompression process, see the L</"OO Interface">section.    use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;    gunzip $input => $output [,OPTS]         or die "gunzip failed: $GunzipError\n";The functional interface needs Perl5.005 or better.=head2 gunzip $input => $output [, OPTS]C<gunzip> expects at least two parameters, C<$input> and C<$output>.=head3 The C<$input> parameterThe parameter, C<$input>, is used to define the source ofthe compressed data. It can take one of the following forms:=over 5=item A filenameIf the C<$input> parameter is a simple scalar, it is assumed to be afilename. This file will be opened for reading and the input datawill be read from it.=item A filehandleIf the C<$input> parameter is a filehandle, the input data will beread from it.The string '-' can be used as an alias for standard input.=item A scalar reference If C<$input> is a scalar reference, the input data will be readfrom C<$$input>.=item An array reference If C<$input> is an array reference, each element in the array must be afilename.The input data will be read from each file in turn. The complete array will be walked to ensure that it onlycontains valid filenames before any data is uncompressed.=item An Input FileGlob stringIf C<$input> is a string that is delimited by the characters "<" and ">"C<gunzip> will assume that it is an I<input fileglob string>. Theinput is the list of files that match the fileglob.If the fileglob does not match any files ...See L<File::GlobMapper|File::GlobMapper> for more details.=backIf the C<$input> parameter is any other type, C<undef> will be returned.=head3 The C<$output> parameterThe parameter C<$output> is used to control the destination of theuncompressed data. This parameter can take one of these forms.=over 5=item A filenameIf the C<$output> parameter is a simple scalar, it is assumed to be afilename.  This file will be opened for writing and the uncompresseddata will be written to it.=item A filehandleIf the C<$output> parameter is a filehandle, the uncompressed datawill be written to it.The string '-' can be used as an alias for standard output.=item A scalar reference If C<$output> is a scalar reference, the uncompressed data will bestored in C<$$output>.=item An Array ReferenceIf C<$output> is an array reference, the uncompressed data will bepushed onto the array.=item An Output FileGlobIf C<$output> is a string that is delimited by the characters "<" and ">"C<gunzip> will assume that it is an I<output fileglob string>. Theoutput is the list of files that match the fileglob.When C<$output> is an fileglob string, C<$input> must also be a fileglobstring. Anything else is an error.=backIf the C<$output> parameter is any other type, C<undef> will be returned.=head2 NotesWhen C<$input> maps to multiple compressed files/buffers and C<$output> isa single file/buffer, after uncompression C<$output> will contain aconcatenation of all the uncompressed data from each of the inputfiles/buffers.=head2 Optional ParametersUnless specified below, the optional parameters for C<gunzip>,C<OPTS>, are the same as those used with the OO interface defined in theL</"Constructor Options"> section below.=over 5=item C<< AutoClose => 0|1 >>This option applies to any input or output data streams to C<gunzip> that are filehandles.If C<AutoClose> is specified, and the value is true, it will result in allinput and/or output filehandles being closed once C<gunzip> hascompleted.This parameter defaults to 0.=item C<< BinModeOut => 0|1 >>When writing to a file or filehandle, set C<binmode> before writing to thefile.Defaults to 0.=item C<< Append => 0|1 >>TODO=item C<< MultiStream => 0|1 >>If the input file/buffer contains multiple compressed data streams, thisoption 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 compresseddata stream once uncompression is complete. This option can be used when there is useful information immediatelyfollowing the compressed data stream, and you don't know the length of thecompressed data stream.If the input is a buffer, C<trailingData> will return everything from theend of the compressed data stream to the end of the buffer.If the input is a filehandle, C<trailingData> will return the data that isleft in the filehandle input buffer once the end of the compressed datastream has been reached. You can then use the filehandle to read the restof 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 startuncompressing, you can avoid having to use C<trailingData> by setting theC<InputLength> option.=back=head2 ExamplesTo read the contents of the file C<file1.txt.gz> and write thecompressed data to the file C<file1.txt>.    use strict ;    use warnings ;    use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;    my $input = "file1.txt.gz";    my $output = "file1.txt";    gunzip $input => $output        or die "gunzip failed: $GunzipError\n";To read from an existing Perl filehandle, C<$input>, and write theuncompressed data to a buffer, C<$buffer>.    use strict ;    use warnings ;    use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;    use IO::File ;    my $input = new IO::File "<file1.txt.gz"        or die "Cannot open 'file1.txt.gz': $!\n" ;    my $buffer ;    gunzip $input => \$buffer         or die "gunzip failed: $GunzipError\n";To uncompress all files in the directory "/my/home" that match "*.txt.gz" and store the compressed data in the same directory

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -