Debian Codegraph @

plugins/wcx/zip/src/fparchive/abxztyp.pas

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