-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnRAR.pas
489 lines (461 loc) · 17.9 KB
/
UnRAR.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
{ MPUI-hcb, an MPlayer frontend for Windows
Copyright (C) 2006-2013 Huang Chen Bin <[email protected]>
based on TDFUnRAR by <[email protected]>
see UNRARDLL.TXT for Informations about UnRar.dll - Functions and structures
}
// ¶¯Ì¬ÔØÈëUnRAR.dll
unit UnRar;
interface
uses
Windows, SysUtils, TntSysUtils, TntWindows, Classes, TntDialogs, ComObj,ActiveX;
const
// Constants from UnRar.h
RAR_OM_LIST = 0;
RAR_OM_EXTRACT = 1;
RAR_SKIP = 0;
RAR_EXTRACT = 2;
RAR_VOL_ASK = 0;
UCM_CHANGEVOLUME = 0;
type
TUnrarCallback = function(Msg: UINT; UserData, P1, P2: LPARAM): Integer; stdcall;
// Header for every file in an archive
TRARHeaderData = record
ArcName : array[0..1023] of char;
ArcNameW : array[0..1023] of WideChar;
FileName : array[0..1023] of char;
FileNameW : array[0..1023] of WideChar;
Flags : cardinal;
PackSize : cardinal;
PackSizeHigh : cardinal;
UnpSize : cardinal;
UnpSizeHigh : cardinal;
HostOS : cardinal;
FileCRC : cardinal;
FileTime : cardinal;
UnpVer : cardinal;
Method : cardinal;
FileAttr : cardinal;
CmtBuf : PChar;
CmtBufSize : cardinal;
CmtSize : cardinal;
CmtState : cardinal;
Reserved : array[0..1023] of cardinal;
end;
// Archive-Data for opening rar-archive
TRAROpenArchiveData = record
ArcName : PChar;
ArcNameW : PWideChar;
OpenMode : cardinal;
OpenResult : cardinal;
CmtBuf : PChar;
CmtBufSize : cardinal;
CmtSize : cardinal;
CmtState : cardinal;
Flags : cardinal;
//Callback: TUnrarCallback;
//UserData: LPARAM;
//Reserved: array[0..27] of cardinal;
Reserved : array[0..31] of cardinal;
end;
TUnRARThread = class(TThread)
private
protected
procedure Execute; override;
end;
var
IsRarLoaded:THandle=0; IsShell32Loaded:THandle = 0;
RAROpenArchive : function(var ArchiveData: TRAROpenArchiveData): THandle; stdcall;
RARCloseArchive : function(hArcData: THandle): integer; stdcall;
RARReadHeader : function(hArcData: THandle; var HeaderData: TRARHeaderData): Integer; stdcall;
RARProcessFile : function(hArcData: THandle; Operation: Integer; DestPath, DestName: PwideChar): Integer; stdcall;
RARSetPassword : procedure(hArcData: THandle; Password: PChar); stdcall;
RARSetCallback : procedure(hArcData: THandle; UnrarCallback: TUnrarCallback; UserData: LPARAM); stdcall;
SHGetKnownFolderPath : function(rfid:PGUID; dwFlags:DWord; hToken:THandle; out ppszPath:PPWideChar):HRESULT; stdcall;
IsUserAnAdmin : function : BOOL; stdcall;
procedure LoadRarLibrary;
procedure UnLoadRarLibrary;
procedure LoadShell32Library;
procedure UnLoadShell32Library;
procedure ClearTmpFiles(Directory:WideString);
function AddRarMovies(ArcName,PW:widestring; Add,msg:boolean):integer;
procedure ExtractRarMovie(ArcName,PW:widestring);
procedure ExtractRarLyric(ArcName,PW:WideString);
function ExtractRarSub(ArcName,PW:WideString):WideString;
function GetShellPath(rfid:TGUID):WideString;
function IsAdmin:boolean;
implementation
uses Core,plist,locale;
procedure TUnRARThread.Execute;
begin
ExtractMovie(TmpURL,ArcPW);
tEnd:=true;
Restart;
end;
procedure LoadRarLibrary;
begin
if IsRarLoaded <> 0 then exit;
IsRarLoaded := Tnt_LoadLibraryW('unrar.dll');
if IsRarLoaded <> 0 then begin
@RAROpenArchive := GetProcAddress(IsRarLoaded, 'RAROpenArchiveEx');
@RARCloseArchive := GetProcAddress(IsRarLoaded, 'RARCloseArchive');
@RARReadHeader := GetProcAddress(IsRarLoaded, 'RARReadHeaderEx');
@RARProcessFile := GetProcAddress(IsRarLoaded, 'RARProcessFileW');
@RARSetPassword := GetProcAddress(IsRarLoaded, 'RARSetPassword');
@RARSetCallback := GetProcAddress(IsRarLoaded, 'RARSetCallback');
end;
end;
procedure UnLoadRarLibrary;
begin
if IsRarLoaded <> 0 then begin
FreeLibrary(IsRarLoaded);
IsRarLoaded := 0;
RAROpenArchive := nil;
RARCloseArchive := nil;
RARReadHeader := nil;
RARProcessFile := nil;
RARSetPassword := nil;
RARSetCallback := nil;
end;
end;
procedure LoadShell32Library;
begin
if IsShell32Loaded <> 0 then exit;
IsShell32Loaded := Tnt_LoadLibraryW('shell32.dll');
if IsShell32Loaded <> 0 then begin
@SHGetKnownFolderPath:= GetProcAddress(IsShell32Loaded, 'SHGetKnownFolderPath');
@IsUserAnAdmin:= GetProcAddress(IsShell32Loaded, 'IsUserAnAdmin');
end;
end;
procedure UnLoadShell32Library;
begin
if IsShell32Loaded <> 0 then begin
FreeLibrary(IsShell32Loaded);
IsShell32Loaded := 0;
SHGetKnownFolderPath:= nil;
IsUserAnAdmin:= nil;
end;
end;
function GetShellPath(rfid:TGUID):WideString;
var PathBuf:PPWideChar; APIResult:HRESULT;
begin //http://msdn.microsoft.com/en-us/library/bb762188(VS.85).aspx
Result:='';
LoadShell32Library;
if IsShell32Loaded=0 then exit;
APIResult:=SHGetKnownFolderPath(@rfid,0,0,PathBuf);
OleCheck(APIResult);
Result:=PWideChar(PathBuf);
CoTaskMemFree(PathBuf);
end;
function IsAdmin():boolean;
begin
Result:=false;
LoadShell32Library;
if IsShell32Loaded<>0 then Result:=IsUserAnAdmin();
end;
procedure ClearTmpFiles(Directory:WideString);
var SR:TSearchRecW;
begin
if not WideDirectoryExists(Directory) then exit;
Directory:=WideIncludeTrailingPathDelimiter(Directory);
if WideFindFirst(Directory+'*.*',faAnyFile,SR)=0 then begin
repeat
if SR.Name[1]<>'.' then begin // exclude . or .. Directory
if (SR.Attr AND faDirectory)<>0 then
ClearTmpFiles(Directory+SR.Name)
else WideDeleteFile(Directory+SR.Name);
end;
until WideFindNext(SR)<>0;
WideFindClose(SR);
end;
WideRemoveDir(Directory);
end;
function UnRARCallback(msg:UINT; UserData,P1,P2:LPARAM):integer; stdcall;
var FileName:WideString; s:String;
begin
if procArc then Result:=1
else begin Result:=-1; exit; end;
if msg=UCM_CHANGEVOLUME then begin
if P2=RAR_VOL_ASK then begin
s:=LowerCase(PChar(P1));
if lastP1=s then begin
StrPCopy(PChar(P1), lastFN);
exit;
end;
lastP1:=s;
s:=copy(s,Pos('.part',s)+1,MaxInt);
FileName := Tnt_WideLowerCase(PWChar(UserData));
FileName := copy(FileName, 1, Pos('.part', FileName));
FileName := FileName + s;
if WidePromptForFileName(FileName, 'RAR file(*.rar)|*.rar|Any file(*.*)|*.*', '', LOCstr_VolAsk_Caption) then begin
lastFN := Tnt_WideLowerCase((FileName));
StrPCopy(PChar(P1), lastFN);
end
else Result:=-1;
end;
end;
end;
function AddRarMovies(ArcName,PW:widestring; Add,msg:boolean):integer;
var hArcData:THandle; HeaderData:TRARHeaderData;
OpenArchiveData:TRAROpenArchiveData;
Entry:TPlaylistEntry; i,k:widestring;
First:boolean; FList:TWStringList; a:integer;
begin
Result:=0; if not Add then TmpPW:=PW;
FillChar(OpenArchiveData ,sizeof(OpenArchiveData),0);
OpenArchiveData.ArcNameW := PWideChar(ArcName);
OpenArchiveData.OpenMode := RAR_OM_LIST;
hArcData := RAROpenArchive(OpenArchiveData);
if (hArcData = 0) OR (OpenArchiveData.OpenResult <> 0) then begin
RARCloseArchive(hArcData);
Result:=-1; exit;
end;
FList:=TWStringList.Create; First:=true; k:=WideExtractFileName(ArcName);
if (OpenArchiveData.Flags and $00000080) = $00000080 then begin
First:=false;
if PW='' then WideInputQuery(LOCstr_SetPW_Caption,k,PW);
if PW<>'' then RARSetPassword(hArcData,PAnsiChar(AnsiString(PW)));
if not Add then TmpPW:=PW;
end;
FillChar(HeaderData ,sizeof(HeaderData),0);
repeat
if msg then PlayMsgAt := GetTickCount() + 500;
if RARReadHeader(hArcData, HeaderData) <> 0 then Break;
if ((HeaderData.Flags and $00000070) <> $00000070) and
(CheckInfo(MediaType,Tnt_WideLowerCase(WideExtractFileExt(HeaderData.FileNameW)))>ZipTypeCount) then begin
if First then begin
First:=false;
if ((HeaderData.Flags and $00000004) = $00000004) and (PW='') then
WideInputQuery(LOCstr_SetPW_Caption,k,PW);
if PW<>'' then RARSetPassword(hArcData,PAnsiChar(AnsiString(PW)));
if not Add then TmpPW:=PW;
end;
inc(Result);
if Add then begin
i:=HeaderData.FileNameW+' <-- '+k;
if playlist.FindItem('.part',i)<0 then begin
if PW='' then FList.Add(i) else FList.Add(i+':'+PW);
end;
end
else Break;
end;
if RARProcessFile(hArcData, RAR_SKIP, nil, nil)<>0 then break;
until False;
RARCloseArchive(hArcData);
Flist.SortStr(mysort);
for a:=0 to Flist.Count-1 do begin
with Entry do begin
State:=psNotPlayed;
FullURL:=ArcName;
DisplayURL:=Flist[a];
end;
playlist.Add(Entry);
end;
Flist.Free;
end;
procedure ExtractRarMovie(ArcName,PW:widestring);
var hArcData:THandle; First:boolean; k:widestring;
HeaderData:TRARHeaderData; OpenArchiveData:TRAROpenArchiveData;
begin
FillChar(OpenArchiveData ,sizeof(OpenArchiveData),0);
OpenArchiveData.ArcNameW := PWideChar(ArcName);
OpenArchiveData.OpenMode := RAR_OM_EXTRACT;
//OpenArchiveData.UserData := LPARAM(OpenArchiveData.ArcNameW);
//OpenArchiveData.Callback := UnRARCallback;
hArcData := RAROpenArchive(OpenArchiveData);
if (hArcData = 0) OR (OpenArchiveData.OpenResult <> 0) then begin
RARCloseArchive(hArcData); exit;
end;
First:=true; k:=WideExtractFileName(ArcName);
// Obsolete, use TRAROpenArchiveData's callback and UserData fields above.
RARSetCallback(hArcData,UnRARCallback,LPARAM(OpenArchiveData.ArcNameW));
if (OpenArchiveData.Flags and $00000080) = $00000080 then begin
First:=false; if PW='' then WideInputQuery(LOCstr_SetPW_Caption,k,PW);
if PW<>'' then RARSetPassword(hArcData,PAnsiChar(AnsiString(PW)));
end;
FillChar(HeaderData ,sizeof(HeaderData),0);
repeat
if RARReadHeader(hArcData,HeaderData) <> 0 then Break;
if ((HeaderData.Flags and $00000070) <> $00000070) and
(HeaderData.FileNameW=ArcMovie) then begin
if First then begin
if ((HeaderData.Flags and $00000004) = $00000004) and (PW='') then
WideInputQuery(LOCstr_SetPW_Caption,k,PW);
if PW<>'' then RARSetPassword(hArcData,PAnsiChar(AnsiString(PW)));
end;
RARProcessFile(hArcData, RAR_EXTRACT, nil, PWideChar(TempDir+ArcMovie));
Break;
end
else if RARProcessFile(hArcData, RAR_SKIP, nil, nil)<>0 then break;
until False;
try
RARCloseArchive(hArcData);
except
end;
end;
procedure ExtractRarLyric(ArcName,PW:widestring);
var hArcData:THandle; First:boolean; FName,t,k:widestring;
HeaderData:TRARHeaderData; OpenArchiveData:TRAROpenArchiveData;
begin
TmpPW:=PW;
FillChar(OpenArchiveData ,sizeof(OpenArchiveData),0);
OpenArchiveData.ArcNameW := PWideChar(ArcName);
OpenArchiveData.OpenMode := RAR_OM_EXTRACT;
//OpenArchiveData.UserData := LPARAM(OpenArchiveData.ArcNameW);
//OpenArchiveData.Callback := UnRARCallback;
hArcData := RAROpenArchive(OpenArchiveData);
if (hArcData = 0) OR (OpenArchiveData.OpenResult <> 0) then begin
RARCloseArchive(hArcData); exit;
end;
First:=true; k:=WideExtractFileName(ArcName);
// Obsolete, use TRAROpenArchiveData's callback and UserData fields above.
RARSetCallback(hArcData,UnRARCallback,LPARAM(OpenArchiveData.ArcNameW));
if (OpenArchiveData.Flags and $00000080) = $00000080 then begin
First:=false;
if PW='' then WideInputQuery(LOCstr_SetPW_Caption,k,TmpPW);
if TmpPW<>'' then RARSetPassword(hArcData,PAnsiChar(AnsiString(TmpPW)));
end;
FillChar(HeaderData ,sizeof(HeaderData),0);
FName:=Tnt_WideLowerCase(getFileName(WideExtractFileName(ArcMovie)))+'.lrc';
repeat
if RARReadHeader(hArcData,HeaderData) <> 0 then Break;
if ((HeaderData.Flags and $00000070) <> $00000070) and
(FName=Tnt_WideLowerCase(WideExtractFileName(HeaderData.FileNameW))) then begin
if First then begin
First:=false;
if ((HeaderData.Flags and $00000004) = $00000004) and (PW='') then
WideInputQuery(LOCstr_SetPW_Caption,k,TmpPW);
if TmpPW<>'' then RARSetPassword(hArcData,PAnsiChar(AnsiString(TmpPW)));
end;
t:= TempDir+HeaderData.FileNameW;
if RARProcessFile(hArcData, RAR_EXTRACT, nil, PWideChar(t))<>0 then
Break
else begin
Lyric.ParseLyric(t);
if HaveLyric<>0 then Break;
end;
end
else if RARProcessFile(hArcData, RAR_SKIP, nil, nil)<>0 then break;
until False;
try
RARCloseArchive(hArcData);
EXCEPT
end;
end;
function ExtractRarSub(ArcName,PW:widestring):widestring;
var i,j,HaveIdx,HaveSub,DirHIdx,DirHSub:integer; FName,FExt,g,t,k:widestring;
First:boolean; hArcData:THandle; HeaderData:TRARHeaderData;
OpenArchiveData:TRAROpenArchiveData;
begin
g:=GetFileName(ArcName); t:= TempDir+'hcb428'; TmpPW:=PW;
DirHIdx:=integer(WideFileExists(g+'.idx'));
DirHSub:=integer(WideFileExists(g+'.sub'));
if (DirHIdx+DirHSub)=2 then begin result:=g; exit; end;
Result:=''; j:=0; First:=true; k:=WideExtractFileName(ArcName);
HaveIdx:=0; HaveSub:=0;
FillChar(OpenArchiveData ,sizeof(OpenArchiveData),0);
OpenArchiveData.ArcNameW := PWideChar(ArcName);
//OpenArchiveData.UserData := LPARAM(OpenArchiveData.ArcNameW);
//OpenArchiveData.Callback := UnRARCallback;
if (DirHIdx+DirHSub)=1 then begin
OpenArchiveData.OpenMode := RAR_OM_LIST;
hArcData := RAROpenArchive(OpenArchiveData);
if (hArcData = 0) OR (OpenArchiveData.OpenResult <> 0) then begin
RARCloseArchive(hArcData); exit;
end;
// Obsolete, use TRAROpenArchiveData's callback and UserData fields above.
RARSetCallback(hArcData,UnRARCallback,LPARAM(OpenArchiveData.ArcNameW));
if (OpenArchiveData.Flags and $00000080) = $00000080 then begin
First:=false;
if PW='' then WideInputQuery(LOCstr_SetPW_Caption,k,TmpPW);
if TmpPW<>'' then RARSetPassword(hArcData,PChar(String(TmpPW)));
end;
FillChar(HeaderData ,sizeof(HeaderData),0);
repeat
if RARReadHeader(hArcData, HeaderData) <> 0 then Break;
if (HeaderData.Flags and $00000070) <> $00000070 then begin
FExt:=Tnt_WideLowerCase(WideExtractFileExt(HeaderData.FileNameW));
if FExt='.idx' then HaveIdx:=1;
if FExt='.sub' then HaveSub:=1;
if HaveIdx+HaveSub=2 then Break;
end;
if RARProcessFile(hArcData, RAR_SKIP, nil, nil)<>0 then break;
until False;
if RARCloseArchive(hArcData)<>0 then exit;
if HaveIdx+HaveSub=0 then exit;
end;
//¿ªÊ¼½âѹRARÎĵµ
OpenArchiveData.OpenMode := RAR_OM_EXTRACT;
hArcData := RAROpenArchive(OpenArchiveData);
if (hArcData = 0) OR (OpenArchiveData.OpenResult <> 0) then begin
RARCloseArchive(hArcData); exit;
end;
// Obsolete, use TRAROpenArchiveData's callback and UserData fields above.
RARSetCallback(hArcData,UnRARCallback,LPARAM(OpenArchiveData.ArcNameW));
if First then begin
if (OpenArchiveData.Flags and $00000080) = $00000080 then begin
First:=false;
if PW='' then WideInputQuery(LOCstr_SetPW_Caption,k,TmpPW);
if TmpPW<>'' then RARSetPassword(hArcData,PAnsiChar(AnsiString(TmpPW)));
end;
end;
FillChar(HeaderData ,sizeof(HeaderData),0);
repeat
if RARReadHeader(hArcData, HeaderData) <> 0 then Break;
if (HeaderData.Flags and $00000070) <> $00000070 then begin
if First then begin
First:=false;
if ((HeaderData.Flags and $00000004) = $00000004) and (PW='') then
WideInputQuery(LOCstr_SetPW_Caption,k,TmpPW);
if TmpPW<>'' then RARSetPassword(hArcData,PAnsiChar(AnsiString(TmpPW)));
end;
FExt:=Tnt_WideLowerCase(WideExtractFileExt(HeaderData.FileNameW));
i:=CheckInfo(SubType,FExt);
if i<1 then begin
if RARProcessFile(hArcData, RAR_SKIP, nil, nil)<>0 then break;
end
else begin
if i<SubTypeCount-1 then begin
FName:=TempDir+HeaderData.FileNameW;
if RARProcessFile(hArcData, RAR_EXTRACT, nil, PWideChar(FName))<>0 then
Break
else begin
if (not IsWideStringMappableToAnsi(FName)) or (pos(',',FName)>0) then FName:=WideExtractShortPathName(FName);
if pos(FName,substring)=0 then begin
if Firstrun or (not Win32PlatformIsUnicode) then begin
Loadsub:=2; Loadsrt:=2;
AddChain(j,substring,EscapeParam(FName));
end
else
SendCommand('sub_load ' + Tnt_WideStringReplace(EscapeParam(FName), '\', '/', [rfReplaceAll]));
end;
end;
end
else begin
if (DirHIdx+DirHSub=0) OR (HaveIdx+HaveSub=2) then begin
if RARProcessFile(hArcData, RAR_EXTRACT, nil, PWideChar(t+FExt))<>0 then
Break
else
Result:=t;
end
else begin
if ((HaveIdx+DirHSub=2) and (FExt='.idx')) OR
((DirHIdx+HaveSub=2) and (FExt='.sub')) then begin
if RARProcessFile(hArcData, RAR_EXTRACT, nil, PWideChar(t+FExt))<>0 then
Break
else begin
if WideCopyFile(t+FExt,g+FExt,false) then
Result:=g
else Result:=t;
end;
end
else if RARProcessFile(hArcData, RAR_SKIP, nil, nil)<>0 then break;
end;
end;
end;
end
else if RARProcessFile(hArcData, RAR_SKIP, nil, nil)<>0 then break;
until False;
RARCloseArchive(hArcData);
if (not Win32PlatformIsUnicode) and (j>0) then Restart;
end;
end.