Debian Codegraph @

plugins/wcx/sevenzip/src/SevenZipFunc.pas

raw
1 {
2 Double Commander
3 -------------------------------------------------------------------------
4 SevenZip archiver plugin
5
6 Copyright (C) 2014-2022 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 SevenZipFunc;
24
25 {$IFDEF FPC}
26 {$MODE DELPHI}
27 {$ENDIF}
28
29 interface
30
31 uses
32 WcxPlugin;
33
34 { Mandatory }
35 function OpenArchiveW(var ArchiveData : tOpenArchiveDataW) : TArcHandle;stdcall;
36 function ReadHeaderExW(hArcData : TArcHandle; var HeaderData: THeaderDataExW) : Integer;stdcall;
37 function ProcessFileW(hArcData : TArcHandle; Operation : Integer; DestPath, DestName : PWideChar) : Integer;stdcall;
38 function CloseArchive (hArcData : TArcHandle) : Integer;stdcall;
39 procedure SetChangeVolProcW(hArcData : TArcHandle; pChangeVolProc : TChangeVolProcW);stdcall;
40 procedure SetProcessDataProcW(hArcData : TArcHandle; pProcessDataProc : TProcessDataProcW);stdcall;
41 { Optional }
42 function PackFilesW(PackedFile: PWideChar; SubPath: PWideChar; SrcPath: PWideChar; AddList: PWideChar; Flags: Integer): Integer; stdcall;
43 function DeleteFilesW(PackedFile, DeleteList: PWideChar): Integer; stdcall;
44 function CanYouHandleThisFileW(FileName: PWideChar): Boolean; stdcall;
45 procedure PackSetDefaultParams(dps: PPackDefaultParamStruct); stdcall;
46 procedure ConfigurePacker(Parent: HWND; DllInstance: THandle); stdcall;
47
48 implementation
49
50 uses
51 JwaWinBase, Windows, SysUtils, Classes, JclCompression, SevenZip, SevenZipAdv, fpTimer,
52 SevenZipDlg, SevenZipLng, SevenZipOpt, LazFileUtils, SyncObjs, LazUTF8, SevenZipCodecs;
53
54 type
55
56 { ESevenZipAbort }
57
58 ESevenZipAbort = class(EJclCompressionError)
59
60 end;
61
62 { TSevenZipUpdate }
63
64 TSevenZipUpdate = class(TThread)
65 FPercent: Int64;
66 FFileName: WideString;
67 FPause: TEventObject;
68 FProgress: TEventObject;
69 FArchive: TJclCompressionArchive;
70 public
71 constructor Create; overload;
72 constructor Create(Archive: TJclCompressionArchive); overload;
73 destructor Destroy; override;
74 public
75 procedure Execute; override;
76 function Update: Integer; virtual;
77 procedure JclCompressionPassword(Sender: TObject; var Password: WideString);
78 procedure JclCompressionProgress(Sender: TObject; const Value, MaxValue: Int64); virtual;
79 end;
80
81 { TSevenZipHandle }
82
83 TSevenZipHandle = class(TSevenZipUpdate)
84 Index,
85 Count: LongWord;
86 OpenMode,
87 OperationMode: Integer;
88 ProcessIndex: Cardinal;
89 ArchiveName: String;
90 ProcessArray: TCardinalArray;
91 FileName: array of String;
92 ProcessDataProc: TProcessDataProcW;
93 public
94 procedure Execute; override;
95 function Update: Integer; override;
96 procedure SetArchive(AValue: TJclDecompressArchive);
97 function JclCompressionExtract(Sender: TObject; AIndex: Integer;
98 var AFileName: TFileName; var Stream: TStream; var AOwnsStream: Boolean): Boolean;
99 end;
100
101 { TPasswordCache }
102
103 TPasswordCache = class
104 private
105 FTimer: TFPTimer;
106 FArchiveSize: Int64;
107 FArchiveName: String;
108 FArchiveTime: Integer;
109 FMutex: TCriticalSection;
110 FArchivePassword: WideString;
111 const FInterval: Cardinal = 120000;
112 private
113 procedure ResetTimer;
114 procedure ZeroPassword;
115 procedure TimerEvent(Sender: TObject);
116 public
117 constructor Create;
118 destructor Destroy; override;
119 function GetPassword(const Archive: String): WideString;
120 procedure SetPassword(const Archive: String; const Password: WideString);
121 end;
122
123 var
124 PasswordCache: TPasswordCache;
125
126 threadvar
127 ProcessDataProcT: TProcessDataProcW;
128
129 function GetArchiveError(const E: Exception): Integer;
130 begin
131 if E is EFOpenError then
132 Result:= E_EOPEN
133 else if E is EFCreateError then
134 Result:= E_ECREATE
135 else if E is EReadError then
136 Result:= E_EREAD
137 else if E is EWriteError then
138 Result:= E_EWRITE
139 else if E is ESevenZipAbort then
140 Result:= E_EABORTED
141 else if Pos(HexStr(E_OUTOFMEMORY, 8), E.Message) > 0 then
142 Result:= E_NO_MEMORY
143 else begin
144 Result:= E_UNKNOWN_FORMAT;
145 end;
146 end;
147
148 function WinToDosTime(const WinTime: TFILETIME; var DosTime: Cardinal): LongBool;
149 var
150 lft : Windows.TFILETIME;
151 begin
152 Result:= Windows.FileTimeToLocalFileTime(@Windows.FILETIME(WinTime), @lft) and
153 Windows.FileTimeToDosDateTime(@lft, @LongRec(Dostime).Hi, @LongRec(DosTime).Lo);
154 end;
155
156 function OpenArchiveW(var ArchiveData : tOpenArchiveDataW) : TArcHandle; stdcall;
157 var
158 I: Integer;
159 ResultHandle: TSevenZipHandle;
160 Archive: TJclDecompressArchive;
161 AFormats: TJclDecompressArchiveClassArray;
162 begin
163 ResultHandle:= TSevenZipHandle.Create;
164 with ResultHandle do
165 begin
166 Index:= 0;
167 ProcessIndex:= 0;
168 OpenMode:= ArchiveData.OpenMode;
169 ArchiveName := Utf16ToUtf8(WideString(ArchiveData.ArcName));
170 AFormats := FindDecompressFormats(ArchiveName);
171 for I := Low(AFormats) to High(AFormats) do
172 begin
173 Archive := AFormats[I].Create(ArchiveName, 0, False);
174 try
175 SetArchive(Archive);
176
177 Archive.Password:= PasswordCache.GetPassword(ArchiveName);
178
179 Archive.ListFiles;
180
181 PasswordCache.SetPassword(ArchiveName, Archive.Password);
182
183 Count:= Archive.ItemCount;
184
185 if OpenMode = PK_OM_EXTRACT then
186 begin
187 SetLength(FileName, Count);
188 SetLength(ProcessArray, Count);
189 end;
190
191 ArchiveData.OpenResult:= E_SUCCESS;
192
193 Exit(TArcHandle(ResultHandle));
194 except
195 on E: Exception do
196 begin
197 ArchiveData.OpenResult:= GetArchiveError(E);
198 FreeAndNil(Archive);
199 Continue;
200 end;
201 end;
202 end;
203 if (Archive = nil) then Free;
204 end;
205 Result:= 0;
206 end;
207
208 function ReadHeaderExW(hArcData : TArcHandle; var HeaderData: THeaderDataExW) : Integer; stdcall;
209 var
210 FileNameW: UnicodeString;
211 Item: TJclCompressionItem;
212 Handle: TSevenZipHandle absolute hArcData;
213 begin
214 with Handle do
215 begin
216 if Index >= Count then Exit(E_END_ARCHIVE);
217 Item:= FArchive.Items[Index];
218 FileNameW:= Item.PackedName;
219 HeaderData.UnpSize:= Int64Rec(Item.FileSize).Lo;
220 HeaderData.UnpSizeHigh:= Int64Rec(Item.FileSize).Hi;
221 HeaderData.PackSize:= Int64Rec(Item.PackedSize).Lo;
222 HeaderData.PackSizeHigh:= Int64Rec(Item.PackedSize).Hi;
223 if ipAttributes in Item.ValidProperties then
224 HeaderData.FileAttr:= LongInt(Item.Attributes)
225 else begin
226 HeaderData.FileAttr:= FILE_ATTRIBUTE_ARCHIVE;
227 end;
228 HeaderData.MfileTime:= UInt64(Item.LastWriteTime);
229 WinToDosTime(Item.LastWriteTime, LongWord(HeaderData.FileTime));
230 if Item.Encrypted then begin
231 HeaderData.Flags:= RHDF_ENCRYPTED;
232 end;
233 // Special case for absolute file name
234 if (Length(FileNameW) > 0) and (FileNameW[1] = PathDelim) then
235 HeaderData.FileName:= Copy(FileNameW, 2, Length(FileNameW) - 1)
236 else begin
237 HeaderData.FileName:= FileNameW;
238 end;
239 // Special case for BZip2, GZip and Xz archives
240 if (HeaderData.FileName[0] = #0) then
241 begin
242 HeaderData.FileName:= GetNestedArchiveName(ArchiveName, Item);
243 end;
244 end;
245 Result:= E_SUCCESS;
246 end;
247
248 function ProcessFileW(hArcData: TArcHandle; Operation: Integer; DestPath, DestName: PWideChar): Integer; stdcall;
249 var
250 Handle: TSevenZipHandle absolute hArcData;
251 begin
252 try
253 with Handle do
254 case Operation of
255 PK_TEST,
256 PK_EXTRACT:
257 begin
258 if Operation = PK_EXTRACT then
259 begin
260 if Assigned(DestPath) then
261 begin
262 FileName[Index]:= IncludeTrailingPathDelimiter(Utf16ToUtf8(WideString(DestPath))) +
263 Utf16ToUtf8(WideString(DestName));
264 end
265 else begin
266 FileName[Index]:= Utf16ToUtf8(WideString(DestName));
267 end;
268 end;
269 Result:= E_SUCCESS;
270 OperationMode:= Operation;
271 ProcessArray[ProcessIndex]:= Index;
272 Inc(ProcessIndex);
273 end;
274 else
275 Result:= E_SUCCESS;
276 end;
277 finally
278 Inc(Handle.Index);
279 end;
280 end;
281
282 function CloseArchive(hArcData: TArcHandle): Integer; stdcall;
283 var
284 Handle: TSevenZipHandle absolute hArcData;
285 begin
286 Result:= E_SUCCESS;
287 if (hArcData <> wcxInvalidHandle) then
288 with Handle do
289 begin
290 if OpenMode = PK_OM_EXTRACT then
291 begin
292 Start;
293 Update;
294 end;
295 FArchive.Free;
296 Free;
297 end;
298 end;
299
300 procedure SetChangeVolProcW(hArcData : TArcHandle; pChangeVolProc : TChangeVolProcW); stdcall;
301 begin
302
303 end;
304
305 procedure SetProcessDataProcW(hArcData : TArcHandle; pProcessDataProc : TProcessDataProcW); stdcall;
306 var
307 Handle: TSevenZipHandle absolute hArcData;
308 begin
309 if (hArcData = wcxInvalidHandle) then
310 ProcessDataProcT:= pProcessDataProc
311 else begin
312 Handle.ProcessDataProc:= pProcessDataProc;
313 end;
314 end;
315
316 function PackFilesW(PackedFile: PWideChar; SubPath: PWideChar;
317 SrcPath: PWideChar; AddList: PWideChar; Flags: Integer): Integer; stdcall;
318 var
319 I, J: Integer;
320 Encrypt: Boolean;
321 AMessage: String;
322 Password: WideString;
323 FilePath: WideString;
324 FileName: WideString;
325 SfxModule: String = '';
326 FileNameUTF8: String;
327 AItem: TJclCompressionItem;
328 AProgress: TSevenZipUpdate;
329 Archive: TJclCompressArchive;
330 AFormats: TJclCompressArchiveClassArray;
331 begin
332 FileNameUTF8 := Utf16ToUtf8(WideString(PackedFile));
333
334 // If update existing archive
335 if (GetFileAttributesW(PackedFile) <> INVALID_FILE_ATTRIBUTES) then
336 AFormats := TJclCompressArchiveClassArray(FindUpdateFormats(FileNameUTF8))
337 else begin
338 if not SameText(ExtractFileExt(FileNameUTF8), '.exe') then
339 AFormats := FindCompressFormats(FileNameUTF8)
340 else begin
341 // Only 7-Zip supports self-extract
342 SfxModule := ExtractFilePath(SevenzipLibraryName) + '7z.sfx';
343 if FileExistsUTF8(SfxModule) then
344 begin
345 SetLength(AFormats, 1);
346 AFormats[0] := TJcl7zCompressArchive;
347 end
348 else begin
349 AMessage := SysErrorMessage(GetLastError) + LineEnding;
350 AMessage += rsSevenZipSfxNotFound + LineEnding + SfxModule;
351 MessageBoxW(0, PWideChar(UTF8ToUTF16(AMessage)), nil, MB_OK or MB_ICONERROR);
352 Exit(E_NO_FILES);
353 end;
354 end;
355 end;
356
357 for I := Low(AFormats) to High(AFormats) do
358 begin
359 Archive := AFormats[I].Create(FileNameUTF8, 0, False);
360 try
361 AProgress:= TSevenZipUpdate.Create(Archive);
362
363 if (Flags and PK_PACK_ENCRYPT) <> 0 then
364 begin
365 Encrypt:= Archive is IJclArchiveEncryptHeader;
366 if not ShowPasswordQuery(Encrypt, Password) then
367 Exit(E_EABORTED)
368 else begin
369 Archive.Password:= Password;
370 if Archive is TJcl7zUpdateArchive then TJcl7zUpdateArchive(Archive).SetEncryptHeader(Encrypt);
371 if Archive is TJcl7zCompressArchive then TJcl7zCompressArchive(Archive).SetEncryptHeader(Encrypt);
372 if Archive is TJclZipUpdateArchive then TJclZipUpdateArchive(Archive).SetEncryptionMethod(emAES256);
373 if Archive is TJclZipCompressArchive then TJclZipCompressArchive(Archive).SetEncryptionMethod(emAES256);
374 end;
375 end;
376
377 if (Archive is TJclUpdateArchive) then
378 try
379 TJclUpdateArchive(Archive).ListFiles;
380 except
381 Continue;
382 end;
383
384 SetArchiveOptions(Archive);
385
386 if Archive is TJcl7zCompressArchive then
387 begin
388 TJcl7zCompressArchive(Archive).SfxModule := SfxModule;
389 end;
390
391 if Assigned(SubPath) then
392 begin
393 FilePath:= WideString(SubPath);
394 if FilePath[Length(FilePath)] <> PathDelim then
395 FilePath := FilePath + PathDelim;
396 end;
397
398 while True do
399 begin
400 FileName := WideString(AddList);
401 FileNameUTF8:= Utf16ToUtf8(WideString(SrcPath + FileName));
402 if FileName[Length(FileName)] = PathDelim then
403 Archive.AddDirectory(FilePath + Copy(FileName, 1, Length(FileName) - 1), FileNameUTF8)
404 else
405 Archive.AddFile(FilePath + FileName, FileNameUTF8);
406 if (AddList + Length(FileName) + 1)^ = #0 then
407 Break;
408 Inc(AddList, Length(FileName) + 1);
409 end;
410
411 AProgress.Start;
412 Result:= AProgress.Update;
413
414 // If move files requested
415 if (Result = E_SUCCESS) and (Flags and PK_PACK_MOVE_FILES <> 0) then
416 begin
417 // First remove files
418 for J:= 0 to Archive.ItemCount - 1 do
419 begin
420 AItem:= Archive.Items[J];
421 if AItem.OperationSuccess = osOK then
422 begin
423 if not AItem.Directory then DeleteFileUtf8(AItem.FileName);
424 end;
425 end;
426 // Second remove directories
427 for J:= Archive.ItemCount - 1 downto 0 do
428 begin
429 AItem:= Archive.Items[J];
430 if AItem.Directory then RemoveDirUtf8(AItem.FileName);
431 end;
432 end;
433
434 Exit;
435 finally
436 Archive.Free;
437 AProgress.Free;
438 end;
439 end;
440 Result:= E_NOT_SUPPORTED;
441 end;
442
443 function DeleteFilesW(PackedFile, DeleteList: PWideChar): Integer; stdcall;
444 var
445 I: Integer;
446 PathEnd : WideChar;
447 FileList : PWideChar;
448 FileName : WideString;
449 FileNameUTF8 : String;
450 Archive: TJclUpdateArchive;
451 AProgress: TSevenZipUpdate;
452 AFormats: TJclUpdateArchiveClassArray;
453 begin
454 FileNameUTF8 := Utf16ToUtf8(WideString(PackedFile));
455 AFormats := FindUpdateFormats(FileNameUTF8);
456 for I := Low(AFormats) to High(AFormats) do
457 begin
458 Archive := AFormats[I].Create(FileNameUTF8, 0, False);
459 try
460 AProgress:= TSevenZipUpdate.Create(Archive);
461
462 try
463 Archive.ListFiles;
464 except
465 Continue;
466 end;
467
468 // Parse file list.
469 FileList := DeleteList;
470 while FileList^ <> #0 do
471 begin
472 FileName := FileList; // Convert PWideChar to WideString (up to first #0)
473 PathEnd := (FileList + Length(FileName) - 1)^;
474 // If ends with '.../*.*' or '.../' then delete directory.
475 if (PathEnd = '*') or (PathEnd = PathDelim) then
476 TJclSevenzipUpdateArchive(Archive).RemoveDirectory(WideExtractFilePath(FileName))
477 else
478 TJclSevenzipUpdateArchive(Archive).RemoveItem(FileName);
479
480 FileList := FileList + Length(FileName) + 1; // move after filename and ending #0
481 if FileList^ = #0 then
482 Break; // end of list
483 end;
484
485 AProgress.Start;
486 Exit(AProgress.Update);
487 finally
488 Archive.Free;
489 AProgress.Free;
490 end;
491 end;
492 Result:= E_NOT_SUPPORTED;
493 end;
494
495 function CanYouHandleThisFileW(FileName: PWideChar): Boolean; stdcall;
496 begin
497 Result:= FindDecompressFormats(Utf16ToUtf8(WideString(FileName))) <> nil;
498 end;
499
500 procedure PackSetDefaultParams(dps: PPackDefaultParamStruct); stdcall;
501 var
502 ModulePath: AnsiString;
503 begin
504 // Save configuration file name
505 ConfigFile:= ExtractFilePath(dps^.DefaultIniName);
506 ConfigFile:= WinCPToUTF8(ConfigFile) + DefaultIniName;
507 // Get plugin path
508 if GetModulePath(ModulePath) then
509 begin
510 // Use configuration from plugin path
511 if FileExistsUTF8(ModulePath + DefaultIniName) then
512 ConfigFile:= ModulePath + DefaultIniName;
513 end;
514 // Load plugin configuration
515 LoadConfiguration;
516 // Try to find library path
517 if FileExistsUTF8(LibraryPath) then
518 SevenzipLibraryName:= LibraryPath
519 else if Length(ModulePath) > 0 then
520 begin
521 if FileExistsUTF8(ModulePath + TargetCPU + PathDelim + SevenzipDefaultLibraryName) then
522 SevenzipLibraryName:= ModulePath + TargetCPU + PathDelim + SevenzipDefaultLibraryName
523 else if FileExistsUTF8(ModulePath + SevenzipDefaultLibraryName) then begin
524 SevenzipLibraryName:= ModulePath + SevenzipDefaultLibraryName;
525 end;
526 end;
527 // Process Xz files as archives
528 GetArchiveFormats.RegisterFormat(TJclXzDecompressArchive);
529 // Replace TJclXzCompressArchive by TJclXzCompressArchiveEx
530 GetArchiveFormats.UnregisterFormat(TJclXzCompressArchive);
531 GetArchiveFormats.RegisterFormat(TJclXzCompressArchiveEx);
532 // Don't process PE files as archives
533 GetArchiveFormats.UnregisterFormat(TJclPeDecompressArchive);
534 // Try to load 7z.dll
535 if (Is7ZipLoaded or Load7Zip) then
536 LoadLibraries
537 else begin
538 MessageBoxW(0, PWideChar(UTF8ToUTF16(rsSevenZipLoadError)), 'SevenZip', MB_OK or MB_ICONERROR);
539 end;
540 // Create password cache object
541 PasswordCache:= TPasswordCache.Create;
542 end;
543
544 procedure ConfigurePacker(Parent: WcxPlugin.HWND; DllInstance: THandle); stdcall;
545 begin
546 ShowConfigurationDialog(Parent);
547 end;
548
549 { TSevenZipUpdate }
550
551 constructor TSevenZipUpdate.Create;
552 begin
553 inherited Create(True);
554 FPause:= TEventObject.Create(nil, False, False, '');
555 FProgress:= TEventObject.Create(nil, False, False, '');
556 end;
557
558 constructor TSevenZipUpdate.Create(Archive: TJclCompressionArchive);
559 begin
560 Create;
561 FArchive:= Archive;
562 Archive.OnPassword:= JclCompressionPassword;
563 end;
564
565 destructor TSevenZipUpdate.Destroy;
566 begin
567 FPause.Free;
568 FProgress.Free;
569 inherited Destroy;
570 end;
571
572 procedure TSevenZipUpdate.Execute;
573 begin
574 try
575 (FArchive as TJclCompressArchive).Compress;
576 ReturnValue:= E_SUCCESS;
577 except
578 on E: Exception do
579 ReturnValue:= GetArchiveError(E);
580 end;
581 Terminate;
582 FProgress.SetEvent;
583 end;
584
585 function TSevenZipUpdate.Update: Integer;
586 begin
587 FArchive.OnProgress:= JclCompressionProgress;
588 while not Terminated do
589 begin
590 // Wait progress event
591 FProgress.WaitFor(INFINITE);
592 // If the user has clicked on Cancel, the function returns zero
593 FArchive.CancelCurrentOperation:= (ProcessDataProcT(PWideChar(FFileName), -FPercent) = 0);
594 // Drop pause
595 FPause.SetEvent;
596 end;
597 Result:= ReturnValue;
598 end;
599
600 procedure TSevenZipUpdate.JclCompressionPassword(Sender: TObject;
601 var Password: WideString);
602 var
603 Encrypt: Boolean = False;
604 begin
605 if not ShowPasswordQuery(Encrypt, Password) then
606 raise ESevenZipAbort.Create(EmptyStr);
607 end;
608
609 procedure TSevenZipUpdate.JclCompressionProgress(Sender: TObject; const Value, MaxValue: Int64);
610 begin
611 if MaxValue > 0 then begin
612 FPercent:= (Value * 100) div MaxValue;
613 end;
614 if FArchive.ItemCount > 0 then begin
615 FFileName:= FArchive.Items[FArchive.CurrentItemIndex].PackedName;
616 end;
617 // Fire progress event
618 FProgress.SetEvent;
619 // Check pause progress
620 FPause.WaitFor(INFINITE);
621 end;
622
623 { TSevenZipHandle }
624
625 procedure TSevenZipHandle.Execute;
626 begin
627 try
628 SetLength(ProcessArray, ProcessIndex);
629 TJclSevenzipDecompressArchive(FArchive).ProcessSelected(ProcessArray, OperationMode = PK_TEST);
630 ReturnValue:= E_SUCCESS;
631 except
632 on E: Exception do
633 begin
634 ReturnValue:= GetArchiveError(E);
635 MessageBoxW(0, PWideChar(UTF8ToUTF16(E.Message)), nil, MB_OK or MB_ICONERROR);
636 end;
637 end;
638 Terminate;
639 FProgress.SetEvent;
640 end;
641
642 function TSevenZipHandle.Update: Integer;
643 begin
644 FArchive.OnProgress:= JclCompressionProgress;
645 while not Terminated do
646 begin
647 // Wait progress event
648 FProgress.WaitFor(INFINITE);
649 if Assigned(ProcessDataProc) then
650 begin
651 // If the user has clicked on Cancel, the function returns zero
652 FArchive.CancelCurrentOperation:= ProcessDataProc(PWideChar(FFileName), -FPercent) = 0;
653 end;
654 // Drop pause
655 FPause.SetEvent;
656 end;
657 Result:= ReturnValue;
658 end;
659
660 procedure TSevenZipHandle.SetArchive(AValue: TJclDecompressArchive);
661 begin
662 FArchive:= AValue;
663 AValue.OnPassword := JclCompressionPassword;
664 AValue.OnExtract := JclCompressionExtract;
665 end;
666
667 function TSevenZipHandle.JclCompressionExtract(Sender: TObject; AIndex: Integer;
668 var AFileName: TFileName; var Stream: TStream; var AOwnsStream: Boolean): Boolean;
669 begin
670 Result:= True;
671 AFileName:= FileName[AIndex];
672 end;
673
674 { TPasswordCache }
675
676 procedure TPasswordCache.ResetTimer;
677 begin
678 if FTimer.Interval > FInterval then
679 FTimer.Interval:= FTimer.Interval - 1
680 else
681 FTimer.Interval:= FTimer.Interval + 1;
682 end;
683
684 procedure TPasswordCache.ZeroPassword;
685 begin
686 if (Length(FArchivePassword) > 0) then
687 begin
688 FillWord(FArchivePassword[1], Length(FArchivePassword), 0);
689 SetLength(FArchivePassword, 0);
690 end;
691 end;
692
693 procedure TPasswordCache.TimerEvent(Sender: TObject);
694 begin
695 FMutex.Acquire;
696 try
697 ZeroPassword;
698 FTimer.Enabled:= False;
699 finally
700 FMutex.Release;
701 end;
702 end;
703
704 function TPasswordCache.GetPassword(const Archive: String): WideString;
705 begin
706 FMutex.Acquire;
707 try
708 if (SameText(FArchiveName, Archive)) and
709 (FArchiveSize = FileSizeUtf8(Archive)) and
710 (FArchiveTime = FileAgeUtf8(Archive)) then
711 begin
712 ResetTimer;
713 Result:= FArchivePassword
714 end
715 else begin
716 FTimer.Enabled:= False;
717 Result:= EmptyWideStr;
718 ZeroPassword;
719 end;
720 finally
721 FMutex.Release;
722 end;
723 end;
724
725 procedure TPasswordCache.SetPassword(const Archive: String; const Password: WideString);
726 begin
727 FMutex.Acquire;
728 try
729 if (Length(Password) = 0) then
730 FArchiveName:= EmptyStr
731 else begin
732 FArchiveName:= Archive;
733 FArchivePassword:= Password;
734 FArchiveTime:= FileAgeUtf8(Archive);
735 FArchiveSize:= FileSizeUtf8(Archive);
736 FTimer.Enabled:= True;
737 ResetTimer;
738 end;
739 finally
740 FMutex.Release;
741 end;
742 end;
743
744 constructor TPasswordCache.Create;
745 begin
746 FTimer:= TFPTimer.Create(nil);
747 FTimer.UseTimerThread:= True;
748 FTimer.OnTimer:= TimerEvent;
749 FTimer.Interval:= FInterval;
750 FMutex:= TCriticalSection.Create;
751 end;
752
753 destructor TPasswordCache.Destroy;
754 begin
755 FTimer.Free;
756 FMutex.Free;
757 inherited Destroy;
758 end;
759
760 end.
761