OSDN Git Service

CheckArchiveの役割を縮小して高速化。
[tpi/lychee.git] / src / plugin / spiLibrary / spiLibrary.cpp
1 /*******************************************************************************\r
2   TPI - flexible but useless plug-in framework.\r
3   Copyright (C) 2002-2009 Silky\r
4 \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
8   any later version.\r
9 \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
13   for more details.\r
14 \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
18 \r
19   $Id$\r
20 *******************************************************************************/\r
21 \r
22 //******************************************************************************\r
23 //    Includes\r
24 //******************************************************************************\r
25 \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
31 #include <wx/dir.h>\r
32 #include <windows.h>\r
33 #include "spiLibrary.h"\r
34 \r
35 //******************************************************************************\r
36 //    Global varients\r
37 //******************************************************************************\r
38 \r
39 HMODULE g_hLib;\r
40 TPI_PROC g_prProc;\r
41 \r
42 //******************************************************************************\r
43 //    Callback Wrapper\r
44 //******************************************************************************\r
45 \r
46 int __stdcall CallbackProc(int _nNow, int, long _lData)\r
47 {\r
48         // 構造体を初期化。\r
49         TPI_PROCESSINFO * piInfo = (TPI_PROCESSINFO *) _lData;\r
50         piInfo->nProcessedSize = _nNow;\r
51 \r
52         // コールバック関数に送信。\r
53         return g_prProc != NULL && g_prProc(TPI_NOTIFY_COMMON, piInfo) == TPI_CALLBACK_CANCEL;\r
54 }\r
55 \r
56 //******************************************************************************\r
57 //    Inside Functions\r
58 //******************************************************************************\r
59 \r
60 int ErrorCodeConvert(int nErrorCode)\r
61 {\r
62         switch (nErrorCode)\r
63         {\r
64         case -1: return TPI_ERROR_U_USE_LIBRARY;\r
65         case 0:  return TPI_ERROR_SUCCESS;\r
66         case 1:  return TPI_ERROR_D_SKIPPED;\r
67         case 2:  return TPI_ERROR_ARC_UNSUPPORTED;\r
68         case 3:  return TPI_ERROR_ARC_BROKEN_MISC;\r
69         case 4:  return TPI_ERROR_D_OUTOFMEMORY;\r
70         case 5:  return TPI_ERROR_D_USEMEMORY;\r
71         case 6:  return TPI_ERROR_IO_ARC_READ;\r
72         default: return TPI_ERROR_UNDEFINED;\r
73         }\r
74 }\r
75 \r
76 //******************************************************************************\r
77 //    Functions\r
78 //******************************************************************************\r
79 \r
80 #ifdef __cplusplus\r
81 extern "C"\r
82 {\r
83 #endif\r
84 \r
85 int __stdcall GetPluginInformation\r
86 (\r
87         unsigned int _uInfoId,\r
88         wxULongLong_t,\r
89         void * _pPtr\r
90 )\r
91 {\r
92         if (_pPtr == NULL)\r
93         {\r
94                 return TPI_ERROR_D_PARAMETER;\r
95         }\r
96         switch (_uInfoId)\r
97         {\r
98         case TPI_INFO_VERSION_MAJOR:\r
99         case TPI_INFO_VERSION_MINOR:\r
100                 * (int *) _pPtr = 0;\r
101                 break;\r
102         case TPI_INFO_VERSION_API:\r
103                 * (int *) _pPtr = 2;\r
104                 break;\r
105         default:\r
106                 return TPI_ERROR_D_PARAMETER;\r
107         }\r
108         return TPI_ERROR_SUCCESS;\r
109 }\r
110 \r
111 int __stdcall GetFormatInformation(TPI_FORMATINFO *, bool)\r
112 {\r
113         return TPI_ERROR_D_UNSUPPORTED;\r
114 }\r
115 \r
116 int __stdcall LoadPlugin\r
117 (\r
118         const wxString & _szArcName,\r
119         wxULongLong_t\r
120 )\r
121 {\r
122         wxStandardPaths p;\r
123         wxString szSPIPath = wxPathOnly(p.GetExecutablePath()) + wxT("/lib/"), szSPIName;\r
124         wxDir fs(szSPIPath);\r
125         if (fs.GetFirst(& szSPIName, wxT("*.spi")))\r
126         {\r
127                 do\r
128                 {\r
129                         // SPIをロード。\r
130                         g_hLib = ::LoadLibrary((szSPIPath + szSPIName).wchar_str());\r
131                         if (g_hLib == NULL)\r
132                         {\r
133                                 continue;\r
134                         }\r
135 \r
136                         // GetPluginInfoを実行。\r
137                         FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");\r
138                         char szPluginType[5]; // 種類4bytes + NULL\r
139                         if (fpProc == NULL\r
140                                 || ((int (PASCAL *)(int, char *, int)) fpProc)(0, szPluginType, sizeof(szPluginType)) <= 0\r
141                                 || szPluginType[2] != 'A' || szPluginType[3] != 'M')\r
142                         {\r
143                                 ::FreeLibrary(g_hLib);\r
144                                 continue;\r
145                         }\r
146 \r
147                         // 書庫に対応しているかチェック。\r
148                         if (CheckArchive(_szArcName, NULL) == TPI_ERROR_SUCCESS)\r
149                         {\r
150                                 // 対応していれば処理を終了。\r
151                                 return TPI_ERROR_SUCCESS;\r
152                         }\r
153                 }\r
154                 while (fs.GetNext(& szSPIName));\r
155         }\r
156         return TPI_ERROR_U_LOAD_LIBRARY;\r
157 }\r
158 \r
159 int __stdcall FreePlugin\r
160 (\r
161         void * // _pReserved\r
162 )\r
163 {\r
164         ::FreeLibrary(g_hLib);\r
165         return TPI_ERROR_SUCCESS;\r
166 }\r
167 \r
168 int __stdcall CheckArchive\r
169 (\r
170         const wxString & _szArcName,\r
171         wxULongLong_t *\r
172 )\r
173 {\r
174         FARPROC fpProc = ::GetProcAddress(g_hLib, "IsSupported");\r
175         if (fpProc == NULL)\r
176         {\r
177                 return TPI_ERROR_U_USE_LIBRARY;\r
178         }\r
179 \r
180         wxFile hFile;\r
181         if (! hFile.Exists(_szArcName) || ! hFile.Open(_szArcName, wxFile::read))\r
182         {\r
183                 return TPI_ERROR_IO_ARC_OPEN;\r
184         }\r
185 \r
186         char buffer[2050];\r
187         ::ZeroMemory(buffer, sizeof(buffer));\r
188         if (hFile.Read(buffer, sizeof(buffer)) == wxInvalidOffset)\r
189         {\r
190                 hFile.Close();\r
191                 return TPI_ERROR_IO_ARC_READ;\r
192         }\r
193         hFile.Close();\r
194         return ((BOOL (PASCAL *)(const char *, unsigned long)) fpProc)(_szArcName.ToUTF8(), (unsigned long) buffer) ? TPI_ERROR_SUCCESS : TPI_ERROR_D_UNSUPPORTED;\r
195 }\r
196 \r
197 int __stdcall OpenArchive\r
198 (\r
199         const wxString & _szArcName,\r
200         void * * _hArchive\r
201 )\r
202 {\r
203         FARPROC fpProc = ::GetProcAddress(g_hLib, "GetArchiveInfo");\r
204         if (fpProc == NULL)\r
205         {\r
206                 return TPI_ERROR_U_USE_LIBRARY;\r
207         }\r
208 \r
209         int nReturnCode = ErrorCodeConvert(((int (PASCAL *)(const char *, long, unsigned int, HLOCAL *)) fpProc)(_szArcName.ToUTF8(), 0, 0, _hArchive));\r
210         if (nReturnCode != TPI_ERROR_SUCCESS)\r
211         {\r
212                 return nReturnCode;\r
213         }\r
214 \r
215         return * _hArchive == NULL ? TPI_ERROR_IO_ARC_OPEN : nReturnCode;\r
216 }\r
217 \r
218 int __stdcall CloseArchive\r
219 (\r
220         void * _hArchive\r
221 )\r
222 {\r
223         return ::LocalFree(_hArchive) == NULL ? TPI_ERROR_SUCCESS : TPI_ERROR_IO_ARC_CLOSE;\r
224 }\r
225 \r
226 int __stdcall GetFileInformation\r
227 (\r
228         void * _hArchive,\r
229         TPI_FILEINFO * _fiInfo,\r
230         bool _bFirst\r
231 )\r
232 {\r
233         static wxULongLong_t nFileCount, nFilePointer;\r
234 \r
235         if (_bFirst)\r
236         {\r
237                 nFilePointer = 0;\r
238                 nFileCount = ::LocalSize(_hArchive) / sizeof(fileInfo);\r
239         }\r
240 \r
241         if (nFilePointer > nFileCount)\r
242         {\r
243                 return TPI_ERROR_S_ENDOFDATA;\r
244         }\r
245 \r
246         fileInfo pfiInfo = ((fileInfo *) ::LocalLock(_hArchive))[nFilePointer];\r
247         if (pfiInfo.method[0] == 0)\r
248         {\r
249                 // 丁寧なプラグインのための数合わせ (^^;\r
250                 nFileCount = nFilePointer;\r
251                 return TPI_ERROR_S_ENDOFDATA;\r
252         }\r
253 \r
254         _fiInfo->dwAttribute    = 0;\r
255         _fiInfo->dwCRC32        = pfiInfo.crc;\r
256         _fiInfo->nPackedSize    = pfiInfo.compsize;\r
257         _fiInfo->nUnpackedSize  = pfiInfo.filesize;\r
258         _fiInfo->wCompressRatio = pfiInfo.compsize >= pfiInfo.filesize ? 1000 : (WORD) (1000 * pfiInfo.compsize / pfiInfo.filesize);\r
259         _fiInfo->nFileId        = nFilePointer++;\r
260         _fiInfo->tmModified     = pfiInfo.timestamp;\r
261         _fiInfo->szStoredName   = MB2String(pfiInfo.path) + MB2String(pfiInfo.filename);\r
262         _fiInfo->fnFileName     = wxFileName(_fiInfo->szStoredName);\r
263         _fiInfo->szMethod       = MB2String((char *) pfiInfo.method);\r
264         _fiInfo->pCustomInfo    = (void *) pfiInfo.position;\r
265         ::LocalUnlock(_hArchive);\r
266 \r
267         return TPI_ERROR_SUCCESS;\r
268 }\r
269 \r
270 int __stdcall GetArchiveInformation\r
271 (\r
272         void *,\r
273         TPI_ARCHIVEINFO * _aiInfo\r
274 )\r
275 {\r
276         // GetPluginInfoを実行。\r
277         FARPROC fpProc = ::GetProcAddress(g_hLib, "GetPluginInfo");\r
278         if (fpProc != NULL)\r
279         {\r
280                 char szTemp[20];\r
281                 if (((int (PASCAL *)(int, char *, int)) fpProc)(2, szTemp, sizeof(szTemp)) > 0)\r
282                 {\r
283                         _aiInfo->fiInfo.szSuffix = MB2String(szTemp);\r
284                 }\r
285                 if (((int (PASCAL *)(int, char *, int)) fpProc)(3, szTemp, sizeof(szTemp)) > 0)\r
286                 {\r
287                         _aiInfo->fiInfo.szTypeName = MB2String(szTemp);\r
288                 }\r
289         }\r
290         _aiInfo->fiInfo.szTPIName = wxT("spiLibrary");\r
291         _aiInfo->fiInfo.eSupportedCommand = TPI_COMMAND_EXTRACT;\r
292 \r
293         return TPI_ERROR_SUCCESS;\r
294 }\r
295 \r
296 int __stdcall Command\r
297 (\r
298         wxULongLong_t _eCommand,\r
299         TPI_SWITCHES * _swInfo,\r
300         const wxString & _szArcName,\r
301         const wxArrayString & _szFiles\r
302 )\r
303 {\r
304         if (_eCommand != TPI_COMMAND_EXTRACT)\r
305         {\r
306                 return TPI_ERROR_D_UNSUPPORTED;\r
307         }\r
308 \r
309         // 展開処理のみを行う。\r
310         FARPROC fpProc = ::GetProcAddress(g_hLib, "GetFile");\r
311         if (fpProc == NULL)\r
312         {\r
313                 return TPI_ERROR_U_USE_LIBRARY;\r
314         }\r
315 \r
316         // 書庫ハンドルを取得。\r
317         void * hArchive;\r
318         int nErrorCode = OpenArchive(_szArcName, & hArchive);\r
319         if (nErrorCode != TPI_ERROR_SUCCESS)\r
320         {\r
321                 return nErrorCode;\r
322         }\r
323 \r
324         // コールバックを送信。\r
325         wxFileName _fnArcName(_szArcName);\r
326         TPI_PROCESSINFO piInfo;\r
327         piInfo.eMessage = TPI_MESSAGE_STATUS;\r
328         piInfo.eStatus  = TPI_STATUS_OPENARCHIVE;\r
329         piInfo.fiInfo.fnFileName = _fnArcName;\r
330         if (CallbackProc(0, 0, (long) & piInfo))\r
331         {\r
332                 return TPI_ERROR_D_SKIPPED;\r
333         }\r
334 \r
335         // 処理実行。\r
336         piInfo.eStatus = TPI_STATUS_INPROCESS;\r
337         nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, true);\r
338         if (nErrorCode == TPI_ERROR_SUCCESS)\r
339         {\r
340                 do\r
341                 {\r
342                         // コールバックを送信。\r
343                         piInfo.eStatus = TPI_STATUS_BEGINPROCESS;\r
344                         if (CallbackProc(0, 0, (long) & piInfo))\r
345                         {\r
346                                 nErrorCode = TPI_ERROR_D_SKIPPED;\r
347                                 break;\r
348                         }\r
349                         piInfo.eStatus = TPI_STATUS_INPROCESS;\r
350 \r
351                         // 処理対象かどうか判定。\r
352                         if ((! _szFiles.IsEmpty()) && _szFiles.Index(piInfo.fiInfo.szStoredName) == wxNOT_FOUND)\r
353                         {\r
354                                 continue;\r
355                         }\r
356 \r
357                         // 出力名作成。\r
358                         wxString szTargetPath = _swInfo->fnDestinationDirectory.GetPathWithSep();\r
359                         if (_swInfo->fStoreDirectoryPathes)\r
360                         {\r
361                                 // 展開先ディレクトリを作成。\r
362                                 szTargetPath += piInfo.fiInfo.fnFileName.GetFullPath();\r
363                                 wxFileName fnDest(szTargetPath);\r
364                                 if (! fnDest.Mkdir(0777, wxPATH_MKDIR_FULL) || ::wxDirExists(fnDest.GetFullPath()))\r
365                                 {\r
366                                         nErrorCode = TPI_ERROR_IO_DIR_WRITE;\r
367                                         break;\r
368                                 }\r
369                         }\r
370                         else\r
371                         {\r
372                                 szTargetPath += piInfo.fiInfo.fnFileName.GetFullName();\r
373                         }\r
374 \r
375                         // ファイル出力には対応してないのでメモリ出力で代行。\r
376                         HLOCAL hMemory = NULL;\r
377                         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
378                         if (nErrorCode == TPI_ERROR_SUCCESS && hMemory == NULL)\r
379                         {\r
380                                 nErrorCode = TPI_ERROR_UNDEFINED;\r
381                         }\r
382                         if (nErrorCode != TPI_ERROR_SUCCESS)\r
383                         {\r
384                                 break;\r
385                         }\r
386 \r
387                         // 展開先に出力。\r
388                         wxFile hFile;\r
389                         // 強制上書きするので注意。\r
390                         if (! hFile.Create(szTargetPath, true))\r
391                         {\r
392                                 nErrorCode = TPI_ERROR_IO_FILE_OPEN;\r
393                                 break;\r
394                         }\r
395 \r
396                         bool bErrorOccured = hFile.Write(::LocalLock(hMemory), piInfo.fiInfo.nUnpackedSize) != piInfo.fiInfo.nUnpackedSize;\r
397                         ::LocalUnlock(hMemory);\r
398                         ::LocalFree(hMemory);\r
399                         hFile.Close();\r
400                         if (bErrorOccured)\r
401                         {\r
402                                 nErrorCode = TPI_ERROR_IO_FILE_WRITE;\r
403                                 break;\r
404                         }\r
405                         nErrorCode = TPI_ERROR_SUCCESS;\r
406 \r
407                         // コールバックを送信。\r
408                         piInfo.eStatus = TPI_STATUS_ENDPROCESS;\r
409                         if (CallbackProc(0, 0, (long) & piInfo))\r
410                         {\r
411                                 nErrorCode = TPI_ERROR_D_SKIPPED;\r
412                                 break;\r
413                         }\r
414                 }\r
415                 while ((nErrorCode = GetFileInformation(hArchive, & piInfo.fiInfo, false)) != TPI_ERROR_S_ENDOFDATA);\r
416                 if (nErrorCode == TPI_ERROR_S_ENDOFDATA)\r
417                 {\r
418                         // 終端に達した場合。\r
419                         nErrorCode = TPI_ERROR_SUCCESS;\r
420                 }\r
421         }\r
422         CloseArchive(hArchive);\r
423 \r
424         return nErrorCode;\r
425 }\r
426 \r
427 int __stdcall SetCallbackProc\r
428 (\r
429         TPI_PROC _prArcProc\r
430 )\r
431 {\r
432         // ポインタを保存。\r
433         if (_prArcProc == NULL)\r
434         {\r
435                 return TPI_ERROR_D_PARAMETER;\r
436         }\r
437         g_prProc = * _prArcProc;\r
438 \r
439         return TPI_ERROR_SUCCESS;\r
440 }\r
441 \r
442 #ifdef __cplusplus\r
443 }\r
444 #endif\r