Merge branch 'vendor/OPENSSL'
[dragonfly.git] / lib / libc / db / btree / bt_conv.c
1 /*-
2  * Copyright (c) 1990, 1993, 1994
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  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * @(#)bt_conv.c        8.5 (Berkeley) 8/17/94
33  * $DragonFly: src/lib/libc/db/btree/bt_conv.c,v 1.6 2005/11/12 23:01:54 swildner Exp $
34  */
35
36 #include <sys/param.h>
37
38 #include <stdio.h>
39
40 #include <db.h>
41 #include "btree.h"
42
43 static void mswap (PAGE *);
44
45 /*
46  * __BT_BPGIN, __BT_BPGOUT --
47  *      Convert host-specific number layout to/from the host-independent
48  *      format stored on disk.
49  *
50  * Parameters:
51  *      t:      tree
52  *      pg:     page number
53  *      h:      page to convert
54  */
55 void
56 __bt_pgin(void *t, pgno_t pg, void *pp)
57 {
58         PAGE *h;
59         indx_t i, top;
60         u_char flags;
61         char *p;
62
63         if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
64                 return;
65         if (pg == P_META) {
66                 mswap(pp);
67                 return;
68         }
69
70         h = pp;
71         M_32_SWAP(h->pgno);
72         M_32_SWAP(h->prevpg);
73         M_32_SWAP(h->nextpg);
74         M_32_SWAP(h->flags);
75         M_16_SWAP(h->lower);
76         M_16_SWAP(h->upper);
77
78         top = NEXTINDEX(h);
79         if ((h->flags & P_TYPE) == P_BINTERNAL)
80                 for (i = 0; i < top; i++) {
81                         M_16_SWAP(h->linp[i]);
82                         p = (char *)GETBINTERNAL(h, i);
83                         P_32_SWAP(p);
84                         p += sizeof(u_int32_t);
85                         P_32_SWAP(p);
86                         p += sizeof(pgno_t);
87                         if (*(u_char *)p & P_BIGKEY) {
88                                 p += sizeof(u_char);
89                                 P_32_SWAP(p);
90                                 p += sizeof(pgno_t);
91                                 P_32_SWAP(p);
92                         }
93                 }
94         else if ((h->flags & P_TYPE) == P_BLEAF)
95                 for (i = 0; i < top; i++) {
96                         M_16_SWAP(h->linp[i]);
97                         p = (char *)GETBLEAF(h, i);
98                         P_32_SWAP(p);
99                         p += sizeof(u_int32_t);
100                         P_32_SWAP(p);
101                         p += sizeof(u_int32_t);
102                         flags = *(u_char *)p;
103                         if (flags & (P_BIGKEY | P_BIGDATA)) {
104                                 p += sizeof(u_char);
105                                 if (flags & P_BIGKEY) {
106                                         P_32_SWAP(p);
107                                         p += sizeof(pgno_t);
108                                         P_32_SWAP(p);
109                                 }
110                                 if (flags & P_BIGDATA) {
111                                         p += sizeof(u_int32_t);
112                                         P_32_SWAP(p);
113                                         p += sizeof(pgno_t);
114                                         P_32_SWAP(p);
115                                 }
116                         }
117                 }
118 }
119
120 void
121 __bt_pgout(void *t, pgno_t pg, void *pp)
122 {
123         PAGE *h;
124         indx_t i, top;
125         u_char flags;
126         char *p;
127
128         if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))
129                 return;
130         if (pg == P_META) {
131                 mswap(pp);
132                 return;
133         }
134
135         h = pp;
136         top = NEXTINDEX(h);
137         if ((h->flags & P_TYPE) == P_BINTERNAL)
138                 for (i = 0; i < top; i++) {
139                         p = (char *)GETBINTERNAL(h, i);
140                         P_32_SWAP(p);
141                         p += sizeof(u_int32_t);
142                         P_32_SWAP(p);
143                         p += sizeof(pgno_t);
144                         if (*(u_char *)p & P_BIGKEY) {
145                                 p += sizeof(u_char);
146                                 P_32_SWAP(p);
147                                 p += sizeof(pgno_t);
148                                 P_32_SWAP(p);
149                         }
150                         M_16_SWAP(h->linp[i]);
151                 }
152         else if ((h->flags & P_TYPE) == P_BLEAF)
153                 for (i = 0; i < top; i++) {
154                         p = (char *)GETBLEAF(h, i);
155                         P_32_SWAP(p);
156                         p += sizeof(u_int32_t);
157                         P_32_SWAP(p);
158                         p += sizeof(u_int32_t);
159                         flags = *(u_char *)p;
160                         if (flags & (P_BIGKEY | P_BIGDATA)) {
161                                 p += sizeof(u_char);
162                                 if (flags & P_BIGKEY) {
163                                         P_32_SWAP(p);
164                                         p += sizeof(pgno_t);
165                                         P_32_SWAP(p);
166                                 }
167                                 if (flags & P_BIGDATA) {
168                                         p += sizeof(u_int32_t);
169                                         P_32_SWAP(p);
170                                         p += sizeof(pgno_t);
171                                         P_32_SWAP(p);
172                                 }
173                         }
174                         M_16_SWAP(h->linp[i]);
175                 }
176
177         M_32_SWAP(h->pgno);
178         M_32_SWAP(h->prevpg);
179         M_32_SWAP(h->nextpg);
180         M_32_SWAP(h->flags);
181         M_16_SWAP(h->lower);
182         M_16_SWAP(h->upper);
183 }
184
185 /*
186  * MSWAP -- Actually swap the bytes on the meta page.
187  *
188  * Parameters:
189  *      p:      page to convert
190  */
191 static void
192 mswap(PAGE *pg)
193 {
194         char *p;
195
196         p = (char *)pg;
197         P_32_SWAP(p);           /* magic */
198         p += sizeof(u_int32_t);
199         P_32_SWAP(p);           /* version */
200         p += sizeof(u_int32_t);
201         P_32_SWAP(p);           /* psize */
202         p += sizeof(u_int32_t);
203         P_32_SWAP(p);           /* free */
204         p += sizeof(u_int32_t);
205         P_32_SWAP(p);           /* nrecs */
206         p += sizeof(u_int32_t);
207         P_32_SWAP(p);           /* flags */
208         p += sizeof(u_int32_t);
209 }