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

📄 rawinflate.pm

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 PM
📖 第 1 页 / 共 2 页
字号:
package IO::Uncompress::RawInflate ;# for RFC1951use strict ;use warnings;use bytes;use Compress::Raw::Zlib  2.008 ;use IO::Compress::Base::Common  2.008 qw(:Status createSelfTiedObject);use IO::Uncompress::Base  2.008 ;use IO::Uncompress::Adapter::Inflate  2.008 ;require Exporter ;our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, %DEFLATE_CONSTANTS, $RawInflateError);$VERSION = '2.008';$RawInflateError = '';@ISA    = qw( Exporter IO::Uncompress::Base );@EXPORT_OK = qw( $RawInflateError rawinflate ) ;%DEFLATE_CONSTANTS = ();%EXPORT_TAGS = %IO::Uncompress::Base::EXPORT_TAGS ;push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ;Exporter::export_ok_tags('all');sub new{    my $class = shift ;    my $obj = createSelfTiedObject($class, \$RawInflateError);    $obj->_create(undef, 0, @_);}sub rawinflate{    my $obj = createSelfTiedObject(undef, \$RawInflateError);    return $obj->_inf(@_);}sub getExtraParams{    return ();}sub ckParams{    my $self = shift ;    my $got = shift ;    return 1;}sub mkUncomp{    my $self = shift ;    my $class = shift ;    my $got = shift ;    my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::Inflate::mkUncompObject(                                                                $got->value('CRC32'),                                                                $got->value('ADLER32'),                                                                $got->value('Scan'),                                                            );    return $self->saveErrorString(undef, $errstr, $errno)        if ! defined $obj;    *$self->{Uncomp} = $obj;     my $magic = $self->ckMagic()        or return 0;    *$self->{Info} = $self->readHeader($magic)        or return undef ;    return 1;}sub ckMagic{    my $self = shift;    return $self->_isRaw() ;}sub readHeader{    my $self = shift;    my $magic = shift ;    return {        'Type'          => 'rfc1951',        'FingerprintLength'  => 0,        'HeaderLength'  => 0,        'TrailerLength' => 0,        'Header'        => ''        };}sub chkTrailer{    return STATUS_OK ;}sub _isRaw{    my $self   = shift ;    my $got = $self->_isRawx(@_);    if ($got) {        *$self->{Pending} = *$self->{HeaderPending} ;    }    else {        $self->pushBack(*$self->{HeaderPending});        *$self->{Uncomp}->reset();    }    *$self->{HeaderPending} = '';    return $got ;}sub _isRawx{    my $self   = shift ;    my $magic = shift ;    $magic = '' unless defined $magic ;    my $buffer = '';    $self->smartRead(\$buffer, *$self->{BlockSize}) >= 0          or return $self->saveErrorString(undef, "No data to read");    my $temp_buf = $magic . $buffer ;    *$self->{HeaderPending} = $temp_buf ;        $buffer = '';    my $status = *$self->{Uncomp}->uncompr(\$temp_buf, \$buffer, $self->smartEof()) ;    return $self->saveErrorString(undef, *$self->{Uncomp}{Error}, STATUS_ERROR)        if $status == STATUS_ERROR;    #my $buf_len = *$self->{Uncomp}->uncompressedBytes();    my $buf_len = length $buffer;    if ($status == STATUS_ENDSTREAM) {        if (*$self->{MultiStream}                     && (length $temp_buf || ! $self->smartEof())){            *$self->{NewStream} = 1 ;            *$self->{EndStream} = 0 ;            $self->pushBack($temp_buf);        }        else {            *$self->{EndStream} = 1 ;            $self->pushBack($temp_buf);        }    }    *$self->{HeaderPending} = $buffer ;        *$self->{InflatedBytesRead} = $buf_len ;        *$self->{TotalInflatedBytesRead} += $buf_len ;        *$self->{Type} = 'rfc1951';    $self->saveStatus(STATUS_OK);    return {        'Type'          => 'rfc1951',        'HeaderLength'  => 0,        'TrailerLength' => 0,        'Header'        => ''        };}sub inflateSync{    my $self = shift ;    # inflateSync is a no-op in Plain mode    return 1        if *$self->{Plain} ;    return 0 if *$self->{Closed} ;    #return G_EOF if !length *$self->{Pending} && *$self->{EndStream} ;    return 0 if ! length *$self->{Pending} && *$self->{EndStream} ;    # Disable CRC check    *$self->{Strict} = 0 ;    my $status ;    while (1)    {        my $temp_buf ;        if (length *$self->{Pending} )        {            $temp_buf = *$self->{Pending} ;            *$self->{Pending} = '';        }        else        {            $status = $self->smartRead(\$temp_buf, *$self->{BlockSize}) ;            return $self->saveErrorString(0, "Error Reading Data")                if $status < 0  ;            if ($status == 0 ) {                *$self->{EndStream} = 1 ;                return $self->saveErrorString(0, "unexpected end of file", STATUS_ERROR);            }        }                $status = *$self->{Uncomp}->sync($temp_buf) ;        if ($status == STATUS_OK)        {            *$self->{Pending} .= $temp_buf ;            return 1 ;        }        last unless $status == STATUS_ERROR ;    }    return 0;}#sub performScan#{#    my $self = shift ;##    my $status ;#    my $end_offset = 0;##    $status = $self->scan() #    #or return $self->saveErrorString(undef, "Error Scanning: $$error_ref", $self->errorNo) ;#        or return $self->saveErrorString(G_ERR, "Error Scanning: $status")##    $status = $self->zap($end_offset) #        or return $self->saveErrorString(G_ERR, "Error Zapping: $status");#    #or return $self->saveErrorString(undef, "Error Zapping: $$error_ref", $self->errorNo) ;##    #(*$obj->{Deflate}, $status) = $inf->createDeflate();###    *$obj->{Header} = *$inf->{Info}{Header};##    *$obj->{UnCompSize_32bit} = ##        *$obj->{BytesWritten} = *$inf->{UnCompSize_32bit} ;##    *$obj->{CompSize_32bit} = *$inf->{CompSize_32bit} ;####    if ( $outType eq 'buffer') ##      { substr( ${ *$self->{Buffer} }, $end_offset) = '' }##    elsif ($outType eq 'handle' || $outType eq 'filename') {##        *$self->{FH} = *$inf->{FH} ;##        delete *$inf->{FH};##        *$obj->{FH}->flush() ;##        *$obj->{Handle} = 1 if $outType eq 'handle';####        #seek(*$obj->{FH}, $end_offset, SEEK_SET) ##        *$obj->{FH}->seek($end_offset, SEEK_SET) ##            or return $obj->saveErrorString(undef, $!, $!) ;##    }#    #}sub scan{    my $self = shift ;    return 1 if *$self->{Closed} ;    return 1 if !length *$self->{Pending} && *$self->{EndStream} ;    my $buffer = '' ;    my $len = 0;    $len = $self->_raw_read(\$buffer, 1)         while ! *$self->{EndStream} && $len >= 0 ;    #return $len if $len < 0 ? $len : 0 ;    return $len < 0 ? 0 : 1 ;}sub zap{    my $self  = shift ;    my $headerLength = *$self->{Info}{HeaderLength};    my $block_offset =  $headerLength + *$self->{Uncomp}->getLastBlockOffset();    $_[0] = $headerLength + *$self->{Uncomp}->getEndOffset();    #printf "# End $_[0], headerlen $headerLength \n";;    #printf "# block_offset $block_offset %x\n", $block_offset;    my $byte ;    ( $self->smartSeek($block_offset) &&      $self->smartRead(\$byte, 1) )         or return $self->saveErrorString(0, $!, $!);     #printf "#byte is %x\n", unpack('C*',$byte);    *$self->{Uncomp}->resetLastBlockByte($byte);    #printf "#to byte is %x\n", unpack('C*',$byte);    ( $self->smartSeek($block_offset) &&       $self->smartWrite($byte) )        or return $self->saveErrorString(0, $!, $!);     #$self->smartSeek($end_offset, 1);    return 1 ;}sub createDeflate{    my $self  = shift ;    my ($def, $status) = *$self->{Uncomp}->createDeflateStream(                                    -AppendOutput   => 1,                                    -WindowBits => - MAX_WBITS,                                    -CRC32      => *$self->{Params}->value('CRC32'),                                    -ADLER32    => *$self->{Params}->value('ADLER32'),                                );        return wantarray ? ($status, $def) : $def ;                                }1; __END__=head1 NAMEIO::Uncompress::RawInflate - Read RFC 1951 files/buffers=head1 SYNOPSIS    use IO::Uncompress::RawInflate qw(rawinflate $RawInflateError) ;    my $status = rawinflate $input => $output [,OPTS]        or die "rawinflate failed: $RawInflateError\n";    my $z = new IO::Uncompress::RawInflate $input [OPTS]         or die "rawinflate failed: $RawInflateError\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()    $RawInflateError ;    # 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 1951.For writing RFC 1951 files/buffers, see the companion module IO::Compress::RawDeflate.=head1 Functional InterfaceA top-level function, C<rawinflate>, 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::RawInflate qw(rawinflate $RawInflateError) ;    rawinflate $input => $output [,OPTS]         or die "rawinflate failed: $RawInflateError\n";The functional interface needs Perl5.005 or better.=head2 rawinflate $input => $output [, OPTS]C<rawinflate> 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<rawinflate> 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<rawinflate> 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<rawinflate>,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<rawinflate> 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<rawinflate> 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 >>This option is a no-op.

⌨️ 快捷键说明

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