hash.c

来自「模仿cisco路由器」· C语言 代码 · 共 59 行

C
59
字号
/*	Copyright (C) 2002, 2003 Slava Astashonok <sla@0n.ru>	This program is free software; you can redistribute it and/or	modify it under the terms of the GNU General Public License.	$Id: hash.c,v 1.3.2.1 2003/03/28 22:57:51 sla Exp $*/#include <common.h>#include <hash.h>#ifdef HASH_TYPE_CRCstatic uint16_t crc16_table[256];uint16_t crc16(uint16_t crc, uint8_t val){	int i;	crc ^= val << 8;	for (i = 8; i--; )		crc = crc & 0x8000 ? (crc << 1) ^ CRC16_POLY : crc << 1;	return crc;}#endifhash_t hash(void *p, int size){	/*	?FIXME?	Check for valid size (> 0)	*/	hash_t hash = 0;#if defined HASH_TYPE_XOR && HASH_BITS == 16	size--;#endif	for (;size--;) {#ifdef HASH_TYPE_XOR		hash ^= *((hash_t *)p++);#endif#ifdef HASH_TYPE_CRC		hash = crc16_table[*((uint8_t *)p++) ^ (hash >> 8)] ^ (hash << 8);#endif	}	return hash;}void hash_init(){#ifdef HASH_TYPE_CRC	int i;	for (i = 0; i < 256 ; i++) crc16_table[i] = crc16(0, i);#endif}

⌨️ 快捷键说明

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