diff --git a/src/m_misc.c b/src/m_misc.c index 7f002d87b..bdfa0c779 100644 --- a/src/m_misc.c +++ b/src/m_misc.c @@ -2143,3 +2143,48 @@ void M_MkdirEach(const char *path, int start, int mode) { M_MkdirEachUntil(path, start, -1, mode); } + +// Hashes some message using FNV-1a +#define FNV1A_OFFSET_BASIS 0x811C9DC5 +#define FNV1A_PRIME 0x01000193 + +UINT32 FNV1a_Hash(const char *message, size_t size) +{ + UINT32 hash = FNV1A_OFFSET_BASIS; + + for (size_t i = 0; i < size; i++) + { + hash ^= message[i]; + hash *= FNV1A_PRIME; + } + + return hash; +} + +UINT32 FNV1a_HashString(const char *message) +{ + UINT32 hash = FNV1A_OFFSET_BASIS; + + while (*message) + { + hash ^= *message; + hash *= FNV1A_PRIME; + message++; + } + + return hash; +} + +UINT32 FNV1a_HashLowercaseString(const char *message) +{ + UINT32 hash = FNV1A_OFFSET_BASIS; + + while (*message) + { + hash ^= tolower(*message); + hash *= FNV1A_PRIME; + message++; + } + + return hash; +} diff --git a/src/m_misc.h b/src/m_misc.h index 013da2235..56b035868 100644 --- a/src/m_misc.h +++ b/src/m_misc.h @@ -107,6 +107,11 @@ void M_MkdirEachUntil (const char *path, int start, int end, int mode); // counting bits, for weapon ammo code, usually FUNCMATH UINT8 M_CountBits(UINT32 num, UINT8 size); +// Hashes some message using FNV-1a +UINT32 FNV1a_Hash(const char *message, size_t size); +UINT32 FNV1a_HashString(const char *message); +UINT32 FNV1a_HashLowercaseString(const char *message); + #include "w_wad.h" extern char configfile[MAX_WADPATH]; diff --git a/src/w_wad.c b/src/w_wad.c index ac61e574f..2a2efb19e 100644 --- a/src/w_wad.c +++ b/src/w_wad.c @@ -337,6 +337,11 @@ static void W_InvalidateLumpnumCache(void) memset(lumpnumcache, 0, sizeof (lumpnumcache)); } +UINT32 W_HashLumpName(const char *name) +{ + return FNV1a_HashLowercaseString(name); +} + /** Detect a file type. * \todo Actually detect the wad/pkzip headers and whatnot, instead of just checking the extensions. */ @@ -361,12 +366,24 @@ static lumpinfo_t* ResGetLumpsStandalone (FILE* handle, UINT16* numlumps, const fseek(handle, 0, SEEK_END); lumpinfo->size = ftell(handle); fseek(handle, 0, SEEK_SET); - strlcpy(lumpinfo->name, lumpname, 9); - lumpinfo->hash = quickncasehash(lumpname, 8); - // Allocate the lump's full and long name. - lumpinfo->fullname = Z_StrDup(lumpname); - lumpinfo->longname = Z_StrDup(lumpname); + strlcpy(lumpinfo->name, lumpname, sizeof(lumpinfo->name)); + lumpinfo->namelength = strlen(lumpinfo->name); + lumpinfo->hash.name = W_HashLumpName(lumpname); + + // Allocate the lump's long name. + lumpinfo->longname = Z_Malloc((lumpinfo->namelength + 1) * sizeof(char), PU_STATIC, NULL); + strcpy(lumpinfo->longname, lumpname); + lumpinfo->longname[lumpinfo->namelength] = '\0'; + lumpinfo->longnamelength = lumpinfo->namelength; + lumpinfo->hash.longname = lumpinfo->hash.name; + + // Allocate the lump's full name. + lumpinfo->fullname = Z_Malloc((lumpinfo->namelength + 1) * sizeof(char), PU_STATIC, NULL); + strcpy(lumpinfo->fullname, lumpname); + lumpinfo->fullname[lumpinfo->namelength] = '\0'; + lumpinfo->fullnamelength = lumpinfo->namelength; + lumpinfo->hash.fullname = lumpinfo->hash.name; *numlumps = 1; return lumpinfo; @@ -454,17 +471,22 @@ static lumpinfo_t* ResGetLumpsWad (FILE* handle, UINT16* nlmp, const char* filen lump_p->compression = CM_NOCOMPRESSION; memset(lump_p->name, 0x00, 9); strncpy(lump_p->name, fileinfo->name, 8); - lump_p->hash = quickncasehash(lump_p->name, 8); + lump_p->namelength = strlen(lump_p->name); + lump_p->hash.name = W_HashLumpName(lump_p->name); // Allocate the lump's long name. lump_p->longname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL); strncpy(lump_p->longname, fileinfo->name, 8); lump_p->longname[8] = '\0'; + lump_p->longnamelength = lump_p->namelength; + lump_p->hash.longname = lump_p->hash.name; // Allocate the lump's full name. lump_p->fullname = Z_Malloc(9 * sizeof(char), PU_STATIC, NULL); strncpy(lump_p->fullname, fileinfo->name, 8); lump_p->fullname[8] = '\0'; + lump_p->fullnamelength = lump_p->namelength; + lump_p->hash.fullname = lump_p->hash.name; } free(fileinfov); *nlmp = numlumps; @@ -657,13 +679,18 @@ static lumpinfo_t* ResGetLumpsZip (FILE* handle, UINT16* nlmp) memset(lump_p->name, '\0', 9); // Making sure they're initialized to 0. Is it necessary? strncpy(lump_p->name, trimname, min(8, dotpos - trimname)); - lump_p->hash = quickncasehash(lump_p->name, 8); + lump_p->namelength = strlen(lump_p->name); + lump_p->hash.name = W_HashLumpName(lump_p->name); lump_p->longname = Z_Calloc(dotpos - trimname + 1, PU_STATIC, NULL); strlcpy(lump_p->longname, trimname, dotpos - trimname + 1); + lump_p->longnamelength = strlen(lump_p->longname); + lump_p->hash.longname = W_HashLumpName(lump_p->longname); lump_p->fullname = (char*)(Z_Calloc(SHORT(zentry->namelen) + 1, PU_STATIC, NULL)); strncpy(lump_p->fullname, fullname, SHORT(zentry->namelen)); + lump_p->fullnamelength = zentry->namelen; + lump_p->hash.fullname = W_HashLumpName(lump_p->fullname); switch(SHORT(zentry->compression)) { @@ -979,6 +1006,7 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) UINT16 i; static char uname[9]; UINT32 hash; + size_t namelen; if (!TestValidLump(wad,0)) return INT16_MAX; @@ -986,7 +1014,8 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) memset(uname, 0, sizeof uname); strncpy(uname, name, sizeof(uname)-1); strupr(uname); - hash = quickncasehash(uname, 8); + namelen = strlen(uname); + hash = W_HashLumpName(uname); // // scan forward @@ -997,7 +1026,9 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) { lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - if (lump_p->hash == hash && memcmp(lump_p->name, uname, sizeof(uname) - 1) == 0) + if (lump_p->namelength == namelen + && lump_p->hash.name == hash + && !memcmp(lump_p->name, uname, sizeof(uname) - 1)) return i; } @@ -1014,13 +1045,12 @@ UINT16 W_CheckNumForNamePwad(const char *name, UINT16 wad, UINT16 startlump) UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) { UINT16 i; - static char uname[256 + 1]; if (!TestValidLump(wad,0)) return INT16_MAX; - strlcpy(uname, name, sizeof uname); - strupr(uname); + size_t namelen = strlen(name); + UINT32 hash = W_HashLumpName(name); // // scan forward @@ -1031,7 +1061,9 @@ UINT16 W_CheckNumForLongNamePwad(const char *name, UINT16 wad, UINT16 startlump) { lumpinfo_t *lump_p = wadfiles[wad]->lumpinfo + startlump; for (i = startlump; i < wadfiles[wad]->numlumps; i++, lump_p++) - if (!strcmp(lump_p->longname, uname)) + if (lump_p->longnamelength == namelen + && lump_p->hash.name == hash + && !stricmp(lump_p->longname, name)) return i; } @@ -1205,50 +1237,6 @@ lumpnum_t W_CheckNumForLongName(const char *name) } } -// Look for valid map data through all added files in descendant order. -// Get a map marker for WADs, and a standalone WAD file lump inside PK3s. -// TODO: Make it search through cache first, maybe...? -lumpnum_t W_CheckNumForMap(const char *name) -{ - UINT32 hash = name ? quickncasehash(name, 8) : 0; - UINT16 lumpNum, end; - UINT32 i; - lumpinfo_t *p; - for (i = numwadfiles - 1; i < numwadfiles; i--) - { - if (wadfiles[i]->type == RET_WAD) - { - for (lumpNum = 0; lumpNum < wadfiles[i]->numlumps; lumpNum++) - { - p = wadfiles[i]->lumpinfo + lumpNum; - if (p->hash == hash && !strncmp(name, p->name, 8)) - return (i<<16) + lumpNum; - } - } - else if (wadfiles[i]->type == RET_PK3) - { - lumpNum = W_CheckNumForFolderStartPK3("maps/", i, 0); - if (lumpNum != INT16_MAX) - end = W_CheckNumForFolderEndPK3("maps/", i, lumpNum); - else - continue; - // Now look for the specified map. - for (; lumpNum < end; lumpNum++) - { - p = wadfiles[i]->lumpinfo + lumpNum; - - if (p->hash == hash && !strnicmp(name, p->name, 8)) - { - const char *extension = strrchr(p->fullname, '.'); - if (!(extension && stricmp(extension, ".wad"))) - return (i<<16) + lumpNum; - } - } - } - } - return LUMPERROR; -} - // // W_GetNumForName // diff --git a/src/w_wad.h b/src/w_wad.h index 67e28a290..dd7e94050 100644 --- a/src/w_wad.h +++ b/src/w_wad.h @@ -69,11 +69,21 @@ typedef struct unsigned long position; // filelump_t filepos unsigned long disksize; // filelump_t size char name[9]; // filelump_t name[] e.g. "LongEntr" - UINT32 hash; char *longname; // e.g. "LongEntryName" char *fullname; // e.g. "Folder/Subfolder/LongEntryName.extension" + + size_t namelength; // length of name + size_t longnamelength; // length of longname + size_t fullnamelength; // length of fullname + size_t size; // real (uncompressed) size compmethod compression; // lump compression method + + struct { + UINT32 name; // hash of name + UINT32 longname; // hash of longname + UINT32 fullname; // hash of fullname + } hash; } lumpinfo_t; // ========================================================================= @@ -164,6 +174,8 @@ INT32 W_AddAutoloadedLocalFiles(char **filenames); #define W_FileHasFolders(wadfile) ((wadfile)->type == RET_PK3) +UINT32 W_HashLumpName(const char *name); + const char *W_CheckNameForNumPwad(UINT16 wad, UINT16 lump); const char *W_CheckNameForNum(lumpnum_t lumpnum); @@ -177,7 +189,6 @@ UINT16 W_CheckNumForFullNamePK3(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForFolderStartPK3(const char *name, UINT16 wad, UINT16 startlump); UINT16 W_CheckNumForFolderEndPK3(const char *name, UINT16 wad, UINT16 startlump); -lumpnum_t W_CheckNumForMap(const char *name); lumpnum_t W_CheckNumForName(const char *name); lumpnum_t W_CheckNumForLongName(const char *name); lumpnum_t W_GetNumForName(const char *name); // like W_CheckNumForName but I_Error on LUMPERROR