#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAXMPQHASHTABLELEN 8192
typedef struct {
long nHashA;
long nHashB;
unsigned int bExists;
}MPQHASHTABLE;
unsigned long cryptTable[0x500];
unsigned long HashString(char *lpszString, unsigned long dwHashType);
unsigned int MPQHashTableInit(char **ppHashTable, long nTableLength);
void MPQHashTableFree(char *pHashTable);
unsigned int MPQHashTableAdd(char *lpszString, char *pHashTable);
long MPQHashTableIsExist(char *lpszString, char *pHashTable);
static void InitCryptTable()
{
unsigned long seed = 0x00100001, index1 = 0, index2 = 0, i;
for (index1 = 0; index1 < 0x100; index1++)
{
for (index2 = index1, i = 0; i < 5; i++, index2 += 0x100)
{
unsigned long temp1, temp2;
seed = (seed * 125 + 3) % 0x2AAAAB;
temp1 = (seed & 0xFFFF) << 0x10;
seed = (seed * 125 + 3) % 0x2AAAAB;
temp2 = (seed & 0xFFFF);
cryptTable[index2] = (temp1 | temp2);
}
}
}
/*
函数名:HashString
功能:计算字符串的哈希值
参数:lpszString:字符串的地址
dwHashType:哈希值类型
dwHashType = 0时计算的哈希值用于确定字符串在哈希表中的位置;
dwHashType = 1,dwHashType = 2时计算的哈希值用于验证字符串
返回值:字符串的哈希值
*/
unsigned long HashString(char *lpszString, unsigned long dwHashType)
{
unsigned char *key = (unsigned char *)lpszString;
unsigned long seed1 = 0x7FED7FED, seed2 = 0xEEEEEEEE;
int ch;
while(*key != 0)
{
ch = toupper(*key++);
seed1 = cryptTable[(dwHashType << 8) + ch] ^ (seed1 + seed2);