Annotation of libaitcrc/src/aitcrc.c, revision 1.5.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.5.2.1 ! misho 6: * $Id: aitcrc.c,v 1.5 2012/07/04 14:53:43 misho Exp $
1.1 misho 7: *
1.4 misho 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.5 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.4 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: */
1.1 misho 46: #include "global.h"
47:
48:
49: static int crc_Errno;
1.5.2.1 ! misho 50: static char crc_Error[STRSIZ];
1.1 misho 51:
1.5 misho 52: /* Adler module */
1.3 misho 53: const u_int crc_modAdler = 0xFFF1L;
1.1 misho 54:
1.5 misho 55: /* All known library CRC types ... */
1.1 misho 56: const crcPoly_t crc_Poly[] = {
1.3 misho 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" },
62: { 8, (u_int) 0x8D, "CRC-8-CCITT" },
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" },
70: { 32, (u_int) 0x04C11DB7, "CRC-32-802.3" }
1.1 misho 71: };
72:
73:
1.5 misho 74: /*
75: * Error maintenance functions ...
76: */
1.2 misho 77:
1.5 misho 78: /* crc_GetErrno() Get error code of last operation */
79: inline int
80: crc_GetErrno()
1.1 misho 81: {
82: return crc_Errno;
83: }
84:
1.5 misho 85: /* crc_GetError() Get error text of last operation */
86: inline const char *
87: crc_GetError()
1.1 misho 88: {
89: return crc_Error;
90: }
91:
1.5 misho 92: /* crc_SetErr() Set error to variables for internal use!!! */
93: inline void
94: crc_SetErr(int eno, char *estr, ...)
1.2 misho 95: {
96: va_list lst;
97:
98: crc_Errno = eno;
1.5 misho 99: memset(crc_Error, 0, sizeof crc_Error);
1.2 misho 100: va_start(lst, estr);
1.5 misho 101: vsnprintf(crc_Error, sizeof crc_Error, estr, lst);
1.2 misho 102: va_end(lst);
103: }
104:
1.1 misho 105:
1.2 misho 106: /*
1.5 misho 107: * crcReflect() - Reflect all bits of number
108: *
1.2 misho 109: * @crcNum = Number for reflection
110: * @crcBits = Number width bits
111: * return: -1 error, !=-1 reflecting number
112: */
1.3 misho 113: inline u_int
114: crcReflect(u_int crcNum, u_char crcBits)
1.1 misho 115: {
1.3 misho 116: register u_int i, j, rev;
1.1 misho 117:
1.3 misho 118: for (i = (u_int) 1 << (crcBits - 1), j = 1, rev ^= rev; i; i >>= 1, j <<= 1)
1.1 misho 119: if (crcNum & i)
120: rev |= j;
121:
122: crc_Errno ^= crc_Errno;
123: return rev;
124: }
125:
1.2 misho 126: /*
1.5 misho 127: * crcCalc() - Generic CRC calculation function for many sub variants of CRC algorithms
128: *
1.2 misho 129: * @psBuf = Data for calculation
130: * @bufLen = Length of data
131: * @crcBits = CRC algorithm bits (1, 4, 5, 6, 7, 8, 10, 11, 12, 15, 16, 24, 30, 32)
132: * @RevOpts = Options for computation (REVOPTS_REVERTBYTE, REVOPTS_REVERTCRC)
133: * @initCRC = Initial CRC value
134: * @xorCRC = Last xor CRC value
135: * return: -1 error, !=-1 CRC checksum
136: */
1.3 misho 137: inline u_int
138: crcCalc(u_char * __restrict psBuf, u_int bufLen, u_char crcBits, u_char RevOpts, u_int initCRC, u_int xorCRC)
1.1 misho 139: {
1.3 misho 140: const u_int bits = sizeof(int) * 8 - crcBits;
141: u_int poly, crchibit, crc;
142: register u_int i, j, b, ch;
1.1 misho 143:
1.5 misho 144: assert(psBuf);
1.1 misho 145:
146: switch (crcBits) {
147: case 1:
148: poly = crc_Poly[0].poly_num;
149: break;
150: case 4:
151: poly = crc_Poly[1].poly_num;
152: break;
153: case 5:
154: poly = crc_Poly[2].poly_num;
155: break;
156: case 6:
157: poly = crc_Poly[3].poly_num;
158: break;
159: case 7:
160: poly = crc_Poly[4].poly_num;
161: break;
162: case 8:
163: poly = crc_Poly[5].poly_num;
164: break;
165: case 10:
166: poly = crc_Poly[6].poly_num;
167: break;
168: case 11:
169: poly = crc_Poly[7].poly_num;
170: break;
171: case 12:
172: poly = crc_Poly[8].poly_num;
173: break;
174: case 15:
175: poly = crc_Poly[9].poly_num;
176: break;
177: case 16:
178: poly = crc_Poly[10].poly_num;
179: break;
180: case 24:
181: poly = crc_Poly[11].poly_num;
182: break;
183: case 30:
184: poly = crc_Poly[12].poly_num;
185: break;
186: case 32:
187: poly = crc_Poly[13].poly_num;
188: break;
189: default:
1.5 misho 190: crc_SetErr(EINVAL, "crcCalc(): Unsupported CRC method!!!");
1.1 misho 191: return -1;
192: }
193: poly <<= bits;
194:
1.3 misho 195: crchibit = (u_int) 1 << (crcBits - 1);
1.1 misho 196: crchibit <<= bits;
197: crc = initCRC << bits;
198:
199: for (i = 0; i < bufLen; i++) {
1.3 misho 200: ch = (u_int) *psBuf++;
1.1 misho 201: if (RevOpts & REVOPTS_REVERTBYTE)
202: ch = crcReflect(ch, 8);
203:
204: for (j = 0x80; j; j >>= 1) {
205: b = crc & crchibit;
206: crc <<= 1;
207:
208: if (ch & j)
209: b ^= crchibit;
210: if (b)
211: crc ^= poly;
212: }
213: }
214:
215: if (RevOpts & REVOPTS_REVERTCRC)
1.3 misho 216: crc = crcReflect(crc, sizeof(int) * 8);
1.1 misho 217: crc ^= xorCRC << bits;
218: crc &= (((crchibit - 1) << 1) | 1);
219: if (!(RevOpts & REVOPTS_REVERTCRC))
220: crc >>= bits;
221:
222: crc_Errno ^= crc_Errno;
223: return crc;
224: }
225:
226:
1.2 misho 227: /*
1.5 misho 228: * crcIP() - Checksum in IP communication
229: *
230: * @buf = Data for calculation
1.2 misho 231: * @bufLen = Length of data
232: * return: -1 error, !=-1 Checksum
233: */
1.3 misho 234: inline u_short
1.5 misho 235: crcIP(u_char * __restrict buf, int bufLen)
1.1 misho 236: {
1.3 misho 237: register u_int sum;
1.5 misho 238: u_short last = 0, *nBuf = (u_short*) buf;
1.1 misho 239:
1.5 misho 240: assert(buf);
1.1 misho 241:
1.5 misho 242: for (sum = 0; bufLen && bufLen > 1; bufLen -= 2)
1.1 misho 243: sum += *nBuf++;
1.5 misho 244: if (bufLen == 1) {
245: *(u_char*)(&last) += *(u_char*) nBuf;
246: sum += last;
247: }
1.1 misho 248:
249: sum = (sum >> 16) + (sum & 0xFFFF);
250: sum += sum >> 16;
251:
252: crc_Errno ^= crc_Errno;
253: return (u_short) ~sum;
254: }
255:
1.2 misho 256: /*
1.5 misho 257: * crcFletcher16() - Fletcher-16 Checksum computing
258: *
1.2 misho 259: * @nBuf = Data for calculation
260: * @bufLen = Length of data
261: * return: -1 error, !=-1 Checksum
262: */
1.3 misho 263: inline u_short
264: crcFletcher16(u_short * __restrict nBuf, int bufLen)
1.1 misho 265: {
1.3 misho 266: register u_short s1, s2;
267: register u_int clen;
268:
1.5 misho 269: assert(nBuf);
1.3 misho 270:
271: s1 = s2 = 0xFF;
272: while (bufLen) {
273: clen = bufLen > MAX_FLETCHER16_DIGEST ? MAX_FLETCHER16_DIGEST : bufLen;
274: bufLen -= clen;
275:
276: do {
277: s1 += (u_short) *nBuf++;
278: s2 += s1;
279: } while (--clen);
280:
281: s1 = (s1 >> 8) + (s1 & 0xFF);
282: s2 = (s2 >> 8) + (s2 & 0xFF);
283: }
284:
285: crc_Errno ^= crc_Errno;
286: return (s2 << 8) | s1;
287: }
288:
289: /*
1.5 misho 290: * crcFletcher() - Fletcher-32 Checksum computing
291: *
1.3 misho 292: * @nBuf = Data for calculation
293: * @bufLen = Length of data
294: * return: -1 error, !=-1 Checksum
295: */
296: inline u_int
297: crcFletcher(u_short * __restrict nBuf, int bufLen)
298: {
299: register u_int s1, s2, clen;
1.1 misho 300:
1.5 misho 301: assert(nBuf);
1.1 misho 302:
303: s1 = s2 = 0xFFFF;
304: while (bufLen) {
305: clen = bufLen > MAX_FLETCHER_DIGEST ? MAX_FLETCHER_DIGEST : bufLen;
306: bufLen -= clen;
307:
308: do {
1.3 misho 309: s1 += (u_int) *nBuf++;
1.1 misho 310: s2 += s1;
311: } while (--clen);
312:
313: s1 = (s1 >> 16) + (s1 & 0xFFFF);
314: s2 = (s2 >> 16) + (s2 & 0xFFFF);
315: }
316:
317: crc_Errno ^= crc_Errno;
318: return (s2 << 16) | s1;
319: }
320:
1.2 misho 321: /*
1.5 misho 322: * crcAdler() - crcAdler-32 Checksum computing
323: *
1.2 misho 324: * @psBuf = Data for calculation
325: * @bufLen = Length of data
326: * return: -1 error, !=-1 Checksum
327: */
1.3 misho 328: inline u_int
329: crcAdler(u_char * __restrict psBuf, int bufLen)
1.1 misho 330: {
1.3 misho 331: register u_int s1, s2, clen;
1.1 misho 332:
1.5 misho 333: assert(psBuf);
1.1 misho 334:
335: s1 = 1L;
336: s2 ^= s2;
337: while (bufLen) {
338: clen = bufLen > MAX_ADLER_DIGEST ? MAX_ADLER_DIGEST : bufLen;
339: bufLen -= clen;
340:
341: do {
1.3 misho 342: s1 += (u_int) *psBuf++;
1.1 misho 343: s2 += s1;
344: } while (--clen);
345:
346: s1 %= crc_modAdler;
347: s2 %= crc_modAdler;
348: }
349:
350: crc_Errno ^= crc_Errno;
351: return (s2 << 16) | s1;
352: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>