annotate libutil/crc8gen.c @ 2:26383ed8b79f

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