| 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 |
* Joel Haynie |
| 18 |
* Craig Peterson |
| 19 |
* |
| 20 |
* Portions created by the Initial Developer are Copyright (C) 1997-2002 |
| 21 |
* the Initial Developer. All Rights Reserved. |
| 22 |
* |
| 23 |
* Contributor(s): |
| 24 |
* Alexander Koblov <[email protected]> |
| 25 |
* |
| 26 |
* ***** END LICENSE BLOCK ***** *) |
| 27 |
|
| 28 |
{*********************************************************} |
| 29 |
{* ABBREVIA: AbLzmaTyp.pas *} |
| 30 |
{*********************************************************} |
| 31 |
{* ABBREVIA: TAbLzmaArchive, TAbLzmaItem classes *} |
| 32 |
{*********************************************************} |
| 33 |
{* Misc. constants, types, and routines for working *} |
| 34 |
{* with Lzma files *} |
| 35 |
{*********************************************************} |
| 36 |
|
| 37 |
unit AbLzmaTyp; |
| 38 |
|
| 39 |
{$I AbDefine.inc} |
| 40 |
|
| 41 |
interface |
| 42 |
|
| 43 |
uses |
| 44 |
Classes, |
| 45 |
AbArcTyp, AbTarTyp, AbUtils; |
| 46 |
|
| 47 |
type |
| 48 |
PAbLzmaHeader = ^TAbLzmaHeader; { File Header } |
| 49 |
TAbLzmaHeader = packed record { SizeOf(TAbLzmaHeader) = 13 } |
| 50 |
Properties: array[0..4] of Byte; { LZMA properties } |
| 51 |
UncompressedSize : Int64; { Uncompressed size } |
| 52 |
end; |
| 53 |
|
| 54 |
{ The Purpose for this Item is the placeholder for aaAdd and aaDelete Support. } |
| 55 |
{ For all intents and purposes we could just use a TAbArchiveItem } |
| 56 |
type |
| 57 |
TAbLzmaItem = class(TabArchiveItem); |
| 58 |
|
| 59 |
TAbLzmaArchiveState = (gsLzma, gsTar); |
| 60 |
|
| 61 |
{ TAbLzmaArchive } |
| 62 |
|
| 63 |
TAbLzmaArchive = class(TAbTarArchive) |
| 64 |
private |
| 65 |
FLzmaStream : TStream; { stream for Lzma file} |
| 66 |
FLzmaItem : TAbArchiveList; { item in lzma (only one, but need polymorphism of class)} |
| 67 |
FTarStream : TStream; { stream for possible contained Tar } |
| 68 |
FTarList : TAbArchiveList; { items in possible contained Tar } |
| 69 |
FTarAutoHandle: Boolean; |
| 70 |
FState : TAbLzmaArchiveState; |
| 71 |
FIsLzmaTar : Boolean; |
| 72 |
|
| 73 |
procedure CompressFromStream(aStream: TStream); |
| 74 |
procedure DecompressToStream(aStream: TStream); |
| 75 |
procedure SetTarAutoHandle(const Value: Boolean); |
| 76 |
procedure SwapToLzma; |
| 77 |
procedure SwapToTar; |
| 78 |
|
| 79 |
protected |
| 80 |
{ Inherited Abstract functions } |
| 81 |
function CreateItem(const SourceFileName : string; |
| 82 |
const ArchiveDirectory : string): TAbArchiveItem; override; |
| 83 |
procedure ExtractItemAt(Index : Integer; const NewName : string); override; |
| 84 |
procedure ExtractItemToStreamAt(Index : Integer; aStream : TStream); override; |
| 85 |
procedure LoadArchive; override; |
| 86 |
procedure SaveArchive; override; |
| 87 |
procedure TestItemAt(Index : Integer); override; |
| 88 |
function GetSupportsEmptyFolders : Boolean; override; |
| 89 |
|
| 90 |
public {methods} |
| 91 |
constructor CreateFromStream(aStream : TStream; const aArchiveName : string); override; |
| 92 |
destructor Destroy; override; |
| 93 |
|
| 94 |
procedure DoSpanningMediaRequest(Sender : TObject; ImageNumber : Integer; |
| 95 |
var ImageName : string; var Abort : Boolean); override; |
| 96 |
|
| 97 |
{ Properties } |
| 98 |
property TarAutoHandle : Boolean |
| 99 |
read FTarAutoHandle write SetTarAutoHandle; |
| 100 |
|
| 101 |
property IsLzmaTar : Boolean |
| 102 |
read FIsLzmaTar write FIsLzmaTar; |
| 103 |
end; |
| 104 |
|
| 105 |
function VerifyLzma(Strm : TStream) : TAbArchiveType; |
| 106 |
|
| 107 |
implementation |
| 108 |
|
| 109 |
uses |
| 110 |
StrUtils, SysUtils, Math, |
| 111 |
AbExcept, AbVMStrm, AbBitBkt, ULZMADecoder, ULZMAEncoder, DCOSUtils, DCClassesUtf8; |
| 112 |
|
| 113 |
{ ****************** Helper functions Not from Classes Above ***************** } |
| 114 |
function VerifyLzma(Strm : TStream) : TAbArchiveType; |
| 115 |
var |
| 116 |
CurPos : Int64; |
| 117 |
TarStream: TStream; |
| 118 |
Hdr : TAbLzmaHeader; |
| 119 |
UncompressedSize: Int64; |
| 120 |
DecompStream: TLZMADecoder; |
| 121 |
begin |
| 122 |
Result := atUnknown; |
| 123 |
|
| 124 |
CurPos := Strm.Position; |
| 125 |
Strm.Seek(0, soBeginning); |
| 126 |
|
| 127 |
try |
| 128 |
if Strm.Read(Hdr, SizeOf(Hdr)) = SizeOf(Hdr) then |
| 129 |
begin |
| 130 |
TarStream := TMemoryStream.Create; |
| 131 |
try |
| 132 |
DecompStream := TLZMADecoder.Create; |
| 133 |
try |
| 134 |
if Hdr.UncompressedSize <> -1 then |
| 135 |
UncompressedSize:= Min(AB_TAR_RECORDSIZE * 4, Hdr.UncompressedSize) |
| 136 |
else if Strm.Size < AB_TAR_RECORDSIZE * 8 then |
| 137 |
UncompressedSize:= -1 |
| 138 |
else begin |
| 139 |
UncompressedSize:= AB_TAR_RECORDSIZE * 4; |
| 140 |
end; |
| 141 |
if DecompStream.SetDecoderProperties(Hdr.Properties) and |
| 142 |
DecompStream.Code(Strm, TarStream, UncompressedSize) then |
| 143 |
begin |
| 144 |
Result := atLzma; |
| 145 |
{ Check for embedded TAR } |
| 146 |
TarStream.Seek(0, soBeginning); |
| 147 |
if VerifyTar(TarStream) = atTar then |
| 148 |
Result := atLzmaTar; |
| 149 |
end; |
| 150 |
finally |
| 151 |
DecompStream.Free; |
| 152 |
end; |
| 153 |
finally |
| 154 |
TarStream.Free; |
| 155 |
end; |
| 156 |
end; |
| 157 |
except |
| 158 |
on EReadError do |
| 159 |
Result := atUnknown; |
| 160 |
end; |
| 161 |
Strm.Position := CurPos; { Return to original position. } |
| 162 |
end; |
| 163 |
|
| 164 |
|
| 165 |
{ ****************************** TAbLzmaArchive ***************************** } |
| 166 |
constructor TAbLzmaArchive.CreateFromStream(aStream: TStream; |
| 167 |
const aArchiveName: string); |
| 168 |
begin |
| 169 |
inherited CreateFromStream(aStream, aArchiveName); |
| 170 |
FState := gsLzma; |
| 171 |
FLzmaStream := FStream; |
| 172 |
FLzmaItem := FItemList; |
| 173 |
FTarStream := TAbVirtualMemoryStream.Create; |
| 174 |
FTarList := TAbArchiveList.Create(True); |
| 175 |
end; |
| 176 |
{ -------------------------------------------------------------------------- } |
| 177 |
procedure TAbLzmaArchive.SwapToTar; |
| 178 |
begin |
| 179 |
FStream := FTarStream; |
| 180 |
FItemList := FTarList; |
| 181 |
FState := gsTar; |
| 182 |
end; |
| 183 |
{ -------------------------------------------------------------------------- } |
| 184 |
procedure TAbLzmaArchive.SwapToLzma; |
| 185 |
begin |
| 186 |
FStream := FLzmaStream; |
| 187 |
FItemList := FLzmaItem; |
| 188 |
FState := gsLzma; |
| 189 |
end; |
| 190 |
{ -------------------------------------------------------------------------- } |
| 191 |
function TAbLzmaArchive.CreateItem(const SourceFileName : string; |
| 192 |
const ArchiveDirectory : string): TAbArchiveItem; |
| 193 |
var |
| 194 |
LzmaItem : TAbLzmaItem; |
| 195 |
FullSourceFileName, FullArchiveFileName: String; |
| 196 |
begin |
| 197 |
if IsLzmaTar and TarAutoHandle then begin |
| 198 |
SwapToTar; |
| 199 |
Result := inherited CreateItem(SourceFileName, ArchiveDirectory); |
| 200 |
end |
| 201 |
else begin |
| 202 |
SwapToLzma; |
| 203 |
LzmaItem := TAbLzmaItem.Create; |
| 204 |
try |
| 205 |
MakeFullNames(SourceFileName, ArchiveDirectory, |
| 206 |
FullSourceFileName, FullArchiveFileName); |
| 207 |
|
| 208 |
LzmaItem.FileName := FullArchiveFileName; |
| 209 |
LzmaItem.DiskFileName := FullSourceFileName; |
| 210 |
|
| 211 |
Result := LzmaItem; |
| 212 |
except |
| 213 |
Result := nil; |
| 214 |
raise; |
| 215 |
end; |
| 216 |
end; |
| 217 |
end; |
| 218 |
{ -------------------------------------------------------------------------- } |
| 219 |
destructor TAbLzmaArchive.Destroy; |
| 220 |
begin |
| 221 |
SwapToLzma; |
| 222 |
FTarList.Free; |
| 223 |
FTarStream.Free; |
| 224 |
inherited Destroy; |
| 225 |
end; |
| 226 |
{ -------------------------------------------------------------------------- } |
| 227 |
procedure TAbLzmaArchive.ExtractItemAt(Index: Integer; |
| 228 |
const NewName: string); |
| 229 |
var |
| 230 |
OutStream : TStream; |
| 231 |
begin |
| 232 |
if IsLzmaTar and TarAutoHandle then begin |
| 233 |
SwapToTar; |
| 234 |
inherited ExtractItemAt(Index, NewName); |
| 235 |
end |
| 236 |
else begin |
| 237 |
SwapToLzma; |
| 238 |
OutStream := TFileStreamEx.Create(NewName, fmCreate or fmShareDenyNone); |
| 239 |
try |
| 240 |
try |
| 241 |
ExtractItemToStreamAt(Index, OutStream); |
| 242 |
finally |
| 243 |
OutStream.Free; |
| 244 |
end; |
| 245 |
{ Lzma doesn't store the last modified time or attributes, so don't set them } |
| 246 |
except |
| 247 |
on E : EAbUserAbort do begin |
| 248 |
FStatus := asInvalid; |
| 249 |
if mbFileExists(NewName) then |
| 250 |
mbDeleteFile(NewName); |
| 251 |
raise; |
| 252 |
end else begin |
| 253 |
if mbFileExists(NewName) then |
| 254 |
mbDeleteFile(NewName); |
| 255 |
raise; |
| 256 |
end; |
| 257 |
end; |
| 258 |
end; |
| 259 |
end; |
| 260 |
{ -------------------------------------------------------------------------- } |
| 261 |
procedure TAbLzmaArchive.ExtractItemToStreamAt(Index: Integer; |
| 262 |
aStream: TStream); |
| 263 |
begin |
| 264 |
if IsLzmaTar and TarAutoHandle then begin |
| 265 |
SwapToTar; |
| 266 |
inherited ExtractItemToStreamAt(Index, aStream); |
| 267 |
end |
| 268 |
else begin |
| 269 |
SwapToLzma; |
| 270 |
{ Index ignored as there's only one item in a Lzma } |
| 271 |
DecompressToStream(aStream); |
| 272 |
end; |
| 273 |
end; |
| 274 |
{ -------------------------------------------------------------------------- } |
| 275 |
function TAbLzmaArchive.GetSupportsEmptyFolders : Boolean; |
| 276 |
begin |
| 277 |
Result := IsLzmaTar and TarAutoHandle; |
| 278 |
end; |
| 279 |
{ -------------------------------------------------------------------------- } |
| 280 |
procedure TAbLzmaArchive.LoadArchive; |
| 281 |
var |
| 282 |
Item: TAbLzmaItem; |
| 283 |
Abort: Boolean; |
| 284 |
ItemName: string; |
| 285 |
Header: TAbLzmaHeader; |
| 286 |
begin |
| 287 |
if FLzmaStream.Size = 0 then |
| 288 |
Exit; |
| 289 |
|
| 290 |
if IsLzmaTar and TarAutoHandle then begin |
| 291 |
{ Decompress and send to tar LoadArchive } |
| 292 |
DecompressToStream(FTarStream); |
| 293 |
SwapToTar; |
| 294 |
inherited LoadArchive; |
| 295 |
end |
| 296 |
else begin |
| 297 |
SwapToLzma; |
| 298 |
FStream.Read(Header, SizeOf(Header)); |
| 299 |
Item := TAbLzmaItem.Create; |
| 300 |
Item.Action := aaNone; |
| 301 |
if Header.UncompressedSize <> -1 then |
| 302 |
Item.UncompressedSize := Header.UncompressedSize; |
| 303 |
{ Filename isn't stored, so constuct one based on the archive name } |
| 304 |
ItemName := ExtractFileName(ArchiveName); |
| 305 |
if ItemName = '' then |
| 306 |
Item.FileName := 'unknown' |
| 307 |
else if AnsiEndsText('.tlz', ItemName) then |
| 308 |
Item.FileName := ChangeFileExt(ItemName, '.tar') |
| 309 |
else |
| 310 |
Item.FileName := ChangeFileExt(ItemName, ''); |
| 311 |
Item.DiskFileName := Item.FileName; |
| 312 |
FItemList.Add(Item); |
| 313 |
end; |
| 314 |
DoArchiveProgress(100, Abort); |
| 315 |
FIsDirty := False; |
| 316 |
end; |
| 317 |
{ -------------------------------------------------------------------------- } |
| 318 |
procedure TAbLzmaArchive.SaveArchive; |
| 319 |
var |
| 320 |
I: Integer; |
| 321 |
CurItem: TAbLzmaItem; |
| 322 |
UpdateArchive: Boolean; |
| 323 |
TempFileName: String; |
| 324 |
InputFileStream: TStream; |
| 325 |
begin |
| 326 |
if IsLzmaTar and TarAutoHandle then |
| 327 |
begin |
| 328 |
SwapToTar; |
| 329 |
inherited SaveArchive; |
| 330 |
UpdateArchive := (FLzmaStream.Size > 0) and (FLzmaStream is TFileStreamEx); |
| 331 |
if UpdateArchive then |
| 332 |
begin |
| 333 |
FreeAndNil(FLzmaStream); |
| 334 |
TempFileName := GetTempName(FArchiveName); |
| 335 |
{ Create new archive with temporary name } |
| 336 |
FLzmaStream := TFileStreamEx.Create(TempFileName, fmCreate or fmShareDenyWrite); |
| 337 |
end; |
| 338 |
FTarStream.Position := 0; |
| 339 |
CompressFromStream(FTarStream); |
| 340 |
if UpdateArchive then |
| 341 |
begin |
| 342 |
FreeAndNil(FLzmaStream); |
| 343 |
{ Replace original by new archive } |
| 344 |
if not (mbDeleteFile(FArchiveName) and mbRenameFile(TempFileName, FArchiveName)) then |
| 345 |
RaiseLastOSError; |
| 346 |
{ Open new archive } |
| 347 |
FLzmaStream := TFileStreamEx.Create(FArchiveName, fmOpenRead or fmShareDenyNone); |
| 348 |
end; |
| 349 |
end |
| 350 |
else begin |
| 351 |
{ Things we know: There is only one file per archive.} |
| 352 |
{ Actions we have to address in SaveArchive: } |
| 353 |
{ aaNone & aaMove do nothing, as the file does not change, only the meta data } |
| 354 |
{ aaDelete could make a zero size file unless there are two files in the list.} |
| 355 |
{ aaAdd, aaStreamAdd, aaFreshen, & aaReplace will be the only ones to take action. } |
| 356 |
SwapToLzma; |
| 357 |
for I := 0 to Pred(Count) do begin |
| 358 |
FCurrentItem := ItemList[I]; |
| 359 |
CurItem := TAbLzmaItem(ItemList[I]); |
| 360 |
case CurItem.Action of |
| 361 |
aaNone, aaMove: Break;{ Do nothing; lzma doesn't store metadata } |
| 362 |
aaDelete: ; {doing nothing omits file from new stream} |
| 363 |
aaAdd, aaFreshen, aaReplace, aaStreamAdd: begin |
| 364 |
FLzmaStream.Size := 0; |
| 365 |
if CurItem.Action = aaStreamAdd then |
| 366 |
begin |
| 367 |
CompressFromStream(InStream); { Copy/compress entire Instream to FLzmaStream } |
| 368 |
end |
| 369 |
else begin |
| 370 |
InputFileStream := TFileStreamEx.Create(CurItem.DiskFileName, fmOpenRead or fmShareDenyWrite); |
| 371 |
try |
| 372 |
CompressFromStream(InputFileStream); { Copy/compress entire Instream to FLzmaStream } |
| 373 |
finally |
| 374 |
InputFileStream.Free; |
| 375 |
end; |
| 376 |
end; |
| 377 |
Break; |
| 378 |
end; { End aaAdd, aaFreshen, aaReplace, & aaStreamAdd } |
| 379 |
end; { End of CurItem.Action Case } |
| 380 |
end; { End Item for loop } |
| 381 |
end; { End Tar Else } |
| 382 |
end; |
| 383 |
{ -------------------------------------------------------------------------- } |
| 384 |
procedure TAbLzmaArchive.SetTarAutoHandle(const Value: Boolean); |
| 385 |
begin |
| 386 |
if Value then |
| 387 |
SwapToTar |
| 388 |
else |
| 389 |
SwapToLzma; |
| 390 |
FTarAutoHandle := Value; |
| 391 |
end; |
| 392 |
{ -------------------------------------------------------------------------- } |
| 393 |
procedure TAbLzmaArchive.CompressFromStream(aStream: TStream); |
| 394 |
var |
| 395 |
Encoder: TLZMAEncoder; |
| 396 |
begin |
| 397 |
Encoder := TLZMAEncoder.Create; |
| 398 |
try |
| 399 |
Encoder.WriteCoderProperties(FLzmaStream); |
| 400 |
FLzmaStream.WriteQWord(NToLE(aStream.Size)); |
| 401 |
Encoder.Code(aStream, FLzmaStream, -1, -1); |
| 402 |
finally |
| 403 |
Encoder.Free; |
| 404 |
end; |
| 405 |
end; |
| 406 |
{ -------------------------------------------------------------------------- } |
| 407 |
procedure TAbLzmaArchive.DecompressToStream(aStream: TStream); |
| 408 |
var |
| 409 |
Header: TAbLzmaHeader; |
| 410 |
Decoder: TLZMADecoder; |
| 411 |
begin |
| 412 |
FLzmaStream.Seek(0, soBeginning); |
| 413 |
if FLzmaStream.Read(Header, SizeOf(Header)) = SizeOf(Header) then |
| 414 |
begin |
| 415 |
Decoder := TLZMADecoder.Create; |
| 416 |
try |
| 417 |
if Decoder.SetDecoderProperties(Header.Properties) and |
| 418 |
Decoder.Code(FLzmaStream, aStream, Header.UncompressedSize) then |
| 419 |
begin |
| 420 |
Exit; { Success } |
| 421 |
end; |
| 422 |
finally |
| 423 |
Decoder.Free; |
| 424 |
end; |
| 425 |
end; |
| 426 |
raise EAbUnhandledType.Create; |
| 427 |
end; |
| 428 |
{ -------------------------------------------------------------------------- } |
| 429 |
procedure TAbLzmaArchive.TestItemAt(Index: Integer); |
| 430 |
var |
| 431 |
LzmaType: TAbArchiveType; |
| 432 |
BitBucket: TAbBitBucketStream; |
| 433 |
begin |
| 434 |
if IsLzmaTar and TarAutoHandle then begin |
| 435 |
SwapToTar; |
| 436 |
inherited TestItemAt(Index); |
| 437 |
end |
| 438 |
else begin |
| 439 |
{ Note Index ignored as there's only one item in a GZip } |
| 440 |
LzmaType := VerifyLzma(FLzmaStream); |
| 441 |
if not (LzmaType in [atLzma, atLzmaTar]) then |
| 442 |
raise EAbGzipInvalid.Create; // TODO: Add lzma-specific exceptions } |
| 443 |
BitBucket := TAbBitBucketStream.Create(1024); |
| 444 |
try |
| 445 |
DecompressToStream(BitBucket); |
| 446 |
finally |
| 447 |
BitBucket.Free; |
| 448 |
end; |
| 449 |
end; |
| 450 |
end; |
| 451 |
{ -------------------------------------------------------------------------- } |
| 452 |
procedure TAbLzmaArchive.DoSpanningMediaRequest(Sender: TObject; |
| 453 |
ImageNumber: Integer; var ImageName: string; var Abort: Boolean); |
| 454 |
begin |
| 455 |
Abort := False; |
| 456 |
end; |
| 457 |
|
| 458 |
end. |