GNU Radio Manual and C++ API Reference 3.10.5.1
The Free & Open Software Radio Ecosystem
crc.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2022 Daniel Estevez <daniel@destevez.net>
4 *
5 * This file is part of GNU Radio
6 *
7 * SPDX-License-Identifier: GPL-3.0-or-later
8 *
9 */
10
11#ifndef INCLUDED_DIGITAL_CRC_H
12#define INCLUDED_DIGITAL_CRC_H
13
15
16#include <stdint.h>
17#include <array>
18#include <vector>
19
20namespace gr {
21namespace digital {
22
23/*!
24 * \brief Calculates a CRC
25 * \ingroup packet_operators_blk
26 *
27 * \details
28 * This class calculates a CRC with configurable parameters.
29 * A table-driven byte-by-byte approach is used in the CRC
30 * computation.
31 */
33{
34public:
35 /*!
36 * \brief Construct a CRC calculator instance.
37 *
38 * \param num_bits CRC size in bits
39 * \param poly CRC polynomial, in MSB-first notation
40 * \param initial_value Initial register value
41 * \param final_xor Final XOR value
42 * \param input_reflected true if the input is LSB-first, false if not
43 * \param result_reflected true if the output is LSB-first, false if not
44 */
45 crc(unsigned num_bits,
46 uint64_t poly,
47 uint64_t initial_value,
48 uint64_t final_xor,
49 bool input_reflected,
50 bool result_reflected);
52
53 /*!
54 * \brief Computes a CRC
55 *
56 * \param data the input data for the CRC calculation
57 * \param len the length in bytes of the data
58 */
59 uint64_t compute(const uint8_t* data, std::size_t len);
60
61 /*!
62 * \brief Computes a CRC
63 *
64 * \param data the input data for the CRC calculation
65 */
66 uint64_t compute(std::vector<uint8_t> const& data)
67 {
68 return compute(data.data(), data.size());
69 }
70
71private:
72 std::array<uint64_t, 256> d_table;
73 unsigned d_num_bits;
74 uint64_t d_mask;
75 uint64_t d_initial_value;
76 uint64_t d_final_xor;
77 bool d_input_reflected;
78 bool d_result_reflected;
79
80 uint64_t reflect(uint64_t word) const;
81};
82
83} // namespace digital
84} // namespace gr
85
86#endif /* INCLUDED_DIGITAL_CRC_H */
Calculates a CRC.
Definition: crc.h:33
uint64_t compute(std::vector< uint8_t > const &data)
Computes a CRC.
Definition: crc.h:66
uint64_t compute(const uint8_t *data, std::size_t len)
Computes a CRC.
crc(unsigned num_bits, uint64_t poly, uint64_t initial_value, uint64_t final_xor, bool input_reflected, bool result_reflected)
Construct a CRC calculator instance.
#define DIGITAL_API
Definition: gr-digital/include/gnuradio/digital/api.h:18
GNU Radio logging wrapper.
Definition: basic_block.h:29