/******************************************************************************
 * FIDOCONFIG --- library for fidonet configs
 ******************************************************************************
 * crc.c : crc calculation routines
 *
 * Collected & written by Stas Degteff <g@grumbler.org> 2:5080/102
 * (c) Stas Degteff
 * (c) HUSKY Developers Team
 *
 * Latest version may be foind on http://husky.sourceforge.net
 *
 * This file is part of FIDOCONFIG library (part of the Husky FIDOnet
 * software project)
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published
 * by the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * FIDOCONFIG library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with FIDOCONFIG library; see the file COPYING.  If not, write
 * to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA
 *
 * See also http://www.gnu.org
 *****************************************************************************
 * $Id: crc.c,v 1.7.2.1 2004/03/13 15:53:51 stas_degteff Exp $
 */

#include "common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "crc.h"

/**************************** CRC32 table ******************************/

/* First, the polynomial itself and its table of feedback terms.  The  */
/* polynomial is						       */
/* X^32+X^26+X^23+X^22+X^16+X^12+X^11+X^10+X^8+X^7+X^5+X^4+X^2+X^1+X^0 */
/* Note that we take it "backwards" and put the highest-order term in  */
/* the lowest-order bit.  The X^32 term is "implied"; the LSB is the   */
/* X^31 term, etc.  The X^0 term (usually shown as "+1") results in    */
/* the MSB being 1.						       */

/* Note that the usual hardware shift register implementation, which   */
/* is what we're using (we're merely optimizing it by doing eight-bit  */
/* chunks at a time) shifts bits into the lowest-order term.  In our   */
/* implementation, that means shifting towards the right.  Why do we   */
/* do it this way?  Because the calculated CRC must be transmitted in  */
/* order from highest-order term to lowest-order term.	UARTs transmit */
/* characters in order from LSB to MSB.  By storing the CRC this way,  */
/* we hand it to the UART in the order low-byte to high-byte; the UART */
/* sends each low-bit to hight-bit; and the result is transmission bit */
/* by bit from highest- to lowest-order term without requiring any bit */
/* shuffling on our part.  Reception works similarly.		       */

/* The feedback terms table consists of 256, 32-bit entries.  Notes:   */
/*									   */
/*	   The table can be generated at runtime if desired; code to do so */
/*	   is shown later. It might not be obvious, but the feedback	   */
/*	   terms simply represent the results of eight shift/xor opera-    */
/*	   tions for all combinations of data and CRC register values.	   */
/*									   */
/*	   The values must be right-shifted by eight bits by the "updcrc"  */
/*	   logic; the shift must be unsigned (bring in zeroes).  On some   */
/*	   hardware you could probably optimize the shift in assembler by  */
/*	   using byte-swap instructions.				   */

static UINT32 crc32tab[] =	/* CRC polynomial 0xedb88320 */
{
  0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL,
  0x0edb8832UL, 0x79dcb8a4UL, 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL, 0x90bf1d91UL,
  0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL, 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL,
  0x136c9856UL, 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL, 0xfa0f3d63UL, 0x8d080df5UL,
  0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL, 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL,
  0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL, 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL,
  0x26d930acUL, 0x51de003aUL, 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL, 0xb8bda50fUL,
  0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL, 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL,
  0x76dc4190UL, 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL, 0x9fbfe4a5UL, 0xe8b8d433UL,
  0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL, 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL,
  0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL, 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL,
  0x65b0d9c6UL, 0x12b7e950UL, 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL, 0xfbd44c65UL,
  0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL, 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL,
  0x4369e96aUL, 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL, 0xaa0a4c5fUL, 0xdd0d7cc9UL,
  0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL, 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL,
  0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL, 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL,
  0xedb88320UL, 0x9abfb3b6UL, 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL, 0x73dc1683UL,
  0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL, 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL,
  0xf00f9344UL, 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL, 0x196c3671UL, 0x6e6b06e7UL,
  0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL, 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL,
  0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL, 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL,
  0xd80d2bdaUL, 0xaf0a1b4cUL, 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL, 0x4669be79UL,
  0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL, 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL,
  0xc5ba3bbeUL, 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL, 0x2cd99e8bUL, 0x5bdeae1dUL,
  0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL, 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL,
  0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL, 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL,
  0x86d3d2d4UL, 0xf1d4e242UL, 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL, 0x18b74777UL,
  0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL, 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL,
  0xa00ae278UL, 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL, 0x4969474dUL, 0x3e6e77dbUL,
  0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL, 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL,
  0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL, 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL,
  0xb3667a2eUL, 0xc4614ab8UL, 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, 0x2d02ef8dUL
};


/**************************** CRC16 table ******************************/

/* CCITT recommended polinomial table */
UINT16 crc16tab[] =
{
   0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
   0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
   0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
   0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
   0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
   0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
   0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
   0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
   0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
   0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
   0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
   0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
   0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
   0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
   0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
   0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
   0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
   0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
   0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
   0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
   0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
   0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
   0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
   0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
   0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
   0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
   0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
   0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
   0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
   0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
   0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
   0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};


UINT32 memcrc32(const char *str, int size, UINT32 initcrc)
{
  register UINT32 crc = initcrc;

  if(str) for (; size; str++, size--)
    crc = crc32tab[((int) crc ^ (*str)) & 0xff] ^ ((crc >> 8) & 0x00ffffffUL);

  return crc;
}


UINT32 strcrc32(const char *str, UINT32 initcrc)
{
  register UINT32 crc = initcrc;

  if(str) for (; *str; str++)
    crc = crc32tab[((int) crc ^ (*str)) & 0xff] ^ ((crc >> 8) & 0x00ffffffUL);

  return crc;
}


UINT32 filecrc32(const char *filename)
{
  FILE *fd;
  unsigned char* buffer;
  size_t got;
  UINT32 crc;

  fd = fopen(filename, "rb");

  if (!fd)
    return 0UL;

  buffer = smalloc(CRC_BUFFER_SIZE);
  if (buffer == NULL) return 0L;

  crc = 0xFFFFFFFFUL;
  while (1)
  {
    got = fread(buffer, 1, CRC_BUFFER_SIZE, fd);
    if( got )
      crc = memcrc32((char*)buffer, got, crc);
    if (got != CRC_BUFFER_SIZE)
      break;
  }
  nfree(buffer);
  fclose(fd);
  return crc ^ 0xFFFFFFFFUL;
}

#define _byteCRC16_(b, crc) (UINT16)(((UINT16)crc << 8) ^ crc16tab[(unsigned char)((((UINT16)crc >> 8) ^ ((unsigned char)(b))) & 0xff)])

/* Calculate CRC16 for memory array
   str: array
   size: array size
   initcrc: initial value (start from 0x0000)
 */
UINT16 memcrc16(const char *str, int size, UINT16 initcrc)
{
  register UINT16 crc = initcrc;

  if(str) for (; size; str++, size--)
  { crc = _byteCRC16_(*str,crc); }

  return crc;
}

/* Calculate CRC16 for ASCIIZ string
   str: string
   initcrc: initial value (start from 0x0000)
 */
UINT16 strcrc16(const char *str, UINT16 initcrc)
{
  register UINT16 crc = initcrc;

  if(str) for (; *str; str++)
  { crc = _byteCRC16_(*str,crc); }

  return crc;
}


UINT16 filecrc16(const char *filename)
{
  FILE *fd;
  unsigned char* buffer;
  size_t got;
  UINT16 crc;

  fd = fopen(filename, "rb");

  if (!fd)
    return 0UL;

  buffer = smalloc(CRC_BUFFER_SIZE);
  if (buffer == NULL) return 0;

  crc = 0x0000;
  while (1)
  {
    got = fread(buffer, 1, CRC_BUFFER_SIZE, fd);
    if( got )
      crc = memcrc16((char*)buffer, got, crc);
    if (got != CRC_BUFFER_SIZE)
      break;
  }
  nfree(buffer);
  fclose(fd);
  return crc;
}


/* Calculating 16-bit checksum, rotating right before each addition;
 * overflow is discarded.
 * (This algorithm is the algorithm used by historic BSD UNIX systems as
 * the `sum` algorithm and by historic AT&T System V UNIX systems
 * as the `sum -r` algorithm.)
 */

/* 16-bit checksum (sum -r) for ASCIIZ string */
UINT16 strsum16( const char *str )
{ register UINT32 crc=0;

  if(str) for (; *str; str++){
    if (crc & 1) crc |= 0x10000;
    crc = ((crc >> 1) + *str) & 0xffff;
  }

  return (UINT16)crc;
}

/* 16-bit checksum (sum -r) for array of bytes */
UINT16 memsum16( const char *str, unsigned size )
{ register UINT32 crc=0;

  if(str) for (; size; str++, size--)
  { if (crc & 1) crc |= 0x10000;
    crc = ((crc >> 1) + *str) & 0xffff;
  }

  return (UINT16)crc;
}

/* 16-bit checksum (sum -r) for file */
UINT16 filesum16(const char *filename)
{
  FILE *fd;
  unsigned char* buffer;
  size_t got;
  UINT16 crc=0;

  fd = fopen(filename, "rb");

  if (!fd)
    return 0UL;

  buffer = smalloc(CRC_BUFFER_SIZE);

  while (1)
  {
    got = fread(buffer, 1, CRC_BUFFER_SIZE, fd);
    if( got )
      crc = memsum16((char*)buffer, got);
    if (got != CRC_BUFFER_SIZE)
      break;
  }
  nfree(buffer);
  fclose(fd);
  return crc;
}


/* Calculating 32-bit checksum described in Draft 8 POSIX 1003.2
 * (This algorithm is the algorithm used by historic AT&T System V UNIX
 * systems as the `sum` algorithm.)
 */

/* 32bit checksum for ASCIIZ string */
UINT32 strsum32( const char *str )
{ register UINT32 crc=0;

  if(str) for (; *str; str++)
            crc += *str;

  crc = (crc & 0xffff) + (crc >> 16);

  return (UINT32)crc;
}


/* 32bit checksum for array of bytes */
UINT32 memsum32( const char *str, unsigned size )
{ register UINT32 crc=0;

  if(str) for (; size; str++, size--)
            crc += *str;

  crc = (crc & 0xffff) + (crc >> 16);

  return (UINT32)crc;
}


/* 32bit checksum for file
 * plen: pointer to return file lenght, unuse if plen is NULL
 */
UINT32 filesum32( const char *filename, unsigned long *plen )
{ FILE *fd;
  char buffer[CRC_BUFFER_SIZE], *str;
  size_t got;
  register UINT32 crc=0;
  register unsigned long len=0;

  fd = fopen(filename, "rb");

  if (!fd)
    return 0UL;

  while (!feof(fd) && !ferror(fd))
  {
    got = fread(buffer, 1, CRC_BUFFER_SIZE, fd);
    if( got>0 )
      len+=got;
      for (str=buffer; got; str++, got--)
            crc += *str;
  }
  fclose(fd);

  if( plen) *plen = len;
  crc = (crc & 0xffff) + (crc >> 16);
  return crc;
}


syntax highlighted by Code2HTML, v. 0.9.1