C/C++ API Reference
Loading...
Searching...
No Matches
crc8.h
1// Copyright 2026 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#pragma once
16
17#include <cstdint>
18
19#include "pw_span/span.h"
20
21namespace pw::checksum {
22
23// A class for calculating 8-bit Cyclic Redundancy Checks (CRCs).
24//
25// This class supports various CRC-8 algorithms by configuring the polynomial,
26// initial value, reflection settings, and final XOR value.
27class Crc8 {
28 public:
37 constexpr Crc8(uint8_t polynomial,
38 uint8_t initial_value,
39 bool reflect_in,
40 bool reflect_out,
41 uint8_t xor_out)
42 : polynomial_(polynomial),
43 initial_value_(initial_value),
44 reflect_in_(reflect_in),
45 reflect_out_(reflect_out),
46 xor_out_(xor_out) {}
47
52 constexpr uint8_t Calculate(pw::span<const std::byte> data) const;
53
55 static const Crc8 kCrc8;
56
58 static const Crc8 kMaxim;
59
61 static const Crc8 kWcdma;
62
64 static const Crc8 kItu;
65
67 static const Crc8 kRohc;
68
69 private:
70 const uint8_t polynomial_;
71 const uint8_t initial_value_;
72 const bool reflect_in_;
73 const bool reflect_out_;
74 const uint8_t xor_out_;
75};
76
77namespace internal {
78
79constexpr uint8_t Reflect(uint8_t value) {
80 uint8_t reflected = 0;
81 for (uint8_t i = 0; i < 8; ++i) {
82 if ((value >> i) & 1) {
83 reflected |= 1 << (7 - i);
84 }
85 }
86 return reflected;
87}
88
89} // namespace internal
90
91constexpr uint8_t Crc8::Calculate(pw::span<const std::byte> data) const {
92 uint8_t crc = initial_value_;
93 for (std::byte byte : data) {
94 uint8_t current_byte = static_cast<uint8_t>(byte);
95 if (reflect_in_) {
96 current_byte = internal::Reflect(current_byte);
97 }
98 crc ^= current_byte;
99 for (uint8_t i = 0; i < 8; ++i) {
100 if (crc & 0x80) {
101 crc = static_cast<uint8_t>((crc << 1) ^ polynomial_);
102 } else {
103 crc <<= 1;
104 }
105 }
106 }
107
108 if (reflect_out_) {
109 crc = internal::Reflect(crc);
110 }
111
112 return crc ^ xor_out_;
113}
114
115inline constexpr Crc8 Crc8::kCrc8(0x07, 0x00, false, false, 0x00);
116inline constexpr Crc8 Crc8::kMaxim(0x31, 0x00, true, true, 0x00);
117inline constexpr Crc8 Crc8::kWcdma(0x9B, 0x00, true, true, 0x00);
118inline constexpr Crc8 Crc8::kItu(0x07, 0x00, false, false, 0x55);
119inline constexpr Crc8 Crc8::kRohc(0x07, 0xFF, true, true, 0x00);
120
121} // namespace pw::checksum
Definition: crc8.h:27
static const Crc8 kRohc
CRC-8/ROHC: poly=0x07, init=0xFF, refin=true, refout=true, xout=0x00.
Definition: crc8.h:67
static const Crc8 kCrc8
CRC-8: poly=0x07, init=0x00, refin=false, refout=false, xout=0x00.
Definition: crc8.h:55
static const Crc8 kItu
CRC-8/ITU: poly=0x07, init=0x00, refin=false, refout=false, xout=0x55.
Definition: crc8.h:64
constexpr Crc8(uint8_t polynomial, uint8_t initial_value, bool reflect_in, bool reflect_out, uint8_t xor_out)
Constructs a Crc8 calculator with the specified parameters.
Definition: crc8.h:37
static const Crc8 kMaxim
CRC-8/MAXIM: poly=0x31, init=0x00, refin=true, refout=true, xout=0x00.
Definition: crc8.h:58
constexpr uint8_t Calculate(pw::span< const std::byte > data) const
Calculates the CRC-8 value for the given data.
Definition: crc8.h:91
static const Crc8 kWcdma
CRC-8/WCDMA: poly=0x9B, init=0x00, refin=true, refout=true, xout=0x00.
Definition: crc8.h:61
Definition: span_impl.h:235
Checksum calculation library.
Definition: crc16_ccitt.h:41