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