Debian Codegraph @

plugins/wcx/zip/src/ZipCache.pas

raw
1 unit ZipCache;
2
3 {$mode ObjFPC}{$H+}
4
5 interface
6
7 uses
8 Classes, SysUtils, SyncObjs, fpTimer;
9
10 type
11
12 { TPasswordCache }
13
14 TPasswordCache = class
15 private
16 FTimer: TFPTimer;
17 FArchiveSize: Int64;
18 FArchiveName: String;
19 FArchiveTime: Integer;
20 FMutex: TCriticalSection;
21 FArchivePassword: String;
22 const FInterval: Cardinal = 120000;
23 private
24 procedure ResetTimer;
25 procedure ZeroPassword;
26 procedure TimerEvent(Sender: TObject);
27 public
28 constructor Create;
29 destructor Destroy; override;
30 function GetPassword(const Archive: String): String;
31 procedure SetPassword(const Archive: String; const Password: String);
32 end;
33
34 implementation
35
36 uses
37 LazFileUtils;
38
39 { TPasswordCache }
40
41 procedure TPasswordCache.ResetTimer;
42 begin
43 if FTimer.Interval > FInterval then
44 FTimer.Interval:= FTimer.Interval - 1
45 else
46 FTimer.Interval:= FTimer.Interval + 1;
47 end;
48
49 procedure TPasswordCache.ZeroPassword;
50 begin
51 if (Length(FArchivePassword) > 0) then
52 begin
53 FillChar(FArchivePassword[1], Length(FArchivePassword), #0);
54 SetLength(FArchivePassword, 0);
55 end;
56 end;
57
58 procedure TPasswordCache.TimerEvent(Sender: TObject);
59 begin
60 FMutex.Acquire;
61 try
62 ZeroPassword;
63 FTimer.Enabled:= False;
64 finally
65 FMutex.Release;
66 end;
67 end;
68
69 function TPasswordCache.GetPassword(const Archive: String): String;
70 begin
71 FMutex.Acquire;
72 try
73 if (SameText(FArchiveName, Archive)) and
74 (FArchiveSize = FileSizeUtf8(Archive)) and
75 (FArchiveTime = FileAgeUtf8(Archive)) then
76 begin
77 ResetTimer;
78 Result:= FArchivePassword
79 end
80 else begin
81 FTimer.Enabled:= False;
82 Result:= EmptyStr;
83 ZeroPassword;
84 end;
85 finally
86 FMutex.Release;
87 end;
88 end;
89
90 procedure TPasswordCache.SetPassword(const Archive: String; const Password: String);
91 begin
92 FMutex.Acquire;
93 try
94 if (Length(Password) = 0) then
95 FArchiveName:= EmptyStr
96 else begin
97 FArchiveName:= Archive;
98 FArchivePassword:= Password;
99 FArchiveTime:= FileAgeUtf8(Archive);
100 FArchiveSize:= FileSizeUtf8(Archive);
101 FTimer.Enabled:= True;
102 ResetTimer;
103 end;
104 finally
105 FMutex.Release;
106 end;
107 end;
108
109 constructor TPasswordCache.Create;
110 begin
111 FTimer:= TFPTimer.Create(nil);
112 FTimer.UseTimerThread:= True;
113 FTimer.OnTimer:= @TimerEvent;
114 FTimer.Interval:= FInterval;
115 FMutex:= TCriticalSection.Create;
116 end;
117
118 destructor TPasswordCache.Destroy;
119 begin
120 FTimer.Free;
121 FMutex.Free;
122 inherited Destroy;
123 end;
124
125 end.
126