xref: /csrg-svn/lib/libc/db/btree/bt_conv.c (revision 59638)
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*59638Sbostic static char sccsid[] = "@(#)bt_conv.c	5.9 (Berkeley) 05/01/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 
2251101Sbostic static void kdswap __P((PAGE *));
2359631Sbostic static void mswap __P((PAGE *));
2451101Sbostic 
2550990Sbostic /*
2650990Sbostic  * __BT_BPGIN, __BT_BPGOUT --
2750990Sbostic  *	Convert host-specific number layout to/from the host-independent
2850990Sbostic  *	format stored on disk.
2950990Sbostic  *
3050990Sbostic  * Parameters:
3151101Sbostic  *	t:	tree
3251101Sbostic  *	pg:	page number
3350990Sbostic  *	h:	page to convert
3450990Sbostic  */
3550990Sbostic void
3659631Sbostic __bt_pgin(t, pg, pp)
3750990Sbostic 	void *t;
3850990Sbostic 	pgno_t pg;
3959631Sbostic 	void *pp;
4050990Sbostic {
4150990Sbostic 	PAGE *h;
4259631Sbostic 	int i, top;
4359631Sbostic 	u_char flags;
4459631Sbostic 	char *p;
4550990Sbostic 
4659631Sbostic 	if (!ISSET(((BTREE *)t), BTF_NEEDSWAP))
4750990Sbostic 		return;
4859631Sbostic 	if (pg == P_META) {
4959631Sbostic 		mswap(pp);
5059631Sbostic 		return;
5159631Sbostic 	}
5250990Sbostic 
5359631Sbostic 	h = pp;
5450990Sbostic 	BLSWAP(h->pgno);
5550990Sbostic 	BLSWAP(h->prevpg);
5650990Sbostic 	BLSWAP(h->nextpg);
5750990Sbostic 	BLSWAP(h->flags);
5851101Sbostic 	BSSWAP(h->lower);
5951101Sbostic 	BSSWAP(h->upper);
6059631Sbostic 
6159631Sbostic 	top = NEXTINDEX(h);
6259631Sbostic 	if ((h->flags & P_TYPE) == P_BINTERNAL)
6359631Sbostic 		for (i = 0; i < top; i++) {
6459631Sbostic 			BSSWAP(h->linp[i]);
6559631Sbostic 			p = (char *)GETBINTERNAL(h, i);
6659631Sbostic 			BLPSWAP(p);
6759631Sbostic 			p += sizeof(size_t);
6859631Sbostic 			BLPSWAP(p);
6959631Sbostic 			p += sizeof(pgno_t);
7059631Sbostic 			if (*(u_char *)p & P_BIGKEY) {
7159631Sbostic 				p += sizeof(u_char);
7259631Sbostic 				BLPSWAP(p);
7359631Sbostic 				p += sizeof(pgno_t);
7459631Sbostic 				BLPSWAP(p);
7559631Sbostic 			}
7659631Sbostic 		}
7759631Sbostic 	else if ((h->flags & P_TYPE) == P_BLEAF)
7859631Sbostic 		for (i = 0; i < top; i++) {
7959631Sbostic 			BSSWAP(h->linp[i]);
8059631Sbostic 			p = (char *)GETBLEAF(h, i);
8159631Sbostic 			BLPSWAP(p);
8259631Sbostic 			p += sizeof(size_t);
8359631Sbostic 			BLPSWAP(p);
8459631Sbostic 			p += sizeof(size_t);
8559631Sbostic 			flags = *(u_char *)p;
8659631Sbostic 			if (flags & (P_BIGKEY | P_BIGDATA)) {
8759631Sbostic 				p += sizeof(u_char);
8859631Sbostic 				if (flags & P_BIGKEY) {
8959631Sbostic 					BLPSWAP(p);
9059631Sbostic 					p += sizeof(pgno_t);
9159631Sbostic 					BLPSWAP(p);
9259631Sbostic 				}
9359631Sbostic 				if (flags & P_BIGDATA) {
9459631Sbostic 					p += sizeof(size_t);
9559631Sbostic 					BLPSWAP(p);
9659631Sbostic 					p += sizeof(pgno_t);
9759631Sbostic 					BLPSWAP(p);
9859631Sbostic 				}
9959631Sbostic 			}
10059631Sbostic 		}
10150990Sbostic }
10250990Sbostic 
10350990Sbostic void
10459631Sbostic __bt_pgout(t, pg, pp)
10550990Sbostic 	void *t;
10650990Sbostic 	pgno_t pg;
10759631Sbostic 	void *pp;
10850990Sbostic {
10950990Sbostic 	PAGE *h;
11059631Sbostic 	int i, top;
11159631Sbostic 	u_char flags;
11259631Sbostic 	char *p;
11350990Sbostic 
11459631Sbostic 	if (!ISSET(((BTREE *)t), BTF_NEEDSWAP))
11550990Sbostic 		return;
11659631Sbostic 	if (pg == P_META) {
11759631Sbostic 		mswap(pp);
11859631Sbostic 		return;
11959631Sbostic 	}
12050990Sbostic 
12159631Sbostic 	h = pp;
12250990Sbostic 	top = NEXTINDEX(h);
12359631Sbostic 	if ((h->flags & P_TYPE) == P_BINTERNAL)
12450990Sbostic 		for (i = 0; i < top; i++) {
12551763Sbostic 			p = (char *)GETBINTERNAL(h, i);
12651101Sbostic 			BLPSWAP(p);
12751101Sbostic 			p += sizeof(size_t);
12851101Sbostic 			BLPSWAP(p);
12951101Sbostic 			p += sizeof(pgno_t);
13051101Sbostic 			if (*(u_char *)p & P_BIGKEY) {
13151101Sbostic 				p += sizeof(u_char);
13251101Sbostic 				BLPSWAP(p);
13351101Sbostic 				p += sizeof(pgno_t);
13451968Sbostic 				BLPSWAP(p);
13551101Sbostic 			}
13659631Sbostic 			BSSWAP(h->linp[i]);
13750990Sbostic 		}
13859631Sbostic 	else if ((h->flags & P_TYPE) == P_BLEAF)
13950990Sbostic 		for (i = 0; i < top; i++) {
14051763Sbostic 			p = (char *)GETBLEAF(h, i);
14151968Sbostic 			BLPSWAP(p);
14251101Sbostic 			p += sizeof(size_t);
14351968Sbostic 			BLPSWAP(p);
14451101Sbostic 			p += sizeof(size_t);
14551101Sbostic 			flags = *(u_char *)p;
14651101Sbostic 			if (flags & (P_BIGKEY | P_BIGDATA)) {
14751101Sbostic 				p += sizeof(u_char);
14851101Sbostic 				if (flags & P_BIGKEY) {
14951101Sbostic 					BLPSWAP(p);
15051101Sbostic 					p += sizeof(pgno_t);
15151968Sbostic 					BLPSWAP(p);
15251101Sbostic 				}
15351101Sbostic 				if (flags & P_BIGDATA) {
15451101Sbostic 					p += sizeof(size_t);
15551101Sbostic 					BLPSWAP(p);
15651101Sbostic 					p += sizeof(pgno_t);
15751968Sbostic 					BLPSWAP(p);
15851101Sbostic 				}
15951101Sbostic 			}
16059631Sbostic 			BSSWAP(h->linp[i]);
16150990Sbostic 		}
16259631Sbostic 
16359631Sbostic 	BLSWAP(h->pgno);
16459631Sbostic 	BLSWAP(h->prevpg);
16559631Sbostic 	BLSWAP(h->nextpg);
16659631Sbostic 	BLSWAP(h->flags);
16759631Sbostic 	BSSWAP(h->lower);
16859631Sbostic 	BSSWAP(h->upper);
16950990Sbostic }
17059631Sbostic 
17159631Sbostic /*
17259631Sbostic  * MSWAP -- Actually swap the bytes on the meta page.
17359631Sbostic  *
17459631Sbostic  * Parameters:
17559631Sbostic  *	p:	page to convert
17659631Sbostic  */
177*59638Sbostic static void
17859631Sbostic mswap(pg)
17959631Sbostic 	PAGE *pg;
18059631Sbostic {
18159631Sbostic 	char *p;
18259631Sbostic 
18359631Sbostic 	p = (char *)pg;
18459631Sbostic 	BLPSWAP(p);		/* m_magic */
18559631Sbostic 	p += sizeof(u_long);
18659631Sbostic 	BLPSWAP(p);		/* m_version */
18759631Sbostic 	p += sizeof(u_long);
18859631Sbostic 	BLPSWAP(p);		/* m_psize */
18959631Sbostic 	p += sizeof(u_long);
19059631Sbostic 	BLPSWAP(p);		/* m_free */
19159631Sbostic 	p += sizeof(u_long);
19259631Sbostic 	BLPSWAP(p);		/* m_nrecs */
19359631Sbostic 	p += sizeof(u_long);
19459631Sbostic 	BLPSWAP(p);		/* m_flags */
19559631Sbostic 	p += sizeof(u_long);
19659631Sbostic }
197