Debian Codegraph @

plugins/wcx/unrar/src/unrarfunc.pas

raw
1 {
2 Double commander
3 -------------------------------------------------------------------------
4 WCX plugin for unpacking RAR archives
5 This is simple wrapper for unrar.dll or libunrar.so
6
7 Copyright (C) 2008-2024 Alexander Koblov ([email protected])
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation; either version 2 of the
12 License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 in a file called COPYING along with this program; if not, write to
21 the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
22 02139, USA.
23 }
24
25 unit UnRARFunc;
26
27 {$mode objfpc}{$H+}
28 {$if FPC_FULLVERSION >= 30300}
29 {$modeswitch arraytodynarray}
30 {$endif}
31 {$include calling.inc}
32
33 interface
34
35 uses
36 DynLibs, WcxPlugin, Extension;
37
38 const
39 {$IF DEFINED(MSWINDOWS)}
40 // libunrar must be built with sizeof(wchar_t) = 2 (default on Windows)
41 _unrar = 'unrar.dll';
42 {$ELSEIF DEFINED(DARWIN)}
43 // libunrar must be built with sizeof(wchar_t) = 4 (default on Unix)
44 _unrar = 'libunrar.dylib';
45 {$ELSEIF DEFINED(UNIX)}
46 // libunrar must be built with sizeof(wchar_t) = 4 (default on Unix)
47 _unrar = 'libunrar.so';
48 {$ENDIF}
49
50 const
51 // Unrar callback messages.
52 UCM_CHANGEVOLUME = 0;
53 UCM_PROCESSDATA = 1;
54 UCM_NEEDPASSWORD = 2;
55 UCM_CHANGEVOLUMEW = 3;
56 UCM_NEEDPASSWORDW = 4;
57 UCM_LARGEDICT = 5;
58
59 // Main header flags.
60 MHD_VOLUME = $0001;
61 MHD_COMMENT = $0002;
62 MHD_LOCK = $0004;
63 MHD_SOLID = $0008;
64 MHD_PACK_COMMENT = $0010;
65 MHD_NEWNUMBERING = $0010;
66 MHD_AV = $0020; // (archive signed)
67 MHD_PROTECT = $0040;
68 MHD_PASSWORD = $0080;
69 MHD_FIRSTVOLUME = $0100;
70 MHD_ENCRYPTVER = $0200;
71
72 type
73
74 {$IFDEF UNIX}
75 TRarUnicodeChar = UCS4Char;
76 TRarUnicodeString = UCS4String;
77 {$ENDIF}
78
79 {$IFDEF WINDOWS}
80 TRarUnicodeChar = WideChar; // assuming 2 byte WideChar
81 TRarUnicodeString = UnicodeString;
82 {$ENDIF}
83
84 PRarUnicodeChar = ^TRarUnicodeChar;
85 TRarUnicodeArray = packed array [0..1023] of TRarUnicodeChar;
86
87 RARHeaderDataEx = packed record
88 ArcName: packed array [0..1023] of Char;
89 ArcNameW: TRarUnicodeArray;
90 FileName: packed array [0..1023] of Char;
91 FileNameW: TRarUnicodeArray;
92 Flags: LongWord;
93 PackSize: LongWord;
94 PackSizeHigh: LongWord;
95 UnpSize: LongWord;
96 UnpSizeHigh: LongWord;
97 HostOS: LongWord;
98 FileCRC: LongWord;
99 FileTime: LongWord;
100 UnpVer: LongWord;
101 Method: LongWord;
102 FileAttr: LongWord;
103 CmtBuf: PChar;
104 CmtBufSize: LongWord;
105 CmtSize: LongWord;
106 CmtState: LongWord;
107 DictSize: LongWord;
108 HashType: LongWord;
109 Hash: array[0..31] of Byte;
110 RedirType: LongWord;
111 RedirName: PRarUnicodeChar;
112 RedirNameSize: LongWord;
113 DirTarget: LongWord;
114 MtimeLow: LongWord;
115 MtimeHigh: LongWord;
116 CtimeLow: LongWord;
117 CtimeHigh: LongWord;
118 AtimeLow: LongWord;
119 AtimeHigh: LongWord;
120 ArcNameEx: PRarUnicodeChar;
121 ArcNameExSize: LongWord;
122 FileNameEx: PRarUnicodeChar;
123 FileNameExSize: LongWord;
124 Reserved: packed array [0..981] of LongWord;
125 end;
126
127 {$IFDEF MSWINDOWS}{$CALLING STDCALL}{$ELSE}{$CALLING CDECL}{$ENDIF}
128
129 TUnrarCallback = function(Msg: LongWord; UserData, P1: Pointer; P2: PtrInt): Integer;
130
131 RAROpenArchiveDataEx = packed record
132 ArcName: PAnsiChar;
133 ArcNameW: PRarUnicodeChar;
134 OpenMode: LongWord;
135 OpenResult: LongWord;
136 CmtBuf: PChar;
137 CmtBufSize: LongWord;
138 CmtSize: LongWord;
139 CmtState: LongWord;
140 Flags: LongWord;
141 Callback: TUnrarCallback;
142 UserData: PtrInt;
143 Reserved: packed array [0..27] of LongWord;
144 end;
145
146 TRAROpenArchiveEx = function(var ArchiveData: RAROpenArchiveDataEx) : TArcHandle;
147 TRARCloseArchive = function(hArcData: TArcHandle) : Integer;
148 TRARReadHeaderEx = function (hArcData: TArcHandle; var HeaderData: RARHeaderDataEx) : Integer;
149 TRARProcessFileW = function(hArcData: TArcHandle; Operation: Integer; DestPath, DestName: PRarUnicodeChar) : Integer;
150 TRARSetCallback = procedure(hArcData: TArcHandle; UnrarCallback: TUnrarCallback; UserData: PtrInt);
151 TRARSetPassword = procedure(hArcData: TArcHandle; Password: PChar);
152 TRARGetDllVersion = function: Integer;
153
154 {$CALLING DEFAULT}
155
156 var
157 RAROpenArchiveEx : TRAROpenArchiveEx = nil;
158 RARCloseArchive : TRARCloseArchive = nil;
159 RARReadHeaderEx : TRARReadHeaderEx = nil;
160 RARProcessFileW : TRARProcessFileW = nil;
161 RARSetCallback : TRARSetCallback = nil;
162 RARSetPassword : TRARSetPassword = nil;
163 RARGetDllVersion : TRARGetDllVersion = nil;
164
165 ModuleHandle : TLibHandle = NilHandle;
166
167 { Mandatory }
168 function OpenArchive(var ArchiveData: TOpenArchiveData) : TArcHandle; dcpcall; export;
169 function OpenArchiveW(var ArchiveData: tOpenArchiveDataW) : TArcHandle; dcpcall; export;
170 function ReadHeader(hArcData: TArcHandle; var HeaderData: THeaderData) : Integer; dcpcall; export;
171 function ReadHeaderExW(hArcData: TArcHandle; var HeaderData: THeaderDataExW) : Integer; dcpcall; export;
172 function ProcessFile(hArcData: TArcHandle; Operation: Integer; DestPath, DestName: PAnsiChar) : Integer; dcpcall; export;
173 function ProcessFileW(hArcData: TArcHandle; Operation: Integer; DestPath, DestName: PWideChar) : Integer; dcpcall; export;
174 function CloseArchive(hArcData: TArcHandle): Integer; dcpcall; export;
175 procedure SetChangeVolProc(hArcData : TArcHandle; pChangeVolProc : TChangeVolProc); dcpcall; export;
176 procedure SetChangeVolProcW(hArcData : TArcHandle; pChangeVolProc : TChangeVolProcW); dcpcall; export;
177 procedure SetProcessDataProc(hArcData : TArcHandle; pProcessDataProc : TProcessDataProc); dcpcall; export;
178 procedure SetProcessDataProcW(hArcData : TArcHandle; pProcessDataProc : TProcessDataProcW); dcpcall; export;
179 { Optional }
180 function GetPackerCaps : Integer; dcpcall; export;
181 function GetBackgroundFlags: Integer; dcpcall; export;
182 { Extension API }
183 procedure ExtensionInitialize(StartupInfo: PExtensionStartupInfo); dcpcall; export;
184
185 var
186 gStartupInfo: TExtensionStartupInfo;
187
188 threadvar
189 ProcessDataProcW : TProcessDataProcW;
190
191 implementation
192
193 uses
194 SysUtils, DCBasicTypes, DCDateTimeUtils, DCConvertEncoding, DCFileAttributes,
195 RarLng;
196
197 type
198 // From libunrar (dll.hpp)
199 RarHostSystem = (
200 HOST_MSDOS = 0,
201 HOST_OS2 = 1,
202 HOST_WIN32 = 2,
203 HOST_UNIX = 3,
204 HOST_MACOS = 4,
205 HOST_BEOS = 5,
206 HOST_MAX
207 );
208
209 TRARHandle = class
210 Handle: TArcHandle;
211 ChangeVolProcW: TChangeVolProcW;
212 ProcessDataProcW: TProcessDataProcW;
213 ProcessFileNameW: array [0..1023] of WideChar;
214 end;
215
216 function StrLCopy(Dest, Source: PRarUnicodeChar; MaxLen: SizeInt): PRarUnicodeChar; overload;
217 var
218 ACounter: SizeInt;
219 begin
220 ACounter := 0;
221 while (Source[ACounter] <> TRarUnicodeChar(0)) and (ACounter < MaxLen) do
222 begin
223 Dest[ACounter] := TRarUnicodeChar(Source[ACounter]);
224 Inc(ACounter);
225 end;
226 Dest[ACounter] := TRarUnicodeChar(0);
227 StrLCopy := Dest;
228 end;
229
230 procedure StringToArrayW(src: UnicodeString;
231 pDst: PWideChar;
232 MaxDstLength: Integer);
233 begin
234 if Length(src) < MaxDstLength then
235 MaxDstLength := Length(src)
236 else
237 MaxDstLength := MaxDstLength - 1; // for ending #0
238
239 if Length(src) > 0 then
240 Move(src[1], pDst^, SizeOf(WideChar) * MaxDstLength);
241 pDst[MaxDstLength] := WideChar(0);
242 end;
243
244 function RarUnicodeStringToWideString(src: TRarUnicodeString): UnicodeString;
245 begin
246 {$IFDEF UNIX}
247 Result := UCS4StringToUnicodeString(src);
248 {$ELSE}
249 Result := src;
250 {$ENDIF}
251 end;
252
253 function WideStringToRarUnicodeString(src: UnicodeString): TRarUnicodeString;
254 begin
255 {$IFDEF UNIX}
256 Result := UnicodeStringToUCS4String(src);
257 {$ELSE}
258 Result := src;
259 {$ENDIF}
260 end;
261
262 function GetSystemSpecificFileTime(FileTime: LongInt) : LongInt;
263 begin
264 Result := FileTime;
265
266 {$IFDEF UNIX}
267 Result := LongInt(DateTimeToUnixFileTime(DosFileTimeToDateTime(TDosFileTime(Result))));
268 {$ENDIF}
269 end;
270
271 function GetSystemSpecificAttributes(HostOS: RarHostSystem; Attrs: LongInt): LongInt;
272 begin
273 Result := Attrs;
274
275 {$IFDEF MSWINDOWS}
276 if (HostOS = HOST_UNIX) or
277 // Ugly hack: $1FFFF is max value of attributes on Windows
278 (Result > $1FFFF) then
279 begin
280 Result := LongInt(UnixToWinFileAttr(TFileAttrs(Attrs)));
281 end;
282 {$ENDIF}
283 {$IFDEF UNIX}
284 if HostOS in [HOST_MSDOS, HOST_WIN32] then
285 Result := LongInt(WinToUnixFileAttr(TFileAttrs(Result)));
286 {$ENDIF}
287 end;
288
289 function UnrarCallback(Msg: LongWord; UserData, P1: Pointer; P2: PtrInt) : Integer; dcpcall;
290 const
291 Giga = 1024 * 1024;
292 var
293 PasswordU: String;
294 Buttons: PPAnsiChar;
295 DictSize: UIntPtr absolute P1;
296 VolumeNameA: TRarUnicodeArray;
297 VolumeNameU: TRarUnicodeString;
298 PasswordA: array[0..511] of AnsiChar;
299 AHandle: TRARHandle absolute UserData;
300 VolumeNameW: array [0..1023] of WideChar;
301 begin
302 Result := 0;
303 case Msg of
304 UCM_CHANGEVOLUMEW:
305 begin
306 if Assigned(AHandle.ChangeVolProcW) then
307 begin
308 Move(PRarUnicodeChar(P1)^, VolumeNameA[0], SizeOf(TRarUnicodeArray));
309 VolumeNameW := RarUnicodeStringToWideString(VolumeNameA);
310 if AHandle.ChangeVolProcW(VolumeNameW, LongInt(P2)) = 0 then
311 Result := -1
312 else begin
313 Result := 1;
314 if (P2 = PK_VOL_ASK) then
315 begin
316 VolumeNameU := WideStringToRarUnicodeString(VolumeNameW);
317 Move(PRarUnicodeChar(VolumeNameU)^, P1^, SizeOf(TRarUnicodeArray));
318 end;
319 end;
320 end
321 else begin
322 Result := -1;
323 end;
324 end;
325 UCM_PROCESSDATA:
326 begin
327 // P1 - pointer to data buffer (first param of ProcessDataProc)
328 // P2 - number of bytes in the buffer (second param of ProcessDataProc)
329 if Assigned(AHandle.ProcessDataProcW) then
330 begin
331 if AHandle.ProcessDataProcW(PWideChar(AHandle.ProcessFileNameW), LongInt(P2)) = 0 then
332 Result := -1;
333 end;
334 end;
335 UCM_NEEDPASSWORDW:
336 begin
337 // DLL needs a password to process archive. This message must be
338 // processed if you wish to be able to handle encrypted archives.
339 // Return zero or a positive value to continue process or -1
340 // to cancel the archive operation.
341 // P1 - contains the address pointing to the buffer for a password.
342 // You need to copy a password here.
343 // P2 - contains the size of password buffer in characters.
344 StrLCopy(VolumeNameA, PRarUnicodeChar(P1), High(VolumeNameA));
345 PasswordU := CeUtf16ToUtf8(RarUnicodeStringToWideString(VolumeNameA));
346 StrLCopy(PasswordA, PAnsiChar(PasswordU), High(PasswordA));
347 if not gStartupInfo.InputBox('Unrar', 'Please enter the password:', True, PasswordA, High(PasswordA)) then
348 Result := -1
349 else begin
350 Result := 1;
351 StrPLCopy(VolumeNameW, CeUtf8ToUtf16(PasswordA), High(VolumeNameW));
352 StrLCopy(PRarUnicodeChar(P1), PRarUnicodeChar(WideStringToRarUnicodeString(VolumeNameW)), P2 - 1);
353 end;
354 end;
355 UCM_LARGEDICT:
356 begin
357 P2:= P2 div Giga;
358 DictSize:= (DictSize div Giga) + Ord((DictSize mod Giga <> 0));
359 Buttons:= ArrayStringToPPchar([rsMsgButtonExtract, rsMsgButtonCancel], 0);
360 try
361 PasswordU:= Format(rsDictNotAllowed, [DictSize, P2, DictSize]) + LineEnding;
362
363 if gStartupInfo.MsgChoiceBox(PAnsiChar(PasswordU), PAnsiChar(rsDictLargeWarning), Buttons, 0, 1) = 0 then
364 Result:= 1
365 else begin
366 Result:= -1;
367 end;
368 finally
369 FreeMem(Buttons);
370 end;
371 end;
372 end;
373 end;
374
375 function OpenArchive(var ArchiveData: TOpenArchiveData) : TArcHandle; dcpcall; export;
376 begin
377 Result := 0;
378 ArchiveData.OpenResult := E_NOT_SUPPORTED;
379 end;
380
381 function OpenArchiveW(var ArchiveData: tOpenArchiveDataW): TArcHandle; dcpcall; export;
382 var
383 RarArcName: TRarUnicodeString;
384 AHandle: TRARHandle absolute Result;
385 RarArchiveData: RAROpenArchiveDataEx;
386 begin
387 if (RAROpenArchiveEx = nil) then
388 begin
389 Result := 0;
390 ArchiveData.OpenResult := E_EOPEN;
391 end
392 else begin
393 AHandle:= TRARHandle.Create;
394
395 RarArcName := WideStringToRarUnicodeString(ArchiveData.ArcName);
396
397 RarArchiveData := Default(RAROpenArchiveDataEx);
398 RarArchiveData.ArcNameW := PRarUnicodeChar(RarArcName);
399 RarArchiveData.OpenMode := ArchiveData.OpenMode;
400 RarArchiveData.Callback := @UnrarCallback;
401 RarArchiveData.UserData := PtrInt(Result);
402
403 AHandle.Handle := RAROpenArchiveEx(RarArchiveData);
404 ArchiveData.OpenResult := RarArchiveData.OpenResult;
405
406 if AHandle.Handle = 0 then
407 FreeAndNil(AHandle)
408 else begin
409 ArchiveData.CmtSize := RarArchiveData.CmtSize;
410 ArchiveData.CmtState := RarArchiveData.CmtState;
411
412 RARSetCallback(AHandle.Handle, @UnrarCallback, PtrInt(Result));
413 end;
414 end;
415 end;
416
417 function ReadHeader(hArcData: TArcHandle; var HeaderData: THeaderData) : Integer; dcpcall; export;
418 begin
419 Result := E_NOT_SUPPORTED;
420 end;
421
422 function ReadHeaderExW(hArcData: TArcHandle; var HeaderData: THeaderDataExW) : Integer; dcpcall; export;
423 var
424 RarHeader: RARHeaderDataEx;
425 AHandle: TRARHandle absolute hArcData;
426 begin
427 if (RARReadHeaderEx = nil) then
428 Result := E_EREAD
429 else begin
430 RarHeader:= Default(RARHeaderDataEx);
431
432 RarHeader.CmtBuf := HeaderData.CmtBuf;
433 RarHeader.CmtBufSize := HeaderData.CmtBufSize;
434
435 Result := RARReadHeaderEx(AHandle.Handle, RarHeader);
436
437 if Result <> E_SUCCESS then Exit;
438
439 {$PUSH}{$Q-}{$R-}
440 StringToArrayW(
441 RarUnicodeStringToWideString(TRarUnicodeString(RarHeader.ArcNameW)),
442 @HeaderData.ArcName, SizeOf(HeaderData.ArcName));
443
444 StringToArrayW(
445 RarUnicodeStringToWideString(TRarUnicodeString(RarHeader.FileNameW)),
446 @HeaderData.FileName, SizeOf(HeaderData.FileName));
447
448 HeaderData.Flags := RarHeader.Flags;
449 HeaderData.PackSize := RarHeader.PackSize;
450 HeaderData.PackSizeHigh := RarHeader.PackSizeHigh;
451 HeaderData.UnpSize := RarHeader.UnpSize;
452 HeaderData.UnpSizeHigh := RarHeader.UnpSizeHigh;
453 HeaderData.HostOS := RarHeader.HostOS;
454 HeaderData.FileCRC := RarHeader.FileCRC;
455 HeaderData.FileTime := RarHeader.FileTime;
456 HeaderData.UnpVer := RarHeader.UnpVer;
457 HeaderData.Method := RarHeader.Method;
458 HeaderData.FileAttr := RarHeader.FileAttr;
459 HeaderData.CmtSize := RarHeader.CmtSize;
460 HeaderData.CmtState := RarHeader.CmtState;
461
462 HeaderData.FileAttr :=
463 GetSystemSpecificAttributes(RarHostSystem(HeaderData.HostOS),
464 HeaderData.FileAttr);
465 HeaderData.FileTime := GetSystemSpecificFileTime(HeaderData.FileTime);
466
467 Int64Rec(HeaderData.MfileTime).Lo:= RarHeader.MtimeLow;
468 Int64Rec(HeaderData.MfileTime).Hi:= RarHeader.MtimeHigh;
469 {$POP}
470 AHandle.ProcessFileNameW := HeaderData.FileName;
471 end;
472 end;
473
474 function ProcessFile(hArcData: TArcHandle; Operation: Integer; DestPath, DestName: PAnsiChar) : Integer; dcpcall; export;
475 begin
476 Result := E_NOT_SUPPORTED;
477 end;
478
479 function ProcessFileW(hArcData: TArcHandle; Operation: Integer; DestPath, DestName: PWideChar) : Integer; dcpcall; export;
480 var
481 pwcDestPath: PRarUnicodeChar = nil;
482 pwcDestName: PRarUnicodeChar = nil;
483 AHandle: TRARHandle absolute hArcData;
484 SysSpecDestPath, SysSpecDestName: TRarUnicodeString;
485 begin
486 if (RARProcessFileW = nil) then
487 Result := E_EREAD
488 else begin
489 if DestPath <> nil then
490 begin
491 SysSpecDestPath:= WideStringToRarUnicodeString(DestPath);
492 pwcDestPath := PRarUnicodeChar(SysSpecDestPath);
493 end;
494 if DestName <> nil then
495 begin
496 SysSpecDestName:= WideStringToRarUnicodeString(DestName);
497 pwcDestName := PRarUnicodeChar(SysSpecDestName);
498 end;
499 Result := RARProcessFileW(AHandle.Handle, Operation, pwcDestPath, pwcDestName);
500 end;
501 end;
502
503 function CloseArchive(hArcData: TArcHandle) : Integer;dcpcall; export;
504 var
505 AHandle: TRARHandle absolute hArcData;
506 begin
507 if (RARCloseArchive = nil) then
508 Result := E_ECLOSE
509 else begin
510 Result := RARCloseArchive(AHandle.Handle);
511 end;
512 AHandle.Free;
513 end;
514
515 procedure SetChangeVolProc(hArcData: TArcHandle; pChangeVolProc: TChangeVolProc); dcpcall; export;
516 begin
517
518 end;
519
520 procedure SetProcessDataProc(hArcData : TArcHandle; pProcessDataProc : TProcessDataProc); dcpcall; export;
521 begin
522
523 end;
524
525 procedure SetChangeVolProcW(hArcData: TArcHandle; pChangeVolProc: TChangeVolProcW); dcpcall; export;
526 var
527 AHandle: TRARHandle absolute hArcData;
528 begin
529 if (hArcData <> wcxInvalidHandle) then
530 AHandle.ChangeVolProcW := pChangeVolProc
531 end;
532
533 procedure SetProcessDataProcW(hArcData : TArcHandle; pProcessDataProc : TProcessDataProcW); dcpcall; export;
534 var
535 AHandle: TRARHandle absolute hArcData;
536 begin
537 if (hArcData <> wcxInvalidHandle) then
538 AHandle.ProcessDataProcW := pProcessDataProc
539 else begin
540 ProcessDataProcW := pProcessDataProc;
541 end;
542 end;
543
544 function GetPackerCaps: Integer; dcpcall; export;
545 begin
546 Result := PK_CAPS_MULTIPLE or PK_CAPS_BY_CONTENT
547 or PK_CAPS_NEW or PK_CAPS_MODIFY or PK_CAPS_DELETE
548 or PK_CAPS_OPTIONS or PK_CAPS_ENCRYPT;
549 end;
550
551 function GetBackgroundFlags: Integer; dcpcall; export;
552 begin
553 Result:= BACKGROUND_UNPACK or BACKGROUND_PACK;
554 end;
555
556 procedure ExtensionInitialize(StartupInfo: PExtensionStartupInfo); dcpcall; export;
557 begin
558 gStartupInfo := StartupInfo^;
559 TranslateResourceStrings;
560
561 if ModuleHandle = NilHandle then
562 begin
563 gStartupInfo.MessageBox(PAnsiChar(Format(rsMsgLibraryNotFound, [_unrar])),
564 nil, MB_OK or MB_ICONERROR);
565 end;
566 end;
567
568 finalization
569 if ModuleHandle <> 0 then
570 UnloadLibrary(ModuleHandle);
571
572 end.
573