Debian Codegraph @

plugins/wcx/sevenzip/src/SevenZipAdv.pas

raw
1 {
2 Double Commander
3 -------------------------------------------------------------------------
4 SevenZip archiver plugin
5
6 Copyright (C) 2014-2023 Alexander Koblov ([email protected])
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 }
22
23 unit SevenZipAdv;
24
25 {$mode delphi}
26
27 interface
28
29 uses
30 Classes, SysUtils, SevenZip, JclCompression;
31
32 type
33 TBytes = array of Byte;
34 TCardinalArray = array of Cardinal;
35 TJclCompressionArchiveClassArray = array of TJclCompressionArchiveClass;
36
37 type
38
39 { TArchiveFormat }
40
41 TArchiveFormat = class
42 Name: UnicodeString;
43 Extension: UnicodeString;
44 AddExtension: UnicodeString;
45 Update: WordBool;
46 KeepName: WordBool;
47 ClassID: TGUID;
48 StartSignature: TBytes;
49 end;
50
51 { TJclXzCompressArchiveEx }
52
53 TJclXzCompressArchiveEx = class(TJclSevenzipCompressArchive)
54 public
55 class function ArchiveExtensions: string; override;
56 class function ArchiveName: string; override;
57 class function ArchiveSubExtensions: string; override;
58 class function ArchiveCLSID: TGUID; override;
59 end;
60
61 { TJclSevenzipUpdateArchiveHelper }
62
63 TJclSevenzipUpdateArchiveHelper = class helper for TJclSevenzipUpdateArchive
64 procedure RemoveDirectory(const PackedName: WideString); overload;
65 end;
66
67 { TJclSevenzipDecompressArchiveHelper }
68
69 TJclSevenzipDecompressArchiveHelper = class helper for TJclSevenzipDecompressArchive
70 procedure ProcessSelected(const SelectedArray: TCardinalArray; Verify: Boolean);
71 end;
72
73 function FindUpdateFormats(const AFileName: TFileName): TJclUpdateArchiveClassArray;
74 function FindCompressFormats(const AFileName: TFileName): TJclCompressArchiveClassArray;
75 function FindDecompressFormats(const AFileName: TFileName): TJclDecompressArchiveClassArray;
76
77 function GetNestedArchiveName(const ArchiveName: String; Item: TJclCompressionItem): WideString;
78 function ExpandEnvironmentStrings(const FileName: UnicodeString): UnicodeString;
79 function WideExtractFilePath(const FileName: WideString): WideString;
80 function GetModulePath(out ModulePath: AnsiString): Boolean;
81
82 implementation
83
84 uses
85 CTypes, ActiveX, Windows, LazFileUtils, LazUTF8, SevenZipHlp;
86
87 type
88 TArchiveFormats = array of TArchiveFormat;
89 TJclSevenzipUpdateArchiveClass = class of TJclSevenzipUpdateArchive;
90 TJclSevenzipCompressArchiveClass = class of TJclSevenzipCompressArchive;
91 TJclSevenzipDecompressArchiveClass = class of TJclSevenzipDecompressArchive;
92 TJclArchiveType = (atUpdateArchive, atCompressArchive, atDecompressArchive);
93
94 type
95 TArchiveFormatCache = record
96 ArchiveName: String;
97 ArchiveClassArray: TJclCompressionArchiveClassArray;
98 end;
99
100 var
101 Mutex: TRTLCriticalSection;
102 ArchiveFormatsX: TArchiveFormats;
103
104 var
105 UpdateFormatsCache: TArchiveFormatCache;
106 CompressFormatsCache: TArchiveFormatCache;
107 DecompressFormatsCache: TArchiveFormatCache;
108
109 function _wcsnicmp(const s1, s2: pwidechar; count: csize_t): cint; cdecl; external 'msvcrt.dll';
110
111 function ReadStringProp(FormatIndex: Cardinal; PropID: TPropID;
112 out Value: UnicodeString): LongBool;
113 var
114 PropValue: TPropVariant;
115 begin
116 PropValue.vt:= VT_EMPTY;
117 Result:= Succeeded(GetHandlerProperty2(FormatIndex, PropID, PropValue));
118 Result:= Result and (PropValue.vt = VT_BSTR);
119 if Result then
120 try
121 Value:= BinaryToUnicode(PropValue.bstrVal);
122 finally
123 VarStringClear(PropValue);
124 end;
125 end;
126
127 {$OPTIMIZATION OFF}
128 function ReadBooleanProp(FormatIndex: Cardinal;
129 PropID: TPropID; out Value: WordBool): LongBool;
130 var
131 PropValue: TPropVariant;
132 begin
133 PropValue.vt:= VT_EMPTY;
134 Result:= Succeeded(GetHandlerProperty2(FormatIndex, PropID, PropValue));
135 Result:= Result and (PropValue.vt = VT_BOOL);
136 if Result then Value:= PropValue.boolVal;
137 end;
138 {$OPTIMIZATION DEFAULT}
139
140 procedure LoadArchiveFormats(var ArchiveFormats: TArchiveFormats);
141 var
142 Idx: Integer = 0;
143 PropSize: Cardinal;
144 PropValue: TPropVariant;
145 ArchiveFormat: TArchiveFormat;
146 Index, NumberOfFormats: Cardinal;
147 begin
148 if (not Is7ZipLoaded) and (not Load7Zip) then Exit;
149
150 if not Succeeded(GetNumberOfFormats(@NumberOfFormats)) then
151 Exit;
152 SetLength(ArchiveFormats, NumberOfFormats);
153 for Index := Low(ArchiveFormats) to High(ArchiveFormats) do
154 begin
155 PropValue.vt:= VT_EMPTY;
156 // Archive format GUID
157 if Succeeded(GetHandlerProperty2(Index, kClassID, PropValue)) then
158 begin
159 if PropValue.vt = VT_BSTR then
160 try
161 if SysStringByteLen(PropValue.bstrVal) <> SizeOf(TGUID) then
162 Continue
163 else begin
164 ArchiveFormat:= TArchiveFormat.Create;
165 ArchiveFormat.ClassID:= PGUID(PropValue.bstrVal)^;
166 end;
167 finally
168 VarStringClear(PropValue);
169 end;
170 end;
171 PropValue.vt:= VT_EMPTY;
172 // Archive format signature
173 if Succeeded(GetHandlerProperty2(Index, kStartSignature, PropValue)) then
174 begin
175 if PropValue.vt = VT_BSTR then
176 try
177 PropSize:= SysStringByteLen(PropValue.bstrVal);
178 if (PropSize > 0) then
179 begin
180 SetLength(ArchiveFormat.StartSignature, PropSize);
181 CopyMemory(@ArchiveFormat.StartSignature[0], PropValue.bstrVal, PropSize);
182 end;
183 finally
184 VarStringClear(PropValue);
185 end;
186 end;
187
188 ReadStringProp(Index, kArchiveName, ArchiveFormat.Name);
189 ReadStringProp(Index, kExtension, ArchiveFormat.Extension);
190 ReadStringProp(Index, kAddExtension, ArchiveFormat.AddExtension);
191 ReadBooleanProp(Index, kUpdate, ArchiveFormat.Update);
192 ReadBooleanProp(Index, kKeepName, ArchiveFormat.KeepName);
193
194 ArchiveFormats[Idx]:= ArchiveFormat;
195 Inc(Idx);
196 end;
197 SetLength(ArchiveFormats, Idx);
198 end;
199
200 function Contains(const ArrayToSearch: TJclCompressionArchiveClassArray; const ArchiveClass: TJclCompressionArchiveClass): Boolean;
201 var
202 Index: Integer;
203 begin
204 for Index := Low(ArrayToSearch) to High(ArrayToSearch) do
205 if ArrayToSearch[Index] = ArchiveClass then
206 Exit(True);
207 Result := False;
208 end;
209
210 function FindArchiveFormat(const ClassID: TGUID; ArchiveType: TJclArchiveType): TJclCompressionArchiveClass;
211 var
212 Index: Integer;
213 UpdateClass: TJclSevenzipUpdateArchiveClass;
214 CompressClass: TJclSevenzipCompressArchiveClass;
215 DecompressClass: TJclSevenzipDecompressArchiveClass;
216 begin
217 case ArchiveType of
218 atUpdateArchive:
219 for Index:= 0 to GetArchiveFormats.UpdateFormatCount - 1 do
220 begin
221 UpdateClass:= TJclSevenzipUpdateArchiveClass(GetArchiveFormats.UpdateFormats[Index]);
222 if IsEqualGUID(ClassID, UpdateClass.ArchiveCLSID) then
223 Exit(GetArchiveFormats.UpdateFormats[Index]);
224 end;
225 atCompressArchive:
226 for Index:= 0 to GetArchiveFormats.CompressFormatCount - 1 do
227 begin
228 CompressClass:= TJclSevenzipCompressArchiveClass(GetArchiveFormats.CompressFormats[Index]);
229 if IsEqualGUID(ClassID, CompressClass.ArchiveCLSID) then
230 Exit(GetArchiveFormats.CompressFormats[Index]);
231 end;
232 atDecompressArchive:
233 for Index:= 0 to GetArchiveFormats.DecompressFormatCount - 1 do
234 begin
235 DecompressClass:= TJclSevenzipDecompressArchiveClass(GetArchiveFormats.DecompressFormats[Index]);
236 if IsEqualGUID(ClassID, DecompressClass.ArchiveCLSID) then
237 Exit(GetArchiveFormats.DecompressFormats[Index]);
238 end;
239 end;
240 Result:= nil;
241 end;
242
243 procedure FindArchiveFormats(const AFileName: TFileName; ArchiveType: TJclArchiveType; var Result: TJclCompressionArchiveClassArray);
244 const
245 BufferSize = 524288;
246 var
247 AFile: THandle;
248 Buffer: TBytes;
249 Idx, Index: Integer;
250 ArchiveFormat: TArchiveFormat;
251 ArchiveClass: TJclCompressionArchiveClass;
252 begin
253 if Length(ArchiveFormatsX) = 0 then LoadArchiveFormats(ArchiveFormatsX);
254
255 AFile:= FileOpenUTF8(AFileName, fmOpenRead or fmShareDenyNone);
256 if AFile = feInvalidHandle then Exit;
257 try
258 SetLength(Buffer, BufferSize);
259 if FileRead(AFile, Buffer[0], BufferSize) = 0 then
260 Exit;
261 finally
262 FileClose(AFile);
263 end;
264
265 for Index := Low(ArchiveFormatsX) to High(ArchiveFormatsX) do
266 begin
267 ArchiveFormat:= ArchiveFormatsX[Index];
268
269 if (not ArchiveFormat.Update) and (ArchiveType in [atUpdateArchive, atCompressArchive]) then
270 Continue;
271
272 // Skip container types
273 if IsEqualGUID(ArchiveFormat.ClassID, CLSID_CFormatPe) then Continue;
274 if IsEqualGUID(ArchiveFormat.ClassID, CLSID_CFormatIso) then Continue;
275 if IsEqualGUID(ArchiveFormat.ClassID, CLSID_CFormatUdf) then Continue;
276
277 if Length(ArchiveFormat.StartSignature) = 0 then Continue;
278 for Idx:= 0 to Pred(BufferSize) - Length(ArchiveFormat.StartSignature) do
279 begin
280 if CompareMem(@Buffer[Idx], @ArchiveFormat.StartSignature[0], Length(ArchiveFormat.StartSignature)) then
281 begin
282 ArchiveClass:= FindArchiveFormat(ArchiveFormat.ClassID, ArchiveType);
283 if Assigned(ArchiveClass) and not Contains(Result, ArchiveClass) then
284 begin
285 SetLength(Result, Length(Result) + 1);
286 Result[High(Result)] := ArchiveClass;
287 end;
288 Break;
289 end;
290 end;
291 end;
292 end;
293
294 function FindUpdateFormats(const AFileName: TFileName): TJclUpdateArchiveClassArray;
295 var
296 ArchiveClassArray: TJclCompressionArchiveClassArray absolute Result;
297 begin
298 System.EnterCriticalSection(Mutex);
299 try
300 // Try to find archive type in cache
301 if UpdateFormatsCache.ArchiveName = AFileName then
302 Exit(TJclUpdateArchiveClassArray(UpdateFormatsCache.ArchiveClassArray))
303 else begin
304 UpdateFormatsCache.ArchiveName:= AFileName;
305 SetLength(UpdateFormatsCache.ArchiveClassArray, 0);
306 end;
307
308 Result:= GetArchiveFormats.FindUpdateFormats(AFileName);
309
310 FindArchiveFormats(AFileName, atUpdateArchive, ArchiveClassArray);
311
312 // Save archive type in cache
313 UpdateFormatsCache.ArchiveClassArray:= ArchiveClassArray;
314 finally
315 System.LeaveCriticalSection(Mutex);
316 end;
317 end;
318
319 function FindCompressFormats(const AFileName: TFileName): TJclCompressArchiveClassArray;
320 var
321 ArchiveClassArray: TJclCompressionArchiveClassArray absolute Result;
322 begin
323 System.EnterCriticalSection(Mutex);
324 try
325 // Try to find archive type in cache
326 if CompressFormatsCache.ArchiveName = AFileName then
327 Exit(TJclCompressArchiveClassArray(CompressFormatsCache.ArchiveClassArray))
328 else begin
329 CompressFormatsCache.ArchiveName:= AFileName;
330 SetLength(CompressFormatsCache.ArchiveClassArray, 0);
331 end;
332
333 Result:= GetArchiveFormats.FindCompressFormats(AFileName);
334
335 FindArchiveFormats(AFileName, atCompressArchive, ArchiveClassArray);
336
337 // Save archive type in cache
338 CompressFormatsCache.ArchiveClassArray:= ArchiveClassArray;
339 finally
340 System.LeaveCriticalSection(Mutex);
341 end;
342 end;
343
344 function FindDecompressFormats(const AFileName: TFileName): TJclDecompressArchiveClassArray;
345 var
346 ArchiveClassArray: TJclCompressionArchiveClassArray absolute Result;
347 begin
348 System.EnterCriticalSection(Mutex);
349 try
350 // Try to find archive type in cache
351 if DecompressFormatsCache.ArchiveName = AFileName then
352 Exit(TJclDecompressArchiveClassArray(DecompressFormatsCache.ArchiveClassArray))
353 else begin
354 DecompressFormatsCache.ArchiveName:= AFileName;
355 SetLength(DecompressFormatsCache.ArchiveClassArray, 0);
356 end;
357
358 Result:= GetArchiveFormats.FindDecompressFormats(AFileName);
359
360 FindArchiveFormats(AFileName, atDecompressArchive, ArchiveClassArray);
361
362 // Save archive type in cache
363 DecompressFormatsCache.ArchiveClassArray:= ArchiveClassArray;
364 finally
365 System.LeaveCriticalSection(Mutex);
366 end;
367 end;
368
369 { TJclXzCompressArchiveEx }
370
371 class function TJclXzCompressArchiveEx.ArchiveExtensions: string;
372 begin
373 Result:= TJclXzCompressArchive.ArchiveExtensions;
374 end;
375
376 class function TJclXzCompressArchiveEx.ArchiveName: string;
377 begin
378 Result:= TJclXzCompressArchive.ArchiveName;
379 end;
380
381 class function TJclXzCompressArchiveEx.ArchiveSubExtensions: string;
382 begin
383 Result:= TJclXzCompressArchive.ArchiveSubExtensions;
384 end;
385
386 class function TJclXzCompressArchiveEx.ArchiveCLSID: TGUID;
387 begin
388 Result:= TJclXzCompressArchive.ArchiveCLSID;
389 end;
390
391 { TJclSevenzipUpdateArchiveHelper }
392
393 procedure TJclSevenzipUpdateArchiveHelper.RemoveDirectory(const PackedName: WideString);
394 var
395 DirectoryName: WideString;
396 AItem: TJclCompressionItem;
397 Index, PackedNamesIndex: Integer;
398 begin
399 DirectoryName:= Copy(PackedName, 1, Length(PackedName) - 1);
400 // Remove directory
401 for Index := 0 to ItemCount - 1 do
402 begin
403 AItem := Items[Index];
404 // Can be with or without path delimiter at end
405 if WideSameText(AItem.PackedName, PackedName) or WideSameText(AItem.PackedName, DirectoryName) then
406 begin
407 FItems.Delete(Index);
408 PackedNamesIndex := -1;
409 if (FPackedNames <> nil) and FPackedNames.Find(PackedName, PackedNamesIndex) then
410 FPackedNames.Delete(PackedNamesIndex);
411 Break;
412 end;
413 end;
414 // Remove directory content
415 for Index := ItemCount - 1 downto 0 do
416 begin
417 if (_wcsnicmp(PWideChar(PackedName), PWideChar(Items[Index].PackedName), Length(PackedName)) = 0) then
418 begin
419 if (FPackedNames <> nil) and FPackedNames.Find(Items[Index].PackedName, PackedNamesIndex) then
420 FPackedNames.Delete(PackedNamesIndex);
421 FItems.Delete(Index);
422 end;
423 end;
424 end;
425
426 { TJclSevenzipDecompressArchiveHelper }
427
428 procedure TJclSevenzipDecompressArchiveHelper.ProcessSelected(const SelectedArray: TCardinalArray; Verify: Boolean);
429 var
430 AExtractCallback: IArchiveExtractCallback;
431 begin
432 CheckNotDecompressing;
433
434 FDecompressing := True;
435 AExtractCallback := TJclSevenzipExtractCallback.Create(Self);
436 try
437 OpenArchive;
438
439 SevenzipCheck(InArchive.Extract(@SelectedArray[0], Length(SelectedArray), Cardinal(Verify), AExtractCallback));
440 CheckOperationSuccess;
441 finally
442 FDestinationDir := '';
443 FDecompressing := False;
444 AExtractCallback := nil;
445 end;
446 end;
447
448 function GetNestedArchiveName(const ArchiveName: String; Item: TJclCompressionItem): WideString;
449 var
450 Extension: String;
451 begin
452 Result:= Item.NestedArchiveName;
453 Extension:= LowerCase(ExtractFileExt(ArchiveName));
454 if (Extension = '.tbz') or (Extension = '.tgz') or (Extension = '.txz') then
455 begin
456 Result:= Result + '.tar';
457 end;
458 end;
459
460 function ExpandEnvironmentStrings(const FileName: UnicodeString): UnicodeString;
461 var
462 dwSize: DWORD;
463 begin
464 SetLength(Result, MAX_PATH + 1);
465 dwSize:= ExpandEnvironmentStringsW(PWideChar(FileName), PWideChar(Result), MAX_PATH);
466 if dwSize > 0 then SetLength(Result, dwSize - 1);
467 end;
468
469 function WideExtractFilePath(const FileName: WideString): WideString;
470 var
471 Index: Integer;
472 begin
473 for Index:= Length(FileName) downto 1 do
474 case FileName[Index] of
475 PathDelim: Exit(Copy(FileName, 1, Index));
476 end;
477 Result:= EmptyWideStr;
478 end;
479
480 function GetModulePath(out ModulePath: AnsiString): Boolean;
481 var
482 lpBuffer: TMemoryBasicInformation;
483 ModuleName: array[0..MAX_PATH] of WideChar;
484 begin
485 Result:= VirtualQuery(@GetModulePath, @lpBuffer, SizeOf(lpBuffer)) = SizeOf(lpBuffer);
486 if Result then
487 begin
488 Result:= GetModuleFileNameW(THandle(lpBuffer.AllocationBase), ModuleName, MAX_PATH) > 0;
489 if Result then
490 begin
491 ModulePath:= ExtractFilePath(Utf16ToUtf8(WideString(ModuleName)));
492 end;
493 end;
494 end;
495
496 initialization
497 InitCriticalSection(Mutex);
498
499 finalization
500 DoneCriticalSection(Mutex);
501
502 end.
503