Path: blob/main/contrib/elftoolchain/libelftc/libelftc_hash.c
39478 views
/*-1* Copyright (c) 2013, Joseph Koshy2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer9* in this position and unchanged.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526/*27* An implementation of the Fowler-Noll-Vo hash function.28*29* References:30* - http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function31* - http://www.isthe.com/chongo/tech/comp/fnv/32*/3334#include <sys/types.h>3536#include <limits.h>3738#include "_libelftc.h"3940ELFTC_VCSID("$Id: libelftc_hash.c 2870 2013-01-07 10:38:43Z jkoshy $");4142/*43* Use the size of an 'int' to determine the magic numbers used by the44* hash function.45*/4647#if INT_MAX == 2147483647UL48#define FNV_PRIME 16777619UL49#define FNV_OFFSET 2166136261UL50#elif INT_MAX == 18446744073709551615ULL51#define FNV_PRIME 1099511628211ULL52#define FNV_OFFSET 14695981039346656037ULL53#else54#error sizeof(int) is unknown.55#endif5657unsigned int58libelftc_hash_string(const char *s)59{60char c;61unsigned int hash;6263for (hash = FNV_OFFSET; (c = *s) != '\0'; s++) {64hash ^= c;65hash *= FNV_PRIME;66}6768return (hash);69}707172