blob: ea69d88b446c5e1954aa1e088e8bdc42b554ecb7 [file] [log] [blame]
Piotr Krysik9e2e8352018-02-27 12:16:25 +01001/*
2 * crc16gen.c
3 *
4 * Generic CRC routines (for max 16 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 crc16gen.c
30 * Osmocom generic CRC routines (for max 16 bits poly)
31 */
32
33#include <stdint.h>
34
35#include <osmocom/core/bits.h>
36#include <osmocom/core/crc16gen.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 */
45uint16_t
46osmo_crc16gen_compute_bits(const struct osmo_crc16gen_code *code,
47 const ubit_t *in, int len)
48{
49 const uint16_t poly = code->poly;
50 uint16_t crc = code->init;
51 int i, n = code->bits-1;
52
53 for (i=0; i<len; i++) {
54 uint16_t bit = in[i] & 1;
55 crc ^= (bit << n);
56 if (crc & ((uint16_t)1 << n)) {
57 crc <<= 1;
58 crc ^= poly;
59 } else {
60 crc <<= 1;
61 }
62 crc &= ((uint16_t)1 << code->bits) - 1;
63 }
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_crc16gen_check_bits(const struct osmo_crc16gen_code *code,
82 const ubit_t *in, int len, const ubit_t *crc_bits)
83{
84 uint16_t crc;
85 int i;
86
87 crc = osmo_crc16gen_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_crc16gen_set_bits(const struct osmo_crc16gen_code *code,
107 const ubit_t *in, int len, ubit_t *crc_bits)
108{
109 uint16_t crc;
110 int i;
111
112 crc = osmo_crc16gen_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
118/*! @} */
119
120/* vim: set syntax=c: */