-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathextract_memory_worker.cpp
121 lines (107 loc) · 2.72 KB
/
extract_memory_worker.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
// CEZEO software Ltd. https://www.cezeo.com
#include "extract_memory_worker.h"
#include <fcntl.h>
#include "string_utils.h"
namespace cab
{
namespace
{
const char notUniqueKey[] = "not unique key in map";
}
extract_memory_worker::extract_memory_worker(const uint8_t* sourceData, size_t sourceSize) : cabData(sourceData), cabSize(sourceSize)
{
}
extract_memory_worker::~extract_memory_worker()
{
for (PEXTRACTMEMORYITEM item : sources)
{
delete item;
}
for (PEXTRACTMEMORYITEM item : files)
{
delete item;
}
}
FNOPEN(extract_memory_worker::fnFileOpen)
{
// here we open source cab file (mostry twice)
// pszFile is not used in this call because we know the source file name
// this is source cab file
PEXTRACTMEMORYITEM memoryItemPtr = new extract_memory_item(cabData, cabSize);
sources.push_back(memoryItemPtr);
// return pointer to file
return (INT_PTR)memoryItemPtr;
}
FNFDINOTIFY(extract_memory_worker::fnNotify)
{
INT_PTR result = 0;
PEXTRACTMEMORYITEM memoryItemPtr(nullptr);
switch (fdint)
{
case fdintCOPY_FILE:
{
// Append the destination directory to the file name.
// name in CP_UTF8 code page
std::wstring fileName(StringUtils::MbToWide((LPCSTR)pfdin->psz1, CP_UTF8));
memoryItemPtr = new extract_memory_item(fileName, pfdin->cb);
files.emplace_back(memoryItemPtr);
result = (INT_PTR)memoryItemPtr;
}
break;
case fdintCLOSE_FILE_INFO:
{
memoryItemPtr = (extract_memory_item*)pfdin->hf;
if (memoryItemPtr != nullptr)
{
result = !memoryItemPtr->fnFileClose(pfdin->hf);
}
}
break;
case fdintNEXT_CABINET:
{
if (pfdin->fdie != FDIERROR_NONE)
{
result = -1;
}
}
break;
case fdintPARTIAL_FILE:
{
result = 0;
}
break;
case fdintCABINET_INFO:
{
result = 0;
}
break;
case fdintENUMERATE:
{
result = 0;
}
break;
default:
{
result = -1;
}
break;
}
return result;
}
void extract_memory_worker::MoveFilesTo(std::vector<PEXTRACTMEMORYITEM>& moveToFiles)
{
moveToFiles = std::move(files);
}
void extract_memory_worker::MoveFilesTo(std::map<std::wstring, buffer>& moveToFiles)
{
std::pair<std::map<std::wstring, buffer>::iterator, bool> result;
for (PEXTRACTMEMORYITEM memItem : files)
{
result = moveToFiles.insert({memItem->GetFileName(), std::move(memItem->GetBuffer())});
if (!result.second)
{
throw std::logic_error(notUniqueKey);
}
}
}
}; // namespace cab