23#ifndef DCCLV4FIELDCODECCRCN
24#define DCCLV4FIELDCODECCRCN
30#include "field_codec_default.h"
43inline uint16_t
crc16(
const char* data,
size_t size)
45 uint16_t crc = 0xFFFF;
46 for (
size_t i = 0; i < size; ++i)
48 crc ^=
static_cast<uint16_t
>(
static_cast<uint8_t
>(data[i])) << 8;
49 for (
int bit = 0; bit < 8; ++bit)
52 crc =
static_cast<uint16_t
>((crc << 1) ^ 0x1021);
54 crc =
static_cast<uint16_t
>(crc << 1);
67inline uint32_t
crc32(
const char* data,
size_t size)
69 uint32_t crc = 0xFFFFFFFF;
70 for (
size_t i = 0; i < size; ++i)
72 crc ^=
static_cast<uint8_t
>(data[i]);
73 for (
int bit = 0; bit < 8; ++bit)
76 crc = (crc >> 1) ^ 0xEDB88320;
81 return crc ^ 0xFFFFFFFF;
92template <
int CRCBits>
class CRCCodecBase :
public v4::DefaultNumericFieldCodec<uint32>
97 double min()
override {
return 0; }
98 double max()
override {
return (CRCBits == 32) ?
static_cast<double>(0xFFFFFFFFu) :
static_cast<double>(0xFFFFu); }
106 uint32 pre_encode(
const uint32& )
override {
return compute_crc_encode(); }
110 uint32 expected = compute_crc_decode();
111 uint32 received = wire_value;
112 if (expected != received)
114 std::stringstream ss;
115 ss <<
"CRC mismatch. Expected: 0x" << std::hex << expected <<
", received: 0x"
117 <<
". Message data may be corrupted.";
118 throw(
Exception(ss.str(), this->root_descriptor()));
127 this->validate_numeric_bounds();
131 uint32 compute_crc_encode()
const
138 return compute_crc(byte_str.data(), byte_str.size());
146 uint32 compute_crc_decode()
const
154 return compute_crc(byte_str.data(), byte_str.size());
157 static uint32 compute_crc(
const char* data,
size_t size)
A variable size container of bits (subclassed from std::deque<bool>) with an optional hierarchy....
std::string to_byte_string()
Returns the value of the Bitset to a byte string, where each character represents 8 bits of the Bitse...
Exception class for DCCL.
const Bitset * root_bitset() const
Returns the root Bitset for the current encode or decode operation.
void set_force_use_required(bool force_required=true)
Force the codec to always use the "required" field encoding, regardless of the FieldDescriptor settin...
DCCL CRC-16 codec (CRC-16/IBM-3740 / CCITT-FALSE).
DCCL CRC-32 codec (CRC-32/ISO-HDLC).
DCCL version 4 default field codecs.
uint32_t crc32(const char *data, size_t size)
Computes a CRC-32/ISO-HDLC checksum (standard Ethernet/zlib CRC-32).
uint16_t crc16(const char *data, size_t size)
Computes a CRC-16/IBM-3740 (CRC-16/CCITT-FALSE) checksum.
Dynamic Compact Control Language namespace.
google::protobuf::uint32 uint32
an unsigned 32 bit integer