Annotation of libelwix/src/crc.c, revision 1.7.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.7.2.1 ! misho 6: * $Id: crc.c,v 1.7 2024/10/10 23:55:48 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.7.2.1 ! 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
284: * @bufLen = Length of data
1.6 misho 285: * return: Checksum
1.1 misho 286: */
1.5 misho 287: u_short
1.1 misho 288: crcIP(u_char * __restrict buf, int bufLen)
289: {
290: register u_int sum;
291: u_short last = 0, *nBuf = (u_short*) buf;
292:
293: assert(buf);
294:
1.5 misho 295: for (sum = 0; bufLen > 1; bufLen -= 2)
1.1 misho 296: sum += *nBuf++;
297: if (bufLen == 1) {
298: *(u_char*)(&last) += *(u_char*) nBuf;
299: sum += last;
300: }
301:
302: sum = (sum >> 16) + (sum & 0xFFFF);
303: sum += sum >> 16;
304:
305: return (u_short) ~sum;
306: }
307:
308: /*
1.5 misho 309: * crcTCP() - Checksum for TCP v4 communication
310: *
311: * @buf = Data for calculation
312: * @bufLen = Length of data
313: * @th = TCP header
1.6 misho 314: * return: Checksum
1.5 misho 315: */
316: u_short
317: crcTCP(struct in_addr src, struct in_addr dst, u_char * __restrict th)
318: {
319: struct psd_tcp {
320: struct in_addr src;
321: struct in_addr dst;
322: u_char pad;
323: u_char proto;
324: u_short tcp_len;
325: u_char tcp[20];
326: } buf;
327:
328: buf.src = src;
329: buf.dst = dst;
330: buf.pad = 0;
331: buf.proto = IPPROTO_TCP;
332: buf.tcp_len = htons(sizeof buf.tcp);
333: memcpy(&buf.tcp, th, sizeof buf.tcp);
334:
335: return crcIP((u_char*) &buf, sizeof buf);
336: }
337:
338: /*
339: * crcUDP() - Checksum for UDP v4 communication
340: *
341: * @buf = Data for calculation
342: * @bufLen = Length of data
343: * @uh = UDP header
1.6 misho 344: * return: Checksum
1.5 misho 345: */
346: u_short
347: crcUDP(struct in_addr src, struct in_addr dst, u_char * __restrict uh)
348: {
349: struct psd_udp {
350: struct in_addr src;
351: struct in_addr dst;
352: u_char pad;
353: u_char proto;
354: u_short udp_len;
355: u_char udp[8];
356: } buf;
357:
358: buf.src = src;
359: buf.dst = dst;
360: buf.pad = 0;
361: buf.proto = IPPROTO_UDP;
362: buf.udp_len = htons(sizeof buf.udp);
363: memcpy(&buf.udp, uh, sizeof buf.udp);
364:
365: return crcIP((u_char*) &buf, sizeof buf);
366: }
367:
368:
369: /*
1.1 misho 370: * crcFletcher16() - Fletcher-16 Checksum computing
371: *
372: * @nBuf = Data for calculation
373: * @bufLen = Length of data
1.6 misho 374: * return: Checksum
1.1 misho 375: */
1.5 misho 376: u_short
1.1 misho 377: crcFletcher16(u_short * __restrict nBuf, int bufLen)
378: {
379: register u_short s1, s2;
380: register u_int clen;
381:
382: assert(nBuf);
383:
384: s1 = s2 = 0xFF;
385: while (bufLen) {
386: clen = bufLen > MAX_FLETCHER16_DIGEST ? MAX_FLETCHER16_DIGEST : bufLen;
387: bufLen -= clen;
388:
389: do {
390: s1 += (u_short) *nBuf++;
391: s2 += s1;
392: } while (--clen);
393:
394: s1 = (s1 >> 8) + (s1 & 0xFF);
395: s2 = (s2 >> 8) + (s2 & 0xFF);
396: }
397:
398: return (s2 << 8) | s1;
399: }
400:
401: /*
402: * crcFletcher() - Fletcher-32 Checksum computing
403: *
404: * @nBuf = Data for calculation
405: * @bufLen = Length of data
1.6 misho 406: * return: Checksum
1.1 misho 407: */
1.5 misho 408: u_int
1.1 misho 409: crcFletcher(u_short * __restrict nBuf, int bufLen)
410: {
411: register u_int s1, s2, clen;
412:
413: assert(nBuf);
414:
415: s1 = s2 = 0xFFFF;
416: while (bufLen) {
417: clen = bufLen > MAX_FLETCHER_DIGEST ? MAX_FLETCHER_DIGEST : bufLen;
418: bufLen -= clen;
419:
420: do {
421: s1 += (u_int) *nBuf++;
422: s2 += s1;
423: } while (--clen);
424:
425: s1 = (s1 >> 16) + (s1 & 0xFFFF);
426: s2 = (s2 >> 16) + (s2 & 0xFFFF);
427: }
428:
429: return (s2 << 16) | s1;
430: }
431:
432: /*
433: * crcAdler() - crcAdler-32 Checksum computing
434: *
435: * @psBuf = Data for calculation
436: * @bufLen = Length of data
1.6 misho 437: * return: Checksum
1.1 misho 438: */
1.5 misho 439: u_int
1.1 misho 440: crcAdler(u_char * __restrict psBuf, int bufLen)
441: {
1.2 misho 442: register u_int s1 = 1, s2 = 0, clen;
1.1 misho 443:
444: assert(psBuf);
445:
446: while (bufLen) {
447: clen = bufLen > MAX_ADLER_DIGEST ? MAX_ADLER_DIGEST : bufLen;
448: bufLen -= clen;
449:
450: do {
451: s1 += (u_int) *psBuf++;
452: s2 += s1;
453: } while (--clen);
454:
455: s1 %= crc_modAdler;
456: s2 %= crc_modAdler;
457: }
458:
459: return (s2 << 16) | s1;
460: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>