bt_conv.c revision 331722
1321936Shselasky/*-
2321936Shselasky * Copyright (c) 1990, 1993, 1994
3321936Shselasky *	The Regents of the University of California.  All rights reserved.
4321936Shselasky *
5321936Shselasky * This code is derived from software contributed to Berkeley by
6321936Shselasky * Mike Olson.
7321936Shselasky *
8321936Shselasky * Redistribution and use in source and binary forms, with or without
9321936Shselasky * modification, are permitted provided that the following conditions
10321936Shselasky * are met:
11321936Shselasky * 1. Redistributions of source code must retain the above copyright
12321936Shselasky *    notice, this list of conditions and the following disclaimer.
13321936Shselasky * 2. Redistributions in binary form must reproduce the above copyright
14321936Shselasky *    notice, this list of conditions and the following disclaimer in the
15321936Shselasky *    documentation and/or other materials provided with the distribution.
16321936Shselasky * 4. Neither the name of the University nor the names of its contributors
17321936Shselasky *    may be used to endorse or promote products derived from this software
18321936Shselasky *    without specific prior written permission.
19321936Shselasky *
20321936Shselasky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21321936Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22321936Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23321936Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24321936Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25321936Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26321936Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27321936Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28321936Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29321936Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30321936Shselasky * SUCH DAMAGE.
31321936Shselasky */
32321936Shselasky
33321936Shselasky#if defined(LIBC_SCCS) && !defined(lint)
34321936Shselaskystatic char sccsid[] = "@(#)bt_conv.c	8.5 (Berkeley) 8/17/94";
35321936Shselasky#endif /* LIBC_SCCS and not lint */
36321936Shselasky#include <sys/cdefs.h>
37321936Shselasky__FBSDID("$FreeBSD: stable/11/lib/libc/db/btree/bt_conv.c 331722 2018-03-29 02:50:57Z eadler $");
38321936Shselasky
39321936Shselasky#include <sys/param.h>
40321936Shselasky
41321936Shselasky#include <stdio.h>
42321936Shselasky
43321936Shselasky#include <db.h>
44321936Shselasky#include "btree.h"
45321936Shselasky
46321936Shselaskystatic void mswap(PAGE *);
47321936Shselasky
48321936Shselasky/*
49321936Shselasky * __BT_BPGIN, __BT_BPGOUT --
50321936Shselasky *	Convert host-specific number layout to/from the host-independent
51321936Shselasky *	format stored on disk.
52321936Shselasky *
53321936Shselasky * Parameters:
54321936Shselasky *	t:	tree
55321936Shselasky *	pg:	page number
56321936Shselasky *	h:	page to convert
57321936Shselasky */
58321936Shselaskyvoid
59321936Shselasky__bt_pgin(void *t, pgno_t pg, void *pp)
60321936Shselasky{
61321936Shselasky	PAGE *h;
62321936Shselasky	indx_t i, top;
63321936Shselasky	u_char flags;
64321936Shselasky	char *p;
65321936Shselasky
66321936Shselasky	if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
67321936Shselasky		return;
68321936Shselasky	if (pg == P_META) {
69321936Shselasky		mswap(pp);
70321936Shselasky		return;
71321936Shselasky	}
72321936Shselasky
73321936Shselasky	h = pp;
74321936Shselasky	M_32_SWAP(h->pgno);
75321936Shselasky	M_32_SWAP(h->prevpg);
76321936Shselasky	M_32_SWAP(h->nextpg);
77321936Shselasky	M_32_SWAP(h->flags);
78321936Shselasky	M_16_SWAP(h->lower);
79321936Shselasky	M_16_SWAP(h->upper);
80321936Shselasky
81321936Shselasky	top = NEXTINDEX(h);
82321936Shselasky	if ((h->flags & P_TYPE) == P_BINTERNAL)
83321936Shselasky		for (i = 0; i < top; i++) {
84321936Shselasky			M_16_SWAP(h->linp[i]);
85321936Shselasky			p = (char *)GETBINTERNAL(h, i);
86321936Shselasky			P_32_SWAP(p);
87321936Shselasky			p += sizeof(u_int32_t);
88321936Shselasky			P_32_SWAP(p);
89321936Shselasky			p += sizeof(pgno_t);
90321936Shselasky			if (*(u_char *)p & P_BIGKEY) {
91321936Shselasky				p += sizeof(u_char);
92321936Shselasky				P_32_SWAP(p);
93321936Shselasky				p += sizeof(pgno_t);
94321936Shselasky				P_32_SWAP(p);
95321936Shselasky			}
96321936Shselasky		}
97321936Shselasky	else if ((h->flags & P_TYPE) == P_BLEAF)
98321936Shselasky		for (i = 0; i < top; i++) {
99321936Shselasky			M_16_SWAP(h->linp[i]);
100321936Shselasky			p = (char *)GETBLEAF(h, i);
101321936Shselasky			P_32_SWAP(p);
102321936Shselasky			p += sizeof(u_int32_t);
103321936Shselasky			P_32_SWAP(p);
104321936Shselasky			p += sizeof(u_int32_t);
105321936Shselasky			flags = *(u_char *)p;
106321936Shselasky			if (flags & (P_BIGKEY | P_BIGDATA)) {
107321936Shselasky				p += sizeof(u_char);
108321936Shselasky				if (flags & P_BIGKEY) {
109321936Shselasky					P_32_SWAP(p);
110321936Shselasky					p += sizeof(pgno_t);
111321936Shselasky					P_32_SWAP(p);
112321936Shselasky				}
113321936Shselasky				if (flags & P_BIGDATA) {
114321936Shselasky					p += sizeof(u_int32_t);
115321936Shselasky					P_32_SWAP(p);
116321936Shselasky					p += sizeof(pgno_t);
117321936Shselasky					P_32_SWAP(p);
118321936Shselasky				}
119321936Shselasky			}
120321936Shselasky		}
121321936Shselasky}
122321936Shselasky
123321936Shselaskyvoid
124321936Shselasky__bt_pgout(void *t, pgno_t pg, void *pp)
125321936Shselasky{
126321936Shselasky	PAGE *h;
127321936Shselasky	indx_t i, top;
128321936Shselasky	u_char flags;
129321936Shselasky	char *p;
130321936Shselasky
131321936Shselasky	if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
132321936Shselasky		return;
133321936Shselasky	if (pg == P_META) {
134321936Shselasky		mswap(pp);
135321936Shselasky		return;
136321936Shselasky	}
137321936Shselasky
138321936Shselasky	h = pp;
139321936Shselasky	top = NEXTINDEX(h);
140321936Shselasky	if ((h->flags & P_TYPE) == P_BINTERNAL)
141321936Shselasky		for (i = 0; i < top; i++) {
142321936Shselasky			p = (char *)GETBINTERNAL(h, i);
143321936Shselasky			P_32_SWAP(p);
144321936Shselasky			p += sizeof(u_int32_t);
145321936Shselasky			P_32_SWAP(p);
146321936Shselasky			p += sizeof(pgno_t);
147321936Shselasky			if (*(u_char *)p & P_BIGKEY) {
148321936Shselasky				p += sizeof(u_char);
149321936Shselasky				P_32_SWAP(p);
150321936Shselasky				p += sizeof(pgno_t);
151321936Shselasky				P_32_SWAP(p);
152321936Shselasky			}
153321936Shselasky			M_16_SWAP(h->linp[i]);
154321936Shselasky		}
155321936Shselasky	else if ((h->flags & P_TYPE) == P_BLEAF)
156321936Shselasky		for (i = 0; i < top; i++) {
157321936Shselasky			p = (char *)GETBLEAF(h, i);
158321936Shselasky			P_32_SWAP(p);
159321936Shselasky			p += sizeof(u_int32_t);
160321936Shselasky			P_32_SWAP(p);
161321936Shselasky			p += sizeof(u_int32_t);
162321936Shselasky			flags = *(u_char *)p;
163321936Shselasky			if (flags & (P_BIGKEY | P_BIGDATA)) {
164321936Shselasky				p += sizeof(u_char);
165321936Shselasky				if (flags & P_BIGKEY) {
166321936Shselasky					P_32_SWAP(p);
167321936Shselasky					p += sizeof(pgno_t);
168321936Shselasky					P_32_SWAP(p);
169321936Shselasky				}
170321936Shselasky				if (flags & P_BIGDATA) {
171321936Shselasky					p += sizeof(u_int32_t);
172321936Shselasky					P_32_SWAP(p);
173321936Shselasky					p += sizeof(pgno_t);
174321936Shselasky					P_32_SWAP(p);
175321936Shselasky				}
176321936Shselasky			}
177321936Shselasky			M_16_SWAP(h->linp[i]);
178321936Shselasky		}
179321936Shselasky
180321936Shselasky	M_32_SWAP(h->pgno);
181321936Shselasky	M_32_SWAP(h->prevpg);
182321936Shselasky	M_32_SWAP(h->nextpg);
183321936Shselasky	M_32_SWAP(h->flags);
184321936Shselasky	M_16_SWAP(h->lower);
185321936Shselasky	M_16_SWAP(h->upper);
186321936Shselasky}
187321936Shselasky
188321936Shselasky/*
189321936Shselasky * MSWAP -- Actually swap the bytes on the meta page.
190321936Shselasky *
191321936Shselasky * Parameters:
192321936Shselasky *	p:	page to convert
193321936Shselasky */
194321936Shselaskystatic void
195321936Shselaskymswap(PAGE *pg)
196321936Shselasky{
197321936Shselasky	char *p;
198321936Shselasky
199321936Shselasky	p = (char *)pg;
200321936Shselasky	P_32_SWAP(p);		/* magic */
201321936Shselasky	p += sizeof(u_int32_t);
202321936Shselasky	P_32_SWAP(p);		/* version */
203321936Shselasky	p += sizeof(u_int32_t);
204321936Shselasky	P_32_SWAP(p);		/* psize */
205321936Shselasky	p += sizeof(u_int32_t);
206321936Shselasky	P_32_SWAP(p);		/* free */
207321936Shselasky	p += sizeof(u_int32_t);
208321936Shselasky	P_32_SWAP(p);		/* nrecs */
209321936Shselasky	p += sizeof(u_int32_t);
210321936Shselasky	P_32_SWAP(p);		/* flags */
211321936Shselasky	p += sizeof(u_int32_t);
212321936Shselasky}
213321936Shselasky