[comp.lang.c] crc-hash functions.

oz@yunexus.UUCP (Ozan Yigit) (05/17/89)

The following is a crc-hash function (16bit) from the Decus Diff.
I am not familiar with CRC polynomials, nor hash functions based
on them. Would someone knowledgeable on the topic either provide
a 32-bit version of this, or send me the appropriate references ?

Please mail. many thnx... oz

----
/*
 * Return the CRC16 hash code for the buffer (but never 0).
 * Algorithm from Stu Wecker (Digital memo 130-959-002-00).
 * Since the value 0 is used as a flag, the hash will be
 * 1 if the CRC is 0.
 */

u_long crc16a[] = {
	0000000, 0140301, 0140601, 0000500, 
	0141401, 0001700, 0001200, 0141101,
	0143001, 0003300, 0003600, 0143501, 
	0002400, 0142701, 0142201, 0002100,
};
u_long crc16b[] = {
	0000000, 0146001, 0154001, 0012000, 
	0170001, 0036000, 0024000, 0162001,
	0120001, 0066000, 0074000, 0132001, 
	0050000, 0116001, 0104001, 0043000,
};

u_long
hash(str)
register char *str;
{
	register u_long crc;
	register u_long tmp;

	crc = 0;
	while (*str)
		tmp = *str++ ^ crc;    /* XOR crc with new char  */
		crc = (crc >> 8)
			^ crc16a[(tmp & 0017)]
			^ crc16b[(tmp & 0360) >> 4];
	}
	return ((crc == 0) ? (u_long) 1 : crc);
}