| 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 |
* |
| 25 |
* ***** END LICENSE BLOCK ***** *) |
| 26 |
|
| 27 |
{*********************************************************} |
| 28 |
{* ABBREVIA: AbBzip2Typ.pas *} |
| 29 |
{*********************************************************} |
| 30 |
{* ABBREVIA: TAbBzip2Archive, TAbBzip2Item classes *} |
| 31 |
{*********************************************************} |
| 32 |
{* Misc. constants, types, and routines for working *} |
| 33 |
{* with Bzip2 files *} |
| 34 |
{*********************************************************} |
| 35 |
|
| 36 |
unit AbBzip2Typ; |
| 37 |
|
| 38 |
{$I AbDefine.inc} |
| 39 |
|
| 40 |
interface |
| 41 |
|
| 42 |
uses |
| 43 |
Classes, |
| 44 |
AbArcTyp, AbTarTyp, AbUtils; |
| 45 |
|
| 46 |
const |
| 47 |
{ Default Stream Header for Bzip2s is 'BZhX', where X is the block size setting 1-9 in ASCII } |
| 48 |
{ Each block has the following header: '1AY&SY', and are in units of 100kilobytes NOT 100kibiBytes } |
| 49 |
AB_BZIP2_FILE_HEADER = 'BZh'; |
| 50 |
AB_BZIP2_BLOCK_SIZE = ['1','2','3','4','5','6','7','8','9']; |
| 51 |
AB_BZIP2_BLOCK_HEADER = '1AY&SY'; { Note: $314159265359, BCD for Pi :) } |
| 52 |
{ Note that Blocks are bit aligned, as such the only time you will "for sure" see |
| 53 |
the block header is on the start of stream/File } |
| 54 |
AB_BZIP2_FILE_TAIL =#23#114#36#83#133#9#0; { $1772245385090, BCD for sqrt(Pi) :) } |
| 55 |
{ This is odd as the blocks are bit allgned so this is a string that is 13*4 bits = 52 bits } |
| 56 |
|
| 57 |
type |
| 58 |
PAbBzip2Header = ^TAbBzip2Header; { File Header } |
| 59 |
TAbBzip2Header = packed record { SizeOf(TAbBzip2Header) = 10 } |
| 60 |
FileHeader : array[0..2] of AnsiChar;{ 'BZh'; $42,5A,68 } |
| 61 |
BlockSize : AnsiChar; { '1'..'9'; $31-$39 } |
| 62 |
BlockHeader : array[0..5] of AnsiChar;{ '1AY&SY'; $31,41,59,26,53,59 } |
| 63 |
end; |
| 64 |
|
| 65 |
{ The Purpose for this Item is the placeholder for aaAdd and aaDelete Support. } |
| 66 |
{ For all intents and purposes we could just use a TAbArchiveItem } |
| 67 |
type |
| 68 |
TAbBzip2Item = class(TabArchiveItem); |
| 69 |
|
| 70 |
TAbBzip2ArchiveState = (gsBzip2, gsTar); |
| 71 |
|
| 72 |
TAbBzip2Archive = class(TAbTarArchive) |
| 73 |
private |
| 74 |
FBzip2Stream : TStream; { stream for Bzip2 file} |
| 75 |
FBzip2Item : TAbArchiveList; { item in bzip2 (only one, but need polymorphism of class)} |
| 76 |
FTarStream : TStream; { stream for possible contained Tar } |
| 77 |
FTarList : TAbArchiveList; { items in possible contained Tar } |
| 78 |
FTarAutoHandle: Boolean; |
| 79 |
FState : TAbBzip2ArchiveState; |
| 80 |
FIsBzippedTar : Boolean; |
| 81 |
|
| 82 |
procedure DecompressToStream(aStream: TStream); |
| 83 |
procedure SetTarAutoHandle(const Value: Boolean); |
| 84 |
procedure SwapToBzip2; |
| 85 |
procedure SwapToTar; |
| 86 |
|
| 87 |
protected |
| 88 |
{ Inherited Abstract functions } |
| 89 |
function CreateItem(const SourceFileName : string; |
| 90 |
const ArchiveDirectory : string): TAbArchiveItem; override; |
| 91 |
procedure ExtractItemAt(Index : Integer; const NewName : string); override; |
| 92 |
procedure ExtractItemToStreamAt(Index : Integer; aStream : TStream); override; |
| 93 |
procedure LoadArchive; override; |
| 94 |
procedure SaveArchive; override; |
| 95 |
procedure TestItemAt(Index : Integer); override; |
| 96 |
function GetSupportsEmptyFolders : Boolean; override; |
| 97 |
|
| 98 |
public {methods} |
| 99 |
constructor CreateFromStream(aStream : TStream; const aArchiveName : string); override; |
| 100 |
destructor Destroy; override; |
| 101 |
|
| 102 |
procedure DoSpanningMediaRequest(Sender : TObject; ImageNumber : Integer; |
| 103 |
var ImageName : string; var Abort : Boolean); override; |
| 104 |
|
| 105 |
{ Properties } |
| 106 |
property TarAutoHandle : Boolean |
| 107 |
read FTarAutoHandle write SetTarAutoHandle; |
| 108 |
|
| 109 |
property IsBzippedTar : Boolean |
| 110 |
read FIsBzippedTar write FIsBzippedTar; |
| 111 |
end; |
| 112 |
|
| 113 |
function VerifyBzip2(Strm : TStream) : TAbArchiveType; |
| 114 |
|
| 115 |
implementation |
| 116 |
|
| 117 |
uses |
| 118 |
{$IFDEF MSWINDOWS} |
| 119 |
Windows, // Fix inline warnings |
| 120 |
{$ENDIF} |
| 121 |
StrUtils, SysUtils, BufStream, |
| 122 |
AbBzip2, AbExcept, AbVMStrm, AbBitBkt, AbProgress, DCOSUtils, DCClassesUtf8; |
| 123 |
|
| 124 |
{ ****************** Helper functions Not from Classes Above ***************** } |
| 125 |
function VerifyHeader(const Header : TAbBzip2Header) : Boolean; |
| 126 |
begin |
| 127 |
Result := (Header.FileHeader = AB_BZIP2_FILE_HEADER) and |
| 128 |
(Header.BlockSize in AB_BZIP2_BLOCK_SIZE) and |
| 129 |
(Header.BlockHeader = AB_BZIP2_BLOCK_HEADER); |
| 130 |
end; |
| 131 |
{ -------------------------------------------------------------------------- } |
| 132 |
function VerifyBzip2(Strm : TStream) : TAbArchiveType; |
| 133 |
var |
| 134 |
Hdr : TAbBzip2Header; |
| 135 |
CurPos, DecompSize : Int64; |
| 136 |
DecompStream, TarStream: TStream; |
| 137 |
Buffer: array[0..Pred(AB_TAR_RECORDSIZE * 4)] of Byte; |
| 138 |
begin |
| 139 |
Result := atUnknown; |
| 140 |
|
| 141 |
CurPos := Strm.Position; |
| 142 |
Strm.Seek(0, soBeginning); |
| 143 |
|
| 144 |
try |
| 145 |
if (Strm.Read(Hdr, SizeOf(Hdr)) = SizeOf(Hdr)) and VerifyHeader(Hdr) then begin |
| 146 |
Result := atBzip2; |
| 147 |
{ Check for embedded TAR } |
| 148 |
Strm.Seek(0, soBeginning); |
| 149 |
DecompStream := TBZDecompressionStream.Create(Strm); |
| 150 |
try |
| 151 |
TarStream := TMemoryStream.Create; |
| 152 |
try |
| 153 |
DecompSize:= DecompStream.Read(Buffer, SizeOf(Buffer)); |
| 154 |
TarStream.Write(Buffer, DecompSize); |
| 155 |
TarStream.Seek(0, soBeginning); |
| 156 |
if VerifyTar(TarStream) = atTar then |
| 157 |
Result := atBzippedTar; |
| 158 |
finally |
| 159 |
TarStream.Free; |
| 160 |
end; |
| 161 |
finally |
| 162 |
DecompStream.Free; |
| 163 |
end; |
| 164 |
end; |
| 165 |
except |
| 166 |
on EReadError do |
| 167 |
Result := atUnknown; |
| 168 |
end; |
| 169 |
Strm.Position := CurPos; { Return to original position. } |
| 170 |
end; |
| 171 |
|
| 172 |
|
| 173 |
{ ****************************** TAbBzip2Archive ***************************** } |
| 174 |
constructor TAbBzip2Archive.CreateFromStream(aStream: TStream; |
| 175 |
const aArchiveName: string); |
| 176 |
begin |
| 177 |
inherited CreateFromStream(aStream, aArchiveName); |
| 178 |
FState := gsBzip2; |
| 179 |
FBzip2Stream := FStream; |
| 180 |
FBzip2Item := FItemList; |
| 181 |
FTarStream := TAbVirtualMemoryStream.Create; |
| 182 |
FTarList := TAbArchiveList.Create(True); |
| 183 |
end; |
| 184 |
{ -------------------------------------------------------------------------- } |
| 185 |
procedure TAbBzip2Archive.SwapToTar; |
| 186 |
begin |
| 187 |
FStream := FTarStream; |
| 188 |
FItemList := FTarList; |
| 189 |
FState := gsTar; |
| 190 |
end; |
| 191 |
{ -------------------------------------------------------------------------- } |
| 192 |
procedure TAbBzip2Archive.SwapToBzip2; |
| 193 |
begin |
| 194 |
FStream := FBzip2Stream; |
| 195 |
FItemList := FBzip2Item; |
| 196 |
FState := gsBzip2; |
| 197 |
end; |
| 198 |
{ -------------------------------------------------------------------------- } |
| 199 |
function TAbBzip2Archive.CreateItem(const SourceFileName : string; |
| 200 |
const ArchiveDirectory : string): TAbArchiveItem; |
| 201 |
var |
| 202 |
Bz2Item : TAbBzip2Item; |
| 203 |
FullSourceFileName, FullArchiveFileName: String; |
| 204 |
begin |
| 205 |
if IsBzippedTar and TarAutoHandle then begin |
| 206 |
SwapToTar; |
| 207 |
Result := inherited CreateItem(SourceFileName, ArchiveDirectory); |
| 208 |
end |
| 209 |
else begin |
| 210 |
SwapToBzip2; |
| 211 |
Bz2Item := TAbBzip2Item.Create; |
| 212 |
try |
| 213 |
MakeFullNames(SourceFileName, ArchiveDirectory, |
| 214 |
FullSourceFileName, FullArchiveFileName); |
| 215 |
|
| 216 |
Bz2Item.FileName := FullArchiveFileName; |
| 217 |
Bz2Item.DiskFileName := FullSourceFileName; |
| 218 |
|
| 219 |
Result := Bz2Item; |
| 220 |
except |
| 221 |
Result := nil; |
| 222 |
raise; |
| 223 |
end; |
| 224 |
end; |
| 225 |
end; |
| 226 |
{ -------------------------------------------------------------------------- } |
| 227 |
destructor TAbBzip2Archive.Destroy; |
| 228 |
begin |
| 229 |
SwapToBzip2; |
| 230 |
FTarList.Free; |
| 231 |
FTarStream.Free; |
| 232 |
inherited Destroy; |
| 233 |
end; |
| 234 |
{ -------------------------------------------------------------------------- } |
| 235 |
procedure TAbBzip2Archive.ExtractItemAt(Index: Integer; |
| 236 |
const NewName: string); |
| 237 |
var |
| 238 |
OutStream : TStream; |
| 239 |
begin |
| 240 |
if IsBzippedTar and TarAutoHandle then begin |
| 241 |
SwapToTar; |
| 242 |
inherited ExtractItemAt(Index, NewName); |
| 243 |
end |
| 244 |
else begin |
| 245 |
SwapToBzip2; |
| 246 |
OutStream := TFileStreamEx.Create(NewName, fmCreate or fmShareDenyNone); |
| 247 |
try |
| 248 |
try |
| 249 |
ExtractItemToStreamAt(Index, OutStream); |
| 250 |
finally |
| 251 |
OutStream.Free; |
| 252 |
end; |
| 253 |
{ Bz2 doesn't store the last modified time or attributes, so don't set them } |
| 254 |
except |
| 255 |
on E : EAbUserAbort do begin |
| 256 |
FStatus := asInvalid; |
| 257 |
if mbFileExists(NewName) then |
| 258 |
mbDeleteFile(NewName); |
| 259 |
raise; |
| 260 |
end else begin |
| 261 |
if mbFileExists(NewName) then |
| 262 |
mbDeleteFile(NewName); |
| 263 |
raise; |
| 264 |
end; |
| 265 |
end; |
| 266 |
end; |
| 267 |
end; |
| 268 |
{ -------------------------------------------------------------------------- } |
| 269 |
procedure TAbBzip2Archive.ExtractItemToStreamAt(Index: Integer; |
| 270 |
aStream: TStream); |
| 271 |
begin |
| 272 |
if IsBzippedTar and TarAutoHandle then begin |
| 273 |
SwapToTar; |
| 274 |
inherited ExtractItemToStreamAt(Index, aStream); |
| 275 |
end |
| 276 |
else begin |
| 277 |
SwapToBzip2; |
| 278 |
{ Index ignored as there's only one item in a Bz2 } |
| 279 |
DecompressToStream(aStream); |
| 280 |
end; |
| 281 |
end; |
| 282 |
{ -------------------------------------------------------------------------- } |
| 283 |
function TAbBzip2Archive.GetSupportsEmptyFolders : Boolean; |
| 284 |
begin |
| 285 |
Result := IsBzippedTar and TarAutoHandle; |
| 286 |
end; |
| 287 |
{ -------------------------------------------------------------------------- } |
| 288 |
procedure TAbBzip2Archive.LoadArchive; |
| 289 |
var |
| 290 |
Item: TAbBzip2Item; |
| 291 |
Abort: Boolean; |
| 292 |
ItemName: string; |
| 293 |
begin |
| 294 |
if FBzip2Stream.Size = 0 then |
| 295 |
Exit; |
| 296 |
|
| 297 |
if IsBzippedTar and TarAutoHandle then begin |
| 298 |
{ Decompress and send to tar LoadArchive } |
| 299 |
DecompressToStream(FTarStream); |
| 300 |
SwapToTar; |
| 301 |
inherited LoadArchive; |
| 302 |
end |
| 303 |
else begin |
| 304 |
SwapToBzip2; |
| 305 |
Item := TAbBzip2Item.Create; |
| 306 |
Item.Action := aaNone; |
| 307 |
{ Filename isn't stored, so constuct one based on the archive name } |
| 308 |
ItemName := ExtractFileName(ArchiveName); |
| 309 |
if ItemName = '' then |
| 310 |
Item.FileName := 'unknown' |
| 311 |
else if AnsiEndsText('.tbz', ItemName) or AnsiEndsText('.tbz2', ItemName) then |
| 312 |
Item.FileName := ChangeFileExt(ItemName, '.tar') |
| 313 |
else |
| 314 |
Item.FileName := ChangeFileExt(ItemName, ''); |
| 315 |
Item.DiskFileName := Item.FileName; |
| 316 |
FItemList.Add(Item); |
| 317 |
end; |
| 318 |
DoArchiveProgress(100, Abort); |
| 319 |
FIsDirty := False; |
| 320 |
end; |
| 321 |
{ -------------------------------------------------------------------------- } |
| 322 |
procedure TAbBzip2Archive.SaveArchive; |
| 323 |
var |
| 324 |
CompStream: TStream; |
| 325 |
i: Integer; |
| 326 |
CurItem: TAbBzip2Item; |
| 327 |
UpdateArchive: Boolean; |
| 328 |
TempFileName: String; |
| 329 |
InputFileStream: TStream; |
| 330 |
begin |
| 331 |
if IsBzippedTar and TarAutoHandle then |
| 332 |
begin |
| 333 |
SwapToTar; |
| 334 |
UpdateArchive := (FBzip2Stream.Size > 0) and (FBzip2Stream is TFileStreamEx); |
| 335 |
if UpdateArchive then |
| 336 |
begin |
| 337 |
FreeAndNil(FBzip2Stream); |
| 338 |
TempFileName := GetTempName(FArchiveName); |
| 339 |
{ Create new archive with temporary name } |
| 340 |
FBzip2Stream := TFileStreamEx.Create(TempFileName, fmCreate or fmShareDenyWrite); |
| 341 |
end; |
| 342 |
FTarStream.Position := 0; |
| 343 |
CompStream := TBZCompressionStream.Create(CompressionLevel, FBzip2Stream); |
| 344 |
try |
| 345 |
FTargetStream := TWriteBufStream.Create(CompStream, $40000); |
| 346 |
try |
| 347 |
inherited SaveArchive; |
| 348 |
finally |
| 349 |
FreeAndNil(FTargetStream); |
| 350 |
end; |
| 351 |
finally |
| 352 |
CompStream.Free; |
| 353 |
end; |
| 354 |
if UpdateArchive then |
| 355 |
begin |
| 356 |
FreeAndNil(FBzip2Stream); |
| 357 |
{ Replace original by new archive } |
| 358 |
if not (mbDeleteFile(FArchiveName) and mbRenameFile(TempFileName, FArchiveName)) then |
| 359 |
RaiseLastOSError; |
| 360 |
{ Open new archive } |
| 361 |
FBzip2Stream := TFileStreamEx.Create(FArchiveName, fmOpenRead or fmShareDenyNone); |
| 362 |
end; |
| 363 |
end |
| 364 |
else begin |
| 365 |
{ Things we know: There is only one file per archive.} |
| 366 |
{ Actions we have to address in SaveArchive: } |
| 367 |
{ aaNone & aaMove do nothing, as the file does not change, only the meta data } |
| 368 |
{ aaDelete could make a zero size file unless there are two files in the list.} |
| 369 |
{ aaAdd, aaStreamAdd, aaFreshen, & aaReplace will be the only ones to take action. } |
| 370 |
SwapToBzip2; |
| 371 |
for i := 0 to pred(Count) do begin |
| 372 |
FCurrentItem := ItemList[i]; |
| 373 |
CurItem := TAbBzip2Item(ItemList[i]); |
| 374 |
case CurItem.Action of |
| 375 |
aaNone, aaMove: Break;{ Do nothing; bz2 doesn't store metadata } |
| 376 |
aaDelete: ; {doing nothing omits file from new stream} |
| 377 |
aaAdd, aaFreshen, aaReplace, aaStreamAdd: begin |
| 378 |
FBzip2Stream.Size := 0; |
| 379 |
CompStream := TBZCompressionStream.Create(CompressionLevel, FBzip2Stream); |
| 380 |
try |
| 381 |
if CurItem.Action = aaStreamAdd then |
| 382 |
CompStream.CopyFrom(InStream, 0){ Copy/compress entire Instream to FBzip2Stream } |
| 383 |
else begin |
| 384 |
InputFileStream := TFileStreamEx.Create(CurItem.DiskFileName, fmOpenRead or fmShareDenyWrite); |
| 385 |
try |
| 386 |
with TAbProgressWriteStream.Create(CompStream, InputFileStream.Size, OnProgress) do |
| 387 |
try |
| 388 |
CopyFrom(InputFileStream, 0);{ Copy/compress entire Instream to FBzip2Stream } |
| 389 |
finally |
| 390 |
Free; |
| 391 |
end; |
| 392 |
finally |
| 393 |
InputFileStream.Free; |
| 394 |
end; |
| 395 |
end; |
| 396 |
finally |
| 397 |
CompStream.Free; |
| 398 |
end; |
| 399 |
Break; |
| 400 |
end; { End aaAdd, aaFreshen, aaReplace, & aaStreamAdd } |
| 401 |
end; { End of CurItem.Action Case } |
| 402 |
end; { End Item for loop } |
| 403 |
end; { End Tar Else } |
| 404 |
end; |
| 405 |
{ -------------------------------------------------------------------------- } |
| 406 |
procedure TAbBzip2Archive.SetTarAutoHandle(const Value: Boolean); |
| 407 |
begin |
| 408 |
if Value then |
| 409 |
SwapToTar |
| 410 |
else |
| 411 |
SwapToBzip2; |
| 412 |
FTarAutoHandle := Value; |
| 413 |
end; |
| 414 |
{ -------------------------------------------------------------------------- } |
| 415 |
procedure TAbBzip2Archive.DecompressToStream(aStream: TStream); |
| 416 |
const |
| 417 |
BufSize = $F000; |
| 418 |
var |
| 419 |
DecompStream: TBZDecompressionStream; |
| 420 |
ProxyStream: TAbProgressReadStream; |
| 421 |
Buffer: PByte; |
| 422 |
N: Integer; |
| 423 |
begin |
| 424 |
ProxyStream:= TAbProgressReadStream.Create(FBzip2Stream, OnProgress); |
| 425 |
try |
| 426 |
DecompStream := TBZDecompressionStream.Create(ProxyStream); |
| 427 |
try |
| 428 |
GetMem(Buffer, BufSize); |
| 429 |
try |
| 430 |
N := DecompStream.Read(Buffer^, BufSize); |
| 431 |
while N > 0 do begin |
| 432 |
aStream.WriteBuffer(Buffer^, N); |
| 433 |
N := DecompStream.Read(Buffer^, BufSize); |
| 434 |
end; |
| 435 |
finally |
| 436 |
FreeMem(Buffer, BufSize); |
| 437 |
end; |
| 438 |
finally |
| 439 |
DecompStream.Free; |
| 440 |
end; |
| 441 |
finally |
| 442 |
ProxyStream.Free; |
| 443 |
end; |
| 444 |
end; |
| 445 |
{ -------------------------------------------------------------------------- } |
| 446 |
procedure TAbBzip2Archive.TestItemAt(Index: Integer); |
| 447 |
var |
| 448 |
Bzip2Type: TAbArchiveType; |
| 449 |
BitBucket: TAbBitBucketStream; |
| 450 |
begin |
| 451 |
if IsBzippedTar and TarAutoHandle then begin |
| 452 |
SwapToTar; |
| 453 |
inherited TestItemAt(Index); |
| 454 |
end |
| 455 |
else begin |
| 456 |
{ note Index ignored as there's only one item in a GZip } |
| 457 |
Bzip2Type := VerifyBzip2(FBzip2Stream); |
| 458 |
if not (Bzip2Type in [atBzip2, atBzippedTar]) then |
| 459 |
raise EAbGzipInvalid.Create;// TODO: Add bzip2-specific exceptions } |
| 460 |
BitBucket := TAbBitBucketStream.Create(1024); |
| 461 |
try |
| 462 |
DecompressToStream(BitBucket); |
| 463 |
finally |
| 464 |
BitBucket.Free; |
| 465 |
end; |
| 466 |
end; |
| 467 |
end; |
| 468 |
{ -------------------------------------------------------------------------- } |
| 469 |
procedure TAbBzip2Archive.DoSpanningMediaRequest(Sender: TObject; |
| 470 |
ImageNumber: Integer; var ImageName: string; var Abort: Boolean); |
| 471 |
begin |
| 472 |
Abort := False; |
| 473 |
end; |
| 474 |
|
| 475 |
end. |