1*0a6a1f1dSLionel Sambuc /* $NetBSD: hash.h,v 1.8 2014/09/05 05:46:15 matt Exp $ */
2f6aac1c3SLionel Sambuc
3f6aac1c3SLionel Sambuc /*-
4f6aac1c3SLionel Sambuc * Copyright (c) 2001 The NetBSD Foundation, Inc.
5f6aac1c3SLionel Sambuc * All rights reserved.
6f6aac1c3SLionel Sambuc *
7f6aac1c3SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8f6aac1c3SLionel Sambuc * by Luke Mewburn.
9f6aac1c3SLionel Sambuc *
10f6aac1c3SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11f6aac1c3SLionel Sambuc * modification, are permitted provided that the following conditions
12f6aac1c3SLionel Sambuc * are met:
13f6aac1c3SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14f6aac1c3SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15f6aac1c3SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16f6aac1c3SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17f6aac1c3SLionel Sambuc * documentation and/or other materials provided with the distribution.
18f6aac1c3SLionel Sambuc *
19f6aac1c3SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f6aac1c3SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f6aac1c3SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f6aac1c3SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f6aac1c3SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f6aac1c3SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f6aac1c3SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f6aac1c3SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f6aac1c3SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f6aac1c3SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f6aac1c3SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30f6aac1c3SLionel Sambuc */
31f6aac1c3SLionel Sambuc
32f6aac1c3SLionel Sambuc #ifndef _SYS_HASH_H_
33f6aac1c3SLionel Sambuc #define _SYS_HASH_H_
34f6aac1c3SLionel Sambuc
35f6aac1c3SLionel Sambuc #include <sys/types.h>
36f6aac1c3SLionel Sambuc
37f6aac1c3SLionel Sambuc #ifdef __HAVE_MACHINE_HASH_H
38f6aac1c3SLionel Sambuc #include <machine/hash.h>
39f6aac1c3SLionel Sambuc #endif
40f6aac1c3SLionel Sambuc
41f6aac1c3SLionel Sambuc #ifndef __HAVE_HASH32_BUF /* not overridden by MD hash */
42f6aac1c3SLionel Sambuc
43f6aac1c3SLionel Sambuc #define HASH32_BUF_INIT 5381
44f6aac1c3SLionel Sambuc
45f6aac1c3SLionel Sambuc /*
46f6aac1c3SLionel Sambuc * uint32_t
47f6aac1c3SLionel Sambuc * hash32_buf(const void *bf, size_t len, uint32_t hash)
48f6aac1c3SLionel Sambuc * return a 32 bit hash of the binary buffer buf (size len),
49f6aac1c3SLionel Sambuc * seeded with an initial hash value of hash (usually HASH32_BUF_INIT).
50f6aac1c3SLionel Sambuc */
51f6aac1c3SLionel Sambuc static __inline uint32_t
hash32_buf(const void * bf,size_t len,uint32_t hash)52f6aac1c3SLionel Sambuc hash32_buf(const void *bf, size_t len, uint32_t hash)
53f6aac1c3SLionel Sambuc {
54*0a6a1f1dSLionel Sambuc const uint8_t *s = (const uint8_t *)bf;
55f6aac1c3SLionel Sambuc
56f6aac1c3SLionel Sambuc while (len-- != 0) /* "nemesi": k=257, r=r*257 */
57f6aac1c3SLionel Sambuc hash = hash * 257 + *s++;
58f6aac1c3SLionel Sambuc return (hash * 257);
59f6aac1c3SLionel Sambuc }
60f6aac1c3SLionel Sambuc #endif /* __HAVE_HASH32_BUF */
61f6aac1c3SLionel Sambuc
62f6aac1c3SLionel Sambuc
63f6aac1c3SLionel Sambuc #ifndef __HAVE_HASH32_STR /* not overridden by MD hash */
64f6aac1c3SLionel Sambuc
65f6aac1c3SLionel Sambuc #define HASH32_STR_INIT 5381
66f6aac1c3SLionel Sambuc /*
67f6aac1c3SLionel Sambuc * uint32_t
68f6aac1c3SLionel Sambuc * hash32_str(const void *bf, uint32_t hash)
69f6aac1c3SLionel Sambuc * return a 32 bit hash of NUL terminated ASCII string buf,
70f6aac1c3SLionel Sambuc * seeded with an initial hash value of hash (usually HASH32_STR_INIT).
71f6aac1c3SLionel Sambuc */
72f6aac1c3SLionel Sambuc static __inline uint32_t
hash32_str(const void * bf,uint32_t hash)73f6aac1c3SLionel Sambuc hash32_str(const void *bf, uint32_t hash)
74f6aac1c3SLionel Sambuc {
75*0a6a1f1dSLionel Sambuc const uint8_t *s = (const uint8_t *)bf;
76f6aac1c3SLionel Sambuc uint8_t c;
77f6aac1c3SLionel Sambuc
78f6aac1c3SLionel Sambuc while ((c = *s++) != 0)
79f6aac1c3SLionel Sambuc hash = hash * 33 + c; /* "perl": k=33, r=r+r/32 */
80f6aac1c3SLionel Sambuc return (hash + (hash >> 5));
81f6aac1c3SLionel Sambuc }
82f6aac1c3SLionel Sambuc
83f6aac1c3SLionel Sambuc /*
84f6aac1c3SLionel Sambuc * uint32_t
85f6aac1c3SLionel Sambuc * hash32_strn(const void *bf, size_t len, uint32_t hash)
86f6aac1c3SLionel Sambuc * return a 32 bit hash of NUL terminated ASCII string buf up to
87f6aac1c3SLionel Sambuc * a maximum of len bytes,
88f6aac1c3SLionel Sambuc * seeded with an initial hash value of hash (usually HASH32_STR_INIT).
89f6aac1c3SLionel Sambuc */
90f6aac1c3SLionel Sambuc static __inline uint32_t
hash32_strn(const void * bf,size_t len,uint32_t hash)91f6aac1c3SLionel Sambuc hash32_strn(const void *bf, size_t len, uint32_t hash)
92f6aac1c3SLionel Sambuc {
93*0a6a1f1dSLionel Sambuc const uint8_t *s = (const uint8_t *)bf;
94f6aac1c3SLionel Sambuc uint8_t c;
95f6aac1c3SLionel Sambuc
96f6aac1c3SLionel Sambuc while ((c = *s++) != 0 && len-- != 0)
97f6aac1c3SLionel Sambuc hash = hash * 33 + c; /* "perl": k=33, r=r+r/32 */
98f6aac1c3SLionel Sambuc return (hash + (hash >> 5));
99f6aac1c3SLionel Sambuc }
100f6aac1c3SLionel Sambuc #endif /* __HAVE_HASH32_STR */
101f6aac1c3SLionel Sambuc
102f6aac1c3SLionel Sambuc __BEGIN_DECLS
103f6aac1c3SLionel Sambuc uint32_t murmurhash2(const void *, size_t, uint32_t);
104f6aac1c3SLionel Sambuc __END_DECLS
105f6aac1c3SLionel Sambuc
106f6aac1c3SLionel Sambuc #endif /* !_SYS_HASH_H_ */
107