RIT VEXU Core API
Loading...
Searching...
No Matches
crc32.h
1#pragma once
2#include <cstddef>
3#include <cstdint>
4
5#define FLASH_READ_DWORD(x) (*(const uint32_t*)(x))
6
7static constexpr uint32_t crc32_table[] = {
8 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac, 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
9 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c, 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c};
14class CRC32 {
15 public:
17 constexpr CRC32() { reset(); };
18
20 void constexpr reset() { _state = ~0L; };
21
26 void constexpr update(const uint8_t& data) {
27 // via http://forum.arduino.cc/index.php?topic=91179.0
28 uint8_t tbl_idx = 0;
29
30 tbl_idx = _state ^ (data >> (0 * 4));
31 _state = FLASH_READ_DWORD(crc32_table + (tbl_idx & 0x0f)) ^ (_state >> 4);
32 tbl_idx = _state ^ (data >> (1 * 4));
33 _state = FLASH_READ_DWORD(crc32_table + (tbl_idx & 0x0f)) ^ (_state >> 4);
34 }
35
41 template <typename Type>
42 void update(const Type& data) {
43 update(&data, 1);
44 }
45
52 template <typename Type>
53 void constexpr update(const Type* data, std::size_t size) {
54 std::size_t nBytes = size * sizeof(Type);
55 const uint8_t* pData = (const uint8_t*)data;
56
57 for (std::size_t i = 0; i < nBytes; i++) {
58 update(pData[i]);
59 }
60 }
61
63 uint32_t constexpr finalize() const { return ~_state; }
64
72 template <typename Type>
73 static constexpr uint32_t calculate(const Type* data, std::size_t size) {
74 CRC32 crc;
75 crc.update(data, size);
76 return crc.finalize();
77 }
78
79 private:
81 uint32_t _state = ~0L;
82};
uint32_t constexpr finalize() const
Definition crc32.h:63
void update(const Type &data)
Update the current checksum caclulation with the given data.
Definition crc32.h:42
static constexpr uint32_t calculate(const Type *data, std::size_t size)
Calculate the checksum of an arbitrary data array.
Definition crc32.h:73
void constexpr update(const Type *data, std::size_t size)
Update the current checksum caclulation with the given data.
Definition crc32.h:53
void constexpr reset()
Reset the checksum claculation.
Definition crc32.h:20
void constexpr update(const uint8_t &data)
Update the current checksum caclulation with the given data.
Definition crc32.h:26
constexpr CRC32()
Initialize an empty CRC32 checksum.
Definition crc32.h:17