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