-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathBinaryReader.cpp
More file actions
197 lines (180 loc) · 4.75 KB
/
BinaryReader.cpp
File metadata and controls
197 lines (180 loc) · 4.75 KB
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
#include "BinaryReader.hpp"
BinaryReader::BinaryReader(Stream& _input)
: input(_input),
buffer{ 0 }
{
}
//---------------------------------------------------------------------------
bool BinaryReader::ReadBoolean()
{
FillBuffer(1);
return buffer[0] != 0;
}
//---------------------------------------------------------------------------
uint8_t BinaryReader::ReadByte()
{
int b = input.ReadByte();
if (b == -1)
{
throw IOException();
}
return (uint8_t)b;
}
//---------------------------------------------------------------------------
std::vector<uint8_t> BinaryReader::ReadBytes(int count)
{
std::vector<uint8_t> result(count);
result.resize(count);
int numRead = 0;
do
{
int n = input.Read(result.data(), numRead, count);
if (n == 0)
{
break;
}
numRead += n;
count -= n;
} while (count > 0);
if (numRead != count)
{
result = std::vector<uint8_t>(std::begin(result), std::begin(result) + numRead);
}
return result;
}
//---------------------------------------------------------------------------
short BinaryReader::ReadInt16()
{
FillBuffer(2);
return (short)(buffer[0] | buffer[1] << 8);
}
//---------------------------------------------------------------------------
unsigned short BinaryReader::ReadUInt16()
{
FillBuffer(2);
return (unsigned short)(buffer[0] | buffer[1] << 8);
}
//---------------------------------------------------------------------------
int BinaryReader::ReadInt32()
{
FillBuffer(4);
return (int)(buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24);
}
//---------------------------------------------------------------------------
unsigned int BinaryReader::ReadUInt32()
{
FillBuffer(4);
return (unsigned int)(buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24);
}
//---------------------------------------------------------------------------
long long BinaryReader::ReadInt64()
{
FillBuffer(8);
unsigned int lo = (unsigned int)(buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24);
unsigned int hi = (unsigned int)(buffer[4] | buffer[5] << 8 | buffer[6] << 16 | buffer[7] << 24);
return (long long)((unsigned long long)hi) << 32 | lo;
}
//---------------------------------------------------------------------------
unsigned long long BinaryReader::ReadUInt64()
{
FillBuffer(8);
unsigned int lo = (unsigned int)(buffer[0] | buffer[1] << 8 | buffer[2] << 16 | buffer[3] << 24);
unsigned int hi = (unsigned int)(buffer[4] | buffer[5] << 8 | buffer[6] << 16 | buffer[7] << 24);
return ((unsigned long long)hi) << 32 | lo;
}
//---------------------------------------------------------------------------
void* BinaryReader::ReadIntPtr()
{
#ifdef _WIN64
return reinterpret_cast<void*>(ReadUInt64());
#else
return reinterpret_cast<void*>(ReadUInt32());
#endif
}
//---------------------------------------------------------------------------
float BinaryReader::ReadSingle()
{
auto tmp = ReadUInt32();
return *(float*)&tmp;
}
//---------------------------------------------------------------------------
double BinaryReader::ReadDouble()
{
auto tmp = ReadUInt64();
return *(double*)&tmp;
}
//---------------------------------------------------------------------------
std::wstring BinaryReader::ReadString()
{
const int MaxCharBytesSize = 128;
auto byteLength = Read7BitEncodedInt();
if (byteLength < 0)
{
throw IOException();
}
if (byteLength == 0)
{
return std::wstring();
}
std::wstring tmp;
int currPos = 0;
uint8_t charBytes[MaxCharBytesSize];
do
{
auto readLength = (byteLength - currPos) > MaxCharBytesSize ? MaxCharBytesSize : byteLength - currPos;
auto n = input.Read(charBytes, 0, readLength);
if (n == 0)
{
throw IOException();
}
tmp.append(reinterpret_cast<const std::wstring::value_type*>(charBytes), readLength / sizeof(std::wstring::value_type));
currPos += n;
} while (currPos < byteLength);
return tmp;
}
//---------------------------------------------------------------------------
void BinaryReader::FillBuffer(int numBytes)
{
int bytesRead = 0;
int n = 0;
if (numBytes == 1)
{
n = input.ReadByte();
if (n == -1)
{
throw IOException();
}
buffer[0] = (uint8_t)n;
return;
}
do
{
n = input.Read(buffer, bytesRead, numBytes - bytesRead);
if (n == 0)
{
throw IOException();
}
bytesRead += n;
} while (bytesRead < numBytes);
}
//---------------------------------------------------------------------------
int BinaryReader::Read7BitEncodedInt()
{
// Read out an int32 7 bits at a time. The high bit of the
// byte when on means to continue reading more bytes.
int count = 0;
int shift = 0;
uint8_t b;
do
{
if (shift == 5 * 7)
{
throw IOException();
}
b = ReadByte();
count |= (b & 0x7F) << shift;
shift += 7;
} while ((b & 0x80) != 0);
return count;
}
//---------------------------------------------------------------------------