security/nss/lib/dbm/src/h_func.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*-
michael@0 2 * Copyright (c) 1990, 1993
michael@0 3 * The Regents of the University of California. All rights reserved.
michael@0 4 *
michael@0 5 * This code is derived from software contributed to Berkeley by
michael@0 6 * Margo Seltzer.
michael@0 7 *
michael@0 8 * Redistribution and use in source and binary forms, with or without
michael@0 9 * modification, are permitted provided that the following conditions
michael@0 10 * are met:
michael@0 11 * 1. Redistributions of source code must retain the above copyright
michael@0 12 * notice, this list of conditions and the following disclaimer.
michael@0 13 * 2. Redistributions in binary form must reproduce the above copyright
michael@0 14 * notice, this list of conditions and the following disclaimer in the
michael@0 15 * documentation and/or other materials provided with the distribution.
michael@0 16 * 3. ***REMOVED*** - see
michael@0 17 * ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
michael@0 18 * 4. Neither the name of the University nor the names of its contributors
michael@0 19 * may be used to endorse or promote products derived from this software
michael@0 20 * without specific prior written permission.
michael@0 21 *
michael@0 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
michael@0 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
michael@0 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
michael@0 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
michael@0 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
michael@0 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
michael@0 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
michael@0 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
michael@0 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
michael@0 32 * SUCH DAMAGE.
michael@0 33 */
michael@0 34
michael@0 35 #if defined(LIBC_SCCS) && !defined(lint)
michael@0 36 static char sccsid[] = "@(#)hash_func.c 8.2 (Berkeley) 2/21/94";
michael@0 37 #endif /* LIBC_SCCS and not lint */
michael@0 38
michael@0 39 #ifndef macintosh
michael@0 40 #include <sys/types.h>
michael@0 41 #endif
michael@0 42 #include "mcom_db.h"
michael@0 43 #include "hash.h"
michael@0 44 #include "page.h"
michael@0 45 /* #include "extern.h" */
michael@0 46
michael@0 47 #if 0
michael@0 48 static uint32 hash1 __P((const void *, size_t));
michael@0 49 static uint32 hash2 __P((const void *, size_t));
michael@0 50 static uint32 hash3 __P((const void *, size_t));
michael@0 51 #endif
michael@0 52 static uint32 hash4 __P((const void *, size_t));
michael@0 53
michael@0 54 /* Global default hash function */
michael@0 55 uint32 (*__default_hash) __P((const void *, size_t)) = hash4;
michael@0 56
michael@0 57 /*
michael@0 58 * HASH FUNCTIONS
michael@0 59 *
michael@0 60 * Assume that we've already split the bucket to which this key hashes,
michael@0 61 * calculate that bucket, and check that in fact we did already split it.
michael@0 62 *
michael@0 63 * This came from ejb's hsearch.
michael@0 64 */
michael@0 65
michael@0 66 #define PRIME1 37
michael@0 67 #define PRIME2 1048583
michael@0 68
michael@0 69 #if 0
michael@0 70 static uint32
michael@0 71 hash1(const void *keyarg, register size_t len)
michael@0 72 {
michael@0 73 register const uint8 *key;
michael@0 74 register uint32 h;
michael@0 75
michael@0 76 /* Convert string to integer */
michael@0 77 for (key = (const uint8 *)keyarg, h = 0; len--;)
michael@0 78 h = h * PRIME1 ^ (*key++ - ' ');
michael@0 79 h %= PRIME2;
michael@0 80 return (h);
michael@0 81 }
michael@0 82
michael@0 83 /*
michael@0 84 * Phong's linear congruential hash
michael@0 85 */
michael@0 86 #define dcharhash(h, c) ((h) = 0x63c63cd9*(h) + 0x9c39c33d + (c))
michael@0 87
michael@0 88 static uint32
michael@0 89 hash2(const void *keyarg, size_t len)
michael@0 90 {
michael@0 91 register const uint8 *e, *key;
michael@0 92 register uint32 h;
michael@0 93 register uint8 c;
michael@0 94
michael@0 95 key = (const uint8 *)keyarg;
michael@0 96 e = key + len;
michael@0 97 for (h = 0; key != e;) {
michael@0 98 c = *key++;
michael@0 99 if (!c && key > e)
michael@0 100 break;
michael@0 101 dcharhash(h, c);
michael@0 102 }
michael@0 103 return (h);
michael@0 104 }
michael@0 105
michael@0 106 /*
michael@0 107 * This is INCREDIBLY ugly, but fast. We break the string up into 8 byte
michael@0 108 * units. On the first time through the loop we get the "leftover bytes"
michael@0 109 * (strlen % 8). On every other iteration, we perform 8 HASHC's so we handle
michael@0 110 * all 8 bytes. Essentially, this saves us 7 cmp & branch instructions. If
michael@0 111 * this routine is heavily used enough, it's worth the ugly coding.
michael@0 112 *
michael@0 113 * OZ's original sdbm hash
michael@0 114 */
michael@0 115 static uint32
michael@0 116 hash3(const void *keyarg, register size_t len)
michael@0 117 {
michael@0 118 register const uint8 *key;
michael@0 119 register size_t loop;
michael@0 120 register uint32 h;
michael@0 121
michael@0 122 #define HASHC h = *key++ + 65599 * h
michael@0 123
michael@0 124 h = 0;
michael@0 125 key = (const uint8 *)keyarg;
michael@0 126 if (len > 0) {
michael@0 127 loop = (len + 8 - 1) >> 3;
michael@0 128
michael@0 129 switch (len & (8 - 1)) {
michael@0 130 case 0:
michael@0 131 do {
michael@0 132 HASHC;
michael@0 133 /* FALLTHROUGH */
michael@0 134 case 7:
michael@0 135 HASHC;
michael@0 136 /* FALLTHROUGH */
michael@0 137 case 6:
michael@0 138 HASHC;
michael@0 139 /* FALLTHROUGH */
michael@0 140 case 5:
michael@0 141 HASHC;
michael@0 142 /* FALLTHROUGH */
michael@0 143 case 4:
michael@0 144 HASHC;
michael@0 145 /* FALLTHROUGH */
michael@0 146 case 3:
michael@0 147 HASHC;
michael@0 148 /* FALLTHROUGH */
michael@0 149 case 2:
michael@0 150 HASHC;
michael@0 151 /* FALLTHROUGH */
michael@0 152 case 1:
michael@0 153 HASHC;
michael@0 154 } while (--loop);
michael@0 155 }
michael@0 156 }
michael@0 157 return (h);
michael@0 158 }
michael@0 159 #endif /* 0 */
michael@0 160
michael@0 161 /* Hash function from Chris Torek. */
michael@0 162 static uint32
michael@0 163 hash4(const void *keyarg, register size_t len)
michael@0 164 {
michael@0 165 register const uint8 *key;
michael@0 166 register size_t loop;
michael@0 167 register uint32 h;
michael@0 168
michael@0 169 #define HASH4a h = (h << 5) - h + *key++;
michael@0 170 #define HASH4b h = (h << 5) + h + *key++;
michael@0 171 #define HASH4 HASH4b
michael@0 172
michael@0 173 h = 0;
michael@0 174 key = (const uint8 *)keyarg;
michael@0 175 if (len > 0) {
michael@0 176 loop = (len + 8 - 1) >> 3;
michael@0 177
michael@0 178 switch (len & (8 - 1)) {
michael@0 179 case 0:
michael@0 180 do {
michael@0 181 HASH4;
michael@0 182 /* FALLTHROUGH */
michael@0 183 case 7:
michael@0 184 HASH4;
michael@0 185 /* FALLTHROUGH */
michael@0 186 case 6:
michael@0 187 HASH4;
michael@0 188 /* FALLTHROUGH */
michael@0 189 case 5:
michael@0 190 HASH4;
michael@0 191 /* FALLTHROUGH */
michael@0 192 case 4:
michael@0 193 HASH4;
michael@0 194 /* FALLTHROUGH */
michael@0 195 case 3:
michael@0 196 HASH4;
michael@0 197 /* FALLTHROUGH */
michael@0 198 case 2:
michael@0 199 HASH4;
michael@0 200 /* FALLTHROUGH */
michael@0 201 case 1:
michael@0 202 HASH4;
michael@0 203 } while (--loop);
michael@0 204 }
michael@0 205 }
michael@0 206 return (h);
michael@0 207 }

mercurial