C/C++ API Reference
Loading...
Searching...
No Matches
crc16_ccitt.h
1// Copyright 2020 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// Provides an implementation of the CRC-16-CCITT or CRC-CCITT checksum, which
16// uses the polynomial 0x1021:
17//
18// x^16 + x^12 + x^5 + 1
19//
20// with initial value 0xFFFF. See https://www.zlib.net/crc_v3.txt.
21#pragma once
22
23#include <stddef.h>
24#include <stdint.h>
25
26#ifdef __cplusplus
27extern "C" {
28#endif // __cplusplus
29
30// C API for calculating the CRC-16-CCITT of an array of data.
31uint16_t pw_checksum_Crc16Ccitt(const void* data,
32 size_t size_bytes,
33 uint16_t initial_value);
34
35#ifdef __cplusplus
36} // extern "C"
37
38#include "pw_bytes/span.h"
39#include "pw_span/span.h"
40
41namespace pw::checksum {
42
43// Calculates the CRC-16-CCITT for all data passed to Update.
45 public:
46 static constexpr uint16_t kInitialValue = 0xFFFF;
47
48 // Calculates the CRC-16-CCITT for the provided data and returns it as a
49 // uint16_t. To update a CRC in multiple calls, use an instance of the
50 // Crc16Ccitt class or pass the previous value as the initial_value argument.
51 static uint16_t Calculate(span<const std::byte> data,
52 uint16_t initial_value = kInitialValue) {
53 return pw_checksum_Crc16Ccitt(
54 data.data(), data.size_bytes(), initial_value);
55 }
56
57 static uint16_t Calculate(std::byte data,
58 uint16_t initial_value = kInitialValue) {
59 return Calculate(ConstByteSpan(&data, 1), initial_value);
60 }
61
62 constexpr Crc16Ccitt() : value_(kInitialValue) {}
63
64 void Update(span<const std::byte> data) { value_ = Calculate(data, value_); }
65
66 void Update(std::byte data) { Update(ByteSpan(&data, 1)); }
67
68 // Returns the value of the CRC-16-CCITT for all data passed to Update.
69 uint16_t value() const { return value_; }
70
71 // Resets the CRC to the initial value.
72 void clear() { value_ = kInitialValue; }
73
74 private:
75 uint16_t value_;
76};
77
78} // namespace pw::checksum
79
80#endif // __cplusplus
Definition: crc16_ccitt.h:44
Definition: span_impl.h:235
Checksum calculation library.
Definition: crc16_ccitt.h:41