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