-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFsdDictOffsetLookup.cpp
More file actions
231 lines (189 loc) · 6.93 KB
/
Copy pathFsdDictOffsetLookup.cpp
File metadata and controls
231 lines (189 loc) · 6.93 KB
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
// Copyright © 2016 CCP ehf.
#include "StdAfx.h"
#include "FsdDictOffsetLookup.h"
#include "BinaryLoaderHelper.h"
#include "BinaryLoader.h"
template<typename T>
struct KeyedFooterOffset
{
T key;
uint32_t offset;
};
template<typename T>
struct KeyedFooterOffsetWithSize
{
T key;
uint32_t offset;
uint32_t size;
};
template<typename KEY_TYPE>
FastLoadingHashedOffsetLookup<KEY_TYPE>::FastLoadingHashedOffsetLookup(const char* footerData, const char* keyConverter, bool hasSize)
{
m_keyConverter = keyConverter;
uint32_t dictLength = BinaryLoaderHelper::GetUnsignedIntFromData(footerData, 0);
if (hasSize)
{
for (size_t i = 0; i < dictLength; i++)
{
// Need to do a c style cast
KeyedFooterOffsetWithSize<KEY_TYPE>* castKey = (KeyedFooterOffsetWithSize<KEY_TYPE>*)(&footerData[(i * sizeof(KeyedFooterOffsetWithSize<KEY_TYPE>) + 4)]);
FooterOffsetSize fos;
fos.offset = castKey->offset;
fos.size = castKey->size;
m_hash[castKey->key] = fos;
}
}
else
{
for (size_t i = 0; i < dictLength; i++)
{
KeyedFooterOffset<KEY_TYPE>* castKey = (KeyedFooterOffset<KEY_TYPE>*)(&footerData[(i * sizeof(KeyedFooterOffset<KEY_TYPE>) + 4)]);
FooterOffsetSize fos;
fos.offset = castKey->offset;
fos.size = -1;
m_hash[castKey->key] = fos;
}
}
}
template<typename KEY_TYPE>
FastLoadingHashedOffsetLookup<KEY_TYPE>::~FastLoadingHashedOffsetLookup()
{
}
template<typename KEY_TYPE>
OFFSET_LOOKUP_RESULT FastLoadingHashedOffsetLookup<KEY_TYPE>::GetOffsetAndSize(PyObject* key, FooterOffsetSize &fos)
{
KEY_TYPE convertedKey(PyLong_AsLong(key));
if (PyErr_Occurred() != nullptr)
{
return OFFSET_LOOKUP_ERROR_INVALID_KEY;
}
auto findResult = m_hash.find(convertedKey);
if (findResult == m_hash.cend()){
return OFFSET_LOOKUP_ERROR_KEY_NOT_PRESENT;
}
fos = findResult->second;
return OFFSET_LOOKUP_OK;
}
template<typename KEY_TYPE>
bool FastLoadingHashedOffsetLookup<KEY_TYPE>::Contains(PyObject* key)
{
KEY_TYPE convertedKey( PyLong_AsLong(key) );
// We go in here if the key isn't an int
if (convertedKey == -1 && PyErr_Occurred() != nullptr)
{
return false;
}
auto findResult = m_hash.find(convertedKey);
// We go in here if the key wasn't found in the hashmap
if (findResult == m_hash.cend()){
return false;
}
// Success! We found it
return true;
}
SemiFastStringLoadingHashedOffsetLookup::SemiFastStringLoadingHashedOffsetLookup(const char* footerData, const FsdSchemaAttributes &offsetTableSchema, bool hasSize)
{
ObjectSchemaAttributes keyOffsetObject = *offsetTableSchema.listAttributes->listItemSchema->objectAttributes.get();
auto offsetToKey = keyOffsetObject.endOfFixedSizedData + 12;
auto offsetToOffset = keyOffsetObject.constantAttributeOffsets["offset"];
uint32_t offsetToSize = 0;
if (hasSize){
offsetToSize = keyOffsetObject.constantAttributeOffsets["size"];
}
uint32_t dictLength = BinaryLoaderHelper::GetUnsignedIntFromData(footerData, 0);
std::string dataAsString(footerData);
uint32_t offsetToStartOfData = 4 + dictLength * 4;
for (size_t i = 0; i < dictLength; i++)
{
// footer data is a list that doesn't have a fixed size
// so firstly we need to find the offset to the object in the list
std::string key = BinaryLoader::CLoadStringFromBinaryString(footerData, offsetToStartOfData + offsetToKey, "");
uint32_t offset = BinaryLoader::CLoadUnsigned32BitIntFromBinaryString(footerData, offsetToStartOfData + offsetToOffset, "");
int32_t size = -1;
if (hasSize)
{
size = BinaryLoader::CLoadSigned32BitIntFromBinaryString(footerData, offsetToStartOfData + offsetToSize, "");
}
FooterOffsetSize fos;
fos.offset = offset;
fos.size = size;
m_hash[key] = fos;
offsetToStartOfData += offsetToKey + 4 + uint32_t(key.length());
}
}
SemiFastStringLoadingHashedOffsetLookup::~SemiFastStringLoadingHashedOffsetLookup()
{
}
OFFSET_LOOKUP_RESULT SemiFastStringLoadingHashedOffsetLookup::GetOffsetAndSize(PyObject* key, FooterOffsetSize &fos)
{
std::string convertedKey = PyUnicode_AsUTF8(key);
if (PyErr_Occurred() != nullptr)
{
return OFFSET_LOOKUP_ERROR_INVALID_KEY;
}
auto findResult = m_hash.find(convertedKey);
if (findResult == m_hash.cend()){
return OFFSET_LOOKUP_ERROR_KEY_NOT_PRESENT;
}
fos = findResult->second;
return OFFSET_LOOKUP_OK;
}
bool SemiFastStringLoadingHashedOffsetLookup::Contains(PyObject* key)
{
const char* keyAsCharArray = PyUnicode_AsUTF8(key);
// We go in here if the key isn't an int
if (keyAsCharArray == nullptr && PyErr_Occurred() != nullptr)
{
return false;
}
std::string convertedKey = std::string(keyAsCharArray);
auto findResult = m_hash.find(convertedKey);
// We go in here if the key wasn't found in the hashmap
if (findResult == m_hash.cend()){
return false;
}
// Success! We found it
return true;
}
SlowBinarySearchOffsetLookup::SlowBinarySearchOffsetLookup(const char* footerData, const FsdSchemaAttributes &s, bool hasSize)
{
m_list = CreateFsdList(footerData, 0, s, "offsetFooter");
}
SlowBinarySearchOffsetLookup::~SlowBinarySearchOffsetLookup()
{
delete m_list;
}
OFFSET_LOOKUP_RESULT SlowBinarySearchOffsetLookup::GetOffsetAndSize(PyObject* key, FooterOffsetSize &fos)
{
return OFFSET_LOOKUP_ERROR_INVALID_KEY;
}
bool SlowBinarySearchOffsetLookup::Contains(PyObject* key)
{
return false;
}
FsdDictOffsetLookup* CreateFsdDictOffsetLookup(const char* data, uint32_t offset, const FsdSchemaAttributes &schemaAttributes)
{
SchemaType dictKeySchemaType = schemaAttributes.dictAttributes->keySchema->schemaType;
// the size of the data is situated at the top of the dictionary
uint32_t totalDataSize = BinaryLoaderHelper::GetUnsignedIntFromData(data, offset);
// the footer size is situated at the end of the data
// (offset + size of the data header (4) + size of the data (m_totalDataSize) - the size of the footer (4))
uint32_t footerSize = BinaryLoaderHelper::GetUnsignedIntFromData(data, offset + 4 + totalDataSize - 4);
// The keys are held in a key footer that contains the keys and the offsets (and size if known) to the data
std::string footerData(&data[offset + totalDataSize - footerSize], footerSize);
auto offsetSizeObjectSchema = schemaAttributes.dictAttributes->keyFooterSchema->listAttributes->listItemSchema;
auto objectAttributeNames = offsetSizeObjectSchema->objectAttributes->attributes;
bool footerOffsetHasSize = objectAttributeNames.find("size") != objectAttributeNames.end();
switch (dictKeySchemaType)
{
case UNSIGNED_32_BIT_INT_SCHEMA_TYPE:
return new FastLoadingHashedOffsetLookup<uint32_t>(footerData.c_str(), "I", footerOffsetHasSize);
case SIGNED_32_BIT_INT_SCHEMA_TYPE:
return new FastLoadingHashedOffsetLookup<int32_t>(footerData.c_str(), "i", footerOffsetHasSize);
case STRING_SCHEMA_TYPE:
return new SemiFastStringLoadingHashedOffsetLookup(footerData.c_str(), *schemaAttributes.dictAttributes->keyFooterSchema, footerOffsetHasSize);
default:
return new SlowBinarySearchOffsetLookup(footerData.c_str(), *schemaAttributes.dictAttributes->keyFooterSchema, footerOffsetHasSize);
};
return nullptr;
}