|
@@ -8,6 +8,12 @@
|
8
|
8
|
#include "global.h"
|
9
|
9
|
#include "utils/binary.h"
|
10
|
10
|
|
|
11
|
+BinaryFile::BinaryFile(const char* f) {
|
|
12
|
+ int r = open(f);
|
|
13
|
+ if (r != 0)
|
|
14
|
+ throw r;
|
|
15
|
+}
|
|
16
|
+
|
11
|
17
|
BinaryFile::~BinaryFile() {
|
12
|
18
|
if (file.is_open())
|
13
|
19
|
file.close();
|
|
@@ -17,6 +23,8 @@ int BinaryFile::open(const char* f) {
|
17
|
23
|
if (file.is_open()) {
|
18
|
24
|
return 1;
|
19
|
25
|
} else {
|
|
26
|
+ if (f == nullptr)
|
|
27
|
+ return 0;
|
20
|
28
|
file.open(f, std::ios_base::in | std::ios_base::binary);
|
21
|
29
|
return (file ? 0 : 1);
|
22
|
30
|
}
|
|
@@ -139,3 +147,119 @@ int64_t BinaryFile::read64() {
|
139
|
147
|
return ret;
|
140
|
148
|
}
|
141
|
149
|
|
|
150
|
+// ----------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+BinaryMemory::BinaryMemory(char* d) : data(nullptr), offset(0) {
|
|
153
|
+ int r = open(d);
|
|
154
|
+ if (r != 0)
|
|
155
|
+ throw r;
|
|
156
|
+}
|
|
157
|
+
|
|
158
|
+int BinaryMemory::open(char* d) {
|
|
159
|
+ if (data != nullptr)
|
|
160
|
+ return 1;
|
|
161
|
+
|
|
162
|
+ if (d != nullptr) {
|
|
163
|
+ data = d;
|
|
164
|
+ offset = 0;
|
|
165
|
+ }
|
|
166
|
+
|
|
167
|
+ return 0;
|
|
168
|
+}
|
|
169
|
+
|
|
170
|
+long long BinaryMemory::tell() {
|
|
171
|
+ assert(offset >= 0);
|
|
172
|
+ return offset;
|
|
173
|
+}
|
|
174
|
+
|
|
175
|
+void BinaryMemory::seek(long long pos) {
|
|
176
|
+ assert(pos >= 0);
|
|
177
|
+ offset = pos;
|
|
178
|
+}
|
|
179
|
+
|
|
180
|
+void BinaryMemory::read(char* d, int c) {
|
|
181
|
+ for (int i = 0; i < c; i++) {
|
|
182
|
+ d[i] = data[offset + i];
|
|
183
|
+ }
|
|
184
|
+ offset += c;
|
|
185
|
+}
|
|
186
|
+
|
|
187
|
+uint8_t BinaryMemory::readU8() {
|
|
188
|
+ assert(offset >= 0);
|
|
189
|
+ uint8_t ret;
|
|
190
|
+ char* c = reinterpret_cast<char*>(&ret);
|
|
191
|
+ read(c, 1);
|
|
192
|
+ return ret;
|
|
193
|
+}
|
|
194
|
+
|
|
195
|
+uint16_t BinaryMemory::readU16() {
|
|
196
|
+ assert(offset >= 0);
|
|
197
|
+ uint8_t a = readU8();
|
|
198
|
+ uint8_t b = readU8();
|
|
199
|
+ return ((uint16_t)a | (uint16_t)(b << 8));
|
|
200
|
+}
|
|
201
|
+
|
|
202
|
+uint32_t BinaryMemory::readU32() {
|
|
203
|
+ assert(offset >= 0);
|
|
204
|
+ uint16_t a = readU16();
|
|
205
|
+ uint16_t b = readU16();
|
|
206
|
+ return ((uint32_t)a | (uint32_t)(b << 16));
|
|
207
|
+}
|
|
208
|
+
|
|
209
|
+uint64_t BinaryMemory::readU64() {
|
|
210
|
+ assert(offset >= 0);
|
|
211
|
+ uint32_t a = readU32();
|
|
212
|
+ uint32_t b = readU32();
|
|
213
|
+ return ((uint64_t)a | (uint64_t)(b << 32));
|
|
214
|
+}
|
|
215
|
+
|
|
216
|
+float BinaryMemory::readFloat() {
|
|
217
|
+ assert(offset >= 0);
|
|
218
|
+ uint32_t val = readU32();
|
|
219
|
+ char* a = reinterpret_cast<char*>(&val);
|
|
220
|
+
|
|
221
|
+ float ret;
|
|
222
|
+ char* b = reinterpret_cast<char*>(&ret);
|
|
223
|
+
|
|
224
|
+ for (int i = 0; i < 4; i++)
|
|
225
|
+ b[i] = a[i];
|
|
226
|
+
|
|
227
|
+ return ret;
|
|
228
|
+}
|
|
229
|
+
|
|
230
|
+int8_t BinaryMemory::read8() {
|
|
231
|
+ assert(offset >= 0);
|
|
232
|
+ int8_t ret;
|
|
233
|
+ char* p = reinterpret_cast<char*>(&ret);
|
|
234
|
+ read(p, sizeof(ret));
|
|
235
|
+ return ret;
|
|
236
|
+}
|
|
237
|
+
|
|
238
|
+int16_t BinaryMemory::read16() {
|
|
239
|
+ assert(offset >= 0);
|
|
240
|
+ int16_t ret;
|
|
241
|
+ char* p = reinterpret_cast<char*>(&ret);
|
|
242
|
+ read(p, sizeof(ret));
|
|
243
|
+ swapByteOrder(p, 2);
|
|
244
|
+ return ret;
|
|
245
|
+}
|
|
246
|
+
|
|
247
|
+int32_t BinaryMemory::read32() {
|
|
248
|
+ assert(offset >= 0);
|
|
249
|
+ int32_t ret;
|
|
250
|
+ char* p = reinterpret_cast<char*>(&ret);
|
|
251
|
+ read(p, sizeof(ret));
|
|
252
|
+ swapByteOrder(p, 4);
|
|
253
|
+ return ret;
|
|
254
|
+}
|
|
255
|
+
|
|
256
|
+int64_t BinaryMemory::read64() {
|
|
257
|
+ assert(offset >= 0);
|
|
258
|
+ assert(offset >= 0);
|
|
259
|
+ int64_t ret;
|
|
260
|
+ char* p = reinterpret_cast<char*>(&ret);
|
|
261
|
+ read(p, sizeof(ret));
|
|
262
|
+ swapByteOrder(p, 8);
|
|
263
|
+ return ret;
|
|
264
|
+}
|
|
265
|
+
|