⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crc.c

📁 开放源码的编译器open watcom 1.6.0版的源代码
💻 C
字号:
/*
 *  These CRC functions are derived from code in chapter 19 of the book
 *  "C Programmer's Guide to Serial Communications", by Joe Campbell.
 *
 *  Only used by the language module (ref. language.l)
 */

#include <stdio.h>
#include <stdlib.h>

#include "wattcp.h"
#include "crc.h"

#define CRCBITS    32
#define CRCHIBIT   ((DWORD) (1L << (CRCBITS-1)))
#define CRCSHIFTS  (CRCBITS-8)
#define PRZCRC     0x864CFBL   /* PRZ's 24-bit CRC generator polynomial */

static DWORD *crctable;        /* Table for speeding up CRC's */

/*
 * mk_crctbl() derives a CRC lookup table from the CRC polynomial.
 * The table is used later by watt_crc_bytes() below.
 * mk_crctbl() only needs to be called once at the dawn of time.
 *
 * The theory behind mk_crctbl() is that table[i] is initialized
 * with the CRC of i, and this is related to the CRC of i>>1,
 * so the CRC of i>>1 (pointed to by p) can be used to derive
 * the CRC of i (pointed to by q).
 */
static void mk_crctbl (DWORD poly, DWORD *tab)
{
  int   i;
  DWORD *p = tab;
  DWORD *q = tab;

  *q++ = 0;
  *q++ = poly;
  for (i = 1; i < 128; i++)
  {
    DWORD t = *++p;

    if (t & CRCHIBIT)
    {
      t <<= 1;
      *q++ = t ^ poly;
      *q++ = t;
    }
    else
    {
      t <<= 1;
      *q++ = t;
      *q++ = t ^ poly;
    }
  }
}

/*
 * Calculate 32-bit CRC on buffer buf with length len
 */
DWORD watt_crc_bytes (const char *buf, unsigned len)
{
  DWORD accum = 0;

  if (!crctable)
     return (0L);

  while (len > 0)
  {
    accum = (accum << 8) ^ crctable[(BYTE)(accum >> CRCSHIFTS) ^ *buf++];
    len--;
  }
  return (accum);
}

int watt_crc_init (void)
{
  if (crctable)
     return (1);

  crctable = (DWORD*) calloc (sizeof(DWORD), 256);
  if (!crctable)
     return (0);

  mk_crctbl (PRZCRC, crctable);
  return (1);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -