-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwcx_sii_decrypt.cpp
253 lines (238 loc) · 7.11 KB
/
wcx_sii_decrypt.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
#include "stdafx.h"
#include "wcx_sii_decrypt.h"
#include "SII_Decrypt.h"
// Fake functions to make TC load the plugin
HANDLE __stdcall OpenArchive(tOpenArchiveData* ArchiveData)
{
UNREFERENCED_PARAMETER(ArchiveData);
ArchiveData->OpenResult = E_NOT_SUPPORTED;
return NULL;
}
int __stdcall ReadHeader(HANDLE hArcData, tHeaderData* HeaderData)
{
UNREFERENCED_PARAMETER(hArcData);
UNREFERENCED_PARAMETER(HeaderData);
return E_NOT_SUPPORTED;
}
int __stdcall ProcessFile(HANDLE hArcData, int Operation, char* DestPath, char* DestName)
{
UNREFERENCED_PARAMETER(hArcData);
UNREFERENCED_PARAMETER(Operation);
UNREFERENCED_PARAMETER(DestPath);
UNREFERENCED_PARAMETER(DestName);
return E_NOT_SUPPORTED;
}
void __stdcall SetChangeVolProc(HANDLE hArcData, tChangeVolProc pChangeVolProc1)
{
UNREFERENCED_PARAMETER(hArcData);
UNREFERENCED_PARAMETER(pChangeVolProc1);
}
void __stdcall SetProcessDataProc(HANDLE hArcData, tProcessDataProc pProcessDataProc)
{
UNREFERENCED_PARAMETER(hArcData);
UNREFERENCED_PARAMETER(pProcessDataProc);
}
// Wrapper class for converting UTF-16LE string into UTF-8 with automatic
// cleanup when the data are no longer needed.
class Utf8String
{
public:
Utf8String(const wchar_t* str)
{
int data_sz = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL);
m_data = new char[data_sz];
WideCharToMultiByte(CP_UTF8, 0, str, -1, m_data, data_sz, NULL, NULL);
}
~Utf8String()
{
if (m_data != NULL)
delete[] m_data;
}
operator char*() const { return m_data; }
private:
char* m_data;
};
// Open archive
HANDLE __stdcall OpenArchiveW(tOpenArchiveDataW* ArchiveDataW)
{
ArchiveHandleW* arch_data = NULL;
// Create the new archive handle object and initialize it
try
{
arch_data = new ArchiveHandleW;
}
catch (...)
{
ArchiveDataW->OpenResult = E_NO_MEMORY;
return NULL;
}
arch_data->hFile = CreateFile(ArchiveDataW->ArcName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (arch_data->hFile == INVALID_HANDLE_VALUE)
{
delete arch_data;
ArchiveDataW->OpenResult = E_EOPEN;
return NULL;
}
// Check the file signature
BYTE signature[4];
DWORD dr;
if (!ReadFile(arch_data->hFile, signature, 4, &dr, NULL) ||
(dr != 4) ||
((memcmp(signature, "BSII", 4) != 0) &&
(memcmp(signature, "ScsC", 4) != 0) &&
(memcmp(signature, "3nK", 3) != 0)))
{
CloseHandle(arch_data->hFile);
delete arch_data;
ArchiveDataW->OpenResult = E_UNKNOWN_FORMAT;
return NULL;
}
int32_t fmt = GetFileFormat(Utf8String(ArchiveDataW->ArcName));
if ((fmt != SIIDEC_RESULT_FORMAT_ENCRYPTED) && (fmt != SIIDEC_RESULT_FORMAT_BINARY) && (fmt != SIIDEC_RESULT_FORMAT_3NK))
{
CloseHandle(arch_data->hFile);
delete arch_data;
ArchiveDataW->OpenResult = E_UNKNOWN_FORMAT;
return NULL;
}
// File looks OK, return the valid handle
SetFilePointer(arch_data->hFile, 0, NULL, FILE_BEGIN);
wcscpy_s(arch_data->ArcName, ArchiveDataW->ArcName);
arch_data->OpenMode = ArchiveDataW->OpenMode;
arch_data->pProcessDataProcW = NULL;
arch_data->ContentsPos = 0;
return (HANDLE)arch_data;
}
// Get the callback function for change volume requests (not used here)
void __stdcall SetChangeVolProcW(HANDLE hArcData, tChangeVolProcW pChangeVolProc1W)
{
UNREFERENCED_PARAMETER(hArcData);
UNREFERENCED_PARAMETER(pChangeVolProc1W);
}
// Get the callback function for progress bar updating
void __stdcall SetProcessDataProcW(HANDLE hArcData, tProcessDataProcW pProcessDataProcW)
{
ArchiveHandleW* arch_data = (ArchiveHandleW*)hArcData;
arch_data->pProcessDataProcW = pProcessDataProcW;
}
// Return information about the next packed file
int __stdcall ReadHeaderExW(HANDLE hArcData, tHeaderDataExW* HeaderDataExW)
{
ArchiveHandleW* arch_data = (ArchiveHandleW*)hArcData;
int ret_value = 0;
if (arch_data->ContentsPos > 0)
ret_value = E_END_ARCHIVE;
else
{
memset(HeaderDataExW, 0, sizeof(tHeaderDataExW));
wchar_t* FileName = wcsrchr(arch_data->ArcName, L'\\');
if (FileName == NULL)
wcscpy_s(HeaderDataExW->FileName, L"decrypted.txt");
else
{
wcscpy_s(HeaderDataExW->FileName, FileName + 1);
wcscat_s(HeaderDataExW->FileName, L".txt");
}
FILETIME tm, ltm;
if (GetFileTime(arch_data->hFile, NULL, NULL, &tm) && FileTimeToLocalFileTime(&tm, <m))
FileTimeToDosDateTime(<m, ((WORD*)(&HeaderDataExW->FileTime)) + 1, (WORD*)(&HeaderDataExW->FileTime));
// Cannot get the decrypted content size without actual decryption which is very long; skipping
// HeaderDataExW->PackSize = HeaderDataExW->UnpSize = elem->Length & 0xffffffff;
//#ifdef _WIN64
// HeaderDataExW->PackSizeHigh = HeaderDataExW->UnpSizeHigh = elem->Length >> 32;
//#else
// HeaderDataExW->PackSizeHigh = HeaderDataExW->UnpSizeHigh = 0;
//#endif
HeaderDataExW->FileAttr = 0x20;
}
return ret_value;
}
// Unpack the next packed file
int __stdcall ProcessFileW(HANDLE hArcData, int Operation, WCHAR* DestPathW, WCHAR* DestNameW)
{
ArchiveHandleW* arch_data = (ArchiveHandleW*)hArcData;
if (arch_data->ContentsPos > 0)
return E_END_ARCHIVE;
++arch_data->ContentsPos;
switch (Operation)
{
case PK_SKIP:
return 0;
case PK_TEST:
case PK_EXTRACT:
{
LARGE_INTEGER zero, sz;
zero.QuadPart = 0;
SetFilePointerEx(arch_data->hFile, zero, &sz, FILE_END);
SetFilePointerEx(arch_data->hFile, zero, NULL, FILE_BEGIN);
BYTE* data = new BYTE[static_cast<unsigned int>(sz.QuadPart)];
DWORD dr;
if (!ReadFile(arch_data->hFile, data, static_cast<DWORD>(sz.QuadPart), &dr, NULL) || (dr != sz.QuadPart))
{
delete[] data;
return E_EREAD;
}
size_t res_sz = 0;
void* SiiHelper = NULL;
int32_t res = DecryptAndDecodeMemoryHelper(data, static_cast<size_t>(sz.QuadPart), NULL, &res_sz, &SiiHelper);
if (res != SIIDEC_RESULT_SUCCESS)
{
delete[] data;
return E_BAD_DATA;
}
BYTE* res_data = new BYTE[res_sz];
res = DecryptAndDecodeMemoryHelper(data, static_cast<size_t>(sz.QuadPart), res_data, &res_sz, &SiiHelper);
if (res != SIIDEC_RESULT_SUCCESS)
{
FreeHelper(&SiiHelper);
delete[] data;
delete[] res_data;
return E_BAD_DATA;
}
int ret_value = 0;
if (Operation == PK_EXTRACT)
{
WCHAR DestFullName[MAX_PATH_EX];
DestFullName[0] = L'\0';
if (DestPathW != NULL)
wcscpy_s(DestFullName, MAX_PATH_EX, DestPathW);
wcscat_s(DestFullName, MAX_PATH_EX, DestNameW);
HANDLE f = CreateFile(DestFullName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (f == INVALID_HANDLE_VALUE)
{
ret_value = E_ECREATE;
}
else
{
DWORD dw;
if (!WriteFile(f, res_data, static_cast<DWORD>(res_sz), &dw, NULL) || (dw != res_sz))
ret_value = E_EWRITE;
CloseHandle(f);
}
}
delete[] data;
delete[] res_data;
return ret_value;
}
default:
return E_NOT_SUPPORTED;
}
}
// Close the archive
int __stdcall CloseArchive(HANDLE hArcData)
{
ArchiveHandleW* arch_data = (ArchiveHandleW*)hArcData;
CloseHandle(arch_data->hFile);
delete arch_data;
return 0;
}
// Return the plugin's capabilities
int __stdcall GetPackerCaps(void)
{
return PK_CAPS_BY_CONTENT | PK_CAPS_SEARCHTEXT | PK_CAPS_HIDE;
}
// Return the plugin's background operations support information
int __stdcall GetBackgroundFlags(void)
{
return BACKGROUND_UNPACK;
}