annotate libutil/crc8gen.c @ 240:c7a3ba56c636

themwi-short-dial: fix bug in printf arguments
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 14 Aug 2023 15:37:59 -0800
parents 05d01e810217
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
194
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*! \file crc8gen.c
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Osmocom generic CRC routines (for max 8 bits poly). */
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 /*
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com>
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 *
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 * All Rights Reserved
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 *
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 * SPDX-License-Identifier: GPL-2.0+
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 *
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 * This program is free software; you can redistribute it and/or modify
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 * it under the terms of the GNU General Public License as published by
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 * the Free Software Foundation; either version 2 of the License, or
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 * (at your option) any later version.
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 *
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 * This program is distributed in the hope that it will be useful,
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 * GNU General Public License for more details.
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 *
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 * You should have received a copy of the GNU General Public License along
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 * with this program; if not, write to the Free Software Foundation, Inc.,
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 */
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 /*! \addtogroup crc
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 * @{
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 * Osmocom generic CRC routines (for max 8 bits poly).
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 *
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 * \file crc8gen.c.tpl */
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 #include <stdint.h>
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 #include "osmo_bits.h"
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 /*! Compute the CRC value of a given array of hard-bits
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 * \param[in] code The CRC code description to apply
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 * \param[in] in Array of hard bits
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 * \param[in] len Length of the array of hard bits
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 * \returns The CRC value
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 */
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 uint8_t
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 osmo_crc8gen_compute_bits(const struct osmo_crc8gen_code *code,
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 const ubit_t *in, int len)
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 {
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 const uint8_t poly = code->poly;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 uint8_t crc = code->init;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 int i, n = code->bits-1;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 for (i=0; i<len; i++) {
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 uint8_t bit = in[i] & 1;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 crc ^= (bit << n);
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 if (crc & ((uint8_t)1 << n)) {
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 crc <<= 1;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 crc ^= poly;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 } else {
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 crc <<= 1;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 }
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 crc &= ((uint8_t)1 << code->bits) - 1;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 crc ^= code->remainder;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 return crc;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 }
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 /*! Checks the CRC value of a given array of hard-bits
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 * \param[in] code The CRC code description to apply
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 * \param[in] in Array of hard bits
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 * \param[in] len Length of the array of hard bits
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 * \param[in] crc_bits Array of hard bits with the alleged CRC
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 * \returns 0 if CRC matches. 1 in case of error.
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 *
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 * The crc_bits array must have a length of code->len
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 */
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 int
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 osmo_crc8gen_check_bits(const struct osmo_crc8gen_code *code,
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 const ubit_t *in, int len, const ubit_t *crc_bits)
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 {
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 uint8_t crc;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 int i;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 crc = osmo_crc8gen_compute_bits(code, in, len);
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 for (i=0; i<code->bits; i++)
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1))
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 return 1;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 return 0;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 /*! Computes and writes the CRC value of a given array of bits
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 * \param[in] code The CRC code description to apply
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 * \param[in] in Array of hard bits
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 * \param[in] len Length of the array of hard bits
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 * \param[in] crc_bits Array of hard bits to write the computed CRC to
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 *
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 * The crc_bits array must have a length of code->len
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 */
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 void
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 osmo_crc8gen_set_bits(const struct osmo_crc8gen_code *code,
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 const ubit_t *in, int len, ubit_t *crc_bits)
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 {
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 uint8_t crc;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 int i;
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 crc = osmo_crc8gen_compute_bits(code, in, len);
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 for (i=0; i<code->bits; i++)
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 crc_bits[i] = ((crc >> (code->bits-i-1)) & 1);
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 }
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 /*! @} */
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114
05d01e810217 libutil: add TFO message gen function based on Osmocom crc8gen
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 /* vim: set syntax=c: */