1 #pragma ident	"%Z%%M%	%I%	%E% SMI"
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 static char sccsid[] = "@(#)bt_conv.c	8.5 (Berkeley) 8/17/94";
41 #endif /* LIBC_SCCS and not lint */
42 
43 #include <sys/param.h>
44 
45 #include <stdio.h>
46 
47 #include "db-int.h"
48 #include "btree.h"
49 
50 static void mswap __P((PAGE *));
51 
52 /*
53  * __BT_BPGIN, __BT_BPGOUT --
54  *	Convert host-specific number layout to/from the host-independent
55  *	format stored on disk.
56  *
57  * Parameters:
58  *	t:	tree
59  *	pg:	page number
60  *	h:	page to convert
61  */
62 void
__bt_pgin(t,pg,pp)63 __bt_pgin(t, pg, pp)
64 	void *t;
65 	db_pgno_t pg;
66 	void *pp;
67 {
68 	PAGE *h;
69 	indx_t i, top;
70 	u_char flags;
71 	char *p;
72 
73 	if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
74 		return;
75 	if (pg == P_META) {
76 		mswap(pp);
77 		return;
78 	}
79 
80 	h = pp;
81 	M_32_SWAP(h->pgno);
82 	M_32_SWAP(h->prevpg);
83 	M_32_SWAP(h->nextpg);
84 	M_32_SWAP(h->flags);
85 	M_16_SWAP(h->lower);
86 	M_16_SWAP(h->upper);
87 
88 	top = NEXTINDEX(h);
89 	if ((h->flags & P_TYPE) == P_BINTERNAL)
90 		for (i = 0; i < top; i++) {
91 			M_16_SWAP(h->linp[i]);
92 			p = (char *)GETBINTERNAL(h, i);
93 			P_32_SWAP(p);
94 			p += sizeof(u_int32_t);
95 			P_32_SWAP(p);
96 			p += sizeof(db_pgno_t);
97 			if (*(u_char *)p & P_BIGKEY) {
98 				p += sizeof(u_char);
99 				P_32_SWAP(p);
100 				p += sizeof(db_pgno_t);
101 				P_32_SWAP(p);
102 			}
103 		}
104 	else if ((h->flags & P_TYPE) == P_BLEAF)
105 		for (i = 0; i < top; i++) {
106 			M_16_SWAP(h->linp[i]);
107 			p = (char *)GETBLEAF(h, i);
108 			P_32_SWAP(p);
109 			p += sizeof(u_int32_t);
110 			P_32_SWAP(p);
111 			p += sizeof(u_int32_t);
112 			flags = *(u_char *)p;
113 			if (flags & (P_BIGKEY | P_BIGDATA)) {
114 				p += sizeof(u_char);
115 				if (flags & P_BIGKEY) {
116 					P_32_SWAP(p);
117 					p += sizeof(db_pgno_t);
118 					P_32_SWAP(p);
119 				}
120 				if (flags & P_BIGDATA) {
121 					p += sizeof(u_int32_t);
122 					P_32_SWAP(p);
123 					p += sizeof(db_pgno_t);
124 					P_32_SWAP(p);
125 				}
126 			}
127 		}
128 }
129 
130 void
__bt_pgout(t,pg,pp)131 __bt_pgout(t, pg, pp)
132 	void *t;
133 	db_pgno_t pg;
134 	void *pp;
135 {
136 	PAGE *h;
137 	indx_t i, top;
138 	u_char flags;
139 	char *p;
140 
141 	if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
142 		return;
143 	if (pg == P_META) {
144 		mswap(pp);
145 		return;
146 	}
147 
148 	h = pp;
149 	top = NEXTINDEX(h);
150 	if ((h->flags & P_TYPE) == P_BINTERNAL)
151 		for (i = 0; i < top; i++) {
152 			p = (char *)GETBINTERNAL(h, i);
153 			P_32_SWAP(p);
154 			p += sizeof(u_int32_t);
155 			P_32_SWAP(p);
156 			p += sizeof(db_pgno_t);
157 			if (*(u_char *)p & P_BIGKEY) {
158 				p += sizeof(u_char);
159 				P_32_SWAP(p);
160 				p += sizeof(db_pgno_t);
161 				P_32_SWAP(p);
162 			}
163 			M_16_SWAP(h->linp[i]);
164 		}
165 	else if ((h->flags & P_TYPE) == P_BLEAF)
166 		for (i = 0; i < top; i++) {
167 			p = (char *)GETBLEAF(h, i);
168 			P_32_SWAP(p);
169 			p += sizeof(u_int32_t);
170 			P_32_SWAP(p);
171 			p += sizeof(u_int32_t);
172 			flags = *(u_char *)p;
173 			if (flags & (P_BIGKEY | P_BIGDATA)) {
174 				p += sizeof(u_char);
175 				if (flags & P_BIGKEY) {
176 					P_32_SWAP(p);
177 					p += sizeof(db_pgno_t);
178 					P_32_SWAP(p);
179 				}
180 				if (flags & P_BIGDATA) {
181 					p += sizeof(u_int32_t);
182 					P_32_SWAP(p);
183 					p += sizeof(db_pgno_t);
184 					P_32_SWAP(p);
185 				}
186 			}
187 			M_16_SWAP(h->linp[i]);
188 		}
189 
190 	M_32_SWAP(h->pgno);
191 	M_32_SWAP(h->prevpg);
192 	M_32_SWAP(h->nextpg);
193 	M_32_SWAP(h->flags);
194 	M_16_SWAP(h->lower);
195 	M_16_SWAP(h->upper);
196 }
197 
198 /*
199  * MSWAP -- Actually swap the bytes on the meta page.
200  *
201  * Parameters:
202  *	p:	page to convert
203  */
204 static void
mswap(pg)205 mswap(pg)
206 	PAGE *pg;
207 {
208 	char *p;
209 
210 	p = (char *)pg;
211 	P_32_SWAP(p);		/* magic */
212 	p += sizeof(u_int32_t);
213 	P_32_SWAP(p);		/* version */
214 	p += sizeof(u_int32_t);
215 	P_32_SWAP(p);		/* psize */
216 	p += sizeof(u_int32_t);
217 	P_32_SWAP(p);		/* free */
218 	p += sizeof(u_int32_t);
219 	P_32_SWAP(p);		/* nrecs */
220 	p += sizeof(u_int32_t);
221 	P_32_SWAP(p);		/* flags */
222 	p += sizeof(u_int32_t);
223 }
224