1 /*-
2 * Copyright (c) 1990, 1993
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 * %sccs.include.redist.c%
9 */
10
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)bt_conv.c 8.2 (Berkeley) 02/21/94";
13 #endif /* LIBC_SCCS and not lint */
14
15 #include <sys/param.h>
16
17 #include <stdio.h>
18
19 #include <db.h>
20 #include "btree.h"
21
22 static void mswap __P((PAGE *));
23
24 /*
25 * __BT_BPGIN, __BT_BPGOUT --
26 * Convert host-specific number layout to/from the host-independent
27 * format stored on disk.
28 *
29 * Parameters:
30 * t: tree
31 * pg: page number
32 * h: page to convert
33 */
34 void
__bt_pgin(t,pg,pp)35 __bt_pgin(t, pg, pp)
36 void *t;
37 pgno_t pg;
38 void *pp;
39 {
40 PAGE *h;
41 indx_t i, top;
42 u_char flags;
43 char *p;
44
45 if (!ISSET(((BTREE *)t), B_NEEDSWAP))
46 return;
47 if (pg == P_META) {
48 mswap(pp);
49 return;
50 }
51
52 h = pp;
53 M_32_SWAP(h->pgno);
54 M_32_SWAP(h->prevpg);
55 M_32_SWAP(h->nextpg);
56 M_32_SWAP(h->flags);
57 M_16_SWAP(h->lower);
58 M_16_SWAP(h->upper);
59
60 top = NEXTINDEX(h);
61 if ((h->flags & P_TYPE) == P_BINTERNAL)
62 for (i = 0; i < top; i++) {
63 M_16_SWAP(h->linp[i]);
64 p = (char *)GETBINTERNAL(h, i);
65 P_32_SWAP(p);
66 p += sizeof(size_t);
67 P_32_SWAP(p);
68 p += sizeof(pgno_t);
69 if (*(u_char *)p & P_BIGKEY) {
70 p += sizeof(u_char);
71 P_32_SWAP(p);
72 p += sizeof(pgno_t);
73 P_32_SWAP(p);
74 }
75 }
76 else if ((h->flags & P_TYPE) == P_BLEAF)
77 for (i = 0; i < top; i++) {
78 M_16_SWAP(h->linp[i]);
79 p = (char *)GETBLEAF(h, i);
80 P_32_SWAP(p);
81 p += sizeof(size_t);
82 P_32_SWAP(p);
83 p += sizeof(size_t);
84 flags = *(u_char *)p;
85 if (flags & (P_BIGKEY | P_BIGDATA)) {
86 p += sizeof(u_char);
87 if (flags & P_BIGKEY) {
88 P_32_SWAP(p);
89 p += sizeof(pgno_t);
90 P_32_SWAP(p);
91 }
92 if (flags & P_BIGDATA) {
93 p += sizeof(size_t);
94 P_32_SWAP(p);
95 p += sizeof(pgno_t);
96 P_32_SWAP(p);
97 }
98 }
99 }
100 }
101
102 void
__bt_pgout(t,pg,pp)103 __bt_pgout(t, pg, pp)
104 void *t;
105 pgno_t pg;
106 void *pp;
107 {
108 PAGE *h;
109 indx_t i, top;
110 u_char flags;
111 char *p;
112
113 if (!ISSET(((BTREE *)t), B_NEEDSWAP))
114 return;
115 if (pg == P_META) {
116 mswap(pp);
117 return;
118 }
119
120 h = pp;
121 top = NEXTINDEX(h);
122 if ((h->flags & P_TYPE) == P_BINTERNAL)
123 for (i = 0; i < top; i++) {
124 p = (char *)GETBINTERNAL(h, i);
125 P_32_SWAP(p);
126 p += sizeof(size_t);
127 P_32_SWAP(p);
128 p += sizeof(pgno_t);
129 if (*(u_char *)p & P_BIGKEY) {
130 p += sizeof(u_char);
131 P_32_SWAP(p);
132 p += sizeof(pgno_t);
133 P_32_SWAP(p);
134 }
135 M_16_SWAP(h->linp[i]);
136 }
137 else if ((h->flags & P_TYPE) == P_BLEAF)
138 for (i = 0; i < top; i++) {
139 p = (char *)GETBLEAF(h, i);
140 P_32_SWAP(p);
141 p += sizeof(size_t);
142 P_32_SWAP(p);
143 p += sizeof(size_t);
144 flags = *(u_char *)p;
145 if (flags & (P_BIGKEY | P_BIGDATA)) {
146 p += sizeof(u_char);
147 if (flags & P_BIGKEY) {
148 P_32_SWAP(p);
149 p += sizeof(pgno_t);
150 P_32_SWAP(p);
151 }
152 if (flags & P_BIGDATA) {
153 p += sizeof(size_t);
154 P_32_SWAP(p);
155 p += sizeof(pgno_t);
156 P_32_SWAP(p);
157 }
158 }
159 M_16_SWAP(h->linp[i]);
160 }
161
162 M_32_SWAP(h->pgno);
163 M_32_SWAP(h->prevpg);
164 M_32_SWAP(h->nextpg);
165 M_32_SWAP(h->flags);
166 M_16_SWAP(h->lower);
167 M_16_SWAP(h->upper);
168 }
169
170 /*
171 * MSWAP -- Actually swap the bytes on the meta page.
172 *
173 * Parameters:
174 * p: page to convert
175 */
176 static void
mswap(pg)177 mswap(pg)
178 PAGE *pg;
179 {
180 char *p;
181
182 p = (char *)pg;
183 P_32_SWAP(p); /* m_magic */
184 p += sizeof(u_int32_t);
185 P_32_SWAP(p); /* m_version */
186 p += sizeof(u_int32_t);
187 P_32_SWAP(p); /* m_psize */
188 p += sizeof(u_int32_t);
189 P_32_SWAP(p); /* m_free */
190 p += sizeof(u_int32_t);
191 P_32_SWAP(p); /* m_nrecs */
192 p += sizeof(u_int32_t);
193 P_32_SWAP(p); /* m_flags */
194 p += sizeof(u_int32_t);
195 }
196