| 1 |
(* ***** BEGIN LICENSE BLOCK ***** |
| 2 |
* Version: MPL 1.1 |
| 3 |
* |
| 4 |
* The contents of this file are subject to the Mozilla Public License Version |
| 5 |
* 1.1 (the "License"); you may not use this file except in compliance with |
| 6 |
* the License. You may obtain a copy of the License at |
| 7 |
* http://www.mozilla.org/MPL/ |
| 8 |
* |
| 9 |
* Software distributed under the License is distributed on an "AS IS" basis, |
| 10 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 11 |
* for the specific language governing rights and limitations under the |
| 12 |
* License. |
| 13 |
* |
| 14 |
* The Original Code is TurboPower Abbrevia |
| 15 |
* |
| 16 |
* The Initial Developer of the Original Code is |
| 17 |
* TurboPower Software |
| 18 |
* |
| 19 |
* Portions created by the Initial Developer are Copyright (C) 1997-2002 |
| 20 |
* the Initial Developer. All Rights Reserved. |
| 21 |
* |
| 22 |
* Contributor(s): |
| 23 |
* Craig Peterson <[email protected]> |
| 24 |
* |
| 25 |
* ***** END LICENSE BLOCK ***** *) |
| 26 |
|
| 27 |
{*********************************************************} |
| 28 |
{* ABBREVIA: AbUnzPrc.pas *} |
| 29 |
{*********************************************************} |
| 30 |
{* ABBREVIA: UnZip procedures *} |
| 31 |
{*********************************************************} |
| 32 |
|
| 33 |
unit AbUnzPrc; |
| 34 |
|
| 35 |
{$I AbDefine.inc} |
| 36 |
|
| 37 |
interface |
| 38 |
|
| 39 |
uses |
| 40 |
Classes, |
| 41 |
AbArcTyp, |
| 42 |
AbZipTyp; |
| 43 |
|
| 44 |
type |
| 45 |
TAbUnzipHelper = class( TObject ) |
| 46 |
protected {private} |
| 47 |
{internal variables} |
| 48 |
FOutWriter : TStream; |
| 49 |
FOutStream : TStream; |
| 50 |
FUnCompressedSize : LongInt; |
| 51 |
FCompressionMethod : TAbZipCompressionMethod; |
| 52 |
FDictionarySize : TAbZipDictionarySize; |
| 53 |
FShannonFanoTreeCount : Byte; |
| 54 |
|
| 55 |
FOutBuf : PAbByteArray; {output buffer} |
| 56 |
FOutSent : LongInt; {number of bytes sent to output buffer} |
| 57 |
FOutPos : Cardinal; {current position in output buffer} |
| 58 |
FBitSValid : Byte; {Number of valid bits} |
| 59 |
|
| 60 |
FInBuf : TAbByteArray4K; |
| 61 |
FInPos : Integer; {current position in input buffer} |
| 62 |
FInCnt : Integer; {number of bytes in input buffer} |
| 63 |
FInEof : Boolean; {set when stream read returns 0} |
| 64 |
FCurByte : Byte; {current input byte} |
| 65 |
FBitsLeft : Byte; {bits left to process in FCurByte} |
| 66 |
|
| 67 |
FZStream : TStream; |
| 68 |
protected |
| 69 |
procedure uzFlushOutBuf; |
| 70 |
{-Flushes the output buffer} |
| 71 |
|
| 72 |
function uzReadBits(Bits : Byte) : Integer; |
| 73 |
{-Read the specified number of bits} |
| 74 |
procedure uzReadNextPrim; |
| 75 |
{-does less likely part of uzReadNext} |
| 76 |
|
| 77 |
{$IFDEF UnzipImplodeSupport} |
| 78 |
procedure uzUnImplode; |
| 79 |
{-Extract an imploded file} |
| 80 |
{$ENDIF} |
| 81 |
|
| 82 |
{$IFDEF UnzipReduceSupport} |
| 83 |
procedure uzUnReduce; |
| 84 |
{-Extract a reduced file} |
| 85 |
{$ENDIF} |
| 86 |
|
| 87 |
{$IFDEF UnzipShrinkSupport} |
| 88 |
procedure uzUnShrink; |
| 89 |
{-Extract a shrunk file} |
| 90 |
{$ENDIF} |
| 91 |
procedure uzWriteByte(B : Byte); |
| 92 |
{write to output} |
| 93 |
public |
| 94 |
constructor Create( InputStream, OutputStream : TStream ); |
| 95 |
destructor Destroy; |
| 96 |
override; |
| 97 |
|
| 98 |
procedure Execute; |
| 99 |
|
| 100 |
property UnCompressedSize : LongInt |
| 101 |
read FUncompressedSize |
| 102 |
write FUncompressedSize; |
| 103 |
property CompressionMethod : TAbZipCompressionMethod |
| 104 |
read FCompressionMethod |
| 105 |
write FCompressionMethod; |
| 106 |
property DictionarySize : TAbZipDictionarySize |
| 107 |
read FDictionarySize |
| 108 |
write FDictionarySize; |
| 109 |
property ShannonFanoTreeCount : Byte |
| 110 |
read FShannonFanoTreeCount |
| 111 |
write FShannonFanoTreeCount; |
| 112 |
end; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
procedure AbUnzipToStream( Sender : TObject; Item : TAbZipItem; |
| 117 |
OutStream : TStream); |
| 118 |
|
| 119 |
procedure AbUnzip(Sender : TObject; Item : TAbZipItem; const UseName : string); |
| 120 |
|
| 121 |
procedure AbTestZipItem(Sender : TObject; Item : TAbZipItem); |
| 122 |
|
| 123 |
procedure InflateStream(CompressedStream, UnCompressedStream : TStream); |
| 124 |
{-Inflates everything in CompressedStream to UncompressedStream |
| 125 |
no encryption is tried, no check on CRC is done, uses the whole |
| 126 |
compressedstream - no Progress events - no Frills!} |
| 127 |
|
| 128 |
implementation |
| 129 |
|
| 130 |
uses |
| 131 |
SysUtils, |
| 132 |
{$IFDEF UnzipBzip2Support} |
| 133 |
AbBzip2, |
| 134 |
{$ENDIF} |
| 135 |
{$IFDEF UnzipLzmaSupport} |
| 136 |
ULZMADecoder, |
| 137 |
{$ENDIF} |
| 138 |
{$IFDEF UnzipPPMdSupport} |
| 139 |
AbPPMd, |
| 140 |
{$ENDIF} |
| 141 |
{$IFDEF UnzipWavPackSupport} |
| 142 |
AbWavPack, |
| 143 |
{$ENDIF} |
| 144 |
{$IFDEF UnzipXzSupport} |
| 145 |
AbXz, |
| 146 |
{$ENDIF} |
| 147 |
{$IFDEF UnzipZstdSupport} |
| 148 |
AbZstd, |
| 149 |
{$ENDIF} |
| 150 |
AbBitBkt, |
| 151 |
AbConst, |
| 152 |
AbDfBase, |
| 153 |
AbDfCryS, |
| 154 |
AbExcept, |
| 155 |
AbSpanSt, |
| 156 |
AbSWStm, |
| 157 |
AbUnzOutStm, |
| 158 |
AbUtils, |
| 159 |
AbZlibPrc, |
| 160 |
AbWinZipAes, |
| 161 |
Inflate64Stream, |
| 162 |
DCOSUtils, |
| 163 |
DCStrUtils, |
| 164 |
DCClassesUtf8, |
| 165 |
DCConvertEncoding; |
| 166 |
|
| 167 |
{ -------------------------------------------------------------------------- } |
| 168 |
procedure AbReverseBits(var W : Word); |
| 169 |
{-Reverse the order of the bits in W} |
| 170 |
register; |
| 171 |
const |
| 172 |
RevTable : array[0..255] of Byte = ($00, $80, $40, $C0, $20, $A0, $60, |
| 173 |
$E0, $10, $90, $50, $D0, $30, $B0, $70, $F0, $08, $88, $48, $C8, $28, |
| 174 |
$A8, $68, $E8, $18, $98, $58, $D8, $38, $B8, $78, $F8, $04, $84, $44, |
| 175 |
$C4, $24, $A4, $64, $E4, $14, $94, $54, $D4, $34, $B4, $74, $F4, $0C, |
| 176 |
$8C, $4C, $CC, $2C, $AC, $6C, $EC, $1C, $9C, $5C, $DC, $3C, $BC, $7C, |
| 177 |
$FC, $02, $82, $42, $C2, $22, $A2, $62, $E2, $12, $92, $52, $D2, $32, |
| 178 |
$B2, $72, $F2, $0A, $8A, $4A, $CA, $2A, $AA, $6A, $EA, $1A, $9A, $5A, |
| 179 |
$DA, $3A, $BA, $7A, $FA, $06, $86, $46, $C6, $26, $A6, $66, $E6, $16, |
| 180 |
$96, $56, $D6, $36, $B6, $76, $F6, $0E, $8E, $4E, $CE, $2E, $AE, $6E, |
| 181 |
$EE, $1E, $9E, $5E, $DE, $3E, $BE, $7E, $FE, $01, $81, $41, $C1, $21, |
| 182 |
$A1, $61, $E1, $11, $91, $51, $D1, $31, $B1, $71, $F1, $09, $89, $49, |
| 183 |
$C9, $29, $A9, $69, $E9, $19, $99, $59, $D9, $39, $B9, $79, $F9, $05, |
| 184 |
$85, $45, $C5, $25, $A5, $65, $E5, $15, $95, $55, $D5, $35, $B5, $75, |
| 185 |
$F5, $0D, $8D, $4D, $CD, $2D, $AD, $6D, $ED, $1D, $9D, $5D, $DD, $3D, |
| 186 |
$BD, $7D, $FD, $03, $83, $43, $C3, $23, $A3, $63, $E3, $13, $93, $53, |
| 187 |
$D3, $33, $B3, $73, $F3, $0B, $8B, $4B, $CB, $2B, $AB, $6B, $EB, $1B, |
| 188 |
$9B, $5B, $DB, $3B, $BB, $7B, $FB, $07, $87, $47, $C7, $27, $A7, $67, |
| 189 |
$E7, $17, $97, $57, $D7, $37, $B7, $77, $F7, $0F, $8F, $4F, $CF, $2F, |
| 190 |
$AF, $6F, $EF, $1F, $9F, $5F, $DF, $3F, $BF, $7F, $FF); |
| 191 |
begin |
| 192 |
W := RevTable[Byte(W shr 8)] or Word(RevTable[Byte(W)] shl 8); |
| 193 |
end; |
| 194 |
|
| 195 |
|
| 196 |
{ TAbUnzipHelper implementation ============================================ } |
| 197 |
|
| 198 |
{ -------------------------------------------------------------------------- } |
| 199 |
constructor TAbUnzipHelper.Create( InputStream, OutputStream : TStream ); |
| 200 |
begin |
| 201 |
inherited Create; |
| 202 |
FOutBuf := AllocMem( AbBufferSize ); |
| 203 |
FOutPos := 0; |
| 204 |
FZStream := InputStream; |
| 205 |
FOutStream := OutputStream; |
| 206 |
FUncompressedSize := 0; |
| 207 |
FDictionarySize := dsInvalid; |
| 208 |
FShannonFanoTreeCount := 0; |
| 209 |
FCompressionMethod := cmDeflated; |
| 210 |
end; |
| 211 |
{ -------------------------------------------------------------------------- } |
| 212 |
destructor TAbUnzipHelper.Destroy; |
| 213 |
begin |
| 214 |
FreeMem( FOutBuf, AbBufferSize ); |
| 215 |
inherited Destroy; |
| 216 |
end; |
| 217 |
{ -------------------------------------------------------------------------- } |
| 218 |
procedure TAbUnzipHelper.Execute; |
| 219 |
begin |
| 220 |
{parent class handles exceptions via OnExtractFailure} |
| 221 |
FBitsLeft := 0; |
| 222 |
FCurByte := 0; |
| 223 |
FInCnt := 0; |
| 224 |
FOutSent := 0; |
| 225 |
FOutPos := 0; |
| 226 |
FInEof := False; |
| 227 |
|
| 228 |
{set the output stream; for Imploded/Reduced files this has to be |
| 229 |
buffered, for all other types of compression, the code buffers the |
| 230 |
output data nicely and so the given output stream can be used.} |
| 231 |
{$IFDEF UnzipImplodeSupport} |
| 232 |
if (FCompressionMethod = cmImploded) then |
| 233 |
FOutWriter := TabSlidingWindowStream.Create(FOutStream) |
| 234 |
else |
| 235 |
{$ENDIF} |
| 236 |
{$IFDEF UnzipReduceSupport} |
| 237 |
if (FCompressionMethod >= cmReduced1) and |
| 238 |
(FCompressionMethod <= cmReduced4) then |
| 239 |
FOutWriter := TabSlidingWindowStream.Create(FOutStream) |
| 240 |
else |
| 241 |
{$ENDIF} |
| 242 |
FOutWriter := FOutStream; |
| 243 |
FInPos := 1+SizeOf(FInBuf); |
| 244 |
|
| 245 |
{ GetMem( FInBuf, SizeOf(FInBuf^) );} |
| 246 |
try |
| 247 |
{uncompress it with the appropriate method} |
| 248 |
case FCompressionMethod of |
| 249 |
{$IFDEF UnzipShrinkSupport} |
| 250 |
cmShrunk : uzUnshrink; |
| 251 |
{$ENDIF} |
| 252 |
{$IFDEF UnzipReduceSupport} |
| 253 |
cmReduced1..cmReduced4 : uzUnReduce; |
| 254 |
{$ENDIF} |
| 255 |
{$IFDEF UnzipImplodeSupport} |
| 256 |
cmImploded : uzUnImplode; |
| 257 |
{$ENDIF} |
| 258 |
{cmTokenized} |
| 259 |
{cmEnhancedDeflated} |
| 260 |
{cmDCLImploded} |
| 261 |
else |
| 262 |
raise EAbZipInvalidMethod.Create; |
| 263 |
end; |
| 264 |
|
| 265 |
finally |
| 266 |
uzFlushOutBuf; |
| 267 |
{free any memory} |
| 268 |
if (FOutWriter <> FOutStream) then |
| 269 |
FOutWriter.Free; |
| 270 |
end; |
| 271 |
end; |
| 272 |
{ -------------------------------------------------------------------------- } |
| 273 |
procedure TAbUnzipHelper.uzReadNextPrim; |
| 274 |
begin |
| 275 |
FInCnt := FZStream.Read( FInBuf, sizeof( FInBuf ) ); |
| 276 |
FInEof := FInCnt = 0; |
| 277 |
{load first byte in buffer and set position counter} |
| 278 |
FCurByte := FInBuf[1]; |
| 279 |
FInPos := 2; |
| 280 |
end; |
| 281 |
{ -------------------------------------------------------------------------- } |
| 282 |
procedure TAbUnzipHelper.uzFlushOutBuf; |
| 283 |
{-flushes the output buffer} |
| 284 |
begin |
| 285 |
if (FOutPos <> 0) then begin |
| 286 |
FOutWriter.Write( FOutBuf^, FOutPos ); |
| 287 |
Inc( FOutSent, FOutPos ); |
| 288 |
FOutPos := 0; |
| 289 |
end; |
| 290 |
end; |
| 291 |
{ -------------------------------------------------------------------------- } |
| 292 |
procedure TAbUnzipHelper.uzWriteByte(B : Byte); |
| 293 |
{-Write one byte to the output buffer} |
| 294 |
begin |
| 295 |
FOutBuf^[FOutPos] := B; |
| 296 |
inc(FOutPos); |
| 297 |
if (FOutPos = AbBufferSize) or |
| 298 |
(LongInt(FOutPos) + FOutSent = FUncompressedSize) then |
| 299 |
uzFlushOutBuf; |
| 300 |
end; |
| 301 |
{ -------------------------------------------------------------------------- } |
| 302 |
function TAbUnzipHelper.uzReadBits(Bits : Byte) : Integer; |
| 303 |
{-Read the specified number of bits} |
| 304 |
var |
| 305 |
SaveCurByte, Delta, SaveBitsLeft : Byte; |
| 306 |
begin |
| 307 |
{read next byte if we're out of bits} |
| 308 |
if FBitsLeft = 0 then begin |
| 309 |
{do we still have a byte buffered?} |
| 310 |
if FInPos <= FInCnt then begin |
| 311 |
{get next byte out of buffer and advance position counter} |
| 312 |
FCurByte := FInBuf[FInPos]; |
| 313 |
Inc(FInPos); |
| 314 |
end |
| 315 |
{are there any left to read?} |
| 316 |
else |
| 317 |
uzReadNextPrim; |
| 318 |
|
| 319 |
FBitsLeft := 8; |
| 320 |
end; |
| 321 |
if ( Bits < FBitsLeft ) then begin |
| 322 |
Dec( FBitsLeft, Bits ); |
| 323 |
Result := ((1 shl Bits) - 1) and FCurByte; |
| 324 |
FCurByte := FCurByte shr Bits; |
| 325 |
end |
| 326 |
else if ( Bits = FBitsLeft ) then begin |
| 327 |
Result := FCurByte; |
| 328 |
FCurByte := 0; |
| 329 |
FBitsLeft := 0; |
| 330 |
end |
| 331 |
else begin |
| 332 |
SaveCurByte := FCurByte; |
| 333 |
SaveBitsLeft := FBitsLeft; |
| 334 |
{number of additional bits that we need} |
| 335 |
Delta := Bits - FBitsLeft; |
| 336 |
{do we still have a byte buffered?} |
| 337 |
if FInPos <= FInCnt then begin |
| 338 |
{get next byte out of buffer and advance position counter} |
| 339 |
FCurByte := FInBuf[FInPos]; |
| 340 |
Inc(FInPos); |
| 341 |
end |
| 342 |
{are there any left to read?} |
| 343 |
else |
| 344 |
uzReadNextPrim; |
| 345 |
|
| 346 |
FBitsLeft := 8; |
| 347 |
Result := ( uzReadBits( Delta ) shl SaveBitsLeft ) or SaveCurByte; |
| 348 |
end; |
| 349 |
end; |
| 350 |
{$IFDEF UnzipImplodeSupport} |
| 351 |
{ -------------------------------------------------------------------------- } |
| 352 |
procedure TAbUnzipHelper.uzUnImplode; |
| 353 |
{-Extract an imploded file} |
| 354 |
const |
| 355 |
szLengthTree = SizeOf(TAbSfTree)-(192*SizeOf(TAbSfEntry)); |
| 356 |
szDistanceTree = SizeOf(TAbSfTree)-(192*SizeOf(TAbSfEntry)); |
| 357 |
szLitTree = SizeOf(TAbSfTree); |
| 358 |
var |
| 359 |
Length : Integer; |
| 360 |
DIndex : LongInt; |
| 361 |
Distance : Integer; |
| 362 |
SPos : LongInt; |
| 363 |
MyByte : Byte; |
| 364 |
DictBits : Integer; {number of bits used in sliding dictionary} |
| 365 |
MinMatchLength : Integer; {minimum match length} |
| 366 |
LitTree : PAbSfTree; {Literal tree} |
| 367 |
LengthTree : PAbSfTree; {Length tree} |
| 368 |
DistanceTree : PAbSfTree; {Distance tree} |
| 369 |
|
| 370 |
procedure uzLoadTree(var T; TreeSize : Integer); |
| 371 |
{-Load one Shannon-Fano tree} |
| 372 |
var |
| 373 |
I : Word; |
| 374 |
Tree : TAbSfTree absolute T; |
| 375 |
|
| 376 |
procedure GenerateTree; |
| 377 |
{-Generate a Shannon-Fano tree} |
| 378 |
var |
| 379 |
C : Word; |
| 380 |
CodeIncrement : Integer; |
| 381 |
LastBitLength : Integer; |
| 382 |
I : Integer; |
| 383 |
begin |
| 384 |
C := 0; |
| 385 |
CodeIncrement := 0; |
| 386 |
LastBitLength := 0; |
| 387 |
|
| 388 |
for I := Tree.Entries-1 downto 0 do |
| 389 |
with Tree.Entry[I] do begin |
| 390 |
Inc(C, CodeIncrement); |
| 391 |
if BitLength <> LastBitLength then begin |
| 392 |
LastBitLength := BitLength; |
| 393 |
CodeIncrement := 1 shl (16-LastBitLength); |
| 394 |
end; |
| 395 |
Code := C; |
| 396 |
end; |
| 397 |
end; |
| 398 |
|
| 399 |
procedure SortLengths; |
| 400 |
{-Sort the bit lengths in ascending order, while retaining the order |
| 401 |
of the original lengths stored in the file} |
| 402 |
var |
| 403 |
XL : Integer; |
| 404 |
XGL : Integer; |
| 405 |
TXP : PAbSfEntry; |
| 406 |
TXGP : PAbSfEntry; |
| 407 |
X, Gap : Integer; |
| 408 |
Done : Boolean; |
| 409 |
LT : LongInt; |
| 410 |
begin |
| 411 |
Gap := Tree.Entries shr 1; |
| 412 |
repeat |
| 413 |
repeat |
| 414 |
Done := True; |
| 415 |
for X := 0 to (Tree.Entries-1)-Gap do begin |
| 416 |
TXP := @Tree.Entry[X]; |
| 417 |
TXGP := @Tree.Entry[X+Gap]; |
| 418 |
XL := TXP^.BitLength; |
| 419 |
XGL := TXGP^.BitLength; |
| 420 |
if (XL > XGL) or |
| 421 |
((XL = XGL) and (TXP^.Value > TXGP^.Value)) then begin |
| 422 |
LT := TXP^.L; |
| 423 |
TXP^.L := TXGP^.L; |
| 424 |
TXGP^.L := LT; |
| 425 |
Done := False; |
| 426 |
end; |
| 427 |
end; |
| 428 |
until Done; |
| 429 |
|
| 430 |
Gap := Gap shr 1; |
| 431 |
until (Gap = 0); |
| 432 |
end; |
| 433 |
|
| 434 |
procedure uzReadLengths; |
| 435 |
{-Read bit lengths for a tree} |
| 436 |
var |
| 437 |
TreeBytes : Integer; |
| 438 |
I, J, K : Integer; |
| 439 |
Num, Len : Integer; |
| 440 |
B : Byte; |
| 441 |
begin |
| 442 |
{get number of bytes in compressed tree} |
| 443 |
TreeBytes := uzReadBits(8)+1; |
| 444 |
|
| 445 |
I := 0; |
| 446 |
Tree.MaxLength := 0; |
| 447 |
|
| 448 |
{High nibble: Number of values at this bit length + 1. |
| 449 |
Low nibble: Bits needed to represent value + 1} |
| 450 |
for J := 1 to TreeBytes do begin |
| 451 |
B := uzReadBits(8); |
| 452 |
Len := (B and $0F)+1; |
| 453 |
Num := (B shr 4)+1; |
| 454 |
|
| 455 |
for K := I to I+Num-1 do |
| 456 |
with Tree, Entry[K] do begin |
| 457 |
if Len > MaxLength then |
| 458 |
MaxLength := Len; |
| 459 |
BitLength := Len; |
| 460 |
Value := K; |
| 461 |
end; |
| 462 |
Inc(I, Num); |
| 463 |
end; |
| 464 |
end; |
| 465 |
|
| 466 |
begin |
| 467 |
Tree.Entries := TreeSize; |
| 468 |
uzReadLengths; |
| 469 |
SortLengths; |
| 470 |
GenerateTree; |
| 471 |
for I := 0 to TreeSize-1 do |
| 472 |
AbReverseBits(Tree.Entry[I].Code); |
| 473 |
end; |
| 474 |
|
| 475 |
function uzReadTree(var T) : Byte; |
| 476 |
{-Read next byte using a Shannon-Fano tree} |
| 477 |
var |
| 478 |
Bits : Integer; |
| 479 |
CV : Word; |
| 480 |
E : Integer; |
| 481 |
Cur : Integer; |
| 482 |
var |
| 483 |
Tree : TAbSfTree absolute T; |
| 484 |
begin |
| 485 |
Result := 0; |
| 486 |
Bits := 0; |
| 487 |
CV := 0; |
| 488 |
Cur := 0; |
| 489 |
E := Tree.Entries; |
| 490 |
repeat |
| 491 |
CV := CV or (uzReadBits(1) shl Bits); |
| 492 |
Inc(Bits); |
| 493 |
while Tree.Entry[Cur].BitLength < Bits do begin |
| 494 |
Inc(Cur); |
| 495 |
if Cur >= E then |
| 496 |
Exit; |
| 497 |
end; |
| 498 |
while Tree.Entry[Cur].BitLength = Bits do begin |
| 499 |
if Tree.Entry[Cur].Code = CV then begin |
| 500 |
Result := Tree.Entry[Cur].Value; |
| 501 |
Exit; |
| 502 |
end; |
| 503 |
Inc(Cur); |
| 504 |
if Cur >= E then |
| 505 |
Exit; |
| 506 |
end; |
| 507 |
until False; |
| 508 |
end; |
| 509 |
|
| 510 |
begin |
| 511 |
{do we have an 8K dictionary?} |
| 512 |
if FDictionarySize = ds8K then |
| 513 |
DictBits := 7 |
| 514 |
else |
| 515 |
DictBits := 6; |
| 516 |
|
| 517 |
{allocate trees} |
| 518 |
LengthTree := AllocMem(szLengthTree); |
| 519 |
DistanceTree := AllocMem(szDistanceTree); |
| 520 |
LitTree := nil; |
| 521 |
try |
| 522 |
{do we have a Literal tree?} |
| 523 |
MinMatchLength := FShannonFanoTreeCount; |
| 524 |
if MinMatchLength = 3 then begin |
| 525 |
LitTree := AllocMem(szLitTree); |
| 526 |
uzLoadTree(LitTree^, 256); |
| 527 |
end; |
| 528 |
|
| 529 |
{load the other two trees} |
| 530 |
uzLoadTree(LengthTree^, 64); |
| 531 |
uzLoadTree(DistanceTree^, 64); |
| 532 |
|
| 533 |
while (not FInEof) and (FOutSent + LongInt(FOutPos) < FUncompressedSize) do |
| 534 |
{is data literal?} |
| 535 |
if Boolean(uzReadBits(1)) then begin |
| 536 |
{if MinMatchLength = 3 then we have a Literal tree} |
| 537 |
if (MinMatchLength = 3) then |
| 538 |
uzWriteByte( uzReadTree(LitTree^) ) |
| 539 |
else |
| 540 |
uzWriteByte( uzReadBits(8) ); |
| 541 |
end |
| 542 |
else begin |
| 543 |
{data is a sliding dictionary} |
| 544 |
Distance := uzReadBits(DictBits); |
| 545 |
|
| 546 |
{using the Distance Shannon-Fano tree, read and decode the |
| 547 |
upper 6 bits of the Distance value} |
| 548 |
Distance := Distance or (uzReadTree(DistanceTree^) shl DictBits); |
| 549 |
|
| 550 |
{using the Length Shannon-Fano tree, read and decode the Length value} |
| 551 |
Length := uzReadTree(LengthTree^); |
| 552 |
if Length = 63 then |
| 553 |
Inc(Length, uzReadBits(8)); |
| 554 |
Inc(Length, MinMatchLength); |
| 555 |
|
| 556 |
{move backwards Distance+1 bytes in the output stream, and copy |
| 557 |
Length characters from this position to the output stream. |
| 558 |
(if this position is before the start of the output stream, |
| 559 |
then assume that all the data before the start of the output |
| 560 |
stream is filled with zeros)} |
| 561 |
DIndex := (FOutSent + LongInt(FOutPos))-(Distance+1); |
| 562 |
while Length > 0 do begin |
| 563 |
if DIndex < 0 then |
| 564 |
uzWriteByte(0) |
| 565 |
else begin |
| 566 |
uzFlushOutBuf; |
| 567 |
SPos := FOutWriter.Position; |
| 568 |
FOutWriter.Position := DIndex; |
| 569 |
FOutWriter.Read( MyByte, 1 ); |
| 570 |
FOutWriter.Position := SPos; |
| 571 |
uzWriteByte(MyByte); |
| 572 |
end; |
| 573 |
Inc(DIndex); |
| 574 |
Dec(Length); |
| 575 |
end; |
| 576 |
end; |
| 577 |
finally |
| 578 |
if (LitTree <> nil) then |
| 579 |
FreeMem(LitTree, szLitTree); |
| 580 |
FreeMem(LengthTree, szLengthTree); |
| 581 |
FreeMem(DistanceTree, szDistanceTree); |
| 582 |
end; |
| 583 |
end; |
| 584 |
{$ENDIF UnzipImplodeSupport} |
| 585 |
{ -------------------------------------------------------------------------- } |
| 586 |
{$IFDEF UnzipReduceSupport} |
| 587 |
procedure TAbUnzipHelper.uzUnReduce; |
| 588 |
const |
| 589 |
FactorMasks : array[1..4] of Byte = ($7F, $3F, $1F, $0F); |
| 590 |
DLE = 144; |
| 591 |
var |
| 592 |
C, Last : Byte; |
| 593 |
OpI : LongInt; |
| 594 |
I, J, Sz : Integer; |
| 595 |
D : Word; |
| 596 |
SPos : LongInt; |
| 597 |
MyByte : Byte; |
| 598 |
Factor : Byte; {reduction Factor} |
| 599 |
FactorMask : Byte; {bit mask to use based on Factor} |
| 600 |
Followers : PAbFollowerSets; {array of follower sets} |
| 601 |
State : Integer; {used while processing reduced files} |
| 602 |
V : Integer; {"} |
| 603 |
Len : Integer; {"} |
| 604 |
|
| 605 |
function BitsNeeded( i : Byte ) : Word; |
| 606 |
begin |
| 607 |
dec( i ); |
| 608 |
Result := 0; |
| 609 |
repeat |
| 610 |
inc( Result ); |
| 611 |
i := i shr 1; |
| 612 |
until i = 0; |
| 613 |
end; |
| 614 |
|
| 615 |
begin |
| 616 |
GetMem(Followers, SizeOf(TAbFollowerSets)); |
| 617 |
try |
| 618 |
Factor := Ord( FCompressionMethod ) - 1; |
| 619 |
FactorMask := FactorMasks[Factor]; |
| 620 |
State := 0; |
| 621 |
C := 0; |
| 622 |
V := 0; |
| 623 |
Len := 0; |
| 624 |
D := 0; |
| 625 |
|
| 626 |
{load follower sets} |
| 627 |
for I := 255 downto 0 do begin |
| 628 |
Sz := uzReadBits(6); |
| 629 |
Followers^[I].Size := Sz; |
| 630 |
Dec(Sz); |
| 631 |
for J := 0 to Sz do |
| 632 |
Followers^[I].FSet[J] := uzReadBits(8); |
| 633 |
end; |
| 634 |
|
| 635 |
while (not FInEof) and ((FOutSent + LongInt(FOutPos)) < FUncompressedSize) do begin |
| 636 |
Last := C; |
| 637 |
with Followers^[Last] do |
| 638 |
if Size = 0 then |
| 639 |
C := uzReadBits(8) |
| 640 |
else begin |
| 641 |
C := uzReadBits(1); |
| 642 |
if C <> 0 then |
| 643 |
C := uzReadBits(8) |
| 644 |
else |
| 645 |
C := FSet[uzReadBits(BitsNeeded(Size))]; |
| 646 |
end; |
| 647 |
|
| 648 |
if FInEof then |
| 649 |
Exit; |
| 650 |
|
| 651 |
case State of |
| 652 |
0 : |
| 653 |
if C <> DLE then |
| 654 |
uzWriteByte(C) |
| 655 |
else |
| 656 |
State := 1; |
| 657 |
1 : |
| 658 |
if C <> 0 then begin |
| 659 |
V := C; |
| 660 |
Len := V and FactorMask; |
| 661 |
if Len = FactorMask then |
| 662 |
State := 2 |
| 663 |
else |
| 664 |
State := 3; |
| 665 |
end |
| 666 |
else begin |
| 667 |
uzWriteByte(DLE); |
| 668 |
State := 0; |
| 669 |
end; |
| 670 |
|
| 671 |
2 : |
| 672 |
begin |
| 673 |
Inc(Len, C); |
| 674 |
State := 3; |
| 675 |
end; |
| 676 |
|
| 677 |
3 : |
| 678 |
begin |
| 679 |
case Factor of |
| 680 |
1 : D := (V shr 7) and $01; |
| 681 |
2 : D := (V shr 6) and $03; |
| 682 |
3 : D := (V shr 5) and $07; |
| 683 |
4 : D := (V shr 4) and $0f; |
| 684 |
else |
| 685 |
raise EAbZipInvalidFactor.Create; |
| 686 |
end; |
| 687 |
{Delphi raises compiler Hints here, saying D might |
| 688 |
be undefined... If Factor is not in [1..4], the |
| 689 |
exception gets raised, and we never execute the following |
| 690 |
line} |
| 691 |
OpI := (FOutSent + LongInt(FOutPos))-(Swap(D)+C+1); |
| 692 |
|
| 693 |
for I := 0 to Len+2 do begin |
| 694 |
if OpI < 0 then |
| 695 |
uzWriteByte(0) |
| 696 |
else if OpI >= FOutSent then |
| 697 |
uzWriteByte(FOutBuf[OpI - FOutSent]) |
| 698 |
else begin |
| 699 |
SPos := FOutWriter.Position; |
| 700 |
FOutWriter.Position := OpI; |
| 701 |
FOutWriter.Read( MyByte, 1 ); |
| 702 |
FOutWriter.Position := SPos; |
| 703 |
uzWriteByte(MyByte); |
| 704 |
end; |
| 705 |
Inc(OpI); |
| 706 |
end; |
| 707 |
|
| 708 |
State := 0; |
| 709 |
end; |
| 710 |
end; |
| 711 |
end; |
| 712 |
finally |
| 713 |
FreeMem(Followers, SizeOf(Followers^)); |
| 714 |
end; |
| 715 |
end; |
| 716 |
{$ENDIF UnzipReduceSupport} |
| 717 |
{ -------------------------------------------------------------------------- } |
| 718 |
{$IFDEF UnzipShrinkSupport} |
| 719 |
procedure TAbUnzipHelper.uzUnShrink; |
| 720 |
{-Extract a file that was shrunk} |
| 721 |
const |
| 722 |
MaxBits = 13; |
| 723 |
InitBits = 9; |
| 724 |
FirstFree = 257; |
| 725 |
Clear = 256; |
| 726 |
MaxCodeMax = 8192; {= 1 shl MaxBits} |
| 727 |
Unused = -1; |
| 728 |
var |
| 729 |
CodeSize : SmallInt; |
| 730 |
NextFree : SmallInt; |
| 731 |
BaseChar : SmallInt; |
| 732 |
NewCode : SmallInt; |
| 733 |
OldCode : SmallInt; |
| 734 |
SaveCode : SmallInt; |
| 735 |
N, R : SmallInt; |
| 736 |
I : Integer; |
| 737 |
PrefixTable : PAbIntArray8K; {used while processing shrunk files} |
| 738 |
SuffixTable : PAbByteArray8K; {"} |
| 739 |
Stack : PAbByteArray8K; {"} |
| 740 |
StackIndex : Integer; {"} |
| 741 |
begin |
| 742 |
CodeSize := InitBits; |
| 743 |
{ MaxCode := (1 shl InitBits)-1;} |
| 744 |
NextFree := FirstFree; |
| 745 |
|
| 746 |
PrefixTable := nil; |
| 747 |
SuffixTable := nil; |
| 748 |
Stack := nil; |
| 749 |
|
| 750 |
try |
| 751 |
GetMem(PrefixTable, SizeOf(PrefixTable^)); |
| 752 |
SuffixTable := AllocMem(SizeOf(SuffixTable^)); |
| 753 |
GetMem(Stack, SizeOf(Stack^)); |
| 754 |
|
| 755 |
FillChar(PrefixTable^, SizeOf(PrefixTable^), $FF); |
| 756 |
for NewCode := 255 downto 0 do begin |
| 757 |
PrefixTable^[NewCode] := 0; |
| 758 |
SuffixTable^[NewCode] := NewCode; |
| 759 |
end; |
| 760 |
|
| 761 |
OldCode := uzReadBits(CodeSize); |
| 762 |
if FInEof then |
| 763 |
Exit; |
| 764 |
BaseChar := OldCode; |
| 765 |
|
| 766 |
uzWriteByte(BaseChar); |
| 767 |
|
| 768 |
StackIndex := 0; |
| 769 |
while (not FInEof) do begin |
| 770 |
NewCode := uzReadBits(CodeSize); |
| 771 |
while (NewCode = Clear) and (not FInEof) do begin |
| 772 |
case uzReadBits(CodeSize) of |
| 773 |
1 : begin |
| 774 |
Inc(CodeSize); |
| 775 |
end; |
| 776 |
2 : begin |
| 777 |
{mark all nodes as potentially unused} |
| 778 |
for I := FirstFree to pred( NextFree ) do |
| 779 |
PrefixTable^[I] := PrefixTable^[I] or LongInt($8000); |
| 780 |
|
| 781 |
{unmark those used by other nodes} |
| 782 |
for N := FirstFree to NextFree-1 do begin |
| 783 |
{reference to another node?} |
| 784 |
R := PrefixTable^[N] and $7FFF; |
| 785 |
{flag node as referenced} |
| 786 |
if R >= FirstFree then |
| 787 |
PrefixTable^[R] := PrefixTable^[R] and $7FFF; |
| 788 |
end; |
| 789 |
|
| 790 |
{clear the ones that are still marked} |
| 791 |
for I := FirstFree to pred( NextFree ) do |
| 792 |
if PrefixTable^[I] < 0 then |
| 793 |
PrefixTable^[I] := -1; |
| 794 |
|
| 795 |
{recalculate NextFree} |
| 796 |
NextFree := FirstFree; |
| 797 |
while (NextFree < MaxCodeMax) and |
| 798 |
(PrefixTable^[NextFree] <> -1) do |
| 799 |
Inc(NextFree); |
| 800 |
end; |
| 801 |
end; |
| 802 |
|
| 803 |
NewCode := uzReadBits(CodeSize); |
| 804 |
end; |
| 805 |
|
| 806 |
if FInEof then |
| 807 |
Exit; |
| 808 |
|
| 809 |
{save current code} |
| 810 |
SaveCode := NewCode; |
| 811 |
|
| 812 |
{special case} |
| 813 |
if PrefixTable^[NewCode] = Unused then begin |
| 814 |
Stack^[StackIndex] := BaseChar; |
| 815 |
Inc(StackIndex); |
| 816 |
NewCode := OldCode; |
| 817 |
end; |
| 818 |
|
| 819 |
{generate output characters in reverse order} |
| 820 |
while (NewCode >= FirstFree) do begin |
| 821 |
if PrefixTable^[NewCode] = Unused then begin |
| 822 |
Stack^[StackIndex] := BaseChar; |
| 823 |
Inc(StackIndex); |
| 824 |
NewCode := OldCode; |
| 825 |
end else begin |
| 826 |
Stack^[StackIndex] := SuffixTable^[NewCode]; |
| 827 |
Inc(StackIndex); |
| 828 |
NewCode := PrefixTable^[NewCode]; |
| 829 |
end; |
| 830 |
end; |
| 831 |
|
| 832 |
BaseChar := SuffixTable^[NewCode]; |
| 833 |
uzWriteByte(BaseChar); |
| 834 |
|
| 835 |
{put them out in forward order} |
| 836 |
while (StackIndex > 0) do begin |
| 837 |
Dec(StackIndex); |
| 838 |
uzWriteByte(Stack^[StackIndex]); |
| 839 |
end; |
| 840 |
|
| 841 |
{add new entry to tables} |
| 842 |
NewCode := NextFree; |
| 843 |
if NewCode < MaxCodeMax then begin |
| 844 |
PrefixTable^[NewCode] := OldCode; |
| 845 |
SuffixTable^[NewCode] := BaseChar; |
| 846 |
while (NextFree < MaxCodeMax) and |
| 847 |
(PrefixTable^[NextFree] <> Unused) do |
| 848 |
Inc(NextFree); |
| 849 |
end; |
| 850 |
|
| 851 |
{remember previous code} |
| 852 |
OldCode := SaveCode; |
| 853 |
end; |
| 854 |
finally |
| 855 |
FreeMem(PrefixTable, SizeOf(PrefixTable^)); |
| 856 |
FreeMem(SuffixTable, SizeOf(SuffixTable^)); |
| 857 |
FreeMem(Stack, SizeOf(Stack^)); |
| 858 |
end; |
| 859 |
end; |
| 860 |
{$ENDIF} |
| 861 |
{ -------------------------------------------------------------------------- } |
| 862 |
procedure RequestPassword(Archive : TAbZipArchive; var Abort : Boolean); |
| 863 |
var |
| 864 |
APassPhrase : AnsiString; |
| 865 |
begin |
| 866 |
APassPhrase := Archive.Password; |
| 867 |
Abort := False; |
| 868 |
if Assigned(Archive.OnNeedPassword) then begin |
| 869 |
Archive.OnNeedPassword(Archive, APassPhrase); |
| 870 |
if APassPhrase = '' then |
| 871 |
Abort := True |
| 872 |
else |
| 873 |
Archive.Password := APassPhrase; |
| 874 |
end; |
| 875 |
end; |
| 876 |
{ -------------------------------------------------------------------------- } |
| 877 |
procedure CheckPassword(Archive : TAbZipArchive; var Tries : Integer; var Abort : Boolean); |
| 878 |
begin |
| 879 |
{ if current password empty } |
| 880 |
if Archive.Password = '' then begin |
| 881 |
{ request password } |
| 882 |
RequestPassword(Archive, Abort); |
| 883 |
{ increment tries } |
| 884 |
Inc(Tries); |
| 885 |
end; |
| 886 |
|
| 887 |
{ if current password still empty } |
| 888 |
if Archive.Password = '' then begin |
| 889 |
{ abort } |
| 890 |
raise EAbZipInvalidPassword.Create; |
| 891 |
end; |
| 892 |
end; |
| 893 |
{ -------------------------------------------------------------------------- } |
| 894 |
procedure DoInflate(Archive : TAbZipArchive; Item : TAbZipItem; InStream, OutStream : TStream); |
| 895 |
var |
| 896 |
Hlpr : TAbDeflateHelper; |
| 897 |
begin |
| 898 |
Hlpr := TAbDeflateHelper.Create; |
| 899 |
try |
| 900 |
Hlpr.NormalSize := Item.UncompressedSize; |
| 901 |
|
| 902 |
AbZlibPrc.Inflate(InStream, OutStream, Hlpr); |
| 903 |
finally |
| 904 |
Hlpr.Free; |
| 905 |
end; |
| 906 |
end; |
| 907 |
{ -------------------------------------------------------------------------- } |
| 908 |
procedure DoInflate64(Archive : TAbZipArchive; Item : TAbZipItem; InStream, OutStream : TStream); |
| 909 |
var |
| 910 |
Inflate64Stream: TInflate64Stream; |
| 911 |
begin |
| 912 |
Inflate64Stream := TInflate64Stream.Create(InStream); |
| 913 |
try |
| 914 |
OutStream.CopyFrom(Inflate64Stream, Item.UncompressedSize); |
| 915 |
finally |
| 916 |
Inflate64Stream.Free; |
| 917 |
end; |
| 918 |
end; |
| 919 |
{ -------------------------------------------------------------------------- } |
| 920 |
procedure DoLegacyUnzip(Archive : TAbZipArchive; Item : TAbZipItem; InStream, OutStream : TStream); |
| 921 |
var |
| 922 |
Helper : TAbUnzipHelper; |
| 923 |
begin |
| 924 |
Helper := TAbUnzipHelper.Create(InStream, OutStream); |
| 925 |
try {Helper} |
| 926 |
Helper.DictionarySize := Item.DictionarySize; |
| 927 |
Helper.UnCompressedSize := Item.UncompressedSize; |
| 928 |
Helper.CompressionMethod := Item.CompressionMethod; |
| 929 |
Helper.ShannonFanoTreeCount := Item.ShannonFanoTreeCount; |
| 930 |
Helper.Execute; |
| 931 |
finally |
| 932 |
Helper.Free; |
| 933 |
end; |
| 934 |
end; |
| 935 |
{ -------------------------------------------------------------------------- } |
| 936 |
{$IFDEF UnzipBzip2Support} |
| 937 |
procedure DoExtractBzip2(Archive : TAbZipArchive; Item : TAbZipItem; InStream, OutStream : TStream); |
| 938 |
var |
| 939 |
Bzip2Stream: TStream; |
| 940 |
begin |
| 941 |
Bzip2Stream := TBZDecompressionStream.Create(InStream); |
| 942 |
try |
| 943 |
OutStream.CopyFrom(Bzip2Stream, Item.UncompressedSize); |
| 944 |
finally |
| 945 |
Bzip2Stream.Free; |
| 946 |
end; |
| 947 |
end; |
| 948 |
{$ENDIF} |
| 949 |
{ -------------------------------------------------------------------------- } |
| 950 |
{$IFDEF UnzipLzmaSupport} |
| 951 |
procedure DoExtractLzma(Archive : TAbZipArchive; Item : TAbZipItem; |
| 952 |
InStream, OutStream : TStream); |
| 953 |
var |
| 954 |
Header: packed record |
| 955 |
MajorVer, MinorVer: Byte; |
| 956 |
PropSize: Word; |
| 957 |
end; |
| 958 |
Properties: array of Byte; |
| 959 |
begin |
| 960 |
InStream.ReadBuffer(Header, SizeOf(Header)); |
| 961 |
SetLength(Properties, Header.PropSize); |
| 962 |
InStream.ReadBuffer(Properties[0], Header.PropSize); |
| 963 |
with TLZMADecoder.Create do |
| 964 |
try |
| 965 |
SetDecoderProperties(Properties); |
| 966 |
Code(InStream, OutStream, Item.UncompressedSize); |
| 967 |
finally |
| 968 |
Free; |
| 969 |
end; |
| 970 |
end; |
| 971 |
{$ENDIF} |
| 972 |
{ -------------------------------------------------------------------------- } |
| 973 |
{$IFDEF UnzipXzSupport} |
| 974 |
procedure DoExtractXz(Archive : TAbZipArchive; Item : TAbZipItem; InStream, OutStream : TStream); |
| 975 |
var |
| 976 |
XzStream: TXzDecompressionStream; |
| 977 |
begin |
| 978 |
XzStream := TXzDecompressionStream.Create(InStream); |
| 979 |
try |
| 980 |
OutStream.CopyFrom(XzStream, Item.UncompressedSize); |
| 981 |
finally |
| 982 |
XzStream.Free; |
| 983 |
end; |
| 984 |
end; |
| 985 |
{$ENDIF} |
| 986 |
{ -------------------------------------------------------------------------- } |
| 987 |
{$IFDEF UnzipZstdSupport} |
| 988 |
procedure DoExtractZstd(Archive : TAbZipArchive; Item : TAbZipItem; InStream, OutStream : TStream); |
| 989 |
var |
| 990 |
ZstdStream: TStream; |
| 991 |
begin |
| 992 |
ZstdStream := TZstdDecompressionStream.Create(InStream); |
| 993 |
try |
| 994 |
OutStream.CopyFrom(ZstdStream, Item.UncompressedSize); |
| 995 |
finally |
| 996 |
ZstdStream.Free; |
| 997 |
end; |
| 998 |
end; |
| 999 |
{$ENDIF} |
| 1000 |
{ -------------------------------------------------------------------------- } |
| 1001 |
function ExtractPrep(ZipArchive: TAbZipArchive; Item: TAbZipItem): TStream; |
| 1002 |
var |
| 1003 |
LFH : TAbZipLocalFileHeader; |
| 1004 |
Abort : Boolean; |
| 1005 |
Tries : Integer; |
| 1006 |
CheckValue : LongInt; |
| 1007 |
DecryptStream: TAbDfDecryptStream; |
| 1008 |
FieldSize: Word; |
| 1009 |
WinZipAesField: PWinZipAesRec = nil; |
| 1010 |
AesDecryptStream: TAbWinZipAesDecryptStream; |
| 1011 |
begin |
| 1012 |
{ validate } |
| 1013 |
if (Lo(Item.VersionNeededToExtract) > Ab_ZipVersion) then |
| 1014 |
raise EAbZipVersion.Create; |
| 1015 |
|
| 1016 |
{ seek to compressed file } |
| 1017 |
if ZipArchive.FStream is TAbSpanReadStream then |
| 1018 |
TAbSpanReadStream(ZipArchive.FStream).SeekImage(Item.DiskNumberStart, |
| 1019 |
Item.RelativeOffset) |
| 1020 |
else |
| 1021 |
ZipArchive.FStream.Position := Item.RelativeOffset; |
| 1022 |
|
| 1023 |
{ get local header info for Item} |
| 1024 |
LFH := TAbZipLocalFileHeader.Create; |
| 1025 |
try |
| 1026 |
{ select appropriate CRC value based on General Purpose Bit Flag } |
| 1027 |
{ also get whether the file is stored, while we've got the local file header } |
| 1028 |
LFH.LoadFromStream(ZipArchive.FStream); |
| 1029 |
if (LFH.GeneralPurposeBitFlag and AbHasDataDescriptorFlag = AbHasDataDescriptorFlag) then |
| 1030 |
{ if bit 3 is set, then the data descriptor record is appended |
| 1031 |
to the compressed data } |
| 1032 |
CheckValue := LFH.LastModFileTime shl $10 |
| 1033 |
else |
| 1034 |
CheckValue := Item.CRC32; |
| 1035 |
finally |
| 1036 |
LFH.Free; |
| 1037 |
end; |
| 1038 |
|
| 1039 |
Result := TAbUnzipSubsetStream.Create(ZipArchive.FStream, |
| 1040 |
Item.CompressedSize); |
| 1041 |
|
| 1042 |
{ get decrypting stream } |
| 1043 |
if Item.IsEncrypted then begin |
| 1044 |
try |
| 1045 |
{ check WinZip AES extra field } |
| 1046 |
if Item.ExtraField.Get(Ab_WinZipAesID, Pointer(WinZipAesField), FieldSize) then |
| 1047 |
begin |
| 1048 |
{ get real compression method } |
| 1049 |
Item.CompressionMethod := TAbZipCompressionMethod(WinZipAesField.Method); |
| 1050 |
end; |
| 1051 |
{ need to decrypt } |
| 1052 |
Tries := 0; |
| 1053 |
Abort := False; |
| 1054 |
CheckPassword(ZipArchive, Tries, Abort); |
| 1055 |
while True do begin |
| 1056 |
if Abort then |
| 1057 |
raise EAbUserAbort.Create; |
| 1058 |
{ check for valid password } |
| 1059 |
if Assigned(WinZipAesField) then |
| 1060 |
begin |
| 1061 |
AesDecryptStream := TAbWinZipAesDecryptStream.Create(Result, |
| 1062 |
WinZipAesField, ZipArchive.Password); |
| 1063 |
if AesDecryptStream.IsValid then begin |
| 1064 |
AesDecryptStream.OwnsStream := True; |
| 1065 |
Result := AesDecryptStream; |
| 1066 |
Break; |
| 1067 |
end; |
| 1068 |
FreeAndNil(AesDecryptStream); |
| 1069 |
end |
| 1070 |
else begin |
| 1071 |
DecryptStream := TAbDfDecryptStream.Create(Result, |
| 1072 |
CheckValue, ZipArchive.Password); |
| 1073 |
if DecryptStream.IsValid then begin |
| 1074 |
DecryptStream.OwnsStream := True; |
| 1075 |
Result := DecryptStream; |
| 1076 |
Break; |
| 1077 |
end; |
| 1078 |
FreeAndNil(DecryptStream); |
| 1079 |
end; |
| 1080 |
{ prompt again } |
| 1081 |
Inc(Tries); |
| 1082 |
if (Tries > ZipArchive.PasswordRetries) then |
| 1083 |
raise EAbZipInvalidPassword.Create; |
| 1084 |
RequestPassword(ZipArchive, Abort); |
| 1085 |
end; |
| 1086 |
except |
| 1087 |
Result.Free; |
| 1088 |
raise; |
| 1089 |
end; |
| 1090 |
end; |
| 1091 |
end; |
| 1092 |
{ -------------------------------------------------------------------------- } |
| 1093 |
procedure DoExtract(aZipArchive: TAbZipArchive; aItem: TAbZipItem; |
| 1094 |
aInStream, aOutStream: TStream); |
| 1095 |
var |
| 1096 |
Wrong: Boolean; |
| 1097 |
OutStream : TAbUnzipOutputStream; |
| 1098 |
begin |
| 1099 |
if aItem.UncompressedSize = 0 then |
| 1100 |
Exit; |
| 1101 |
|
| 1102 |
OutStream := TAbUnzipOutputStream.Create(aOutStream); |
| 1103 |
try |
| 1104 |
OutStream.UncompressedSize := aItem.UncompressedSize; |
| 1105 |
OutStream.OnProgress := aZipArchive.OnProgress; |
| 1106 |
|
| 1107 |
{ determine storage type } |
| 1108 |
case aItem.CompressionMethod of |
| 1109 |
cmStored: begin |
| 1110 |
{ unstore aItem } |
| 1111 |
OutStream.CopyFrom(aInStream, aItem.UncompressedSize); |
| 1112 |
end; |
| 1113 |
cmDeflated: begin |
| 1114 |
{ inflate aItem } |
| 1115 |
DoInflate(aZipArchive, aItem, aInStream, OutStream); |
| 1116 |
end; |
| 1117 |
cmEnhancedDeflated: begin |
| 1118 |
{ inflate aItem } |
| 1119 |
DoInflate64(aZipArchive, aItem, aInStream, OutStream); |
| 1120 |
end; |
| 1121 |
{$IFDEF UnzipBzip2Support} |
| 1122 |
cmBzip2: begin |
| 1123 |
DoExtractBzip2(aZipArchive, aItem, aInStream, OutStream); |
| 1124 |
end; |
| 1125 |
{$ENDIF} |
| 1126 |
{$IFDEF UnzipLzmaSupport} |
| 1127 |
cmLZMA: begin |
| 1128 |
DoExtractLzma(aZipArchive, aItem, aInStream, OutStream); |
| 1129 |
end; |
| 1130 |
{$ENDIF} |
| 1131 |
{$IFDEF UnzipPPMdSupport} |
| 1132 |
cmPPMd: begin |
| 1133 |
DecompressPPMd(aInStream, OutStream); |
| 1134 |
end; |
| 1135 |
{$ENDIF} |
| 1136 |
{$IFDEF UnzipWavPackSupport} |
| 1137 |
cmWavPack: begin |
| 1138 |
DecompressWavPack(aInStream, OutStream); |
| 1139 |
end; |
| 1140 |
{$ENDIF} |
| 1141 |
{$IFDEF UnzipXzSupport} |
| 1142 |
cmXz: begin |
| 1143 |
DoExtractXz(aZipArchive, aItem, aInStream, OutStream); |
| 1144 |
end; |
| 1145 |
{$ENDIF} |
| 1146 |
{$IFDEF UnzipZstdSupport} |
| 1147 |
cmZstd: begin |
| 1148 |
DoExtractZstd(aZipArchive, aItem, aInStream, OutStream); |
| 1149 |
end; |
| 1150 |
{$ENDIF} |
| 1151 |
cmShrunk..cmImploded: begin |
| 1152 |
DoLegacyUnzip(aZipArchive, aItem, aInStream, OutStream); |
| 1153 |
end; |
| 1154 |
else |
| 1155 |
raise EAbZipInvalidMethod.Create; |
| 1156 |
end; |
| 1157 |
|
| 1158 |
{ check CRC } |
| 1159 |
if not (aInStream is TAbWinZipAesDecryptStream) then |
| 1160 |
Wrong := (OutStream.CRC32 <> aItem.CRC32) |
| 1161 |
else begin |
| 1162 |
Wrong := not TAbWinZipAesDecryptStream(aInStream).Verify; |
| 1163 |
if TAbWinZipAesDecryptStream(aInStream).ExtraField.Version = 1 then |
| 1164 |
Wrong := Wrong or (OutStream.CRC32 <> aItem.CRC32); |
| 1165 |
end; |
| 1166 |
if Wrong then |
| 1167 |
begin |
| 1168 |
if Assigned(aZipArchive.OnProcessItemFailure) then |
| 1169 |
aZipArchive.OnProcessItemFailure(aZipArchive, aItem, ptExtract, |
| 1170 |
ecAbbrevia, AbZipBadCRC) |
| 1171 |
else |
| 1172 |
raise EAbZipBadCRC.Create; |
| 1173 |
end; |
| 1174 |
finally |
| 1175 |
OutStream.Free; |
| 1176 |
end; |
| 1177 |
end; |
| 1178 |
{ -------------------------------------------------------------------------- } |
| 1179 |
procedure AbUnzipToStream( Sender : TObject; Item : TAbZipItem; OutStream : TStream); |
| 1180 |
var |
| 1181 |
ZipArchive : TAbZipArchive; |
| 1182 |
InStream : TStream; |
| 1183 |
begin |
| 1184 |
ZipArchive := Sender as TAbZipArchive; |
| 1185 |
if not Assigned(OutStream) then |
| 1186 |
raise EAbBadStream.Create; |
| 1187 |
|
| 1188 |
InStream := ExtractPrep(ZipArchive, Item); |
| 1189 |
try |
| 1190 |
DoExtract(ZipArchive, Item, InStream, OutStream); |
| 1191 |
finally |
| 1192 |
InStream.Free |
| 1193 |
end; |
| 1194 |
end; |
| 1195 |
{ -------------------------------------------------------------------------- } |
| 1196 |
procedure AbUnzip(Sender : TObject; Item : TAbZipItem; const UseName : string); |
| 1197 |
{create the output filestream and pass it to DoExtract} |
| 1198 |
var |
| 1199 |
LinkTarget : String; |
| 1200 |
PathType : TPathType; |
| 1201 |
AbsolutePath : String; |
| 1202 |
ZipArchive : TAbZipArchive; |
| 1203 |
InStream, OutStream : TStream; |
| 1204 |
begin |
| 1205 |
ZipArchive := TAbZipArchive(Sender); |
| 1206 |
|
| 1207 |
if Item.IsDirectory then |
| 1208 |
AbCreateDirectory(UseName) |
| 1209 |
else begin |
| 1210 |
InStream := ExtractPrep(ZipArchive, Item); |
| 1211 |
try |
| 1212 |
if FPS_ISLNK(Item.NativeFileAttributes) then |
| 1213 |
begin |
| 1214 |
OutStream := TMemoryStream.Create; |
| 1215 |
try |
| 1216 |
try {OutStream} |
| 1217 |
DoExtract(ZipArchive, Item, InStream, OutStream); |
| 1218 |
SetString(LinkTarget, TMemoryStream(OutStream).Memory, OutStream.Size); |
| 1219 |
LinkTarget := NormalizePathDelimiters(CeRawToUtf8(LinkTarget)); |
| 1220 |
finally {OutStream} |
| 1221 |
OutStream.Free; |
| 1222 |
end; {OutStream} |
| 1223 |
|
| 1224 |
PathType := GetPathType(LinkTarget); |
| 1225 |
if PathType in [ptRelative, ptAbsolute] then |
| 1226 |
begin |
| 1227 |
if PathType = ptAbsolute then |
| 1228 |
AbsolutePath := LinkTarget |
| 1229 |
else begin |
| 1230 |
AbsolutePath := GetAbsoluteFileName(ExtractFilePath(UseName), LinkTarget); |
| 1231 |
end; |
| 1232 |
if not IsInPath(ZipArchive.BaseDirectory, AbsolutePath, True, True) then |
| 1233 |
ZipArchive.SuspiciousLinks.Add(NormalizePathDelimiters(Item.FileName)); |
| 1234 |
end; |
| 1235 |
|
| 1236 |
if not CreateSymLink(LinkTarget, UseName, UInt32(Item.NativeFileAttributes)) then |
| 1237 |
RaiseLastOSError; |
| 1238 |
except |
| 1239 |
if ExceptObject is EAbUserAbort then |
| 1240 |
ZipArchive.FStatus := asInvalid; |
| 1241 |
raise; |
| 1242 |
end; |
| 1243 |
end |
| 1244 |
else begin |
| 1245 |
ZipArchive.VerifyItem(Item); |
| 1246 |
OutStream := TFileStreamEx.Create(UseName, fmCreate or fmShareDenyWrite); |
| 1247 |
try |
| 1248 |
try {OutStream} |
| 1249 |
DoExtract(ZipArchive, Item, InStream, OutStream); |
| 1250 |
finally {OutStream} |
| 1251 |
OutStream.Free; |
| 1252 |
end; {OutStream} |
| 1253 |
except |
| 1254 |
if ExceptObject is EAbUserAbort then |
| 1255 |
ZipArchive.FStatus := asInvalid; |
| 1256 |
DeleteFile(UseName); |
| 1257 |
raise; |
| 1258 |
end; |
| 1259 |
end; |
| 1260 |
finally |
| 1261 |
InStream.Free |
| 1262 |
end; |
| 1263 |
end; |
| 1264 |
if not FPS_ISLNK(Item.NativeFileAttributes) then |
| 1265 |
begin |
| 1266 |
AbSetFileTime(UseName, Item.LastModTimeAsDateTime); |
| 1267 |
AbSetFileAttr(UseName, Item.NativeFileAttributes); |
| 1268 |
end; |
| 1269 |
end; |
| 1270 |
{ -------------------------------------------------------------------------- } |
| 1271 |
procedure AbTestZipItem(Sender : TObject; Item : TAbZipItem); |
| 1272 |
{extract item to bit bucket and verify its local file header} |
| 1273 |
var |
| 1274 |
BitBucket : TAbBitBucketStream; |
| 1275 |
FieldSize : Word; |
| 1276 |
LFH : TAbZipLocalFileHeader; |
| 1277 |
Zip64Field : PZip64LocalHeaderRec; |
| 1278 |
ZipArchive : TAbZipArchive; |
| 1279 |
DD : TAbZipDataDescriptor = nil; |
| 1280 |
begin |
| 1281 |
ZipArchive := TAbZipArchive(Sender); |
| 1282 |
|
| 1283 |
if (Lo(Item.VersionNeededToExtract) > Ab_ZipVersion) then |
| 1284 |
raise EAbZipVersion.Create; |
| 1285 |
|
| 1286 |
{ seek to compressed file } |
| 1287 |
if ZipArchive.FStream is TAbSpanReadStream then |
| 1288 |
TAbSpanReadStream(ZipArchive.FStream).SeekImage(Item.DiskNumberStart, |
| 1289 |
Item.RelativeOffset) |
| 1290 |
else |
| 1291 |
ZipArchive.FStream.Position := Item.RelativeOffset; |
| 1292 |
|
| 1293 |
BitBucket := nil; |
| 1294 |
LFH := nil; |
| 1295 |
try |
| 1296 |
BitBucket := TAbBitBucketStream.Create(0); |
| 1297 |
LFH := TAbZipLocalFileHeader.Create; |
| 1298 |
{get the item's local file header} |
| 1299 |
ZipArchive.FStream.Seek(Item.RelativeOffset, soBeginning); |
| 1300 |
LFH.LoadFromStream(ZipArchive.FStream); |
| 1301 |
if LFH.HasDataDescriptor then |
| 1302 |
begin |
| 1303 |
DD := TAbZipDataDescriptor.Create; |
| 1304 |
ZipArchive.FStream.Seek(Item.CompressedSize, soCurrent); |
| 1305 |
DD.LoadFromStream(ZipArchive.FStream); |
| 1306 |
end; |
| 1307 |
ZipArchive.FStream.Seek(Item.RelativeOffset, soBeginning); |
| 1308 |
|
| 1309 |
{currently a single exception is raised for any LFH error} |
| 1310 |
if (LFH.VersionNeededToExtract <> Item.VersionNeededToExtract) then |
| 1311 |
raise EAbZipInvalidLFH.Create; |
| 1312 |
if (LFH.GeneralPurposeBitFlag <> Item.GeneralPurposeBitFlag) then |
| 1313 |
raise EAbZipInvalidLFH.Create; |
| 1314 |
if (LFH.LastModFileTime <> Item.LastModFileTime) then |
| 1315 |
raise EAbZipInvalidLFH.Create; |
| 1316 |
if (LFH.LastModFileDate <> Item.LastModFileDate) then |
| 1317 |
raise EAbZipInvalidLFH.Create; |
| 1318 |
if Assigned(DD) then |
| 1319 |
begin |
| 1320 |
if (DD.CRC32 <> Item.CRC32) then |
| 1321 |
raise EAbZipInvalidLFH.Create; |
| 1322 |
end |
| 1323 |
else begin |
| 1324 |
if (LFH.CRC32 <> Item.CRC32) then |
| 1325 |
raise EAbZipInvalidLFH.Create; |
| 1326 |
end; |
| 1327 |
if LFH.ExtraField.Get(Ab_Zip64SubfieldID, Pointer(Zip64Field), FieldSize) then begin |
| 1328 |
if (Zip64Field.CompressedSize <> Item.CompressedSize) then |
| 1329 |
raise EAbZipInvalidLFH.Create; |
| 1330 |
if (Zip64Field.UncompressedSize <> Item.UncompressedSize) then |
| 1331 |
raise EAbZipInvalidLFH.Create; |
| 1332 |
end |
| 1333 |
else if Assigned(DD) then |
| 1334 |
begin |
| 1335 |
if (DD.CompressedSize <> Item.CompressedSize) then |
| 1336 |
raise EAbZipInvalidLFH.Create; |
| 1337 |
if (DD.UncompressedSize <> Item.UncompressedSize) then |
| 1338 |
raise EAbZipInvalidLFH.Create; |
| 1339 |
end |
| 1340 |
else begin |
| 1341 |
if (LFH.CompressedSize <> Item.CompressedSize) then |
| 1342 |
raise EAbZipInvalidLFH.Create; |
| 1343 |
if (LFH.UncompressedSize <> Item.UncompressedSize) then |
| 1344 |
raise EAbZipInvalidLFH.Create; |
| 1345 |
end; |
| 1346 |
if (LFH.FileName <> Item.RawFileName) then |
| 1347 |
raise EAbZipInvalidLFH.Create; |
| 1348 |
|
| 1349 |
{any CRC errors will raise exception during extraction} |
| 1350 |
AbUnZipToStream(Sender, Item, BitBucket); |
| 1351 |
finally |
| 1352 |
BitBucket.Free; |
| 1353 |
LFH.Free; |
| 1354 |
DD.Free; |
| 1355 |
end; |
| 1356 |
|
| 1357 |
end; |
| 1358 |
{ -------------------------------------------------------------------------- } |
| 1359 |
procedure InflateStream( CompressedStream, UnCompressedStream : TStream ); |
| 1360 |
{-Inflates everything in CompressedStream to UncompressedStream |
| 1361 |
no encryption is tried, no check on CRC is done, uses the whole |
| 1362 |
compressedstream - no Progress events - no Frills!} |
| 1363 |
begin |
| 1364 |
Inflate(CompressedStream, UncompressedStream, nil); |
| 1365 |
end; |
| 1366 |
|
| 1367 |
end. |
| 1368 |
|