crc32.c

来自「压缩算法的C语言源程序」· C语言 代码 · 共 44 行

C
44
字号
/*
   crc32.c
   Cyclic Redundancy Check (32-bit) for gsLib
   Implements the CRC-32 algorithm described in the gzip specs
*/
#include <compress.h>

static unsigned long crc_table[256];

/*
   void init_crc_table():
   Calculate a table for CRC computation
*/
void init_crc_table()
{
   unsigned long c;
   int n, k;

   for(n = 0; n < 256; n++) {
      c = (unsigned long) n;
      for(k = 0; k < 8; k++) {
         if(c & 1) c = 0xedb88320L ^ (c >> 1);
         else c >>= 1;
      }
      crc_table[n] = c;
   }
}

/*
   unsigned long update_crc(unsigned long crc,unsigned char *buf, int len):
   Updates a running crc with the bytes buf[0..len-1] and returns the updated
   crc
*/
unsigned long update_crc(unsigned long crc,unsigned char *buf, int len)
{
   unsigned long c = crc ^ 0xffffffffL;
   int n;

   for(n = 0; n < len; n++)
      c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);

   return c ^ 0xffffffffL;
}

⌨️ 快捷键说明

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