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