CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
orangepi-xunlong

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: orangepi-xunlong/orangepi-build
Path: blob/next/external/cache/sources/hcitools/monitor/crc.c
Views: 3959
1
/*
2
*
3
* BlueZ - Bluetooth protocol stack for Linux
4
*
5
* Copyright (C) 2011-2012 Intel Corporation
6
* Copyright (C) 2004-2010 Marcel Holtmann <[email protected]>
7
*
8
*
9
* This library is free software; you can redistribute it and/or
10
* modify it under the terms of the GNU Lesser General Public
11
* License as published by the Free Software Foundation; either
12
* version 2.1 of the License, or (at your option) any later version.
13
*
14
* This library is distributed in the hope that it will be useful,
15
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
* Lesser General Public License for more details.
18
*
19
* You should have received a copy of the GNU Lesser General Public
20
* License along with this library; if not, write to the Free Software
21
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
*
23
*/
24
25
#ifdef HAVE_CONFIG_H
26
#include <config.h>
27
#endif
28
29
#include "crc.h"
30
31
uint32_t crc24_bit_reverse(uint32_t value)
32
{
33
uint32_t result = 0;
34
uint8_t i;
35
36
for (i = 0; i < 24; i++)
37
result |= ((value >> i) & 1) << (23 - i);
38
39
return result;
40
}
41
42
uint32_t crc24_calculate(uint32_t preset, const uint8_t *data, uint8_t len)
43
{
44
uint32_t state = preset;
45
uint8_t i;
46
47
for (i = 0; i < len; i++) {
48
uint8_t n, cur = data[i];
49
50
for (n = 0; n < 8; n++) {
51
int next_bit = (state ^ cur) & 1;
52
53
cur >>= 1;
54
state >>= 1;
55
if (next_bit) {
56
state |= 1 << 23;
57
state ^= 0x5a6000;
58
}
59
}
60
}
61
62
return state;
63
}
64
65
uint32_t crc24_reverse(uint32_t crc, const uint8_t *data, uint8_t len)
66
{
67
uint32_t state = crc;
68
uint8_t i;
69
70
for (i = 0; i < len; i++) {
71
uint8_t n, cur = data[len - i - 1];
72
73
for (n = 0; n < 8; n++) {
74
int top_bit = state >> 23;
75
76
state = (state << 1) & 0xffffff;
77
state |= top_bit ^ ((cur >> (7 - n)) & 1);
78
if (top_bit)
79
state ^= 0xb4c000;
80
}
81
}
82
83
return state;
84
}
85
86