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