crc32.c

来自「nbfdec是可以把PPC上的rom由nbf转换为nba文件工具,支持自动查询解」· C语言 代码 · 共 50 行

C
50
字号
/* crc32.c -- compute the CRC-32 of a data stream
 * Copyright (C) 1995-2002 Mark Adler
 * For conditions of distribution and use, see copyright notice in zlib.h
 */

#include "project.h"

static int crc_table_empty = 1;
static ULONG crc_table[256];
static void make_crc_table(void);

static void make_crc_table()
{
  ULONG c;
  int n, k;
  ULONG poly;            /* polynomial exclusive-or pattern */
  /* terms of polynomial defining this crc (except x^32): */
  static const BYTE p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26};

  /* make exclusive-or pattern from polynomial (0xedb88320L) */
  poly = 0L;
  for (n = 0; n < sizeof(p)/sizeof(BYTE); n++)
    poly |= 1L << (31 - p[n]);

  for (n = 0; n < 256; n++)
  {
    c = (ULONG)n;
    for (k = 0; k < 8; k++)
      c = c & 1 ? poly ^ (c >> 1) : c >> 1;
    crc_table[n] = c;
  }
  crc_table_empty = 0;
}

ULONG 
crc32(
	  PVOID buf,
	  SIZE_T len
	  )
{
	PUCHAR buffer = buf;
	ULONG crc = 0;
	if (crc_table_empty)
		make_crc_table();
	while( len-- ) {
		crc = crc_table[((int)crc ^ (*buffer++)) & 0xff] ^ (crc >> 8);
	}
	return crc;
}

⌨️ 快捷键说明

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