2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* Fowler/Noll/Vo string hash
|
2009-04-18 18:14:48 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
|
|
|
|
|
|
|
// FNV1-A hash - good for strings.
|
|
|
|
// if len = 0 (default), treat buf as a C-string;
|
|
|
|
// otherwise, hash <len> bytes of buf.
|
|
|
|
u32 fnv_hash(const void* buf, size_t len)
|
|
|
|
{
|
|
|
|
u32 h = 0x811c9dc5u;
|
|
|
|
// give distinct values for different length 0 buffers.
|
|
|
|
// value taken from FNV; it has no special significance.
|
|
|
|
|
|
|
|
const u8* p = (const u8*)buf;
|
|
|
|
|
|
|
|
// expected case: string
|
|
|
|
if(!len)
|
|
|
|
{
|
|
|
|
while(*p)
|
|
|
|
{
|
|
|
|
h ^= *p++;
|
|
|
|
h *= 0x01000193u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t bytes_left = len;
|
|
|
|
while(bytes_left != 0)
|
|
|
|
{
|
|
|
|
h ^= *p++;
|
|
|
|
h *= 0x01000193u;
|
|
|
|
|
|
|
|
bytes_left--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// FNV1-A hash - good for strings.
|
|
|
|
// if len = 0 (default), treat buf as a C-string;
|
|
|
|
// otherwise, hash <len> bytes of buf.
|
|
|
|
u64 fnv_hash64(const void* buf, size_t len)
|
|
|
|
{
|
|
|
|
u64 h = 0xCBF29CE484222325ull;
|
|
|
|
// give distinct values for different length 0 buffers.
|
|
|
|
// value taken from FNV; it has no special significance.
|
|
|
|
|
|
|
|
const u8* p = (const u8*)buf;
|
|
|
|
|
|
|
|
// expected case: string
|
|
|
|
if(!len)
|
|
|
|
{
|
|
|
|
while(*p)
|
|
|
|
{
|
|
|
|
h ^= *p++;
|
|
|
|
h *= 0x100000001B3ull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t bytes_left = len;
|
|
|
|
while(bytes_left != 0)
|
|
|
|
{
|
|
|
|
h ^= *p++;
|
|
|
|
h *= 0x100000001B3ull;
|
|
|
|
|
|
|
|
bytes_left--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// special version for strings: first converts to lowercase
|
|
|
|
// (useful for comparing mixed-case filenames).
|
|
|
|
// note: still need <len>, e.g. to support non-0-terminated strings
|
|
|
|
u32 fnv_lc_hash(const char* str, size_t len)
|
|
|
|
{
|
|
|
|
u32 h = 0x811c9dc5u;
|
|
|
|
// give distinct values for different length 0 buffers.
|
|
|
|
// value taken from FNV; it has no special significance.
|
|
|
|
|
|
|
|
// expected case: string
|
|
|
|
if(!len)
|
|
|
|
{
|
|
|
|
while(*str)
|
|
|
|
{
|
|
|
|
h ^= tolower(*str++);
|
|
|
|
h *= 0x01000193u;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t bytes_left = len;
|
|
|
|
while(bytes_left != 0)
|
|
|
|
{
|
|
|
|
h ^= tolower(*str++);
|
|
|
|
h *= 0x01000193u;
|
|
|
|
|
|
|
|
bytes_left--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return h;
|
|
|
|
}
|