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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>