|
@@ -5,77 +5,41 @@
|
5
|
5
|
* \author xythobuz
|
6
|
6
|
*/
|
7
|
7
|
|
|
8
|
+#include <sstream>
|
|
9
|
+
|
8
|
10
|
#include "global.h"
|
|
11
|
+#include "Exception.h"
|
9
|
12
|
#include "utils/binary.h"
|
10
|
13
|
|
11
|
|
-BinaryFile::BinaryFile(const char* f) {
|
12
|
|
- int r = open(f);
|
13
|
|
- if (r != 0)
|
14
|
|
- throw r;
|
15
|
|
-}
|
16
|
|
-
|
17
|
|
-BinaryFile::~BinaryFile() {
|
18
|
|
- if (file.is_open())
|
19
|
|
- file.close();
|
20
|
|
-}
|
21
|
|
-
|
22
|
|
-int BinaryFile::open(const char* f) {
|
23
|
|
- if (file.is_open()) {
|
24
|
|
- return 1;
|
25
|
|
- } else {
|
26
|
|
- if (f == nullptr)
|
27
|
|
- return 0;
|
28
|
|
- file.open(f, std::ios_base::in | std::ios_base::binary);
|
29
|
|
- return (file ? 0 : 1);
|
30
|
|
- }
|
31
|
|
-}
|
32
|
|
-
|
33
|
|
-long long BinaryFile::tell() {
|
34
|
|
- assert(file.is_open());
|
35
|
|
- return file.tellg();
|
36
|
|
-}
|
37
|
|
-
|
38
|
|
-void BinaryFile::seek(long long pos) {
|
39
|
|
- assert(file.is_open());
|
40
|
|
- file.seekg(pos);
|
|
14
|
+BinaryReader::~BinaryReader() {
|
41
|
15
|
}
|
42
|
16
|
|
43
|
|
-uint8_t BinaryFile::readU8() {
|
44
|
|
- assert(file.is_open());
|
45
|
|
- assert(file.good());
|
|
17
|
+uint8_t BinaryReader::readU8() {
|
46
|
18
|
uint8_t ret;
|
47
|
19
|
char* c = reinterpret_cast<char*>(&ret);
|
48
|
|
- file.read(c, 1);
|
|
20
|
+ read(c, 1);
|
49
|
21
|
return ret;
|
50
|
22
|
}
|
51
|
23
|
|
52
|
|
-uint16_t BinaryFile::readU16() {
|
53
|
|
- assert(file.is_open());
|
54
|
|
- assert(file.good());
|
55
|
|
- uint8_t a = readU8();
|
56
|
|
- uint8_t b = readU8();
|
57
|
|
- return ((uint16_t)a | (uint16_t)(b << 8));
|
|
24
|
+uint16_t BinaryReader::readU16() {
|
|
25
|
+ uint16_t a = readU8();
|
|
26
|
+ uint16_t b = readU8();
|
|
27
|
+ return (a | (b << 8));
|
58
|
28
|
}
|
59
|
29
|
|
60
|
|
-uint32_t BinaryFile::readU32() {
|
61
|
|
- assert(file.is_open());
|
62
|
|
- assert(file.good());
|
63
|
|
- uint16_t a = readU16();
|
64
|
|
- uint16_t b = readU16();
|
65
|
|
- return ((uint32_t)a | (uint32_t)(b << 16));
|
|
30
|
+uint32_t BinaryReader::readU32() {
|
|
31
|
+ uint32_t a = readU16();
|
|
32
|
+ uint32_t b = readU16();
|
|
33
|
+ return (a | (b << 16));
|
66
|
34
|
}
|
67
|
35
|
|
68
|
|
-uint64_t BinaryFile::readU64() {
|
69
|
|
- assert(file.is_open());
|
70
|
|
- assert(file.good());
|
71
|
|
- uint32_t a = readU32();
|
72
|
|
- uint32_t b = readU32();
|
73
|
|
- return ((uint64_t)a | ((uint64_t)b << 32));
|
|
36
|
+uint64_t BinaryReader::readU64() {
|
|
37
|
+ uint64_t a = readU32();
|
|
38
|
+ uint64_t b = readU32();
|
|
39
|
+ return (a | (b << 32));
|
74
|
40
|
}
|
75
|
41
|
|
76
|
|
-float BinaryFile::readFloat() {
|
77
|
|
- assert(file.is_open());
|
78
|
|
- assert(file.good());
|
|
42
|
+float BinaryReader::readFloat() {
|
79
|
43
|
uint32_t val = readU32();
|
80
|
44
|
char* a = reinterpret_cast<char*>(&val);
|
81
|
45
|
|
|
@@ -95,7 +59,7 @@ namespace {
|
95
|
59
|
* without having to detect the endianness at run-time?
|
96
|
60
|
*/
|
97
|
61
|
const int bigendiandetection = 1;
|
98
|
|
-#define ISBIGENDIAN() ((*(char *)&bigendiandetection) == 0)
|
|
62
|
+#define ISBIGENDIAN() ((*reinterpret_cast<const char *>(&bigendiandetection)) == 0)
|
99
|
63
|
|
100
|
64
|
void swapByteOrder(char* d, unsigned int n) {
|
101
|
65
|
if (ISBIGENDIAN()) {
|
|
@@ -108,158 +72,133 @@ namespace {
|
108
|
72
|
}
|
109
|
73
|
}
|
110
|
74
|
|
111
|
|
-int8_t BinaryFile::read8() {
|
112
|
|
- assert(file.is_open());
|
113
|
|
- assert(file.good());
|
|
75
|
+int8_t BinaryReader::read8() {
|
114
|
76
|
int8_t ret;
|
115
|
77
|
char* p = reinterpret_cast<char*>(&ret);
|
116
|
|
- file.read(p, sizeof(ret));
|
|
78
|
+ read(p, sizeof(ret));
|
117
|
79
|
return ret;
|
118
|
80
|
}
|
119
|
81
|
|
120
|
|
-int16_t BinaryFile::read16() {
|
121
|
|
- assert(file.is_open());
|
122
|
|
- assert(file.good());
|
|
82
|
+int16_t BinaryReader::read16() {
|
123
|
83
|
int16_t ret;
|
124
|
84
|
char* p = reinterpret_cast<char*>(&ret);
|
125
|
|
- file.read(p, sizeof(ret));
|
|
85
|
+ read(p, sizeof(ret));
|
126
|
86
|
swapByteOrder(p, 2);
|
127
|
87
|
return ret;
|
128
|
88
|
}
|
129
|
89
|
|
130
|
|
-int32_t BinaryFile::read32() {
|
131
|
|
- assert(file.is_open());
|
132
|
|
- assert(file.good());
|
|
90
|
+int32_t BinaryReader::read32() {
|
133
|
91
|
int32_t ret;
|
134
|
92
|
char* p = reinterpret_cast<char*>(&ret);
|
135
|
|
- file.read(p, sizeof(ret));
|
|
93
|
+ read(p, sizeof(ret));
|
136
|
94
|
swapByteOrder(p, 4);
|
137
|
95
|
return ret;
|
138
|
96
|
}
|
139
|
97
|
|
140
|
|
-int64_t BinaryFile::read64() {
|
141
|
|
- assert(file.is_open());
|
142
|
|
- assert(file.good());
|
|
98
|
+int64_t BinaryReader::read64() {
|
143
|
99
|
int64_t ret;
|
144
|
100
|
char* p = reinterpret_cast<char*>(&ret);
|
145
|
|
- file.read(p, sizeof(ret));
|
|
101
|
+ read(p, sizeof(ret));
|
146
|
102
|
swapByteOrder(p, 8);
|
147
|
103
|
return ret;
|
148
|
104
|
}
|
149
|
105
|
|
150
|
106
|
// ----------------------------------------------------------------------------
|
151
|
107
|
|
152
|
|
-BinaryMemory::BinaryMemory(char* d) : data(nullptr), offset(0) {
|
153
|
|
- int r = open(d);
|
|
108
|
+BinaryFile::BinaryFile(std::string f) {
|
|
109
|
+ int r = open(f);
|
154
|
110
|
if (r != 0)
|
155
|
111
|
throw r;
|
156
|
112
|
}
|
157
|
113
|
|
158
|
|
-int BinaryMemory::open(char* d) {
|
159
|
|
- if (data != nullptr)
|
160
|
|
- return 1;
|
|
114
|
+BinaryFile::~BinaryFile() {
|
|
115
|
+ if (file.is_open())
|
|
116
|
+ file.close();
|
|
117
|
+}
|
161
|
118
|
|
162
|
|
- if (d != nullptr) {
|
163
|
|
- data = d;
|
164
|
|
- offset = 0;
|
|
119
|
+int BinaryFile::open(std::string f) {
|
|
120
|
+ if (file.is_open()) {
|
|
121
|
+ return 1;
|
|
122
|
+ } else {
|
|
123
|
+ if (f == "")
|
|
124
|
+ return 0;
|
|
125
|
+ file.open(f, std::ios_base::in | std::ios_base::binary);
|
|
126
|
+ return (file ? 0 : 1);
|
165
|
127
|
}
|
166
|
|
-
|
167
|
|
- return 0;
|
168
|
128
|
}
|
169
|
129
|
|
170
|
|
-long long BinaryMemory::tell() {
|
171
|
|
- assert(offset >= 0);
|
172
|
|
- return offset;
|
|
130
|
+long long BinaryFile::tell() {
|
|
131
|
+ assert(file.is_open());
|
|
132
|
+ return file.tellg();
|
173
|
133
|
}
|
174
|
134
|
|
175
|
|
-void BinaryMemory::seek(long long pos) {
|
176
|
|
- assert(pos >= 0);
|
177
|
|
- offset = pos;
|
|
135
|
+void BinaryFile::seek(long long pos) {
|
|
136
|
+ assert(file.is_open());
|
|
137
|
+ file.seekg(pos);
|
178
|
138
|
}
|
179
|
139
|
|
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;
|
|
140
|
+bool BinaryFile::eof() {
|
|
141
|
+ file.peek();
|
|
142
|
+ return file.eof();
|
185
|
143
|
}
|
186
|
144
|
|
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;
|
|
145
|
+void BinaryFile::read(char* d, int c) {
|
|
146
|
+ assert(file.is_open());
|
|
147
|
+ assert(file.good());
|
|
148
|
+ file.read(d, c);
|
193
|
149
|
}
|
194
|
150
|
|
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
|
|
-}
|
|
151
|
+// ----------------------------------------------------------------------------
|
201
|
152
|
|
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));
|
|
153
|
+BinaryMemory::BinaryMemory(const char* d, long long m) : data(nullptr), offset(0), max(-1) {
|
|
154
|
+ int r = open(d, m);
|
|
155
|
+ if (r != 0)
|
|
156
|
+ throw r;
|
207
|
157
|
}
|
208
|
158
|
|
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));
|
|
159
|
+BinaryMemory::~BinaryMemory() {
|
214
|
160
|
}
|
215
|
161
|
|
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);
|
|
162
|
+int BinaryMemory::open(const char* d, long long m) {
|
|
163
|
+ if (data != nullptr)
|
|
164
|
+ return 1;
|
223
|
165
|
|
224
|
|
- for (int i = 0; i < 4; i++)
|
225
|
|
- b[i] = a[i];
|
|
166
|
+ if (d != nullptr) {
|
|
167
|
+ data = d;
|
|
168
|
+ offset = 0;
|
|
169
|
+ max = m;
|
|
170
|
+ }
|
226
|
171
|
|
227
|
|
- return ret;
|
|
172
|
+ return 0;
|
228
|
173
|
}
|
229
|
174
|
|
230
|
|
-int8_t BinaryMemory::read8() {
|
|
175
|
+long long BinaryMemory::tell() {
|
231
|
176
|
assert(offset >= 0);
|
232
|
|
- int8_t ret;
|
233
|
|
- char* p = reinterpret_cast<char*>(&ret);
|
234
|
|
- read(p, sizeof(ret));
|
235
|
|
- return ret;
|
|
177
|
+ return offset;
|
236
|
178
|
}
|
237
|
179
|
|
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;
|
|
180
|
+void BinaryMemory::seek(long long pos) {
|
|
181
|
+ assert(pos >= 0);
|
|
182
|
+ offset = pos;
|
245
|
183
|
}
|
246
|
184
|
|
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;
|
|
185
|
+bool BinaryMemory::eof() {
|
|
186
|
+ return (offset > max);
|
254
|
187
|
}
|
255
|
188
|
|
256
|
|
-int64_t BinaryMemory::read64() {
|
257
|
|
- assert(offset >= 0);
|
|
189
|
+void BinaryMemory::read(char* d, int c) {
|
258
|
190
|
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;
|
|
191
|
+ assert(c > 0);
|
|
192
|
+ if ((offset + c) > max) {
|
|
193
|
+ std::ostringstream ss;
|
|
194
|
+ ss << "BinaryMemory read out of bounds ("
|
|
195
|
+ << offset << " + " << c << " > " << max;
|
|
196
|
+ throw new Exception(ss.str());
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ for (int i = 0; i < c; i++) {
|
|
200
|
+ d[i] = data[offset + i];
|
|
201
|
+ }
|
|
202
|
+ offset += c;
|
264
|
203
|
}
|
265
|
204
|
|