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