Annotation of libaitcrc/src/aitcrc.c, revision 1.4.2.3

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

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