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