GNU Radio's SATNOGS Package
crc.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
4 *
5 * Copyright (C) 2019-2023, Libre Space Foundation <http://libre.space>
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 *
20 * SPDX-License-Identifier: GNU General Public License v3.0 or later
21 */
22
23
24#ifndef INCLUDED_SATNOGS_CRC_H
25#define INCLUDED_SATNOGS_CRC_H
26
27#define CRCPP_INCLUDE_ESOTERIC_CRC_DEFINITIONS 1
28#define CRCPP_USE_CPP11 1
29
31#include <CRC.h>
32#include <cstddef>
33#include <cstdint>
34#include <stdexcept>
35
36namespace gr {
37namespace satnogs {
38
39/*!
40 * CRC class providing a range of different CRC calculation static methods
41 *
42 */
44{
45public:
46 /**
47 * @brief Predefined CRC types
48 *
49 * Names and alias retrieved from https://reveng.sourceforge.io/crc-catalogue/
50 */
51 enum class type {
52 NONE = 0,
53 CRC16_AUG_CCITT,
54 CRC16_AUG_CCITT_XOR, //! XOR'd version of the CRC-16/AUG-CCITT
55 CRC16_CMS,
56 CRC16_HDLC, //! Alias: CRC-16/IBM-SDLC, CRC-16/ISO-HDLC, CRC-16/ISO-IEC-14443-3-B,
57 //! CRC-16/X-25, CRC-B, X-25
58 CRC16_KERMIT, //! Alias: CRC-16/BLUETOOTH, CRC-16/CCITT, CRC-16/CCITT-TRUE,
59 //! CRC-16/V-41-LSB, CRC-CCITT, KERMIT
60 CRC16_XMODEM, //! Alias: CRC-16/ACORN, CRC-16/LTE, CRC-16/V-41-MSB, XMODEM, ZMODEM
61 CRC32_C, //! Alias: CRC-32/BASE91-C, CRC-32/CASTAGNOLI, CRC-32/INTERLAKEN,
62 //! CRC-32/ISCSI
63
64 };
65
66 /**
67 *
68 * @param t the CRC method
69 * @return the size of the specified CRC in bytes
70 */
71 static constexpr size_t size(type t)
72 {
73 switch (t) {
74 case type::NONE:
75 return 0;
76 case type::CRC16_XMODEM:
77 case type::CRC16_CMS:
78 case type::CRC16_HDLC:
79 case type::CRC16_AUG_CCITT:
80 case type::CRC16_AUG_CCITT_XOR:
81 case type::CRC16_KERMIT:
82 return sizeof(uint16_t);
83 case type::CRC32_C:
84 return sizeof(uint32_t);
85 default:
86 throw std::invalid_argument("crc: Invalid CRC method");
87 }
88 }
89
90 template <typename CRCType, crcpp_uint16 CRCWidth>
91 static constexpr size_t size(const CRC::Parameters<CRCType, CRCWidth>& t)
92 {
93 static_assert(CRCWidth % 8 == 0, "Expected a CRC size with byte boundaries");
94 return CRCWidth / 8;
95 }
96
97 template <typename CRCType, crcpp_uint16 CRCWidth>
99 uint8_t* out,
100 const uint8_t* data,
101 size_t len,
102 bool msb = true)
103 {
104 static_assert(CRCWidth % 8 == 0, "Expected a CRC size with byte boundaries");
105 auto calc = CRC::Calculate(data, len, t);
106 if (msb) {
107 for (int i = sizeof(calc) - 1; i >= 0; i--) {
108 *out++ = calc >> (i * 8);
109 }
110 } else {
111 for (size_t i = 0; i < sizeof(calc); i++) {
112 *out++ = calc >> (i * 8);
113 }
114 }
115 return sizeof(calc);
116 }
117
118 static size_t
119 append(type t, uint8_t* out, const uint8_t* data, size_t len, bool msb = true);
120
121 static bool check(type t, const uint8_t* data, size_t len, bool msb = true);
122
123 template <typename CRCType, crcpp_uint16 CRCWidth>
125 const uint8_t* data,
126 size_t len,
127 bool msb = true)
128 {
129 const size_t payload_len = len - crc::size(t);
130 auto calc = CRC::Calculate(data, payload_len, t);
131 CRCType recv = 0x0;
132 if (msb) {
133 for (size_t i = 0; i < sizeof(recv); i++) {
134 recv <<= 8;
135 recv |= data[payload_len + i];
136 }
137 } else {
138 for (int i = sizeof(recv) - 1; i >= 0; i--) {
139 recv <<= 8;
140 recv |= data[payload_len + i];
141 }
142 }
143 return calc == recv;
144 }
145};
146
147} // namespace satnogs
148} // namespace gr
149
150#endif /* INCLUDED_SATNOGS_CRC_H */
#define SATNOGS_API
Definition: api.h:19
static CRCType Calculate(const void *data, crcpp_size size, const Parameters< CRCType, CRCWidth > &parameters)
Computes a CRC.
Definition: CRC.h:463
Definition: crc.h:44
type
Predefined CRC types.
Definition: crc.h:51
static constexpr size_t size(type t)
Definition: crc.h:71
static constexpr size_t size(const CRC::Parameters< CRCType, CRCWidth > &t)
Definition: crc.h:91
static bool check(type t, const uint8_t *data, size_t len, bool msb=true)
static bool check(const CRC::Parameters< CRCType, CRCWidth > &t, const uint8_t *data, size_t len, bool msb=true)
Definition: crc.h:124
static size_t append(type t, uint8_t *out, const uint8_t *data, size_t len, bool msb=true)
static size_t append(const CRC::Parameters< CRCType, CRCWidth > &t, uint8_t *out, const uint8_t *data, size_t len, bool msb=true)
Definition: crc.h:98
data_t t[NROOTS+1]
Definition: decode_rs.h:77
int i
Definition: decode_rs.h:73
Definition: amsat_duv_decoder.h:29
CRC parameters.
Definition: CRC.h:160