This repository was archived by the owner on Dec 10, 2022. It is now read-only.
forked from ataulien/ZenLib
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathparserImplBinSafe.cpp
285 lines (242 loc) · 8.05 KB
/
parserImplBinSafe.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "parserImplBinSafe.h"
#include <utils/logger.h>
using namespace ZenLoad;
ZenLoad::ParserImplBinSafe::ParserImplBinSafe(ZenParser* parser)
: ParserImpl(parser)
{
}
/**
* @brief Read the start of a chunk. [...]
*/
bool ParserImplBinSafe::readChunkStart(ZenParser::ChunkHeader& header)
{
size_t seek = m_pParser->getSeek();
EZenValueType type;
size_t size;
// Check if this actually will be a string
readTypeAndSizeBinSafe(type, size);
m_pParser->setSeek(seek);
if(type != ZVT_STRING)
return false; // Next property isn't a string or the end
// Read chunk-header
char vobDescStk[256] = {};
std::string vobDescHeap;
const char* vobDesc = vobDescStk;
if(!readString(vobDescStk,sizeof(vobDescStk))) {
vobDescHeap = readString();
vobDesc = vobDescHeap.c_str();
}
if(parseHeader(header,vobDesc,std::strlen(vobDesc)))
return true;
m_pParser->setSeek(seek);
return false;
}
/**
* @brief Read the end of a chunk. []
*/
bool ParserImplBinSafe::readChunkEnd() {
size_t seek = m_pParser->getSeek();
EZenValueType type;
size_t size;
// Check if this actually will be a string
readTypeAndSizeBinSafe(type, size);
m_pParser->setSeek(seek);
if(type != ZVT_STRING)
return false; // Next property isn't a string or the end
char l[3] = {};
if(!readString(l,3) || l[0]!='[' || l[1]!=']') {
m_pParser->setSeek(seek); // Next property isn't a string or the end
return false;
}
return true;
}
/**
* @brief Read the implementation specific header and stores it in the parsers main-header struct
*/
void ParserImplBinSafe::readImplHeader()
{
// Bin-Safe archiver has its own version stored again for some reason
m_pParser->m_Header.binSafeHeader.bsVersion = m_pParser->readBinaryDWord();
// Read object count
m_pParser->m_Header.objectCount = m_pParser->readBinaryDWord();
// Read offset of where to find the global hash-table of all objects
m_pParser->m_Header.binSafeHeader.bsHashTableOffset = m_pParser->readBinaryDWord();
// Read hashtable
size_t s = m_pParser->m_Seek;
m_pParser->m_Seek = m_pParser->m_Header.binSafeHeader.bsHashTableOffset;
struct BinSafeEntry
{
std::string key;
uint16_t insertionIndex;
};
std::unordered_map<uint32_t, std::vector<BinSafeEntry>> map;
uint32_t htSize = m_pParser->readBinaryDWord();
for (uint32_t i = 0; i < htSize; i++)
{
uint16_t keyLen = m_pParser->readBinaryWord();
uint16_t insIdx = m_pParser->readBinaryWord();
uint32_t hashValue = m_pParser->readBinaryDWord();
// Read the keys data from the archive
std::vector<uint8_t> keyData(keyLen);
m_pParser->readBinaryRaw(keyData.data(), keyLen);
// Put the source key together with the insertion index into the map
//std::string key(keyData.begin(), keyData.end());
//map[hashValue].push_back( {key, insIdx} );
}
// Restore old position
m_pParser->m_Seek = s;
}
/**
* @brief Reads a string
*/
std::string ParserImplBinSafe::readString()
{
EZenValueType type;
size_t size;
// These values start with a small header
readTypeAndSizeBinSafe(type, size);
if (type != ZVT_STRING)
throw std::runtime_error("Expected string-type");
std::string str;
str.resize(size);
m_pParser->readBinaryRaw(&str[0], size);
// Skip potential hash-value at the end of the string
EZenValueType t = static_cast<EZenValueType>(m_pParser->readBinaryByte());
if (t != ZVT_HASH)
m_pParser->m_Seek -= sizeof(uint8_t);
else
m_pParser->m_Seek += sizeof(uint32_t);
return str;
}
bool ParserImplBinSafe::readString(char* buf, size_t bufSize)
{
EZenValueType type;
size_t size;
// These values start with a small header
readTypeAndSizeBinSafe(type, size);
if(type!=ZVT_STRING)
throw std::runtime_error("Expected string-type");
if(size>=bufSize)
return false;
m_pParser->readBinaryRaw(buf, size);
buf[size] = '\0';
// Skip potential hash-value at the end of the string
EZenValueType t = static_cast<EZenValueType>(m_pParser->readBinaryByte());
if (t != ZVT_HASH)
m_pParser->m_Seek -= sizeof(uint8_t);
else
m_pParser->m_Seek += sizeof(uint32_t);
return true;
}
/**
* @brief reads the small header in front of datatypes
*/
void ParserImplBinSafe::readTypeAndSizeBinSafe(EZenValueType& type, size_t& size)
{
type = static_cast<EZenValueType>(m_pParser->readBinaryByte());
if(type==ZVT_RAW || type==ZVT_RAW_FLOAT || type==ZVT_STRING)
size = m_pParser->readBinaryWord(); else
size = valueTypeSize(type);
}
/**
* @brief Reads data of the expected type. Throws if the read type is not the same as specified and not 0
*/
void ParserImplBinSafe::readEntryImpl(const char *expectedName, void* target, size_t targetSize, EZenValueType expectedType)
{
EZenValueType realType;
size_t size;
// Read type and size of the entry
readTypeAndSizeBinSafe(realType, size);
if(realType==ZVT_ENUM) {
switch(targetSize) {
case 1:
realType = ZVT_BYTE;
size = 1;
break;
case 2:
realType = ZVT_WORD;
size = 2;
break;
case 4:
realType = ZVT_INT;
size = 4;
break;
}
}
if(expectedType!=realType && realType!=ZVT_0)
throw std::runtime_error(std::string("Valuetype name does not match expected type. Value:") + expectedName);
if(expectedType!=ZVT_RAW && expectedType!=ZVT_RAW_FLOAT && expectedType!=ZVT_STRING && realType!=ZVT_0) {
if(size!=targetSize)
throw std::runtime_error(std::string("Valuetype size does not match expected size. Value:") + expectedName);
}
switch(realType) {
case ZVT_0:
break;
case ZVT_STRING: {
std::string str;
str.resize(size);
m_pParser->readBinaryRaw(&str[0], size);
*reinterpret_cast<std::string*>(target) = str;
break;
}
case ZVT_HASH:
case ZVT_INT:
*reinterpret_cast<uint32_t*>(target) = m_pParser->readBinaryDWord();
break;
case ZVT_FLOAT:
*reinterpret_cast<float*>(target) = m_pParser->readBinaryFloat();
break;
case ZVT_BYTE:
*reinterpret_cast<uint8_t*>(target) = m_pParser->readBinaryByte();
break;
case ZVT_WORD:
*reinterpret_cast<int16_t*>(target) = m_pParser->readBinaryWord();
break;
case ZVT_BOOL:
*reinterpret_cast<bool*>(target) = m_pParser->readBinaryDWord() != 0;
break;
case ZVT_VEC3:
reinterpret_cast<ZMath::float3*>(target)->x = m_pParser->readBinaryFloat();
reinterpret_cast<ZMath::float3*>(target)->y = m_pParser->readBinaryFloat();
reinterpret_cast<ZMath::float3*>(target)->z = m_pParser->readBinaryFloat();
break;
case ZVT_COLOR:
reinterpret_cast<uint8_t*>(target)[2] = m_pParser->readBinaryByte(); // FIXME: These are may ordered wrong
reinterpret_cast<uint8_t*>(target)[1] = m_pParser->readBinaryByte();
reinterpret_cast<uint8_t*>(target)[0] = m_pParser->readBinaryByte();
reinterpret_cast<uint8_t*>(target)[3] = m_pParser->readBinaryByte();
break;
case ZVT_RAW_FLOAT:
case ZVT_RAW:
m_pParser->readBinaryRaw(target, size);
break;
case ZVT_10:
break;
case ZVT_11:
break;
case ZVT_12:
break;
case ZVT_13:
break;
case ZVT_14:
break;
case ZVT_15:
break;
case ZVT_ENUM:
*reinterpret_cast<uint8_t*>(target) = m_pParser->readBinaryDWord();
break;
}
// Skip potential hash-value at the end of the entry
EZenValueType t = static_cast<EZenValueType>(m_pParser->readBinaryByte());
if(t != ZVT_HASH)
m_pParser->m_Seek -= sizeof(uint8_t);
else
m_pParser->m_Seek += sizeof(uint32_t);
}
/**
* @brief Reads the type of a single entry
*/
void ParserImplBinSafe::readEntryType(EZenValueType& outtype, size_t& size)
{
readTypeAndSizeBinSafe(outtype, size);
}