Coverage Report

Created: 2026-10-19 14:15

src/zserio/BitStreamReader.cpp
Line
Count
Source (jump to first uncovered line)
1
#include <array>
2
#include <limits>
3
4
#include "zserio/BitStreamReader.h"
5
#include "zserio/CppRuntimeException.h"
6
#include "zserio/FloatUtil.h"
7
#include "zserio/RuntimeArch.h"
8
9
namespace zserio
10
{
11
12
namespace
13
{
14
15
// max size calculated to prevent "bitIndex + numBits" overflows
16
const size_t MAX_BUFFER_SIZE = std::numeric_limits<size_t>::max() / 8 - sizeof(size_t);
17
18
using BitPosType = BitStreamReader::BitPosType;
19
using ReaderContext = BitStreamReader::ReaderContext;
20
21
#ifdef ZSERIO_RUNTIME_64BIT
22
using BaseType = uint64_t;
23
using BaseSignedType = int64_t;
24
#else
25
using BaseType = uint32_t;
26
using BaseSignedType = int32_t;
27
#endif
28
29
#ifdef ZSERIO_RUNTIME_64BIT
30
const std::array<BaseType, 65> MASK_TABLE = {
31
        UINT64_C(0x00),
32
        UINT64_C(0x0001),
33
        UINT64_C(0x0003),
34
        UINT64_C(0x0007),
35
        UINT64_C(0x000f),
36
        UINT64_C(0x001f),
37
        UINT64_C(0x003f),
38
        UINT64_C(0x007f),
39
        UINT64_C(0x00ff),
40
        UINT64_C(0x01ff),
41
        UINT64_C(0x03ff),
42
        UINT64_C(0x07ff),
43
        UINT64_C(0x0fff),
44
        UINT64_C(0x1fff),
45
        UINT64_C(0x3fff),
46
        UINT64_C(0x7fff),
47
        UINT64_C(0xffff),
48
        UINT64_C(0x0001ffff),
49
        UINT64_C(0x0003ffff),
50
        UINT64_C(0x0007ffff),
51
        UINT64_C(0x000fffff),
52
        UINT64_C(0x001fffff),
53
        UINT64_C(0x003fffff),
54
        UINT64_C(0x007fffff),
55
        UINT64_C(0x00ffffff),
56
        UINT64_C(0x01ffffff),
57
        UINT64_C(0x03ffffff),
58
        UINT64_C(0x07ffffff),
59
        UINT64_C(0x0fffffff),
60
        UINT64_C(0x1fffffff),
61
        UINT64_C(0x3fffffff),
62
        UINT64_C(0x7fffffff),
63
        UINT64_C(0xffffffff),
64
65
        UINT64_C(0x00000001ffffffff),
66
        UINT64_C(0x00000003ffffffff),
67
        UINT64_C(0x00000007ffffffff),
68
        UINT64_C(0x0000000fffffffff),
69
        UINT64_C(0x0000001fffffffff),
70
        UINT64_C(0x0000003fffffffff),
71
        UINT64_C(0x0000007fffffffff),
72
        UINT64_C(0x000000ffffffffff),
73
        UINT64_C(0x000001ffffffffff),
74
        UINT64_C(0x000003ffffffffff),
75
        UINT64_C(0x000007ffffffffff),
76
        UINT64_C(0x00000fffffffffff),
77
        UINT64_C(0x00001fffffffffff),
78
        UINT64_C(0x00003fffffffffff),
79
        UINT64_C(0x00007fffffffffff),
80
        UINT64_C(0x0000ffffffffffff),
81
        UINT64_C(0x0001ffffffffffff),
82
        UINT64_C(0x0003ffffffffffff),
83
        UINT64_C(0x0007ffffffffffff),
84
        UINT64_C(0x000fffffffffffff),
85
        UINT64_C(0x001fffffffffffff),
86
        UINT64_C(0x003fffffffffffff),
87
        UINT64_C(0x007fffffffffffff),
88
        UINT64_C(0x00ffffffffffffff),
89
        UINT64_C(0x01ffffffffffffff),
90
        UINT64_C(0x03ffffffffffffff),
91
        UINT64_C(0x07ffffffffffffff),
92
        UINT64_C(0x0fffffffffffffff),
93
        UINT64_C(0x1fffffffffffffff),
94
        UINT64_C(0x3fffffffffffffff),
95
        UINT64_C(0x7fffffffffffffff),
96
        UINT64_C(0xffffffffffffffff),
97
};
98
#else
99
const std::array<BaseType, 33> MASK_TABLE = {
100
        UINT32_C(0x00),
101
        UINT32_C(0x0001),
102
        UINT32_C(0x0003),
103
        UINT32_C(0x0007),
104
        UINT32_C(0x000f),
105
        UINT32_C(0x001f),
106
        UINT32_C(0x003f),
107
        UINT32_C(0x007f),
108
        UINT32_C(0x00ff),
109
        UINT32_C(0x01ff),
110
        UINT32_C(0x03ff),
111
        UINT32_C(0x07ff),
112
        UINT32_C(0x0fff),
113
        UINT32_C(0x1fff),
114
        UINT32_C(0x3fff),
115
        UINT32_C(0x7fff),
116
        UINT32_C(0xffff),
117
        UINT32_C(0x0001ffff),
118
        UINT32_C(0x0003ffff),
119
        UINT32_C(0x0007ffff),
120
        UINT32_C(0x000fffff),
121
        UINT32_C(0x001fffff),
122
        UINT32_C(0x003fffff),
123
        UINT32_C(0x007fffff),
124
        UINT32_C(0x00ffffff),
125
        UINT32_C(0x01ffffff),
126
        UINT32_C(0x03ffffff),
127
        UINT32_C(0x07ffffff),
128
        UINT32_C(0x0fffffff),
129
        UINT32_C(0x1fffffff),
130
        UINT32_C(0x3fffffff),
131
        UINT32_C(0x7fffffff),
132
        UINT32_C(0xffffffff),
133
};
134
#endif
135
136
const uint8_t VARINT_SIGN_1 = UINT8_C(0x80);
137
const uint8_t VARINT_BYTE_1 = UINT8_C(0x3f);
138
const uint8_t VARINT_BYTE_N = UINT8_C(0x7f);
139
const uint8_t VARINT_HAS_NEXT_1 = UINT8_C(0x40);
140
const uint8_t VARINT_HAS_NEXT_N = UINT8_C(0x80);
141
142
const uint8_t VARUINT_BYTE = UINT8_C(0x7f);
143
const uint8_t VARUINT_HAS_NEXT = UINT8_C(0x80);
144
145
const uint32_t VARSIZE_MAX_VALUE = (UINT32_C(1) << 31U) - 1;
146
147
#ifdef ZSERIO_RUNTIME_64BIT
148
inline BaseType parse64(Span<const uint8_t>::const_iterator bufferIt)
149
12.2k
{
150
12.2k
    return static_cast<BaseType>(*bufferIt) << 56U | static_cast<BaseType>(*(bufferIt + 1)) << 48U |
151
12.2k
            static_cast<BaseType>(*(bufferIt + 2)) << 40U | static_cast<BaseType>(*(bufferIt + 3)) << 32U |
152
12.2k
            static_cast<BaseType>(*(bufferIt + 4)) << 24U | static_cast<BaseType>(*(bufferIt + 5)) << 16U |
153
12.2k
            static_cast<BaseType>(*(bufferIt + 6)) << 8U | static_cast<BaseType>(*(bufferIt + 7));
154
12.2k
}
155
156
inline BaseType parse56(Span<const uint8_t>::const_iterator bufferIt)
157
362
{
158
362
    return static_cast<BaseType>(*bufferIt) << 48U | static_cast<BaseType>(*(bufferIt + 1)) << 40U |
159
362
            static_cast<BaseType>(*(bufferIt + 2)) << 32U | static_cast<BaseType>(*(bufferIt + 3)) << 24U |
160
362
            static_cast<BaseType>(*(bufferIt + 4)) << 16U | static_cast<BaseType>(*(bufferIt + 5)) << 8U |
161
362
            static_cast<BaseType>(*(bufferIt + 6));
162
362
}
163
164
inline BaseType parse48(Span<const uint8_t>::const_iterator bufferIt)
165
343
{
166
343
    return static_cast<BaseType>(*bufferIt) << 40U | static_cast<BaseType>(*(bufferIt + 1)) << 32U |
167
343
            static_cast<BaseType>(*(bufferIt + 2)) << 24U | static_cast<BaseType>(*(bufferIt + 3)) << 16U |
168
343
            static_cast<BaseType>(*(bufferIt + 4)) << 8U | static_cast<BaseType>(*(bufferIt + 5));
169
343
}
170
171
inline BaseType parse40(Span<const uint8_t>::const_iterator bufferIt)
172
480
{
173
480
    return static_cast<BaseType>(*bufferIt) << 32U | static_cast<BaseType>(*(bufferIt + 1)) << 24U |
174
480
            static_cast<BaseType>(*(bufferIt + 2)) << 16U | static_cast<BaseType>(*(bufferIt + 3)) << 8U |
175
480
            static_cast<BaseType>(*(bufferIt + 4));
176
480
}
177
#endif
178
inline BaseType parse32(Span<const uint8_t>::const_iterator bufferIt)
179
664
{
180
664
    return static_cast<BaseType>(*bufferIt) << 24U | static_cast<BaseType>(*(bufferIt + 1)) << 16U |
181
664
            static_cast<BaseType>(*(bufferIt + 2)) << 8U | static_cast<BaseType>(*(bufferIt + 3));
182
664
}
183
184
inline BaseType parse24(Span<const uint8_t>::const_iterator bufferIt)
185
368
{
186
368
    return static_cast<BaseType>(*bufferIt) << 16U | static_cast<BaseType>(*(bufferIt + 1)) << 8U |
187
368
            static_cast<BaseType>(*(bufferIt + 2));
188
368
}
189
190
inline BaseType parse16(Span<const uint8_t>::const_iterator bufferIt)
191
810
{
192
810
    return static_cast<BaseType>(*bufferIt) << 8U | static_cast<BaseType>(*(bufferIt + 1));
193
810
}
194
195
inline BaseType parse8(Span<const uint8_t>::const_iterator bufferIt)
196
1.01k
{
197
1.01k
    return static_cast<BaseType>(*bufferIt);
198
1.01k
}
199
200
/** Optimization which increases chances to inline checkNumBits and checkNumBits64. */
201
inline void throwNumBitsIsNotValid(uint8_t numBits)
202
12
{
203
12
    throw CppRuntimeException("BitStreamReader: ReadBits #")
204
12
            << numBits << " is not valid, reading from stream failed!";
205
12
}
206
207
/** Checks numBits validity for 32-bit reads. */
208
inline void checkNumBits(uint8_t numBits)
209
10.5k
{
210
10.5k
    if (numBits == 0 || 
numBits > 3210.5k
)
211
6
    {
212
6
        throwNumBitsIsNotValid(numBits);
213
6
    }
214
10.5k
}
215
216
/** Checks numBits validity for 64-bit reads. */
217
inline void checkNumBits64(uint8_t numBits)
218
6.76k
{
219
6.76k
    if (numBits == 0 || 
numBits > 646.76k
)
220
6
    {
221
6
        throwNumBitsIsNotValid(numBits);
222
6
    }
223
6.76k
}
224
225
/** Optimization which increases chances to inline loadCacheNext. */
226
inline void throwEof()
227
284
{
228
284
    throw CppRuntimeException("BitStreamReader: Reached eof(), reading from stream failed!");
229
284
}
230
231
/** Loads next 32/64 bits to 32/64 bit-cache. */
232
inline void loadCacheNext(ReaderContext& ctx, uint8_t numBits)
233
16.5k
{
234
16.5k
    static const uint8_t cacheBitSize = sizeof(BaseType) * 8;
235
236
    // ctx.bitIndex is always byte aligned and ctx.cacheNumBits is always zero in this call
237
16.5k
    const size_t byteIndex = ctx.bitIndex >> 3U;
238
16.5k
    if (ctx.bufferBitSize >= ctx.bitIndex + cacheBitSize)
239
11.9k
    {
240
11.9k
        ctx.cache =
241
11.9k
#ifdef ZSERIO_RUNTIME_64BIT
242
11.9k
                parse64(ctx.buffer.begin() + byteIndex);
243
#else
244
                parse32(ctx.buffer.begin() + byteIndex);
245
#endif
246
11.9k
        ctx.cacheNumBits = cacheBitSize;
247
11.9k
    }
248
4.59k
    else
249
4.59k
    {
250
4.59k
        if (ctx.bitIndex + numBits > ctx.bufferBitSize)
251
284
        {
252
284
            throwEof();
253
284
        }
254
255
4.59k
        ctx.cacheNumBits = static_cast<uint8_t>(ctx.bufferBitSize - ctx.bitIndex);
256
257
        // buffer must be always available in full bytes, even if some last bits are not used
258
4.59k
        const uint8_t alignedNumBits = static_cast<uint8_t>((ctx.cacheNumBits + 7U) & ~0x7U);
259
260
4.59k
        switch (alignedNumBits)
261
4.59k
        {
262
0
#ifdef ZSERIO_RUNTIME_64BIT
263
263
        case 64:
264
263
            ctx.cache = parse64(ctx.buffer.begin() + byteIndex);
265
263
            break;
266
362
        case 56:
267
362
            ctx.cache = parse56(ctx.buffer.begin() + byteIndex);
268
362
            break;
269
343
        case 48:
270
343
            ctx.cache = parse48(ctx.buffer.begin() + byteIndex);
271
343
            break;
272
480
        case 40:
273
480
            ctx.cache = parse40(ctx.buffer.begin() + byteIndex);
274
480
            break;
275
0
#endif
276
664
        case 32:
277
664
            ctx.cache = parse32(ctx.buffer.begin() + byteIndex);
278
664
            break;
279
368
        case 24:
280
368
            ctx.cache = parse24(ctx.buffer.begin() + byteIndex);
281
368
            break;
282
810
        case 16:
283
810
            ctx.cache = parse16(ctx.buffer.begin() + byteIndex);
284
810
            break;
285
1.01k
        default: // 8
286
1.01k
            ctx.cache = parse8(ctx.buffer.begin() + byteIndex);
287
1.01k
            break;
288
4.59k
        }
289
290
4.30k
        ctx.cache >>= static_cast<uint8_t>(alignedNumBits - ctx.cacheNumBits);
291
4.30k
    }
292
16.5k
}
293
294
/** Unchecked implementation of readBits. */
295
inline BaseType readBitsImpl(ReaderContext& ctx, uint8_t numBits)
296
77.1k
{
297
77.1k
    BaseType value = 0;
298
77.1k
    if (ctx.cacheNumBits < numBits)
299
16.5k
    {
300
        // read all remaining cache bits
301
16.5k
        value = ctx.cache & MASK_TABLE[ctx.cacheNumBits];
302
16.5k
        ctx.bitIndex += ctx.cacheNumBits;
303
16.5k
        numBits = static_cast<uint8_t>(numBits - ctx.cacheNumBits);
304
305
        // load next piece of buffer into cache
306
16.5k
        loadCacheNext(ctx, numBits);
307
308
        // add the remaining bits to the result
309
        // if numBits is sizeof(BaseType) * 8 here, value is already 0 (see MASK_TABLE[0])
310
16.5k
        if (numBits < sizeof(BaseType) * 8)
311
16.2k
        {
312
16.2k
            value <<= numBits;
313
16.2k
        }
314
16.5k
    }
315
77.1k
    value |= ((ctx.cache >> static_cast<uint8_t>(ctx.cacheNumBits - numBits)) & MASK_TABLE[numBits]);
316
77.1k
    ctx.cacheNumBits = static_cast<uint8_t>(ctx.cacheNumBits - numBits);
317
77.1k
    ctx.bitIndex += numBits;
318
319
77.1k
    return value;
320
77.1k
}
321
322
/** Unchecked version of readSignedBits. */
323
inline BaseSignedType readSignedBitsImpl(ReaderContext& ctx, uint8_t numBits)
324
5.10k
{
325
5.10k
    static const uint8_t typeSize = sizeof(BaseSignedType) * 8;
326
5.10k
    BaseType value = readBitsImpl(ctx, numBits);
327
328
    // Skip the signed overflow correction if numBits == typeSize.
329
    // In that case, the value that comes out the readBits function
330
    // is already correct.
331
5.10k
    if (numBits != 0 && 
numBits < typeSize5.03k
&&
332
5.10k
            
(value >= (static_cast<BaseType>(1) << static_cast<uint8_t>(numBits - 1)))3.78k
)
333
693
    {
334
693
        value -= static_cast<BaseType>(1) << numBits;
335
693
    }
336
337
5.10k
    return static_cast<BaseSignedType>(value);
338
5.10k
}
339
340
#ifndef ZSERIO_RUNTIME_64BIT
341
/** Unchecked implementation of readBits64. Always reads > 32bit! */
342
inline uint64_t readBits64Impl(ReaderContext& ctx, uint8_t numBits)
343
{
344
    // read the first 32 bits
345
    numBits = static_cast<uint8_t>(numBits - 32);
346
    uint64_t value = readBitsImpl(ctx, 32);
347
348
    // add the remaining bits
349
    value <<= numBits;
350
    value |= readBitsImpl(ctx, numBits);
351
352
    return value;
353
}
354
#endif
355
356
} // namespace
357
358
BitStreamReader::ReaderContext::ReaderContext(
359
        Span<const uint8_t> readBuffer, size_t readBufferBitSize, size_t maxArrayPrealloc) :
360
        buffer(readBuffer),
361
        bufferBitSize(readBufferBitSize),
362
        cache(0),
363
        cacheNumBits(0),
364
        bitIndex(0),
365
        maxArrayPreallocation(maxArrayPrealloc)
366
4.65k
{
367
4.65k
    if (buffer.size() > MAX_BUFFER_SIZE)
368
1
    {
369
1
        throw CppRuntimeException("BitStreamReader: Buffer size exceeded limit '")
370
1
                << MAX_BUFFER_SIZE << "' bytes!";
371
1
    }
372
4.65k
}
373
374
BitStreamReader::BitStreamReader(
375
        const uint8_t* buffer, size_t bufferByteSize, ArrayPreallocation maxArrayPrealloc) :
376
        BitStreamReader(Span<const uint8_t>(buffer, bufferByteSize), maxArrayPrealloc)
377
414
{}
378
379
BitStreamReader::BitStreamReader(Span<const uint8_t> buffer, ArrayPreallocation maxArrayPrealloc) :
380
        m_context(buffer, buffer.size() * 8, maxArrayPrealloc)
381
433
{}
382
383
BitStreamReader::BitStreamReader(
384
        Span<const uint8_t> buffer, size_t bufferBitSize, ArrayPreallocation maxArrayPrealloc) :
385
        m_context(buffer, bufferBitSize, maxArrayPrealloc)
386
398
{
387
398
    if (buffer.size() < (bufferBitSize + 7) / 8)
388
1
    {
389
1
        throw CppRuntimeException("BitStreamReader: Wrong buffer bit size ('")
390
1
                << buffer.size() << "' < '" << (bufferBitSize + 7) / 8 << "')!";
391
1
    }
392
398
}
393
394
BitStreamReader::BitStreamReader(
395
        const uint8_t* buffer, size_t bufferBitSize, BitsTag, ArrayPreallocation maxArrayPrealloc) :
396
        m_context(Span<const uint8_t>(buffer, (bufferBitSize + 7) / 8), bufferBitSize, maxArrayPrealloc)
397
3.82k
{}
398
399
uint32_t BitStreamReader::readBits(uint8_t numBits)
400
8.12k
{
401
8.12k
    checkNumBits(numBits);
402
403
8.12k
    return static_cast<uint32_t>(readBitsImpl(m_context, numBits));
404
8.12k
}
405
406
uint64_t BitStreamReader::readBits64(uint8_t numBits)
407
4.04k
{
408
4.04k
    checkNumBits64(numBits);
409
410
4.04k
#ifdef ZSERIO_RUNTIME_64BIT
411
4.04k
    return readBitsImpl(m_context, numBits);
412
#else
413
    if (numBits <= 32)
414
    {
415
        return readBitsImpl(m_context, numBits);
416
    }
417
418
    return readBits64Impl(m_context, numBits);
419
#endif
420
4.04k
}
421
422
int64_t BitStreamReader::readSignedBits64(uint8_t numBits)
423
2.72k
{
424
2.72k
    checkNumBits64(numBits);
425
426
2.72k
#ifdef ZSERIO_RUNTIME_64BIT
427
2.72k
    return readSignedBitsImpl(m_context, numBits);
428
#else
429
    if (numBits <= 32)
430
    {
431
        return readSignedBitsImpl(m_context, numBits);
432
    }
433
434
    int64_t value = static_cast<int64_t>(readBits64Impl(m_context, numBits));
435
436
    // Skip the signed overflow correction if numBits == 64.
437
    // In that case, the value that comes out the readBits function
438
    // is already correct.
439
    const bool needsSignExtension =
440
            numBits < 64 && (static_cast<uint64_t>(value) >= (UINT64_C(1) << (numBits - 1)));
441
    if (needsSignExtension)
442
    {
443
        value = static_cast<int64_t>(static_cast<uint64_t>(value) - (UINT64_C(1) << numBits));
444
    }
445
446
    return value;
447
#endif
448
2.72k
}
449
450
int32_t BitStreamReader::readSignedBits(uint8_t numBits)
451
2.38k
{
452
2.38k
    checkNumBits(numBits);
453
454
2.38k
    return static_cast<int32_t>(readSignedBitsImpl(m_context, numBits));
455
2.38k
}
456
457
int64_t BitStreamReader::readVarInt64()
458
3.12k
{
459
3.12k
    uint8_t byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 1
460
3.12k
    const bool sign = (byte & VARINT_SIGN_1) != 0;
461
3.12k
    uint64_t result = byte & VARINT_BYTE_1;
462
3.12k
    if ((byte & VARINT_HAS_NEXT_1) == 0)
463
1.11k
    {
464
1.11k
        return sign ? 
-static_cast<int64_t>(result)63
:
static_cast<int64_t>(result)1.05k
;
465
1.11k
    }
466
467
2.00k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 2
468
2.00k
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
469
2.00k
    if ((byte & VARINT_HAS_NEXT_N) == 0)
470
284
    {
471
284
        return sign ? 
-static_cast<int64_t>(result)63
:
static_cast<int64_t>(result)221
;
472
284
    }
473
474
1.72k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 3
475
1.72k
    result = static_cast<uint64_t>(result) << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
476
1.72k
    if ((byte & VARINT_HAS_NEXT_N) == 0)
477
284
    {
478
284
        return sign ? 
-static_cast<int64_t>(result)63
:
static_cast<int64_t>(result)221
;
479
284
    }
480
481
1.44k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 4
482
1.44k
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
483
1.44k
    if ((byte & VARINT_HAS_NEXT_N) == 0)
484
297
    {
485
297
        return sign ? 
-static_cast<int64_t>(result)72
:
static_cast<int64_t>(result)225
;
486
297
    }
487
488
1.14k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 5
489
1.14k
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
490
1.14k
    if ((byte & VARINT_HAS_NEXT_N) == 0)
491
284
    {
492
284
        return sign ? 
-static_cast<int64_t>(result)63
:
static_cast<int64_t>(result)221
;
493
284
    }
494
495
860
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 6
496
860
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
497
860
    if ((byte & VARINT_HAS_NEXT_N) == 0)
498
284
    {
499
284
        return sign ? 
-static_cast<int64_t>(result)63
:
static_cast<int64_t>(result)221
;
500
284
    }
501
502
576
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 7
503
576
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
504
576
    if ((byte & VARINT_HAS_NEXT_N) == 0)
505
284
    {
506
284
        return sign ? 
-static_cast<int64_t>(result)63
:
static_cast<int64_t>(result)221
;
507
284
    }
508
509
292
    result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 8
510
292
    return sign ? 
-static_cast<int64_t>(result)63
:
static_cast<int64_t>(result)229
;
511
576
}
512
513
int32_t BitStreamReader::readVarInt32()
514
1.44k
{
515
1.44k
    uint8_t byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 1
516
1.44k
    const bool sign = (byte & VARINT_SIGN_1) != 0;
517
1.44k
    uint32_t result = byte & VARINT_BYTE_1;
518
1.44k
    if ((byte & VARINT_HAS_NEXT_1) == 0)
519
955
    {
520
955
        return sign ? 
-static_cast<int32_t>(result)31
:
static_cast<int32_t>(result)924
;
521
955
    }
522
523
489
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 2
524
489
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
525
489
    if ((byte & VARINT_HAS_NEXT_N) == 0)
526
156
    {
527
156
        return sign ? 
-static_cast<int32_t>(result)31
:
static_cast<int32_t>(result)125
;
528
156
    }
529
530
333
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 3
531
333
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
532
333
    if ((byte & VARINT_HAS_NEXT_N) == 0)
533
165
    {
534
165
        return sign ? 
-static_cast<int32_t>(result)35
:
static_cast<int32_t>(result)130
;
535
165
    }
536
537
168
    result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 4
538
168
    return sign ? 
-static_cast<int32_t>(result)35
:
static_cast<int32_t>(result)133
;
539
333
}
540
541
int16_t BitStreamReader::readVarInt16()
542
992
{
543
992
    uint8_t byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 1
544
992
    const bool sign = (byte & VARINT_SIGN_1) != 0;
545
992
    uint16_t result = byte & VARINT_BYTE_1;
546
992
    if ((byte & VARINT_HAS_NEXT_1) == 0)
547
883
    {
548
883
        return sign ? 
static_cast<int16_t>(-result)19
:
static_cast<int16_t>(result)864
;
549
883
    }
550
551
109
    result = static_cast<uint16_t>(result << 8U);
552
109
    result = static_cast<uint16_t>(result | readBitsImpl(m_context, 8)); // byte 2
553
109
    return sign ? 
static_cast<int16_t>(-result)24
:
static_cast<int16_t>(result)85
;
554
992
}
555
556
uint64_t BitStreamReader::readVarUInt64()
557
3.11k
{
558
3.11k
    uint8_t byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 1
559
3.11k
    uint64_t result = byte & VARUINT_BYTE;
560
3.11k
    if ((byte & VARUINT_HAS_NEXT) == 0)
561
1.59k
    {
562
1.59k
        return result;
563
1.59k
    }
564
565
1.51k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 2
566
1.51k
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
567
1.51k
    if ((byte & VARUINT_HAS_NEXT) == 0)
568
192
    {
569
192
        return result;
570
192
    }
571
572
1.32k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 3
573
1.32k
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
574
1.32k
    if ((byte & VARUINT_HAS_NEXT) == 0)
575
322
    {
576
322
        return result;
577
322
    }
578
579
1.00k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 4
580
1.00k
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
581
1.00k
    if ((byte & VARUINT_HAS_NEXT) == 0)
582
226
    {
583
226
        return result;
584
226
    }
585
586
777
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 5
587
777
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
588
777
    if ((byte & VARUINT_HAS_NEXT) == 0)
589
195
    {
590
195
        return result;
591
195
    }
592
593
582
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 6
594
582
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
595
582
    if ((byte & VARUINT_HAS_NEXT) == 0)
596
190
    {
597
190
        return result;
598
190
    }
599
600
392
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 7
601
392
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
602
392
    if ((byte & VARUINT_HAS_NEXT) == 0)
603
190
    {
604
190
        return result;
605
190
    }
606
607
202
    result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 8
608
202
    return result;
609
392
}
610
611
uint32_t BitStreamReader::readVarUInt32()
612
1.25k
{
613
1.25k
    uint8_t byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 1
614
1.25k
    uint32_t result = byte & VARUINT_BYTE;
615
1.25k
    if ((byte & VARUINT_HAS_NEXT) == 0)
616
893
    {
617
893
        return result;
618
893
    }
619
620
365
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 2
621
365
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
622
365
    if ((byte & VARUINT_HAS_NEXT) == 0)
623
96
    {
624
96
        return result;
625
96
    }
626
627
269
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 3
628
269
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
629
269
    if ((byte & VARUINT_HAS_NEXT) == 0)
630
162
    {
631
162
        return result;
632
162
    }
633
634
107
    result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 4
635
107
    return result;
636
269
}
637
638
uint16_t BitStreamReader::readVarUInt16()
639
962
{
640
962
    uint8_t byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 1
641
962
    uint16_t result = byte & VARUINT_BYTE;
642
962
    if ((byte & VARUINT_HAS_NEXT) == 0)
643
868
    {
644
868
        return result;
645
868
    }
646
647
94
    result = static_cast<uint16_t>(result << 8U);
648
94
    result = static_cast<uint16_t>(result | readBitsImpl(m_context, 8)); // byte 2
649
94
    return result;
650
962
}
651
652
int64_t BitStreamReader::readVarInt()
653
3.88k
{
654
3.88k
    uint8_t byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 1
655
3.88k
    const bool sign = (byte & VARINT_SIGN_1) != 0;
656
3.88k
    uint64_t result = byte & VARINT_BYTE_1;
657
3.88k
    if ((byte & VARINT_HAS_NEXT_1) == 0)
658
1.33k
    {
659
1.33k
        return sign ? 
(285
result == 0285
? INT64_MIN :
-static_cast<int64_t>(result)190
) :
static_cast<int64_t>(result)1.05k
;
660
1.33k
    }
661
662
2.54k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 2
663
2.54k
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
664
2.54k
    if ((byte & VARINT_HAS_NEXT_N) == 0)
665
316
    {
666
316
        return sign ? 
-static_cast<int64_t>(result)158
:
static_cast<int64_t>(result)158
;
667
316
    }
668
669
2.23k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 3
670
2.23k
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
671
2.23k
    if ((byte & VARINT_HAS_NEXT_N) == 0)
672
316
    {
673
316
        return sign ? 
-static_cast<int64_t>(result)158
:
static_cast<int64_t>(result)158
;
674
316
    }
675
676
1.91k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 4
677
1.91k
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
678
1.91k
    if ((byte & VARINT_HAS_NEXT_N) == 0)
679
328
    {
680
328
        return sign ? 
-static_cast<int64_t>(result)166
:
static_cast<int64_t>(result)162
;
681
328
    }
682
683
1.58k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 5
684
1.58k
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
685
1.58k
    if ((byte & VARINT_HAS_NEXT_N) == 0)
686
316
    {
687
316
        return sign ? 
-static_cast<int64_t>(result)158
:
static_cast<int64_t>(result)158
;
688
316
    }
689
690
1.27k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 6
691
1.27k
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
692
1.27k
    if ((byte & VARINT_HAS_NEXT_N) == 0)
693
316
    {
694
316
        return sign ? 
-static_cast<int64_t>(result)158
:
static_cast<int64_t>(result)158
;
695
316
    }
696
697
957
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 7
698
957
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
699
957
    if ((byte & VARINT_HAS_NEXT_N) == 0)
700
316
    {
701
316
        return sign ? 
-static_cast<int64_t>(result)158
:
static_cast<int64_t>(result)158
;
702
316
    }
703
704
641
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 8
705
641
    result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
706
641
    if ((byte & VARINT_HAS_NEXT_N) == 0)
707
316
    {
708
316
        return sign ? 
-static_cast<int64_t>(result)158
:
static_cast<int64_t>(result)158
;
709
316
    }
710
711
325
    result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 9
712
325
    return sign ? 
-static_cast<int64_t>(result)158
:
static_cast<int64_t>(result)167
;
713
641
}
714
715
uint64_t BitStreamReader::readVarUInt()
716
2.33k
{
717
2.33k
    uint8_t byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 1
718
2.33k
    uint64_t result = byte & VARUINT_BYTE;
719
2.33k
    if ((byte & VARUINT_HAS_NEXT) == 0)
720
1.05k
    {
721
1.05k
        return result;
722
1.05k
    }
723
724
1.28k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 2
725
1.28k
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
726
1.28k
    if ((byte & VARUINT_HAS_NEXT) == 0)
727
160
    {
728
160
        return result;
729
160
    }
730
731
1.12k
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 3
732
1.12k
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
733
1.12k
    if ((byte & VARUINT_HAS_NEXT) == 0)
734
164
    {
735
164
        return result;
736
164
    }
737
738
961
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 4
739
961
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
740
961
    if ((byte & VARUINT_HAS_NEXT) == 0)
741
162
    {
742
162
        return result;
743
162
    }
744
745
799
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 5
746
799
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
747
799
    if ((byte & VARUINT_HAS_NEXT) == 0)
748
158
    {
749
158
        return result;
750
158
    }
751
752
641
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 6
753
641
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
754
641
    if ((byte & VARUINT_HAS_NEXT) == 0)
755
158
    {
756
158
        return result;
757
158
    }
758
759
483
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 7
760
483
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
761
483
    if ((byte & VARUINT_HAS_NEXT) == 0)
762
158
    {
763
158
        return result;
764
158
    }
765
766
325
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 8
767
325
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
768
325
    if ((byte & VARUINT_HAS_NEXT) == 0)
769
158
    {
770
158
        return result;
771
158
    }
772
773
167
    result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 9
774
167
    return result;
775
325
}
776
777
uint32_t BitStreamReader::readVarSize()
778
5.01k
{
779
5.01k
    uint8_t byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 1
780
5.01k
    uint32_t result = byte & VARUINT_BYTE;
781
5.01k
    if ((byte & VARUINT_HAS_NEXT) == 0)
782
4.19k
    {
783
4.19k
        return result;
784
4.19k
    }
785
786
815
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 2
787
815
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
788
815
    if ((byte & VARUINT_HAS_NEXT) == 0)
789
128
    {
790
128
        return result;
791
128
    }
792
793
687
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 3
794
687
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
795
687
    if ((byte & VARUINT_HAS_NEXT) == 0)
796
194
    {
797
194
        return result;
798
194
    }
799
800
493
    byte = static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 4
801
493
    result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
802
493
    if ((byte & VARUINT_HAS_NEXT) == 0)
803
340
    {
804
340
        return result;
805
340
    }
806
807
153
    result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8)); // byte 5
808
153
    if (result > VARSIZE_MAX_VALUE)
809
4
    {
810
4
        throw CppRuntimeException("BitStreamReader: Read value '")
811
4
                << result << "' is out of range for varsize type!";
812
4
    }
813
814
149
    return result;
815
153
}
816
817
float BitStreamReader::readFloat16()
818
215
{
819
215
    const uint16_t halfPrecisionFloatValue = static_cast<uint16_t>(readBitsImpl(m_context, 16));
820
821
215
    return convertUInt16ToFloat(halfPrecisionFloatValue);
822
215
}
823
824
float BitStreamReader::readFloat32()
825
313
{
826
313
    const uint32_t singlePrecisionFloatValue = static_cast<uint32_t>(readBitsImpl(m_context, 32));
827
828
313
    return convertUInt32ToFloat(singlePrecisionFloatValue);
829
313
}
830
831
double BitStreamReader::readFloat64()
832
493
{
833
493
#ifdef ZSERIO_RUNTIME_64BIT
834
493
    const uint64_t doublePrecisionFloatValue = static_cast<uint64_t>(readBitsImpl(m_context, 64));
835
#else
836
    const uint64_t doublePrecisionFloatValue = readBits64Impl(m_context, 64);
837
#endif
838
839
493
    return convertUInt64ToDouble(doublePrecisionFloatValue);
840
493
}
841
842
bool BitStreamReader::readBool()
843
1.01k
{
844
1.01k
    return readBitsImpl(m_context, 1) != 0;
845
1.01k
}
846
847
void BitStreamReader::setBitPosition(BitPosType position)
848
1.34k
{
849
1.34k
    if (position > m_context.bufferBitSize)
850
1
    {
851
1
        throw CppRuntimeException("BitStreamReader: Reached eof(), setting of bit position failed!");
852
1
    }
853
854
1.34k
    m_context.bitIndex = (position / 8) * 8; // set to byte aligned position
855
1.34k
    m_context.cacheNumBits = 0; // invalidate cache
856
1.34k
    const uint8_t skip = static_cast<uint8_t>(position - m_context.bitIndex);
857
1.34k
    if (skip != 0)
858
1
    {
859
1
        (void)readBits(skip);
860
1
    }
861
1.34k
}
862
863
void BitStreamReader::alignTo(size_t alignment)
864
11.1k
{
865
11.1k
    const BitPosType offset = getBitPosition() % alignment;
866
11.1k
    if (offset != 0)
867
2.62k
    {
868
2.62k
        const uint8_t skip = static_cast<uint8_t>(alignment - offset);
869
2.62k
        (void)readBits64(skip);
870
2.62k
    }
871
11.1k
}
872
873
uint8_t BitStreamReader::readByte()
874
974
{
875
974
    return static_cast<uint8_t>(readBitsImpl(m_context, 8));
876
974
}
877
878
} // namespace zserio