blob: 7e45c111b8446b329ec61174aca90fc034a0ebd3 [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 * @{
27 */
28
29/*! \file crcXXgen.c
Katerina Barone-Adesic28c6a02013-02-15 13:27:59 +010030 * Osmocom generic CRC routines (for max XX bits poly)
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020031 */
32
33#include <stdint.h>
34
35#include <osmocom/core/bits.h>
36#include <osmocom/core/crcXXgen.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 */
45uintXX_t
46osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code,
47 const ubit_t *in, int len)
48{
49 const uintXX_t poly = code->poly;
50 uintXX_t crc = code->init;
51 int i, n = code->bits-1;
52
53 for (i=0; i<len; i++) {
54 uintXX_t bit = in[i] & 1;
55 crc ^= (bit << n);
Sylvain Munaut9adfda22013-02-01 20:37:03 +010056 if (crc & ((uintXX_t)1 << n)) {
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020057 crc <<= 1;
58 crc ^= poly;
59 } else {
60 crc <<= 1;
61 }
Sylvain Munaut9adfda22013-02-01 20:37:03 +010062 crc &= ((uintXX_t)1 << code->bits) - 1;
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020063 }
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 */
80int
81osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code,
82 const ubit_t *in, int len, const ubit_t *crc_bits)
83{
84 uintXX_t crc;
85 int i;
86
87 crc = osmo_crcXXgen_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 */
105void
106osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code,
107 const ubit_t *in, int len, ubit_t *crc_bits)
108{
109 uintXX_t crc;
110 int i;
111
112 crc = osmo_crcXXgen_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
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200118/*! @} */
Sylvain Munaut2749c0b2011-09-16 22:59:18 +0200119
120/* vim: set syntax=c: */