-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextractor.cpp
195 lines (177 loc) · 5.03 KB
/
extractor.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
// CEZEO software Ltd. https://www.cezeo.com
#include "extractor.h"
#include "extract_worker.h"
#include "extract_file_worker.h"
#include "extract_memory_worker.h"
#include "extract_item.h"
#include <fcntl.h>
#include "string_utils.h"
#include "resource_buffer.h"
namespace cab
{
namespace
{
// FDI callbacks
// will not override
FNALLOC(fnMemAlloc)
{
return HeapAlloc(GetProcessHeap(), NULL, cb);
}
// will not override
FNFREE(fnMemFree)
{
HeapFree(GetProcessHeap(), NULL, pv);
}
// worker handled
FNOPEN(fnFileOpen)
{
// here we have CabWorker object pointer in the pszFile
if (pszFile != nullptr)
{
std::string hexPointer(pszFile);
if (hexPointer.size() == StringUtils::ptrInCharsSize)
{
hexPointer = hexPointer.substr(0, StringUtils::ptrInCharsSize);
PEXTRACTWORKER cabWorkerPtr = reinterpret_cast<PEXTRACTWORKER>(StringUtils::HexToPtr(hexPointer));
if (cabWorkerPtr != nullptr)
{
return cabWorkerPtr->fnFileOpen(pszFile, oflag, pmode);
}
}
}
return 0;
}
// item handled
FNREAD(fnFileRead)
{
PEXTRACTITEM cabItemPtr((PEXTRACTITEM)hf);
return cabItemPtr->fnFileRead(hf, pv, cb);
}
// item handled
FNWRITE(fnFileWrite)
{
PEXTRACTITEM cabItemPtr((PEXTRACTITEM)hf);
return cabItemPtr->fnFileWrite(hf, pv, cb);
}
// item handled
FNSEEK(fnFileSeek)
{
PEXTRACTITEM cabItemPtr((PEXTRACTITEM)hf);
return cabItemPtr->fnFileSeek(hf, dist, seektype);
}
// item handled
FNCLOSE(fnFileClose)
{
PEXTRACTITEM cabItemPtr((PEXTRACTITEM)hf);
return cabItemPtr->fnFileClose(hf);
}
// worker handled
FNFDINOTIFY(fnNotify)
{
PEXTRACTWORKER cabWorkerPtr((PEXTRACTWORKER)pfdin->pv);
return cabWorkerPtr->fnNotify(fdint, pfdin);
}
} // namespace
extractor::extractor()
{
memset(&extractErrors, 0, sizeof(extractErrors));
#ifdef CAB_STATIC_LINK
// use fci.lib
pfnFDICreate = FDICreate;
pfnFDICopy = FDICopy;
pfnFDIDestroy = FDIDestroy;
#else
// dynamic link to cabinet.dll
cabinetDll.load("cabinet.dll");
if (nullptr != cabinetDll)
{
pfnFDICreate = (fnFDICreate)GetProcAddress(cabinetDll, "FDICreate");
pfnFDICopy = (fnFDICopy)GetProcAddress(cabinetDll, "FDICopy");
pfnFDIDestroy = (fnFDIDestroy)GetProcAddress(cabinetDll, "FDIDestroy");
}
else
{
throw(std::runtime_error("cabinet.dll can not be loaded"));
}
#endif
if (nullptr == pfnFDICreate || nullptr == pfnFDICopy || nullptr == pfnFDIDestroy)
{
throw(std::runtime_error("fdi funciton are not found"));
}
// create FDI context
create_context();
}
extractor::~extractor()
{
destroy_context();
}
void extractor::create_context()
{
// create FDI context
extractHandler = pfnFDICreate(fnMemAlloc, fnMemFree, fnFileOpen, fnFileRead, fnFileWrite, fnFileClose, fnFileSeek, cpu80386, &extractErrors);
if (nullptr == extractHandler)
{
throw(std::runtime_error("can't create fdi context"));
}
}
void extractor::destroy_context()
{
if (nullptr != extractHandler)
{
pfnFDIDestroy(extractHandler);
extractHandler = nullptr;
}
}
bool extractor::cab_to_files(const std::wstring& cabPath, const std::wstring& outputDir)
{
bool result = false;
try
{
if (CreateDirectory(outputDir.c_str(), NULL) || GetLastError() == ERROR_ALREADY_EXISTS)
{
extract_file_worker fileWorker(cabPath, outputDir);
std::string ptrs(StringUtils::PtrToHex((INT_PTR)(&fileWorker)));
result = pfnFDICopy(extractHandler, &ptrs[ 0 ], &ptrs[ 0 ], 0, fnNotify, NULL, (void*)(&fileWorker)) == TRUE;
}
}
catch (...)
{
// do nothing
}
return result;
}
bool extractor::memory_to_memory(uint8_t* sourceData, size_t sourceSize, std::map<std::wstring, buffer>& output)
{
bool result = false;
try
{
extract_memory_worker memoryWorker(sourceData, sourceSize);
std::string ptrs(StringUtils::PtrToHex((INT_PTR)(&memoryWorker)));
result = pfnFDICopy(extractHandler, &ptrs[ 0 ], &ptrs[ 0 ], 0, fnNotify, NULL, (void*)(&memoryWorker)) == TRUE;
memoryWorker.MoveFilesTo(output);
}
catch (...)
{
// do nothing
}
return result;
}
bool extractor::resources_to_memory(const UINT cabResourceId, std::map<std::wstring, buffer>& output)
{
bool result = false;
try
{
// assume binary resoruce type and current module instance
resource_buffer resource(cabResourceId);
extract_memory_worker memoryWorker(resource.data(), resource.size());
std::string ptrs(StringUtils::PtrToHex((INT_PTR)(&memoryWorker)));
result = pfnFDICopy(extractHandler, &ptrs[ 0 ], &ptrs[ 0 ], 0, fnNotify, NULL, (void*)(&memoryWorker)) == TRUE;
memoryWorker.MoveFilesTo(output);
}
catch (...)
{
// do nothing
}
return result;
}
}; // namespace cab