forked from mas-bandwidth/yojimbo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyojimbo_bitpack.h
499 lines (366 loc) · 18.9 KB
/
yojimbo_bitpack.h
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/*
Yojimbo Client/Server Network Protocol Library.
Copyright © 2016, The Network Protocol Company, Inc.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef YOJIMBO_BITPACK_H
#define YOJIMBO_BITPACK_H
#include "yojimbo_config.h"
#include "yojimbo_common.h"
#include <assert.h>
#include <string.h>
/** @file */
namespace yojimbo
{
/**
Bitpacks unsigned integer values to a buffer.
Integer bit values are written to a 64 bit scratch value from right to left.
Once the low 32 bits of the scratch is filled with bits it is flushed to memory as a dword and the scratch value is shifted right by 32.
The bit stream is written to memory in little endian order, which is considered network byte order for this library.
@see BitReader
*/
class BitWriter
{
public:
/**
Bit writer constructor.
Creates a bit writer object to write to the specified buffer.
@param data The pointer to the buffer to fill with bitpacked data.
@param bytes The size of the buffer in bytes. Must be a multiple of 4, because the bitpacker reads and writes memory as dwords, not bytes.
*/
BitWriter( void * data, int bytes ) : m_data( (uint32_t*) data ), m_numWords( bytes / 4 )
{
assert( data );
assert( ( bytes % 4 ) == 0 );
m_numBits = m_numWords * 32;
m_bitsWritten = 0;
m_wordIndex = 0;
m_scratch = 0;
m_scratchBits = 0;
}
/**
Write bits to the buffer.
Bits are written to the buffer as-is, without padding to nearest byte. Will assert if you try to write past the end of the buffer.
A boolean value writes just 1 bit to the buffer, a value in range [0,31] can be written with just 5 bits and so on.
IMPORTANT: When you have finished writing to your buffer, take care to call BitWrite::FlushBits, otherwise the last dword of data will not get flushed to memory!
@param value The integer value to write to the buffer. Must be in [0,(1<<bits)-1].
@param bits The number of bits to encode in [1,32].
@see BitReader::ReadBits
*/
void WriteBits( uint32_t value, int bits )
{
assert( bits > 0 );
assert( bits <= 32 );
assert( m_bitsWritten + bits <= m_numBits );
assert( uint64_t( value ) <= ( ( 1ULL << bits ) - 1 ) );
m_scratch |= uint64_t( value ) << m_scratchBits;
m_scratchBits += bits;
if ( m_scratchBits >= 32 )
{
assert( m_wordIndex < m_numWords );
m_data[m_wordIndex] = host_to_network( uint32_t( m_scratch & 0xFFFFFFFF ) );
m_scratch >>= 32;
m_scratchBits -= 32;
m_wordIndex++;
}
m_bitsWritten += bits;
}
/**
Write an alignment to the bit stream, padding zeros so the bit index becomes is a multiple of 8.
This is useful if you want to write some data to a packet that should be byte aligned. For example, an array of bytes, or a string.
IMPORTANT: If the current bit index is already a multiple of 8, nothing is written.
@see BitReader::ReadAlign
*/
void WriteAlign()
{
const int remainderBits = m_bitsWritten % 8;
if ( remainderBits != 0 )
{
uint32_t zero = 0;
WriteBits( zero, 8 - remainderBits );
assert( ( m_bitsWritten % 8 ) == 0 );
}
}
/**
Write an array of bytes to the bit stream.
Use this when you have to copy a large block of data into your bitstream.
Faster than just writing each byte to the bit stream via BitWriter::WriteBits( value, 8 ), because it aligns to byte index and copies into the buffer without bitpacking.
@param data The byte array data to write to the bit stream.
@param bytes The number of bytes to write.
@see BitReader::ReadBytes
*/
void WriteBytes( const uint8_t * data, int bytes )
{
assert( GetAlignBits() == 0 );
assert( m_bitsWritten + bytes * 8 <= m_numBits );
assert( ( m_bitsWritten % 32 ) == 0 || ( m_bitsWritten % 32 ) == 8 || ( m_bitsWritten % 32 ) == 16 || ( m_bitsWritten % 32 ) == 24 );
int headBytes = ( 4 - ( m_bitsWritten % 32 ) / 8 ) % 4;
if ( headBytes > bytes )
headBytes = bytes;
for ( int i = 0; i < headBytes; ++i )
WriteBits( data[i], 8 );
if ( headBytes == bytes )
return;
FlushBits();
assert( GetAlignBits() == 0 );
int numWords = ( bytes - headBytes ) / 4;
if ( numWords > 0 )
{
assert( ( m_bitsWritten % 32 ) == 0 );
memcpy( &m_data[m_wordIndex], data + headBytes, numWords * 4 );
m_bitsWritten += numWords * 32;
m_wordIndex += numWords;
m_scratch = 0;
}
assert( GetAlignBits() == 0 );
int tailStart = headBytes + numWords * 4;
int tailBytes = bytes - tailStart;
assert( tailBytes >= 0 && tailBytes < 4 );
for ( int i = 0; i < tailBytes; ++i )
WriteBits( data[tailStart+i], 8 );
assert( GetAlignBits() == 0 );
assert( headBytes + numWords * 4 + tailBytes == bytes );
}
/**
Flush any remaining bits to memory.
Call this once after you've finished writing bits to flush the last dword of scratch to memory!
@see BitWriter::WriteBits
*/
void FlushBits()
{
if ( m_scratchBits != 0 )
{
assert( m_scratchBits <= 32 );
assert( m_wordIndex < m_numWords );
m_data[m_wordIndex] = host_to_network( uint32_t( m_scratch & 0xFFFFFFFF ) );
m_scratch >>= 32;
m_scratchBits = 0;
m_wordIndex++;
}
}
/**
How many align bits would be written, if we were to write an align right now?
@returns Result in [0,7], where 0 is zero bits required to align (already aligned) and 7 is worst case.
*/
int GetAlignBits() const
{
return ( 8 - ( m_bitsWritten % 8 ) ) % 8;
}
/**
How many bits have we written so far?
@returns The number of bits written to the bit buffer.
*/
int GetBitsWritten() const
{
return m_bitsWritten;
}
/**
How many bits are still available to write?
For example, if the buffer size is 4, we have 32 bits available to write, if we have already written 10 bytes then 22 are still available to write.
@returns The number of bits available to write.
*/
int GetBitsAvailable() const
{
return m_numBits - m_bitsWritten;
}
/**
Get a pointer to the data written by the bit writer.
Corresponds to the data block passed in to the constructor.
@returns Pointer to the data written by the bit writer.
*/
const uint8_t * GetData() const
{
return (uint8_t*) m_data;
}
/**
The number of bytes flushed to memory.
This is effectively the size of the packet that you should send after you have finished bitpacking values with this class.
The returned value is not always a multiple of 4, even though we flush dwords to memory. You won't miss any data in this case because the order of bits written is designed to work with the little endian memory layout.
IMPORTANT: Make sure you call BitWriter::FlushBits before calling this method, otherwise you risk missing the last dword of data.
*/
int GetBytesWritten() const
{
return ( m_bitsWritten + 7 ) / 8;
}
private:
uint32_t * m_data; ///< The buffer we are writing to, as a uint32_t * because we're writing dwords at a time.
uint64_t m_scratch; ///< The scratch value where we write bits to (right to left). 64 bit for overflow. Once # of bits in scratch is >= 32, the low 32 bits are flushed to memory.
int m_numBits; ///< The number of bits in the buffer. This is equivalent to the size of the buffer in bytes multiplied by 8. Note that the buffer size must always be a multiple of 4.
int m_numWords; ///< The number of words in the buffer. This is equivalent to the size of the buffer in bytes divided by 4. Note that the buffer size must always be a multiple of 4.
int m_bitsWritten; ///< The number of bits written so far.
int m_wordIndex; ///< The current word index. The next word flushed to memory will be at this index in m_data.
int m_scratchBits; ///< The number of bits in scratch. When this is >= 32, the low 32 bits of scratch is flushed to memory as a dword and scratch is shifted right by 32.
};
/**
Reads bit packed integer values from a buffer.
Relies on the user reconstructing the exact same set of bit reads as bit writes when the buffer was written. This is an unattributed bitpacked binary stream!
Implementation: 32 bit dwords are read in from memory to the high bits of a scratch value as required. The user reads off bit values from the scratch value from the right, after which the scratch value is shifted by the same number of bits.
*/
class BitReader
{
public:
/**
Bit reader constructor.
Non-multiples of four buffer sizes are supported, as this naturally tends to occur when packets are read from the network.
However, actual buffer allocated for the packet data must round up at least to the next 4 bytes in memory, because the bit reader reads dwords from memory not bytes.
@param data Pointer to the bitpacked data to read.
@param bytes The number of bytes of bitpacked data to read.
@see BitWriter
*/
#ifndef NDEBUG
BitReader( const void * data, int bytes ) : m_data( (const uint32_t*) data ), m_numBytes( bytes ), m_numWords( ( bytes + 3 ) / 4)
#else // #ifndef NDEBUG
BitReader( const void * data, int bytes ) : m_data( (const uint32_t*) data ), m_numBytes( bytes )
#endif // #ifndef NDEBUG
{
assert( data );
m_numBits = m_numBytes * 8;
m_bitsRead = 0;
m_scratch = 0;
m_scratchBits = 0;
m_wordIndex = 0;
}
/**
Would the bit reader would read past the end of the buffer if it read this many bits?
@param bits The number of bits that would be read.
@returns True if reading the number of bits would read past the end of the buffer.
*/
bool WouldReadPastEnd( int bits ) const
{
return m_bitsRead + bits > m_numBits;
}
/**
Read bits from the bit buffer.
This function will assert in debug builds if this read would read past the end of the buffer.
In production situations, the higher level ReadStream takes care of checking all packet data and never calling this function if it would read past the end of the buffer.
@param bits The number of bits to read in [1,32].
@returns The integer value read in range [0,(1<<bits)-1].
@see BitReader::WouldReadPastEnd
@see BitWriter::WriteBits
*/
uint32_t ReadBits( int bits )
{
assert( bits > 0 );
assert( bits <= 32 );
assert( m_bitsRead + bits <= m_numBits );
m_bitsRead += bits;
assert( m_scratchBits >= 0 && m_scratchBits <= 64 );
if ( m_scratchBits < bits )
{
assert( m_wordIndex < m_numWords );
m_scratch |= uint64_t( network_to_host( m_data[m_wordIndex] ) ) << m_scratchBits;
m_scratchBits += 32;
m_wordIndex++;
}
assert( m_scratchBits >= bits );
const uint32_t output = m_scratch & ( (uint64_t(1)<<bits) - 1 );
m_scratch >>= bits;
m_scratchBits -= bits;
return output;
}
/**
Read an align.
Call this on read to correspond to a WriteAlign call when the bitpacked buffer was written.
This makes sure we skip ahead to the next aligned byte index. As a safety check, we verify that the padding to next byte is zero bits and return false if that's not the case.
This will typically abort packet read. Just another safety measure...
@returns True if we successfully read an align and skipped ahead past zero pad, false otherwise (probably means, no align was written to the stream).
@see BitWriter::WriteAlign
*/
bool ReadAlign()
{
const int remainderBits = m_bitsRead % 8;
if ( remainderBits != 0 )
{
uint32_t value = ReadBits( 8 - remainderBits );
assert( m_bitsRead % 8 == 0 );
if ( value != 0 )
return false;
}
return true;
}
/**
Read bytes from the bitpacked data.
@see BitWriter::WriteBytes
*/
void ReadBytes( uint8_t * data, int bytes )
{
assert( GetAlignBits() == 0 );
assert( m_bitsRead + bytes * 8 <= m_numBits );
assert( ( m_bitsRead % 32 ) == 0 || ( m_bitsRead % 32 ) == 8 || ( m_bitsRead % 32 ) == 16 || ( m_bitsRead % 32 ) == 24 );
int headBytes = ( 4 - ( m_bitsRead % 32 ) / 8 ) % 4;
if ( headBytes > bytes )
headBytes = bytes;
for ( int i = 0; i < headBytes; ++i )
data[i] = (uint8_t) ReadBits( 8 );
if ( headBytes == bytes )
return;
assert( GetAlignBits() == 0 );
int numWords = ( bytes - headBytes ) / 4;
if ( numWords > 0 )
{
assert( ( m_bitsRead % 32 ) == 0 );
memcpy( data + headBytes, &m_data[m_wordIndex], numWords * 4 );
m_bitsRead += numWords * 32;
m_wordIndex += numWords;
m_scratchBits = 0;
}
assert( GetAlignBits() == 0 );
int tailStart = headBytes + numWords * 4;
int tailBytes = bytes - tailStart;
assert( tailBytes >= 0 && tailBytes < 4 );
for ( int i = 0; i < tailBytes; ++i )
data[tailStart+i] = (uint8_t) ReadBits( 8 );
assert( GetAlignBits() == 0 );
assert( headBytes + numWords * 4 + tailBytes == bytes );
}
/**
How many align bits would be read, if we were to read an align right now?
@returns Result in [0,7], where 0 is zero bits required to align (already aligned) and 7 is worst case.
*/
int GetAlignBits() const
{
return ( 8 - m_bitsRead % 8 ) % 8;
}
/**
How many bits have we read so far?
@returns The number of bits read from the bit buffer so far.
*/
int GetBitsRead() const
{
return m_bitsRead;
}
/**
How many bits are still available to read?
For example, if the buffer size is 4, we have 32 bits available to read, if we have already written 10 bytes then 22 are still available.
@returns The number of bits available to read.
*/
int GetBitsRemaining() const
{
return m_numBits - m_bitsRead;
}
private:
const uint32_t * m_data; ///< The bitpacked data we're reading as a dword array.
uint64_t m_scratch; ///< The scratch value. New data is read in 32 bits at a top to the left of this buffer, and data is read off to the right.
int m_numBits; ///< Number of bits to read in the buffer. Of course, we can't *really* know this so it's actually m_numBytes * 8.
int m_numBytes; ///< Number of bytes to read in the buffer. We know this, and this is the non-rounded up version.
#ifndef NDEBUG
int m_numWords; ///< Number of words to read in the buffer. This is rounded up to the next word if necessary.
#endif // #ifndef NDEBUG
int m_bitsRead; ///< Number of bits read from the buffer so far.
int m_scratchBits; ///< Number of bits currently in the scratch value. If the user wants to read more bits than this, we have to go fetch another dword from memory.
int m_wordIndex; ///< Index of the next word to read from memory.
};
}
#endif // #ifndef YOJIMBO_BITPACK_H