Home | History | Annotate | Line # | Download | only in btree
bt_conv.c revision 1.2
      1 /*-
      2  * Copyright (c) 1990, 1993
      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. All advertising materials mentioning features or use of this software
     17  *    must display the following acknowledgement:
     18  *	This product includes software developed by the University of
     19  *	California, Berkeley and its contributors.
     20  * 4. Neither the name of the University nor the names of its contributors
     21  *    may be used to endorse or promote products derived from this software
     22  *    without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  * SUCH DAMAGE.
     35  */
     36 
     37 #if defined(LIBC_SCCS) && !defined(lint)
     38 /*static char sccsid[] = "from: @(#)bt_conv.c	8.1 (Berkeley) 6/4/93";*/
     39 static char rcsid[] = "$Id: bt_conv.c,v 1.2 1993/08/01 18:43:59 mycroft Exp $";
     40 #endif /* LIBC_SCCS and not lint */
     41 
     42 #include <sys/param.h>
     43 
     44 #include <stdio.h>
     45 
     46 #include <db.h>
     47 #include "btree.h"
     48 
     49 static void mswap __P((PAGE *));
     50 
     51 /*
     52  * __BT_BPGIN, __BT_BPGOUT --
     53  *	Convert host-specific number layout to/from the host-independent
     54  *	format stored on disk.
     55  *
     56  * Parameters:
     57  *	t:	tree
     58  *	pg:	page number
     59  *	h:	page to convert
     60  */
     61 void
     62 __bt_pgin(t, pg, pp)
     63 	void *t;
     64 	pgno_t pg;
     65 	void *pp;
     66 {
     67 	PAGE *h;
     68 	int i, top;
     69 	u_char flags;
     70 	char *p;
     71 
     72 	if (!ISSET(((BTREE *)t), B_NEEDSWAP))
     73 		return;
     74 	if (pg == P_META) {
     75 		mswap(pp);
     76 		return;
     77 	}
     78 
     79 	h = pp;
     80 	BLSWAP(h->pgno);
     81 	BLSWAP(h->prevpg);
     82 	BLSWAP(h->nextpg);
     83 	BLSWAP(h->flags);
     84 	BSSWAP(h->lower);
     85 	BSSWAP(h->upper);
     86 
     87 	top = NEXTINDEX(h);
     88 	if ((h->flags & P_TYPE) == P_BINTERNAL)
     89 		for (i = 0; i < top; i++) {
     90 			BSSWAP(h->linp[i]);
     91 			p = (char *)GETBINTERNAL(h, i);
     92 			BLPSWAP(p);
     93 			p += sizeof(size_t);
     94 			BLPSWAP(p);
     95 			p += sizeof(pgno_t);
     96 			if (*(u_char *)p & P_BIGKEY) {
     97 				p += sizeof(u_char);
     98 				BLPSWAP(p);
     99 				p += sizeof(pgno_t);
    100 				BLPSWAP(p);
    101 			}
    102 		}
    103 	else if ((h->flags & P_TYPE) == P_BLEAF)
    104 		for (i = 0; i < top; i++) {
    105 			BSSWAP(h->linp[i]);
    106 			p = (char *)GETBLEAF(h, i);
    107 			BLPSWAP(p);
    108 			p += sizeof(size_t);
    109 			BLPSWAP(p);
    110 			p += sizeof(size_t);
    111 			flags = *(u_char *)p;
    112 			if (flags & (P_BIGKEY | P_BIGDATA)) {
    113 				p += sizeof(u_char);
    114 				if (flags & P_BIGKEY) {
    115 					BLPSWAP(p);
    116 					p += sizeof(pgno_t);
    117 					BLPSWAP(p);
    118 				}
    119 				if (flags & P_BIGDATA) {
    120 					p += sizeof(size_t);
    121 					BLPSWAP(p);
    122 					p += sizeof(pgno_t);
    123 					BLPSWAP(p);
    124 				}
    125 			}
    126 		}
    127 }
    128 
    129 void
    130 __bt_pgout(t, pg, pp)
    131 	void *t;
    132 	pgno_t pg;
    133 	void *pp;
    134 {
    135 	PAGE *h;
    136 	int i, top;
    137 	u_char flags;
    138 	char *p;
    139 
    140 	if (!ISSET(((BTREE *)t), B_NEEDSWAP))
    141 		return;
    142 	if (pg == P_META) {
    143 		mswap(pp);
    144 		return;
    145 	}
    146 
    147 	h = pp;
    148 	top = NEXTINDEX(h);
    149 	if ((h->flags & P_TYPE) == P_BINTERNAL)
    150 		for (i = 0; i < top; i++) {
    151 			p = (char *)GETBINTERNAL(h, i);
    152 			BLPSWAP(p);
    153 			p += sizeof(size_t);
    154 			BLPSWAP(p);
    155 			p += sizeof(pgno_t);
    156 			if (*(u_char *)p & P_BIGKEY) {
    157 				p += sizeof(u_char);
    158 				BLPSWAP(p);
    159 				p += sizeof(pgno_t);
    160 				BLPSWAP(p);
    161 			}
    162 			BSSWAP(h->linp[i]);
    163 		}
    164 	else if ((h->flags & P_TYPE) == P_BLEAF)
    165 		for (i = 0; i < top; i++) {
    166 			p = (char *)GETBLEAF(h, i);
    167 			BLPSWAP(p);
    168 			p += sizeof(size_t);
    169 			BLPSWAP(p);
    170 			p += sizeof(size_t);
    171 			flags = *(u_char *)p;
    172 			if (flags & (P_BIGKEY | P_BIGDATA)) {
    173 				p += sizeof(u_char);
    174 				if (flags & P_BIGKEY) {
    175 					BLPSWAP(p);
    176 					p += sizeof(pgno_t);
    177 					BLPSWAP(p);
    178 				}
    179 				if (flags & P_BIGDATA) {
    180 					p += sizeof(size_t);
    181 					BLPSWAP(p);
    182 					p += sizeof(pgno_t);
    183 					BLPSWAP(p);
    184 				}
    185 			}
    186 			BSSWAP(h->linp[i]);
    187 		}
    188 
    189 	BLSWAP(h->pgno);
    190 	BLSWAP(h->prevpg);
    191 	BLSWAP(h->nextpg);
    192 	BLSWAP(h->flags);
    193 	BSSWAP(h->lower);
    194 	BSSWAP(h->upper);
    195 }
    196 
    197 /*
    198  * MSWAP -- Actually swap the bytes on the meta page.
    199  *
    200  * Parameters:
    201  *	p:	page to convert
    202  */
    203 static void
    204 mswap(pg)
    205 	PAGE *pg;
    206 {
    207 	char *p;
    208 
    209 	p = (char *)pg;
    210 	BLPSWAP(p);		/* m_magic */
    211 	p += sizeof(u_long);
    212 	BLPSWAP(p);		/* m_version */
    213 	p += sizeof(u_long);
    214 	BLPSWAP(p);		/* m_psize */
    215 	p += sizeof(u_long);
    216 	BLPSWAP(p);		/* m_free */
    217 	p += sizeof(u_long);
    218 	BLPSWAP(p);		/* m_nrecs */
    219 	p += sizeof(u_long);
    220 	BLPSWAP(p);		/* m_flags */
    221 	p += sizeof(u_long);
    222 }
    223