Annotation of libelwix/src/crc.c, revision 1.8.2.1
1.1 misho 1: /*************************************************************************
2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.8.2.1 ! misho 6: * $Id: crc.c,v 1.8 2024/10/12 16:08:18 misho Exp $
1.1 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.7 misho 15: Copyright 2004 - 2024
1.1 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
1.6 misho 49: /* CRC16 poly */
50: const u_short crc_16poly = 0x1021;
51:
1.1 misho 52: /* Adler module */
53: const u_int crc_modAdler = 0xFFF1L;
54:
55: /* All known library CRC types ... */
56: const crcPoly_t crc_Poly[] = {
57: { 1, (u_int) 0x1, "CRC-1-Parity" },
58: { 4, (u_int) 0x3, "CRC-4-ITU" },
59: { 5, (u_int) 0x15, "CRC-5-ITU" },
60: { 6, (u_int) 0x3, "CRC-6-ITU" },
61: { 7, (u_int) 0x9, "CRC-7-MMC" },
1.8 misho 62: { 8, (u_int) 0x7, "CRC-8-CCITT" },
1.1 misho 63: { 10, (u_int) 0x233, "CRC-10" },
64: { 11, (u_int) 0x385, "CRC-11-FlexRay" },
65: { 12, (u_int) 0x80F, "CRC-12-Telco" },
66: { 15, (u_int) 0x4599, "CRC-15-CAN" },
67: { 16, (u_int) 0x8005, "CRC-16-IBM" },
68: { 24, (u_int) 0x864CFB, "CRC-24-Radix64" },
69: { 30, (u_int) 0x2030B9C7, "CRC-30-CDMA" },
1.7 misho 70: { 32, (u_int) 0x04C11DB7, "CRC-32-802.3" },
71: { 16, (u_int) 0x1021, "CRC-16-CCITT" },
72: { 16, (u_int) 0x8408, "CRC-16-XMODEM" }
1.1 misho 73: };
74:
75:
76: /*
77: * crcReflect() - Reflect all bits of number
78: *
79: * @crcNum = Number for reflection
80: * @crcBits = Number width bits
1.6 misho 81: * return: reflecting number
1.1 misho 82: */
1.5 misho 83: u_int
1.1 misho 84: crcReflect(u_int crcNum, u_char crcBits)
85: {
1.2 misho 86: register u_int i, j = 1, rev = 0;
1.1 misho 87:
1.2 misho 88: for (i = (u_int) 1 << (crcBits - 1); i; i >>= 1, j <<= 1)
1.1 misho 89: if (crcNum & i)
90: rev |= j;
91: return rev;
92: }
93:
94: /*
95: * crcCalc() - Generic CRC calculation function for many sub variants of CRC algorithms
96: *
97: * @psBuf = Data for calculation
98: * @bufLen = Length of data
99: * @crcBits = CRC algorithm bits (1, 4, 5, 6, 7, 8, 10, 11, 12, 15, 16, 24, 30, 32)
100: * @RevOpts = Options for computation (REVOPTS_REVERTBYTE, REVOPTS_REVERTCRC)
101: * @initCRC = Initial CRC value
102: * @xorCRC = Last xor CRC value
1.6 misho 103: * return: CRC checksum
1.1 misho 104: */
1.5 misho 105: u_int
1.1 misho 106: crcCalc(u_char * __restrict psBuf, u_int bufLen, u_char crcBits, u_char RevOpts, u_int initCRC, u_int xorCRC)
107: {
1.7 misho 108: u_int bits = sizeof(int) * 8;
1.1 misho 109: u_int poly, crchibit, crc;
110: register u_int i, j, b, ch;
111:
112: assert(psBuf);
113:
114: switch (crcBits) {
115: case 1:
116: poly = crc_Poly[0].poly_num;
1.7 misho 117: bits -= crc_Poly[0].poly_bits;
1.1 misho 118: break;
119: case 4:
120: poly = crc_Poly[1].poly_num;
1.7 misho 121: bits -= crc_Poly[1].poly_bits;
1.1 misho 122: break;
123: case 5:
124: poly = crc_Poly[2].poly_num;
1.7 misho 125: bits -= crc_Poly[2].poly_bits;
1.1 misho 126: break;
127: case 6:
128: poly = crc_Poly[3].poly_num;
1.7 misho 129: bits -= crc_Poly[3].poly_bits;
1.1 misho 130: break;
131: case 7:
132: poly = crc_Poly[4].poly_num;
1.7 misho 133: bits -= crc_Poly[4].poly_bits;
1.1 misho 134: break;
135: case 8:
136: poly = crc_Poly[5].poly_num;
1.7 misho 137: bits -= crc_Poly[5].poly_bits;
1.1 misho 138: break;
139: case 10:
140: poly = crc_Poly[6].poly_num;
1.7 misho 141: bits -= crc_Poly[6].poly_bits;
1.1 misho 142: break;
143: case 11:
144: poly = crc_Poly[7].poly_num;
1.7 misho 145: bits -= crc_Poly[7].poly_bits;
1.1 misho 146: break;
147: case 12:
148: poly = crc_Poly[8].poly_num;
1.7 misho 149: bits -= crc_Poly[8].poly_bits;
1.1 misho 150: break;
151: case 15:
152: poly = crc_Poly[9].poly_num;
1.7 misho 153: bits -= crc_Poly[9].poly_bits;
1.1 misho 154: break;
155: case 16:
156: poly = crc_Poly[10].poly_num;
1.7 misho 157: bits -= crc_Poly[10].poly_bits;
1.1 misho 158: break;
159: case 24:
160: poly = crc_Poly[11].poly_num;
1.7 misho 161: bits -= crc_Poly[11].poly_bits;
1.1 misho 162: break;
163: case 30:
164: poly = crc_Poly[12].poly_num;
1.7 misho 165: bits -= crc_Poly[12].poly_bits;
1.1 misho 166: break;
167: case 32:
168: poly = crc_Poly[13].poly_num;
1.7 misho 169: bits -= crc_Poly[13].poly_bits;
170: break;
171: case 161:
172: poly = crc_Poly[14].poly_num;
173: bits -= crc_Poly[14].poly_bits;
174: crcBits = crc_Poly[14].poly_bits;
175: break;
176: case 162:
177: poly = crc_Poly[15].poly_num;
178: bits -= crc_Poly[15].poly_bits;
179: crcBits = crc_Poly[15].poly_bits;
1.1 misho 180: break;
181: default:
182: elwix_SetErr(EINVAL, "crcCalc(): Unsupported CRC method!!!");
183: return -1;
184: }
185: poly <<= bits;
186:
187: crchibit = (u_int) 1 << (crcBits - 1);
188: crchibit <<= bits;
189: crc = initCRC << bits;
190:
191: for (i = 0; i < bufLen; i++) {
192: ch = (u_int) *psBuf++;
193: if (RevOpts & REVOPTS_REVERTBYTE)
194: ch = crcReflect(ch, 8);
195:
196: for (j = 0x80; j; j >>= 1) {
197: b = crc & crchibit;
198: crc <<= 1;
199:
200: if (ch & j)
201: b ^= crchibit;
202: if (b)
203: crc ^= poly;
204: }
205: }
206:
207: if (RevOpts & REVOPTS_REVERTCRC)
208: crc = crcReflect(crc, sizeof(int) * 8);
209: crc ^= xorCRC << bits;
210: crc &= (((crchibit - 1) << 1) | 1);
211: if (!(RevOpts & REVOPTS_REVERTCRC))
212: crc >>= bits;
213:
214: return crc;
215: }
216:
217:
218: /*
1.7 misho 219: * crc16_xy() - Checksum calculation in X/Y modem communication
1.6 misho 220: *
221: * @buf = Data for calculation
222: * @bufLen = Length of data
223: * return: Checksum
224: */
225: u_short
1.7 misho 226: crc16_xy(u_char * __restrict buf, int bufLen)
1.6 misho 227: {
228: u_short crc, x;
229: register u_short i;
230:
231: assert(buf);
232:
233: for (crc = 0; bufLen > 0; bufLen--, buf++)
234: for (i = 0x80; i; i >>= 1) {
235: x = crc >> 15;
236: crc <<= 1;
237:
238: if (*buf & i)
239: crc++;
240: if (x)
241: crc ^= crc_16poly;
242: }
243:
244: for (i = 0; i < 16; i++) {
245: x = crc >> 15;
246: crc <<= 1;
247:
248: if (x)
249: crc ^= crc_16poly;
250: }
251:
252: return crc;
253: }
254:
255: /*
1.7 misho 256: * crc16_ccitt() - Checksum calculation
1.6 misho 257: *
258: * @buf = Data for calculation
259: * @bufLen = Length of data
260: * return: Checksum
261: */
262: u_short
1.7 misho 263: crc16_ccitt(u_char * __restrict buf, int bufLen)
1.6 misho 264: {
265: u_short crc;
266: register u_char i;
267:
268: for (crc = 0; bufLen > 0; bufLen--, buf++) {
269: crc ^= (u_short) *buf << 8;
270: for (i = 0x80; i; i >>= 1)
271: if (crc & 0x8000)
272: crc = crc << 1 ^ crc_16poly;
273: else
274: crc <<= 1;
275: }
276:
277: return crc;
278: }
279:
280: /*
1.1 misho 281: * crcIP() - Checksum in IP communication
282: *
283: * @buf = Data for calculation
1.8.2.1 ! misho 284: * @len = Length of data in bytes
1.6 misho 285: * return: Checksum
1.1 misho 286: */
1.5 misho 287: u_short
1.8.2.1 ! misho 288: crcIP(u_short* __restrict buf, int len)
1.1 misho 289: {
1.8.2.1 ! misho 290: register u_long sum = 0;
1.1 misho 291:
292: assert(buf);
293:
1.8.2.1 ! misho 294: for (sum = 0; len > 1; len -= 2)
! 295: sum += *buf++;
! 296: if (len > 0)
! 297: sum += ((*buf) & htons(0xFF00));
1.1 misho 298:
299: sum = (sum >> 16) + (sum & 0xFFFF);
300: sum += sum >> 16;
301:
1.8.2.1 ! misho 302: return (u_short) (~sum);
1.1 misho 303: }
304:
305: /*
1.5 misho 306: * crcTCP() - Checksum for TCP v4 communication
307: *
308: * @buf = Data for calculation
309: * @bufLen = Length of data
310: * @th = TCP header
1.6 misho 311: * return: Checksum
1.5 misho 312: */
313: u_short
314: crcTCP(struct in_addr src, struct in_addr dst, u_char * __restrict th)
315: {
316: struct psd_tcp {
317: struct in_addr src;
318: struct in_addr dst;
319: u_char pad;
320: u_char proto;
321: u_short tcp_len;
322: u_char tcp[20];
323: } buf;
324:
325: buf.src = src;
326: buf.dst = dst;
327: buf.pad = 0;
328: buf.proto = IPPROTO_TCP;
329: buf.tcp_len = htons(sizeof buf.tcp);
330: memcpy(&buf.tcp, th, sizeof buf.tcp);
331:
1.8.2.1 ! misho 332: return crcIP((u_short*) &buf, sizeof buf);
1.5 misho 333: }
334:
335: /*
336: * crcUDP() - Checksum for UDP v4 communication
337: *
338: * @buf = Data for calculation
339: * @bufLen = Length of data
340: * @uh = UDP header
1.6 misho 341: * return: Checksum
1.5 misho 342: */
343: u_short
344: crcUDP(struct in_addr src, struct in_addr dst, u_char * __restrict uh)
345: {
346: struct psd_udp {
347: struct in_addr src;
348: struct in_addr dst;
349: u_char pad;
350: u_char proto;
351: u_short udp_len;
352: u_char udp[8];
353: } buf;
354:
355: buf.src = src;
356: buf.dst = dst;
357: buf.pad = 0;
358: buf.proto = IPPROTO_UDP;
359: buf.udp_len = htons(sizeof buf.udp);
360: memcpy(&buf.udp, uh, sizeof buf.udp);
361:
1.8.2.1 ! misho 362: return crcIP((u_short*) &buf, sizeof buf);
1.5 misho 363: }
364:
365:
366: /*
1.1 misho 367: * crcFletcher16() - Fletcher-16 Checksum computing
368: *
369: * @nBuf = Data for calculation
370: * @bufLen = Length of data
1.6 misho 371: * return: Checksum
1.1 misho 372: */
1.5 misho 373: u_short
1.1 misho 374: crcFletcher16(u_short * __restrict nBuf, int bufLen)
375: {
376: register u_short s1, s2;
377: register u_int clen;
378:
379: assert(nBuf);
380:
381: s1 = s2 = 0xFF;
382: while (bufLen) {
383: clen = bufLen > MAX_FLETCHER16_DIGEST ? MAX_FLETCHER16_DIGEST : bufLen;
384: bufLen -= clen;
385:
386: do {
387: s1 += (u_short) *nBuf++;
388: s2 += s1;
389: } while (--clen);
390:
391: s1 = (s1 >> 8) + (s1 & 0xFF);
392: s2 = (s2 >> 8) + (s2 & 0xFF);
393: }
394:
395: return (s2 << 8) | s1;
396: }
397:
398: /*
399: * crcFletcher() - Fletcher-32 Checksum computing
400: *
401: * @nBuf = Data for calculation
402: * @bufLen = Length of data
1.6 misho 403: * return: Checksum
1.1 misho 404: */
1.5 misho 405: u_int
1.1 misho 406: crcFletcher(u_short * __restrict nBuf, int bufLen)
407: {
408: register u_int s1, s2, clen;
409:
410: assert(nBuf);
411:
412: s1 = s2 = 0xFFFF;
413: while (bufLen) {
414: clen = bufLen > MAX_FLETCHER_DIGEST ? MAX_FLETCHER_DIGEST : bufLen;
415: bufLen -= clen;
416:
417: do {
418: s1 += (u_int) *nBuf++;
419: s2 += s1;
420: } while (--clen);
421:
422: s1 = (s1 >> 16) + (s1 & 0xFFFF);
423: s2 = (s2 >> 16) + (s2 & 0xFFFF);
424: }
425:
426: return (s2 << 16) | s1;
427: }
428:
429: /*
430: * crcAdler() - crcAdler-32 Checksum computing
431: *
432: * @psBuf = Data for calculation
433: * @bufLen = Length of data
1.6 misho 434: * return: Checksum
1.1 misho 435: */
1.5 misho 436: u_int
1.1 misho 437: crcAdler(u_char * __restrict psBuf, int bufLen)
438: {
1.2 misho 439: register u_int s1 = 1, s2 = 0, clen;
1.1 misho 440:
441: assert(psBuf);
442:
443: while (bufLen) {
444: clen = bufLen > MAX_ADLER_DIGEST ? MAX_ADLER_DIGEST : bufLen;
445: bufLen -= clen;
446:
447: do {
448: s1 += (u_int) *psBuf++;
449: s2 += s1;
450: } while (--clen);
451:
452: s1 %= crc_modAdler;
453: s2 %= crc_modAdler;
454: }
455:
456: return (s2 << 16) | s1;
457: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>