-
Notifications
You must be signed in to change notification settings - Fork 3
/
NanaZ.cpp
248 lines (189 loc) · 8.99 KB
/
NanaZ.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
#define NOMINMAX
#include <dokan/dokan.h>
#include <7z/CPP/Common/Common.h>
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <numeric>
#include <optional>
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <vector>
#include <Windows.h>
#include "NanaZ.hpp"
#include "COMError.hpp"
#include "PropVariantUtil.hpp"
#include "PropVariantWrapper.hpp"
using namespace std::literals;
namespace {
constexpr int DefaultFormatPriority = 0;
const std::unordered_map<std::wstring, int> gFormatPriorityMap{
// Prioritize Udf over Iso
{L"Udf"s, 100},
{L"Iso"s, 0},
};
template<typename T>
std::optional<T> GetPropertyN(Func_GetHandlerProperty2 GetHandlerProperty2, UInt32 index, PROPID propId) {
PropVariantWrapper propVariant;
COMError::CheckHRESULT(GetHandlerProperty2(index, propId, &propVariant));
return FromPropVariantN<T>(propVariant);
}
template<typename T>
T GetProperty(Func_GetHandlerProperty2 GetHandlerProperty2, UInt32 index, PROPID propId) {
return GetPropertyN<T>(GetHandlerProperty2, index, propId).value();
}
}
NanaZ::FormatStore::FormatStore(Func_GetIsArc GetIsArc, Func_GetNumberOfFormats GetNumberOfFormats, Func_GetHandlerProperty2 GetHandlerProperty2) {
maxSignatureSize = 0;
UInt32 numFormats = 1;
COMError::CheckHRESULT(GetNumberOfFormats(&numFormats));
formats.resize(numFormats);
for (UInt32 formatIndex = 0; formatIndex < numFormats; formatIndex++) {
auto& format = formats[formatIndex];
// retrieve format.name
format.name = GetProperty<std::wstring>(GetHandlerProperty2, formatIndex, NArchive::NHandlerPropID::kName);
// retrieve format.clsid
const auto clsidV = GetProperty<std::vector<std::byte>>(GetHandlerProperty2, formatIndex, NArchive::NHandlerPropID::kClassID);
if (clsidV.size() != sizeof(CLSID)) {
throw std::invalid_argument("invalid CLSID given");
}
//clsid = *reinterpret_cast<const CLSID*>(clsidV.data());
std::memcpy(&format.clsid, clsidV.data(), sizeof(CLSID));
// retrieve format.extensions
const auto extensionN = GetPropertyN<std::wstring>(GetHandlerProperty2, formatIndex, NArchive::NHandlerPropID::kExtension);
const auto addExtensionN = GetPropertyN<std::wstring>(GetHandlerProperty2, formatIndex, NArchive::NHandlerPropID::kAddExtension);
if (extensionN) {
std::wstring_view restExtension(extensionN.value());
std::wstring_view restAddExtension;
if (addExtensionN) {
restAddExtension = addExtensionN.value();
}
while (!restExtension.empty()) {
const auto extensionFirstSpace = restExtension.find_first_of(L' ');
const std::wstring_view currentExtension = restExtension.substr(0, extensionFirstSpace);
restExtension = extensionFirstSpace != std::wstring_view::npos ? restExtension.substr(extensionFirstSpace + 1) : L""sv;
std::wstring_view currentAddExtension;
if (!restAddExtension.empty()) {
const auto addExtensionFirstSpace = restAddExtension.find_first_of(L' ');
currentAddExtension = restAddExtension.substr(0, addExtensionFirstSpace);
restAddExtension = addExtensionFirstSpace != std::wstring_view::npos ? restAddExtension.substr(addExtensionFirstSpace + 1) : L""sv;
}
format.extensions.emplace_back(currentExtension, currentAddExtension != L"*"sv ? currentAddExtension : L""sv);
};
}
// retrieve flags
format.flags = GetProperty<std::uint32_t>(GetHandlerProperty2, formatIndex, NArchive::NHandlerPropID::kFlags);
// retrieve format.signatures
std::size_t currentMaxSignatureSize = 0;
if (auto singleSignatureN = GetPropertyN<std::vector<std::byte>>(GetHandlerProperty2, formatIndex, NArchive::NHandlerPropID::kSignature); singleSignatureN && !singleSignatureN.value().empty()) {
currentMaxSignatureSize = singleSignatureN.value().size();
format.signatures.emplace_back(std::move(singleSignatureN.value()));
} else {
const auto multiSignatureN = GetPropertyN<std::vector<std::byte>>(GetHandlerProperty2, formatIndex, NArchive::NHandlerPropID::kMultiSignature);
if (multiSignatureN) {
const auto multiSignature = multiSignatureN.value();
const std::size_t multiSignatureSize = multiSignature.size();
for (std::size_t i = 0; i < multiSignatureSize;) {
const std::size_t currentSignatureSize = static_cast<unsigned char>(multiSignature[i++]);
if (i + currentSignatureSize > multiSignatureSize) {
throw std::invalid_argument("invalid multiSignature given");
}
format.signatures.emplace_back(multiSignature.cbegin() + i, multiSignature.cbegin() + i + currentSignatureSize);
i += currentSignatureSize;
if (currentMaxSignatureSize < currentSignatureSize) {
currentMaxSignatureSize = currentSignatureSize;
}
}
}
}
// retrieve format.signatureOffset
format.signatureOffset = GetPropertyN<std::uint32_t>(GetHandlerProperty2, formatIndex, NArchive::NHandlerPropID::kSignatureOffset);
if (format.signatureOffset && currentMaxSignatureSize) {
currentMaxSignatureSize += format.signatureOffset.value();
}
// retrieve format.IsArc
format.IsArc = nullptr;
if (FAILED(GetIsArc(formatIndex, &format.IsArc))) {
format.IsArc = nullptr;
}
if (maxSignatureSize < currentMaxSignatureSize) {
maxSignatureSize = currentMaxSignatureSize;
}
}
std::vector<int> formatPriorities(formats.size());
for (std::size_t formatIndex = 0; formatIndex < formats.size(); formatIndex++) {
const auto& format = formats[formatIndex];
formatPriorities[formatIndex] = gFormatPriorityMap.count(format.name) ? gFormatPriorityMap.at(format.name) : DefaultFormatPriority;
}
orderedFormatIndices.resize(formats.size());
std::iota(orderedFormatIndices.begin(), orderedFormatIndices.end(), 0);
std::sort(orderedFormatIndices.begin(), orderedFormatIndices.end(), [&formatPriorities](const auto& a, const auto& b) {
return formatPriorities[a] > formatPriorities[b];
});
}
const NanaZ::Format& NanaZ::FormatStore::GetFormat(std::size_t index) const {
return formats.at(index);
}
UInt32 NanaZ::FormatStore::IsArc(std::size_t index, const void* data, std::size_t size) const {
return formats.at(index).IsArc(reinterpret_cast<const Byte*>(data), size);
}
std::vector<std::size_t> NanaZ::FormatStore::FindFormatByExtension(const std::wstring& extension) const {
// TODO
return {};
}
std::vector<std::size_t> NanaZ::FormatStore::FindFormatByStream(winrt::com_ptr<IInStream> inStream) const {
constexpr std::size_t DefaultBufferSize = 1 << 20; // see CArc::OpenStream2
COMError::CheckHRESULT(inStream->Seek(0, STREAM_SEEK_SET, nullptr));
std::size_t bufferSize = std::max(DefaultBufferSize, maxSignatureSize);
auto buffer = std::make_unique<std::byte[]>(bufferSize);
UInt32 readSize = 0;
COMError::CheckHRESULT(inStream->Read(buffer.get(), static_cast<UInt32>(bufferSize), &readSize));
COMError::CheckHRESULT(inStream->Seek(0, STREAM_SEEK_SET, nullptr));
std::vector<std::size_t> formatIndices;
for (const auto& formatIndex : orderedFormatIndices) {
const auto& format = formats[formatIndex];
// check format by IsArc if provided
if (format.IsArc) {
const auto isArcResult = format.IsArc(reinterpret_cast<const Byte*>(buffer.get()), readSize);
if (isArcResult == k_IsArc_Res_NO || (isArcResult == k_IsArc_Res_NEED_MORE && readSize < bufferSize)) {
continue;
}
formatIndices.emplace_back(formatIndex);
continue;
}
// check format by signature
std::size_t signatureOffset = format.signatureOffset.value_or(0);
for (const auto& signature : format.signatures) {
if (signature.size() + signatureOffset > bufferSize) {
continue;
}
if (std::memcmp(signature.data(), buffer.get() + signatureOffset, signature.size()) == 0) {
formatIndices.emplace_back(formatIndex);
break;
}
}
}
return formatIndices;
}
NanaZ::NanaZ(LPCWSTR dllFilepath) :
nanaZDll(dllFilepath),
CreateObject(reinterpret_cast<Func_CreateObject>(nanaZDll.GetProc("CreateObject"))),
GetIsArc(reinterpret_cast<Func_GetIsArc>(nanaZDll.GetProc("GetIsArc"))),
GetNumberOfFormats(reinterpret_cast<Func_GetNumberOfFormats>(nanaZDll.GetProc("GetNumberOfFormats"))),
GetHandlerProperty2(reinterpret_cast<Func_GetHandlerProperty2>(nanaZDll.GetProc("GetHandlerProperty2"))),
formatStore(GetIsArc, GetNumberOfFormats, GetHandlerProperty2)
{}
const NanaZ::Format& NanaZ::GetFormat(std::size_t index) const {
return formatStore.GetFormat(index);
}
UInt32 NanaZ::IsArc(std::size_t index, const void* data, std::size_t size) const {
return formatStore.IsArc(index, data, size);
}
std::vector<std::size_t> NanaZ::FindFormatByExtension(const std::wstring& extension) const {
return formatStore.FindFormatByExtension(extension);
}
std::vector<std::size_t> NanaZ::FindFormatByStream(winrt::com_ptr<IInStream> inStream) const {
return formatStore.FindFormatByStream(inStream);
}