release.sh: restore -jJAILDIR option
[minix.git] / lib / libc / db / btree / bt_conv.c
blob2c5217a39102e32b0f08c58226eb4c5b2c5b9bf0
1 /* $NetBSD: bt_conv.c,v 1.14 2008/09/10 17:52:35 joerg Exp $ */
3 /*-
4 * Copyright (c) 1990, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Mike Olson.
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.
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.
35 #if HAVE_NBTOOL_CONFIG_H
36 #include "nbtool_config.h"
37 #endif
39 #include <sys/cdefs.h>
40 __RCSID("$NetBSD: bt_conv.c,v 1.14 2008/09/10 17:52:35 joerg Exp $");
42 #include <assert.h>
43 #include <stdio.h>
45 #include <db.h>
46 #include "btree.h"
48 static void mswap(PAGE *);
51 * __BT_BPGIN, __BT_BPGOUT --
52 * Convert host-specific number layout to/from the host-independent
53 * format stored on disk.
55 * Parameters:
56 * t: tree
57 * pg: page number
58 * h: page to convert
60 void
61 __bt_pgin(void *t, pgno_t pg, void *pp)
63 PAGE *h;
64 indx_t i, top;
65 uint8_t flags;
66 char *p;
68 if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
69 return;
70 if (pg == P_META) {
71 mswap(pp);
72 return;
75 h = pp;
76 M_32_SWAP(h->pgno);
77 M_32_SWAP(h->prevpg);
78 M_32_SWAP(h->nextpg);
79 M_32_SWAP(h->flags);
80 M_16_SWAP(h->lower);
81 M_16_SWAP(h->upper);
83 top = NEXTINDEX(h);
84 if ((h->flags & P_TYPE) == P_BINTERNAL)
85 for (i = 0; i < top; i++) {
86 M_16_SWAP(h->linp[i]);
87 p = (char *)(void *)GETBINTERNAL(h, i);
88 P_32_SWAP(p);
89 p += sizeof(uint32_t);
90 P_32_SWAP(p);
91 p += sizeof(pgno_t);
92 if (*(uint8_t *)p & P_BIGKEY) {
93 p += sizeof(uint8_t);
94 P_32_SWAP(p);
95 p += sizeof(pgno_t);
96 P_32_SWAP(p);
99 else if ((h->flags & P_TYPE) == P_BLEAF)
100 for (i = 0; i < top; i++) {
101 M_16_SWAP(h->linp[i]);
102 p = (char *)(void *)GETBLEAF(h, i);
103 P_32_SWAP(p);
104 p += sizeof(uint32_t);
105 P_32_SWAP(p);
106 p += sizeof(uint32_t);
107 flags = *(uint8_t *)p;
108 if (flags & (P_BIGKEY | P_BIGDATA)) {
109 p += sizeof(uint8_t);
110 if (flags & P_BIGKEY) {
111 P_32_SWAP(p);
112 p += sizeof(pgno_t);
113 P_32_SWAP(p);
115 if (flags & P_BIGDATA) {
116 p += sizeof(uint32_t);
117 P_32_SWAP(p);
118 p += sizeof(pgno_t);
119 P_32_SWAP(p);
125 void
126 __bt_pgout(void *t, pgno_t pg, void *pp)
128 PAGE *h;
129 indx_t i, top;
130 uint8_t flags;
131 char *p;
133 if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
134 return;
135 if (pg == P_META) {
136 mswap(pp);
137 return;
140 h = pp;
141 top = NEXTINDEX(h);
142 if ((h->flags & P_TYPE) == P_BINTERNAL)
143 for (i = 0; i < top; i++) {
144 p = (char *)(void *)GETBINTERNAL(h, i);
145 P_32_SWAP(p);
146 p += sizeof(uint32_t);
147 P_32_SWAP(p);
148 p += sizeof(pgno_t);
149 if (*(uint8_t *)p & P_BIGKEY) {
150 p += sizeof(uint8_t);
151 P_32_SWAP(p);
152 p += sizeof(pgno_t);
153 P_32_SWAP(p);
155 M_16_SWAP(h->linp[i]);
157 else if ((h->flags & P_TYPE) == P_BLEAF)
158 for (i = 0; i < top; i++) {
159 p = (char *)(void *)GETBLEAF(h, i);
160 P_32_SWAP(p);
161 p += sizeof(uint32_t);
162 P_32_SWAP(p);
163 p += sizeof(uint32_t);
164 flags = *(uint8_t *)p;
165 if (flags & (P_BIGKEY | P_BIGDATA)) {
166 p += sizeof(uint8_t);
167 if (flags & P_BIGKEY) {
168 P_32_SWAP(p);
169 p += sizeof(pgno_t);
170 P_32_SWAP(p);
172 if (flags & P_BIGDATA) {
173 p += sizeof(uint32_t);
174 P_32_SWAP(p);
175 p += sizeof(pgno_t);
176 P_32_SWAP(p);
179 M_16_SWAP(h->linp[i]);
182 M_32_SWAP(h->pgno);
183 M_32_SWAP(h->prevpg);
184 M_32_SWAP(h->nextpg);
185 M_32_SWAP(h->flags);
186 M_16_SWAP(h->lower);
187 M_16_SWAP(h->upper);
191 * MSWAP -- Actually swap the bytes on the meta page.
193 * Parameters:
194 * p: page to convert
196 static void
197 mswap(PAGE *pg)
199 char *p;
201 p = (char *)(void *)pg;
202 P_32_SWAP(p); /* magic */
203 p += sizeof(uint32_t);
204 P_32_SWAP(p); /* version */
205 p += sizeof(uint32_t);
206 P_32_SWAP(p); /* psize */
207 p += sizeof(uint32_t);
208 P_32_SWAP(p); /* free */
209 p += sizeof(uint32_t);
210 P_32_SWAP(p); /* nrecs */
211 p += sizeof(uint32_t);
212 P_32_SWAP(p); /* flags */
213 p += sizeof(uint32_t);