-
Notifications
You must be signed in to change notification settings - Fork 51
/
reader_lcf.cpp
322 lines (279 loc) · 6.36 KB
/
reader_lcf.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
/*
* This file is part of liblcf. Copyright (c) 2017 liblcf authors.
* https://github.com/EasyRPG/liblcf - https://easyrpg.org
*
* liblcf is Free/Libre Open Source Software, released under the MIT License.
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/
#include <cstdarg>
#include <istream>
#include "reader_lcf.h"
#ifdef NDEBUG
# include <stdio.h>
#endif
// Statics
std::string LcfReader::error_str;
LcfReader::LcfReader(std::istream& filestream, std::string encoding) :
encoding(encoding),
stream(filestream)
{
}
LcfReader::~LcfReader() {
}
size_t LcfReader::Read0(void *ptr, size_t size, size_t nmemb) {
if (size == 0) { //avoid division by 0
return 0;
}
//Read nmemb elements of size and return the number of read elements
stream.read(reinterpret_cast<char*>(ptr), size*nmemb);
size_t result = stream.gcount() / size;
#ifdef NDEBUG
if (result != nmemb && !Eof()) {
perror("Reading error: ");
}
#endif
return result;
}
void LcfReader::Read(void *ptr, size_t size, size_t nmemb) {
#ifdef NDEBUG
Read0(ptr, size, nmemb);
#else
if (Read0(ptr, size, nmemb) != nmemb) {
fprintf(stderr, "Read error at %d. The file is probably corrupted\n", Tell());
}
#endif
}
template <>
void LcfReader::Read<bool>(bool& ref) {
ref = ReadInt() > 0;
}
template <>
void LcfReader::Read<uint8_t>(uint8_t& ref) {
Read(&ref, 1, 1);
}
template <>
void LcfReader::Read<int16_t>(int16_t& ref) {
Read(&ref, 2, 1);
SwapByteOrder(ref);
}
template <>
void LcfReader::Read<uint32_t>(uint32_t& ref) {
Read(&ref, 4, 1);
SwapByteOrder(ref);
}
int LcfReader::ReadInt() {
int value = 0;
unsigned char temp = 0;
int loops = 0;
do {
value <<= 7;
if (Read0(&temp, 1, 1) == 0) {
assert(value == 0);
return 0;
}
value |= temp & 0x7F;
if (loops > 5) {
fprintf(stderr, "Invalid compressed integer at %d\n", Tell());
}
++loops;
} while (temp & 0x80);
return loops > 5 ? 0 : value;
}
template <>
void LcfReader::Read<int32_t>(int32_t& ref) {
ref = ReadInt();
}
template <>
void LcfReader::Read<double>(double& ref) {
Read(&ref, 8, 1);
SwapByteOrder(ref);
}
template <>
void LcfReader::Read<bool>(std::vector<bool> &buffer, size_t size) {
buffer.clear();
for (unsigned i = 0; i < size; ++i) {
uint8_t val;
Read(&val, 1, 1);
buffer.push_back(val > 0);
}
}
template <>
void LcfReader::Read<uint8_t>(std::vector<uint8_t> &buffer, size_t size) {
buffer.clear();
for (unsigned int i = 0; i < size; ++i) {
uint8_t val;
Read(&val, 1, 1);
buffer.push_back(val);
}
}
template <>
void LcfReader::Read<int16_t>(std::vector<int16_t> &buffer, size_t size) {
buffer.clear();
size_t items = size / 2;
for (unsigned int i = 0; i < items; ++i) {
int16_t val;
Read(&val, 2, 1);
SwapByteOrder(val);
buffer.push_back(val);
}
if (size % 2 != 0) {
Seek(1, FromCurrent);
buffer.push_back(0);
}
}
template <>
void LcfReader::Read<int32_t>(std::vector<int32_t> &buffer, size_t size) {
buffer.clear();
size_t items = size / 4;
for (unsigned int i = 0; i < items; ++i) {
int32_t val;
Read(&val, 4, 1);
SwapByteOrder(val);
buffer.push_back(val);
}
if (size % 4 != 0) {
Seek(size % 4, FromCurrent);
buffer.push_back(0);
}
}
template <>
void LcfReader::Read<uint32_t>(std::vector<uint32_t> &buffer, size_t size) {
buffer.clear();
size_t items = size / 4;
for (unsigned int i = 0; i < items; ++i) {
uint32_t val;
Read(&val, 4, 1);
SwapByteOrder(val);
buffer.push_back(val);
}
if (size % 4 != 0) {
Seek(size % 4, FromCurrent);
buffer.push_back(0);
}
}
void LcfReader::ReadString(std::string& ref, size_t size) {
char* chars = new char[size];
Read(chars, 1, size);
ref = Encode(std::string(chars, size));
delete[] chars;
}
bool LcfReader::IsOk() const {
return (stream.good());
}
bool LcfReader::Eof() const {
return stream.eof();
}
void LcfReader::Seek(size_t pos, SeekMode mode) {
switch (mode) {
case LcfReader::FromStart:
stream.seekg(pos, std::ios_base::beg);
break;
case LcfReader::FromCurrent:
stream.seekg(pos, std::ios_base::cur);
break;
case LcfReader::FromEnd:
stream.seekg(pos, std::ios_base::end);
break;
default:
assert(false && "Invalid SeekMode");
}
}
uint32_t LcfReader::Tell() {
return (uint32_t)stream.tellg();
}
int LcfReader::Peek() {
return stream.peek();
}
#ifdef _DEBUG
void LcfReader::SkipDebug(const struct LcfReader::Chunk& chunk_info, const char* srcfile) {
// Dump the Chunk Data in Debug Mode
#ifdef _WIN32
const char* srcfilename = strrchr(srcfile, '\\');
#else
const char* srcfilename = strrchr(srcfile, '/');
#endif
if (srcfilename == NULL) {
srcfilename = srcfile;
} else {
srcfilename++;
}
fprintf(stderr, "Skipped Chunk %02X (%d byte) in lcf at %X (%s)\n",
chunk_info.ID, chunk_info.length, Tell(),
srcfilename);
for (uint32_t i = 0; i < chunk_info.length; ++i) {
uint8_t byte;
LcfReader::Read(byte);
fprintf(stderr, "%02X ", byte);
if ((i+1) % 16 == 0) {
fprintf(stderr, "\n");
}
if (Eof()) {
break;
}
}
fprintf(stderr, "\n");
}
#else
void LcfReader::Skip(const struct LcfReader::Chunk& chunk_info) {
Seek((size_t)chunk_info.length, FromCurrent);
}
#endif
void LcfReader::SetError(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
char str[256];
vsprintf(str, fmt, args);
error_str = str;
//Output::ErrorStr((std::string)str);
va_end(args);
}
const std::string& LcfReader::GetError() {
return error_str;
}
std::string LcfReader::Encode(const std::string& str_to_encode) {
return ReaderUtil::Recode(str_to_encode, encoding, "UTF-8");
}
int LcfReader::IntSize(unsigned int x) {
int result = 0;
do {
x >>= 7;
result++;
} while (x != 0);
return result;
}
#ifdef WORDS_BIGENDIAN
void LcfReader::SwapByteOrder(uint16_t& us)
{
us = (us >> 8) |
(us << 8);
}
void LcfReader::SwapByteOrder(uint32_t& ui)
{
ui = (ui >> 24) |
((ui<<8) & 0x00FF0000) |
((ui>>8) & 0x0000FF00) |
(ui << 24);
}
void LcfReader::SwapByteOrder(double& d)
{
uint32_t *p = reinterpret_cast<uint32_t *>(&d);
SwapByteOrder(p[0]);
SwapByteOrder(p[1]);
uint32_t tmp = p[0];
p[0] = p[1];
p[1] = tmp;
}
#else
void LcfReader::SwapByteOrder(uint16_t& /* us */) {}
void LcfReader::SwapByteOrder(uint32_t& /* ui */) {}
void LcfReader::SwapByteOrder(double& /* d */) {}
#endif
void LcfReader::SwapByteOrder(int16_t& s)
{
SwapByteOrder((uint16_t&) s);
}
void LcfReader::SwapByteOrder(int32_t& s)
{
SwapByteOrder((uint32_t&) s);
}