version 1.2.2.1, 2010/06/13 16:10:16
|
version 1.6, 2012/07/22 21:46:23
|
Line 5
|
Line 5
|
* $Author$ |
* $Author$ |
* $Id$ |
* $Id$ |
* |
* |
*************************************************************************/ | ************************************************************************** |
| The ELWIX and AITNET software is distributed under the following |
| terms: |
| |
| All of the documentation and software included in the ELWIX and AITNET |
| Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org> |
| |
| Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 |
| by Michael Pounov <misho@elwix.org>. All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions |
| are met: |
| 1. Redistributions of source code must retain the above copyright |
| notice, this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| 3. All advertising materials mentioning features or use of this software |
| must display the following acknowledgement: |
| This product includes software developed by Michael Pounov <misho@elwix.org> |
| ELWIX - Embedded LightWeight unIX and its contributors. |
| 4. Neither the name of AITNET nor the names of its contributors |
| may be used to endorse or promote products derived from this software |
| without specific prior written permission. |
| |
| THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND |
| ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| SUCH DAMAGE. |
| */ |
#include "global.h" |
#include "global.h" |
|
|
|
|
static int crc_Errno; |
static int crc_Errno; |
static char crc_Error[MAX_STR + 1]; | static char crc_Error[STRSIZ]; |
|
|
// Adler module | /* Adler module */ |
const u_int crc_modAdler = 0xFFF1L; |
const u_int crc_modAdler = 0xFFF1L; |
|
|
// All known library CRC types ... | /* All known library CRC types ... */ |
const crcPoly_t crc_Poly[] = { |
const crcPoly_t crc_Poly[] = { |
{ 1, (u_int) 0x1, "CRC-1-Parity" }, |
{ 1, (u_int) 0x1, "CRC-1-Parity" }, |
{ 4, (u_int) 0x3, "CRC-4-ITU" }, |
{ 4, (u_int) 0x3, "CRC-4-ITU" }, |
Line 33 const crcPoly_t crc_Poly[] = {
|
Line 70 const crcPoly_t crc_Poly[] = {
|
{ 32, (u_int) 0x04C11DB7, "CRC-32-802.3" } |
{ 32, (u_int) 0x04C11DB7, "CRC-32-802.3" } |
}; |
}; |
|
|
// ---------------------------------------------------------- |
|
|
|
// | /* |
// Error maintenance functions ... | * Error maintenance functions ... |
// | */ |
|
|
// crc_GetErrno() Get error code of last operation | /* crc_GetErrno() Get error code of last operation */ |
inline int crc_GetErrno() | inline int |
| crc_GetErrno() |
{ |
{ |
return crc_Errno; |
return crc_Errno; |
} |
} |
|
|
// crc_GetError() Get error text of last operation | /* crc_GetError() Get error text of last operation */ |
inline const char *crc_GetError() | inline const char * |
| crc_GetError() |
{ |
{ |
return crc_Error; |
return crc_Error; |
} |
} |
|
|
// crcSetErr() Set error to variables for internal use!!! | /* crc_SetErr() Set error to variables for internal use!!! */ |
inline void crcSetErr(int eno, char *estr, ...) | inline void |
| crc_SetErr(int eno, char *estr, ...) |
{ |
{ |
va_list lst; |
va_list lst; |
|
|
crc_Errno = eno; |
crc_Errno = eno; |
memset(crc_Error, 0, MAX_STR + 1); | memset(crc_Error, 0, sizeof crc_Error); |
va_start(lst, estr); |
va_start(lst, estr); |
vsnprintf(crc_Error, MAX_STR + 1, estr, lst); | vsnprintf(crc_Error, sizeof crc_Error, estr, lst); |
va_end(lst); |
va_end(lst); |
} |
} |
|
|
// ---------------------------------------------------------- |
|
|
|
/* |
/* |
* crcReflect() Reflect all bits of number | * crcReflect() - Reflect all bits of number |
| * |
* @crcNum = Number for reflection |
* @crcNum = Number for reflection |
* @crcBits = Number width bits |
* @crcBits = Number width bits |
* return: -1 error, !=-1 reflecting number |
* return: -1 error, !=-1 reflecting number |
Line 85 crcReflect(u_int crcNum, u_char crcBits)
|
Line 124 crcReflect(u_int crcNum, u_char crcBits)
|
} |
} |
|
|
/* |
/* |
* crcCalc() Generic CRC calculation function for many sub variants of CRC algorithms | * crcCalc() - Generic CRC calculation function for many sub variants of CRC algorithms |
| * |
* @psBuf = Data for calculation |
* @psBuf = Data for calculation |
* @bufLen = Length of data |
* @bufLen = Length of data |
* @crcBits = CRC algorithm bits (1, 4, 5, 6, 7, 8, 10, 11, 12, 15, 16, 24, 30, 32) |
* @crcBits = CRC algorithm bits (1, 4, 5, 6, 7, 8, 10, 11, 12, 15, 16, 24, 30, 32) |
Line 101 crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha
|
Line 141 crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha
|
u_int poly, crchibit, crc; |
u_int poly, crchibit, crc; |
register u_int i, j, b, ch; |
register u_int i, j, b, ch; |
|
|
if (!psBuf) { | assert(psBuf); |
crc_Errno = 1; | |
strlcpy(crc_Error, "crcCalc(): Invalid parameters!", MAX_STR + 1); | |
return -1; | |
} | |
|
|
switch (crcBits) { |
switch (crcBits) { |
case 1: |
case 1: |
Line 151 crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha
|
Line 187 crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha
|
poly = crc_Poly[13].poly_num; |
poly = crc_Poly[13].poly_num; |
break; |
break; |
default: |
default: |
crc_Errno = 2; | crc_SetErr(EINVAL, "crcCalc(): Unsupported CRC method!!!"); |
strlcpy(crc_Error, "crcCalc(): Unsupported CRC method!!!", MAX_STR + 1); | |
return -1; |
return -1; |
} |
} |
poly <<= bits; |
poly <<= bits; |
Line 188 crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha
|
Line 223 crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha
|
return crc; |
return crc; |
} |
} |
|
|
// ---------------------------------------------------------- |
|
|
|
/* |
/* |
* crcIP() Checksum in IP communication | * crcIP() - Checksum in IP communication |
* @nBuf = Data for calculation | * |
| * @buf = Data for calculation |
* @bufLen = Length of data |
* @bufLen = Length of data |
* return: -1 error, !=-1 Checksum |
* return: -1 error, !=-1 Checksum |
*/ |
*/ |
inline u_short |
inline u_short |
crcIP(u_short * __restrict nBuf, int bufLen) | crcIP(u_char * __restrict buf, int bufLen) |
{ |
{ |
register u_int sum; |
register u_int sum; |
|
u_short last = 0, *nBuf = (u_short*) buf; |
|
|
if (!nBuf) { | assert(buf); |
crc_Errno = 1; | |
strlcpy(crc_Error, "crcIP(): Invalid parameters!", MAX_STR + 1); | |
return -1; | |
} | |
|
|
for (sum = 0; bufLen; bufLen--) | for (sum = 0; bufLen && bufLen > 1; bufLen -= 2) |
sum += *nBuf++; |
sum += *nBuf++; |
|
if (bufLen == 1) { |
|
*(u_char*)(&last) += *(u_char*) nBuf; |
|
sum += last; |
|
} |
|
|
sum = (sum >> 16) + (sum & 0xFFFF); |
sum = (sum >> 16) + (sum & 0xFFFF); |
sum += sum >> 16; |
sum += sum >> 16; |
Line 218 crcIP(u_short * __restrict nBuf, int bufLen)
|
Line 254 crcIP(u_short * __restrict nBuf, int bufLen)
|
} |
} |
|
|
/* |
/* |
* crcFletcher16() Fletcher-16 Checksum computing | * crcFletcher16() - Fletcher-16 Checksum computing |
| * |
* @nBuf = Data for calculation |
* @nBuf = Data for calculation |
* @bufLen = Length of data |
* @bufLen = Length of data |
* return: -1 error, !=-1 Checksum |
* return: -1 error, !=-1 Checksum |
Line 229 crcFletcher16(u_short * __restrict nBuf, int bufLen)
|
Line 266 crcFletcher16(u_short * __restrict nBuf, int bufLen)
|
register u_short s1, s2; |
register u_short s1, s2; |
register u_int clen; |
register u_int clen; |
|
|
if (!nBuf) { | assert(nBuf); |
crc_Errno = 1; | |
strlcpy(crc_Error, "crcFletcher16(): Invalid parameters!", MAX_STR + 1); | |
return -1; | |
} | |
|
|
s1 = s2 = 0xFF; |
s1 = s2 = 0xFF; |
while (bufLen) { |
while (bufLen) { |
Line 254 crcFletcher16(u_short * __restrict nBuf, int bufLen)
|
Line 287 crcFletcher16(u_short * __restrict nBuf, int bufLen)
|
} |
} |
|
|
/* |
/* |
* crcFletcher() Fletcher-32 Checksum computing | * crcFletcher() - Fletcher-32 Checksum computing |
| * |
* @nBuf = Data for calculation |
* @nBuf = Data for calculation |
* @bufLen = Length of data |
* @bufLen = Length of data |
* return: -1 error, !=-1 Checksum |
* return: -1 error, !=-1 Checksum |
Line 264 crcFletcher(u_short * __restrict nBuf, int bufLen)
|
Line 298 crcFletcher(u_short * __restrict nBuf, int bufLen)
|
{ |
{ |
register u_int s1, s2, clen; |
register u_int s1, s2, clen; |
|
|
if (!nBuf) { | assert(nBuf); |
crc_Errno = 1; | |
strlcpy(crc_Error, "crcFletcher(): Invalid parameters!", MAX_STR + 1); | |
return -1; | |
} | |
|
|
s1 = s2 = 0xFFFF; |
s1 = s2 = 0xFFFF; |
while (bufLen) { |
while (bufLen) { |
Line 289 crcFletcher(u_short * __restrict nBuf, int bufLen)
|
Line 319 crcFletcher(u_short * __restrict nBuf, int bufLen)
|
} |
} |
|
|
/* |
/* |
* crcAdler() crcAdler-32 Checksum computing | * crcAdler() - crcAdler-32 Checksum computing |
| * |
* @psBuf = Data for calculation |
* @psBuf = Data for calculation |
* @bufLen = Length of data |
* @bufLen = Length of data |
* return: -1 error, !=-1 Checksum |
* return: -1 error, !=-1 Checksum |
Line 299 crcAdler(u_char * __restrict psBuf, int bufLen)
|
Line 330 crcAdler(u_char * __restrict psBuf, int bufLen)
|
{ |
{ |
register u_int s1, s2, clen; |
register u_int s1, s2, clen; |
|
|
if (!psBuf) { | assert(psBuf); |
crc_Errno = 1; | |
strlcpy(crc_Error, "crcAdler(): Invalid parameters!", MAX_STR + 1); | |
return -1; | |
} | |
|
|
s1 = 1L; |
s1 = 1L; |
s2 ^= s2; |
s2 ^= s2; |