xref: /csrg-svn/lib/libc/db/btree/bt_conv.c (revision 60043)
150990Sbostic /*-
250990Sbostic  * Copyright (c) 1990 The Regents of the University of California.
350990Sbostic  * 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*60043Sbostic static char sccsid[] = "@(#)bt_conv.c	5.10 (Berkeley) 05/16/93";
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
3559631Sbostic __bt_pgin(t, pg, pp)
3650990Sbostic 	void *t;
3750990Sbostic 	pgno_t pg;
3859631Sbostic 	void *pp;
3950990Sbostic {
4050990Sbostic 	PAGE *h;
4159631Sbostic 	int i, top;
4259631Sbostic 	u_char flags;
4359631Sbostic 	char *p;
4450990Sbostic 
45*60043Sbostic 	if (!ISSET(((BTREE *)t), B_NEEDSWAP))
4650990Sbostic 		return;
4759631Sbostic 	if (pg == P_META) {
4859631Sbostic 		mswap(pp);
4959631Sbostic 		return;
5059631Sbostic 	}
5150990Sbostic 
5259631Sbostic 	h = pp;
5350990Sbostic 	BLSWAP(h->pgno);
5450990Sbostic 	BLSWAP(h->prevpg);
5550990Sbostic 	BLSWAP(h->nextpg);
5650990Sbostic 	BLSWAP(h->flags);
5751101Sbostic 	BSSWAP(h->lower);
5851101Sbostic 	BSSWAP(h->upper);
5959631Sbostic 
6059631Sbostic 	top = NEXTINDEX(h);
6159631Sbostic 	if ((h->flags & P_TYPE) == P_BINTERNAL)
6259631Sbostic 		for (i = 0; i < top; i++) {
6359631Sbostic 			BSSWAP(h->linp[i]);
6459631Sbostic 			p = (char *)GETBINTERNAL(h, i);
6559631Sbostic 			BLPSWAP(p);
6659631Sbostic 			p += sizeof(size_t);
6759631Sbostic 			BLPSWAP(p);
6859631Sbostic 			p += sizeof(pgno_t);
6959631Sbostic 			if (*(u_char *)p & P_BIGKEY) {
7059631Sbostic 				p += sizeof(u_char);
7159631Sbostic 				BLPSWAP(p);
7259631Sbostic 				p += sizeof(pgno_t);
7359631Sbostic 				BLPSWAP(p);
7459631Sbostic 			}
7559631Sbostic 		}
7659631Sbostic 	else if ((h->flags & P_TYPE) == P_BLEAF)
7759631Sbostic 		for (i = 0; i < top; i++) {
7859631Sbostic 			BSSWAP(h->linp[i]);
7959631Sbostic 			p = (char *)GETBLEAF(h, i);
8059631Sbostic 			BLPSWAP(p);
8159631Sbostic 			p += sizeof(size_t);
8259631Sbostic 			BLPSWAP(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) {
8859631Sbostic 					BLPSWAP(p);
8959631Sbostic 					p += sizeof(pgno_t);
9059631Sbostic 					BLPSWAP(p);
9159631Sbostic 				}
9259631Sbostic 				if (flags & P_BIGDATA) {
9359631Sbostic 					p += sizeof(size_t);
9459631Sbostic 					BLPSWAP(p);
9559631Sbostic 					p += sizeof(pgno_t);
9659631Sbostic 					BLPSWAP(p);
9759631Sbostic 				}
9859631Sbostic 			}
9959631Sbostic 		}
10050990Sbostic }
10150990Sbostic 
10250990Sbostic void
10359631Sbostic __bt_pgout(t, pg, pp)
10450990Sbostic 	void *t;
10550990Sbostic 	pgno_t pg;
10659631Sbostic 	void *pp;
10750990Sbostic {
10850990Sbostic 	PAGE *h;
10959631Sbostic 	int i, top;
11059631Sbostic 	u_char flags;
11159631Sbostic 	char *p;
11250990Sbostic 
113*60043Sbostic 	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);
12551101Sbostic 			BLPSWAP(p);
12651101Sbostic 			p += sizeof(size_t);
12751101Sbostic 			BLPSWAP(p);
12851101Sbostic 			p += sizeof(pgno_t);
12951101Sbostic 			if (*(u_char *)p & P_BIGKEY) {
13051101Sbostic 				p += sizeof(u_char);
13151101Sbostic 				BLPSWAP(p);
13251101Sbostic 				p += sizeof(pgno_t);
13351968Sbostic 				BLPSWAP(p);
13451101Sbostic 			}
13559631Sbostic 			BSSWAP(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);
14051968Sbostic 			BLPSWAP(p);
14151101Sbostic 			p += sizeof(size_t);
14251968Sbostic 			BLPSWAP(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) {
14851101Sbostic 					BLPSWAP(p);
14951101Sbostic 					p += sizeof(pgno_t);
15051968Sbostic 					BLPSWAP(p);
15151101Sbostic 				}
15251101Sbostic 				if (flags & P_BIGDATA) {
15351101Sbostic 					p += sizeof(size_t);
15451101Sbostic 					BLPSWAP(p);
15551101Sbostic 					p += sizeof(pgno_t);
15651968Sbostic 					BLPSWAP(p);
15751101Sbostic 				}
15851101Sbostic 			}
15959631Sbostic 			BSSWAP(h->linp[i]);
16050990Sbostic 		}
16159631Sbostic 
16259631Sbostic 	BLSWAP(h->pgno);
16359631Sbostic 	BLSWAP(h->prevpg);
16459631Sbostic 	BLSWAP(h->nextpg);
16559631Sbostic 	BLSWAP(h->flags);
16659631Sbostic 	BSSWAP(h->lower);
16759631Sbostic 	BSSWAP(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
17759631Sbostic mswap(pg)
17859631Sbostic 	PAGE *pg;
17959631Sbostic {
18059631Sbostic 	char *p;
18159631Sbostic 
18259631Sbostic 	p = (char *)pg;
18359631Sbostic 	BLPSWAP(p);		/* m_magic */
18459631Sbostic 	p += sizeof(u_long);
18559631Sbostic 	BLPSWAP(p);		/* m_version */
18659631Sbostic 	p += sizeof(u_long);
18759631Sbostic 	BLPSWAP(p);		/* m_psize */
18859631Sbostic 	p += sizeof(u_long);
18959631Sbostic 	BLPSWAP(p);		/* m_free */
19059631Sbostic 	p += sizeof(u_long);
19159631Sbostic 	BLPSWAP(p);		/* m_nrecs */
19259631Sbostic 	p += sizeof(u_long);
19359631Sbostic 	BLPSWAP(p);		/* m_flags */
19459631Sbostic 	p += sizeof(u_long);
19559631Sbostic }
196