28constexpr std::array<uint32_t, 256> MakeCrcTable() {
29 std::array<uint32_t, 256> table{};
30 for (uint32_t i = 0; i < 256; ++i) {
32 for (uint32_t bit = 0; bit < 8; ++bit) {
34 crc = (crc >> 1U) ^ 0xEDB88320U;
45constexpr auto kCrc32Table = MakeCrcTable();
50 uint32_t crc = 0xFFFFFFFFU;
51 for (std::size_t i = 0; i < length; ++i) {
52 const uint8_t index =
static_cast<uint8_t
>((crc ^ data[i]) & 0xFFU);
53 crc = (crc >> 8U) ^ kCrc32Table[index];
55 return crc ^ 0xFFFFFFFFU;
95 result.bytesConsumed = 1;
101 result.bytesConsumed = 1;
107 const std::size_t expectedSize = payloadSize +
kChecksumSize;
108 if (length < expectedSize) {
115 const uint32_t computedCrc =
ComputeCrc32(data, payloadSize);
116 if (transmittedCrc != computedCrc) {
118 result.bytesConsumed = expectedSize;
128 result.bytesConsumed = expectedSize;
158 result.bytesConsumed = 1;
constexpr std::size_t kMaxMessagesPerPacket
Maximum number of messages allowed in a single packet.
constexpr std::size_t kChecksumSize
Size of CRC32 checksum in bytes.
uint32_t LoadU32(const uint8_t *p)
uint16_t LoadU16(const uint8_t *p)
constexpr std::size_t kHeaderMsgCountIndex
std::optional< MessageInfo > GetMessageInfo(MessageTypeId typeId)
DecodeViewResult DecodePacketView(const uint8_t *data, std::size_t length)
Decode a packet using the global message type registry.
constexpr std::size_t kHeaderMsgTypeIndex
uint32_t ComputeCrc32(const uint8_t *data, std::size_t length)
Compute CRC32 checksum for data integrity verification.
constexpr uint8_t kProtocolMinorV3
@ UnsupportedVersion
Protocol version mismatch.
@ ChecksumMismatch
CRC32 validation failed.
@ TooManyMessages
Message count exceeds kMaxMessagesPerPacket.
@ Truncated
Buffer ends before expected packet length.
@ UnknownMessageType
Message type ID not in registry.
@ None
No error - packet decoded successfully.
@ TooSmall
Buffer too small to contain header.
DecodeViewResult DecodePacketViewWithSize(const uint8_t *data, std::size_t length, std::size_t wireSize)
Decode a packet with explicitly provided message wire size.
constexpr std::size_t kHeaderSizeV3
constexpr uint8_t kProtocolMajorV3
BCNP packet structures, encoding, and decoding utilities.
Result of decoding a packet from raw bytes.
Zero-copy view into a decoded packet buffer.
PacketHeader header
Parsed header information.
std::size_t GetPayloadSize() const
Calculate total payload size in bytes.
const uint8_t * payloadStart
Pointer to first message in buffer.