150990Sbostic /*-
261194Sbostic * Copyright (c) 1990, 1993
361194Sbostic * The Regents of the University of California. All rights reserved.
450990Sbostic *
550990Sbostic * This code is derived from software contributed to Berkeley by
650990Sbostic * Mike Olson.
750990Sbostic *
850990Sbostic * %sccs.include.redist.c%
950990Sbostic */
1050990Sbostic
1150990Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*66192Sbostic static char sccsid[] = "@(#)bt_conv.c 8.2 (Berkeley) 02/21/94";
1350990Sbostic #endif /* LIBC_SCCS and not lint */
1450990Sbostic
1550990Sbostic #include <sys/param.h>
1656734Sbostic
1750990Sbostic #include <stdio.h>
1856734Sbostic
1957932Sbostic #include <db.h>
2050990Sbostic #include "btree.h"
2150990Sbostic
2259631Sbostic static void mswap __P((PAGE *));
2351101Sbostic
2450990Sbostic /*
2550990Sbostic * __BT_BPGIN, __BT_BPGOUT --
2650990Sbostic * Convert host-specific number layout to/from the host-independent
2750990Sbostic * format stored on disk.
2850990Sbostic *
2950990Sbostic * Parameters:
3051101Sbostic * t: tree
3151101Sbostic * pg: page number
3250990Sbostic * h: page to convert
3350990Sbostic */
3450990Sbostic void
__bt_pgin(t,pg,pp)3559631Sbostic __bt_pgin(t, pg, pp)
3650990Sbostic void *t;
3750990Sbostic pgno_t pg;
3859631Sbostic void *pp;
3950990Sbostic {
4050990Sbostic PAGE *h;
41*66192Sbostic indx_t i, top;
4259631Sbostic u_char flags;
4359631Sbostic char *p;
4450990Sbostic
4560043Sbostic if (!ISSET(((BTREE *)t), B_NEEDSWAP))
4650990Sbostic return;
4759631Sbostic if (pg == P_META) {
4859631Sbostic mswap(pp);
4959631Sbostic return;
5059631Sbostic }
5150990Sbostic
5259631Sbostic h = pp;
53*66192Sbostic M_32_SWAP(h->pgno);
54*66192Sbostic M_32_SWAP(h->prevpg);
55*66192Sbostic M_32_SWAP(h->nextpg);
56*66192Sbostic M_32_SWAP(h->flags);
57*66192Sbostic M_16_SWAP(h->lower);
58*66192Sbostic M_16_SWAP(h->upper);
5959631Sbostic
6059631Sbostic top = NEXTINDEX(h);
6159631Sbostic if ((h->flags & P_TYPE) == P_BINTERNAL)
6259631Sbostic for (i = 0; i < top; i++) {
63*66192Sbostic M_16_SWAP(h->linp[i]);
6459631Sbostic p = (char *)GETBINTERNAL(h, i);
65*66192Sbostic P_32_SWAP(p);
6659631Sbostic p += sizeof(size_t);
67*66192Sbostic P_32_SWAP(p);
6859631Sbostic p += sizeof(pgno_t);
6959631Sbostic if (*(u_char *)p & P_BIGKEY) {
7059631Sbostic p += sizeof(u_char);
71*66192Sbostic P_32_SWAP(p);
7259631Sbostic p += sizeof(pgno_t);
73*66192Sbostic P_32_SWAP(p);
7459631Sbostic }
7559631Sbostic }
7659631Sbostic else if ((h->flags & P_TYPE) == P_BLEAF)
7759631Sbostic for (i = 0; i < top; i++) {
78*66192Sbostic M_16_SWAP(h->linp[i]);
7959631Sbostic p = (char *)GETBLEAF(h, i);
80*66192Sbostic P_32_SWAP(p);
8159631Sbostic p += sizeof(size_t);
82*66192Sbostic P_32_SWAP(p);
8359631Sbostic p += sizeof(size_t);
8459631Sbostic flags = *(u_char *)p;
8559631Sbostic if (flags & (P_BIGKEY | P_BIGDATA)) {
8659631Sbostic p += sizeof(u_char);
8759631Sbostic if (flags & P_BIGKEY) {
88*66192Sbostic P_32_SWAP(p);
8959631Sbostic p += sizeof(pgno_t);
90*66192Sbostic P_32_SWAP(p);
9159631Sbostic }
9259631Sbostic if (flags & P_BIGDATA) {
9359631Sbostic p += sizeof(size_t);
94*66192Sbostic P_32_SWAP(p);
9559631Sbostic p += sizeof(pgno_t);
96*66192Sbostic P_32_SWAP(p);
9759631Sbostic }
9859631Sbostic }
9959631Sbostic }
10050990Sbostic }
10150990Sbostic
10250990Sbostic void
__bt_pgout(t,pg,pp)10359631Sbostic __bt_pgout(t, pg, pp)
10450990Sbostic void *t;
10550990Sbostic pgno_t pg;
10659631Sbostic void *pp;
10750990Sbostic {
10850990Sbostic PAGE *h;
109*66192Sbostic indx_t i, top;
11059631Sbostic u_char flags;
11159631Sbostic char *p;
11250990Sbostic
11360043Sbostic if (!ISSET(((BTREE *)t), B_NEEDSWAP))
11450990Sbostic return;
11559631Sbostic if (pg == P_META) {
11659631Sbostic mswap(pp);
11759631Sbostic return;
11859631Sbostic }
11950990Sbostic
12059631Sbostic h = pp;
12150990Sbostic top = NEXTINDEX(h);
12259631Sbostic if ((h->flags & P_TYPE) == P_BINTERNAL)
12350990Sbostic for (i = 0; i < top; i++) {
12451763Sbostic p = (char *)GETBINTERNAL(h, i);
125*66192Sbostic P_32_SWAP(p);
12651101Sbostic p += sizeof(size_t);
127*66192Sbostic P_32_SWAP(p);
12851101Sbostic p += sizeof(pgno_t);
12951101Sbostic if (*(u_char *)p & P_BIGKEY) {
13051101Sbostic p += sizeof(u_char);
131*66192Sbostic P_32_SWAP(p);
13251101Sbostic p += sizeof(pgno_t);
133*66192Sbostic P_32_SWAP(p);
13451101Sbostic }
135*66192Sbostic M_16_SWAP(h->linp[i]);
13650990Sbostic }
13759631Sbostic else if ((h->flags & P_TYPE) == P_BLEAF)
13850990Sbostic for (i = 0; i < top; i++) {
13951763Sbostic p = (char *)GETBLEAF(h, i);
140*66192Sbostic P_32_SWAP(p);
14151101Sbostic p += sizeof(size_t);
142*66192Sbostic P_32_SWAP(p);
14351101Sbostic p += sizeof(size_t);
14451101Sbostic flags = *(u_char *)p;
14551101Sbostic if (flags & (P_BIGKEY | P_BIGDATA)) {
14651101Sbostic p += sizeof(u_char);
14751101Sbostic if (flags & P_BIGKEY) {
148*66192Sbostic P_32_SWAP(p);
14951101Sbostic p += sizeof(pgno_t);
150*66192Sbostic P_32_SWAP(p);
15151101Sbostic }
15251101Sbostic if (flags & P_BIGDATA) {
15351101Sbostic p += sizeof(size_t);
154*66192Sbostic P_32_SWAP(p);
15551101Sbostic p += sizeof(pgno_t);
156*66192Sbostic P_32_SWAP(p);
15751101Sbostic }
15851101Sbostic }
159*66192Sbostic M_16_SWAP(h->linp[i]);
16050990Sbostic }
16159631Sbostic
162*66192Sbostic M_32_SWAP(h->pgno);
163*66192Sbostic M_32_SWAP(h->prevpg);
164*66192Sbostic M_32_SWAP(h->nextpg);
165*66192Sbostic M_32_SWAP(h->flags);
166*66192Sbostic M_16_SWAP(h->lower);
167*66192Sbostic M_16_SWAP(h->upper);
16850990Sbostic }
16959631Sbostic
17059631Sbostic /*
17159631Sbostic * MSWAP -- Actually swap the bytes on the meta page.
17259631Sbostic *
17359631Sbostic * Parameters:
17459631Sbostic * p: page to convert
17559631Sbostic */
17659638Sbostic static void
mswap(pg)17759631Sbostic mswap(pg)
17859631Sbostic PAGE *pg;
17959631Sbostic {
18059631Sbostic char *p;
18159631Sbostic
18259631Sbostic p = (char *)pg;
183*66192Sbostic P_32_SWAP(p); /* m_magic */
184*66192Sbostic p += sizeof(u_int32_t);
185*66192Sbostic P_32_SWAP(p); /* m_version */
186*66192Sbostic p += sizeof(u_int32_t);
187*66192Sbostic P_32_SWAP(p); /* m_psize */
188*66192Sbostic p += sizeof(u_int32_t);
189*66192Sbostic P_32_SWAP(p); /* m_free */
190*66192Sbostic p += sizeof(u_int32_t);
191*66192Sbostic P_32_SWAP(p); /* m_nrecs */
192*66192Sbostic p += sizeof(u_int32_t);
193*66192Sbostic P_32_SWAP(p); /* m_flags */
194*66192Sbostic p += sizeof(u_int32_t);
19559631Sbostic }
196