1 /*******************************************************************************
\r
2 TPI - flexible but useless plug-in framework.
\r
3 Copyright (C) 2002-2009 Silky
\r
5 This library is free software; you can redistribute it and/or modify it under
\r
6 the terms of the GNU Lesser General Public License as published by the Free
\r
7 Software Foundation; either version 2.1 of the License, or (at your option)
\r
10 This library is distributed in the hope that it will be useful, but WITHOUT
\r
11 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
\r
12 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
\r
15 You should have received a copy of the GNU Lesser General Public License along
\r
16 with this library; if not, write to the Free Software Foundation, Inc.,
\r
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
\r
20 *******************************************************************************/
\r
22 //******************************************************************************
\r
24 //******************************************************************************
\r
26 #define wxUSE_DYNLIB_CLASS 1
\r
27 #include "../../common/header/plugin.h"
\r
28 #include "../../common/header/plugin-extra.h"
\r
29 #include "../../common/library/library.h"
\r
30 #include <wx/dynlib.h>
\r
33 //******************************************************************************
\r
35 //******************************************************************************
\r
37 wxDynamicLibrary g_hLib;
\r
39 char g_szComment[64001];
\r
40 RAROpenArchiveDataEx g_oaInfo;
\r
41 RARHeaderDataEx * g_hdInfo;
\r
43 //******************************************************************************
\r
45 //******************************************************************************
\r
47 int __stdcall CallbackProc(unsigned int msg, long, long P1, long P2)
\r
50 static TPI_PROCESSINFO piInfo;
\r
54 case UCM_CHANGEVOLUME:
\r
59 piInfo.eMessage = TPI_MESSAGE_ASK;
\r
60 piInfo.eStatus = TPI_PARAM_NEXTVOLUME;
\r
61 piInfo.szParam.Empty();
\r
63 case RAR_VOL_NOTIFY:
\r
65 piInfo.eMessage = TPI_MESSAGE_STATUS;
\r
66 piInfo.eStatus = TPI_STATUS_OPENARCHIVE;
\r
67 piInfo.fiInfo.fnFileName = wxFileName(UTF82String((char *) P1));
\r
73 case UCM_PROCESSDATA:
\r
74 piInfo.eMessage = TPI_MESSAGE_STATUS;
\r
75 piInfo.eStatus = TPI_STATUS_INPROCESS;
\r
76 piInfo.nProcessedSize += P2;
\r
78 case UCM_NEEDPASSWORD:
\r
79 piInfo.eMessage = TPI_MESSAGE_ASK;
\r
80 piInfo.eStatus = TPI_PARAM_PASSWORD;
\r
81 piInfo.szParam.Empty();
\r
88 if (g_prProc == NULL || g_prProc(TPI_NOTIFY_COMMON, & piInfo) == TPI_CALLBACK_CONTINUE)
\r
90 if (msg != UCM_PROCESSDATA && P2 != RAR_VOL_NOTIFY)
\r
92 strncpy((char *) P1, piInfo.szParam.char_str(), (msg == UCM_CHANGEVOLUME ? 1024 : P2) - 1);
\r
102 //******************************************************************************
\r
103 // Inside Functions
\r
104 //******************************************************************************
\r
106 int ErrorCodeConvert(int nErrorCode)
\r
108 switch (nErrorCode)
\r
110 case 0: return TPI_ERROR_SUCCESS;
\r
111 case 1: return TPI_ERROR_SUCCESS;
\r
112 case ERAR_END_ARCHIVE: return TPI_ERROR_S_ENDOFDATA;
\r
113 case ERAR_NO_MEMORY: return TPI_ERROR_D_OUTOFMEMORY;
\r
114 case ERAR_BAD_DATA: return TPI_ERROR_ARC_BROKEN_MISC;
\r
115 case ERAR_BAD_ARCHIVE: return TPI_ERROR_ARC_UNSUPPORTED;
\r
116 case ERAR_UNKNOWN_FORMAT: return TPI_ERROR_ARC_ENCRYPTED;
\r
117 case ERAR_EOPEN: return TPI_ERROR_IO_ARC_OPEN;
\r
118 case ERAR_ECREATE: return TPI_ERROR_IO_FILE_OPEN;
\r
119 case ERAR_ECLOSE: return TPI_ERROR_IO_CLOSE;
\r
120 case ERAR_EREAD: return TPI_ERROR_IO_ARC_READ;
\r
121 case ERAR_EWRITE: return TPI_ERROR_IO_FILE_WRITE;
\r
122 case ERAR_SMALL_BUF: return TPI_ERROR_UNDEFINED;
\r
123 case ERAR_UNKNOWN: return TPI_ERROR_UNDEFINED;
\r
124 case ERAR_MISSING_PASSWORD: return TPI_ERROR_ARC_ENCRYPTED;
\r
125 default: return TPI_ERROR_UNDEFINED;
\r
129 //******************************************************************************
\r
131 //******************************************************************************
\r
138 int __stdcall GetPluginInformation
\r
140 unsigned int _uInfoId,
\r
147 return TPI_ERROR_D_PARAMETER;
\r
151 case TPI_INFO_VERSION_MAJOR:
\r
152 case TPI_INFO_VERSION_MINOR:
\r
153 * (int *) _pPtr = 0;
\r
155 case TPI_INFO_VERSION_API:
\r
156 * (int *) _pPtr = 2;
\r
159 return TPI_ERROR_D_UNSUPPORTED;
\r
161 return TPI_ERROR_SUCCESS;
\r
164 int __stdcall GetFormatInformation(TPI_FORMATINFO * _fiInfo, bool _bFirst)
\r
168 return TPI_ERROR_S_ENDOFDATA;
\r
171 _fiInfo->szTypeName = wxT("RAR");
\r
172 _fiInfo->szSuffix = wxT("rar");
\r
173 _fiInfo->szEngineName = g_hLib.CanonicalizeName(wxT("unrar"));
\r
174 _fiInfo->szTPIName = wxT("rarArc");
\r
175 _fiInfo->nTypeId = 0;
\r
176 _fiInfo->eSupportedCommand = TPI_COMMAND_EXTRACT | TPI_COMMAND_TEST;
\r
177 _fiInfo->fArchive = true;
\r
178 _fiInfo->fComment = true;
\r
179 _fiInfo->fSFX = true;
\r
180 _fiInfo->fSolid = true;
\r
181 _fiInfo->fEncryptPassword = true;
\r
182 _fiInfo->fMultiVolume = true;
\r
184 return TPI_ERROR_SUCCESS;
\r
187 int __stdcall LoadPlugin
\r
193 g_hLib.Load(g_hLib.CanonicalizeName(wxT("unrar")));
\r
194 if (! g_hLib.IsLoaded())
\r
197 return TPI_ERROR_U_LOAD_LIBRARY;
\r
200 return TPI_ERROR_SUCCESS;
\r
203 int __stdcall FreePlugin
\r
205 void * // _pReserved
\r
209 return TPI_ERROR_SUCCESS;
\r
212 int __stdcall CheckArchive
\r
214 const wxString & _szArcName,
\r
215 wxULongLong_t * _llFileCount
\r
219 int nErrorCode = OpenArchive(_szArcName, & _hArchive);
\r
220 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
225 if (_llFileCount != NULL)
\r
227 * _llFileCount = 1;
\r
230 return CloseArchive(_hArchive);
\r
233 int __stdcall OpenArchive
\r
235 const wxString & _szArcName,
\r
239 if (! g_hLib.HasSymbol(wxT("RAROpenArchiveEx")))
\r
241 return TPI_ERROR_U_USE_LIBRARY;
\r
243 void * p = g_hLib.GetSymbol(wxT("RAROpenArchiveEx"));
\r
246 return TPI_ERROR_U_USE_LIBRARY;
\r
249 memset(& g_oaInfo, 0, sizeof(g_oaInfo));
\r
250 g_oaInfo.ArcName = NULL;
\r
251 g_oaInfo.ArcNameW = (wchar_t *) malloc((_szArcName.Len() + 1) * sizeof(wchar_t));
\r
252 wcscpy(g_oaInfo.ArcNameW, _szArcName.wchar_str());
\r
253 g_oaInfo.OpenMode = RAR_OM_EXTRACT;
\r
254 g_oaInfo.CmtBuf = g_szComment;
\r
255 g_oaInfo.CmtBufSize = sizeof(g_szComment) - 1;
\r
256 * _hArchive = ((void * (__stdcall *)(RAROpenArchiveDataEx *)) p)(& g_oaInfo);
\r
257 free(g_oaInfo.ArcNameW);
\r
258 if (* _hArchive == NULL)
\r
260 return TPI_ERROR_UNDEFINED;
\r
262 int nErrorCode = ErrorCodeConvert(g_oaInfo.OpenResult);
\r
263 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
269 if (! g_hLib.HasSymbol(wxT("RARSetCallback")))
\r
271 return TPI_ERROR_U_USE_LIBRARY;
\r
273 p = g_hLib.GetSymbol(wxT("RARSetCallback"));
\r
276 return TPI_ERROR_U_USE_LIBRARY;
\r
278 ((void (__stdcall *)(void *, void *, long)) p)(* _hArchive, (void *) CallbackProc, (long) & g_hdInfo);
\r
280 return TPI_ERROR_SUCCESS;
\r
283 int __stdcall CloseArchive
\r
288 if (! g_hLib.HasSymbol(wxT("RARCloseArchive")))
\r
290 return TPI_ERROR_U_USE_LIBRARY;
\r
292 void * p = g_hLib.GetSymbol(wxT("RARCloseArchive"));
\r
293 return (! p || _hArchive == NULL) ? TPI_ERROR_U_USE_LIBRARY : ErrorCodeConvert(((int (__stdcall *)(void *)) p)(_hArchive));
\r
296 int __stdcall GetFileInformation
\r
299 TPI_FILEINFO * _fiInfo,
\r
303 static wxULongLong_t s_nFileId;
\r
304 static void * pR, * pP;
\r
310 pR = g_hLib.HasSymbol(wxT("RARReadHeaderEx")) ? g_hLib.GetSymbol(wxT("RARReadHeaderEx")) : NULL;
\r
311 pP = g_hLib.HasSymbol(wxT("RARProcessFileW")) ? g_hLib.GetSymbol(wxT("RARProcessFileW")) : NULL;
\r
314 return TPI_ERROR_U_USE_LIBRARY;
\r
318 RARHeaderDataEx hdInfo;
\r
319 char szComment[64001];
\r
320 hdInfo.CmtBuf = szComment;
\r
321 hdInfo.CmtBufSize = sizeof(szComment) - 1;
\r
322 nErrorCode = ErrorCodeConvert(((int (__stdcall *)(void *, RARHeaderDataEx *)) pR)(_hArchive, & hdInfo));
\r
323 if (nErrorCode == TPI_ERROR_SUCCESS)
\r
325 _fiInfo->dwAttribute = hdInfo.FileAttr;
\r
326 if (hdInfo.Flags & 0x04)
\r
328 _fiInfo->dwAttribute |= TPI_ATTRIBUTE_ENCRYPTED;
\r
330 _fiInfo->dwCRC32 = hdInfo.FileCRC;
\r
331 _fiInfo->nPackedSize = hdInfo.PackSizeHigh;
\r
332 _fiInfo->nPackedSize = _fiInfo->nPackedSize << 32;
\r
333 _fiInfo->nPackedSize += hdInfo.PackSize;
\r
334 _fiInfo->nUnpackedSize = hdInfo.UnpSizeHigh;
\r
335 _fiInfo->nUnpackedSize = _fiInfo->nUnpackedSize << 32;
\r
336 _fiInfo->nUnpackedSize += hdInfo.UnpSize;
\r
337 _fiInfo->tmModified.SetFromDOS(hdInfo.FileTime);
\r
338 _fiInfo->eOSType = hdInfo.HostOS;
\r
339 _fiInfo->szStoredName = WC2String(hdInfo.FileNameW);
\r
340 _fiInfo->szMethod.Printf(wxT("%x"), hdInfo.Method);
\r
341 _fiInfo->szComment = UTF82String(hdInfo.CmtBuf);
\r
342 _fiInfo->nFileId = s_nFileId++;
\r
343 _fiInfo->fnFileName = wxFileName(_fiInfo->szStoredName);
\r
346 nErrorCode = ErrorCodeConvert(((int (__stdcall *)(void *, int, wchar_t *, wchar_t *)) pP)(_hArchive, RAR_SKIP, NULL, NULL));
\r
352 int __stdcall GetArchiveInformation
\r
355 TPI_ARCHIVEINFO * _aiInfo
\r
358 _aiInfo->fSolid = (g_oaInfo.Flags & 0x0008) == 1;
\r
359 _aiInfo->fEncryptHeader = (g_oaInfo.Flags & 0x0080) == 1;
\r
360 _aiInfo->szComment = UTF82String(g_szComment);
\r
361 GetFormatInformation(& _aiInfo->fiInfo, true);
\r
362 return TPI_ERROR_SUCCESS;
\r
365 int __stdcall Command
\r
367 wxULongLong_t _eCommand,
\r
368 TPI_SWITCHES * _swInfo,
\r
369 const wxString & _szArcName,
\r
370 const wxArrayString & _szFiles
\r
373 if (_eCommand != TPI_COMMAND_EXTRACT && _eCommand != TPI_COMMAND_TEST)
\r
375 return TPI_ERROR_U_USE_LIBRARY;
\r
380 int nErrorCode = OpenArchive(_szArcName, & hArc);
\r
381 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
387 * pS = g_hLib.HasSymbol(wxT("RARSetPassword")) ? g_hLib.GetSymbol(wxT("RARSetPassword")) : NULL,
\r
388 * pR = g_hLib.HasSymbol(wxT("RARReadHeaderEx")) ? g_hLib.GetSymbol(wxT("RARReadHeaderEx")) : NULL,
\r
389 * pP = g_hLib.HasSymbol(wxT("RARProcessFileW")) ? g_hLib.GetSymbol(wxT("RARProcessFileW")) : NULL;
\r
392 return TPI_ERROR_U_USE_LIBRARY;
\r
396 ((void (__stdcall *)(void *, char *)) pS)(hArc, _swInfo->szPassword.char_str());
\r
399 RARHeaderDataEx hdInfo;
\r
400 g_hdInfo = & hdInfo;
\r
401 while (nErrorCode == TPI_ERROR_SUCCESS && ErrorCodeConvert(((int (__stdcall *)(void *, RARHeaderDataEx *)) pR)(hArc, & hdInfo)) == TPI_ERROR_SUCCESS)
\r
405 ((int (__stdcall *)(void *, int, wchar_t *, wchar_t *)) pP)(
\r
407 (_szFiles.GetCount() != 0 && _szFiles.Index(wxString(hdInfo.FileNameW)) == wxNOT_FOUND) ? RAR_SKIP : _eCommand == TPI_COMMAND_EXTRACT ? RAR_EXTRACT : RAR_TEST,
\r
408 _swInfo->fStoreDirectoryPathes ? _swInfo->fnDestinationDirectory.GetFullPath().wchar_str() : NULL,
\r
409 _swInfo->fStoreDirectoryPathes ? NULL : (_swInfo->fnDestinationDirectory.GetPathWithSep() + wxFileName(hdInfo.FileNameW).GetFullName()).wchar_str()
\r
413 CloseArchive(hArc);
\r
417 int __stdcall SetCallbackProc
\r
419 TPI_PROC _prArcProc
\r
423 if (_prArcProc == NULL)
\r
425 return TPI_ERROR_D_PARAMETER;
\r
427 g_prProc = * _prArcProc;
\r
429 return TPI_ERROR_SUCCESS;
\r