root/libdb/bt_conv.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __bt_pgin
  2. __bt_pgout
  3. mswap

   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  * Mike Olson.
   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  * 3. 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[] = "@(#)bt_conv.c   8.5 (Berkeley) 8/17/94";
  35 #endif /* LIBC_SCCS and not lint */
  36 
  37 #ifdef HAVE_CONFIG_H
  38 #include <config.h>
  39 #endif
  40 #include <stdio.h>
  41 
  42 #include "db.h"
  43 #include "btree.h"
  44 
  45 static void mswap(PAGE *);
  46 
  47 /**
  48  * __BT_BPGIN --
  49  *      Convert host-specific number layout to/from the host-independent
  50  *      format stored on disk.
  51  *
  52  *      @param t        tree
  53  *      @param pg       page number
  54  *      @param pp       page to convert
  55  */
  56 void
  57 __bt_pgin(t, pg, pp)
  58         void *t;
  59         pgno_t pg;
  60         void *pp;
  61 {
  62         PAGE *h;
  63         indx_t i, top;
  64         u_char flags;
  65         char *p;
  66 
  67         if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
  68                 return;
  69         if (pg == P_META) {
  70                 mswap(pp);
  71                 return;
  72         }
  73 
  74         h = pp;
  75         M_32_SWAP(h->pgno);
  76         M_32_SWAP(h->prevpg);
  77         M_32_SWAP(h->nextpg);
  78         M_32_SWAP(h->flags);
  79         M_16_SWAP(h->lower);
  80         M_16_SWAP(h->upper);
  81 
  82         top = NEXTINDEX(h);
  83         if ((h->flags & P_TYPE) == P_BINTERNAL)
  84                 for (i = 0; i < top; i++) {
  85                         M_16_SWAP(h->linp[i]);
  86                         p = (char *)GETBINTERNAL(h, i);
  87                         P_32_SWAP(p);
  88                         p += sizeof(u_int32_t);
  89                         P_32_SWAP(p);
  90                         p += sizeof(pgno_t);
  91                         if (*(u_char *)p & P_BIGKEY) {
  92                                 p += sizeof(u_char);
  93                                 P_32_SWAP(p);
  94                                 p += sizeof(pgno_t);
  95                                 P_32_SWAP(p);
  96                         }
  97                 }
  98         else if ((h->flags & P_TYPE) == P_BLEAF)
  99                 for (i = 0; i < top; i++) {
 100                         M_16_SWAP(h->linp[i]);
 101                         p = (char *)GETBLEAF(h, i);
 102                         P_32_SWAP(p);
 103                         p += sizeof(u_int32_t);
 104                         P_32_SWAP(p);
 105                         p += sizeof(u_int32_t);
 106                         flags = *(u_char *)p;
 107                         if (flags & (P_BIGKEY | P_BIGDATA)) {
 108                                 p += sizeof(u_char);
 109                                 if (flags & P_BIGKEY) {
 110                                         P_32_SWAP(p);
 111                                         p += sizeof(pgno_t);
 112                                         P_32_SWAP(p);
 113                                 }
 114                                 if (flags & P_BIGDATA) {
 115                                         p += sizeof(u_int32_t);
 116                                         P_32_SWAP(p);
 117                                         p += sizeof(pgno_t);
 118                                         P_32_SWAP(p);
 119                                 }
 120                         }
 121                 }
 122 }
 123 
 124 /**
 125  * __BT_BPGOUT --
 126  *      Convert host-specific number layout to/from the host-independent
 127  *      format stored on disk.
 128  *
 129  *      @param t        tree
 130  *      @param pg       page number
 131  *      @param pp       page to convert
 132  */
 133 void
 134 __bt_pgout(t, pg, pp)
 135         void *t;
 136         pgno_t pg;
 137         void *pp;
 138 {
 139         PAGE *h;
 140         indx_t i, top;
 141         u_char flags;
 142         char *p;
 143 
 144         if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
 145                 return;
 146         if (pg == P_META) {
 147                 mswap(pp);
 148                 return;
 149         }
 150 
 151         h = pp;
 152         top = NEXTINDEX(h);
 153         if ((h->flags & P_TYPE) == P_BINTERNAL)
 154                 for (i = 0; i < top; i++) {
 155                         p = (char *)GETBINTERNAL(h, i);
 156                         P_32_SWAP(p);
 157                         p += sizeof(u_int32_t);
 158                         P_32_SWAP(p);
 159                         p += sizeof(pgno_t);
 160                         if (*(u_char *)p & P_BIGKEY) {
 161                                 p += sizeof(u_char);
 162                                 P_32_SWAP(p);
 163                                 p += sizeof(pgno_t);
 164                                 P_32_SWAP(p);
 165                         }
 166                         M_16_SWAP(h->linp[i]);
 167                 }
 168         else if ((h->flags & P_TYPE) == P_BLEAF)
 169                 for (i = 0; i < top; i++) {
 170                         p = (char *)GETBLEAF(h, i);
 171                         P_32_SWAP(p);
 172                         p += sizeof(u_int32_t);
 173                         P_32_SWAP(p);
 174                         p += sizeof(u_int32_t);
 175                         flags = *(u_char *)p;
 176                         if (flags & (P_BIGKEY | P_BIGDATA)) {
 177                                 p += sizeof(u_char);
 178                                 if (flags & P_BIGKEY) {
 179                                         P_32_SWAP(p);
 180                                         p += sizeof(pgno_t);
 181                                         P_32_SWAP(p);
 182                                 }
 183                                 if (flags & P_BIGDATA) {
 184                                         p += sizeof(u_int32_t);
 185                                         P_32_SWAP(p);
 186                                         p += sizeof(pgno_t);
 187                                         P_32_SWAP(p);
 188                                 }
 189                         }
 190                         M_16_SWAP(h->linp[i]);
 191                 }
 192 
 193         M_32_SWAP(h->pgno);
 194         M_32_SWAP(h->prevpg);
 195         M_32_SWAP(h->nextpg);
 196         M_32_SWAP(h->flags);
 197         M_16_SWAP(h->lower);
 198         M_16_SWAP(h->upper);
 199 }
 200 
 201 /**
 202  * MSWAP -- Actually swap the bytes on the meta page.
 203  *
 204  *      @param pg       page to convert
 205  */
 206 static void
 207 mswap(pg)
 208         PAGE *pg;
 209 {
 210         char *p;
 211 
 212         p = (char *)pg;
 213         P_32_SWAP(p);           /* magic */
 214         p += sizeof(u_int32_t);
 215         P_32_SWAP(p);           /* version */
 216         p += sizeof(u_int32_t);
 217         P_32_SWAP(p);           /* psize */
 218         p += sizeof(u_int32_t);
 219         P_32_SWAP(p);           /* free */
 220         p += sizeof(u_int32_t);
 221         P_32_SWAP(p);           /* nrecs */
 222         p += sizeof(u_int32_t);
 223         P_32_SWAP(p);           /* flags */
 224 #if 0   /* To satisfy compiler */
 225         p += sizeof(u_int32_t);
 226 #endif
 227 }

/* [previous][next][first][last][top][bottom][index][help] */