]> CyberLeo.Net >> Repos - FreeBSD/releng/10.3.git/blob - lib/libc/db/hash/hash.c
Fix multiple OpenSSL vulnerabilitites. [SA-16:17]
[FreeBSD/releng/10.3.git] / lib / libc / db / hash / hash.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Margo Seltzer.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32
33 #if defined(LIBC_SCCS) && !defined(lint)
34 static char sccsid[] = "@(#)hash.c      8.9 (Berkeley) 6/16/94";
35 #endif /* LIBC_SCCS and not lint */
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "namespace.h"
40 #include <sys/param.h>
41 #include <sys/stat.h>
42
43 #include <errno.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #ifdef DEBUG
50 #include <assert.h>
51 #endif
52 #include "un-namespace.h"
53
54 #include <db.h>
55 #include "hash.h"
56 #include "page.h"
57 #include "extern.h"
58
59 static int   alloc_segs(HTAB *, int);
60 static int   flush_meta(HTAB *);
61 static int   hash_access(HTAB *, ACTION, DBT *, DBT *);
62 static int   hash_close(DB *);
63 static int   hash_delete(const DB *, const DBT *, u_int32_t);
64 static int   hash_fd(const DB *);
65 static int   hash_get(const DB *, const DBT *, DBT *, u_int32_t);
66 static int   hash_put(const DB *, DBT *, const DBT *, u_int32_t);
67 static void *hash_realloc(SEGMENT **, int, int);
68 static int   hash_seq(const DB *, DBT *, DBT *, u_int32_t);
69 static int   hash_sync(const DB *, u_int32_t);
70 static int   hdestroy(HTAB *);
71 static HTAB *init_hash(HTAB *, const char *, const HASHINFO *);
72 static int   init_htab(HTAB *, int);
73 #if BYTE_ORDER == LITTLE_ENDIAN
74 static void  swap_header(HTAB *);
75 static void  swap_header_copy(HASHHDR *, HASHHDR *);
76 #endif
77
78 /* Fast arithmetic, relying on powers of 2, */
79 #define MOD(x, y)               ((x) & ((y) - 1))
80
81 #define RETURN_ERROR(ERR, LOC)  { save_errno = ERR; goto LOC; }
82
83 /* Return values */
84 #define SUCCESS  (0)
85 #define ERROR   (-1)
86 #define ABNORMAL (1)
87
88 #ifdef HASH_STATISTICS
89 int hash_accesses, hash_collisions, hash_expansions, hash_overflows;
90 #endif
91
92 /************************** INTERFACE ROUTINES ***************************/
93 /* OPEN/CLOSE */
94
95 /* ARGSUSED */
96 DB *
97 __hash_open(const char *file, int flags, int mode,
98     const HASHINFO *info,       /* Special directives for create */
99     int dflags)
100 {
101         HTAB *hashp;
102         struct stat statbuf;
103         DB *dbp;
104         int bpages, hdrsize, new_table, nsegs, save_errno;
105
106         if ((flags & O_ACCMODE) == O_WRONLY) {
107                 errno = EINVAL;
108                 return (NULL);
109         }
110
111         if (!(hashp = (HTAB *)calloc(1, sizeof(HTAB))))
112                 return (NULL);
113         hashp->fp = -1;
114
115         /*
116          * Even if user wants write only, we need to be able to read
117          * the actual file, so we need to open it read/write. But, the
118          * field in the hashp structure needs to be accurate so that
119          * we can check accesses.
120          */
121         hashp->flags = flags;
122
123         if (file) {
124                 if ((hashp->fp = _open(file, flags | O_CLOEXEC, mode)) == -1)
125                         RETURN_ERROR(errno, error0);
126                 new_table = _fstat(hashp->fp, &statbuf) == 0 &&
127                     statbuf.st_size == 0 && (flags & O_ACCMODE) != O_RDONLY;
128         } else
129                 new_table = 1;
130
131         if (new_table) {
132                 if (!(hashp = init_hash(hashp, file, info)))
133                         RETURN_ERROR(errno, error1);
134         } else {
135                 /* Table already exists */
136                 if (info && info->hash)
137                         hashp->hash = info->hash;
138                 else
139                         hashp->hash = __default_hash;
140
141                 hdrsize = _read(hashp->fp, &hashp->hdr, sizeof(HASHHDR));
142 #if BYTE_ORDER == LITTLE_ENDIAN
143                 swap_header(hashp);
144 #endif
145                 if (hdrsize == -1)
146                         RETURN_ERROR(errno, error1);
147                 if (hdrsize != sizeof(HASHHDR))
148                         RETURN_ERROR(EFTYPE, error1);
149                 /* Verify file type, versions and hash function */
150                 if (hashp->MAGIC != HASHMAGIC)
151                         RETURN_ERROR(EFTYPE, error1);
152 #define OLDHASHVERSION  1
153                 if (hashp->VERSION != HASHVERSION &&
154                     hashp->VERSION != OLDHASHVERSION)
155                         RETURN_ERROR(EFTYPE, error1);
156                 if ((int32_t)hashp->hash(CHARKEY, sizeof(CHARKEY)) != hashp->H_CHARKEY)
157                         RETURN_ERROR(EFTYPE, error1);
158                 /*
159                  * Figure out how many segments we need.  Max_Bucket is the
160                  * maximum bucket number, so the number of buckets is
161                  * max_bucket + 1.
162                  */
163                 nsegs = (hashp->MAX_BUCKET + 1 + hashp->SGSIZE - 1) /
164                          hashp->SGSIZE;
165                 if (alloc_segs(hashp, nsegs))
166                         /*
167                          * If alloc_segs fails, table will have been destroyed
168                          * and errno will have been set.
169                          */
170                         return (NULL);
171                 /* Read in bitmaps */
172                 bpages = (hashp->SPARES[hashp->OVFL_POINT] +
173                     (hashp->BSIZE << BYTE_SHIFT) - 1) >>
174                     (hashp->BSHIFT + BYTE_SHIFT);
175
176                 hashp->nmaps = bpages;
177                 (void)memset(&hashp->mapp[0], 0, bpages * sizeof(u_int32_t *));
178         }
179
180         /* Initialize Buffer Manager */
181         if (info && info->cachesize)
182                 __buf_init(hashp, info->cachesize);
183         else
184                 __buf_init(hashp, DEF_BUFSIZE);
185
186         hashp->new_file = new_table;
187         hashp->save_file = file && (hashp->flags & O_RDWR);
188         hashp->cbucket = -1;
189         if (!(dbp = (DB *)malloc(sizeof(DB)))) {
190                 save_errno = errno;
191                 hdestroy(hashp);
192                 errno = save_errno;
193                 return (NULL);
194         }
195         dbp->internal = hashp;
196         dbp->close = hash_close;
197         dbp->del = hash_delete;
198         dbp->fd = hash_fd;
199         dbp->get = hash_get;
200         dbp->put = hash_put;
201         dbp->seq = hash_seq;
202         dbp->sync = hash_sync;
203         dbp->type = DB_HASH;
204
205 #ifdef DEBUG
206         (void)fprintf(stderr,
207 "%s\n%s%p\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%d\n%s%x\n%s%x\n%s%d\n%s%d\n",
208             "init_htab:",
209             "TABLE POINTER   ", hashp,
210             "BUCKET SIZE     ", hashp->BSIZE,
211             "BUCKET SHIFT    ", hashp->BSHIFT,
212             "DIRECTORY SIZE  ", hashp->DSIZE,
213             "SEGMENT SIZE    ", hashp->SGSIZE,
214             "SEGMENT SHIFT   ", hashp->SSHIFT,
215             "FILL FACTOR     ", hashp->FFACTOR,
216             "MAX BUCKET      ", hashp->MAX_BUCKET,
217             "OVFL POINT      ", hashp->OVFL_POINT,
218             "LAST FREED      ", hashp->LAST_FREED,
219             "HIGH MASK       ", hashp->HIGH_MASK,
220             "LOW  MASK       ", hashp->LOW_MASK,
221             "NSEGS           ", hashp->nsegs,
222             "NKEYS           ", hashp->NKEYS);
223 #endif
224 #ifdef HASH_STATISTICS
225         hash_overflows = hash_accesses = hash_collisions = hash_expansions = 0;
226 #endif
227         return (dbp);
228
229 error1:
230         if (hashp != NULL)
231                 (void)_close(hashp->fp);
232
233 error0:
234         free(hashp);
235         errno = save_errno;
236         return (NULL);
237 }
238
239 static int
240 hash_close(DB *dbp)
241 {
242         HTAB *hashp;
243         int retval;
244
245         if (!dbp)
246                 return (ERROR);
247
248         hashp = (HTAB *)dbp->internal;
249         retval = hdestroy(hashp);
250         free(dbp);
251         return (retval);
252 }
253
254 static int
255 hash_fd(const DB *dbp)
256 {
257         HTAB *hashp;
258
259         if (!dbp)
260                 return (ERROR);
261
262         hashp = (HTAB *)dbp->internal;
263         if (hashp->fp == -1) {
264                 errno = ENOENT;
265                 return (-1);
266         }
267         return (hashp->fp);
268 }
269
270 /************************** LOCAL CREATION ROUTINES **********************/
271 static HTAB *
272 init_hash(HTAB *hashp, const char *file, const HASHINFO *info)
273 {
274         struct stat statbuf;
275         int nelem;
276
277         nelem = 1;
278         hashp->NKEYS = 0;
279         hashp->LORDER = BYTE_ORDER;
280         hashp->BSIZE = DEF_BUCKET_SIZE;
281         hashp->BSHIFT = DEF_BUCKET_SHIFT;
282         hashp->SGSIZE = DEF_SEGSIZE;
283         hashp->SSHIFT = DEF_SEGSIZE_SHIFT;
284         hashp->DSIZE = DEF_DIRSIZE;
285         hashp->FFACTOR = DEF_FFACTOR;
286         hashp->hash = __default_hash;
287         memset(hashp->SPARES, 0, sizeof(hashp->SPARES));
288         memset(hashp->BITMAPS, 0, sizeof (hashp->BITMAPS));
289
290         /* Fix bucket size to be optimal for file system */
291         if (file != NULL) {
292                 if (stat(file, &statbuf))
293                         return (NULL);
294                 hashp->BSIZE = statbuf.st_blksize;
295                 if (hashp->BSIZE > MAX_BSIZE)
296                         hashp->BSIZE = MAX_BSIZE;
297                 hashp->BSHIFT = __log2(hashp->BSIZE);
298         }
299
300         if (info) {
301                 if (info->bsize) {
302                         /* Round pagesize up to power of 2 */
303                         hashp->BSHIFT = __log2(info->bsize);
304                         hashp->BSIZE = 1 << hashp->BSHIFT;
305                         if (hashp->BSIZE > MAX_BSIZE) {
306                                 errno = EINVAL;
307                                 return (NULL);
308                         }
309                 }
310                 if (info->ffactor)
311                         hashp->FFACTOR = info->ffactor;
312                 if (info->hash)
313                         hashp->hash = info->hash;
314                 if (info->nelem)
315                         nelem = info->nelem;
316                 if (info->lorder) {
317                         if (info->lorder != BIG_ENDIAN &&
318                             info->lorder != LITTLE_ENDIAN) {
319                                 errno = EINVAL;
320                                 return (NULL);
321                         }
322                         hashp->LORDER = info->lorder;
323                 }
324         }
325         /* init_htab should destroy the table and set errno if it fails */
326         if (init_htab(hashp, nelem))
327                 return (NULL);
328         else
329                 return (hashp);
330 }
331 /*
332  * This calls alloc_segs which may run out of memory.  Alloc_segs will destroy
333  * the table and set errno, so we just pass the error information along.
334  *
335  * Returns 0 on No Error
336  */
337 static int
338 init_htab(HTAB *hashp, int nelem)
339 {
340         int nbuckets, nsegs, l2;
341
342         /*
343          * Divide number of elements by the fill factor and determine a
344          * desired number of buckets.  Allocate space for the next greater
345          * power of two number of buckets.
346          */
347         nelem = (nelem - 1) / hashp->FFACTOR + 1;
348
349         l2 = __log2(MAX(nelem, 2));
350         nbuckets = 1 << l2;
351
352         hashp->SPARES[l2] = l2 + 1;
353         hashp->SPARES[l2 + 1] = l2 + 1;
354         hashp->OVFL_POINT = l2;
355         hashp->LAST_FREED = 2;
356
357         /* First bitmap page is at: splitpoint l2 page offset 1 */
358         if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0))
359                 return (-1);
360
361         hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1;
362         hashp->HIGH_MASK = (nbuckets << 1) - 1;
363         hashp->HDRPAGES = ((MAX(sizeof(HASHHDR), MINHDRSIZE) - 1) >>
364             hashp->BSHIFT) + 1;
365
366         nsegs = (nbuckets - 1) / hashp->SGSIZE + 1;
367         nsegs = 1 << __log2(nsegs);
368
369         if (nsegs > hashp->DSIZE)
370                 hashp->DSIZE = nsegs;
371         return (alloc_segs(hashp, nsegs));
372 }
373
374 /********************** DESTROY/CLOSE ROUTINES ************************/
375
376 /*
377  * Flushes any changes to the file if necessary and destroys the hashp
378  * structure, freeing all allocated space.
379  */
380 static int
381 hdestroy(HTAB *hashp)
382 {
383         int i, save_errno;
384
385         save_errno = 0;
386
387 #ifdef HASH_STATISTICS
388         (void)fprintf(stderr, "hdestroy: accesses %ld collisions %ld\n",
389             hash_accesses, hash_collisions);
390         (void)fprintf(stderr, "hdestroy: expansions %ld\n",
391             hash_expansions);
392         (void)fprintf(stderr, "hdestroy: overflows %ld\n",
393             hash_overflows);
394         (void)fprintf(stderr, "keys %ld maxp %d segmentcount %d\n",
395             hashp->NKEYS, hashp->MAX_BUCKET, hashp->nsegs);
396
397         for (i = 0; i < NCACHED; i++)
398                 (void)fprintf(stderr,
399                     "spares[%d] = %d\n", i, hashp->SPARES[i]);
400 #endif
401         /*
402          * Call on buffer manager to free buffers, and if required,
403          * write them to disk.
404          */
405         if (__buf_free(hashp, 1, hashp->save_file))
406                 save_errno = errno;
407         if (hashp->dir) {
408                 free(*hashp->dir);      /* Free initial segments */
409                 /* Free extra segments */
410                 while (hashp->exsegs--)
411                         free(hashp->dir[--hashp->nsegs]);
412                 free(hashp->dir);
413         }
414         if (flush_meta(hashp) && !save_errno)
415                 save_errno = errno;
416         /* Free Bigmaps */
417         for (i = 0; i < hashp->nmaps; i++)
418                 if (hashp->mapp[i])
419                         free(hashp->mapp[i]);
420         if (hashp->tmp_key)
421                 free(hashp->tmp_key);
422         if (hashp->tmp_buf)
423                 free(hashp->tmp_buf);
424
425         if (hashp->fp != -1) {
426                 if (hashp->save_file)
427                         (void)_fsync(hashp->fp);
428                 (void)_close(hashp->fp);
429         }
430
431         free(hashp);
432
433         if (save_errno) {
434                 errno = save_errno;
435                 return (ERROR);
436         }
437         return (SUCCESS);
438 }
439 /*
440  * Write modified pages to disk
441  *
442  * Returns:
443  *       0 == OK
444  *      -1 ERROR
445  */
446 static int
447 hash_sync(const DB *dbp, u_int32_t flags)
448 {
449         HTAB *hashp;
450
451         if (flags != 0) {
452                 errno = EINVAL;
453                 return (ERROR);
454         }
455
456         if (!dbp)
457                 return (ERROR);
458
459         hashp = (HTAB *)dbp->internal;
460         if (!hashp->save_file)
461                 return (0);
462         if (__buf_free(hashp, 0, 1) || flush_meta(hashp))
463                 return (ERROR);
464         if (hashp->fp != -1 && _fsync(hashp->fp) != 0)
465                 return (ERROR);
466         hashp->new_file = 0;
467         return (0);
468 }
469
470 /*
471  * Returns:
472  *       0 == OK
473  *      -1 indicates that errno should be set
474  */
475 static int
476 flush_meta(HTAB *hashp)
477 {
478         HASHHDR *whdrp;
479 #if BYTE_ORDER == LITTLE_ENDIAN
480         HASHHDR whdr;
481 #endif
482         int fp, i, wsize;
483
484         if (!hashp->save_file)
485                 return (0);
486         hashp->MAGIC = HASHMAGIC;
487         hashp->VERSION = HASHVERSION;
488         hashp->H_CHARKEY = hashp->hash(CHARKEY, sizeof(CHARKEY));
489
490         fp = hashp->fp;
491         whdrp = &hashp->hdr;
492 #if BYTE_ORDER == LITTLE_ENDIAN
493         whdrp = &whdr;
494         swap_header_copy(&hashp->hdr, whdrp);
495 #endif
496         if ((wsize = pwrite(fp, whdrp, sizeof(HASHHDR), (off_t)0)) == -1)
497                 return (-1);
498         else
499                 if (wsize != sizeof(HASHHDR)) {
500                         errno = EFTYPE;
501                         hashp->error = errno;
502                         return (-1);
503                 }
504         for (i = 0; i < NCACHED; i++)
505                 if (hashp->mapp[i])
506                         if (__put_page(hashp, (char *)hashp->mapp[i],
507                                 hashp->BITMAPS[i], 0, 1))
508                                 return (-1);
509         return (0);
510 }
511
512 /*******************************SEARCH ROUTINES *****************************/
513 /*
514  * All the access routines return
515  *
516  * Returns:
517  *       0 on SUCCESS
518  *       1 to indicate an external ERROR (i.e. key not found, etc)
519  *      -1 to indicate an internal ERROR (i.e. out of memory, etc)
520  */
521 static int
522 hash_get(const DB *dbp, const DBT *key, DBT *data, u_int32_t flag)
523 {
524         HTAB *hashp;
525
526         hashp = (HTAB *)dbp->internal;
527         if (flag) {
528                 hashp->error = errno = EINVAL;
529                 return (ERROR);
530         }
531         return (hash_access(hashp, HASH_GET, (DBT *)key, data));
532 }
533
534 static int
535 hash_put(const DB *dbp, DBT *key, const DBT *data, u_int32_t flag)
536 {
537         HTAB *hashp;
538
539         hashp = (HTAB *)dbp->internal;
540         if (flag && flag != R_NOOVERWRITE) {
541                 hashp->error = errno = EINVAL;
542                 return (ERROR);
543         }
544         if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
545                 hashp->error = errno = EPERM;
546                 return (ERROR);
547         }
548         return (hash_access(hashp, flag == R_NOOVERWRITE ?
549             HASH_PUTNEW : HASH_PUT, (DBT *)key, (DBT *)data));
550 }
551
552 static int
553 hash_delete(const DB *dbp, const DBT *key,
554     u_int32_t flag)             /* Ignored */
555 {
556         HTAB *hashp;
557
558         hashp = (HTAB *)dbp->internal;
559         if (flag && flag != R_CURSOR) {
560                 hashp->error = errno = EINVAL;
561                 return (ERROR);
562         }
563         if ((hashp->flags & O_ACCMODE) == O_RDONLY) {
564                 hashp->error = errno = EPERM;
565                 return (ERROR);
566         }
567         return (hash_access(hashp, HASH_DELETE, (DBT *)key, NULL));
568 }
569
570 /*
571  * Assume that hashp has been set in wrapper routine.
572  */
573 static int
574 hash_access(HTAB *hashp, ACTION action, DBT *key, DBT *val)
575 {
576         BUFHEAD *rbufp;
577         BUFHEAD *bufp, *save_bufp;
578         u_int16_t *bp;
579         int n, ndx, off, size;
580         char *kp;
581         u_int16_t pageno;
582
583 #ifdef HASH_STATISTICS
584         hash_accesses++;
585 #endif
586
587         off = hashp->BSIZE;
588         size = key->size;
589         kp = (char *)key->data;
590         rbufp = __get_buf(hashp, __call_hash(hashp, kp, size), NULL, 0);
591         if (!rbufp)
592                 return (ERROR);
593         save_bufp = rbufp;
594
595         /* Pin the bucket chain */
596         rbufp->flags |= BUF_PIN;
597         for (bp = (u_int16_t *)rbufp->page, n = *bp++, ndx = 1; ndx < n;)
598                 if (bp[1] >= REAL_KEY) {
599                         /* Real key/data pair */
600                         if (size == off - *bp &&
601                             memcmp(kp, rbufp->page + *bp, size) == 0)
602                                 goto found;
603                         off = bp[1];
604 #ifdef HASH_STATISTICS
605                         hash_collisions++;
606 #endif
607                         bp += 2;
608                         ndx += 2;
609                 } else if (bp[1] == OVFLPAGE) {
610                         rbufp = __get_buf(hashp, *bp, rbufp, 0);
611                         if (!rbufp) {
612                                 save_bufp->flags &= ~BUF_PIN;
613                                 return (ERROR);
614                         }
615                         /* FOR LOOP INIT */
616                         bp = (u_int16_t *)rbufp->page;
617                         n = *bp++;
618                         ndx = 1;
619                         off = hashp->BSIZE;
620                 } else if (bp[1] < REAL_KEY) {
621                         if ((ndx =
622                             __find_bigpair(hashp, rbufp, ndx, kp, size)) > 0)
623                                 goto found;
624                         if (ndx == -2) {
625                                 bufp = rbufp;
626                                 if (!(pageno =
627                                     __find_last_page(hashp, &bufp))) {
628                                         ndx = 0;
629                                         rbufp = bufp;
630                                         break;  /* FOR */
631                                 }
632                                 rbufp = __get_buf(hashp, pageno, bufp, 0);
633                                 if (!rbufp) {
634                                         save_bufp->flags &= ~BUF_PIN;
635                                         return (ERROR);
636                                 }
637                                 /* FOR LOOP INIT */
638                                 bp = (u_int16_t *)rbufp->page;
639                                 n = *bp++;
640                                 ndx = 1;
641                                 off = hashp->BSIZE;
642                         } else {
643                                 save_bufp->flags &= ~BUF_PIN;
644                                 return (ERROR);
645                         }
646                 }
647
648         /* Not found */
649         switch (action) {
650         case HASH_PUT:
651         case HASH_PUTNEW:
652                 if (__addel(hashp, rbufp, key, val)) {
653                         save_bufp->flags &= ~BUF_PIN;
654                         return (ERROR);
655                 } else {
656                         save_bufp->flags &= ~BUF_PIN;
657                         return (SUCCESS);
658                 }
659         case HASH_GET:
660         case HASH_DELETE:
661         default:
662                 save_bufp->flags &= ~BUF_PIN;
663                 return (ABNORMAL);
664         }
665
666 found:
667         switch (action) {
668         case HASH_PUTNEW:
669                 save_bufp->flags &= ~BUF_PIN;
670                 return (ABNORMAL);
671         case HASH_GET:
672                 bp = (u_int16_t *)rbufp->page;
673                 if (bp[ndx + 1] < REAL_KEY) {
674                         if (__big_return(hashp, rbufp, ndx, val, 0))
675                                 return (ERROR);
676                 } else {
677                         val->data = (u_char *)rbufp->page + (int)bp[ndx + 1];
678                         val->size = bp[ndx] - bp[ndx + 1];
679                 }
680                 break;
681         case HASH_PUT:
682                 if ((__delpair(hashp, rbufp, ndx)) ||
683                     (__addel(hashp, rbufp, key, val))) {
684                         save_bufp->flags &= ~BUF_PIN;
685                         return (ERROR);
686                 }
687                 break;
688         case HASH_DELETE:
689                 if (__delpair(hashp, rbufp, ndx))
690                         return (ERROR);
691                 break;
692         default:
693                 abort();
694         }
695         save_bufp->flags &= ~BUF_PIN;
696         return (SUCCESS);
697 }
698
699 static int
700 hash_seq(const DB *dbp, DBT *key, DBT *data, u_int32_t flag)
701 {
702         u_int32_t bucket;
703         BUFHEAD *bufp;
704         HTAB *hashp;
705         u_int16_t *bp, ndx;
706
707         hashp = (HTAB *)dbp->internal;
708         if (flag && flag != R_FIRST && flag != R_NEXT) {
709                 hashp->error = errno = EINVAL;
710                 return (ERROR);
711         }
712 #ifdef HASH_STATISTICS
713         hash_accesses++;
714 #endif
715         if ((hashp->cbucket < 0) || (flag == R_FIRST)) {
716                 hashp->cbucket = 0;
717                 hashp->cndx = 1;
718                 hashp->cpage = NULL;
719         }
720 next_bucket:
721         for (bp = NULL; !bp || !bp[0]; ) {
722                 if (!(bufp = hashp->cpage)) {
723                         for (bucket = hashp->cbucket;
724                             bucket <= hashp->MAX_BUCKET;
725                             bucket++, hashp->cndx = 1) {
726                                 bufp = __get_buf(hashp, bucket, NULL, 0);
727                                 if (!bufp)
728                                         return (ERROR);
729                                 hashp->cpage = bufp;
730                                 bp = (u_int16_t *)bufp->page;
731                                 if (bp[0])
732                                         break;
733                         }
734                         hashp->cbucket = bucket;
735                         if ((u_int32_t)hashp->cbucket > hashp->MAX_BUCKET) {
736                                 hashp->cbucket = -1;
737                                 return (ABNORMAL);
738                         }
739                 } else {
740                         bp = (u_int16_t *)hashp->cpage->page;
741                         if (flag == R_NEXT || flag == 0) {
742                                 hashp->cndx += 2;
743                                 if (hashp->cndx > bp[0]) {
744                                         hashp->cpage = NULL;
745                                         hashp->cbucket++;
746                                         hashp->cndx = 1;
747                                         goto next_bucket;
748                                 }
749                         }
750                 }
751
752 #ifdef DEBUG
753                 assert(bp);
754                 assert(bufp);
755 #endif
756                 while (bp[hashp->cndx + 1] == OVFLPAGE) {
757                         bufp = hashp->cpage =
758                             __get_buf(hashp, bp[hashp->cndx], bufp, 0);
759                         if (!bufp)
760                                 return (ERROR);
761                         bp = (u_int16_t *)(bufp->page);
762                         hashp->cndx = 1;
763                 }
764                 if (!bp[0]) {
765                         hashp->cpage = NULL;
766                         ++hashp->cbucket;
767                 }
768         }
769         ndx = hashp->cndx;
770         if (bp[ndx + 1] < REAL_KEY) {
771                 if (__big_keydata(hashp, bufp, key, data, 1))
772                         return (ERROR);
773         } else {
774                 if (hashp->cpage == 0)
775                         return (ERROR);
776                 key->data = (u_char *)hashp->cpage->page + bp[ndx];
777                 key->size = (ndx > 1 ? bp[ndx - 1] : hashp->BSIZE) - bp[ndx];
778                 data->data = (u_char *)hashp->cpage->page + bp[ndx + 1];
779                 data->size = bp[ndx] - bp[ndx + 1];
780         }
781         return (SUCCESS);
782 }
783
784 /********************************* UTILITIES ************************/
785
786 /*
787  * Returns:
788  *       0 ==> OK
789  *      -1 ==> Error
790  */
791 int
792 __expand_table(HTAB *hashp)
793 {
794         u_int32_t old_bucket, new_bucket;
795         int dirsize, new_segnum, spare_ndx;
796
797 #ifdef HASH_STATISTICS
798         hash_expansions++;
799 #endif
800         new_bucket = ++hashp->MAX_BUCKET;
801         old_bucket = (hashp->MAX_BUCKET & hashp->LOW_MASK);
802
803         new_segnum = new_bucket >> hashp->SSHIFT;
804
805         /* Check if we need a new segment */
806         if (new_segnum >= hashp->nsegs) {
807                 /* Check if we need to expand directory */
808                 if (new_segnum >= hashp->DSIZE) {
809                         /* Reallocate directory */
810                         dirsize = hashp->DSIZE * sizeof(SEGMENT *);
811                         if (!hash_realloc(&hashp->dir, dirsize, dirsize << 1))
812                                 return (-1);
813                         hashp->DSIZE = dirsize << 1;
814                 }
815                 if ((hashp->dir[new_segnum] =
816                     (SEGMENT)calloc(hashp->SGSIZE, sizeof(SEGMENT))) == NULL)
817                         return (-1);
818                 hashp->exsegs++;
819                 hashp->nsegs++;
820         }
821         /*
822          * If the split point is increasing (MAX_BUCKET's log base 2
823          * * increases), we need to copy the current contents of the spare
824          * split bucket to the next bucket.
825          */
826         spare_ndx = __log2(hashp->MAX_BUCKET + 1);
827         if (spare_ndx > hashp->OVFL_POINT) {
828                 hashp->SPARES[spare_ndx] = hashp->SPARES[hashp->OVFL_POINT];
829                 hashp->OVFL_POINT = spare_ndx;
830         }
831
832         if (new_bucket > hashp->HIGH_MASK) {
833                 /* Starting a new doubling */
834                 hashp->LOW_MASK = hashp->HIGH_MASK;
835                 hashp->HIGH_MASK = new_bucket | hashp->LOW_MASK;
836         }
837         /* Relocate records to the new bucket */
838         return (__split_page(hashp, old_bucket, new_bucket));
839 }
840
841 /*
842  * If realloc guarantees that the pointer is not destroyed if the realloc
843  * fails, then this routine can go away.
844  */
845 static void *
846 hash_realloc(SEGMENT **p_ptr, int oldsize, int newsize)
847 {
848         void *p;
849
850         if ( (p = malloc(newsize)) ) {
851                 memmove(p, *p_ptr, oldsize);
852                 memset((char *)p + oldsize, 0, newsize - oldsize);
853                 free(*p_ptr);
854                 *p_ptr = p;
855         }
856         return (p);
857 }
858
859 u_int32_t
860 __call_hash(HTAB *hashp, char *k, int len)
861 {
862         unsigned int n, bucket;
863
864         n = hashp->hash(k, len);
865         bucket = n & hashp->HIGH_MASK;
866         if (bucket > hashp->MAX_BUCKET)
867                 bucket = bucket & hashp->LOW_MASK;
868         return (bucket);
869 }
870
871 /*
872  * Allocate segment table.  On error, destroy the table and set errno.
873  *
874  * Returns 0 on success
875  */
876 static int
877 alloc_segs(HTAB *hashp, int nsegs)
878 {
879         int i;
880         SEGMENT store;
881
882         int save_errno;
883
884         if ((hashp->dir =
885             (SEGMENT *)calloc(hashp->DSIZE, sizeof(SEGMENT *))) == NULL) {
886                 save_errno = errno;
887                 (void)hdestroy(hashp);
888                 errno = save_errno;
889                 return (-1);
890         }
891         hashp->nsegs = nsegs;
892         if (nsegs == 0)
893                 return (0);
894         /* Allocate segments */
895         if ((store = (SEGMENT)calloc(nsegs << hashp->SSHIFT,
896             sizeof(SEGMENT))) == NULL) {
897                 save_errno = errno;
898                 (void)hdestroy(hashp);
899                 errno = save_errno;
900                 return (-1);
901         }
902         for (i = 0; i < nsegs; i++)
903                 hashp->dir[i] = &store[i << hashp->SSHIFT];
904         return (0);
905 }
906
907 #if BYTE_ORDER == LITTLE_ENDIAN
908 /*
909  * Hashp->hdr needs to be byteswapped.
910  */
911 static void
912 swap_header_copy(HASHHDR *srcp, HASHHDR *destp)
913 {
914         int i;
915
916         P_32_COPY(srcp->magic, destp->magic);
917         P_32_COPY(srcp->version, destp->version);
918         P_32_COPY(srcp->lorder, destp->lorder);
919         P_32_COPY(srcp->bsize, destp->bsize);
920         P_32_COPY(srcp->bshift, destp->bshift);
921         P_32_COPY(srcp->dsize, destp->dsize);
922         P_32_COPY(srcp->ssize, destp->ssize);
923         P_32_COPY(srcp->sshift, destp->sshift);
924         P_32_COPY(srcp->ovfl_point, destp->ovfl_point);
925         P_32_COPY(srcp->last_freed, destp->last_freed);
926         P_32_COPY(srcp->max_bucket, destp->max_bucket);
927         P_32_COPY(srcp->high_mask, destp->high_mask);
928         P_32_COPY(srcp->low_mask, destp->low_mask);
929         P_32_COPY(srcp->ffactor, destp->ffactor);
930         P_32_COPY(srcp->nkeys, destp->nkeys);
931         P_32_COPY(srcp->hdrpages, destp->hdrpages);
932         P_32_COPY(srcp->h_charkey, destp->h_charkey);
933         for (i = 0; i < NCACHED; i++) {
934                 P_32_COPY(srcp->spares[i], destp->spares[i]);
935                 P_16_COPY(srcp->bitmaps[i], destp->bitmaps[i]);
936         }
937 }
938
939 static void
940 swap_header(HTAB *hashp)
941 {
942         HASHHDR *hdrp;
943         int i;
944
945         hdrp = &hashp->hdr;
946
947         M_32_SWAP(hdrp->magic);
948         M_32_SWAP(hdrp->version);
949         M_32_SWAP(hdrp->lorder);
950         M_32_SWAP(hdrp->bsize);
951         M_32_SWAP(hdrp->bshift);
952         M_32_SWAP(hdrp->dsize);
953         M_32_SWAP(hdrp->ssize);
954         M_32_SWAP(hdrp->sshift);
955         M_32_SWAP(hdrp->ovfl_point);
956         M_32_SWAP(hdrp->last_freed);
957         M_32_SWAP(hdrp->max_bucket);
958         M_32_SWAP(hdrp->high_mask);
959         M_32_SWAP(hdrp->low_mask);
960         M_32_SWAP(hdrp->ffactor);
961         M_32_SWAP(hdrp->nkeys);
962         M_32_SWAP(hdrp->hdrpages);
963         M_32_SWAP(hdrp->h_charkey);
964         for (i = 0; i < NCACHED; i++) {
965                 M_32_SWAP(hdrp->spares[i]);
966                 M_16_SWAP(hdrp->bitmaps[i]);
967         }
968 }
969 #endif