|
@@ -8,51 +8,114 @@
|
8
|
8
|
#include "global.h"
|
9
|
9
|
#include "utils/binary.h"
|
10
|
10
|
|
11
|
|
-void binOpen(const char *file) {
|
|
11
|
+/*! \fixme Left-Shift with signed integer is undefined!
|
|
12
|
+ * Is there a portable way to read multi-byte signed integers?
|
|
13
|
+ * Without having to detect the endianness at run-time?
|
|
14
|
+ */
|
|
15
|
+const int bigendiandetection = 1;
|
|
16
|
+#define ISBIGENDIAN() ((*(char *)&bigendiandetection) == 0)
|
12
|
17
|
|
|
18
|
+BinaryFile::BinaryFile(const char *f) {
|
|
19
|
+ file.open(f, std::ios_base::in | std::ios_base:: binary);
|
13
|
20
|
}
|
14
|
21
|
|
15
|
|
-void binClose() {
|
16
|
|
-
|
|
22
|
+BinaryFile::~BinaryFile() {
|
|
23
|
+ file.close();
|
17
|
24
|
}
|
18
|
25
|
|
19
|
|
-unsigned long binTell() {
|
20
|
|
- return 0;
|
|
26
|
+long long BinaryFile::tell() {
|
|
27
|
+ return file.tellg();
|
21
|
28
|
}
|
22
|
29
|
|
23
|
|
-void binSeek(unsigned long pos) {
|
24
|
|
-
|
|
30
|
+void BinaryFile::seek(long long pos) {
|
|
31
|
+ assert(pos >= 0);
|
|
32
|
+ file.seekg(pos);
|
25
|
33
|
}
|
26
|
34
|
|
27
|
|
-int8_t binRead8() {
|
28
|
|
- return 0;
|
|
35
|
+int8_t BinaryFile::read8() {
|
|
36
|
+ int8_t ret;
|
|
37
|
+ file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
|
|
38
|
+ return ret;
|
29
|
39
|
}
|
30
|
40
|
|
31
|
|
-uint8_t binReadU8() {
|
32
|
|
- return 0;
|
|
41
|
+uint8_t BinaryFile::readU8() {
|
|
42
|
+ uint8_t ret;
|
|
43
|
+ file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
|
|
44
|
+ return ret;
|
33
|
45
|
}
|
34
|
46
|
|
35
|
|
-int16_t binRead16() {
|
36
|
|
- return 0;
|
|
47
|
+int16_t BinaryFile::read16() {
|
|
48
|
+ int16_t ret;
|
|
49
|
+ file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
|
|
50
|
+ if (ISBIGENDIAN()) {
|
|
51
|
+ char *p = reinterpret_cast<char *>(&ret);
|
|
52
|
+ char tmp = p[0];
|
|
53
|
+ p[0] = p[1];
|
|
54
|
+ p[1] = tmp;
|
|
55
|
+ }
|
|
56
|
+ return ret;
|
37
|
57
|
}
|
38
|
58
|
|
39
|
|
-uint16_t binReadU16() {
|
40
|
|
- return 0;
|
|
59
|
+uint16_t BinaryFile::readU16() {
|
|
60
|
+ uint16_t a = readU8();
|
|
61
|
+ uint16_t b = readU8();
|
|
62
|
+ return (uint16_t)(a | (b << 8));
|
41
|
63
|
}
|
42
|
64
|
|
43
|
|
-int32_t binRead32() {
|
44
|
|
- return 0;
|
|
65
|
+int32_t BinaryFile::read32() {
|
|
66
|
+ int32_t ret;
|
|
67
|
+ file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
|
|
68
|
+ if (ISBIGENDIAN()) {
|
|
69
|
+ char *p = reinterpret_cast<char *>(&ret);
|
|
70
|
+ char tmp = p[0];
|
|
71
|
+ p[0] = p[3];
|
|
72
|
+ p[3] = tmp;
|
|
73
|
+ tmp = p[1];
|
|
74
|
+ p[1] = p[2];
|
|
75
|
+ p[2] = tmp;
|
|
76
|
+ }
|
|
77
|
+ return ret;
|
45
|
78
|
}
|
46
|
79
|
|
47
|
|
-uint32_t binReadU32() {
|
48
|
|
- return 0;
|
|
80
|
+uint32_t BinaryFile::readU32() {
|
|
81
|
+ uint32_t a = readU8();
|
|
82
|
+ uint32_t b = readU8();
|
|
83
|
+ uint32_t c = readU8();
|
|
84
|
+ uint32_t d = readU8();
|
|
85
|
+ return (uint32_t)(a | (b << 8) | (c << 16) | (d << 24));
|
49
|
86
|
}
|
50
|
87
|
|
51
|
|
-int64_t binRead64() {
|
52
|
|
- return 0;
|
|
88
|
+int64_t BinaryFile::read64() {
|
|
89
|
+ int64_t ret;
|
|
90
|
+ file.read(reinterpret_cast<char *>(&ret), sizeof(ret));
|
|
91
|
+ if (ISBIGENDIAN()) {
|
|
92
|
+ char *p = reinterpret_cast<char *>(&ret);
|
|
93
|
+ char tmp = p[0];
|
|
94
|
+ p[0] = p[7];
|
|
95
|
+ p[7] = tmp;
|
|
96
|
+ tmp = p[1];
|
|
97
|
+ p[1] = p[6];
|
|
98
|
+ p[6] = tmp;
|
|
99
|
+ tmp = p[2];
|
|
100
|
+ p[2] = p[5];
|
|
101
|
+ p[5] = tmp;
|
|
102
|
+ tmp = p[3];
|
|
103
|
+ p[3] = p[4];
|
|
104
|
+ p[4] = tmp;
|
|
105
|
+ }
|
|
106
|
+ return ret;
|
53
|
107
|
}
|
54
|
108
|
|
55
|
|
-uint64_t binReadU64() {
|
56
|
|
- return 0;
|
|
109
|
+uint64_t BinaryFile::readU64() {
|
|
110
|
+ uint64_t a = readU8();
|
|
111
|
+ uint64_t b = readU8();
|
|
112
|
+ uint64_t c = readU8();
|
|
113
|
+ uint64_t d = readU8();
|
|
114
|
+ uint64_t e = readU8();
|
|
115
|
+ uint64_t f = readU8();
|
|
116
|
+ uint64_t g = readU8();
|
|
117
|
+ uint64_t h = readU8();
|
|
118
|
+ return (uint64_t)(a | (b << 8) | (c << 16) | (d << 24)
|
|
119
|
+ | (e << 32) | (f << 40) | (g << 48) | (h << 56));
|
57
|
120
|
}
|
58
|
121
|
|