]> git.saurik.com Git - apple/libc.git/blame - db/btree/bt_conv.c
Libc-262.2.12.tar.gz
[apple/libc.git] / db / btree / bt_conv.c
CommitLineData
e9ce8d39
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
734aad71 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
e9ce8d39 7 *
734aad71
A
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
e9ce8d39
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
734aad71
A
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
e9ce8d39
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/*
26 * Copyright (c) 1990, 1993
27 * The Regents of the University of California. All rights reserved.
28 *
29 * This code is derived from software contributed to Berkeley by
30 * Mike Olson.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61
62#include <sys/param.h>
63
64#include <stdio.h>
65
66#include <db.h>
67#include "btree.h"
68
69static void mswap __P((PAGE *));
70
71/*
72 * __BT_BPGIN, __BT_BPGOUT --
73 * Convert host-specific number layout to/from the host-independent
74 * format stored on disk.
75 *
76 * Parameters:
77 * t: tree
78 * pg: page number
79 * h: page to convert
80 */
81void
82__bt_pgin(t, pg, pp)
83 void *t;
84 pgno_t pg;
85 void *pp;
86{
87 PAGE *h;
88 indx_t i, top;
89 u_char flags;
90 char *p;
91
92 if (!ISSET(((BTREE *)t), B_NEEDSWAP))
93 return;
94 if (pg == P_META) {
95 mswap(pp);
96 return;
97 }
98
99 h = pp;
100 M_32_SWAP(h->pgno);
101 M_32_SWAP(h->prevpg);
102 M_32_SWAP(h->nextpg);
103 M_32_SWAP(h->flags);
104 M_16_SWAP(h->lower);
105 M_16_SWAP(h->upper);
106
107 top = NEXTINDEX(h);
108 if ((h->flags & P_TYPE) == P_BINTERNAL)
109 for (i = 0; i < top; i++) {
110 M_16_SWAP(h->linp[i]);
111 p = (char *)GETBINTERNAL(h, i);
112 P_32_SWAP(p);
113 p += sizeof(size_t);
114 P_32_SWAP(p);
115 p += sizeof(pgno_t);
116 if (*(u_char *)p & P_BIGKEY) {
117 p += sizeof(u_char);
118 P_32_SWAP(p);
119 p += sizeof(pgno_t);
120 P_32_SWAP(p);
121 }
122 }
123 else if ((h->flags & P_TYPE) == P_BLEAF)
124 for (i = 0; i < top; i++) {
125 M_16_SWAP(h->linp[i]);
126 p = (char *)GETBLEAF(h, i);
127 P_32_SWAP(p);
128 p += sizeof(size_t);
129 P_32_SWAP(p);
130 p += sizeof(size_t);
131 flags = *(u_char *)p;
132 if (flags & (P_BIGKEY | P_BIGDATA)) {
133 p += sizeof(u_char);
134 if (flags & P_BIGKEY) {
135 P_32_SWAP(p);
136 p += sizeof(pgno_t);
137 P_32_SWAP(p);
138 }
139 if (flags & P_BIGDATA) {
140 p += sizeof(size_t);
141 P_32_SWAP(p);
142 p += sizeof(pgno_t);
143 P_32_SWAP(p);
144 }
145 }
146 }
147}
148
149void
150__bt_pgout(t, pg, pp)
151 void *t;
152 pgno_t pg;
153 void *pp;
154{
155 PAGE *h;
156 indx_t i, top;
157 u_char flags;
158 char *p;
159
160 if (!ISSET(((BTREE *)t), B_NEEDSWAP))
161 return;
162 if (pg == P_META) {
163 mswap(pp);
164 return;
165 }
166
167 h = pp;
168 top = NEXTINDEX(h);
169 if ((h->flags & P_TYPE) == P_BINTERNAL)
170 for (i = 0; i < top; i++) {
171 p = (char *)GETBINTERNAL(h, i);
172 P_32_SWAP(p);
173 p += sizeof(size_t);
174 P_32_SWAP(p);
175 p += sizeof(pgno_t);
176 if (*(u_char *)p & P_BIGKEY) {
177 p += sizeof(u_char);
178 P_32_SWAP(p);
179 p += sizeof(pgno_t);
180 P_32_SWAP(p);
181 }
182 M_16_SWAP(h->linp[i]);
183 }
184 else if ((h->flags & P_TYPE) == P_BLEAF)
185 for (i = 0; i < top; i++) {
186 p = (char *)GETBLEAF(h, i);
187 P_32_SWAP(p);
188 p += sizeof(size_t);
189 P_32_SWAP(p);
190 p += sizeof(size_t);
191 flags = *(u_char *)p;
192 if (flags & (P_BIGKEY | P_BIGDATA)) {
193 p += sizeof(u_char);
194 if (flags & P_BIGKEY) {
195 P_32_SWAP(p);
196 p += sizeof(pgno_t);
197 P_32_SWAP(p);
198 }
199 if (flags & P_BIGDATA) {
200 p += sizeof(size_t);
201 P_32_SWAP(p);
202 p += sizeof(pgno_t);
203 P_32_SWAP(p);
204 }
205 }
206 M_16_SWAP(h->linp[i]);
207 }
208
209 M_32_SWAP(h->pgno);
210 M_32_SWAP(h->prevpg);
211 M_32_SWAP(h->nextpg);
212 M_32_SWAP(h->flags);
213 M_16_SWAP(h->lower);
214 M_16_SWAP(h->upper);
215}
216
217/*
218 * MSWAP -- Actually swap the bytes on the meta page.
219 *
220 * Parameters:
221 * p: page to convert
222 */
223static void
224mswap(pg)
225 PAGE *pg;
226{
227 char *p;
228
229 p = (char *)pg;
230 P_32_SWAP(p); /* m_magic */
231 p += sizeof(u_int32_t);
232 P_32_SWAP(p); /* m_version */
233 p += sizeof(u_int32_t);
234 P_32_SWAP(p); /* m_psize */
235 p += sizeof(u_int32_t);
236 P_32_SWAP(p); /* m_free */
237 p += sizeof(u_int32_t);
238 P_32_SWAP(p); /* m_nrecs */
239 p += sizeof(u_int32_t);
240 P_32_SWAP(p); /* m_flags */
241 p += sizeof(u_int32_t);
242}