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