// QBKey, based on efone code for CRC32

/*
 * efone - Distributed internet phone system.
 *
 * (c) 1999,2000 Krzysztof Dabrowski
 * (c) 1999,2000 ElysiuM deeZine
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 *
 */

/* based on implementation by Finn Yannick Jacobs */
/* modified to make quickly QBKeys, and "stylized" the code a bit */

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

/* crc_tab[] -- Array with the CRC tabkle */

unsigned int crc_tab[256];

/* makeQBKey() -- make a QBKey (CRC32 ^ 0xFFFFFFFF of a string) */

unsigned int makeQBKey(unsigned char *block)
{
	register unsigned long crc;
	unsigned long i;
	unsigned int length = strlen(block);

	crc = 0xFFFFFFFF;
	for (i = 0; i < length; i++)
		crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_tab[(crc ^ *block++) & 0xFF];

	return crc;
}

/* initCRC32 --> Make the CRC table */
void initCRC32()
{
	unsigned long crc, poly;
	int i, j;

	poly = 0xEDB88320L;
	for (i = 0; i < 256; i++)
	{
		crc = i;
		for (j = 8; j > 0; j--)
		{
			if (crc & 1)
				crc = (crc >> 1) ^ poly;
			else
				crc >>= 1;
		}

		crc_tab[i] = crc;
	}
}
