-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathBitstream.cpp
390 lines (335 loc) · 7.16 KB
/
Bitstream.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
#include "Bitstream.hpp"
#include "Logging.hpp"
/*
* Creates a new blank bitstream.
* @author eezstreet
*/
Bitstream::Bitstream()
{
pStream = nullptr;
dwTotalStreamSizeBits = 0;
dwTotalStreamSizeBytes = 0;
bExternalStorage = false;
dwStreamStartByte = 0;
dwStreamStartBit = 0;
}
/*
* Deletes the bitstream.
* @author eezstreet
*/
Bitstream::~Bitstream()
{
FreeInternalStreamSource();
}
/*
* Retrieves the data in a bitstream.
* The outSize argument is filled with the size of the bitstream.
* @author eezstreet
*/
BYTE* Bitstream::GetHeldData(size_t& outSize)
{
outSize = dwTotalStreamSizeBytes;
return pStream;
}
/*
* Load in a stream using external memory storage.
* The external source is not modified and needs to be freed manually when the stream is destroyed.
* @author eezstreet
*/
void Bitstream::LoadStream(BYTE* pNewStream, size_t dwNewSizeBytes)
{
FreeInternalStreamSource();
bExternalStorage = true;
pStream = pNewStream;
dwTotalStreamSizeBits = dwNewSizeBytes * 8;
dwTotalStreamSizeBytes = dwNewSizeBytes;
dwStreamStartByte = 0;
dwStreamStartBit = 0;
#ifndef D2_BIG_ENDIAN
dwReadBit = dwCurrentByte = 0;
#endif
}
/*
* "Split" this stream from another stream. It inherits the data but uses a different offset.
* @author eezstreet
*/
void Bitstream::SplitFrom(Bitstream* pSplitStream, size_t dwSplitStreamSizeBits)
{
DWORD dwBitsHanging = (dwSplitStreamSizeBits % 8);
DWORD dwBytes = (dwSplitStreamSizeBits - dwBitsHanging) / 8;
FreeInternalStreamSource();
// Copy from the other bitstream
bExternalStorage = true;
dwReadBit = pSplitStream->dwReadBit;
dwCurrentByte = pSplitStream->dwCurrentByte;
if (dwReadBit == 0)
{ // Send the bitstream back by one byte so we don't encounter errors
dwCurrentByte--;
}
dwTotalStreamSizeBits = pSplitStream->dwTotalStreamSizeBits;
dwTotalStreamSizeBytes = pSplitStream->dwTotalStreamSizeBytes;
pStream = pSplitStream->pStream;
// Advance the other bitstream
pSplitStream->dwCurrentByte += dwBytes;
pSplitStream->dwReadBit += dwBitsHanging;
while (pSplitStream->dwReadBit >= 8)
{
pSplitStream->dwCurrentByte++;
pSplitStream->dwReadBit -= 8;
}
dwStreamStartBit = dwReadBit;
dwStreamStartByte = dwCurrentByte;
}
/*
* Set the current stream position.
* @author eezstreet
*/
void Bitstream::SetCurrentPosition(DWORD dwPosition, DWORD dwBitOffset)
{
dwCurrentByte = dwPosition;
dwReadBit = dwBitOffset;
}
/*
* "Rewind" the stream back to its original start.
* When you use this on a split stream, it will rewind back to the split start.
* When you use this on any other stream, it will rewind all the way back to the beginning.
* @author eezstreet
*/
void Bitstream::Rewind()
{
dwReadBit = dwStreamStartBit;
dwCurrentByte = dwStreamStartByte;
}
/*
* Read a byte from the stream.
* @author eezstreet
*/
// Overload 1
void Bitstream::ReadByte(BYTE& outByte)
{
DWORD bits = ReadBits(8);
outByte = (BYTE)bits;
}
// Overload 2
void Bitstream::ReadByte(BYTE* outByte)
{
DWORD bits = ReadBits(8);
*outByte = (BYTE)bits;
}
/*
* Read a word from the stream.
* @author eezstreet
*/
// Overload 1
void Bitstream::ReadWord(WORD& outWord)
{
DWORD bits = ReadBits(16);
outWord = (WORD)bits;
}
// Overload 2
void Bitstream::ReadWord(WORD* outWord)
{
DWORD bits = ReadBits(16);
*outWord = (WORD)bits;
}
/*
* Read a doubleword from the stream.
* @author eezstreet
*/
// Overload 1
void Bitstream::ReadDWord(DWORD& outDWord)
{
DWORD bits = ReadBits(32);
outDWord = bits;
}
// Overload 2
void Bitstream::ReadDWord(DWORD* outDWord)
{
DWORD bits = ReadBits(32);
*outDWord = bits;
}
/*
* Read bits from the stream (public). If a negative number is used it will read a signed number.
* @author eezstreet
*/
// Overload 1
void Bitstream::ReadBits(BYTE& outBits, int bitCount)
{
BYTE bits = ReadBits(bitCount);
outBits = bits;
}
// Overload 2
void Bitstream::ReadBits(WORD& outBits, int bitCount)
{
WORD bits = ReadBits(bitCount);
outBits = bits;
}
// Overload 3
void Bitstream::ReadBits(DWORD& outBits, int bitCount)
{
DWORD bits = ReadBits(bitCount);
outBits = bits;
}
// Overload 4
void Bitstream::ReadBits(BYTE* outBits, int bitCount)
{
BYTE bits = ReadBits(bitCount);
*outBits = bits;
}
// Overload 5
void Bitstream::ReadBits(WORD* outBits, int bitCount)
{
WORD bits = ReadBits(bitCount);
*outBits = bits;
}
// Overload 6
void Bitstream::ReadBits(DWORD* outBits, int bitCount)
{
DWORD bits = ReadBits(bitCount);
*outBits = bits;
}
// Overload 7
void Bitstream::ReadBits(void* outBits, size_t outBitsSize, int bitCount)
{
switch (outBitsSize)
{
case 1:
{
BYTE nBits = ReadBits(bitCount);
*(BYTE*)outBits = nBits;
}
break;
case 2:
{
WORD wBits = ReadBits(bitCount);
*(WORD*)outBits = wBits;
}
break;
case 4:
{
DWORD dwBits = ReadBits(bitCount);
*(DWORD*)outBits = dwBits;
}
break;
case 8:
{
QWORD qwBits = ReadBits(bitCount);
*(QWORD*)outBits = qwBits;
}
break;
}
}
/*
* Helper function: converts from an unsigned number to a twos complement number.
* @author eezstreet/SVR
*/
void Bitstream::ConvertFormat(long* dwOutBits, int bitCount)
{
long dwValue;
if (dwOutBits == nullptr)
{
return;
}
dwValue = *dwOutBits;
if (bitCount < 32 && (dwValue & (1 << (bitCount - 1))))
{
dwValue |= ~((1 << bitCount) - 1);
}
*dwOutBits = dwValue;
}
/*
* Get the number of bits remaining to be read.
* @author eezstreet
*/
size_t Bitstream::GetRemainingReadBits()
{
return dwTotalStreamSizeBits - ((dwCurrentByte * 8) + dwReadBit);
}
/*
* Read bits from the stream (private). If a negative number is used it will read a signed number.
* @author id Software / eezstreet
*/
int Bitstream::ReadBits(int numBits)
{
int value;
int valueBits;
int get;
int fraction;
bool sgn;
if (numBits == 0)
{
return 0;
}
Log_ErrorAssertReturn(pStream != nullptr, 0);
Log_ErrorAssertReturn(numBits >= -31 && numBits <= 32, 0);
value = 0;
valueBits = 0;
if (numBits < 0) {
numBits = -numBits;
sgn = true;
}
else {
sgn = false;
}
// check for overflow
if (numBits > GetRemainingReadBits()) {
return -1;
}
while (valueBits < numBits) {
if (dwReadBit == 0) {
dwCurrentByte++;
}
get = 8 - dwReadBit;
if (get >(numBits - valueBits)) {
get = numBits - valueBits;
}
fraction = pStream[dwCurrentByte - 1];
fraction >>= dwReadBit;
fraction &= (1 << get) - 1;
value |= fraction << valueBits;
valueBits += get;
dwReadBit = (dwReadBit + get) & 7;
}
if (sgn) {
if (value & (1 << (numBits - 1))) {
value |= -1 ^ ((1 << numBits) - 1);
}
}
Log_ErrorAssertReturn(dwCurrentByte <= dwTotalStreamSizeBytes, 0);
return value;
}
/*
* Reads a big chunk of continuous data from the stream
* @author eezstreet
*/
void Bitstream::ReadData(void* data, size_t dataSize)
{
size_t i;
for (i = 0; i < dataSize; i++)
{
ReadByte((BYTE*)data);
}
}
/*
* Frees the memory that this bitstream is holding onto, if any
* @author eezstreet
*/
void Bitstream::FreeInternalStreamSource()
{
if (pStream != nullptr && !bExternalStorage)
{
free(pStream);
}
}
/*
* Aligns the bitstream to the nearest byte boundary.
* @author Necrolis
*/
void Bitstream::Align()
{
if (dwReadBit != 8)
{
dwReadBit = 8;
dwCurrentByte++;
}
}