XWOS API  4.0
XWOS C/C++ API参考手册
载入中...
搜索中...
未找到
crc8.c
浏览该文件的文档.
1
13#include <xwos/standard.h>
14#include <xwos/lib/xwbop.h>
15#include <xwos/lib/crc8.h>
16
17#if defined(XWLIBCFG_CRC8_0X07) && (1 == XWLIBCFG_CRC8_0X07)
22 0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15,
23 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
24 0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65,
25 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
26 0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB, 0xF2, 0xF5,
27 0xD8, 0xDF, 0xD6, 0xD1, 0xC4, 0xC3, 0xCA, 0xCD,
28 0x90, 0x97, 0x9E, 0x99, 0x8C, 0x8B, 0x82, 0x85,
29 0xA8, 0xAF, 0xA6, 0xA1, 0xB4, 0xB3, 0xBA, 0xBD,
30 0xC7, 0xC0, 0xC9, 0xCE, 0xDB, 0xDC, 0xD5, 0xD2,
31 0xFF, 0xF8, 0xF1, 0xF6, 0xE3, 0xE4, 0xED, 0xEA,
32 0xB7, 0xB0, 0xB9, 0xBE, 0xAB, 0xAC, 0xA5, 0xA2,
33 0x8F, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9D, 0x9A,
34 0x27, 0x20, 0x29, 0x2E, 0x3B, 0x3C, 0x35, 0x32,
35 0x1F, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0D, 0x0A,
36 0x57, 0x50, 0x59, 0x5E, 0x4B, 0x4C, 0x45, 0x42,
37 0x6F, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7D, 0x7A,
38 0x89, 0x8E, 0x87, 0x80, 0x95, 0x92, 0x9B, 0x9C,
39 0xB1, 0xB6, 0xBF, 0xB8, 0xAD, 0xAA, 0xA3, 0xA4,
40 0xF9, 0xFE, 0xF7, 0xF0, 0xE5, 0xE2, 0xEB, 0xEC,
41 0xC1, 0xC6, 0xCF, 0xC8, 0xDD, 0xDA, 0xD3, 0xD4,
42 0x69, 0x6E, 0x67, 0x60, 0x75, 0x72, 0x7B, 0x7C,
43 0x51, 0x56, 0x5F, 0x58, 0x4D, 0x4A, 0x43, 0x44,
44 0x19, 0x1E, 0x17, 0x10, 0x05, 0x02, 0x0B, 0x0C,
45 0x21, 0x26, 0x2F, 0x28, 0x3D, 0x3A, 0x33, 0x34,
46 0x4E, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5C, 0x5B,
47 0x76, 0x71, 0x78, 0x7F, 0x6A, 0x6D, 0x64, 0x63,
48 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2C, 0x2B,
49 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13,
50 0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB,
51 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
52 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB,
53 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3,
54};
55#endif
56
57#if defined(XWLIBCFG_CRC8_0X31) && (1 == XWLIBCFG_CRC8_0X31)
62 0x00, 0x31, 0x62, 0x53, 0xC4, 0xF5, 0xA6, 0x97,
63 0xB9, 0x88, 0xDB, 0xEA, 0x7D, 0x4C, 0x1F, 0x2E,
64 0x43, 0x72, 0x21, 0x10, 0x87, 0xB6, 0xE5, 0xD4,
65 0xFA, 0xCB, 0x98, 0xA9, 0x3E, 0x0F, 0x5C, 0x6D,
66 0x86, 0xB7, 0xE4, 0xD5, 0x42, 0x73, 0x20, 0x11,
67 0x3F, 0x0E, 0x5D, 0x6C, 0xFB, 0xCA, 0x99, 0xA8,
68 0xC5, 0xF4, 0xA7, 0x96, 0x01, 0x30, 0x63, 0x52,
69 0x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56,
91 0x78, 0x49, 0x1A, 0x2B, 0xBC, 0x8D, 0xDE, 0xEF,
92 0x82, 0xB3, 0xE0, 0xD1, 0x46, 0x77, 0x24, 0x15,
93 0x3B, 0x0A, 0x59, 0x68, 0xFF, 0xCE, 0x9D, 0xAC,
94};
95#endif
96
97#if defined(XWLIBCFG_CRC8_0X9B) && (1 == XWLIBCFG_CRC8_0X9B)
102 0x00, 0x9B, 0xAD, 0x36, 0xC1, 0x5A, 0x6C, 0xF7,
103 0x19, 0x82, 0xB4, 0x2F, 0xD8, 0x43, 0x75, 0xEE,
104 0x32, 0xA9, 0x9F, 0x04, 0xF3, 0x68, 0x5E, 0xC5,
105 0x2B, 0xB0, 0x86, 0x1D, 0xEA, 0x71, 0x47, 0xDC,
106 0x64, 0xFF, 0xC9, 0x52, 0xA5, 0x3E, 0x08, 0x93,
107 0x7D, 0xE6, 0xD0, 0x4B, 0xBC, 0x27, 0x11, 0x8A,
108 0x56, 0xCD, 0xFB, 0x60, 0x97, 0x0C, 0x3A, 0xA1,
109 0x4F, 0xD4, 0xE2, 0x79, 0x8E, 0x15, 0x23, 0xB8,
110 0xC8, 0x53, 0x65, 0xFE, 0x09, 0x92, 0xA4, 0x3F,
111 0xD1, 0x4A, 0x7C, 0xE7, 0x10, 0x8B, 0xBD, 0x26,
112 0xFA, 0x61, 0x57, 0xCC, 0x3B, 0xA0, 0x96, 0x0D,
113 0xE3, 0x78, 0x4E, 0xD5, 0x22, 0xB9, 0x8F, 0x14,
114 0xAC, 0x37, 0x01, 0x9A, 0x6D, 0xF6, 0xC0, 0x5B,
115 0xB5, 0x2E, 0x18, 0x83, 0x74, 0xEF, 0xD9, 0x42,
116 0x9E, 0x05, 0x33, 0xA8, 0x5F, 0xC4, 0xF2, 0x69,
117 0x87, 0x1C, 0x2A, 0xB1, 0x46, 0xDD, 0xEB, 0x70,
118 0x0B, 0x90, 0xA6, 0x3D, 0xCA, 0x51, 0x67, 0xFC,
119 0x12, 0x89, 0xBF, 0x24, 0xD3, 0x48, 0x7E, 0xE5,
120 0x39, 0xA2, 0x94, 0x0F, 0xF8, 0x63, 0x55, 0xCE,
121 0x20, 0xBB, 0x8D, 0x16, 0xE1, 0x7A, 0x4C, 0xD7,
122 0x6F, 0xF4, 0xC2, 0x59, 0xAE, 0x35, 0x03, 0x98,
123 0x76, 0xED, 0xDB, 0x40, 0xB7, 0x2C, 0x1A, 0x81,
124 0x5D, 0xC6, 0xF0, 0x6B, 0x9C, 0x07, 0x31, 0xAA,
125 0x44, 0xDF, 0xE9, 0x72, 0x85, 0x1E, 0x28, 0xB3,
126 0xC3, 0x58, 0x6E, 0xF5, 0x02, 0x99, 0xAF, 0x34,
127 0xDA, 0x41, 0x77, 0xEC, 0x1B, 0x80, 0xB6, 0x2D,
128 0xF1, 0x6A, 0x5C, 0xC7, 0x30, 0xAB, 0x9D, 0x06,
129 0xE8, 0x73, 0x45, 0xDE, 0x29, 0xB2, 0x84, 0x1F,
130 0xA7, 0x3C, 0x0A, 0x91, 0x66, 0xFD, 0xCB, 0x50,
131 0xBE, 0x25, 0x13, 0x88, 0x7F, 0xE4, 0xD2, 0x49,
132 0x95, 0x0E, 0x38, 0xA3, 0x54, 0xCF, 0xF9, 0x62,
133 0x8C, 0x17, 0x21, 0xBA, 0x4D, 0xD6, 0xE0, 0x7B,
134};
135#endif
136
137#if defined(SOCCFG_CRC8) && (1 == SOCCFG_CRC8)
138extern
140 bool refin, xwu8_t polynomial,
141 const xwu8_t stream[], xwsz_t * size);
142#else
143# define soc_crc8_cal(crc8, refin, plynml, stream, size) (-EOPNOTSUPP)
144#endif
145
146static __xwlib_code
148 bool refin, xwu8_t plynml,
149 const xwu8_t stream[], xwsz_t * size);
150
166static __xwlib_code
168 bool refin, xwu8_t plynml,
169 const xwu8_t stream[], xwsz_t * size)
170{
171 xwsz_t i;
172 xwsz_t total;
173 xwu8_t byte;
174 xwu8_t remainder;
175 const xwu8_t * table;
176 xwer_t rc;
177
178 // cppcheck-suppress [misra-c2012-16.6]
179 switch (plynml) {
180#if defined(XWLIBCFG_CRC8_0X07) && (1 == XWLIBCFG_CRC8_0X07)
181 case 0x7:
182 table = xwlib_crc8tbl_0x07;
183 break;
184#endif
185#if defined(XWLIBCFG_CRC8_0X31) && (1 == XWLIBCFG_CRC8_0X31)
186 case 0x31:
187 table = xwlib_crc8tbl_0x31;
188 break;
189#endif
190#if defined(XWLIBCFG_CRC8_0X9B) && (1 == XWLIBCFG_CRC8_0X9B)
191 case 0x9B:
192 table = xwlib_crc8tbl_0x9B;
193 break;
194#endif
195 default:
196 table = NULL;
197 break;
198 }
199 if (NULL != table) {
200 remainder = *crc8;
201 total = *size;
202 for (i = 0; i < total; i++) {
203 if (refin) {
204 byte = xwbop_rbit8(stream[i]);
205 } else {
206 byte = stream[i];
207 }
208 remainder = table[remainder ^ byte];
209 }
210 *crc8 = remainder;
211 *size = 0;
212 rc = XWOK;
213 } else {
214 rc = -EOPNOTSUPP;
215 }
216 return rc;
217}
218
221 xwu8_t xorout, bool refin, bool refout, xwu8_t plynml,
222 const xwu8_t stream[], xwsz_t * size)
223{
224 xwer_t rc;
225 xwsz_t total;
226 xwsz_t pos;
227 xwu8_t res;
228
229 XWOS_VALIDATE((crc8), "nullptr", -EFAULT);
230 XWOS_VALIDATE((size), "nullptr", -EFAULT);
231
232 total = *size;
233 res = *crc8;
234 pos = 0;
235 /* 使用硬件计算部分CRC8校验值 */
236 rc = soc_crc8_cal(&res, refin, plynml, &stream[0], size);
237 if (*size > 0) {
238 /* 使用软件计算剩余部分的CRC8校验值 */
239 pos = total - *size;
240 rc = xwlib_crc8_swcal(&res, refin, plynml, &stream[pos], size);
241 pos = total - *size;
242 }
243 if (XWOK == rc) {
244 if (0 == *size) {
245 if (refout) {
246 res = xwbop_rbit8(res);
247 }
248 res = res ^ xorout;
249 }
250 *crc8 = res;
251 }
252 return rc;
253}
254
256xwu8_t xwlib_crc8_calms(const xwu8_t stream[], xwsz_t * size)
257{
258 xwu8_t result;
259
260 result = (xwu8_t)0x0;
261 // cppcheck-suppress [misra-c2012-17.7]
262 xwlib_crc8_cal(&result,
263 (xwu8_t)0x0, false, false, (xwu8_t)0x07,
264 stream, size);
265 return result;
266}
static xwer_t xwlib_crc8_swcal(xwu8_t *crc8, bool refin, xwu8_t plynml, const xwu8_t stream[], xwsz_t *size)
软件方式计算数据的CRC8校验值
Definition crc8.c:167
xwer_t soc_crc8_cal(xwu8_t *crc8, bool refin, xwu8_t polynomial, const xwu8_t stream[], xwsz_t *size)
XWOS通用库:CRC8
#define __xwlib_rodata
Definition compiler.h:211
#define __xwlib_code
Definition compiler.h:199
xwu8_t xwlib_crc8_calms(const xwu8_t stream[], xwsz_t *size)
用主流CRC8参数模型计算数据的CRC8校验值
Definition crc8.c:256
xwer_t xwlib_crc8_cal(xwu8_t *crc8, xwu8_t xorout, bool refin, bool refout, xwu8_t plynml, const xwu8_t stream[], xwsz_t *size)
计算数据的CRC8校验值
Definition crc8.c:220
const xwu8_t xwlib_crc8tbl_0x07[256]
CRC8多项式0x7的查询表
Definition crc8.c:21
const xwu8_t xwlib_crc8tbl_0x9B[256]
CRC8多项式0x9B的查询表
Definition crc8.c:101
const xwu8_t xwlib_crc8tbl_0x31[256]
CRC8多项式0x31的查询表
Definition crc8.c:61
#define EFAULT
Bad address
Definition errno.h:44
#define EOPNOTSUPP
Operation not supported on socket
Definition errno.h:115
#define XWOK
No error
Definition errno.h:182
signed long xwer_t
Definition type.h:554
#define NULL
Definition type.h:28
unsigned long xwsz_t
Definition type.h:339
uint8_t xwu8_t
Definition type.h:194
xwu8_t xwbop_rbit8(xwu8_t x)
#define XWOS_VALIDATE(exp, errstr,...)
检查函数参数是否有效
Definition standard.h:76
XWOS通用库:位操作
XWOS的标准头文件