void prepareCryptTable() { 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 ); } } }
unsigned long HashString( char *lpszFileName, unsigned long dwHashType ) { unsigned char *key = (unsigned char *)lpszFileName; unsigned long seed1 = 0x7FED7FED; unsigned long seed2 = 0xEEEEEEEE; int ch; while( *key != 0 ) { ch = toupper(*key++); seed1 = cryptTable[(dwHashType << 8) + ch] ^ (seed1 + seed2); seed2 = ch + seed1 + seed2 + (seed2 << 5) + 3; } return seed1; }Blizzard的这个算法是非常高效的,被称为"One-Way Hash"( A one-way hash is a an algorithm that is constructed in such a way that deriving the original string (set of strings, actually) is virtually impossible)。举个例子,字符串"unitneutralacritter.grp"通过这个算法得到的结果是0xA26067F3。
typedef struct { int nHashA; int nHashB; bool bExists; ...... } SOMESTRUCTRUE;一种可能的结构体定义?
//lpszString要在Hash表中查找的字符串,lpTable为存储字符串Hash值的Hash表。 int GetHashTablePos( har *lpszString, SOMESTRUCTURE *lpTable ) { int nHash = HashString(lpszString); //调用上述函数二,返回要查找字符串lpszString的Hash值。 int nHashPos = nHash % nTableSize; //如果找到的Hash值在表中存在,且要查找的字符串与表中对应位置的字符串相同,则返回上述调用函数二后,找到的Hash值 if ( lpTable[nHashPos].bExists && !strcmp( lpTable[nHashPos].pString, lpszString ) ) { return nHashPos; } else { return -1; } }
int GetHashTablePos( char *lpszString, MPQHASHTABLE *lpTable, int nTableSize ) { const int HASH_OFFSET = 0, HASH_A = 1, HASH_B = 2; int nHash = HashString( lpszString, HASH_OFFSET ); int nHashA = HashString( lpszString, HASH_A ); int nHashB = HashString( lpszString, HASH_B ); int nHashStart = nHash % nTableSize; int nHashPos = nHashStart; while ( lpTable[nHashPos].bExists ) { if ( lpTable[nHashPos].nHashA == nHashA && lpTable[nHashPos].nHashB == nHashB ) { return nHashPos; } else { nHashPos = (nHashPos + 1) % nTableSize; } if (nHashPos == nHashStart) break; } return -1; }上述程序解释:
6. 看看是不是又回到了原来的位置,如果是,则返回没找到
7. 回到3