diff --git a/error_detection/error_detection.cc b/error_detection/error_detection.cc index 9860ce9..2190bec 100644 --- a/error_detection/error_detection.cc +++ b/error_detection/error_detection.cc @@ -5,17 +5,23 @@ #include "error_detection.h" -uint8_t agi::error_detection::crc8(const uint8_t* data, std::size_t length) { +#include +#include +#include + +uint8_t agi::error_detection::crc8(std::span data) +{ constexpr uint8_t CRC8_POLYNOMIAL = 0x07; // CRC-8 Generator polynomial constexpr uint8_t CRC8_MSB = (1U << 7U); // Most significat bit of byte uint8_t crc = 0x00; - for (std::size_t i = 0; i < length; i++) { - crc ^= data[i]; - for (int b = 0; b < 8; b++) { - if (crc & CRC8_MSB) { - crc = (crc << 1U) ^ CRC8_POLYNOMIAL; + for (auto const val : data) { + crc ^= val; + constexpr int BITS_PER_BYTE = 8; + for (int b = 0; b < BITS_PER_BYTE; b++) { + if (static_cast(crc & CRC8_MSB)) { + crc = (crc << 1U) ^ CRC8_POLYNOMIAL; // NOLINT(*-signed-bitwise) } else { crc <<= 1U; } diff --git a/error_detection/error_detection.h b/error_detection/error_detection.h index 13085b5..033ec06 100644 --- a/error_detection/error_detection.h +++ b/error_detection/error_detection.h @@ -5,17 +5,18 @@ #pragma once -#include #include +#include +#include namespace agi::error_detection { /** * @brief Compute 8 bit checksum on data buffer using polynomial division based CRC-8 algorithm * - * @param data, length + * @param data * @return 8 bit CRC checksum */ -uint8_t crc8(const uint8_t* data, std::size_t length); +uint8_t crc8(std::span data); } // namespace agi::error_detection