Diff for /libelwix/src/crc.c between versions 1.1 and 1.5.28.1

version 1.1, 2013/01/17 10:05:35 version 1.5.28.1, 2019/12/18 20:00:18
Line 12  terms: Line 12  terms:
 All of the documentation and software included in the ELWIX and AITNET  All of the documentation and software included in the ELWIX and AITNET
 Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>  Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   
Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013Copyright 2004 - 2019
         by Michael Pounov <misho@elwix.org>.  All rights reserved.          by Michael Pounov <misho@elwix.org>.  All rights reserved.
   
 Redistribution and use in source and binary forms, with or without  Redistribution and use in source and binary forms, with or without
Line 46  SUCH DAMAGE. Line 46  SUCH DAMAGE.
 #include "global.h"  #include "global.h"
   
   
   /* CRC16 poly */
   const u_short crc_16poly = 0x1021;
   
 /* Adler module */  /* Adler module */
 const u_int crc_modAdler = 0xFFF1L;  const u_int crc_modAdler = 0xFFF1L;
   
Line 73  const crcPoly_t crc_Poly[] = { Line 76  const crcPoly_t crc_Poly[] = {
  *   *
  * @crcNum = Number for reflection   * @crcNum = Number for reflection
  * @crcBits = Number width bits    * @crcBits = Number width bits 
 * return: -1 error, !=-1 reflecting number * return: reflecting number
  */   */
inline u_intu_int
 crcReflect(u_int crcNum, u_char crcBits)  crcReflect(u_int crcNum, u_char crcBits)
 {  {
        register u_int i, j, rev;        register u_int i, j = 1, rev = 0;
   
        for (i = (u_int) 1 << (crcBits - 1), j = 1, rev ^= rev; i; i >>= 1, j <<= 1)        for (i = (u_int) 1 << (crcBits - 1); i; i >>= 1, j <<= 1)
                 if (crcNum & i)                  if (crcNum & i)
                         rev |= j;                          rev |= j;
         return rev;          return rev;
Line 95  crcReflect(u_int crcNum, u_char crcBits) Line 98  crcReflect(u_int crcNum, u_char crcBits)
  * @RevOpts = Options for computation (REVOPTS_REVERTBYTE, REVOPTS_REVERTCRC)   * @RevOpts = Options for computation (REVOPTS_REVERTBYTE, REVOPTS_REVERTCRC)
  * @initCRC = Initial CRC value   * @initCRC = Initial CRC value
  * @xorCRC = Last xor CRC value   * @xorCRC = Last xor CRC value
 * return: -1 error, !=-1 CRC checksum * return: CRC checksum
  */   */
inline u_intu_int
 crcCalc(u_char * __restrict psBuf, u_int bufLen, u_char crcBits, u_char RevOpts, u_int initCRC, u_int xorCRC)  crcCalc(u_char * __restrict psBuf, u_int bufLen, u_char crcBits, u_char RevOpts, u_int initCRC, u_int xorCRC)
 {  {
         const u_int bits = sizeof(int) * 8 - crcBits;          const u_int bits = sizeof(int) * 8 - crcBits;
Line 187  crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha Line 190  crcCalc(u_char * __restrict psBuf, u_int bufLen, u_cha
   
   
 /*  /*
    * crc16() - Checksum in X/Y modem communication
    *
    * @buf = Data for calculation
    * @bufLen = Length of data
    * return: Checksum
    */
   u_short
   crc16(u_char * __restrict buf, int bufLen)
   {
           u_short crc, x;
           register u_short i;
   
           assert(buf);
   
           for (crc = 0; bufLen > 0; bufLen--, buf++)
                   for (i = 0x80; i; i >>= 1) {
                           x = crc >> 15;
                           crc <<= 1;
   
                           if (*buf & i)
                                   crc++;
                           if (x)
                                   crc ^= crc_16poly;
                   }
   
           for (i = 0; i < 16; i++) {
                   x = crc >> 15;
                   crc <<= 1;
   
                   if (x)
                           crc ^= crc_16poly;
           }
   
           return crc;
   }
   
   /*
  * crcIP() - Checksum in IP communication   * crcIP() - Checksum in IP communication
  *   *
  * @buf = Data for calculation   * @buf = Data for calculation
  * @bufLen = Length of data   * @bufLen = Length of data
 * return: -1 error, !=-1 Checksum * return: Checksum
  */   */
inline u_shortu_short
 crcIP(u_char * __restrict buf, int bufLen)  crcIP(u_char * __restrict buf, int bufLen)
 {  {
         register u_int sum;          register u_int sum;
Line 201  crcIP(u_char * __restrict buf, int bufLen) Line 241  crcIP(u_char * __restrict buf, int bufLen)
   
         assert(buf);          assert(buf);
   
        for (sum = 0; bufLen && bufLen > 1; bufLen -= 2)        for (sum = 0; bufLen > 1; bufLen -= 2)
                 sum += *nBuf++;                  sum += *nBuf++;
         if (bufLen == 1) {          if (bufLen == 1) {
                 *(u_char*)(&last) += *(u_char*) nBuf;                  *(u_char*)(&last) += *(u_char*) nBuf;
Line 215  crcIP(u_char * __restrict buf, int bufLen) Line 255  crcIP(u_char * __restrict buf, int bufLen)
 }  }
   
 /*  /*
    * crcTCP() - Checksum for TCP v4 communication
    *
    * @buf = Data for calculation
    * @bufLen = Length of data
    * @th = TCP header
    * return: Checksum
    */
   u_short
   crcTCP(struct in_addr src, struct in_addr dst, u_char * __restrict th)
   {
           struct psd_tcp {
                   struct in_addr src;
                   struct in_addr dst;
                   u_char pad;
                   u_char proto;
                   u_short tcp_len;
                   u_char tcp[20];
           } buf;
   
           buf.src = src;
           buf.dst = dst;
           buf.pad = 0;
           buf.proto = IPPROTO_TCP;
           buf.tcp_len = htons(sizeof buf.tcp);
           memcpy(&buf.tcp, th, sizeof buf.tcp);
   
           return crcIP((u_char*) &buf, sizeof buf);
   }
   
   /*
    * crcUDP() - Checksum for UDP v4 communication
    *
    * @buf = Data for calculation
    * @bufLen = Length of data
    * @uh = UDP header
    * return: Checksum
    */
   u_short
   crcUDP(struct in_addr src, struct in_addr dst, u_char * __restrict uh)
   {
           struct psd_udp {
                   struct in_addr src;
                   struct in_addr dst;
                   u_char pad;
                   u_char proto;
                   u_short udp_len;
                   u_char udp[8];
           } buf;
   
           buf.src = src;
           buf.dst = dst;
           buf.pad = 0;
           buf.proto = IPPROTO_UDP;
           buf.udp_len = htons(sizeof buf.udp);
           memcpy(&buf.udp, uh, sizeof buf.udp);
   
           return crcIP((u_char*) &buf, sizeof buf);
   }
   
   
   /*
  * 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: Checksum
  */   */
inline u_shortu_short
 crcFletcher16(u_short * __restrict nBuf, int bufLen)  crcFletcher16(u_short * __restrict nBuf, int bufLen)
 {  {
         register u_short s1, s2;          register u_short s1, s2;
Line 251  crcFletcher16(u_short * __restrict nBuf, int bufLen) Line 352  crcFletcher16(u_short * __restrict nBuf, int bufLen)
  *   *
  * @nBuf = Data for calculation   * @nBuf = Data for calculation
  * @bufLen = Length of data   * @bufLen = Length of data
 * return: -1 error, !=-1 Checksum * return: Checksum
  */   */
inline u_intu_int
 crcFletcher(u_short * __restrict nBuf, int bufLen)  crcFletcher(u_short * __restrict nBuf, int bufLen)
 {  {
         register u_int s1, s2, clen;          register u_int s1, s2, clen;
Line 282  crcFletcher(u_short * __restrict nBuf, int bufLen) Line 383  crcFletcher(u_short * __restrict nBuf, int bufLen)
  *   *
  * @psBuf = Data for calculation   * @psBuf = Data for calculation
  * @bufLen = Length of data   * @bufLen = Length of data
 * return: -1 error, !=-1 Checksum * return: Checksum
  */   */
inline u_intu_int
 crcAdler(u_char * __restrict psBuf, int bufLen)  crcAdler(u_char * __restrict psBuf, int bufLen)
 {  {
        register u_int s1, s2, clen;        register u_int s1 = 1, s2 = 0, clen;
   
         assert(psBuf);          assert(psBuf);
   
         s1 = 1L;  
         s2 ^= s2;  
         while (bufLen) {          while (bufLen) {
                 clen = bufLen > MAX_ADLER_DIGEST ? MAX_ADLER_DIGEST : bufLen;                  clen = bufLen > MAX_ADLER_DIGEST ? MAX_ADLER_DIGEST : bufLen;
                 bufLen -= clen;                  bufLen -= clen;

Removed from v.1.1  
changed lines
  Added in v.1.5.28.1


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