blob: 74e6d5219994f0c401a150b2711bac8639b7c9b4 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file crcXXgen.c
2 * Osmocom generic CRC routines (for max XX bits poly). */
Sylvain Munaut2749c0b2011-09-16 22:59:18 +02003/*
Sylvain Munaut2749c0b2011-09-16 22:59:18 +02004 * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com>
5 *
6 * All Rights Reserved
7 *
Harald Weltee08da972017-11-13 01:00:26 +09008 * SPDX-License-Identifier: GPL-2.0+
9 *
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020010 * 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 Welte197a4ac2017-10-16 14:29:26 +020025/*! \addtogroup crc
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020026 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020027 * Osmocom generic CRC routines (for max XX bits poly).
28 *
29 * \file crcXXgen.c.tpl */
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020030
31#include <stdint.h>
32
33#include <osmocom/core/bits.h>
34#include <osmocom/core/crcXXgen.h>
35
36
Neels Hofmeyr87e45502017-06-20 00:17:59 +020037/*! Compute the CRC value of a given array of hard-bits
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020038 * \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 */
43uintXX_t
44osmo_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 Munaut9adfda22013-02-01 20:37:03 +010054 if (crc & ((uintXX_t)1 << n)) {
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020055 crc <<= 1;
56 crc ^= poly;
57 } else {
58 crc <<= 1;
59 }
Sylvain Munaut9adfda22013-02-01 20:37:03 +010060 crc &= ((uintXX_t)1 << code->bits) - 1;
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020061 }
62
63 crc ^= code->remainder;
64
65 return crc;
66}
67
68
Neels Hofmeyr87e45502017-06-20 00:17:59 +020069/*! Checks the CRC value of a given array of hard-bits
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020070 * \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 */
78int
79osmo_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 Hofmeyr87e45502017-06-20 00:17:59 +020095/*! Computes and writes the CRC value of a given array of bits
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020096 * \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 */
103void
104osmo_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 Munautdca7d2c2012-04-18 21:53:23 +0200116/*! @} */
Sylvain Munaut2749c0b2011-09-16 22:59:18 +0200117
118/* vim: set syntax=c: */