FreeCalypso > hg > gsm-net-reveng
comparison trau-decode/crc8gen.c @ 4:d9c095357c32
trau-parse: check and report EFR CRC
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Fri, 24 May 2024 09:16:35 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
3:64b15810dc4c | 4:d9c095357c32 |
---|---|
1 /*! \file crc8gen.c | |
2 * Osmocom generic CRC routines (for max 8 bits poly). */ | |
3 /* | |
4 * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com> | |
5 * | |
6 * All Rights Reserved | |
7 * | |
8 * SPDX-License-Identifier: GPL-2.0+ | |
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 crc | |
26 * @{ | |
27 * Osmocom generic CRC routines (for max 8 bits poly). | |
28 * | |
29 * \file crc8gen.c.tpl */ | |
30 | |
31 #include <stdint.h> | |
32 #include "osmo_bits.h" | |
33 | |
34 /*! Compute the CRC value of a given array of hard-bits | |
35 * \param[in] code The CRC code description to apply | |
36 * \param[in] in Array of hard bits | |
37 * \param[in] len Length of the array of hard bits | |
38 * \returns The CRC value | |
39 */ | |
40 uint8_t | |
41 osmo_crc8gen_compute_bits(const struct osmo_crc8gen_code *code, | |
42 const ubit_t *in, int len) | |
43 { | |
44 const uint8_t poly = code->poly; | |
45 uint8_t crc = code->init; | |
46 int i, n = code->bits-1; | |
47 | |
48 for (i=0; i<len; i++) { | |
49 uint8_t bit = in[i] & 1; | |
50 crc ^= (bit << n); | |
51 if (crc & ((uint8_t)1 << n)) { | |
52 crc <<= 1; | |
53 crc ^= poly; | |
54 } else { | |
55 crc <<= 1; | |
56 } | |
57 crc &= ((uint8_t)1 << code->bits) - 1; | |
58 } | |
59 | |
60 crc ^= code->remainder; | |
61 | |
62 return crc; | |
63 } | |
64 | |
65 | |
66 /*! Checks the CRC value of a given array of hard-bits | |
67 * \param[in] code The CRC code description to apply | |
68 * \param[in] in Array of hard bits | |
69 * \param[in] len Length of the array of hard bits | |
70 * \param[in] crc_bits Array of hard bits with the alleged CRC | |
71 * \returns 0 if CRC matches. 1 in case of error. | |
72 * | |
73 * The crc_bits array must have a length of code->len | |
74 */ | |
75 int | |
76 osmo_crc8gen_check_bits(const struct osmo_crc8gen_code *code, | |
77 const ubit_t *in, int len, const ubit_t *crc_bits) | |
78 { | |
79 uint8_t crc; | |
80 int i; | |
81 | |
82 crc = osmo_crc8gen_compute_bits(code, in, len); | |
83 | |
84 for (i=0; i<code->bits; i++) | |
85 if (crc_bits[i] ^ ((crc >> (code->bits-i-1)) & 1)) | |
86 return 1; | |
87 | |
88 return 0; | |
89 } | |
90 | |
91 | |
92 /*! Computes and writes the CRC value of a given array of bits | |
93 * \param[in] code The CRC code description to apply | |
94 * \param[in] in Array of hard bits | |
95 * \param[in] len Length of the array of hard bits | |
96 * \param[in] crc_bits Array of hard bits to write the computed CRC to | |
97 * | |
98 * The crc_bits array must have a length of code->len | |
99 */ | |
100 void | |
101 osmo_crc8gen_set_bits(const struct osmo_crc8gen_code *code, | |
102 const ubit_t *in, int len, ubit_t *crc_bits) | |
103 { | |
104 uint8_t crc; | |
105 int i; | |
106 | |
107 crc = osmo_crc8gen_compute_bits(code, in, len); | |
108 | |
109 for (i=0; i<code->bits; i++) | |
110 crc_bits[i] = ((crc >> (code->bits-i-1)) & 1); | |
111 } | |
112 | |
113 /*! @} */ | |
114 | |
115 /* vim: set syntax=c: */ |