-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspi00in.cpp
269 lines (221 loc) · 6.83 KB
/
spi00in.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
// Copyright (c) 2024 sincos2854
// Licensed under the MIT License
#include <memory>
#include <string>
#include "common.h"
#include "ifsjpeglicm.h"
#define BUF_SIZE_HEADER 2048
int UnicodeToAnsi(LPCWSTR unicode, LPSTR ansi, int size)
{
if (!unicode || !unicode[0])
{
return 0;
}
int len = WideCharToMultiByte(CP_ACP, 0, unicode, -1, NULL, 0, NULL, NULL);
if (len == 0)
{
return 0;
}
// When the buffer size is insufficient, WideCharToMultiByte returns 0, so it is stored once in std::string.
std::string buf(static_cast<size_t>(len) - 1, '\0');
len = WideCharToMultiByte(CP_ACP, 0, unicode, -1, buf.data(), len, NULL, NULL);
if (len == 0)
{
return 0;
}
else if (size < len)
{
len = size;
}
strncpy_s(ansi, len, buf.c_str(), _TRUNCATE);
return static_cast<int>(strlen(ansi));
}
int AnsiToUnicode(LPCSTR ansi, std::wstring& unicode)
{
unicode.clear();
if (!ansi || !ansi[0])
{
return 0;
}
int len = MultiByteToWideChar(CP_ACP, 0, ansi, -1, NULL, 0);
if (len == 0)
{
return 0;
}
unicode.resize(static_cast<size_t>(len) - 1);
len = MultiByteToWideChar(CP_ACP, 0, ansi, -1, unicode.data(), len);
if (len == 0)
{
unicode.clear();
return 0;
}
return static_cast<int>(unicode.size());
}
int ReadDataFromFile(LPCWSTR file_name, LONG_PTR macbin_offset, std::unique_ptr<BYTE[]>& file_data, size_t& file_size)
{
auto handle = std::unique_ptr<HANDLE, FileHandleDeleter>(
CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL), FileHandleDeleter()
);
if (handle.get() == INVALID_HANDLE_VALUE)
{
return SPI_FILE_READ_ERROR;
}
LARGE_INTEGER size{};
if (!GetFileSizeEx(handle.get(), &size))
{
return SPI_FILE_READ_ERROR;
}
#ifndef _WIN64
if (size.HighPart)
{
return SPI_FILE_READ_ERROR;
}
#endif
file_size = static_cast<size_t>(size.QuadPart) - macbin_offset;
if (file_size < HEADER_MIN_SIZE)
{
return SPI_NOT_SUPPORT;
}
if (SetFilePointer(handle.get(), static_cast<LONG>(macbin_offset), NULL, FILE_BEGIN) != static_cast<DWORD>(macbin_offset))
{
return SPI_FILE_READ_ERROR;
}
file_data = std::make_unique_for_overwrite<BYTE[]>(file_size);
size_t remain = file_size, offset = 0;
DWORD to_read = 0, read = 0;
BOOL ret;
while (0 < remain)
{
to_read = (ULONG_MAX < remain) ? ULONG_MAX : static_cast<DWORD>(remain);
ret = ReadFile(handle.get(), file_data.get() + offset, to_read, &read, NULL);
if (!ret || to_read != read)
{
return SPI_FILE_READ_ERROR;
}
remain -= read;
offset += read;
}
return SPI_ALL_RIGHT;
}
int __stdcall GetPluginInfo(int infono, LPSTR buf, int buflen)
{
wchar_t unicode[MAX_PATH];
if (GetPluginInfoW(infono, unicode, MAX_PATH) == 0)
{
return 0;
}
return UnicodeToAnsi(unicode, buf, buflen);
}
int __stdcall GetPluginInfoW(int infono, LPWSTR buf, int buflen)
{
if (buflen <= 1 || infono < 0 || (sizeof(plugin_info) / sizeof(plugin_info[0])) <= static_cast<size_t>(infono))
{
return 0;
}
auto str = plugin_info[infono];
int len = static_cast<int>(wcslen(str)) + 1;
if (buflen < len)
{
len = buflen;
}
wcsncpy_s(buf, len, str, _TRUNCATE);
return static_cast<int>(wcslen(buf));
}
int __stdcall IsSupported(LPCSTR filename, DWORD_PTR dw)
{
std::wstring unicode;
AnsiToUnicode(filename, unicode);
return IsSupportedW(unicode.c_str(), dw);
}
int __stdcall IsSupportedW(LPCWSTR filename, DWORD_PTR dw)
{
std::unique_ptr<BYTE[]> buf;
LPBYTE data = nullptr;
DWORD read_size = 0;
if ((dw & static_cast<DWORD_PTR>(-1) - 0xFFFF) == 0)
{
buf = std::make_unique_for_overwrite<BYTE[]>(BUF_SIZE_HEADER);
if (!ReadFile(reinterpret_cast<HANDLE>(dw), buf.get(), BUF_SIZE_HEADER, &read_size, NULL))
{
return 0;
}
if (read_size < HEADER_MIN_SIZE)
{
return 0;
}
data = buf.get();
}
else
{
data = reinterpret_cast<LPBYTE>(dw);
}
if (IsSupportedEx(filename, data))
{
return 1;
}
return 0;
}
int __stdcall GetPictureInfo(LPCSTR buf, LONG_PTR len, UINT flag, PictureInfo* lpInfo)
{
std::wstring unicode;
if ((flag & 7) == 0)
{
AnsiToUnicode(buf, unicode);
}
return GetPictureInfoW(unicode.c_str(), len, flag, lpInfo);
}
int __stdcall GetPictureInfoW(LPCWSTR buf, LONG_PTR len, UINT flag, PictureInfo* lpInfo)
{
if (!lpInfo)
{
return SPI_OTHER_ERROR;
}
if ((flag & 7) == 0)
{
std::unique_ptr<BYTE[]> file_data;
size_t file_size = 0;
int ret = ReadDataFromFile(buf, len, file_data, file_size);
if (ret)
{
return ret;
}
return GetPictureInfoEx(buf, file_data.get(), file_size, lpInfo);
}
return GetPictureInfoEx(NULL, reinterpret_cast<LPBYTE>(const_cast<LPWSTR>(buf)), len, lpInfo);
}
int __stdcall GetPicture(LPCSTR buf, LONG_PTR len, UINT flag, HANDLE* pHBInfo, HANDLE* pHBm, ProgressCallback lpPrgressCallback, LONG_PTR lData)
{
std::wstring unicode;
if ((flag & 7) == 0)
{
AnsiToUnicode(buf, unicode);
}
return GetPictureW(unicode.c_str(), len, flag, pHBInfo, pHBm, lpPrgressCallback, lData);
}
int __stdcall GetPictureW(LPCWSTR buf, LONG_PTR len, UINT flag, HANDLE* pHBInfo, HANDLE* pHBm, ProgressCallback lpPrgressCallback, LONG_PTR lData)
{
if (!pHBInfo || !pHBm)
{
return SPI_OTHER_ERROR;
}
if ((flag & 7) == 0)
{
std::unique_ptr<BYTE[]> file_data;
size_t file_size = 0;
int ret = ReadDataFromFile(buf, len, file_data, file_size);
if (ret)
{
return ret;
}
return GetPictureEx(buf, file_data.get(), file_size, pHBInfo, pHBm, lpPrgressCallback, lData);
}
return GetPictureEx(NULL, reinterpret_cast<LPBYTE>(const_cast<LPWSTR>(buf)), len, pHBInfo, pHBm, lpPrgressCallback, lData);
}
int __stdcall GetPreview(LPCSTR buf, LONG_PTR len, UINT flag, HANDLE* pHBInfo, HANDLE* pHBm, ProgressCallback lpPrgressCallback, LONG_PTR lData)
{
return GetPicture(buf, len, flag, pHBInfo, pHBm, lpPrgressCallback, lData);
}
int __stdcall GetPreviewW(LPCWSTR buf, LONG_PTR len, UINT flag, HANDLE* pHBInfo, HANDLE* pHBm, ProgressCallback lpPrgressCallback, LONG_PTR lData)
{
return GetPictureW(buf, len, flag, pHBInfo, pHBm, lpPrgressCallback, lData);
}