security/nss/lib/dbm/src/h_bigkey.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, 1994
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_bigkey.c 8.3 (Berkeley) 5/31/94";
michael@0 37 #endif /* LIBC_SCCS and not lint */
michael@0 38
michael@0 39 /*
michael@0 40 * PACKAGE: hash
michael@0 41 * DESCRIPTION:
michael@0 42 * Big key/data handling for the hashing package.
michael@0 43 *
michael@0 44 * ROUTINES:
michael@0 45 * External
michael@0 46 * __big_keydata
michael@0 47 * __big_split
michael@0 48 * __big_insert
michael@0 49 * __big_return
michael@0 50 * __big_delete
michael@0 51 * __find_last_page
michael@0 52 * Internal
michael@0 53 * collect_key
michael@0 54 * collect_data
michael@0 55 */
michael@0 56
michael@0 57 #if !defined(_WIN32) && !defined(_WINDOWS) && !defined(macintosh)
michael@0 58 #include <sys/param.h>
michael@0 59 #endif
michael@0 60
michael@0 61 #include <errno.h>
michael@0 62 #include <stdio.h>
michael@0 63 #include <stdlib.h>
michael@0 64 #include <string.h>
michael@0 65
michael@0 66 #ifdef DEBUG
michael@0 67 #include <assert.h>
michael@0 68 #endif
michael@0 69
michael@0 70 #include "mcom_db.h"
michael@0 71 #include "hash.h"
michael@0 72 #include "page.h"
michael@0 73 /* #include "extern.h" */
michael@0 74
michael@0 75 static int collect_key __P((HTAB *, BUFHEAD *, int, DBT *, int));
michael@0 76 static int collect_data __P((HTAB *, BUFHEAD *, int, int));
michael@0 77
michael@0 78 /*
michael@0 79 * Big_insert
michael@0 80 *
michael@0 81 * You need to do an insert and the key/data pair is too big
michael@0 82 *
michael@0 83 * Returns:
michael@0 84 * 0 ==> OK
michael@0 85 *-1 ==> ERROR
michael@0 86 */
michael@0 87 extern int
michael@0 88 __big_insert(HTAB *hashp, BUFHEAD *bufp, const DBT *key, const DBT *val)
michael@0 89 {
michael@0 90 register uint16 *p;
michael@0 91 uint key_size, n, val_size;
michael@0 92 uint16 space, move_bytes, off;
michael@0 93 char *cp, *key_data, *val_data;
michael@0 94
michael@0 95 cp = bufp->page; /* Character pointer of p. */
michael@0 96 p = (uint16 *)cp;
michael@0 97
michael@0 98 key_data = (char *)key->data;
michael@0 99 key_size = key->size;
michael@0 100 val_data = (char *)val->data;
michael@0 101 val_size = val->size;
michael@0 102
michael@0 103 /* First move the Key */
michael@0 104 for (space = FREESPACE(p) - BIGOVERHEAD; key_size;
michael@0 105 space = FREESPACE(p) - BIGOVERHEAD) {
michael@0 106 move_bytes = PR_MIN(space, key_size);
michael@0 107 off = OFFSET(p) - move_bytes;
michael@0 108 memmove(cp + off, key_data, move_bytes);
michael@0 109 key_size -= move_bytes;
michael@0 110 key_data += move_bytes;
michael@0 111 n = p[0];
michael@0 112 p[++n] = off;
michael@0 113 p[0] = ++n;
michael@0 114 FREESPACE(p) = off - PAGE_META(n);
michael@0 115 OFFSET(p) = off;
michael@0 116 p[n] = PARTIAL_KEY;
michael@0 117 bufp = __add_ovflpage(hashp, bufp);
michael@0 118 if (!bufp)
michael@0 119 return (-1);
michael@0 120 n = p[0];
michael@0 121 if (!key_size) {
michael@0 122 if (FREESPACE(p)) {
michael@0 123 move_bytes = PR_MIN(FREESPACE(p), val_size);
michael@0 124 off = OFFSET(p) - move_bytes;
michael@0 125 p[n] = off;
michael@0 126 memmove(cp + off, val_data, move_bytes);
michael@0 127 val_data += move_bytes;
michael@0 128 val_size -= move_bytes;
michael@0 129 p[n - 2] = FULL_KEY_DATA;
michael@0 130 FREESPACE(p) = FREESPACE(p) - move_bytes;
michael@0 131 OFFSET(p) = off;
michael@0 132 } else
michael@0 133 p[n - 2] = FULL_KEY;
michael@0 134 }
michael@0 135 p = (uint16 *)bufp->page;
michael@0 136 cp = bufp->page;
michael@0 137 bufp->flags |= BUF_MOD;
michael@0 138 }
michael@0 139
michael@0 140 /* Now move the data */
michael@0 141 for (space = FREESPACE(p) - BIGOVERHEAD; val_size;
michael@0 142 space = FREESPACE(p) - BIGOVERHEAD) {
michael@0 143 move_bytes = PR_MIN(space, val_size);
michael@0 144 /*
michael@0 145 * Here's the hack to make sure that if the data ends on the
michael@0 146 * same page as the key ends, FREESPACE is at least one.
michael@0 147 */
michael@0 148 if (space == val_size && val_size == val->size)
michael@0 149 move_bytes--;
michael@0 150 off = OFFSET(p) - move_bytes;
michael@0 151 memmove(cp + off, val_data, move_bytes);
michael@0 152 val_size -= move_bytes;
michael@0 153 val_data += move_bytes;
michael@0 154 n = p[0];
michael@0 155 p[++n] = off;
michael@0 156 p[0] = ++n;
michael@0 157 FREESPACE(p) = off - PAGE_META(n);
michael@0 158 OFFSET(p) = off;
michael@0 159 if (val_size) {
michael@0 160 p[n] = FULL_KEY;
michael@0 161 bufp = __add_ovflpage(hashp, bufp);
michael@0 162 if (!bufp)
michael@0 163 return (-1);
michael@0 164 cp = bufp->page;
michael@0 165 p = (uint16 *)cp;
michael@0 166 } else
michael@0 167 p[n] = FULL_KEY_DATA;
michael@0 168 bufp->flags |= BUF_MOD;
michael@0 169 }
michael@0 170 return (0);
michael@0 171 }
michael@0 172
michael@0 173 /*
michael@0 174 * Called when bufp's page contains a partial key (index should be 1)
michael@0 175 *
michael@0 176 * All pages in the big key/data pair except bufp are freed. We cannot
michael@0 177 * free bufp because the page pointing to it is lost and we can't get rid
michael@0 178 * of its pointer.
michael@0 179 *
michael@0 180 * Returns:
michael@0 181 * 0 => OK
michael@0 182 *-1 => ERROR
michael@0 183 */
michael@0 184 extern int
michael@0 185 __big_delete(HTAB *hashp, BUFHEAD *bufp)
michael@0 186 {
michael@0 187 register BUFHEAD *last_bfp, *rbufp;
michael@0 188 uint16 *bp, pageno;
michael@0 189 int key_done, n;
michael@0 190
michael@0 191 rbufp = bufp;
michael@0 192 last_bfp = NULL;
michael@0 193 bp = (uint16 *)bufp->page;
michael@0 194 pageno = 0;
michael@0 195 key_done = 0;
michael@0 196
michael@0 197 while (!key_done || (bp[2] != FULL_KEY_DATA)) {
michael@0 198 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA)
michael@0 199 key_done = 1;
michael@0 200
michael@0 201 /*
michael@0 202 * If there is freespace left on a FULL_KEY_DATA page, then
michael@0 203 * the data is short and fits entirely on this page, and this
michael@0 204 * is the last page.
michael@0 205 */
michael@0 206 if (bp[2] == FULL_KEY_DATA && FREESPACE(bp))
michael@0 207 break;
michael@0 208 pageno = bp[bp[0] - 1];
michael@0 209 rbufp->flags |= BUF_MOD;
michael@0 210 rbufp = __get_buf(hashp, pageno, rbufp, 0);
michael@0 211 if (last_bfp)
michael@0 212 __free_ovflpage(hashp, last_bfp);
michael@0 213 last_bfp = rbufp;
michael@0 214 if (!rbufp)
michael@0 215 return (-1); /* Error. */
michael@0 216 bp = (uint16 *)rbufp->page;
michael@0 217 }
michael@0 218
michael@0 219 /*
michael@0 220 * If we get here then rbufp points to the last page of the big
michael@0 221 * key/data pair. Bufp points to the first one -- it should now be
michael@0 222 * empty pointing to the next page after this pair. Can't free it
michael@0 223 * because we don't have the page pointing to it.
michael@0 224 */
michael@0 225
michael@0 226 /* This is information from the last page of the pair. */
michael@0 227 n = bp[0];
michael@0 228 pageno = bp[n - 1];
michael@0 229
michael@0 230 /* Now, bp is the first page of the pair. */
michael@0 231 bp = (uint16 *)bufp->page;
michael@0 232 if (n > 2) {
michael@0 233 /* There is an overflow page. */
michael@0 234 bp[1] = pageno;
michael@0 235 bp[2] = OVFLPAGE;
michael@0 236 bufp->ovfl = rbufp->ovfl;
michael@0 237 } else
michael@0 238 /* This is the last page. */
michael@0 239 bufp->ovfl = NULL;
michael@0 240 n -= 2;
michael@0 241 bp[0] = n;
michael@0 242 FREESPACE(bp) = hashp->BSIZE - PAGE_META(n);
michael@0 243 OFFSET(bp) = hashp->BSIZE - 1;
michael@0 244
michael@0 245 bufp->flags |= BUF_MOD;
michael@0 246 if (rbufp)
michael@0 247 __free_ovflpage(hashp, rbufp);
michael@0 248 if (last_bfp != rbufp)
michael@0 249 __free_ovflpage(hashp, last_bfp);
michael@0 250
michael@0 251 hashp->NKEYS--;
michael@0 252 return (0);
michael@0 253 }
michael@0 254 /*
michael@0 255 * Returns:
michael@0 256 * 0 = key not found
michael@0 257 * -1 = get next overflow page
michael@0 258 * -2 means key not found and this is big key/data
michael@0 259 * -3 error
michael@0 260 */
michael@0 261 extern int
michael@0 262 __find_bigpair(HTAB *hashp, BUFHEAD *bufp, int ndx, char *key, int size)
michael@0 263 {
michael@0 264 register uint16 *bp;
michael@0 265 register char *p;
michael@0 266 int ksize;
michael@0 267 uint16 bytes;
michael@0 268 char *kkey;
michael@0 269
michael@0 270 bp = (uint16 *)bufp->page;
michael@0 271 p = bufp->page;
michael@0 272 ksize = size;
michael@0 273 kkey = key;
michael@0 274
michael@0 275 for (bytes = hashp->BSIZE - bp[ndx];
michael@0 276 bytes <= size && bp[ndx + 1] == PARTIAL_KEY;
michael@0 277 bytes = hashp->BSIZE - bp[ndx]) {
michael@0 278 if (memcmp(p + bp[ndx], kkey, bytes))
michael@0 279 return (-2);
michael@0 280 kkey += bytes;
michael@0 281 ksize -= bytes;
michael@0 282 bufp = __get_buf(hashp, bp[ndx + 2], bufp, 0);
michael@0 283 if (!bufp)
michael@0 284 return (-3);
michael@0 285 p = bufp->page;
michael@0 286 bp = (uint16 *)p;
michael@0 287 ndx = 1;
michael@0 288 }
michael@0 289
michael@0 290 if (bytes != ksize || memcmp(p + bp[ndx], kkey, bytes)) {
michael@0 291 #ifdef HASH_STATISTICS
michael@0 292 ++hash_collisions;
michael@0 293 #endif
michael@0 294 return (-2);
michael@0 295 } else
michael@0 296 return (ndx);
michael@0 297 }
michael@0 298
michael@0 299 /*
michael@0 300 * Given the buffer pointer of the first overflow page of a big pair,
michael@0 301 * find the end of the big pair
michael@0 302 *
michael@0 303 * This will set bpp to the buffer header of the last page of the big pair.
michael@0 304 * It will return the pageno of the overflow page following the last page
michael@0 305 * of the pair; 0 if there isn't any (i.e. big pair is the last key in the
michael@0 306 * bucket)
michael@0 307 */
michael@0 308 extern uint16
michael@0 309 __find_last_page(HTAB *hashp, BUFHEAD **bpp)
michael@0 310 {
michael@0 311 BUFHEAD *bufp;
michael@0 312 uint16 *bp, pageno;
michael@0 313 uint n;
michael@0 314
michael@0 315 bufp = *bpp;
michael@0 316 bp = (uint16 *)bufp->page;
michael@0 317 for (;;) {
michael@0 318 n = bp[0];
michael@0 319
michael@0 320 /*
michael@0 321 * This is the last page if: the tag is FULL_KEY_DATA and
michael@0 322 * either only 2 entries OVFLPAGE marker is explicit there
michael@0 323 * is freespace on the page.
michael@0 324 */
michael@0 325 if (bp[2] == FULL_KEY_DATA &&
michael@0 326 ((n == 2) || (bp[n] == OVFLPAGE) || (FREESPACE(bp))))
michael@0 327 break;
michael@0 328
michael@0 329 /* LJM bound the size of n to reasonable limits
michael@0 330 */
michael@0 331 if(n > hashp->BSIZE/sizeof(uint16))
michael@0 332 return(0);
michael@0 333
michael@0 334 pageno = bp[n - 1];
michael@0 335 bufp = __get_buf(hashp, pageno, bufp, 0);
michael@0 336 if (!bufp)
michael@0 337 return (0); /* Need to indicate an error! */
michael@0 338 bp = (uint16 *)bufp->page;
michael@0 339 }
michael@0 340
michael@0 341 *bpp = bufp;
michael@0 342 if (bp[0] > 2)
michael@0 343 return (bp[3]);
michael@0 344 else
michael@0 345 return (0);
michael@0 346 }
michael@0 347
michael@0 348 /*
michael@0 349 * Return the data for the key/data pair that begins on this page at this
michael@0 350 * index (index should always be 1).
michael@0 351 */
michael@0 352 extern int
michael@0 353 __big_return(
michael@0 354 HTAB *hashp,
michael@0 355 BUFHEAD *bufp,
michael@0 356 int ndx,
michael@0 357 DBT *val,
michael@0 358 int set_current)
michael@0 359 {
michael@0 360 BUFHEAD *save_p;
michael@0 361 uint16 *bp, len, off, save_addr;
michael@0 362 char *tp;
michael@0 363 int save_flags;
michael@0 364
michael@0 365 bp = (uint16 *)bufp->page;
michael@0 366 while (bp[ndx + 1] == PARTIAL_KEY) {
michael@0 367 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
michael@0 368 if (!bufp)
michael@0 369 return (-1);
michael@0 370 bp = (uint16 *)bufp->page;
michael@0 371 ndx = 1;
michael@0 372 }
michael@0 373
michael@0 374 if (bp[ndx + 1] == FULL_KEY) {
michael@0 375 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
michael@0 376 if (!bufp)
michael@0 377 return (-1);
michael@0 378 bp = (uint16 *)bufp->page;
michael@0 379 save_p = bufp;
michael@0 380 save_addr = save_p->addr;
michael@0 381 off = bp[1];
michael@0 382 len = 0;
michael@0 383 } else
michael@0 384 if (!FREESPACE(bp)) {
michael@0 385 /*
michael@0 386 * This is a hack. We can't distinguish between
michael@0 387 * FULL_KEY_DATA that contains complete data or
michael@0 388 * incomplete data, so we require that if the data
michael@0 389 * is complete, there is at least 1 byte of free
michael@0 390 * space left.
michael@0 391 */
michael@0 392 off = bp[bp[0]];
michael@0 393 len = bp[1] - off;
michael@0 394 save_p = bufp;
michael@0 395 save_addr = bufp->addr;
michael@0 396 bufp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
michael@0 397 if (!bufp)
michael@0 398 return (-1);
michael@0 399 bp = (uint16 *)bufp->page;
michael@0 400 } else {
michael@0 401 /* The data is all on one page. */
michael@0 402 tp = (char *)bp;
michael@0 403 off = bp[bp[0]];
michael@0 404 val->data = (uint8 *)tp + off;
michael@0 405 val->size = bp[1] - off;
michael@0 406 if (set_current) {
michael@0 407 if (bp[0] == 2) { /* No more buckets in
michael@0 408 * chain */
michael@0 409 hashp->cpage = NULL;
michael@0 410 hashp->cbucket++;
michael@0 411 hashp->cndx = 1;
michael@0 412 } else {
michael@0 413 hashp->cpage = __get_buf(hashp,
michael@0 414 bp[bp[0] - 1], bufp, 0);
michael@0 415 if (!hashp->cpage)
michael@0 416 return (-1);
michael@0 417 hashp->cndx = 1;
michael@0 418 if (!((uint16 *)
michael@0 419 hashp->cpage->page)[0]) {
michael@0 420 hashp->cbucket++;
michael@0 421 hashp->cpage = NULL;
michael@0 422 }
michael@0 423 }
michael@0 424 }
michael@0 425 return (0);
michael@0 426 }
michael@0 427
michael@0 428 /* pin our saved buf so that we don't lose if
michael@0 429 * we run out of buffers */
michael@0 430 save_flags = save_p->flags;
michael@0 431 save_p->flags |= BUF_PIN;
michael@0 432 val->size = collect_data(hashp, bufp, (int)len, set_current);
michael@0 433 save_p->flags = save_flags;
michael@0 434 if (val->size == (size_t)-1)
michael@0 435 return (-1);
michael@0 436 if (save_p->addr != save_addr) {
michael@0 437 /* We are pretty short on buffers. */
michael@0 438 errno = EINVAL; /* OUT OF BUFFERS */
michael@0 439 return (-1);
michael@0 440 }
michael@0 441 memmove(hashp->tmp_buf, (save_p->page) + off, len);
michael@0 442 val->data = (uint8 *)hashp->tmp_buf;
michael@0 443 return (0);
michael@0 444 }
michael@0 445
michael@0 446
michael@0 447 /*
michael@0 448 * Count how big the total datasize is by looping through the pages. Then
michael@0 449 * allocate a buffer and copy the data in the second loop. NOTE: Our caller
michael@0 450 * may already have a bp which it is holding onto. The caller is
michael@0 451 * responsible for copying that bp into our temp buffer. 'len' is how much
michael@0 452 * space to reserve for that buffer.
michael@0 453 */
michael@0 454 static int
michael@0 455 collect_data(
michael@0 456 HTAB *hashp,
michael@0 457 BUFHEAD *bufp,
michael@0 458 int len, int set)
michael@0 459 {
michael@0 460 register uint16 *bp;
michael@0 461 BUFHEAD *save_bufp;
michael@0 462 int save_flags;
michael@0 463 int mylen, totlen;
michael@0 464
michael@0 465 /*
michael@0 466 * save the input buf head because we need to walk the list twice.
michael@0 467 * pin it to make sure it doesn't leave the buffer pool.
michael@0 468 * This has the effect of growing the buffer pool if necessary.
michael@0 469 */
michael@0 470 save_bufp = bufp;
michael@0 471 save_flags = save_bufp->flags;
michael@0 472 save_bufp->flags |= BUF_PIN;
michael@0 473
michael@0 474 /* read the length of the buffer */
michael@0 475 for (totlen = len; bufp ; bufp = __get_buf(hashp, bp[bp[0]-1], bufp, 0)) {
michael@0 476 bp = (uint16 *)bufp->page;
michael@0 477 mylen = hashp->BSIZE - bp[1];
michael@0 478
michael@0 479 /* if mylen ever goes negative it means that the
michael@0 480 * page is screwed up.
michael@0 481 */
michael@0 482 if (mylen < 0) {
michael@0 483 save_bufp->flags = save_flags;
michael@0 484 return (-1);
michael@0 485 }
michael@0 486 totlen += mylen;
michael@0 487 if (bp[2] == FULL_KEY_DATA) { /* End of Data */
michael@0 488 break;
michael@0 489 }
michael@0 490 }
michael@0 491
michael@0 492 if (!bufp) {
michael@0 493 save_bufp->flags = save_flags;
michael@0 494 return (-1);
michael@0 495 }
michael@0 496
michael@0 497 /* allocate a temp buf */
michael@0 498 if (hashp->tmp_buf)
michael@0 499 free(hashp->tmp_buf);
michael@0 500 if ((hashp->tmp_buf = (char *)malloc((size_t)totlen)) == NULL) {
michael@0 501 save_bufp->flags = save_flags;
michael@0 502 return (-1);
michael@0 503 }
michael@0 504
michael@0 505 /* copy the buffers back into temp buf */
michael@0 506 for (bufp = save_bufp; bufp ;
michael@0 507 bufp = __get_buf(hashp, bp[bp[0]-1], bufp, 0)) {
michael@0 508 bp = (uint16 *)bufp->page;
michael@0 509 mylen = hashp->BSIZE - bp[1];
michael@0 510 memmove(&hashp->tmp_buf[len], (bufp->page) + bp[1], (size_t)mylen);
michael@0 511 len += mylen;
michael@0 512 if (bp[2] == FULL_KEY_DATA) {
michael@0 513 break;
michael@0 514 }
michael@0 515 }
michael@0 516
michael@0 517 /* 'clear' the pin flags */
michael@0 518 save_bufp->flags = save_flags;
michael@0 519
michael@0 520 /* update the database cursor */
michael@0 521 if (set) {
michael@0 522 hashp->cndx = 1;
michael@0 523 if (bp[0] == 2) { /* No more buckets in chain */
michael@0 524 hashp->cpage = NULL;
michael@0 525 hashp->cbucket++;
michael@0 526 } else {
michael@0 527 hashp->cpage = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
michael@0 528 if (!hashp->cpage)
michael@0 529 return (-1);
michael@0 530 else if (!((uint16 *)hashp->cpage->page)[0]) {
michael@0 531 hashp->cbucket++;
michael@0 532 hashp->cpage = NULL;
michael@0 533 }
michael@0 534 }
michael@0 535 }
michael@0 536 return (totlen);
michael@0 537 }
michael@0 538
michael@0 539 /*
michael@0 540 * Fill in the key and data for this big pair.
michael@0 541 */
michael@0 542 extern int
michael@0 543 __big_keydata(
michael@0 544 HTAB *hashp,
michael@0 545 BUFHEAD *bufp,
michael@0 546 DBT *key, DBT *val,
michael@0 547 int set)
michael@0 548 {
michael@0 549 key->size = collect_key(hashp, bufp, 0, val, set);
michael@0 550 if (key->size == (size_t)-1)
michael@0 551 return (-1);
michael@0 552 key->data = (uint8 *)hashp->tmp_key;
michael@0 553 return (0);
michael@0 554 }
michael@0 555
michael@0 556 /*
michael@0 557 * Count how big the total key size is by recursing through the pages. Then
michael@0 558 * collect the data, allocate a buffer and copy the key as you recurse up.
michael@0 559 */
michael@0 560 static int
michael@0 561 collect_key(
michael@0 562 HTAB *hashp,
michael@0 563 BUFHEAD *bufp,
michael@0 564 int len,
michael@0 565 DBT *val,
michael@0 566 int set)
michael@0 567 {
michael@0 568 BUFHEAD *xbp;
michael@0 569 char *p;
michael@0 570 int mylen, totlen;
michael@0 571 uint16 *bp, save_addr;
michael@0 572
michael@0 573 p = bufp->page;
michael@0 574 bp = (uint16 *)p;
michael@0 575 mylen = hashp->BSIZE - bp[1];
michael@0 576
michael@0 577 save_addr = bufp->addr;
michael@0 578 totlen = len + mylen;
michael@0 579 if (bp[2] == FULL_KEY || bp[2] == FULL_KEY_DATA) { /* End of Key. */
michael@0 580 if (hashp->tmp_key != NULL)
michael@0 581 free(hashp->tmp_key);
michael@0 582 if ((hashp->tmp_key = (char *)malloc((size_t)totlen)) == NULL)
michael@0 583 return (-1);
michael@0 584 if (__big_return(hashp, bufp, 1, val, set))
michael@0 585 return (-1);
michael@0 586 } else {
michael@0 587 xbp = __get_buf(hashp, bp[bp[0] - 1], bufp, 0);
michael@0 588 if (!xbp || ((totlen =
michael@0 589 collect_key(hashp, xbp, totlen, val, set)) < 1))
michael@0 590 return (-1);
michael@0 591 }
michael@0 592 if (bufp->addr != save_addr) {
michael@0 593 errno = EINVAL; /* MIS -- OUT OF BUFFERS */
michael@0 594 return (-1);
michael@0 595 }
michael@0 596 memmove(&hashp->tmp_key[len], (bufp->page) + bp[1], (size_t)mylen);
michael@0 597 return (totlen);
michael@0 598 }
michael@0 599
michael@0 600 /*
michael@0 601 * Returns:
michael@0 602 * 0 => OK
michael@0 603 * -1 => error
michael@0 604 */
michael@0 605 extern int
michael@0 606 __big_split(
michael@0 607 HTAB *hashp,
michael@0 608 BUFHEAD *op, /* Pointer to where to put keys that go in old bucket */
michael@0 609 BUFHEAD *np, /* Pointer to new bucket page */
michael@0 610 /* Pointer to first page containing the big key/data */
michael@0 611 BUFHEAD *big_keyp,
michael@0 612 uint32 addr, /* Address of big_keyp */
michael@0 613 uint32 obucket,/* Old Bucket */
michael@0 614 SPLIT_RETURN *ret)
michael@0 615 {
michael@0 616 register BUFHEAD *tmpp;
michael@0 617 register uint16 *tp;
michael@0 618 BUFHEAD *bp;
michael@0 619 DBT key, val;
michael@0 620 uint32 change;
michael@0 621 uint16 free_space, n, off;
michael@0 622
michael@0 623 bp = big_keyp;
michael@0 624
michael@0 625 /* Now figure out where the big key/data goes */
michael@0 626 if (__big_keydata(hashp, big_keyp, &key, &val, 0))
michael@0 627 return (-1);
michael@0 628 change = (__call_hash(hashp,(char*) key.data, key.size) != obucket);
michael@0 629
michael@0 630 if ((ret->next_addr = __find_last_page(hashp, &big_keyp))) {
michael@0 631 if (!(ret->nextp =
michael@0 632 __get_buf(hashp, ret->next_addr, big_keyp, 0)))
michael@0 633 return (-1);;
michael@0 634 } else
michael@0 635 ret->nextp = NULL;
michael@0 636
michael@0 637 /* Now make one of np/op point to the big key/data pair */
michael@0 638 #ifdef DEBUG
michael@0 639 assert(np->ovfl == NULL);
michael@0 640 #endif
michael@0 641 if (change)
michael@0 642 tmpp = np;
michael@0 643 else
michael@0 644 tmpp = op;
michael@0 645
michael@0 646 tmpp->flags |= BUF_MOD;
michael@0 647 #ifdef DEBUG1
michael@0 648 (void)fprintf(stderr,
michael@0 649 "BIG_SPLIT: %d->ovfl was %d is now %d\n", tmpp->addr,
michael@0 650 (tmpp->ovfl ? tmpp->ovfl->addr : 0), (bp ? bp->addr : 0));
michael@0 651 #endif
michael@0 652 tmpp->ovfl = bp; /* one of op/np point to big_keyp */
michael@0 653 tp = (uint16 *)tmpp->page;
michael@0 654
michael@0 655
michael@0 656 #if 0 /* this get's tripped on database corrupted error */
michael@0 657 assert(FREESPACE(tp) >= OVFLSIZE);
michael@0 658 #endif
michael@0 659 if(FREESPACE(tp) < OVFLSIZE)
michael@0 660 return(DATABASE_CORRUPTED_ERROR);
michael@0 661
michael@0 662 n = tp[0];
michael@0 663 off = OFFSET(tp);
michael@0 664 free_space = FREESPACE(tp);
michael@0 665 tp[++n] = (uint16)addr;
michael@0 666 tp[++n] = OVFLPAGE;
michael@0 667 tp[0] = n;
michael@0 668 OFFSET(tp) = off;
michael@0 669 FREESPACE(tp) = free_space - OVFLSIZE;
michael@0 670
michael@0 671 /*
michael@0 672 * Finally, set the new and old return values. BIG_KEYP contains a
michael@0 673 * pointer to the last page of the big key_data pair. Make sure that
michael@0 674 * big_keyp has no following page (2 elements) or create an empty
michael@0 675 * following page.
michael@0 676 */
michael@0 677
michael@0 678 ret->newp = np;
michael@0 679 ret->oldp = op;
michael@0 680
michael@0 681 tp = (uint16 *)big_keyp->page;
michael@0 682 big_keyp->flags |= BUF_MOD;
michael@0 683 if (tp[0] > 2) {
michael@0 684 /*
michael@0 685 * There may be either one or two offsets on this page. If
michael@0 686 * there is one, then the overflow page is linked on normally
michael@0 687 * and tp[4] is OVFLPAGE. If there are two, tp[4] contains
michael@0 688 * the second offset and needs to get stuffed in after the
michael@0 689 * next overflow page is added.
michael@0 690 */
michael@0 691 n = tp[4];
michael@0 692 free_space = FREESPACE(tp);
michael@0 693 off = OFFSET(tp);
michael@0 694 tp[0] -= 2;
michael@0 695 FREESPACE(tp) = free_space + OVFLSIZE;
michael@0 696 OFFSET(tp) = off;
michael@0 697 tmpp = __add_ovflpage(hashp, big_keyp);
michael@0 698 if (!tmpp)
michael@0 699 return (-1);
michael@0 700 tp[4] = n;
michael@0 701 } else
michael@0 702 tmpp = big_keyp;
michael@0 703
michael@0 704 if (change)
michael@0 705 ret->newp = tmpp;
michael@0 706 else
michael@0 707 ret->oldp = tmpp;
michael@0 708 return (0);
michael@0 709 }

mercurial