10SN/A/*-
21472SN/A * Copyright (c) 1990, 1993, 1994
30SN/A *	The Regents of the University of California.  All rights reserved.
40SN/A *
50SN/A * This code is derived from software contributed to Berkeley by
60SN/A * Mike Olson.
70SN/A *
80SN/A * Redistribution and use in source and binary forms, with or without
90SN/A * modification, are permitted provided that the following conditions
100SN/A * are met:
110SN/A * 1. Redistributions of source code must retain the above copyright
120SN/A *    notice, this list of conditions and the following disclaimer.
130SN/A * 2. Redistributions in binary form must reproduce the above copyright
140SN/A *    notice, this list of conditions and the following disclaimer in the
150SN/A *    documentation and/or other materials provided with the distribution.
160SN/A * 4. Neither the name of the University nor the names of its contributors
170SN/A *    may be used to endorse or promote products derived from this software
180SN/A *    without specific prior written permission.
191472SN/A *
201472SN/A * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
211472SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
220SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
230SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
240SN/A * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
250SN/A * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
260SN/A * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
270SN/A * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
280SN/A * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
290SN/A * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
300SN/A * SUCH DAMAGE.
310SN/A */
320SN/A
330SN/A#if defined(LIBC_SCCS) && !defined(lint)
340SN/Astatic char sccsid[] = "@(#)bt_conv.c	8.5 (Berkeley) 8/17/94";
350SN/A#endif /* LIBC_SCCS and not lint */
360SN/A#include <sys/cdefs.h>
370SN/A__FBSDID("$FreeBSD$");
380SN/A
390SN/A#include <sys/param.h>
400SN/A
410SN/A#include <stdio.h>
420SN/A
430SN/A#include <db.h>
440SN/A#include "btree.h"
450SN/A
460SN/Astatic void mswap(PAGE *);
470SN/A
480SN/A/*
490SN/A * __BT_BPGIN, __BT_BPGOUT --
500SN/A *	Convert host-specific number layout to/from the host-independent
510SN/A *	format stored on disk.
520SN/A *
530SN/A * Parameters:
540SN/A *	t:	tree
550SN/A *	pg:	page number
560SN/A *	h:	page to convert
570SN/A */
580SN/Avoid
590SN/A__bt_pgin(void *t, pgno_t pg, void *pp)
600SN/A{
610SN/A	PAGE *h;
620SN/A	indx_t i, top;
630SN/A	u_char flags;
640SN/A	char *p;
650SN/A
660SN/A	if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
670SN/A		return;
680SN/A	if (pg == P_META) {
690SN/A		mswap(pp);
700SN/A		return;
710SN/A	}
72
73	h = pp;
74	M_32_SWAP(h->pgno);
75	M_32_SWAP(h->prevpg);
76	M_32_SWAP(h->nextpg);
77	M_32_SWAP(h->flags);
78	M_16_SWAP(h->lower);
79	M_16_SWAP(h->upper);
80
81	top = NEXTINDEX(h);
82	if ((h->flags & P_TYPE) == P_BINTERNAL)
83		for (i = 0; i < top; i++) {
84			M_16_SWAP(h->linp[i]);
85			p = (char *)GETBINTERNAL(h, i);
86			P_32_SWAP(p);
87			p += sizeof(u_int32_t);
88			P_32_SWAP(p);
89			p += sizeof(pgno_t);
90			if (*(u_char *)p & P_BIGKEY) {
91				p += sizeof(u_char);
92				P_32_SWAP(p);
93				p += sizeof(pgno_t);
94				P_32_SWAP(p);
95			}
96		}
97	else if ((h->flags & P_TYPE) == P_BLEAF)
98		for (i = 0; i < top; i++) {
99			M_16_SWAP(h->linp[i]);
100			p = (char *)GETBLEAF(h, i);
101			P_32_SWAP(p);
102			p += sizeof(u_int32_t);
103			P_32_SWAP(p);
104			p += sizeof(u_int32_t);
105			flags = *(u_char *)p;
106			if (flags & (P_BIGKEY | P_BIGDATA)) {
107				p += sizeof(u_char);
108				if (flags & P_BIGKEY) {
109					P_32_SWAP(p);
110					p += sizeof(pgno_t);
111					P_32_SWAP(p);
112				}
113				if (flags & P_BIGDATA) {
114					p += sizeof(u_int32_t);
115					P_32_SWAP(p);
116					p += sizeof(pgno_t);
117					P_32_SWAP(p);
118				}
119			}
120		}
121}
122
123void
124__bt_pgout(void *t, pgno_t pg, void *pp)
125{
126	PAGE *h;
127	indx_t i, top;
128	u_char flags;
129	char *p;
130
131	if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
132		return;
133	if (pg == P_META) {
134		mswap(pp);
135		return;
136	}
137
138	h = pp;
139	top = NEXTINDEX(h);
140	if ((h->flags & P_TYPE) == P_BINTERNAL)
141		for (i = 0; i < top; i++) {
142			p = (char *)GETBINTERNAL(h, i);
143			P_32_SWAP(p);
144			p += sizeof(u_int32_t);
145			P_32_SWAP(p);
146			p += sizeof(pgno_t);
147			if (*(u_char *)p & P_BIGKEY) {
148				p += sizeof(u_char);
149				P_32_SWAP(p);
150				p += sizeof(pgno_t);
151				P_32_SWAP(p);
152			}
153			M_16_SWAP(h->linp[i]);
154		}
155	else if ((h->flags & P_TYPE) == P_BLEAF)
156		for (i = 0; i < top; i++) {
157			p = (char *)GETBLEAF(h, i);
158			P_32_SWAP(p);
159			p += sizeof(u_int32_t);
160			P_32_SWAP(p);
161			p += sizeof(u_int32_t);
162			flags = *(u_char *)p;
163			if (flags & (P_BIGKEY | P_BIGDATA)) {
164				p += sizeof(u_char);
165				if (flags & P_BIGKEY) {
166					P_32_SWAP(p);
167					p += sizeof(pgno_t);
168					P_32_SWAP(p);
169				}
170				if (flags & P_BIGDATA) {
171					p += sizeof(u_int32_t);
172					P_32_SWAP(p);
173					p += sizeof(pgno_t);
174					P_32_SWAP(p);
175				}
176			}
177			M_16_SWAP(h->linp[i]);
178		}
179
180	M_32_SWAP(h->pgno);
181	M_32_SWAP(h->prevpg);
182	M_32_SWAP(h->nextpg);
183	M_32_SWAP(h->flags);
184	M_16_SWAP(h->lower);
185	M_16_SWAP(h->upper);
186}
187
188/*
189 * MSWAP -- Actually swap the bytes on the meta page.
190 *
191 * Parameters:
192 *	p:	page to convert
193 */
194static void
195mswap(PAGE *pg)
196{
197	char *p;
198
199	p = (char *)pg;
200	P_32_SWAP(p);		/* magic */
201	p += sizeof(u_int32_t);
202	P_32_SWAP(p);		/* version */
203	p += sizeof(u_int32_t);
204	P_32_SWAP(p);		/* psize */
205	p += sizeof(u_int32_t);
206	P_32_SWAP(p);		/* free */
207	p += sizeof(u_int32_t);
208	P_32_SWAP(p);		/* nrecs */
209	p += sizeof(u_int32_t);
210	P_32_SWAP(p);		/* flags */
211	p += sizeof(u_int32_t);
212}
213