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