Piotr Krysik | 9e2e835 | 2018-02-27 12:16:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * crc8gen.c |
| 3 | * |
| 4 | * Generic CRC routines (for max 8 bits poly) |
| 5 | * |
| 6 | * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com> |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
| 24 | |
| 25 | /*! \addtogroup crcgen |
| 26 | * @{ |
| 27 | */ |
| 28 | |
| 29 | /*! \file crc8gen.c |
| 30 | * Osmocom generic CRC routines (for max 8 bits poly) |
| 31 | */ |
| 32 | |
| 33 | #include <stdint.h> |
| 34 | |
| 35 | #include <osmocom/core/bits.h> |
| 36 | #include <osmocom/core/crc8gen.h> |
| 37 | |
| 38 | |
| 39 | /*! \brief Compute the CRC value of a given array of hard-bits |
| 40 | * \param[in] code The CRC code description to apply |
| 41 | * \param[in] in Array of hard bits |
| 42 | * \param[in] len Length of the array of hard bits |
| 43 | * \returns The CRC value |
| 44 | */ |
| 45 | uint8_t |
| 46 | osmo_crc8gen_compute_bits(const struct osmo_crc8gen_code *code, |
| 47 | const ubit_t *in, int len) |
| 48 | { |
| 49 | const uint8_t poly = code->poly; |
| 50 | uint8_t crc = code->init; |
| 51 | int i, n = code->bits-1; |
| 52 | |
| 53 | for (i=0; i<len; i++) { |
| 54 | uint8_t bit = in[i] & 1; |
| 55 | crc ^= (bit << n); |
| 56 | if (crc & ((uint8_t)1 << n)) { |
| 57 | crc <<= 1; |
| 58 | crc ^= poly; |
| 59 | } else { |
| 60 | crc <<= 1; |
| 61 | } |
| 62 | crc &= ((uint8_t)1 << code->bits) - 1; |
| 63 | } |
| 64 | |
| 65 | crc ^= code->remainder; |
| 66 | |
| 67 | return crc; |
| 68 | } |
| 69 | |
| 70 | |
| 71 | /*! \brief Checks the CRC value of a given array of hard-bits |
| 72 | * \param[in] code The CRC code description to apply |
| 73 | * \param[in] in Array of hard bits |
| 74 | * \param[in] len Length of the array of hard bits |
| 75 | * \param[in] crc_bits Array of hard bits with the alleged CRC |
| 76 | * \returns 0 if CRC matches. 1 in case of error. |
| 77 | * |
| 78 | * The crc_bits array must have a length of code->len |
| 79 | */ |
| 80 | int |
| 81 | osmo_crc8gen_check_bits(const struct osmo_crc8gen_code *code, |
| 82 | const ubit_t *in, int len, const ubit_t *crc_bits) |
| 83 | { |
| 84 | uint8_t crc; |
| 85 | int i; |
| 86 | |
| 87 | crc = osmo_crc8gen_compute_bits(code, in, len); |
| 88 | |
| 89 | for (i=0; i<code->bits; i++) |
| 90 | if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1)) |
| 91 | return 1; |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | /*! \brief Computes and writes the CRC value of a given array of bits |
| 98 | * \param[in] code The CRC code description to apply |
| 99 | * \param[in] in Array of hard bits |
| 100 | * \param[in] len Length of the array of hard bits |
| 101 | * \param[in] crc_bits Array of hard bits to write the computed CRC to |
| 102 | * |
| 103 | * The crc_bits array must have a length of code->len |
| 104 | */ |
| 105 | void |
| 106 | osmo_crc8gen_set_bits(const struct osmo_crc8gen_code *code, |
| 107 | const ubit_t *in, int len, ubit_t *crc_bits) |
| 108 | { |
| 109 | uint8_t crc; |
| 110 | int i; |
| 111 | |
| 112 | crc = osmo_crc8gen_compute_bits(code, in, len); |
| 113 | |
| 114 | for (i=0; i<code->bits; i++) |
| 115 | crc_bits[i] = ((crc >> (code->bits-i-1)) & 1); |
| 116 | } |
| 117 | |
| 118 | /*! @} */ |
| 119 | |
| 120 | /* vim: set syntax=c: */ |