1260684Skaiw/*- 2260684Skaiw * Copyright (c) 2013, Joseph Koshy 3260684Skaiw * All rights reserved. 4260684Skaiw * 5260684Skaiw * Redistribution and use in source and binary forms, with or without 6260684Skaiw * modification, are permitted provided that the following conditions 7260684Skaiw * are met: 8260684Skaiw * 1. Redistributions of source code must retain the above copyright 9260684Skaiw * notice, this list of conditions and the following disclaimer 10260684Skaiw * in this position and unchanged. 11260684Skaiw * 2. Redistributions in binary form must reproduce the above copyright 12260684Skaiw * notice, this list of conditions and the following disclaimer in the 13260684Skaiw * documentation and/or other materials provided with the distribution. 14260684Skaiw * 15260684Skaiw * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16260684Skaiw * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17260684Skaiw * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18260684Skaiw * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19260684Skaiw * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20260684Skaiw * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21260684Skaiw * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22260684Skaiw * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23260684Skaiw * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24260684Skaiw * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25260684Skaiw */ 26260684Skaiw 27260684Skaiw/* 28260684Skaiw * An implementation of the Fowler-Noll-Vo hash function. 29260684Skaiw * 30260684Skaiw * References: 31260684Skaiw * - http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function 32260684Skaiw * - http://www.isthe.com/chongo/tech/comp/fnv/ 33260684Skaiw */ 34260684Skaiw 35260684Skaiw#include <sys/types.h> 36260684Skaiw 37260684Skaiw#include <limits.h> 38260684Skaiw 39260684Skaiw#include "_libelftc.h" 40260684Skaiw 41260684SkaiwELFTC_VCSID("$Id: libelftc_hash.c 2870 2013-01-07 10:38:43Z jkoshy $"); 42260684Skaiw 43260684Skaiw/* 44260684Skaiw * Use the size of an 'int' to determine the magic numbers used by the 45260684Skaiw * hash function. 46260684Skaiw */ 47260684Skaiw 48260684Skaiw#if INT_MAX == 2147483647UL 49260684Skaiw#define FNV_PRIME 16777619UL 50260684Skaiw#define FNV_OFFSET 2166136261UL 51260684Skaiw#elif INT_MAX == 18446744073709551615ULL 52260684Skaiw#define FNV_PRIME 1099511628211ULL 53260684Skaiw#define FNV_OFFSET 14695981039346656037ULL 54260684Skaiw#else 55260684Skaiw#error sizeof(int) is unknown. 56260684Skaiw#endif 57260684Skaiw 58260684Skaiwunsigned int 59260684Skaiwlibelftc_hash_string(const char *s) 60260684Skaiw{ 61260684Skaiw char c; 62260684Skaiw unsigned int hash; 63260684Skaiw 64260684Skaiw for (hash = FNV_OFFSET; (c = *s) != '\0'; s++) { 65260684Skaiw hash ^= c; 66260684Skaiw hash *= FNV_PRIME; 67260684Skaiw } 68260684Skaiw 69260684Skaiw return (hash); 70260684Skaiw} 71