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