xref: /csrg-svn/lib/libc/db/btree/bt_conv.c (revision 51763)
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*51763Sbostic static char sccsid[] = "@(#)bt_conv.c	5.3 (Berkeley) 11/20/91";
1350990Sbostic #endif /* LIBC_SCCS and not lint */
1450990Sbostic 
1550990Sbostic #include <sys/param.h>
1650990Sbostic #include <db.h>
1750990Sbostic #include <stdio.h>
1850990Sbostic #include "btree.h"
1950990Sbostic 
2051101Sbostic static void kdswap __P((PAGE *));
2151101Sbostic 
2250990Sbostic /*
2350990Sbostic  * __BT_BPGIN, __BT_BPGOUT --
2450990Sbostic  *	Convert host-specific number layout to/from the host-independent
2550990Sbostic  *	format stored on disk.
2650990Sbostic  *
2750990Sbostic  * Parameters:
2851101Sbostic  *	t:	tree
2951101Sbostic  *	pg:	page number
3050990Sbostic  *	h:	page to convert
3150990Sbostic  */
3250990Sbostic void
3350990Sbostic __bt_pgin(t, pg, p)
3450990Sbostic 	void *t;
3550990Sbostic 	pgno_t pg;
3650990Sbostic 	void *p;
3750990Sbostic {
3850990Sbostic 	PAGE *h;
3950990Sbostic 
4050990Sbostic 	if (((BTREE *)t)->bt_lorder == BYTE_ORDER)
4150990Sbostic 		return;
4250990Sbostic 
4350990Sbostic 	h = p;
4450990Sbostic 	BLSWAP(h->pgno);
4550990Sbostic 	BLSWAP(h->prevpg);
4650990Sbostic 	BLSWAP(h->nextpg);
4750990Sbostic 	BLSWAP(h->flags);
4851101Sbostic 	BSSWAP(h->lower);
4951101Sbostic 	BSSWAP(h->upper);
5051101Sbostic 	kdswap(h);
5150990Sbostic }
5250990Sbostic 
5350990Sbostic void
5450990Sbostic __bt_pgout(t, pg, p)
5550990Sbostic 	void *t;
5650990Sbostic 	pgno_t pg;
5750990Sbostic 	void *p;
5850990Sbostic {
5950990Sbostic 	PAGE *h;
6050990Sbostic 
6150990Sbostic 	if (((BTREE *)t)->bt_lorder == BYTE_ORDER)
6250990Sbostic 		return;
6350990Sbostic 
6450990Sbostic 	h = p;
6551101Sbostic 	kdswap(h);
6651101Sbostic 	BLSWAP(h->pgno);
6751101Sbostic 	BLSWAP(h->prevpg);
6851101Sbostic 	BLSWAP(h->nextpg);
6951101Sbostic 	BLSWAP(h->flags);
7051101Sbostic 	BSSWAP(h->lower);
7151101Sbostic 	BSSWAP(h->upper);
7251101Sbostic }
7351101Sbostic 
7451101Sbostic /*
7551101Sbostic  * KDSWAP -- Actually swap the bytes on the page.
7651101Sbostic  *
7751101Sbostic  * Parameters:
7851101Sbostic  *	h:	page to convert
7951101Sbostic  *
8051101Sbostic  * Warnings:
8151101Sbostic  *	Everywhere else in the code, the pgno_t and index_t types are
8251101Sbostic  *	opaque.  These routines know what they really are.
8351101Sbostic  */
8451101Sbostic static void
8551101Sbostic kdswap(h)
8651101Sbostic 	PAGE *h;
8751101Sbostic {
8851101Sbostic 	register int i, top;
89*51763Sbostic 	register char *p;			/* Really void, thanks ANSI! */
9051101Sbostic 	u_char flags;
9151101Sbostic 
9250990Sbostic 	top = NEXTINDEX(h);
9351101Sbostic 	switch (h->flags & P_TYPE) {
9451101Sbostic 	case P_BINTERNAL:
9550990Sbostic 		for (i = 0; i < top; i++) {
9651101Sbostic 			BSSWAP(h->linp[i]);
97*51763Sbostic 			p = (char *)GETBINTERNAL(h, i);
9851101Sbostic 			BLPSWAP(p);
9951101Sbostic 			p += sizeof(size_t);
10051101Sbostic 			BLPSWAP(p);
10151101Sbostic 			p += sizeof(pgno_t);
10251101Sbostic 			if (*(u_char *)p & P_BIGKEY) {
10351101Sbostic 				p += sizeof(u_char);
10451101Sbostic 				BLPSWAP(p);
10551101Sbostic 				p += sizeof(pgno_t);
10651101Sbostic 				BSPSWAP(p);
10751101Sbostic 			}
10850990Sbostic 		}
10951101Sbostic 		break;
11051101Sbostic 	case P_BLEAF:
11150990Sbostic 		for (i = 0; i < top; i++) {
11251101Sbostic 			BSSWAP(h->linp[i]);
113*51763Sbostic 			p = (char *)GETBLEAF(h, i);
11451101Sbostic 			BSPSWAP(p);
11551101Sbostic 			p += sizeof(size_t);
11651101Sbostic 			BSPSWAP(p);
11751101Sbostic 			p += sizeof(size_t);
11851101Sbostic 			flags = *(u_char *)p;
11951101Sbostic 			if (flags & (P_BIGKEY | P_BIGDATA)) {
12051101Sbostic 				p += sizeof(u_char);
12151101Sbostic 				if (flags & P_BIGKEY) {
12251101Sbostic 					BLPSWAP(p);
12351101Sbostic 					p += sizeof(pgno_t);
12451101Sbostic 					BSPSWAP(p);
12551101Sbostic 				}
12651101Sbostic 				if (flags & P_BIGDATA) {
12751101Sbostic 					p += sizeof(size_t);
12851101Sbostic 					BLPSWAP(p);
12951101Sbostic 					p += sizeof(pgno_t);
13051101Sbostic 					BSPSWAP(p);
13151101Sbostic 				}
13251101Sbostic 			}
13350990Sbostic 		}
13451101Sbostic 		break;
13551101Sbostic 	}
13650990Sbostic }
137