Annotation of libaitcrc/src/aitcrc.c, revision 1.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 $
! 6: * $Id: global.h,v 1.1.1.1.2.2 2008/09/30 20:00:26 misho Exp $
! 7: *
! 8: *************************************************************************/
! 9: #include "global.h"
! 10:
! 11:
! 12: static int crc_Errno;
! 13: static char crc_Error[MAX_STR + 1];
! 14:
! 15: const u_long crc_modAdler = 0xFFF1L;
! 16:
! 17: const crcPoly_t crc_Poly[] = {
! 18: { 1, (u_long) 0x1, "CRC-1-Parity" },
! 19: { 4, (u_long) 0x3, "CRC-4-ITU" },
! 20: { 5, (u_long) 0x15, "CRC-5-ITU" },
! 21: { 6, (u_long) 0x3, "CRC-6-ITU" },
! 22: { 7, (u_long) 0x9, "CRC-7-MMC" },
! 23: { 8, (u_long) 0x8D, "CRC-8-CCITT" },
! 24: { 10, (u_long) 0x233, "CRC-10" },
! 25: { 11, (u_long) 0x385, "CRC-11-FlexRay" },
! 26: { 12, (u_long) 0x80F, "CRC-12-Telco" },
! 27: { 15, (u_long) 0x4599, "CRC-15-CAN" },
! 28: { 16, (u_long) 0x8005, "CRC-16-IBM" },
! 29: { 24, (u_long) 0x864CFB, "CRC-24-Radix64" },
! 30: { 30, (u_long) 0x2030B9C7, "CRC-30-CDMA" },
! 31: { 32, (u_long) 0x04C11DB7, "CRC-32-802.3" }
! 32: };
! 33:
! 34: // ----------------------------------------------------------
! 35:
! 36: inline int crc_GetErrno()
! 37: {
! 38: return crc_Errno;
! 39: }
! 40:
! 41: inline const char *crc_GetError()
! 42: {
! 43: return crc_Error;
! 44: }
! 45:
! 46: // ----------------------------------------------------------
! 47:
! 48: inline u_long crcReflect(u_long crcNum, u_char crcBits)
! 49: {
! 50: register u_long i, j, rev;
! 51:
! 52: for (i = (u_long) 1 << (crcBits - 1), j = 1, rev ^= rev; i; i >>= 1, j <<= 1)
! 53: if (crcNum & i)
! 54: rev |= j;
! 55:
! 56: crc_Errno ^= crc_Errno;
! 57: return rev;
! 58: }
! 59:
! 60: inline u_long crcCalc(u_char * __restrict psBuf, u_int bufLen, u_char crcBits, u_char RevOpts, u_long initCRC, u_long xorCRC)
! 61: {
! 62: const u_long bits = sizeof(long) * 8 - crcBits;
! 63: u_long poly, crchibit, crc;
! 64: register u_long i, j, b, ch;
! 65:
! 66: if (!psBuf) {
! 67: crc_Errno = 1;
! 68: strlcpy(crc_Error, "crcCalc(): Invalid parameters!", MAX_STR + 1);
! 69: return -1;
! 70: }
! 71:
! 72: switch (crcBits) {
! 73: case 1:
! 74: poly = crc_Poly[0].poly_num;
! 75: break;
! 76: case 4:
! 77: poly = crc_Poly[1].poly_num;
! 78: break;
! 79: case 5:
! 80: poly = crc_Poly[2].poly_num;
! 81: break;
! 82: case 6:
! 83: poly = crc_Poly[3].poly_num;
! 84: break;
! 85: case 7:
! 86: poly = crc_Poly[4].poly_num;
! 87: break;
! 88: case 8:
! 89: poly = crc_Poly[5].poly_num;
! 90: break;
! 91: case 10:
! 92: poly = crc_Poly[6].poly_num;
! 93: break;
! 94: case 11:
! 95: poly = crc_Poly[7].poly_num;
! 96: break;
! 97: case 12:
! 98: poly = crc_Poly[8].poly_num;
! 99: break;
! 100: case 15:
! 101: poly = crc_Poly[9].poly_num;
! 102: break;
! 103: case 16:
! 104: poly = crc_Poly[10].poly_num;
! 105: break;
! 106: case 24:
! 107: poly = crc_Poly[11].poly_num;
! 108: break;
! 109: case 30:
! 110: poly = crc_Poly[12].poly_num;
! 111: break;
! 112: case 32:
! 113: poly = crc_Poly[13].poly_num;
! 114: break;
! 115: default:
! 116: crc_Errno = 2;
! 117: strlcpy(crc_Error, "crcCalc(): Unsupported CRC method!!!", MAX_STR + 1);
! 118: return -1;
! 119: }
! 120: poly <<= bits;
! 121:
! 122: crchibit = (u_long) 1 << (crcBits - 1);
! 123: crchibit <<= bits;
! 124: crc = initCRC << bits;
! 125:
! 126: for (i = 0; i < bufLen; i++) {
! 127: ch = (u_long) *psBuf++;
! 128: if (RevOpts & REVOPTS_REVERTBYTE)
! 129: ch = crcReflect(ch, 8);
! 130:
! 131: for (j = 0x80; j; j >>= 1) {
! 132: b = crc & crchibit;
! 133: crc <<= 1;
! 134:
! 135: if (ch & j)
! 136: b ^= crchibit;
! 137: if (b)
! 138: crc ^= poly;
! 139: }
! 140: }
! 141:
! 142: if (RevOpts & REVOPTS_REVERTCRC)
! 143: crc = crcReflect(crc, sizeof(long) * 8);
! 144: crc ^= xorCRC << bits;
! 145: crc &= (((crchibit - 1) << 1) | 1);
! 146: if (!(RevOpts & REVOPTS_REVERTCRC))
! 147: crc >>= bits;
! 148:
! 149: crc_Errno ^= crc_Errno;
! 150: return crc;
! 151: }
! 152:
! 153: // ----------------------------------------------------------
! 154:
! 155: inline u_short crcIP(u_short * __restrict nBuf, int bufLen)
! 156: {
! 157: register u_long sum;
! 158:
! 159: if (!nBuf) {
! 160: crc_Errno = 1;
! 161: strlcpy(crc_Error, "crcIP(): Invalid parameters!", MAX_STR + 1);
! 162: return -1;
! 163: }
! 164:
! 165: for (sum = 0; bufLen; bufLen--)
! 166: sum += *nBuf++;
! 167:
! 168: sum = (sum >> 16) + (sum & 0xFFFF);
! 169: sum += sum >> 16;
! 170:
! 171: crc_Errno ^= crc_Errno;
! 172: return (u_short) ~sum;
! 173: }
! 174:
! 175: inline u_long crcFletcher(u_short * __restrict nBuf, int bufLen)
! 176: {
! 177: register u_long s1, s2, clen;
! 178:
! 179: if (!nBuf) {
! 180: crc_Errno = 1;
! 181: strlcpy(crc_Error, "crcFletcher(): Invalid parameters!", MAX_STR + 1);
! 182: return -1;
! 183: }
! 184:
! 185: s1 = s2 = 0xFFFF;
! 186: while (bufLen) {
! 187: clen = bufLen > MAX_FLETCHER_DIGEST ? MAX_FLETCHER_DIGEST : bufLen;
! 188: bufLen -= clen;
! 189:
! 190: do {
! 191: s1 += (u_long) *nBuf++;
! 192: s2 += s1;
! 193: } while (--clen);
! 194:
! 195: s1 = (s1 >> 16) + (s1 & 0xFFFF);
! 196: s2 = (s2 >> 16) + (s2 & 0xFFFF);
! 197: }
! 198:
! 199: crc_Errno ^= crc_Errno;
! 200: return (s2 << 16) | s1;
! 201: }
! 202:
! 203: inline u_long crcAdler(u_char * __restrict psBuf, int bufLen)
! 204: {
! 205: register u_long s1, s2, clen;
! 206:
! 207: if (!psBuf) {
! 208: crc_Errno = 1;
! 209: strlcpy(crc_Error, "crcAdler(): Invalid parameters!", MAX_STR + 1);
! 210: return -1;
! 211: }
! 212:
! 213: s1 = 1L;
! 214: s2 ^= s2;
! 215: while (bufLen) {
! 216: clen = bufLen > MAX_ADLER_DIGEST ? MAX_ADLER_DIGEST : bufLen;
! 217: bufLen -= clen;
! 218:
! 219: do {
! 220: s1 += (u_long) *psBuf++;
! 221: s2 += s1;
! 222: } while (--clen);
! 223:
! 224: s1 %= crc_modAdler;
! 225: s2 %= crc_modAdler;
! 226: }
! 227:
! 228: crc_Errno ^= crc_Errno;
! 229: return (s2 << 16) | s1;
! 230: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>