blob: 154291cc5d6652103c069626156eea774eb2169c [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.
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020019 */
20
Harald Welte197a4ac2017-10-16 14:29:26 +020021/*! \addtogroup crc
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020022 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020023 * Osmocom generic CRC routines (for max XX bits poly).
24 *
25 * \file crcXXgen.c.tpl */
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020026
27#include <stdint.h>
28
29#include <osmocom/core/bits.h>
30#include <osmocom/core/crcXXgen.h>
31
32
Neels Hofmeyr87e45502017-06-20 00:17:59 +020033/*! Compute the CRC value of a given array of hard-bits
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020034 * \param[in] code The CRC code description to apply
35 * \param[in] in Array of hard bits
36 * \param[in] len Length of the array of hard bits
37 * \returns The CRC value
38 */
39uintXX_t
40osmo_crcXXgen_compute_bits(const struct osmo_crcXXgen_code *code,
41 const ubit_t *in, int len)
42{
43 const uintXX_t poly = code->poly;
44 uintXX_t crc = code->init;
45 int i, n = code->bits-1;
46
47 for (i=0; i<len; i++) {
48 uintXX_t bit = in[i] & 1;
49 crc ^= (bit << n);
Sylvain Munaut9adfda22013-02-01 20:37:03 +010050 if (crc & ((uintXX_t)1 << n)) {
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020051 crc <<= 1;
52 crc ^= poly;
53 } else {
54 crc <<= 1;
55 }
Sylvain Munaut9adfda22013-02-01 20:37:03 +010056 crc &= ((uintXX_t)1 << code->bits) - 1;
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020057 }
58
59 crc ^= code->remainder;
60
61 return crc;
62}
63
64
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065/*! Checks the CRC value of a given array of hard-bits
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020066 * \param[in] code The CRC code description to apply
67 * \param[in] in Array of hard bits
68 * \param[in] len Length of the array of hard bits
69 * \param[in] crc_bits Array of hard bits with the alleged CRC
70 * \returns 0 if CRC matches. 1 in case of error.
71 *
72 * The crc_bits array must have a length of code->len
73 */
74int
75osmo_crcXXgen_check_bits(const struct osmo_crcXXgen_code *code,
76 const ubit_t *in, int len, const ubit_t *crc_bits)
77{
78 uintXX_t crc;
79 int i;
80
81 crc = osmo_crcXXgen_compute_bits(code, in, len);
82
83 for (i=0; i<code->bits; i++)
84 if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1))
85 return 1;
86
87 return 0;
88}
89
90
Neels Hofmeyr87e45502017-06-20 00:17:59 +020091/*! Computes and writes the CRC value of a given array of bits
Sylvain Munaut2749c0b2011-09-16 22:59:18 +020092 * \param[in] code The CRC code description to apply
93 * \param[in] in Array of hard bits
94 * \param[in] len Length of the array of hard bits
95 * \param[in] crc_bits Array of hard bits to write the computed CRC to
96 *
97 * The crc_bits array must have a length of code->len
98 */
99void
100osmo_crcXXgen_set_bits(const struct osmo_crcXXgen_code *code,
101 const ubit_t *in, int len, ubit_t *crc_bits)
102{
103 uintXX_t crc;
104 int i;
105
106 crc = osmo_crcXXgen_compute_bits(code, in, len);
107
108 for (i=0; i<code->bits; i++)
109 crc_bits[i] = ((crc >> (code->bits-i-1)) & 1);
110}
111
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200112/*! @} */
Sylvain Munaut2749c0b2011-09-16 22:59:18 +0200113
114/* vim: set syntax=c: */