xref: /dragonfly/lib/libc/db/btree/bt_conv.c (revision 71126e33)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * @(#)bt_conv.c	8.5 (Berkeley) 8/17/94
37  * $DragonFly: src/lib/libc/db/btree/bt_conv.c,v 1.4 2003/11/12 20:21:22 eirikn Exp $
38  */
39 
40 #include <sys/param.h>
41 
42 #include <stdio.h>
43 
44 #include <db.h>
45 #include "btree.h"
46 
47 static void mswap (PAGE *);
48 
49 /*
50  * __BT_BPGIN, __BT_BPGOUT --
51  *	Convert host-specific number layout to/from the host-independent
52  *	format stored on disk.
53  *
54  * Parameters:
55  *	t:	tree
56  *	pg:	page number
57  *	h:	page to convert
58  */
59 void
60 __bt_pgin(t, pg, pp)
61 	void *t;
62 	pgno_t pg;
63 	void *pp;
64 {
65 	PAGE *h;
66 	indx_t i, top;
67 	u_char 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 *)GETBINTERNAL(h, i);
90 			P_32_SWAP(p);
91 			p += sizeof(u_int32_t);
92 			P_32_SWAP(p);
93 			p += sizeof(pgno_t);
94 			if (*(u_char *)p & P_BIGKEY) {
95 				p += sizeof(u_char);
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 *)GETBLEAF(h, i);
105 			P_32_SWAP(p);
106 			p += sizeof(u_int32_t);
107 			P_32_SWAP(p);
108 			p += sizeof(u_int32_t);
109 			flags = *(u_char *)p;
110 			if (flags & (P_BIGKEY | P_BIGDATA)) {
111 				p += sizeof(u_char);
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(u_int32_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(t, pg, pp)
129 	void *t;
130 	pgno_t pg;
131 	void *pp;
132 {
133 	PAGE *h;
134 	indx_t i, top;
135 	u_char flags;
136 	char *p;
137 
138 	if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
139 		return;
140 	if (pg == P_META) {
141 		mswap(pp);
142 		return;
143 	}
144 
145 	h = pp;
146 	top = NEXTINDEX(h);
147 	if ((h->flags & P_TYPE) == P_BINTERNAL)
148 		for (i = 0; i < top; i++) {
149 			p = (char *)GETBINTERNAL(h, i);
150 			P_32_SWAP(p);
151 			p += sizeof(u_int32_t);
152 			P_32_SWAP(p);
153 			p += sizeof(pgno_t);
154 			if (*(u_char *)p & P_BIGKEY) {
155 				p += sizeof(u_char);
156 				P_32_SWAP(p);
157 				p += sizeof(pgno_t);
158 				P_32_SWAP(p);
159 			}
160 			M_16_SWAP(h->linp[i]);
161 		}
162 	else if ((h->flags & P_TYPE) == P_BLEAF)
163 		for (i = 0; i < top; i++) {
164 			p = (char *)GETBLEAF(h, i);
165 			P_32_SWAP(p);
166 			p += sizeof(u_int32_t);
167 			P_32_SWAP(p);
168 			p += sizeof(u_int32_t);
169 			flags = *(u_char *)p;
170 			if (flags & (P_BIGKEY | P_BIGDATA)) {
171 				p += sizeof(u_char);
172 				if (flags & P_BIGKEY) {
173 					P_32_SWAP(p);
174 					p += sizeof(pgno_t);
175 					P_32_SWAP(p);
176 				}
177 				if (flags & P_BIGDATA) {
178 					p += sizeof(u_int32_t);
179 					P_32_SWAP(p);
180 					p += sizeof(pgno_t);
181 					P_32_SWAP(p);
182 				}
183 			}
184 			M_16_SWAP(h->linp[i]);
185 		}
186 
187 	M_32_SWAP(h->pgno);
188 	M_32_SWAP(h->prevpg);
189 	M_32_SWAP(h->nextpg);
190 	M_32_SWAP(h->flags);
191 	M_16_SWAP(h->lower);
192 	M_16_SWAP(h->upper);
193 }
194 
195 /*
196  * MSWAP -- Actually swap the bytes on the meta page.
197  *
198  * Parameters:
199  *	p:	page to convert
200  */
201 static void
202 mswap(pg)
203 	PAGE *pg;
204 {
205 	char *p;
206 
207 	p = (char *)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