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