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