| 1 | package IO::Uncompress::UnLzip ; | ||
| 2 | |||
| 3 | 13 | ▶ | use strict ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 13 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 13 | ▶ | use bytes; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 13 | ▶ | use IO::Compress::Base::Common 2.225 qw(:Status createSelfTiedObject); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 13 | ▶ | use IO::Uncompress::Base 2.225 ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use IO::Uncompress::Adapter::UnLzip 2.225 ; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | |||
| 12 | require Exporter ; | ||
| 13 | our ($VERSION, @ISA, @EXPORT_OK, %EXPORT_TAGS, $UnLzipError); | ||
| 14 | |||
| 15 | $VERSION = '2.225'; | ||
| 16 | $UnLzipError = ''; | ||
| 17 | |||
| 18 | @ISA = qw( IO::Uncompress::Base Exporter ); | ||
| 19 | @EXPORT_OK = qw( $UnLzipError unlzip ) ; | ||
| 20 | #%EXPORT_TAGS = %IO::Uncompress::Base::EXPORT_TAGS ; | ||
| 21 | push @{ $EXPORT_TAGS{all} }, @EXPORT_OK ; | ||
| 22 | #Exporter::export_ok_tags('all'); | ||
| 23 | |||
| 24 | |||
| 25 | sub new | ||
| 26 | { | ||
| 27 | my $class = shift ; | ||
| 28 | my $obj = createSelfTiedObject($class, \$UnLzipError); | ||
| 29 | |||
| 30 | $obj->_create(undef, 0, @_); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub unlzip | ||
| 34 | { | ||
| 35 | my $obj = createSelfTiedObject(undef, \$UnLzipError); | ||
| 36 | return $obj->_inf(@_); | ||
| 37 | } | ||
| 38 | |||
| 39 | #our %PARAMS = ( | ||
| 40 | #'verbosity' => [IO::Compress::Base::Common::Parse_boolean, 0], | ||
| 41 | #'small' => [IO::Compress::Base::Common::Parse_boolean, 0], | ||
| 42 | # ); | ||
| 43 | |||
| 44 | sub getExtraParams | ||
| 45 | { | ||
| 46 | return (); | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | sub ckParams | ||
| 51 | { | ||
| 52 | my $self = shift ; | ||
| 53 | my $got = shift ; | ||
| 54 | |||
| 55 | return 1; | ||
| 56 | } | ||
| 57 | |||
| 58 | sub mkUncomp | ||
| 59 | { | ||
| 60 | my $self = shift ; | ||
| 61 | my $got = shift ; | ||
| 62 | |||
| 63 | my $magic = $self->ckMagic() | ||
| 64 | or return 0; | ||
| 65 | |||
| 66 | *$self->{Info} = $self->readHeader($magic) | ||
| 67 | or return undef ; | ||
| 68 | |||
| 69 | my $Small = $got->getValue('small'); | ||
| 70 | my $Verbosity = $got->getValue('verbosity'); | ||
| 71 | |||
| 72 | my ($obj, $errstr, $errno) = IO::Uncompress::Adapter::UnLzip::mkUncompObject( | ||
| 73 | *$self->{Info}{DictSize}); | ||
| 74 | |||
| 75 | return $self->saveErrorString(undef, $errstr, $errno) | ||
| 76 | if ! defined $obj; | ||
| 77 | |||
| 78 | *$self->{Uncomp} = $obj; | ||
| 79 | |||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | |||
| 84 | sub ckMagic | ||
| 85 | { | ||
| 86 | my $self = shift; | ||
| 87 | |||
| 88 | my $magic ; | ||
| 89 | $self->smartReadExact(\$magic, 4); | ||
| 90 | |||
| 91 | *$self->{HeaderPending} = $magic ; | ||
| 92 | |||
| 93 | return $self->HeaderError("Minimum header size is " . | ||
| 94 | 4 . " bytes") | ||
| 95 | if length $magic != 4 ; | ||
| 96 | |||
| 97 | return $self->HeaderError("Bad Magic") | ||
| 98 | if $magic ne 'LZIP' ; | ||
| 99 | |||
| 100 | *$self->{Type} = 'lzip'; | ||
| 101 | |||
| 102 | return $magic ; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | |||
| 107 | sub readHeader | ||
| 108 | { | ||
| 109 | my $self = shift; | ||
| 110 | my $magic = shift ; | ||
| 111 | my ($buffer) = '' ; | ||
| 112 | |||
| 113 | $self->smartReadExact(\$buffer, 2) | ||
| 114 | or return $self->HeaderError("Minimum header size is " . | ||
| 115 | 2 . " bytes") ; | ||
| 116 | |||
| 117 | my $keep = $magic . $buffer ; | ||
| 118 | *$self->{HeaderPending} = $keep ; | ||
| 119 | |||
| 120 | my ($VN, $DS) = unpack("C C", $buffer) ; | ||
| 121 | |||
| 122 | # Version should be 0 or 1 | ||
| 123 | return $self->HeaderError("lzip version not 0 or 1: $VN") | ||
| 124 | unless $VN == 0 or $VN == 1 ; | ||
| 125 | |||
| 126 | # Dictionary logic derived from libarchive archive_read_support_filter_xz.c | ||
| 127 | my $log2dic = $DS & 0x1f; | ||
| 128 | |||
| 129 | return $self->HeaderError("Bad Dictionary Value") | ||
| 130 | if $log2dic < 12 || $log2dic > 27 ; | ||
| 131 | |||
| 132 | |||
| 133 | my $dicsize = 1 << $log2dic; | ||
| 134 | |||
| 135 | $dicsize -= ($dicsize / 16) * ($DS >> 5) | ||
| 136 | if $log2dic > 12 ; | ||
| 137 | |||
| 138 | return { | ||
| 139 | 'Type' => 'lzip', | ||
| 140 | 'Version' => $VN, | ||
| 141 | 'FingerprintLength' => 4, | ||
| 142 | 'HeaderLength' => 6, | ||
| 143 | 'TrailerLength' => $VN ? 20 : 12, | ||
| 144 | 'Header' => $keep, | ||
| 145 | 'Version' => $VN, | ||
| 146 | 'DictSize' => $dicsize, | ||
| 147 | }; | ||
| 148 | } | ||
| 149 | |||
| 150 | sub chkTrailer | ||
| 151 | { | ||
| 152 | my $self = shift; | ||
| 153 | my $trailer = shift; | ||
| 154 | |||
| 155 | my $not_version_0 = *$self->{Info}{Version} != 0; | ||
| 156 | # Check CRC & ISIZE | ||
| 157 | my $CRC32 = unpack("V", $trailer) ; | ||
| 158 | my $uSize = U64::newUnpack_V64 substr($trailer, 4, 8); | ||
| 159 | my $mSize; | ||
| 160 | if ($not_version_0) | ||
| 161 | { | ||
| 162 | $mSize = U64::newUnpack_V64 substr($trailer, 12, 8); | ||
| 163 | *$self->{Info}{CompressedLength} = $mSize->get64bit(); | ||
| 164 | |||
| 165 | } | ||
| 166 | |||
| 167 | *$self->{Info}{CRC32} = $CRC32; | ||
| 168 | *$self->{Info}{UncompressedLength} = $uSize->get64bit(); | ||
| 169 | |||
| 170 | if (*$self->{Strict}) { | ||
| 171 | return $self->TrailerError("CRC mismatch") | ||
| 172 | if $CRC32 != *$self->{Uncomp}->crc32() ; | ||
| 173 | |||
| 174 | return $self->TrailerError("USIZE mismatch.") | ||
| 175 | if ! $uSize->equal(*$self->{UnCompSize}); | ||
| 176 | |||
| 177 | if ($not_version_0) | ||
| 178 | { | ||
| 179 | $mSize->subtract(6 + 20); # header & trailer | ||
| 180 | |||
| 181 | return $self->TrailerError("CSIZE mismatch.") | ||
| 182 | if ! $mSize->equal(*$self->{CompSize}); | ||
| 183 | } | ||
| 184 | } | ||
| 185 | |||
| 186 | return STATUS_OK; | ||
| 187 | } | ||
| 188 | |||
| 189 | |||
| 190 | 1 ; | ||
| 191 | |||
| 192 | __END__ | ||