-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathSusie.cpp
509 lines (411 loc) · 12.3 KB
/
Susie.cpp
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
#include "StdAfx.h"
#include "Susie.h"
#include "ArcFile.h"
#include "Common.h"
#include "Image.h"
using SPI_PROGRESS = int (WINAPI*)(int, int, long);
using GetPluginInfoProc = int (WINAPI*)(int infono, char* buf, int buflen);
using IsSupportedProc = int (WINAPI*)(char* filename, DWORD dw);
using GetArchiveInfoProc = int (WINAPI*)(char* buf, long len, unsigned int flag, HLOCAL* lphInf);
using GetFileProc = int (WINAPI*)(char* src, long len, char* dest, unsigned int flag, FARPROC prgressCallback, long lData);
using GetPictureProc = int (WINAPI*)(char* buf, long len, unsigned int flag, HLOCAL* pHBInfo, HLOCAL* pHBm, SPI_PROGRESS lpPrgressCallback, long lData);
using ConfigurationDlgProc = int (WINAPI*)(HWND, int);
#pragma pack(push, 1)
struct fileInfo
{
u8 method[8]; // Type of compression method
u32 position; // Position on the file
u32 compsize; // Compressed size
u32 filesize; // Original file size
u32 timestamp; // Modified date of the file
char path[200]; // Relative Path
char filename[200]; // Filename
u32 crc; // CRC
};
#pragma pack(pop, 1)
std::vector<SSusieInfo> CSusie::m_main;
std::vector<SSusieInfo> CSusie::m_temporary;
/// Initialization
void CSusie::Init()
{
m_temporary = m_main;
}
/// Apply
void CSusie::Apply()
{
m_main = m_temporary;
}
/// Mount
///
/// @param archive Archive
///
bool CSusie::Mount(CArcFile* archive)
{
// Get header
const u8* pbtHeader = archive->GetHeader();
// Mount
for (const auto& target : m_main)
{
if (target.validity == 0)
{
// Invalid Susie plugin
continue;
}
// Get IsSupported()
auto* IsSupported = reinterpret_cast<IsSupportedProc>(target.plugin.GetProcAddress(_T("IsSupported")));
if (IsSupported == nullptr)
{
// IsSupported function is not implemented
continue;
}
// Copy the archive file path
char szPathToArc[MAX_PATH];
YCStringA clsPathToArc = archive->GetArcPath();
strcpy(szPathToArc, clsPathToArc);
// Call IsSupported()
u8 abtHeader[2048];
memcpy(abtHeader, pbtHeader, sizeof(abtHeader));
if (IsSupported(szPathToArc, reinterpret_cast<DWORD>(abtHeader)) == 0)
{
// Archive file not supported
continue;
}
// Get GetArchiveInfo()
auto* GetArchiveInfo = reinterpret_cast<GetArchiveInfoProc>(target.plugin.GetProcAddress(_T("GetArchiveInfo")));
if (GetArchiveInfo == nullptr)
{
// We trust IsSupported() and attempt to mount
return archive->Mount();
}
// Copy archive path (Since there is a chance that IsSupported() can be overwritten)
strcpy(szPathToArc, clsPathToArc);
// Call GetArchiveInfo()
YCLocalMemory cllmFileInfo;
if (GetArchiveInfo(szPathToArc, 0, 0, &cllmFileInfo.GetHandle()) != 0)
{
// Error in function GetArchiveInfo()
cllmFileInfo.Free();
// We will trust IsSupported() and attempt to mount
return archive->Mount();
}
// Error in the function GetArchiveInfo()
if (cllmFileInfo.GetHandle() == nullptr)
{
// We will trust IsSupported() and attempt to mount
return archive->Mount();
}
// Get file information
const auto* pstFileInfo = static_cast<fileInfo*>(cllmFileInfo.Lock());
const size_t uSusieFileInfoSize = cllmFileInfo.GetSize();
const size_t uFileCount = uSusieFileInfoSize / sizeof(fileInfo);
for (size_t uIndex = 0; uIndex < uFileCount; uIndex++)
{
if (pstFileInfo->method[0] == '\0')
{
// Exit
break;
}
char szFileName[MAX_PATH];
if (pstFileInfo->path[0] != '\0')
{
// Path exists
strcpy(szFileName, pstFileInfo->path);
PathAppendA(szFileName, pstFileInfo->filename);
}
else
{
strcpy(szFileName, pstFileInfo->filename);
}
// Set the file information
SFileInfo stFileInfo;
stFileInfo.name = szFileName;
stFileInfo.size_cmp = (pstFileInfo->compsize == 0) ? pstFileInfo->filesize : pstFileInfo->compsize;
stFileInfo.size_org = pstFileInfo->filesize;
stFileInfo.start = pstFileInfo->position;
stFileInfo.end = stFileInfo.start + stFileInfo.size_cmp;
stFileInfo.format.Append((LPTSTR)pstFileInfo->method, 8);
archive->AddFileInfo(stFileInfo);
pstFileInfo++;
}
// Release resources
cllmFileInfo.Free();
return true;
}
return false;
}
/// Decode
///
/// @param archive Archive
///
bool CSusie::Decode(CArcFile* archive)
{
YCStringA clsPathToArc = archive->GetArcPath();
YCStringA clsFileName = archive->GetOpenFileInfo()->name;
// Get header
const u8* pbtHeader = archive->GetHeader();
// GetPicture() file input
std::vector<const YCLibrary*> vtSupportPlugin;
for (const auto& target : m_main)
{
if (target.validity == 0)
{
// Invalid Susie plugin
continue;
}
// Get IsSupported()
auto* IsSupported = reinterpret_cast<IsSupportedProc>(target.plugin.GetProcAddress(_T("IsSupported")));
if (IsSupported == nullptr)
{
// IsSupported() function is not implemented
continue;
}
// Copy the archive's file path
char szPathToArc[MAX_PATH];
strcpy(szPathToArc, clsPathToArc);
u8 abtHeader[2048];
memcpy(abtHeader, pbtHeader, sizeof(abtHeader));
if (!IsSupported(szPathToArc, reinterpret_cast<DWORD>(abtHeader)))
{
// Archive file is not supported
continue;
}
// Archive file is supported
vtSupportPlugin.push_back(&target.plugin);
// Copy the archive file path
strcpy(szPathToArc, clsPathToArc);
// Get GetPicture()
auto* GetPicture = reinterpret_cast<GetPictureProc>(target.plugin.GetProcAddress(_T("GetPicture")));
if (GetPicture == nullptr)
{
// GetPicture() function is not implemented
continue;
}
YCLocalMemory cllmBitmapInfo;
YCLocalMemory cllmBitmapData;
// Call GetPicture()
if (GetPicture(szPathToArc, archive->GetOpenFileInfo()->start, 0x0000, &cllmBitmapInfo.GetHandle(), &cllmBitmapData.GetHandle(), nullptr, 0) != 0)
{
// GetPicture() has failed
continue;
}
// Get image information
const auto* pBMPInfo = static_cast<LPBITMAPINFO>(cllmBitmapInfo.Lock());
const u8* pHBm = static_cast<u8*>(cllmBitmapData.Lock());
const size_t HBmSize = cllmBitmapData.GetSize();
// Image output
CImage image;
image.Init(archive, pBMPInfo->bmiHeader.biWidth, pBMPInfo->bmiHeader.biHeight, pBMPInfo->bmiHeader.biBitCount, (u8*) pBMPInfo->bmiColors);
image.Write(pHBm, HBmSize);
image.Close();
// Exit (Free resources)
cllmBitmapInfo.Free();
cllmBitmapData.Free();
return true;
}
// File input - GetFile()
YCLocalMemory cllmSrc;
for (const auto& support_plugin : vtSupportPlugin)
{
// Copy archive file path
char szPathToArc[MAX_PATH];
strcpy(szPathToArc, clsPathToArc);
// Get GetFile()
auto* GetFile = reinterpret_cast<GetFileProc>(support_plugin->GetProcAddress(_T("GetFile")));
if (GetFile == nullptr)
{
// GetFile() function is not supported
continue;
}
// Call GetFile()
if (GetFile(szPathToArc, archive->GetOpenFileInfo()->start, reinterpret_cast<char*>(&cllmSrc.GetHandle()), 0x0100, nullptr, 0) != 0)
{
// GetFile has failed
cllmSrc.Free();
continue;
}
// Successful completion
break;
}
// Lock the memory for reading
bool bGetFileSuccess = true;
u8* pbtSrc = nullptr;
u32 dwSrcSize;
if (cllmSrc.GetHandle() != nullptr)
{
// Successful GetFile()
pbtSrc = static_cast<u8*>(cllmSrc.Lock());
dwSrcSize = cllmSrc.GetSize();
}
else
{
// GetFile() has failed
bGetFileSuccess = false;
// Memory allocation
dwSrcSize = archive->GetOpenFileInfo()->size_cmp;
cllmSrc.Alloc(LHND, dwSrcSize);
pbtSrc = static_cast<u8*>(cllmSrc.Lock());
// Reading
archive->Read(pbtSrc, dwSrcSize);
archive->SeekCur(-(s64)dwSrcSize);
}
// Memory Input - GetPicture()
for (const auto& target : m_main)
{
if (target.validity == 0)
{
// Invalid Susie plugin
continue;
}
// Get IsSupported()
auto* IsSupported = reinterpret_cast<IsSupportedProc>(target.plugin.GetProcAddress(_T("IsSupported")));
if (IsSupported == nullptr)
{
// IsSupported() function is not implemented
continue;
}
// Copy file path
// Plugin fails if the full path does not exist
char szPathToFile[MAX_PATH];
strcpy(szPathToFile, clsPathToArc.GetDirPath());
PathAppendA(szPathToFile, clsFileName);
// Needs 2KB
u8 abtSrcHeader[2048];
const size_t dwCopySize = (dwSrcSize <= sizeof(abtSrcHeader)) ? dwSrcSize : sizeof(abtSrcHeader);
ZeroMemory(abtSrcHeader, sizeof(abtSrcHeader));
memcpy(abtSrcHeader, pbtSrc, dwCopySize);
// Call IsSupported()
if (!IsSupported(szPathToFile, reinterpret_cast<DWORD>(abtSrcHeader)))
{
// File is not supported
continue;
}
// Get GetPicture()
auto* GetPicture = reinterpret_cast<GetPictureProc>(target.plugin.GetProcAddress(_T("GetPicture")));
if (GetPicture == nullptr)
{
// GetPicture() function is not implemented
continue;
}
YCLocalMemory cllmBitmapInfo;
YCLocalMemory cllmBitmapData;
// Call GetPicture()
if (GetPicture(reinterpret_cast<char*>(pbtSrc), dwSrcSize, 0x0001, &cllmBitmapInfo.GetHandle(), &cllmBitmapData.GetHandle(), nullptr, 0) != 0)
{
// GetPicture() has failed
continue;
}
// Get image info
const auto* pBMPInfo = static_cast<LPBITMAPINFO>(cllmBitmapInfo.Lock());
const u8* pHBm = static_cast<u8*>(cllmBitmapData.Lock());
const size_t HBmSize = cllmBitmapData.GetSize();
// Image output
CImage image;
image.Init(archive, pBMPInfo->bmiHeader.biWidth, pBMPInfo->bmiHeader.biHeight, pBMPInfo->bmiHeader.biBitCount, (u8*) pBMPInfo->bmiColors);
image.Write(pHBm, HBmSize);
image.Close();
// Exit
cllmBitmapInfo.Free();
cllmBitmapData.Free();
cllmSrc.Free();
return true;
}
// Exit if the file could not be obtained in the function GetFile()
if (!bGetFileSuccess)
{
cllmSrc.Free();
return false;
}
// Output file obtained from GetFile()
archive->OpenFile();
archive->WriteFile(pbtSrc, dwSrcSize, archive->GetOpenFileInfo()->size_cmp);
archive->CloseFile();
// Exit (Free resources)
cllmSrc.Free();
return true;
}
/// Load Susie Plugin
void CSusie::LoadSpi(const YCString& susie_folder_path)
{
// Release / Free Susie Plugin
m_main.clear();
// Susie Plugin Path
TCHAR susie_plugin_path[MAX_PATH];
lstrcpy(susie_plugin_path, susie_folder_path);
PathAppend(susie_plugin_path, _T("*.spi"));
// Susie Plugin Info
YCFileFindSx finder;
std::vector<YCString> susie_plugin_paths;
finder.FindFile(susie_plugin_paths, susie_folder_path, _T("*.spi"), false);
// Get Susie Plugin Information
YCIni susie_ini(SBL_STR_INI_SUSIE);
susie_ini.SetSection(_T("Plugins"));
size_t index = 0;
m_main.resize(susie_plugin_paths.size());
for (auto& target_path : susie_plugin_paths)
{
SSusieInfo* target = &m_main[index];
// Load SPI
if (!target->plugin.Load(target_path))
{
// Loading failed
continue;
}
// Validate SPI
susie_ini.SetKey(PathFindFileName(target_path));
susie_ini.ReadDec(&target->validity, 1);
// SPI File Path Information
target->path = target_path;
// SPI Folder Name Information
target->name = PathFindFileName(target_path);
// Get SPI Info
char buffer[1024];
auto* GetPluginInfo = reinterpret_cast<GetPluginInfoProc>(target->plugin.GetProcAddress(_T("GetPluginInfo")));
if (GetPluginInfo != nullptr)
{
if (GetPluginInfo(0, buffer, sizeof(buffer)) != 0)
{
// Successful at getting SPI info
target->version = buffer;
}
if (GetPluginInfo(1, buffer, sizeof(buffer)) != 0)
{
// Successful at getting SPI info
target->info = buffer;
}
for (int info_num = 2; ; info_num += 2)
{
if (GetPluginInfo(info_num, buffer, sizeof(buffer)) == 0)
{
// Could not get any more info
break;
}
if (!target->supported_formats.IsEmpty())
{
target->supported_formats += _T(";");
}
target->supported_formats += buffer;
}
}
// Check if the SPI has a configuration dialog
auto* ConfigurationDlg = reinterpret_cast<ConfigurationDlgProc>(target->plugin.GetProcAddress(_T("ConfigurationDlg")));
target->has_config_dialog = ConfigurationDlg != nullptr;
// Prepare the next SPI
index++;
}
// Load segments
m_main.resize(index);
}
/// Save Susie Plugin Information
void CSusie::SaveSpi()
{
YCIni susie_ini(SBL_STR_INI_SUSIE);
susie_ini.SetSection(_T("Plugins"));
// Delete info relating to Susie plugins that no longer exist.
susie_ini.DeleteSection();
// Save Susie plugin info
for (const auto& target : m_main)
{
susie_ini.SetKey(target.name);
susie_ini.WriteDec(target.validity);
}
}