xref: /freebsd/lib/libc/db/btree/bt_conv.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
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 defined(LIBC_SCCS) && !defined(lint)
36 static char sccsid[] = "@(#)bt_conv.c	8.5 (Berkeley) 8/17/94";
37 #endif /* LIBC_SCCS and not lint */
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <sys/param.h>
42 
43 #include <stdio.h>
44 
45 #include <db.h>
46 #include "btree.h"
47 
48 static void mswap(PAGE *);
49 
50 /*
51  * __BT_BPGIN, __BT_BPGOUT --
52  *	Convert host-specific number layout to/from the host-independent
53  *	format stored on disk.
54  *
55  * Parameters:
56  *	t:	tree
57  *	pg:	page number
58  *	h:	page to convert
59  */
60 void
61 __bt_pgin(void *t, pgno_t pg, void *pp)
62 {
63 	PAGE *h;
64 	indx_t i, top;
65 	u_char flags;
66 	char *p;
67 
68 	if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
69 		return;
70 	if (pg == P_META) {
71 		mswap(pp);
72 		return;
73 	}
74 
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);
82 
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 *)GETBINTERNAL(h, i);
88 			P_32_SWAP(p);
89 			p += sizeof(u_int32_t);
90 			P_32_SWAP(p);
91 			p += sizeof(pgno_t);
92 			if (*(u_char *)p & P_BIGKEY) {
93 				p += sizeof(u_char);
94 				P_32_SWAP(p);
95 				p += sizeof(pgno_t);
96 				P_32_SWAP(p);
97 			}
98 		}
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 *)GETBLEAF(h, i);
103 			P_32_SWAP(p);
104 			p += sizeof(u_int32_t);
105 			P_32_SWAP(p);
106 			p += sizeof(u_int32_t);
107 			flags = *(u_char *)p;
108 			if (flags & (P_BIGKEY | P_BIGDATA)) {
109 				p += sizeof(u_char);
110 				if (flags & P_BIGKEY) {
111 					P_32_SWAP(p);
112 					p += sizeof(pgno_t);
113 					P_32_SWAP(p);
114 				}
115 				if (flags & P_BIGDATA) {
116 					p += sizeof(u_int32_t);
117 					P_32_SWAP(p);
118 					p += sizeof(pgno_t);
119 					P_32_SWAP(p);
120 				}
121 			}
122 		}
123 }
124 
125 void
126 __bt_pgout(void *t, pgno_t pg, void *pp)
127 {
128 	PAGE *h;
129 	indx_t i, top;
130 	u_char flags;
131 	char *p;
132 
133 	if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
134 		return;
135 	if (pg == P_META) {
136 		mswap(pp);
137 		return;
138 	}
139 
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 *)GETBINTERNAL(h, i);
145 			P_32_SWAP(p);
146 			p += sizeof(u_int32_t);
147 			P_32_SWAP(p);
148 			p += sizeof(pgno_t);
149 			if (*(u_char *)p & P_BIGKEY) {
150 				p += sizeof(u_char);
151 				P_32_SWAP(p);
152 				p += sizeof(pgno_t);
153 				P_32_SWAP(p);
154 			}
155 			M_16_SWAP(h->linp[i]);
156 		}
157 	else if ((h->flags & P_TYPE) == P_BLEAF)
158 		for (i = 0; i < top; i++) {
159 			p = (char *)GETBLEAF(h, i);
160 			P_32_SWAP(p);
161 			p += sizeof(u_int32_t);
162 			P_32_SWAP(p);
163 			p += sizeof(u_int32_t);
164 			flags = *(u_char *)p;
165 			if (flags & (P_BIGKEY | P_BIGDATA)) {
166 				p += sizeof(u_char);
167 				if (flags & P_BIGKEY) {
168 					P_32_SWAP(p);
169 					p += sizeof(pgno_t);
170 					P_32_SWAP(p);
171 				}
172 				if (flags & P_BIGDATA) {
173 					p += sizeof(u_int32_t);
174 					P_32_SWAP(p);
175 					p += sizeof(pgno_t);
176 					P_32_SWAP(p);
177 				}
178 			}
179 			M_16_SWAP(h->linp[i]);
180 		}
181 
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);
188 }
189 
190 /*
191  * MSWAP -- Actually swap the bytes on the meta page.
192  *
193  * Parameters:
194  *	p:	page to convert
195  */
196 static void
197 mswap(PAGE *pg)
198 {
199 	char *p;
200 
201 	p = (char *)pg;
202 	P_32_SWAP(p);		/* magic */
203 	p += sizeof(u_int32_t);
204 	P_32_SWAP(p);		/* version */
205 	p += sizeof(u_int32_t);
206 	P_32_SWAP(p);		/* psize */
207 	p += sizeof(u_int32_t);
208 	P_32_SWAP(p);		/* free */
209 	p += sizeof(u_int32_t);
210 	P_32_SWAP(p);		/* nrecs */
211 	p += sizeof(u_int32_t);
212 	P_32_SWAP(p);		/* flags */
213 	p += sizeof(u_int32_t);
214 }
215