blob: e92a53817a1020550b89e211221cc0bb951a0d61 [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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23/*! \addtogroup crcgen
24 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 * Osmocom generic CRC routines (for max XX bits poly).
26 *
27 * \file crcXXgen.c.tpl */
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020028
29#include <stdint.h>
30
31#include <osmocom/core/bits.h>
32#include <osmocom/core/crcXXgen.h>
33
34
Neels Hofmeyr87e45502017-06-20 00:17:59 +020035/*! Compute the CRC value of a given array of hard-bits
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020036 * \param[in] code The CRC code description to apply
37 * \param[in] in Array of hard bits
38 * \param[in] len Length of the array of hard bits
39 * \returns The CRC value
40 */
41uintXX_t
42osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code,
43 const ubit_t *in, int len)
44{
45 const uintXX_t poly = code->poly;
46 uintXX_t crc = code->init;
47 int i, n = code->bits-1;
48
49 for (i=0; i<len; i++) {
50 uintXX_t bit = in[i] & 1;
51 crc ^= (bit << n);
Sylvain Munaut9adfda22013-02-01 20:37:03 +010052 if (crc & ((uintXX_t)1 << n)) {
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020053 crc <<= 1;
54 crc ^= poly;
55 } else {
56 crc <<= 1;
57 }
Sylvain Munaut9adfda22013-02-01 20:37:03 +010058 crc &= ((uintXX_t)1 << code->bits) - 1;
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020059 }
60
61 crc ^= code->remainder;
62
63 return crc;
64}
65
66
Neels Hofmeyr87e45502017-06-20 00:17:59 +020067/*! Checks the CRC value of a given array of hard-bits
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020068 * \param[in] code The CRC code description to apply
69 * \param[in] in Array of hard bits
70 * \param[in] len Length of the array of hard bits
71 * \param[in] crc_bits Array of hard bits with the alleged CRC
72 * \returns 0 if CRC matches. 1 in case of error.
73 *
74 * The crc_bits array must have a length of code->len
75 */
76int
77osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code,
78 const ubit_t *in, int len, const ubit_t *crc_bits)
79{
80 uintXX_t crc;
81 int i;
82
83 crc = osmo_crcXXgen_compute_bits(code, in, len);
84
85 for (i=0; i<code->bits; i++)
86 if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1))
87 return 1;
88
89 return 0;
90}
91
92
Neels Hofmeyr87e45502017-06-20 00:17:59 +020093/*! Computes and writes the CRC value of a given array of bits
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020094 * \param[in] code The CRC code description to apply
95 * \param[in] in Array of hard bits
96 * \param[in] len Length of the array of hard bits
97 * \param[in] crc_bits Array of hard bits to write the computed CRC to
98 *
99 * The crc_bits array must have a length of code->len
100 */
101void
102osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code,
103 const ubit_t *in, int len, ubit_t *crc_bits)
104{
105 uintXX_t crc;
106 int i;
107
108 crc = osmo_crcXXgen_compute_bits(code, in, len);
109
110 for (i=0; i<code->bits; i++)
111 crc_bits[i] = ((crc >> (code->bits-i-1)) & 1);
112}
113
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200114/*! @} */
Sylvain Munaut2749c0b2011-09-16 22:59:18 +0200115
116/* vim: set syntax=c: */