16 const size_t MAX_BUFFER_SIZE = std::numeric_limits<size_t>::max() / 8 - 4;
19 using ReaderContext = BitStreamReader::ReaderContext;
21 #ifdef ZSERIO_RUNTIME_64BIT
22 using BaseType = uint64_t;
23 using BaseSignedType = int64_t;
25 using BaseType = uint32_t;
26 using BaseSignedType = int32_t;
29 #ifdef ZSERIO_RUNTIME_64BIT
30 const std::array<BaseType, 65> MASK_TABLE = {
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),
99 const std::array<BaseType, 33> MASK_TABLE = {
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),
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);
142 const uint8_t VARUINT_BYTE = UINT8_C(0x7f);
143 const uint8_t VARUINT_HAS_NEXT = UINT8_C(0x80);
145 const uint32_t VARSIZE_MAX_VALUE = (UINT32_C(1) << 31U) - 1;
147 #ifdef ZSERIO_RUNTIME_64BIT
150 return static_cast<BaseType
>(*bufferIt) << 56U |
static_cast<BaseType
>(*(bufferIt + 1)) << 48U |
151 static_cast<BaseType
>(*(bufferIt + 2)) << 40U |
static_cast<BaseType
>(*(bufferIt + 3)) << 32U |
152 static_cast<BaseType
>(*(bufferIt + 4)) << 24U |
static_cast<BaseType
>(*(bufferIt + 5)) << 16U |
153 static_cast<BaseType
>(*(bufferIt + 6)) << 8U |
static_cast<BaseType
>(*(bufferIt + 7));
158 return static_cast<BaseType
>(*bufferIt) << 48U |
static_cast<BaseType
>(*(bufferIt + 1)) << 40U |
159 static_cast<BaseType
>(*(bufferIt + 2)) << 32U |
static_cast<BaseType
>(*(bufferIt + 3)) << 24U |
160 static_cast<BaseType
>(*(bufferIt + 4)) << 16U |
static_cast<BaseType
>(*(bufferIt + 5)) << 8U |
161 static_cast<BaseType
>(*(bufferIt + 6));
166 return static_cast<BaseType
>(*bufferIt) << 40U |
static_cast<BaseType
>(*(bufferIt + 1)) << 32U |
167 static_cast<BaseType
>(*(bufferIt + 2)) << 24U |
static_cast<BaseType
>(*(bufferIt + 3)) << 16U |
168 static_cast<BaseType
>(*(bufferIt + 4)) << 8U |
static_cast<BaseType
>(*(bufferIt + 5));
173 return static_cast<BaseType
>(*bufferIt) << 32U |
static_cast<BaseType
>(*(bufferIt + 1)) << 24U |
174 static_cast<BaseType
>(*(bufferIt + 2)) << 16U |
static_cast<BaseType
>(*(bufferIt + 3)) << 8U |
175 static_cast<BaseType
>(*(bufferIt + 4));
180 return static_cast<BaseType
>(*bufferIt) << 24U |
static_cast<BaseType
>(*(bufferIt + 1)) << 16U |
181 static_cast<BaseType
>(*(bufferIt + 2)) << 8U |
static_cast<BaseType
>(*(bufferIt + 3));
186 return static_cast<BaseType
>(*bufferIt) << 16U |
static_cast<BaseType
>(*(bufferIt + 1)) << 8U |
187 static_cast<BaseType
>(*(bufferIt + 2));
192 return static_cast<BaseType
>(*bufferIt) << 8U |
static_cast<BaseType
>(*(bufferIt + 1));
197 return static_cast<BaseType
>(*bufferIt);
201 inline void throwNumBitsIsNotValid(uint8_t
numBits)
203 throw CppRuntimeException(
"BitStreamReader: ReadBits #")
204 <<
numBits <<
" is not valid, reading from stream failed!";
208 inline void checkNumBits(uint8_t
numBits)
212 throwNumBitsIsNotValid(
numBits);
217 inline void checkNumBits64(uint8_t
numBits)
221 throwNumBitsIsNotValid(
numBits);
226 inline void throwEof()
228 throw CppRuntimeException(
"BitStreamReader: Reached eof(), reading from stream failed!");
232 inline void loadCacheNext(ReaderContext& ctx, uint8_t
numBits)
234 static const uint8_t cacheBitSize =
sizeof(BaseType) * 8;
237 const size_t byteIndex = ctx.bitIndex >> 3U;
238 if (ctx.bufferBitSize >= ctx.bitIndex + cacheBitSize)
241 #ifdef ZSERIO_RUNTIME_64BIT
242 parse64(ctx.buffer.begin() + byteIndex);
244 parse32(ctx.buffer.begin() + byteIndex);
246 ctx.cacheNumBits = cacheBitSize;
250 if (ctx.bitIndex +
numBits > ctx.bufferBitSize)
255 ctx.cacheNumBits =
static_cast<uint8_t
>(ctx.bufferBitSize - ctx.bitIndex);
258 const uint8_t alignedNumBits =
static_cast<uint8_t
>((ctx.cacheNumBits + 7U) & ~0x7U);
260 switch (alignedNumBits)
262 #ifdef ZSERIO_RUNTIME_64BIT
264 ctx.cache = parse64(ctx.buffer.begin() + byteIndex);
267 ctx.cache = parse56(ctx.buffer.begin() + byteIndex);
270 ctx.cache = parse48(ctx.buffer.begin() + byteIndex);
273 ctx.cache = parse40(ctx.buffer.begin() + byteIndex);
277 ctx.cache = parse32(ctx.buffer.begin() + byteIndex);
280 ctx.cache = parse24(ctx.buffer.begin() + byteIndex);
283 ctx.cache = parse16(ctx.buffer.begin() + byteIndex);
286 ctx.cache = parse8(ctx.buffer.begin() + byteIndex);
290 ctx.cache >>=
static_cast<uint8_t
>(alignedNumBits - ctx.cacheNumBits);
295 inline BaseType readUnsignedBitsImpl(ReaderContext& ctx, uint8_t
numBits)
298 if (ctx.cacheNumBits <
numBits)
301 value = ctx.cache & MASK_TABLE[ctx.cacheNumBits];
302 ctx.bitIndex += ctx.cacheNumBits;
310 if (
numBits <
sizeof(BaseType) * 8)
315 value |= ((ctx.cache >>
static_cast<uint8_t
>(ctx.cacheNumBits -
numBits)) & MASK_TABLE[
numBits]);
316 ctx.cacheNumBits =
static_cast<uint8_t
>(ctx.cacheNumBits -
numBits);
323 inline BaseSignedType readSignedBitsImpl(ReaderContext& ctx, uint8_t
numBits)
325 static const uint8_t typeSize =
sizeof(BaseSignedType) * 8;
326 BaseType value = readUnsignedBitsImpl(ctx,
numBits);
332 (value >= (
static_cast<BaseType
>(1) <<
static_cast<uint8_t
>(
numBits - 1))))
334 value -=
static_cast<BaseType
>(1) <<
numBits;
337 return static_cast<BaseSignedType
>(value);
340 #ifndef ZSERIO_RUNTIME_64BIT
342 inline uint64_t readUnsignedBits64Impl(ReaderContext& ctx, uint8_t
numBits)
346 uint64_t value = readUnsignedBitsImpl(ctx, 32);
350 value |= readUnsignedBitsImpl(ctx,
numBits);
360 bufferBitSize(readBufferBitSize),
368 << MAX_BUFFER_SIZE <<
"' bytes!";
377 m_context(buffer, buffer.size() * 8)
381 m_context(buffer, bufferBitSize)
383 if (buffer.
size() < (bufferBitSize + 7) / 8)
386 << buffer.
size() <<
"' < '" << (bufferBitSize + 7) / 8 <<
"')!";
391 m_context(
Span<const uint8_t>(buffer, (bufferBitSize + 7) / 8), bufferBitSize)
398 return static_cast<uint32_t
>(readUnsignedBitsImpl(m_context,
numBits));
405 #ifdef ZSERIO_RUNTIME_64BIT
406 return readUnsignedBitsImpl(m_context,
numBits);
410 return readUnsignedBitsImpl(m_context,
numBits);
413 return readUnsignedBits64Impl(m_context,
numBits);
421 #ifdef ZSERIO_RUNTIME_64BIT
422 return readSignedBitsImpl(m_context,
numBits);
426 return readSignedBitsImpl(m_context,
numBits);
429 int64_t value =
static_cast<int64_t
>(readUnsignedBits64Impl(m_context,
numBits));
434 const bool needsSignExtension =
435 numBits < 64 && (static_cast<uint64_t>(value) >= (UINT64_C(1) << (
numBits - 1U)));
436 if (needsSignExtension)
438 value =
static_cast<int64_t
>(
static_cast<uint64_t
>(value) - (UINT64_C(1) <<
numBits));
449 return static_cast<int32_t
>(readSignedBitsImpl(m_context,
numBits));
454 return readUnsignedBitsImpl(m_context, 1) != 0;
459 uint8_t
byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
460 const bool sign = (
byte & VARINT_SIGN_1) != 0;
461 uint16_t result =
byte & VARINT_BYTE_1;
462 if ((
byte & VARINT_HAS_NEXT_1) == 0)
464 return sign ?
static_cast<int16_t
>(-result) :
static_cast<int16_t
>(result);
467 result =
static_cast<uint16_t
>(result << 8U);
468 result =
static_cast<uint16_t
>(result | readUnsignedBitsImpl(m_context, 8));
469 return sign ?
static_cast<int16_t
>(-result) :
static_cast<int16_t
>(result);
474 uint8_t
byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
475 const bool sign = (
byte & VARINT_SIGN_1) != 0;
476 uint32_t result =
byte & VARINT_BYTE_1;
477 if ((
byte & VARINT_HAS_NEXT_1) == 0)
479 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
482 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
483 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
484 if ((
byte & VARINT_HAS_NEXT_N) == 0)
486 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
489 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
490 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
491 if ((
byte & VARINT_HAS_NEXT_N) == 0)
493 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
496 result = result << 8U | static_cast<uint8_t>(readUnsignedBitsImpl(m_context, 8));
497 return sign ? -
static_cast<int32_t
>(result) :
static_cast<int32_t
>(result);
502 uint8_t
byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
503 const bool sign = (
byte & VARINT_SIGN_1) != 0;
504 uint64_t result =
byte & VARINT_BYTE_1;
505 if ((
byte & VARINT_HAS_NEXT_1) == 0)
507 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
510 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
511 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
512 if ((
byte & VARINT_HAS_NEXT_N) == 0)
514 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
517 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
518 result =
static_cast<uint64_t
>(result) << 7U |
static_cast<uint8_t
>(
byte & VARINT_BYTE_N);
519 if ((
byte & VARINT_HAS_NEXT_N) == 0)
521 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
524 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
525 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
526 if ((
byte & VARINT_HAS_NEXT_N) == 0)
528 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
531 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
532 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
533 if ((
byte & VARINT_HAS_NEXT_N) == 0)
535 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
538 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
539 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
540 if ((
byte & VARINT_HAS_NEXT_N) == 0)
542 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
545 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
546 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
547 if ((
byte & VARINT_HAS_NEXT_N) == 0)
549 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
552 result = result << 8U | static_cast<uint8_t>(readUnsignedBitsImpl(m_context, 8));
553 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
558 uint8_t
byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
559 const bool sign = (
byte & VARINT_SIGN_1) != 0;
560 uint64_t result =
byte & VARINT_BYTE_1;
561 if ((
byte & VARINT_HAS_NEXT_1) == 0)
563 return sign ? (result == 0 ? INT64_MIN : -
static_cast<int64_t
>(result)) :
static_cast<int64_t
>(result);
566 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
567 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
568 if ((
byte & VARINT_HAS_NEXT_N) == 0)
570 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
573 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
574 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
575 if ((
byte & VARINT_HAS_NEXT_N) == 0)
577 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
580 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
581 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
582 if ((
byte & VARINT_HAS_NEXT_N) == 0)
584 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
587 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
588 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
589 if ((
byte & VARINT_HAS_NEXT_N) == 0)
591 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
594 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
595 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
596 if ((
byte & VARINT_HAS_NEXT_N) == 0)
598 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
601 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
602 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
603 if ((
byte & VARINT_HAS_NEXT_N) == 0)
605 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
608 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
609 result = result << 7U | static_cast<uint8_t>(
byte & VARINT_BYTE_N);
610 if ((
byte & VARINT_HAS_NEXT_N) == 0)
612 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
615 result = result << 8U | static_cast<uint8_t>(readUnsignedBitsImpl(m_context, 8));
616 return sign ? -
static_cast<int64_t
>(result) :
static_cast<int64_t
>(result);
621 uint8_t
byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
622 uint16_t result =
byte & VARUINT_BYTE;
623 if ((
byte & VARUINT_HAS_NEXT) == 0)
628 result =
static_cast<uint16_t
>(result << 8U);
629 result =
static_cast<uint16_t
>(result | readUnsignedBitsImpl(m_context, 8));
635 uint8_t
byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
636 uint32_t result =
byte & VARUINT_BYTE;
637 if ((
byte & VARUINT_HAS_NEXT) == 0)
642 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
643 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
644 if ((
byte & VARUINT_HAS_NEXT) == 0)
649 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
650 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
651 if ((
byte & VARUINT_HAS_NEXT) == 0)
656 result = result << 8U | static_cast<uint8_t>(readUnsignedBitsImpl(m_context, 8));
662 uint8_t
byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
663 uint64_t result =
byte & VARUINT_BYTE;
664 if ((
byte & VARUINT_HAS_NEXT) == 0)
669 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
670 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
671 if ((
byte & VARUINT_HAS_NEXT) == 0)
676 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
677 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
678 if ((
byte & VARUINT_HAS_NEXT) == 0)
683 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
684 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
685 if ((
byte & VARUINT_HAS_NEXT) == 0)
690 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
691 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
692 if ((
byte & VARUINT_HAS_NEXT) == 0)
697 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
698 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
699 if ((
byte & VARUINT_HAS_NEXT) == 0)
704 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
705 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
706 if ((
byte & VARUINT_HAS_NEXT) == 0)
711 result = result << 8U | static_cast<uint8_t>(readUnsignedBitsImpl(m_context, 8));
717 uint8_t
byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
718 uint64_t result =
byte & VARUINT_BYTE;
719 if ((
byte & VARUINT_HAS_NEXT) == 0)
724 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
725 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
726 if ((
byte & VARUINT_HAS_NEXT) == 0)
731 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
732 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
733 if ((
byte & VARUINT_HAS_NEXT) == 0)
738 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
739 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
740 if ((
byte & VARUINT_HAS_NEXT) == 0)
745 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
746 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
747 if ((
byte & VARUINT_HAS_NEXT) == 0)
752 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
753 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
754 if ((
byte & VARUINT_HAS_NEXT) == 0)
759 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
760 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
761 if ((
byte & VARUINT_HAS_NEXT) == 0)
766 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
767 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
768 if ((
byte & VARUINT_HAS_NEXT) == 0)
773 result = result << 8U | static_cast<uint8_t>(readUnsignedBitsImpl(m_context, 8));
779 uint8_t
byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
780 uint32_t result =
byte & VARUINT_BYTE;
781 if ((
byte & VARUINT_HAS_NEXT) == 0)
786 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
787 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
788 if ((
byte & VARUINT_HAS_NEXT) == 0)
793 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
794 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
795 if ((
byte & VARUINT_HAS_NEXT) == 0)
800 byte =
static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
801 result = result << 7U | static_cast<uint8_t>(
byte & VARUINT_BYTE);
802 if ((
byte & VARUINT_HAS_NEXT) == 0)
807 result = result << 8U | static_cast<uint8_t>(readUnsignedBitsImpl(m_context, 8));
808 if (result > VARSIZE_MAX_VALUE)
811 << result <<
"' is out of range for varsize type!";
819 const uint16_t halfPrecisionFloatValue =
static_cast<uint16_t
>(readUnsignedBitsImpl(m_context, 16));
826 const uint32_t singlePrecisionFloatValue =
static_cast<uint32_t
>(readUnsignedBitsImpl(m_context, 32));
833 #ifdef ZSERIO_RUNTIME_64BIT
834 const uint64_t doublePrecisionFloatValue =
static_cast<uint64_t
>(readUnsignedBitsImpl(m_context, 64));
836 const uint64_t doublePrecisionFloatValue = readUnsignedBits64Impl(m_context, 64);
846 throw CppRuntimeException(
"BitStreamReader: Reached eof(), setting of bit position failed!");
849 m_context.
bitIndex = (position / 8) * 8;
851 const uint8_t skip =
static_cast<uint8_t
>(position - m_context.
bitIndex);
863 const uint8_t skip =
static_cast<uint8_t
>(alignment - offset);
868 uint8_t BitStreamReader::readByte()
870 return static_cast<uint8_t
>(readUnsignedBitsImpl(m_context, 8));
void setBitPosition(BitPosType position)
void alignTo(size_t alignment)
BitStreamReader(const uint8_t *buffer, size_t bufferByteSize)
uint32_t readUnsignedBits32(uint8_t numBits=32)
VarUInt16 readVarUInt16()
uint64_t readUnsignedBits64(uint8_t numBits=64)
VarUInt32 readVarUInt32()
VarUInt64 readVarUInt64()
int64_t readSignedBits64(uint8_t numBits=64)
int32_t readSignedBits32(uint8_t numBits=32)
BitPosType getBitPosition() const
constexpr size_type size() const noexcept
const_pointer const_iterator
uint8_t numBits(uint64_t numValues)
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR > VarUInt
detail::FloatWrapper< float, detail::FloatType::FLOAT16 > Float16
detail::FloatWrapper< float, detail::FloatType::FLOAT32 > Float32
detail::VarIntWrapper< int32_t, detail::VarIntType::VAR32 > VarInt32
float convertUInt32ToFloat(uint32_t float32Value)
detail::VarIntWrapper< uint16_t, detail::VarIntType::VAR16 > VarUInt16
double convertUInt64ToDouble(uint64_t float64Value)
detail::VarIntWrapper< uint64_t, detail::VarIntType::VAR64 > VarUInt64
detail::VarIntWrapper< int16_t, detail::VarIntType::VAR16 > VarInt16
detail::FloatWrapper< double, detail::FloatType::FLOAT64 > Float64
detail::VarIntWrapper< uint32_t, detail::VarIntType::VAR32 > VarUInt32
float convertUInt16ToFloat(uint16_t float16Value)
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR > VarInt
detail::VarIntWrapper< int64_t, detail::VarIntType::VAR64 > VarInt64
detail::VarIntWrapper< uint32_t, detail::VarIntType::VARSIZE > VarSize
ReaderContext(Span< const uint8_t > readBuffer, size_t readBufferBitSize)
Span< const uint8_t > buffer
const BitPosType bufferBitSize