-
Notifications
You must be signed in to change notification settings - Fork 21
/
fileloader.cpp
542 lines (454 loc) · 10.2 KB
/
fileloader.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
//////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
#include "otpch.h"
#include "fileloader.h"
FileLoader::FileLoader()
{
m_file = NULL;
m_root = NULL;
m_buffer = new uint8_t[1024];
m_buffer_size = 1024;
m_lastError = ERROR_NONE;
//cache
m_use_cache = false;
m_cache_size = 0;
m_cache_index = NO_VALID_CACHE;
m_cache_offset = NO_VALID_CACHE;
memset(m_cached_data, 0, sizeof(m_cached_data));
}
FileLoader::~FileLoader()
{
if(m_file)
{
fclose(m_file);
m_file = NULL;
}
NodeStruct::clearNet(m_root);
delete[] m_buffer;
for(int32_t i = 0; i < CACHE_BLOCKS; i++)
delete[] m_cached_data[i].data;
}
bool FileLoader::openFile(const char* filename, const char* accept_identifier, bool write, bool caching /*= false*/)
{
if(write)
{
m_file = fopen(filename, "wb");
if(!m_file)
{
m_lastError = ERROR_CAN_NOT_CREATE;
return false;
}
uint32_t version = 0;
writeData(&version, sizeof(version), false);
return true;
}
m_file = fopen(filename, "rb");
if(!m_file)
{
m_lastError = ERROR_CAN_NOT_OPEN;
return false;
}
char identifier[4];
if(fread(identifier, 1, 4, m_file) < 4)
{
fclose(m_file);
m_file = NULL;
m_lastError = ERROR_EOF;
return false;
}
// The first four bytes must either match the accept identifier or be 0x00000000 (wildcard)
if(memcmp(identifier, accept_identifier, 4) != 0 && memcmp(identifier, "\0\0\0\0", 4) != 0)
{
fclose(m_file);
m_file = NULL;
m_lastError = ERROR_INVALID_FILE_VERSION;
return false;
}
if(caching)
{
m_use_cache = true;
fseek(m_file, 0, SEEK_END);
int32_t file_size = ftell(m_file);
m_cache_size = std::min(32768, std::max(file_size/20, 8192)) & ~0x1FFF;
}
if(!safeSeek(4))
{
m_lastError = ERROR_INVALID_FORMAT;
return false;
}
delete m_root;
m_root = new NodeStruct();
m_root->start = 4;
int32_t byte;
if(safeSeek(4) && readByte(byte) && byte == NODE_START)
return parseNode(m_root);
return false;
}
bool FileLoader::parseNode(NODE node)
{
int32_t byte, pos;
NODE currentNode = node;
while(true)
{
//read node type
if(!readByte(byte))
return false;
currentNode->type = byte;
bool setPropsSize = false;
while(true)
{
if(!readByte(byte))
return false;
bool skipNode = false;
switch(byte)
{
case NODE_START:
{
//child node start
if(!safeTell(pos))
return false;
NODE childNode = new NodeStruct();
childNode->start = pos;
currentNode->propsSize = pos - currentNode->start - 2;
currentNode->child = childNode;
setPropsSize = true;
if(!parseNode(childNode))
return false;
break;
}
case NODE_END:
{
//current node end
if(!setPropsSize)
{
if(!safeTell(pos))
return false;
currentNode->propsSize = pos - currentNode->start - 2;
}
if(!readByte(byte))
return true;
switch(byte)
{
case NODE_START:
{
//starts next node
if(!safeTell(pos))
return false;
skipNode = true;
NODE nextNode = new NodeStruct();
nextNode->start = pos;
currentNode->next = nextNode;
currentNode = nextNode;
break;
}
case NODE_END:
return safeTell(pos) && safeSeek(pos);
default:
m_lastError = ERROR_INVALID_FORMAT;
return false;
}
break;
}
case ESCAPE_CHAR:
{
if(!readByte(byte))
return false;
break;
}
default:
break;
}
if(skipNode)
break;
}
}
return false;
}
const uint8_t* FileLoader::getProps(const NODE node, uint32_t &size)
{
if(node)
{
if(node->propsSize >= m_buffer_size)
{
delete[] m_buffer;
while(node->propsSize >= m_buffer_size)
m_buffer_size <<= 1;
m_buffer = new uint8_t[m_buffer_size];
}
//get buffer
if(readBytes(m_buffer, node->propsSize, node->start + 2))
{
//unscape buffer
uint32_t j = 0;
bool escaped = false;
for(uint32_t i = 0; i < node->propsSize; ++i, ++j)
{
if(m_buffer[i] == ESCAPE_CHAR)
{
//escape char found, skip it and write next
++i;
m_buffer[j] = m_buffer[i];
//is neede a displacement for next bytes
escaped = true;
}
else if(escaped)
{
//perform that displacement
m_buffer[j] = m_buffer[i];
}
}
size = j;
return m_buffer;
}
}
return NULL;
}
bool FileLoader::getProps(const NODE node, PropStream &props)
{
uint32_t size;
if(const uint8_t* a = getProps(node, size))
{
props.init((char*)a, size);
return true;
}
props.init(NULL, 0);
return false;
}
int32_t FileLoader::setProps(void* data, uint16_t size)
{
//data
if(!writeData(data, size, true))
return getError();
return ERROR_NONE;
}
void FileLoader::startNode(uint8_t type)
{
uint8_t nodeBegin = NODE_START;
writeData(&nodeBegin, sizeof(nodeBegin), false);
writeData(&type, sizeof(type), true);
}
void FileLoader::endNode()
{
uint8_t nodeEnd = NODE_END;
writeData(&nodeEnd, sizeof(nodeEnd), false);
}
NODE FileLoader::getChildNode(const NODE parent, uint32_t &type)
{
if(parent)
{
NODE child = parent->child;
if(child)
type = child->type;
return child;
}
type = m_root->type;
return m_root;
}
NODE FileLoader::getNextNode(const NODE prev, uint32_t &type)
{
if(prev)
{
NODE next = prev->next;
if(next)
type = next->type;
return next;
}
return NO_NODE;
}
inline bool FileLoader::readByte(int32_t &value)
{
if(m_use_cache)
{
if(m_cache_index == NO_VALID_CACHE)
{
m_lastError = ERROR_CACHE_ERROR;
return false;
}
if(m_cache_offset >= m_cached_data[m_cache_index].size)
{
int32_t pos = m_cache_offset + m_cached_data[m_cache_index].base;
int32_t tmp = getCacheBlock(pos);
if(tmp < 0)
return false;
m_cache_index = tmp;
m_cache_offset = pos - m_cached_data[m_cache_index].base;
if(m_cache_offset >= m_cached_data[m_cache_index].size)
return false;
}
value = m_cached_data[m_cache_index].data[m_cache_offset];
m_cache_offset++;
return true;
}
value = fgetc(m_file);
if(value != EOF)
return true;
m_lastError = ERROR_EOF;
return false;
}
inline bool FileLoader::readBytes(uint8_t* buffer, uint32_t size, int32_t pos)
{
if(m_use_cache)
{
//seek at pos
uint32_t reading, remain = size, bufferPos = 0;
do
{
//prepare cache
uint32_t i = getCacheBlock(pos);
if(i == NO_VALID_CACHE)
return false;
m_cache_index = i;
m_cache_offset = pos - m_cached_data[i].base;
//get maximum read block size and calculate remaining bytes
reading = std::min(remain, m_cached_data[i].size - m_cache_offset);
remain = remain - reading;
//read it
memcpy(buffer + bufferPos, m_cached_data[m_cache_index].data + m_cache_offset, reading);
//update variables
m_cache_offset = m_cache_offset + reading;
bufferPos = bufferPos + reading;
pos = pos + reading;
}
while(remain > 0);
return true;
}
if(fseek(m_file, pos, SEEK_SET))
{
m_lastError = ERROR_SEEK_ERROR;
return false;
}
if(fread(buffer, 1, size, m_file) == (uint32_t)size)
return true;
m_lastError = ERROR_EOF;
return false;
}
inline bool FileLoader::checks(const NODE node)
{
if(!m_file)
{
m_lastError = ERROR_NOT_OPEN;
return false;
}
if(!node)
{
m_lastError = ERROR_INVALID_NODE;
return false;
}
return true;
}
inline bool FileLoader::safeSeek(uint32_t pos)
{
if(m_use_cache)
{
uint32_t i = getCacheBlock(pos);
if(i == NO_VALID_CACHE)
return false;
m_cache_index = i;
m_cache_offset = pos - m_cached_data[i].base;
}
else if(fseek(m_file, pos, SEEK_SET))
{
m_lastError = ERROR_SEEK_ERROR;
return false;
}
return true;
}
inline bool FileLoader::safeTell(int32_t &pos)
{
if(m_use_cache)
{
if(m_cache_index == NO_VALID_CACHE)
{
m_lastError = ERROR_CACHE_ERROR;
return false;
}
pos = m_cached_data[m_cache_index].base + m_cache_offset - 1;
return true;
}
pos = ftell(m_file);
if(pos == -1)
{
m_lastError = ERROR_TELL_ERROR;
return false;
}
pos = pos - 1;
return true;
}
inline uint32_t FileLoader::getCacheBlock(uint32_t pos)
{
bool found = false;
uint32_t i, base_pos = pos & ~(m_cache_size - 1);
for(i = 0; i < CACHE_BLOCKS; i++)
{
if(m_cached_data[i].loaded)
{
if(m_cached_data[i].base == base_pos)
{
found = true;
break;
}
}
}
if(!found)
i = loadCacheBlock(pos);
return i;
}
int32_t FileLoader::loadCacheBlock(uint32_t pos)
{
int32_t i, loading_cache = -1, base_pos = pos & ~(m_cache_size - 1);
for(i = 0; i < CACHE_BLOCKS; i++)
{
if(!m_cached_data[i].loaded)
{
loading_cache = i;
break;
}
}
if(loading_cache == -1)
{
for(i = 0; i < CACHE_BLOCKS; i++)
{
if((long)(abs((long)m_cached_data[i].base - base_pos)) > (long)(2 * m_cache_size))
{
loading_cache = i;
break;
}
}
if(loading_cache == -1)
loading_cache = 0;
}
if(m_cached_data[loading_cache].data == NULL)
m_cached_data[loading_cache].data = new uint8_t[m_cache_size];
m_cached_data[loading_cache].base = base_pos;
if(fseek(m_file, m_cached_data[loading_cache].base, SEEK_SET))
{
m_lastError = ERROR_SEEK_ERROR;
return -1;
}
uint32_t size = fread(m_cached_data[loading_cache].data, 1, m_cache_size, m_file);
m_cached_data[loading_cache].size = size;
if(size < (pos - m_cached_data[loading_cache].base))
{
m_lastError = ERROR_SEEK_ERROR;
return -1;
}
m_cached_data[loading_cache].loaded = 1;
return loading_cache;
}