15 const size_t MAX_BUFFER_SIZE = std::numeric_limits<size_t>::max() / 8 - 4;
18 using ReaderContext = BitStreamReader::ReaderContext;
20 #ifdef ZSERIO_RUNTIME_64BIT 21 using BaseType = uint64_t;
22 using BaseSignedType = int64_t;
24 using BaseType = uint32_t;
25 using BaseSignedType = int32_t;
28 #ifdef ZSERIO_RUNTIME_64BIT 29 const std::array<BaseType, 65> MASK_TABLE =
32 UINT64_C(0x0001), UINT64_C(0x0003), UINT64_C(0x0007), UINT64_C(0x000f),
33 UINT64_C(0x001f), UINT64_C(0x003f), UINT64_C(0x007f), UINT64_C(0x00ff),
34 UINT64_C(0x01ff), UINT64_C(0x03ff), UINT64_C(0x07ff), UINT64_C(0x0fff),
35 UINT64_C(0x1fff), UINT64_C(0x3fff), UINT64_C(0x7fff), UINT64_C(0xffff),
36 UINT64_C(0x0001ffff), UINT64_C(0x0003ffff), UINT64_C(0x0007ffff), UINT64_C(0x000fffff),
37 UINT64_C(0x001fffff), UINT64_C(0x003fffff), UINT64_C(0x007fffff), UINT64_C(0x00ffffff),
38 UINT64_C(0x01ffffff), UINT64_C(0x03ffffff), UINT64_C(0x07ffffff), UINT64_C(0x0fffffff),
39 UINT64_C(0x1fffffff), UINT64_C(0x3fffffff), UINT64_C(0x7fffffff), UINT64_C(0xffffffff),
41 UINT64_C(0x00000001ffffffff), UINT64_C(0x00000003ffffffff),
42 UINT64_C(0x00000007ffffffff), UINT64_C(0x0000000fffffffff),
43 UINT64_C(0x0000001fffffffff), UINT64_C(0x0000003fffffffff),
44 UINT64_C(0x0000007fffffffff), UINT64_C(0x000000ffffffffff),
45 UINT64_C(0x000001ffffffffff), UINT64_C(0x000003ffffffffff),
46 UINT64_C(0x000007ffffffffff), UINT64_C(0x00000fffffffffff),
47 UINT64_C(0x00001fffffffffff), UINT64_C(0x00003fffffffffff),
48 UINT64_C(0x00007fffffffffff), UINT64_C(0x0000ffffffffffff),
49 UINT64_C(0x0001ffffffffffff), UINT64_C(0x0003ffffffffffff),
50 UINT64_C(0x0007ffffffffffff), UINT64_C(0x000fffffffffffff),
51 UINT64_C(0x001fffffffffffff), UINT64_C(0x003fffffffffffff),
52 UINT64_C(0x007fffffffffffff), UINT64_C(0x00ffffffffffffff),
53 UINT64_C(0x01ffffffffffffff), UINT64_C(0x03ffffffffffffff),
54 UINT64_C(0x07ffffffffffffff), UINT64_C(0x0fffffffffffffff),
55 UINT64_C(0x1fffffffffffffff), UINT64_C(0x3fffffffffffffff),
56 UINT64_C(0x7fffffffffffffff), UINT64_C(0xffffffffffffffff)
59 const std::array<BaseType, 33> MASK_TABLE =
62 UINT32_C(0x0001), UINT32_C(0x0003), UINT32_C(0x0007), UINT32_C(0x000f),
63 UINT32_C(0x001f), UINT32_C(0x003f), UINT32_C(0x007f), UINT32_C(0x00ff),
64 UINT32_C(0x01ff), UINT32_C(0x03ff), UINT32_C(0x07ff), UINT32_C(0x0fff),
65 UINT32_C(0x1fff), UINT32_C(0x3fff), UINT32_C(0x7fff), UINT32_C(0xffff),
66 UINT32_C(0x0001ffff), UINT32_C(0x0003ffff), UINT32_C(0x0007ffff), UINT32_C(0x000fffff),
67 UINT32_C(0x001fffff), UINT32_C(0x003fffff), UINT32_C(0x007fffff), UINT32_C(0x00ffffff),
68 UINT32_C(0x01ffffff), UINT32_C(0x03ffffff), UINT32_C(0x07ffffff), UINT32_C(0x0fffffff),
69 UINT32_C(0x1fffffff), UINT32_C(0x3fffffff), UINT32_C(0x7fffffff), UINT32_C(0xffffffff)
73 const uint8_t VARINT_SIGN_1 = UINT8_C(0x80);
74 const uint8_t VARINT_BYTE_1 = UINT8_C(0x3f);
75 const uint8_t VARINT_BYTE_N = UINT8_C(0x7f);
76 const uint8_t VARINT_HAS_NEXT_1 = UINT8_C(0x40);
77 const uint8_t VARINT_HAS_NEXT_N = UINT8_C(0x80);
79 const uint8_t VARUINT_BYTE = UINT8_C(0x7f);
80 const uint8_t VARUINT_HAS_NEXT = UINT8_C(0x80);
82 const uint32_t VARSIZE_MAX_VALUE = (UINT32_C(1) << 31U) - 1;
84 #ifdef ZSERIO_RUNTIME_64BIT 87 return static_cast<BaseType
>(*bufferIt) << 56U |
88 static_cast<BaseType
>(*(bufferIt + 1)) << 48U |
89 static_cast<BaseType
>(*(bufferIt + 2)) << 40U |
90 static_cast<BaseType
>(*(bufferIt + 3)) << 32U |
91 static_cast<BaseType
>(*(bufferIt + 4)) << 24U |
92 static_cast<BaseType
>(*(bufferIt + 5)) << 16U |
93 static_cast<BaseType
>(*(bufferIt + 6)) << 8U |
94 static_cast<BaseType
>(*(bufferIt + 7));
99 return static_cast<BaseType
>(*bufferIt) << 48U |
100 static_cast<BaseType
>(*(bufferIt + 1)) << 40U |
101 static_cast<BaseType
>(*(bufferIt + 2)) << 32U |
102 static_cast<BaseType
>(*(bufferIt + 3)) << 24U |
103 static_cast<BaseType
>(*(bufferIt + 4)) << 16U |
104 static_cast<BaseType
>(*(bufferIt + 5)) << 8U |
105 static_cast<BaseType
>(*(bufferIt + 6));
110 return static_cast<BaseType
>(*bufferIt) << 40U |
111 static_cast<BaseType
>(*(bufferIt + 1)) << 32U |
112 static_cast<BaseType
>(*(bufferIt + 2)) << 24U |
113 static_cast<BaseType
>(*(bufferIt + 3)) << 16U |
114 static_cast<BaseType
>(*(bufferIt + 4)) << 8U |
115 static_cast<BaseType
>(*(bufferIt + 5));
120 return static_cast<BaseType
>(*bufferIt) << 32U |
121 static_cast<BaseType
>(*(bufferIt + 1)) << 24U |
122 static_cast<BaseType
>(*(bufferIt + 2)) << 16U |
123 static_cast<BaseType
>(*(bufferIt + 3)) << 8U |
124 static_cast<BaseType
>(*(bufferIt + 4));
129 return static_cast<BaseType
>(*bufferIt) << 24U |
130 static_cast<BaseType
>(*(bufferIt + 1)) << 16U |
131 static_cast<BaseType
>(*(bufferIt + 2)) << 8U |
132 static_cast<BaseType
>(*(bufferIt + 3));
137 return static_cast<BaseType
>(*bufferIt) << 16U |
138 static_cast<BaseType
>(*(bufferIt + 1)) << 8U |
139 static_cast<BaseType
>(*(bufferIt + 2));
144 return static_cast<BaseType
>(*bufferIt) << 8U |
145 static_cast<BaseType
>(*(bufferIt + 1));
150 return static_cast<BaseType
>(*bufferIt);
154 inline void throwNumBitsIsNotValid(uint8_t
numBits)
156 throw CppRuntimeException(
"BitStreamReader: ReadBits #") << numBits <<
157 " is not valid, reading from stream failed!";
161 inline void checkNumBits(uint8_t
numBits)
164 throwNumBitsIsNotValid(numBits);
168 inline void checkNumBits64(uint8_t numBits)
171 throwNumBitsIsNotValid(numBits);
175 inline void throwEof()
177 throw CppRuntimeException(
"BitStreamReader: Reached eof(), reading from stream failed!");
181 inline void loadCacheNext(ReaderContext& ctx, uint8_t numBits)
183 static const uint8_t cacheBitSize =
sizeof(BaseType) * 8;
186 const size_t byteIndex = ctx.bitIndex >> 3U;
187 if (ctx.bufferBitSize >= ctx.bitIndex + cacheBitSize)
190 #ifdef ZSERIO_RUNTIME_64BIT 191 parse64(ctx.buffer.begin() + byteIndex);
193 parse32(ctx.buffer.begin() + byteIndex);
195 ctx.cacheNumBits = cacheBitSize;
199 if (ctx.bitIndex + numBits > ctx.bufferBitSize)
202 ctx.cacheNumBits =
static_cast<uint8_t
>(ctx.bufferBitSize - ctx.bitIndex);
205 const uint8_t alignedNumBits = (ctx.cacheNumBits + 7U) & ~0x7U;
207 switch (alignedNumBits)
209 #ifdef ZSERIO_RUNTIME_64BIT 211 ctx.cache = parse64(ctx.buffer.begin() + byteIndex);
214 ctx.cache = parse56(ctx.buffer.begin() + byteIndex);
217 ctx.cache = parse48(ctx.buffer.begin() + byteIndex);
220 ctx.cache = parse40(ctx.buffer.begin() + byteIndex);
224 ctx.cache = parse32(ctx.buffer.begin() + byteIndex);
227 ctx.cache = parse24(ctx.buffer.begin() + byteIndex);
230 ctx.cache = parse16(ctx.buffer.begin() + byteIndex);
233 ctx.cache = parse8(ctx.buffer.begin() + byteIndex);
237 ctx.cache >>=
static_cast<uint8_t
>(alignedNumBits - ctx.cacheNumBits);
242 inline BaseType readBitsImpl(ReaderContext& ctx, uint8_t numBits)
245 if (ctx.cacheNumBits < numBits)
248 value = ctx.cache & MASK_TABLE[ctx.cacheNumBits];
249 ctx.bitIndex += ctx.cacheNumBits;
250 numBits -= ctx.cacheNumBits;
253 loadCacheNext(ctx, numBits);
257 if (numBits <
sizeof(BaseType) * 8)
260 value |= ((ctx.cache >>
static_cast<uint8_t
>(ctx.cacheNumBits -
numBits)) & MASK_TABLE[
numBits]);
268 inline BaseSignedType readSignedBitsImpl(ReaderContext& ctx, uint8_t numBits)
270 static const uint8_t typeSize =
sizeof(BaseSignedType) * 8;
271 BaseType value = readBitsImpl(ctx, numBits);
276 if (numBits != 0 && numBits < typeSize &&
277 (value >= (static_cast<BaseType>(1) << static_cast<uint8_t>(numBits - 1))))
279 value -=
static_cast<BaseType
>(1) << numBits;
282 return static_cast<BaseSignedType
>(value);
285 #ifndef ZSERIO_RUNTIME_64BIT 287 inline uint64_t readBits64Impl(ReaderContext& ctx, uint8_t numBits)
291 uint64_t value = readBitsImpl(ctx, 32);
295 value |= readBitsImpl(ctx, numBits);
303 : buffer(readBuffer),
304 bufferBitSize(readBufferBitSize),
311 throw CppRuntimeException(
"BitStreamReader: Buffer size exceeded limit '") << MAX_BUFFER_SIZE <<
321 m_context(buffer, buffer.size() * 8)
325 m_context(buffer, bufferBitSize)
327 if (buffer.
size() < (bufferBitSize + 7) / 8)
330 "' < '" << (bufferBitSize + 7) / 8 <<
"')!";
335 m_context(
Span<const uint8_t>(buffer, (bufferBitSize + 7) / 8), bufferBitSize)
340 checkNumBits(numBits);
342 return static_cast<uint32_t
>(readBitsImpl(m_context, numBits));
347 checkNumBits64(numBits);
349 #ifdef ZSERIO_RUNTIME_64BIT 350 return readBitsImpl(m_context, numBits);
353 return readBitsImpl(m_context, numBits);
355 return readBits64Impl(m_context, numBits);
361 checkNumBits64(numBits);
363 #ifdef ZSERIO_RUNTIME_64BIT 364 return readSignedBitsImpl(m_context, numBits);
367 return readSignedBitsImpl(m_context, numBits);
369 int64_t value =
static_cast<int64_t
>(readBits64Impl(m_context, numBits));
374 const bool needsSignExtension =
375 numBits < 64 && (static_cast<uint64_t>(value) >= (UINT64_C(1) << (numBits - 1)));
376 if (needsSignExtension)
377 value -= UINT64_C(1) <<
numBits;
385 checkNumBits(numBits);
387 return static_cast<int32_t
>(readSignedBitsImpl(m_context, numBits));
392 uint8_t byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
393 const bool sign = (byte & VARINT_SIGN_1) != 0;
394 uint64_t result = byte & VARINT_BYTE_1;
395 if ((byte & VARINT_HAS_NEXT_1) == 0)
396 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
398 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
399 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
400 if ((byte & VARINT_HAS_NEXT_N) == 0)
401 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
403 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
404 result =
static_cast<uint64_t
>(result) << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
405 if ((byte & VARINT_HAS_NEXT_N) == 0)
406 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
408 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
409 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
410 if ((byte & VARINT_HAS_NEXT_N) == 0)
411 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
413 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
414 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
415 if ((byte & VARINT_HAS_NEXT_N) == 0)
416 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
418 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
419 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
420 if ((byte & VARINT_HAS_NEXT_N) == 0)
421 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
423 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
424 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
425 if ((byte & VARINT_HAS_NEXT_N) == 0)
426 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
428 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
429 return sign ? -
static_cast<int64_t
>(result) : static_cast<int64_t>(result);
434 uint8_t byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
435 const bool sign = (byte & VARINT_SIGN_1) != 0;
436 uint32_t result = byte & VARINT_BYTE_1;
437 if ((byte & VARINT_HAS_NEXT_1) == 0)
438 return sign ? -static_cast<int32_t>(result) :
static_cast<int32_t
>(result);
440 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
441 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
442 if ((byte & VARINT_HAS_NEXT_N) == 0)
443 return sign ? -static_cast<int32_t>(result) :
static_cast<int32_t
>(result);
445 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
446 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
447 if ((byte & VARINT_HAS_NEXT_N) == 0)
448 return sign ? -static_cast<int32_t>(result) :
static_cast<int32_t
>(result);
450 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
451 return sign ? -
static_cast<int32_t
>(result) : static_cast<int32_t>(result);
456 uint8_t byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
457 const bool sign = (byte & VARINT_SIGN_1) != 0;
458 uint16_t result = byte & VARINT_BYTE_1;
459 if ((byte & VARINT_HAS_NEXT_1) == 0)
460 return sign ? static_cast<int16_t>(-result) :
static_cast<int16_t
>(result);
462 result =
static_cast<uint16_t
>(result << 8U) | static_cast<uint8_t>(readBitsImpl(m_context, 8));
463 return sign ?
static_cast<int16_t
>(-result) : static_cast<int16_t>(result);
468 uint8_t byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
469 uint64_t result = byte & VARUINT_BYTE;
470 if ((byte & VARUINT_HAS_NEXT) == 0)
473 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
474 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
475 if ((byte & VARUINT_HAS_NEXT) == 0)
478 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
479 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
480 if ((byte & VARUINT_HAS_NEXT) == 0)
483 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
484 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
485 if ((byte & VARUINT_HAS_NEXT) == 0)
488 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
489 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
490 if ((byte & VARUINT_HAS_NEXT) == 0)
493 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
494 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
495 if ((byte & VARUINT_HAS_NEXT) == 0)
498 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
499 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
500 if ((byte & VARUINT_HAS_NEXT) == 0)
503 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
509 uint8_t byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
510 uint32_t result = byte & VARUINT_BYTE;
511 if ((byte & VARUINT_HAS_NEXT) == 0)
514 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
515 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
516 if ((byte & VARUINT_HAS_NEXT) == 0)
519 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
520 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
521 if ((byte & VARUINT_HAS_NEXT) == 0)
524 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
530 uint8_t byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
531 uint16_t result = byte & VARUINT_BYTE;
532 if ((byte & VARUINT_HAS_NEXT) == 0)
535 result =
static_cast<uint16_t
>(result << 8U) | static_cast<uint8_t>(readBitsImpl(m_context, 8));
541 uint8_t byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
542 const bool sign = (byte & VARINT_SIGN_1) != 0;
543 uint64_t result = byte & VARINT_BYTE_1;
544 if ((byte & VARINT_HAS_NEXT_1) == 0)
545 return sign ? (result == 0 ? INT64_MIN : -static_cast<int64_t>(result)) : static_cast<int64_t>(result);
547 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
548 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
549 if ((byte & VARINT_HAS_NEXT_N) == 0)
550 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
552 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
553 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
554 if ((byte & VARINT_HAS_NEXT_N) == 0)
555 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
557 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
558 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
559 if ((byte & VARINT_HAS_NEXT_N) == 0)
560 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
562 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
563 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
564 if ((byte & VARINT_HAS_NEXT_N) == 0)
565 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
567 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
568 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
569 if ((byte & VARINT_HAS_NEXT_N) == 0)
570 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
572 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
573 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
574 if ((byte & VARINT_HAS_NEXT_N) == 0)
575 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
577 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
578 result = result << 7U | static_cast<uint8_t>(byte & VARINT_BYTE_N);
579 if ((byte & VARINT_HAS_NEXT_N) == 0)
580 return sign ? -static_cast<int64_t>(result) :
static_cast<int64_t
>(result);
582 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
583 return sign ? -
static_cast<int64_t
>(result) : static_cast<int64_t>(result);
588 uint8_t byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
589 uint64_t result = byte & VARUINT_BYTE;
590 if ((byte & VARUINT_HAS_NEXT) == 0)
593 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
594 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
595 if ((byte & VARUINT_HAS_NEXT) == 0)
598 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
599 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
600 if ((byte & VARUINT_HAS_NEXT) == 0)
603 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
604 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
605 if ((byte & VARUINT_HAS_NEXT) == 0)
608 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
609 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
610 if ((byte & VARUINT_HAS_NEXT) == 0)
613 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
614 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
615 if ((byte & VARUINT_HAS_NEXT) == 0)
618 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
619 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
620 if ((byte & VARUINT_HAS_NEXT) == 0)
623 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
624 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
625 if ((byte & VARUINT_HAS_NEXT) == 0)
628 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
634 uint8_t byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
635 uint32_t result = byte & VARUINT_BYTE;
636 if ((byte & VARUINT_HAS_NEXT) == 0)
639 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
640 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
641 if ((byte & VARUINT_HAS_NEXT) == 0)
644 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
645 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
646 if ((byte & VARUINT_HAS_NEXT) == 0)
649 byte =
static_cast<uint8_t
>(readBitsImpl(m_context, 8));
650 result = result << 7U | static_cast<uint8_t>(byte & VARUINT_BYTE);
651 if ((byte & VARUINT_HAS_NEXT) == 0)
654 result = result << 8U | static_cast<uint8_t>(readBitsImpl(m_context, 8));
655 if (result > VARSIZE_MAX_VALUE)
657 "' is out of range for varsize type!";
664 const uint16_t halfPrecisionFloatValue =
static_cast<uint16_t
>(readBitsImpl(m_context, 16));
671 const uint32_t singlePrecisionFloatValue =
static_cast<uint32_t
>(readBitsImpl(m_context, 32));
678 #ifdef ZSERIO_RUNTIME_64BIT 679 const uint64_t doublePrecisionFloatValue =
static_cast<uint64_t
>(readBitsImpl(m_context, 64));
681 const uint64_t doublePrecisionFloatValue = readBits64Impl(m_context, 64);
689 return readBitsImpl(m_context, 1) != 0;
695 throw CppRuntimeException(
"BitStreamReader: Reached eof(), setting of bit position failed!");
697 m_context.
bitIndex = (position / 8) * 8;
699 const uint8_t skip =
static_cast<uint8_t
>(position - m_context.
bitIndex);
709 const uint8_t skip =
static_cast<uint8_t
>(alignment - offset);
714 uint8_t BitStreamReader::readByte()
716 return static_cast<uint8_t
>(readBitsImpl(m_context, 8));
uint64_t readBits64(uint8_t numBits=64)
uint8_t numBits(uint64_t numValues)
float convertUInt32ToFloat(uint32_t float32Value)
Span< const uint8_t > buffer
uint32_t readBits(uint8_t numBits=32)
const_pointer const_iterator
BitPosType getBitPosition() const
constexpr size_type size() const noexcept
int64_t readSignedBits64(uint8_t numBits=64)
const BitPosType bufferBitSize
void alignTo(size_t alignment)
ReaderContext(Span< const uint8_t > readBuffer, size_t readBufferBitSize)
BitStreamReader(const uint8_t *buffer, size_t bufferByteSize)
int32_t readSignedBits(uint8_t numBits=32)
double convertUInt64ToDouble(uint64_t float64Value)
float convertUInt16ToFloat(uint16_t float16Value)
void setBitPosition(BitPosType position)