Debian Codegraph @

plugins/wcx/base64/src/base64func.pas

raw
1 {
2 Double Commander
3 -------------------------------------------------------------------------
4 Base64 archiver plugin
5
6 Copyright (C) 2022-2025 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 program. If not, see <http://www.gnu.org/licenses/>.
20 }
21
22 unit Base64Func;
23
24 {$mode objfpc}{$H+}
25 {$include calling.inc}
26
27 interface
28
29 uses
30 Classes, WcxPlugin;
31
32 { Mandatory functions }
33 function OpenArchiveW(var ArchiveData: TOpenArchiveDataW): TArcHandle; dcpcall; export;
34 function ReadHeaderExW(hArcData: TArcHandle; var HeaderData: THeaderDataExW): Integer; dcpcall; export;
35 function ProcessFileW(hArcData: TArcHandle; Operation: Integer; DestPath, DestName: PWideChar): Integer; dcpcall; export;
36 function CloseArchive (hArcData: TArcHandle): Integer; dcpcall; export;
37 procedure SetChangeVolProcW(hArcData: TArcHandle; pChangeVolProc: TChangeVolProcW); dcpcall; export;
38 procedure SetProcessDataProcW(hArcData: TArcHandle; pProcessDataProc: TProcessDataProcW); dcpcall; export;
39
40 { Optional functions }
41 function PackFilesW(PackedFile: PWideChar; SubPath: PWideChar; SrcPath: PWideChar; AddList: PWideChar; Flags: Integer): Integer; dcpcall; export;
42 function GetBackgroundFlags: Integer; dcpcall; export;
43 function GetPackerCaps: Integer; dcpcall; export;
44
45 implementation
46
47 uses
48 SysUtils, Base64Buf, NullStream, LazFileUtils, DCConvertEncoding, DCOSUtils,
49 DCStrUtils, DCClassesUtf8, MimeInLn;
50
51 const
52 BUFFER_SIZE = 32768;
53
54 MIME_VERSION = 'MIME-VERSION:';
55 CONTENT_TYPE = 'CONTENT-TYPE:';
56 CONTENT_DISPOSITION = 'CONTENT-DISPOSITION:';
57 CONTENT_TRANSFER_ENCODING = 'CONTENT-TRANSFER-ENCODING:';
58
59 MIME_HEADER = 'MIME-Version: 1.0' + LineEnding +
60 'Content-Type: application/octet-stream; name="%s"' + LineEnding +
61 'Content-Transfer-Encoding: base64' + LineEnding +
62 'Content-Disposition: attachment; filename="%s"' + LineEnding + LineEnding;
63
64 threadvar
65 gProcessDataProcW : TProcessDataProcW;
66
67 type
68 TRecord = class
69 Count: Integer;
70 FileName: String;
71 Stream: TFileStreamEx;
72 ProcessDataProcW: TProcessDataProcW;
73 end;
74
75 function ParseHeader(var AHandle: TRecord): Boolean;
76 var
77 P, ALength: Integer;
78 S, AText, ABuffer: String;
79 begin
80 SetLength(ABuffer, 4096);
81 ALength:= AHandle.Stream.Read(ABuffer[1], Length(ABuffer));
82 if ALength > 0 then
83 begin
84 SetLength(ABuffer, ALength);
85 AText:= Copy(ABuffer, 1, Length(MIME_VERSION));
86 // No MIME-header, assume raw Base64 data
87 if CompareStr(MIME_VERSION, UpperCase(AText)) <> 0 then
88 AHandle.Stream.Seek(0, soBeginning)
89 else begin
90 P:= 1;
91 while GetNextLine(ABuffer, AText, P) do
92 begin
93 // Base64 data starts after empty line
94 if (Length(AText) = 0) then
95 begin
96 AHandle.Stream.Seek(P - 1, soBeginning);
97 Break;
98 end;
99 S:= UpperCase(AText);
100 if StrBegins(S, CONTENT_TYPE) then
101 begin
102 S:= SeparateRight(S, ':');
103 S:= Trim(SeparateLeft(S, ';'));
104 if (S = 'MESSAGE/PARTIAL') then Exit(False);
105 AHandle.FileName:= GetParameter(AText, 'name');
106 AHandle.FileName:= ExtractFileName(InlineDecodeEx(AHandle.FileName));
107 end
108 else if StrBegins(S, CONTENT_TRANSFER_ENCODING) then
109 begin
110 S:= Trim(SeparateRight(S, ':'));
111 if (S <> 'BASE64') then Exit(False);
112 end
113 else if StrBegins(S, CONTENT_DISPOSITION) then
114 begin
115 S:= SeparateRight(S, ':');
116 S:= Trim(SeparateLeft(S, ';'));
117 if (S <> 'ATTACHMENT') then Exit(False);
118 AHandle.FileName:= GetParameter(AText, 'filename');
119 AHandle.FileName:= ExtractFileName(InlineDecodeEx(AHandle.FileName));
120 end;
121 end;
122 end;
123 end;
124 Result:= True;
125 end;
126
127 { Mandatory functions }
128
129 function OpenArchiveW(var ArchiveData: TOpenArchiveDataW): TArcHandle; dcpcall; export;
130 var
131 AHandle: TRecord absolute Result;
132 begin
133 AHandle:= TRecord.Create;
134 try
135 AHandle.Stream:= TFileStreamEx.Create(CeUtf16ToUtf8(ArchiveData.ArcName),
136 fmOpenRead or fmShareDenyNone);
137
138 if not ParseHeader(AHandle) then
139 begin
140 AHandle.Stream.Free;
141 FreeAndNil(AHandle);
142 ArchiveData.OpenResult:= E_UNKNOWN_FORMAT;
143 end
144 else if Length(AHandle.FileName) = 0 then
145 begin
146 AHandle.FileName:= ExtractFileNameOnly(AHandle.Stream.FileName);
147 end;
148 except
149 AHandle.Stream.Free;
150 FreeAndNil(AHandle);
151 ArchiveData.OpenResult:= E_EOPEN;
152 end;
153 end;
154
155 function ReadHeaderExW(hArcData: TArcHandle; var HeaderData: THeaderDataExW): Integer; dcpcall; export;
156 var
157 PackSize: Int64;
158 FileName: UnicodeString;
159 AHandle: TRecord absolute hArcData;
160 begin
161 if AHandle.Count > 0 then
162 Result:= E_END_ARCHIVE
163 else begin
164 Result := E_SUCCESS;
165 FileName:= CeUtf8ToUtf16(AHandle.FileName);
166 FillChar(HeaderData, SizeOf(AHandle.Count), 0);
167 HeaderData.UnpSize:= $FFFFFFFE;
168 HeaderData.UnpSizeHigh:= $FFFFFFFF;
169 PackSize:= AHandle.Stream.Size - AHandle.Stream.Position;
170 HeaderData.PackSize:= Int64Rec(PackSize).Lo;
171 HeaderData.PackSizeHigh:= Int64Rec(PackSize).Hi;
172 StrPLCopy(HeaderData.FileName, FileName, SizeOf(HeaderData.FileName) - 1);
173 end;
174 end;
175
176 function ProcessFileW(hArcData: TArcHandle; Operation: Integer; DestPath, DestName: PWideChar) : Integer; dcpcall; export;
177 var
178 ARead: Integer;
179 ABuffer: TBytes;
180 AFileSize: Int64;
181 fsOutput: TStream;
182 APercent: Integer;
183 ATargetName: String;
184 ASourceName: UnicodeString;
185 AStream: TBase64DecodingStreamEx;
186 AHandle: TRecord absolute hArcData;
187 begin
188 case Operation of
189 PK_TEST,
190 PK_EXTRACT:
191 begin
192 try
193 if Operation = PK_TEST then
194 fsOutput:= TNullStream.Create
195 else begin
196 ATargetName:= CeUtf16ToUtf8(DestPath) + CeUtf16ToUtf8(DestName);
197 fsOutput:= TFileStreamEx.Create(ATargetName, fmCreate);
198 end;
199 try
200 AStream:= TBase64DecodingStreamEx.Create(AHandle.Stream);
201 try
202 AFileSize:= AHandle.Stream.Size;
203 SetLength(ABuffer, BUFFER_SIZE);
204 ASourceName:= CeUtf8ToUtf16(AHandle.FileName);
205 repeat
206 ARead:= AStream.Read(ABuffer[0], BUFFER_SIZE);
207 if ARead > 0 then
208 begin
209 fsOutput.WriteBuffer(ABuffer[0], ARead);
210 APercent:= (AStream.Source.Position * 100) div AFileSize;
211 if (AHandle.ProcessDataProcW(PWideChar(ASourceName), -APercent) = 0) then
212 begin
213 FreeAndNil(fsOutput);
214 if Operation = PK_EXTRACT then mbDeleteFile(ATargetName);
215 Exit(E_EABORTED);
216 end;
217 end;
218 until ARead < BUFFER_SIZE;
219 finally
220 AStream.Free;
221 end;
222 finally
223 fsOutput.Free;
224 end;
225 except
226 Exit(E_ECREATE);
227 end;
228 end;
229
230 PK_SKIP:
231 begin
232
233 end;
234 end;
235 Inc(AHandle.Count);
236 Result:= E_SUCCESS;
237 end;
238
239 function CloseArchive (hArcData: TArcHandle): Integer; dcpcall; export;
240 var
241 AHandle: TRecord absolute hArcData;
242 begin
243 Result := E_SUCCESS;
244 AHandle.Stream.Free;
245 AHandle.Free;
246 end;
247
248 procedure SetChangeVolProcW(hArcData: TArcHandle; pChangeVolProc: TChangeVolProcW); dcpcall; export;
249 begin
250
251 end;
252
253 procedure SetProcessDataProcW(hArcData: TArcHandle; pProcessDataProc: TProcessDataProcW); dcpcall; export;
254 var
255 AHandle: TRecord absolute hArcData;
256 begin
257 if (hArcData <> wcxInvalidHandle) then
258 AHandle.ProcessDataProcW := pProcessDataProc
259 else begin
260 gProcessDataProcW := pProcessDataProc;
261 end;
262 end;
263
264 { Optional functions }
265
266 function PackFilesW(PackedFile: PWideChar; SubPath: PWideChar; SrcPath: PWideChar; AddList: PWideChar; Flags: Integer): Integer; dcpcall; export;
267 var
268 ARead: Integer;
269 ABuffer: TBytes;
270 AHeader: String;
271 AFileName: String;
272 ATargetName: String;
273 AStream: TBase64EncodingStreamEx;
274 fsInput, fsOutput: TFileStreamEx;
275 begin
276 if (Flags and PK_PACK_MOVE_FILES) <> 0 then begin
277 Exit(E_NOT_SUPPORTED);
278 end;
279
280 try
281 AFileName:= CeUtf16ToUtf8(AddList);
282 fsInput:= TFileStreamEx.Create(CeUtf16ToUtf8(SrcPath) + AFileName, fmOpenRead or fmShareDenyNone);
283 try
284 ATargetName:= CeUtf16ToUtf8(PackedFile);
285 try
286 fsOutput:= TFileStreamEx.Create(ATargetName, fmCreate);
287 try
288 AFileName:= InlineEncodeEx(AFileName);
289 AHeader:= Format(MIME_HEADER, [AFileName, AFileName]);
290 try
291 fsOutput.WriteBuffer(AHeader[1], Length(AHeader));
292 AStream:= TBase64EncodingStreamEx.Create(fsOutput);
293 try
294 SetLength(ABuffer, BUFFER_SIZE);
295 repeat
296 ARead:= fsInput.Read(ABuffer[0], BUFFER_SIZE);
297 if ARead > 0 then
298 begin
299 AStream.WriteBuffer(ABuffer[0], ARead);
300 if (gProcessDataProcW(PackedFile, ARead) = 0) then
301 begin
302 FreeAndNil(AStream);
303 FreeAndNil(fsOutput);
304 mbDeleteFile(ATargetName);
305 Exit(E_EABORTED);
306 end;
307 end;
308 until ARead < BUFFER_SIZE;
309 finally
310 AStream.Free;
311 end;
312 except
313 Exit(E_EWRITE);
314 end;
315 finally
316 fsOutput.Free;
317 end;
318 except
319 Exit(E_ECREATE);
320 end;
321 finally
322 fsInput.Free;
323 end;
324 except
325 Exit(E_EOPEN);
326 end;
327
328 Result:= E_SUCCESS;
329 end;
330
331 function GetBackgroundFlags: Integer; dcpcall; export;
332 begin
333 Result:= BACKGROUND_UNPACK or BACKGROUND_PACK;
334 end;
335
336 function GetPackerCaps: Integer; dcpcall; export;
337 begin
338 Result := PK_CAPS_NEW;
339 end;
340
341 end.