blob: 1a69e8529ceab8f809d2923137ddd65619976475 [file] [log] [blame]
Sylvain Munaut2749c0b2011-09-16 22:59:18 +02001/*
2 * crcXXgen.c
3 *
4 * Generic CRC routines (for max XX 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 * @{
Harald Welte96e2a002017-06-12 21:44:18 +020027 * \brief Osmocom generic CRC routines
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020028 */
29
30/*! \file crcXXgen.c
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010031 * Osmocom generic CRC routines (for max XX bits poly)
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020032 */
33
34#include <stdint.h>
35
36#include <osmocom/core/bits.h>
37#include <osmocom/core/crcXXgen.h>
38
39
40/*! \brief Compute the CRC value of a given array of hard-bits
41 * \param[in] code The CRC code description to apply
42 * \param[in] in Array of hard bits
43 * \param[in] len Length of the array of hard bits
44 * \returns The CRC value
45 */
46uintXX_t
47osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code,
48 const ubit_t *in, int len)
49{
50 const uintXX_t poly = code->poly;
51 uintXX_t crc = code->init;
52 int i, n = code->bits-1;
53
54 for (i=0; i<len; i++) {
55 uintXX_t bit = in[i] & 1;
56 crc ^= (bit << n);
Sylvain Munaut9adfda22013-02-01 20:37:03 +010057 if (crc & ((uintXX_t)1 << n)) {
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020058 crc <<= 1;
59 crc ^= poly;
60 } else {
61 crc <<= 1;
62 }
Sylvain Munaut9adfda22013-02-01 20:37:03 +010063 crc &= ((uintXX_t)1 << code->bits) - 1;
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020064 }
65
66 crc ^= code->remainder;
67
68 return crc;
69}
70
71
72/*! \brief Checks the CRC value of a given array of hard-bits
73 * \param[in] code The CRC code description to apply
74 * \param[in] in Array of hard bits
75 * \param[in] len Length of the array of hard bits
76 * \param[in] crc_bits Array of hard bits with the alleged CRC
77 * \returns 0 if CRC matches. 1 in case of error.
78 *
79 * The crc_bits array must have a length of code->len
80 */
81int
82osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code,
83 const ubit_t *in, int len, const ubit_t *crc_bits)
84{
85 uintXX_t crc;
86 int i;
87
88 crc = osmo_crcXXgen_compute_bits(code, in, len);
89
90 for (i=0; i<code->bits; i++)
91 if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1))
92 return 1;
93
94 return 0;
95}
96
97
98/*! \brief Computes and writes the CRC value of a given array of bits
99 * \param[in] code The CRC code description to apply
100 * \param[in] in Array of hard bits
101 * \param[in] len Length of the array of hard bits
102 * \param[in] crc_bits Array of hard bits to write the computed CRC to
103 *
104 * The crc_bits array must have a length of code->len
105 */
106void
107osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code,
108 const ubit_t *in, int len, ubit_t *crc_bits)
109{
110 uintXX_t crc;
111 int i;
112
113 crc = osmo_crcXXgen_compute_bits(code, in, len);
114
115 for (i=0; i<code->bits; i++)
116 crc_bits[i] = ((crc >> (code->bits-i-1)) & 1);
117}
118
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200119/*! @} */
Sylvain Munaut2749c0b2011-09-16 22:59:18 +0200120
121/* vim: set syntax=c: */