1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/lib/dbm/src/h_func.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,207 @@ 1.4 +/*- 1.5 + * Copyright (c) 1990, 1993 1.6 + * The Regents of the University of California. All rights reserved. 1.7 + * 1.8 + * This code is derived from software contributed to Berkeley by 1.9 + * Margo Seltzer. 1.10 + * 1.11 + * Redistribution and use in source and binary forms, with or without 1.12 + * modification, are permitted provided that the following conditions 1.13 + * are met: 1.14 + * 1. Redistributions of source code must retain the above copyright 1.15 + * notice, this list of conditions and the following disclaimer. 1.16 + * 2. Redistributions in binary form must reproduce the above copyright 1.17 + * notice, this list of conditions and the following disclaimer in the 1.18 + * documentation and/or other materials provided with the distribution. 1.19 + * 3. ***REMOVED*** - see 1.20 + * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change 1.21 + * 4. Neither the name of the University nor the names of its contributors 1.22 + * may be used to endorse or promote products derived from this software 1.23 + * without specific prior written permission. 1.24 + * 1.25 + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1.26 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.27 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.28 + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 1.29 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1.30 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 1.31 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 1.32 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 1.33 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 1.34 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 1.35 + * SUCH DAMAGE. 1.36 + */ 1.37 + 1.38 +#if defined(LIBC_SCCS) && !defined(lint) 1.39 +static char sccsid[] = "@(#)hash_func.c 8.2 (Berkeley) 2/21/94"; 1.40 +#endif /* LIBC_SCCS and not lint */ 1.41 + 1.42 +#ifndef macintosh 1.43 +#include <sys/types.h> 1.44 +#endif 1.45 +#include "mcom_db.h" 1.46 +#include "hash.h" 1.47 +#include "page.h" 1.48 +/* #include "extern.h" */ 1.49 + 1.50 +#if 0 1.51 +static uint32 hash1 __P((const void *, size_t)); 1.52 +static uint32 hash2 __P((const void *, size_t)); 1.53 +static uint32 hash3 __P((const void *, size_t)); 1.54 +#endif 1.55 +static uint32 hash4 __P((const void *, size_t)); 1.56 + 1.57 +/* Global default hash function */ 1.58 +uint32 (*__default_hash) __P((const void *, size_t)) = hash4; 1.59 + 1.60 +/* 1.61 + * HASH FUNCTIONS 1.62 + * 1.63 + * Assume that we've already split the bucket to which this key hashes, 1.64 + * calculate that bucket, and check that in fact we did already split it. 1.65 + * 1.66 + * This came from ejb's hsearch. 1.67 + */ 1.68 + 1.69 +#define PRIME1 37 1.70 +#define PRIME2 1048583 1.71 + 1.72 +#if 0 1.73 +static uint32 1.74 +hash1(const void *keyarg, register size_t len) 1.75 +{ 1.76 + register const uint8 *key; 1.77 + register uint32 h; 1.78 + 1.79 + /* Convert string to integer */ 1.80 + for (key = (const uint8 *)keyarg, h = 0; len--;) 1.81 + h = h * PRIME1 ^ (*key++ - ' '); 1.82 + h %= PRIME2; 1.83 + return (h); 1.84 +} 1.85 + 1.86 +/* 1.87 + * Phong's linear congruential hash 1.88 + */ 1.89 +#define dcharhash(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c)) 1.90 + 1.91 +static uint32 1.92 +hash2(const void *keyarg, size_t len) 1.93 +{ 1.94 + register const uint8 *e, *key; 1.95 + register uint32 h; 1.96 + register uint8 c; 1.97 + 1.98 + key = (const uint8 *)keyarg; 1.99 + e = key + len; 1.100 + for (h = 0; key != e;) { 1.101 + c = *key++; 1.102 + if (!c && key > e) 1.103 + break; 1.104 + dcharhash(h, c); 1.105 + } 1.106 + return (h); 1.107 +} 1.108 + 1.109 +/* 1.110 + * This is INCREDIBLY ugly, but fast. We break the string up into 8 byte 1.111 + * units. On the first time through the loop we get the "leftover bytes" 1.112 + * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle 1.113 + * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If 1.114 + * this routine is heavily used enough, it's worth the ugly coding. 1.115 + * 1.116 + * OZ's original sdbm hash 1.117 + */ 1.118 +static uint32 1.119 +hash3(const void *keyarg, register size_t len) 1.120 +{ 1.121 + register const uint8 *key; 1.122 + register size_t loop; 1.123 + register uint32 h; 1.124 + 1.125 +#define HASHC h = *key++ + 65599 * h 1.126 + 1.127 + h = 0; 1.128 + key = (const uint8 *)keyarg; 1.129 + if (len > 0) { 1.130 + loop = (len + 8 - 1) >> 3; 1.131 + 1.132 + switch (len & (8 - 1)) { 1.133 + case 0: 1.134 + do { 1.135 + HASHC; 1.136 + /* FALLTHROUGH */ 1.137 + case 7: 1.138 + HASHC; 1.139 + /* FALLTHROUGH */ 1.140 + case 6: 1.141 + HASHC; 1.142 + /* FALLTHROUGH */ 1.143 + case 5: 1.144 + HASHC; 1.145 + /* FALLTHROUGH */ 1.146 + case 4: 1.147 + HASHC; 1.148 + /* FALLTHROUGH */ 1.149 + case 3: 1.150 + HASHC; 1.151 + /* FALLTHROUGH */ 1.152 + case 2: 1.153 + HASHC; 1.154 + /* FALLTHROUGH */ 1.155 + case 1: 1.156 + HASHC; 1.157 + } while (--loop); 1.158 + } 1.159 + } 1.160 + return (h); 1.161 +} 1.162 +#endif /* 0 */ 1.163 + 1.164 +/* Hash function from Chris Torek. */ 1.165 +static uint32 1.166 +hash4(const void *keyarg, register size_t len) 1.167 +{ 1.168 + register const uint8 *key; 1.169 + register size_t loop; 1.170 + register uint32 h; 1.171 + 1.172 +#define HASH4a h = (h << 5) - h + *key++; 1.173 +#define HASH4b h = (h << 5) + h + *key++; 1.174 +#define HASH4 HASH4b 1.175 + 1.176 + h = 0; 1.177 + key = (const uint8 *)keyarg; 1.178 + if (len > 0) { 1.179 + loop = (len + 8 - 1) >> 3; 1.180 + 1.181 + switch (len & (8 - 1)) { 1.182 + case 0: 1.183 + do { 1.184 + HASH4; 1.185 + /* FALLTHROUGH */ 1.186 + case 7: 1.187 + HASH4; 1.188 + /* FALLTHROUGH */ 1.189 + case 6: 1.190 + HASH4; 1.191 + /* FALLTHROUGH */ 1.192 + case 5: 1.193 + HASH4; 1.194 + /* FALLTHROUGH */ 1.195 + case 4: 1.196 + HASH4; 1.197 + /* FALLTHROUGH */ 1.198 + case 3: 1.199 + HASH4; 1.200 + /* FALLTHROUGH */ 1.201 + case 2: 1.202 + HASH4; 1.203 + /* FALLTHROUGH */ 1.204 + case 1: 1.205 + HASH4; 1.206 + } while (--loop); 1.207 + } 1.208 + } 1.209 + return (h); 1.210 +}