Line | Count | Source |
1 | | /* hash.c - The gdbm hash function. */ |
2 | | |
3 | | /* This file is part of GDBM, the GNU data base manager. |
4 | | Copyright (C) 1990-2025 Free Software Foundation, Inc. |
5 | | |
6 | | GDBM is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 3, or (at your option) |
9 | | any later version. |
10 | | |
11 | | GDBM is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with GDBM. If not, see <http://www.gnu.org/licenses/>. */ |
18 | | |
19 | | /* Include system configuration before all else. */ |
20 | | #include "autoconf.h" |
21 | | |
22 | | #include "gdbmdefs.h" |
23 | | |
24 | | /* This hash function computes a GDBM_HASH_BITS-bit value. The value is used |
25 | | to index the hash directory using the top n bits. It is also used in a |
26 | | hash bucket to find the home position of the element by taking the value |
27 | | modulo the bucket hash table size. */ |
28 | | |
29 | | int |
30 | | _gdbm_hash (datum key) |
31 | 3.42M | { |
32 | 3.42M | unsigned int value; /* Used to compute the hash value. */ |
33 | 3.42M | int index; /* Used to cycle through random values. */ |
34 | | |
35 | | /* Set the initial value from key. */ |
36 | 3.42M | value = 0x238F13AFu * key.dsize; |
37 | 31.8G | for (index = 0; index < key.dsize; index++) |
38 | 31.8G | value = (value + (((unsigned)key.dptr[index]) << ((unsigned) index * 5 % 24))) & 0x7FFFFFFF; |
39 | | |
40 | 3.42M | value = (1103515243u * value + 12345) & 0x7FFFFFFF; |
41 | | |
42 | | /* Return the value. */ |
43 | 3.42M | return((int) value); |
44 | 3.42M | } |
45 | | |
46 | | int |
47 | | _gdbm_bucket_dir (GDBM_FILE dbf, int hash) |
48 | 3.43M | { |
49 | 3.43M | return hash >> (GDBM_HASH_BITS - dbf->header->dir_bits); |
50 | 3.43M | } |
51 | | |
52 | | void |
53 | | _gdbm_hash_key (GDBM_FILE dbf, datum key, int *hash, int *bucket, int *offset) |
54 | 3.42M | { |
55 | 3.42M | int hashval = _gdbm_hash (key); |
56 | 3.42M | *hash = hashval; |
57 | 3.42M | *bucket = _gdbm_bucket_dir (dbf, hashval); |
58 | 3.42M | *offset = hashval % dbf->header->bucket_elems; |
59 | 3.42M | } |