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