|
| 1 | +/* |
| 2 | + * Copyright (c) 2018 Workaround GmbH. |
| 3 | + * Copyright (c) 2017 Intel Corporation. |
| 4 | + * Copyright (c) 2017 Nordic Semiconductor ASA |
| 5 | + * Copyright (c) 2015 Runtime Inc |
| 6 | + * |
| 7 | + * SPDX-License-Identifier: Apache-2.0 |
| 8 | + */ |
| 9 | +/** @file |
| 10 | + * @brief CRC computation function |
| 11 | + */ |
| 12 | + |
| 13 | +#ifndef ZEPHYR_INCLUDE_CRC_H_ |
| 14 | +#define ZEPHYR_INCLUDE_CRC_H_ |
| 15 | + |
| 16 | +#include <zephyr/types.h> |
| 17 | +#include <stdbool.h> |
| 18 | +#include <stddef.h> |
| 19 | + |
| 20 | +#ifdef __cplusplus |
| 21 | +extern "C" { |
| 22 | +#endif |
| 23 | + |
| 24 | +/* Initial value expected to be used at the beginning of the crc8_ccitt |
| 25 | + * computation. |
| 26 | + */ |
| 27 | +#define CRC8_CCITT_INITIAL_VALUE 0xFF |
| 28 | + |
| 29 | +/** |
| 30 | + * @defgroup checksum Checksum |
| 31 | + */ |
| 32 | + |
| 33 | +/** |
| 34 | + * @defgroup crc CRC |
| 35 | + * @ingroup checksum |
| 36 | + * @{ |
| 37 | + */ |
| 38 | + |
| 39 | +/** |
| 40 | + * @brief Generic function for computing CRC 16 |
| 41 | + * |
| 42 | + * Compute CRC 16 by passing in the address of the input, the input length |
| 43 | + * and polynomial used in addition to the initial value. |
| 44 | + * |
| 45 | + * @param src Input bytes for the computation |
| 46 | + * @param len Length of the input in bytes |
| 47 | + * @param polynomial The polynomial to use omitting the leading x^16 |
| 48 | + * coefficient |
| 49 | + * @param initial_value Initial value for the CRC computation |
| 50 | + * @param pad Adds padding with zeros at the end of input bytes |
| 51 | + * |
| 52 | + * @return The computed CRC16 value |
| 53 | + */ |
| 54 | +u16_t crc16(const u8_t *src, size_t len, u16_t polynomial, |
| 55 | + u16_t initial_value, bool pad); |
| 56 | + |
| 57 | +/** |
| 58 | + * @brief Compute the CRC-16/CCITT checksum of a buffer. |
| 59 | + * |
| 60 | + * See ITU-T Recommendation V.41 (November 1988). Uses 0x1021 as the |
| 61 | + * polynomial, reflects the input, and reflects the output. |
| 62 | + * |
| 63 | + * To calculate the CRC across non-contiguous blocks use the return |
| 64 | + * value from block N-1 as the seed for block N. |
| 65 | + * |
| 66 | + * For CRC-16/CCITT, use 0 as the initial seed. Other checksums in |
| 67 | + * the same family can be calculated by changing the seed and/or |
| 68 | + * XORing the final value. Examples include: |
| 69 | + * |
| 70 | + * - X-25 (used in PPP): seed=0xffff, xor=0xffff, residual=0xf0b8 |
| 71 | + * |
| 72 | + * @note API changed in Zephyr 1.11. |
| 73 | + * |
| 74 | + * @param seed Value to seed the CRC with |
| 75 | + * @param src Input bytes for the computation |
| 76 | + * @param len Length of the input in bytes |
| 77 | + * |
| 78 | + * @return The computed CRC16 value |
| 79 | + */ |
| 80 | +u16_t crc16_ccitt(u16_t seed, const u8_t *src, size_t len); |
| 81 | + |
| 82 | +/** |
| 83 | + * @brief Compute the CRC-16/XMODEM checksum of a buffer. |
| 84 | + * |
| 85 | + * The MSB first version of ITU-T Recommendation V.41 (November 1988). |
| 86 | + * Uses 0x1021 as the polynomial with no reflection. |
| 87 | + * |
| 88 | + * To calculate the CRC across non-contiguous blocks use the return |
| 89 | + * value from block N-1 as the seed for block N. |
| 90 | + * |
| 91 | + * For CRC-16/XMODEM, use 0 as the initial seed. Other checksums in |
| 92 | + * the same family can be calculated by changing the seed and/or |
| 93 | + * XORing the final value. Examples include: |
| 94 | + * |
| 95 | + * - CCIITT-FALSE: seed=0xffff |
| 96 | + * - GSM: seed=0, xorout=0xffff, residue=0x1d0f |
| 97 | + * |
| 98 | + * @param seed Value to seed the CRC with |
| 99 | + * @param src Input bytes for the computation |
| 100 | + * @param len Length of the input in bytes |
| 101 | + * |
| 102 | + * @return The computed CRC16 value |
| 103 | + */ |
| 104 | +u16_t crc16_itu_t(u16_t seed, const u8_t *src, size_t len); |
| 105 | + |
| 106 | +/** |
| 107 | + * @brief Compute ANSI variant of CRC 16 |
| 108 | + * |
| 109 | + * ANSI variant of CRC 16 is using 0x8005 as its polynomial with the initial |
| 110 | + * value set to 0xffff. |
| 111 | + * |
| 112 | + * @param src Input bytes for the computation |
| 113 | + * @param len Length of the input in bytes |
| 114 | + * |
| 115 | + * @return The computed CRC16 value |
| 116 | + */ |
| 117 | +static inline u16_t crc16_ansi(const u8_t *src, size_t len) |
| 118 | +{ |
| 119 | + return crc16(src, len, 0x8005, 0xffff, true); |
| 120 | +} |
| 121 | + |
| 122 | +/** |
| 123 | + * @brief Generate IEEE conform CRC32 checksum. |
| 124 | + * |
| 125 | + * @param *data Pointer to data on which the CRC should be calculated. |
| 126 | + * @param len Data length. |
| 127 | + * |
| 128 | + * @return CRC32 value. |
| 129 | + * |
| 130 | + */ |
| 131 | +u32_t crc32_ieee(const u8_t *data, size_t len); |
| 132 | + |
| 133 | +/** |
| 134 | + * @brief Update an IEEE conforming CRC32 checksum. |
| 135 | + * |
| 136 | + * @param crc CRC32 checksum that needs to be updated. |
| 137 | + * @param *data Pointer to data on which the CRC should be calculated. |
| 138 | + * @param len Data length. |
| 139 | + * |
| 140 | + * @return CRC32 value. |
| 141 | + * |
| 142 | + */ |
| 143 | +u32_t crc32_ieee_update(u32_t crc, const u8_t *data, size_t len); |
| 144 | + |
| 145 | +/** |
| 146 | + * @brief Compute CCITT variant of CRC 8 |
| 147 | + * |
| 148 | + * Normal CCITT variant of CRC 8 is using 0x07. |
| 149 | + * |
| 150 | + * @param initial_value Initial value for the CRC computation |
| 151 | + * @param buf Input bytes for the computation |
| 152 | + * @param len Length of the input in bytes |
| 153 | + * |
| 154 | + * @return The computed CRC8 value |
| 155 | + */ |
| 156 | +u8_t crc8_ccitt(u8_t initial_value, const void *buf, size_t len); |
| 157 | + |
| 158 | +/** |
| 159 | + * @} |
| 160 | + */ |
| 161 | + |
| 162 | +#ifdef __cplusplus |
| 163 | +} |
| 164 | +#endif |
| 165 | + |
| 166 | +#endif |
0 commit comments