Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file crcXXgen.c |
| 2 | * Osmocom generic CRC routines (for max XX bits poly). */ |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 3 | /* |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 4 | * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 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 | |
Harald Welte | 197a4ac | 2017-10-16 14:29:26 +0200 | [diff] [blame] | 25 | /*! \addtogroup crc |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 26 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 27 | * Osmocom generic CRC routines (for max XX bits poly). |
| 28 | * |
| 29 | * \file crcXXgen.c.tpl */ |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 30 | |
| 31 | #include <stdint.h> |
| 32 | |
| 33 | #include <osmocom/core/bits.h> |
| 34 | #include <osmocom/core/crcXXgen.h> |
| 35 | |
| 36 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 37 | /*! Compute the CRC value of a given array of hard-bits |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 38 | * \param[in] code The CRC code description to apply |
| 39 | * \param[in] in Array of hard bits |
| 40 | * \param[in] len Length of the array of hard bits |
| 41 | * \returns The CRC value |
| 42 | */ |
| 43 | uintXX_t |
| 44 | osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code, |
| 45 | const ubit_t *in, int len) |
| 46 | { |
| 47 | const uintXX_t poly = code->poly; |
| 48 | uintXX_t crc = code->init; |
| 49 | int i, n = code->bits-1; |
| 50 | |
| 51 | for (i=0; i<len; i++) { |
| 52 | uintXX_t bit = in[i] & 1; |
| 53 | crc ^= (bit << n); |
Sylvain Munaut | 9adfda2 | 2013-02-01 20:37:03 +0100 | [diff] [blame] | 54 | if (crc & ((uintXX_t)1 << n)) { |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 55 | crc <<= 1; |
| 56 | crc ^= poly; |
| 57 | } else { |
| 58 | crc <<= 1; |
| 59 | } |
Sylvain Munaut | 9adfda2 | 2013-02-01 20:37:03 +0100 | [diff] [blame] | 60 | crc &= ((uintXX_t)1 << code->bits) - 1; |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | crc ^= code->remainder; |
| 64 | |
| 65 | return crc; |
| 66 | } |
| 67 | |
| 68 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 69 | /*! Checks the CRC value of a given array of hard-bits |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 70 | * \param[in] code The CRC code description to apply |
| 71 | * \param[in] in Array of hard bits |
| 72 | * \param[in] len Length of the array of hard bits |
| 73 | * \param[in] crc_bits Array of hard bits with the alleged CRC |
| 74 | * \returns 0 if CRC matches. 1 in case of error. |
| 75 | * |
| 76 | * The crc_bits array must have a length of code->len |
| 77 | */ |
| 78 | int |
| 79 | osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code, |
| 80 | const ubit_t *in, int len, const ubit_t *crc_bits) |
| 81 | { |
| 82 | uintXX_t crc; |
| 83 | int i; |
| 84 | |
| 85 | crc = osmo_crcXXgen_compute_bits(code, in, len); |
| 86 | |
| 87 | for (i=0; i<code->bits; i++) |
| 88 | if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1)) |
| 89 | return 1; |
| 90 | |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 95 | /*! Computes and writes the CRC value of a given array of bits |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 96 | * \param[in] code The CRC code description to apply |
| 97 | * \param[in] in Array of hard bits |
| 98 | * \param[in] len Length of the array of hard bits |
| 99 | * \param[in] crc_bits Array of hard bits to write the computed CRC to |
| 100 | * |
| 101 | * The crc_bits array must have a length of code->len |
| 102 | */ |
| 103 | void |
| 104 | osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code, |
| 105 | const ubit_t *in, int len, ubit_t *crc_bits) |
| 106 | { |
| 107 | uintXX_t crc; |
| 108 | int i; |
| 109 | |
| 110 | crc = osmo_crcXXgen_compute_bits(code, in, len); |
| 111 | |
| 112 | for (i=0; i<code->bits; i++) |
| 113 | crc_bits[i] = ((crc >> (code->bits-i-1)) & 1); |
| 114 | } |
| 115 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 116 | /*! @} */ |
Sylvain Munaut | 2749c0b | 2011-09-16 22:59:18 +0200 | [diff] [blame] | 117 | |
| 118 | /* vim: set syntax=c: */ |