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 #include "../../common/header/plugin.h"
\r
27 #include "../../common/header/plugin-extra.h"
\r
28 #include "../../common/library/library.h"
\r
29 #include <wx/file.h>
\r
30 #include <wx/stdpaths.h>
\r
32 #include <windows.h>
\r
33 #include "spiLibrary.h"
\r
35 //******************************************************************************
\r
37 //******************************************************************************
\r
42 //******************************************************************************
\r
44 //******************************************************************************
\r
46 BOOL __stdcall DllMain(HMODULE, DWORD fdwReason, void *)
\r
50 case DLL_PROCESS_DETACH:
\r
51 ::FreeLibrary(g_hLib);
\r
57 //******************************************************************************
\r
59 //******************************************************************************
\r
61 int __stdcall CallbackProc(int _nNow, int, long _lData)
\r
63 //
\8d\
\91¢
\91Ì
\82ð
\8f\89\8aú
\89»
\81B
\r
64 TPI_PROCESSINFO * piInfo = (TPI_PROCESSINFO *) _lData;
\r
65 piInfo->llProcessedSize = _nNow;
\r
67 //
\83R
\81[
\83\8b\83o
\83b
\83N
\8aÖ
\90\94\82É
\91\97\90M
\81B
\r
68 return g_prProc != NULL && g_prProc(TPI_NOTIFY_COMMON, piInfo) == TPI_CALLBACK_CANCEL;
\r
71 //******************************************************************************
\r
73 //******************************************************************************
\r
75 int ErrorCodeConvert(int nErrorCode)
\r
79 case -1: return TPI_ERROR_U_USE_LIBRARY;
\r
80 case 0: return TPI_ERROR_SUCCESS;
\r
81 case 1: return TPI_ERROR_D_SKIPPED;
\r
82 case 2: return TPI_ERROR_ARC_UNSUPPORTED;
\r
83 case 3: return TPI_ERROR_ARC_BROKEN_MISC;
\r
84 case 4: return TPI_ERROR_D_OUTOFMEMORY;
\r
85 case 5: return TPI_ERROR_D_USEMEMORY;
\r
86 case 6: return TPI_ERROR_IO_ARC_READ;
\r
87 default: return TPI_ERROR_UNDEFINED;
\r
91 //******************************************************************************
\r
93 //******************************************************************************
\r
100 int __stdcall GetPluginInformation
\r
102 unsigned int _uInfoId,
\r
109 return TPI_ERROR_D_PARAMETER;
\r
113 case TPI_INFO_VERSION_MAJOR:
\r
114 case TPI_INFO_VERSION_MINOR:
\r
115 * (int *) _pPtr = 0;
\r
117 case TPI_INFO_VERSION_API:
\r
118 * (int *) _pPtr = 2;
\r
121 return TPI_ERROR_D_PARAMETER;
\r
123 return TPI_ERROR_SUCCESS;
\r
126 int __stdcall GetFormatInformation(TPI_FORMATINFO *, bool)
\r
128 return TPI_ERROR_D_UNSUPPORTED;
\r
131 int __stdcall LoadPlugin
\r
133 const wxString & _szArcName,
\r
138 wxString szSPIPath = wxPathOnly(p.GetExecutablePath()) + wxT("/lib/"), szSPIName;
\r
139 wxDir fs(szSPIPath);
\r
140 bool b = fs.GetFirst(& szSPIName, wxT("*.spi"));
\r
143 // SPI
\82ð
\83\8d\81[
\83h
\81B
\r
144 wxString szLibName = szSPIPath + szSPIName;
\r
145 g_hLib = ::LoadLibrary(szLibName.wchar_str());
\r
146 if (g_hLib == NULL)
\r
148 b = fs.GetNext(& szSPIName);
\r
152 // GetPluginInfo
\82ð
\8eÀ
\8ds
\81B
\r
153 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
154 char szPluginType[5]; //
\8eí
\97Þ4bytes + NULL
\r
156 || ((int (PASCAL *)(int, char *, int)) fpProc)(0, szPluginType, sizeof(szPluginType)) < 0
\r
157 || szPluginType[2] != 'A' || szPluginType[3] != 'M')
\r
159 ::FreeLibrary(g_hLib);
\r
160 b = fs.GetNext(& szSPIName);
\r
164 //
\8f\91\8cÉ
\82É
\91Î
\89\9e\82µ
\82Ä
\82¢
\82é
\82©
\83`
\83F
\83b
\83N
\81B
\r
165 if (CheckArchive(_szArcName, NULL) == TPI_ERROR_SUCCESS)
\r
167 //
\91Î
\89\9e\82µ
\82Ä
\82¢
\82ê
\82Î
\8f\88\97\9d\82ð
\8fI
\97¹
\81B
\r
168 return TPI_ERROR_SUCCESS;
\r
171 b = fs.GetNext(& szSPIName);
\r
173 return TPI_ERROR_U_LOAD_LIBRARY;
\r
176 int __stdcall FreePlugin
\r
178 void * // _pReserved
\r
181 ::FreeLibrary(g_hLib);
\r
182 return TPI_ERROR_SUCCESS;
\r
185 int __stdcall CheckArchive
\r
187 const wxString & _szArcName,
\r
191 FARPROC fpProc = ::GetProcAddress(g_hLib, "IsSupported");
\r
192 if (fpProc == NULL)
\r
194 return TPI_ERROR_U_USE_LIBRARY;
\r
197 wxFile hFile(_szArcName, wxFile::read);
\r
198 if (! hFile.IsOpened())
\r
200 return TPI_ERROR_IO_ARC_OPEN;
\r
204 ::ZeroMemory(buffer, sizeof(buffer));
\r
205 if (hFile.Read(buffer, sizeof(buffer)) == wxInvalidOffset)
\r
208 return TPI_ERROR_IO_ARC_READ;
\r
211 if (! ((BOOL (PASCAL *)(const char *, unsigned long)) fpProc)(_szArcName.ToUTF8(), (unsigned long) buffer))
\r
214 return TPI_ERROR_D_UNSUPPORTED;
\r
218 fpProc = ::GetProcAddress(g_hLib, "GetArchiveInfo");
\r
219 if (fpProc == NULL)
\r
221 return TPI_ERROR_U_USE_LIBRARY;
\r
225 int nReturnCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HLOCAL *)) fpProc)(_szArcName.ToUTF8(), 0, 0, & hMemory));
\r
226 if (nReturnCode == TPI_ERROR_SUCCESS)
\r
228 if (_nFileCount != NULL)
\r
230 * _nFileCount = int(::LocalSize(hMemory) / sizeof(fileInfo));
\r
232 ::LocalFree(hMemory);
\r
235 return nReturnCode;
\r
238 int __stdcall OpenArchive
\r
240 const wxString & _szArcName,
\r
244 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetArchiveInfo");
\r
245 if (fpProc == NULL)
\r
247 return TPI_ERROR_U_USE_LIBRARY;
\r
250 int nReturnCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HLOCAL *)) fpProc)(_szArcName.ToUTF8(), 0, 0, _hArchive));
\r
251 if (nReturnCode != TPI_ERROR_SUCCESS)
\r
253 return nReturnCode;
\r
256 return * _hArchive == NULL ? TPI_ERROR_IO_ARC_OPEN : nReturnCode;
\r
259 int __stdcall CloseArchive
\r
264 return ::LocalFree(_hArchive) == NULL ? TPI_ERROR_SUCCESS : TPI_ERROR_IO_ARC_CLOSE;
\r
267 int __stdcall GetFileInformation
\r
270 TPI_FILEINFO * _fiInfo,
\r
274 static unsigned int uFileCount, uFilePointer;
\r
279 uFileCount = (unsigned int) (::LocalSize(_hArchive) / sizeof(fileInfo));
\r
282 if (uFilePointer > uFileCount)
\r
284 return TPI_ERROR_S_ENDOFDATA;
\r
287 fileInfo pfiInfo = ((fileInfo *) ::LocalLock(_hArchive))[uFilePointer];
\r
288 if (pfiInfo.method[0] == 0)
\r
290 //
\92\9a\94J
\82È
\83v
\83\89\83O
\83C
\83\93\82Ì
\82½
\82ß
\82Ì
\90\94\8d\87\82í
\82¹ (^^;
\r
291 uFileCount = uFilePointer;
\r
292 return TPI_ERROR_S_ENDOFDATA;
\r
295 _fiInfo->dwAttribute = 0;
\r
296 _fiInfo->dwCRC32 = pfiInfo.crc;
\r
297 _fiInfo->llPackedSize = pfiInfo.compsize;
\r
298 _fiInfo->llUnpackedSize = pfiInfo.filesize;
\r
299 _fiInfo->wCompressRatio = pfiInfo.compsize >= pfiInfo.filesize ? 1000 : (WORD) (1000 * pfiInfo.compsize / pfiInfo.filesize);
\r
300 _fiInfo->llFileID = uFilePointer++;
\r
301 _fiInfo->tmModified = pfiInfo.timestamp;
\r
302 _fiInfo->szStoredName = MB2String(pfiInfo.path) + MB2String(pfiInfo.filename);
\r
303 _fiInfo->fnFileName = wxFileName(_fiInfo->szStoredName);
\r
304 _fiInfo->szMethod = MB2String((char *) pfiInfo.method);
\r
305 _fiInfo->pCustomInfo = (void *) pfiInfo.position;
\r
306 ::LocalUnlock(_hArchive);
\r
308 return TPI_ERROR_SUCCESS;
\r
311 int __stdcall GetArchiveInformation
\r
314 TPI_ARCHIVEINFO * _aiInfo
\r
317 // GetPluginInfo
\82ð
\8eÀ
\8ds
\81B
\r
318 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");
\r
319 if (fpProc != NULL)
\r
322 if (((int (PASCAL *)(int, char *, int)) fpProc)(2, szTemp, sizeof(szTemp)) > 0)
\r
324 _aiInfo->fiInfo.szSuffix = MB2String(szTemp);
\r
326 if (((int (PASCAL *)(int, char *, int)) fpProc)(3, szTemp, sizeof(szTemp)) > 0)
\r
328 _aiInfo->fiInfo.szTypeName = MB2String(szTemp);
\r
331 _aiInfo->fiInfo.szTPIName = wxT("spiLibrary");
\r
332 _aiInfo->fiInfo.llSupportedCommand = TPI_COMMAND_EXTRACT;
\r
334 return TPI_ERROR_SUCCESS;
\r
337 int __stdcall Command
\r
339 unsigned int _uCommand,
\r
340 TPI_SWITCHES * _swInfo,
\r
341 const wxString & _szArcName,
\r
342 const wxArrayString & _szFiles
\r
345 if (_uCommand != TPI_COMMAND_EXTRACT)
\r
347 return TPI_ERROR_D_UNSUPPORTED;
\r
350 //
\93W
\8aJ
\8f\88\97\9d\82Ì
\82Ý
\82ð
\8ds
\82¤
\81B
\r
351 FARPROC fpProc = ::GetProcAddress(g_hLib, "GetFile");
\r
352 if (fpProc == NULL)
\r
354 return TPI_ERROR_U_USE_LIBRARY;
\r
357 //
\8f\91\8cÉ
\83n
\83\93\83h
\83\8b\82ð
\8eæ
\93¾
\81B
\r
359 int nErrorCode = OpenArchive(_szArcName, & hArchive);
\r
360 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
365 //
\83R
\81[
\83\8b\83o
\83b
\83N
\82ð
\91\97\90M
\81B
\r
366 wxFileName _fnArcName(_szArcName);
\r
367 TPI_PROCESSINFO piInfo;
\r
368 piInfo.uMessage = TPI_MESSAGE_STATUS;
\r
369 piInfo.uStatus = TPI_STATUS_OPENARCHIVE;
\r
370 piInfo.fiInfo.fnFileName = _fnArcName;
\r
371 if (CallbackProc(0, 0, (long) & piInfo))
\r
373 return TPI_ERROR_D_SKIPPED;
\r
376 //
\8f\88\97\9d\8eÀ
\8ds
\81B
\r
377 piInfo.uStatus = TPI_STATUS_INPROCESS;
\r
378 nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, TRUE);
\r
379 if (nErrorCode == TPI_ERROR_SUCCESS)
\r
383 //
\83R
\81[
\83\8b\83o
\83b
\83N
\82ð
\91\97\90M
\81B
\r
384 piInfo.uStatus = TPI_STATUS_BEGINPROCESS;
\r
385 if (CallbackProc(0, 0, (long) & piInfo))
\r
387 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
390 piInfo.uStatus = TPI_STATUS_INPROCESS;
\r
392 //
\8f\88\97\9d\91Î
\8fÛ
\82©
\82Ç
\82¤
\82©
\94»
\92è
\81B
\r
393 if ((! _szFiles.IsEmpty()) && _szFiles.Index(piInfo.fiInfo.szStoredName) == wxNOT_FOUND)
\r
398 //
\8fo
\97Í
\96¼
\8dì
\90¬
\81B
\r
399 wxString szTargetPath = _swInfo->fnDestinationDirectory.GetPathWithSep();
\r
400 if (_swInfo->fStoreDirectoryPathes)
\r
402 //
\93W
\8aJ
\90æ
\83f
\83B
\83\8c\83N
\83g
\83\8a\82ð
\8dì
\90¬
\81B
\r
403 szTargetPath += piInfo.fiInfo.fnFileName.GetFullPath();
\r
404 wxFileName fnDest(szTargetPath);
\r
405 if (! fnDest.Mkdir(0777, wxPATH_MKDIR_FULL) || ::wxDirExists(fnDest.GetFullPath()))
\r
407 nErrorCode = TPI_ERROR_IO_DIR_WRITE;
\r
413 szTargetPath += piInfo.fiInfo.fnFileName.GetFullName();
\r
416 //
\83t
\83@
\83C
\83\8b\8fo
\97Í
\82É
\82Í
\91Î
\89\9e\82µ
\82Ä
\82È
\82¢
\82Ì
\82Å
\83\81\83\82\83\8a\8fo
\97Í
\82Å
\91ã
\8ds
\81B
\r
417 HLOCAL hMemory = NULL;
\r
418 nErrorCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, char *, unsigned int, FARPROC, long)) fpProc)(_szArcName.ToUTF8(), (long) piInfo.fiInfo.pCustomInfo, (char *) & hMemory, 0x0100, (FARPROC) CallbackProc, (long) & piInfo));
\r
419 if (nErrorCode == TPI_ERROR_SUCCESS && hMemory == NULL)
\r
421 nErrorCode = TPI_ERROR_UNDEFINED;
\r
423 if (nErrorCode != TPI_ERROR_SUCCESS)
\r
428 //
\93W
\8aJ
\90æ
\82É
\8fo
\97Í
\81B
\r
430 //
\8b
\90§
\8fã
\8f\91\82«
\82·
\82é
\82Ì
\82Å
\92\8d\88Ó
\81B
\r
431 if (! hFile.Create(szTargetPath, true))
\r
433 nErrorCode = TPI_ERROR_IO_FILE_OPEN;
\r
437 bool bErrorOccured = hFile.Write(::LocalLock(hMemory), (size_t) piInfo.fiInfo.llUnpackedSize.GetValue()) != piInfo.fiInfo.llUnpackedSize;
\r
438 ::LocalUnlock(hMemory);
\r
439 ::LocalFree(hMemory);
\r
443 nErrorCode = TPI_ERROR_IO_FILE_WRITE;
\r
446 nErrorCode = TPI_ERROR_SUCCESS;
\r
448 //
\83R
\81[
\83\8b\83o
\83b
\83N
\82ð
\91\97\90M
\81B
\r
449 piInfo.uStatus = TPI_STATUS_ENDPROCESS;
\r
450 if (CallbackProc(0, 0, (long) & piInfo))
\r
452 nErrorCode = TPI_ERROR_D_SKIPPED;
\r
456 while ((nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, FALSE)) != TPI_ERROR_S_ENDOFDATA);
\r
457 if (nErrorCode == TPI_ERROR_S_ENDOFDATA)
\r
459 //
\8fI
\92[
\82É
\92B
\82µ
\82½
\8fê
\8d\87\81B
\r
460 nErrorCode = TPI_ERROR_SUCCESS;
\r
463 CloseArchive(hArchive);
\r
468 int __stdcall SetCallbackProc
\r
470 TPI_PROC _prArcProc
\r
473 //
\83|
\83C
\83\93\83^
\82ð
\95Û
\91¶
\81B
\r
474 if (_prArcProc == NULL)
\r
476 return TPI_ERROR_D_PARAMETER;
\r
478 g_prProc = * _prArcProc;
\r
480 return TPI_ERROR_SUCCESS;
\r