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*56734Sbostic static char sccsid[] = "@(#)bt_conv.c 5.5 (Berkeley) 11/13/92"; 1350990Sbostic #endif /* LIBC_SCCS and not lint */ 1450990Sbostic 1550990Sbostic #include <sys/param.h> 16*56734Sbostic 1750990Sbostic #include <db.h> 1850990Sbostic #include <stdio.h> 19*56734Sbostic 2050990Sbostic #include "btree.h" 2150990Sbostic 2251101Sbostic static void kdswap __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 3550990Sbostic __bt_pgin(t, pg, p) 3650990Sbostic void *t; 3750990Sbostic pgno_t pg; 3850990Sbostic void *p; 3950990Sbostic { 4050990Sbostic PAGE *h; 4150990Sbostic 4250990Sbostic if (((BTREE *)t)->bt_lorder == BYTE_ORDER) 4350990Sbostic return; 4450990Sbostic 4550990Sbostic h = p; 4650990Sbostic BLSWAP(h->pgno); 4750990Sbostic BLSWAP(h->prevpg); 4850990Sbostic BLSWAP(h->nextpg); 4950990Sbostic BLSWAP(h->flags); 5051101Sbostic BSSWAP(h->lower); 5151101Sbostic BSSWAP(h->upper); 5251101Sbostic kdswap(h); 5350990Sbostic } 5450990Sbostic 5550990Sbostic void 5650990Sbostic __bt_pgout(t, pg, p) 5750990Sbostic void *t; 5850990Sbostic pgno_t pg; 5950990Sbostic void *p; 6050990Sbostic { 6150990Sbostic PAGE *h; 6250990Sbostic 6350990Sbostic if (((BTREE *)t)->bt_lorder == BYTE_ORDER) 6450990Sbostic return; 6550990Sbostic 6650990Sbostic h = p; 6751101Sbostic kdswap(h); 6851101Sbostic BLSWAP(h->pgno); 6951101Sbostic BLSWAP(h->prevpg); 7051101Sbostic BLSWAP(h->nextpg); 7151101Sbostic BLSWAP(h->flags); 7251101Sbostic BSSWAP(h->lower); 7351101Sbostic BSSWAP(h->upper); 7451101Sbostic } 7551101Sbostic 7651101Sbostic /* 7751101Sbostic * KDSWAP -- Actually swap the bytes on the page. 7851101Sbostic * 7951101Sbostic * Parameters: 8051101Sbostic * h: page to convert 8151101Sbostic * 8251101Sbostic * Warnings: 8351101Sbostic * Everywhere else in the code, the pgno_t and index_t types are 8451101Sbostic * opaque. These routines know what they really are. 8551101Sbostic */ 8651101Sbostic static void 8751101Sbostic kdswap(h) 8851101Sbostic PAGE *h; 8951101Sbostic { 9051101Sbostic register int i, top; 9151763Sbostic register char *p; /* Really void, thanks ANSI! */ 9251101Sbostic u_char flags; 9351101Sbostic 9450990Sbostic top = NEXTINDEX(h); 9551101Sbostic switch (h->flags & P_TYPE) { 9651101Sbostic case P_BINTERNAL: 9750990Sbostic for (i = 0; i < top; i++) { 9851101Sbostic BSSWAP(h->linp[i]); 9951763Sbostic p = (char *)GETBINTERNAL(h, i); 10051101Sbostic BLPSWAP(p); 10151101Sbostic p += sizeof(size_t); 10251101Sbostic BLPSWAP(p); 10351101Sbostic p += sizeof(pgno_t); 10451101Sbostic if (*(u_char *)p & P_BIGKEY) { 10551101Sbostic p += sizeof(u_char); 10651101Sbostic BLPSWAP(p); 10751101Sbostic p += sizeof(pgno_t); 10851968Sbostic BLPSWAP(p); 10951101Sbostic } 11050990Sbostic } 11151101Sbostic break; 11251101Sbostic case P_BLEAF: 11350990Sbostic for (i = 0; i < top; i++) { 11451101Sbostic BSSWAP(h->linp[i]); 11551763Sbostic p = (char *)GETBLEAF(h, i); 11651968Sbostic BLPSWAP(p); 11751101Sbostic p += sizeof(size_t); 11851968Sbostic BLPSWAP(p); 11951101Sbostic p += sizeof(size_t); 12051101Sbostic flags = *(u_char *)p; 12151101Sbostic if (flags & (P_BIGKEY | P_BIGDATA)) { 12251101Sbostic p += sizeof(u_char); 12351101Sbostic if (flags & P_BIGKEY) { 12451101Sbostic BLPSWAP(p); 12551101Sbostic p += sizeof(pgno_t); 12651968Sbostic BLPSWAP(p); 12751101Sbostic } 12851101Sbostic if (flags & P_BIGDATA) { 12951101Sbostic p += sizeof(size_t); 13051101Sbostic BLPSWAP(p); 13151101Sbostic p += sizeof(pgno_t); 13251968Sbostic BLPSWAP(p); 13351101Sbostic } 13451101Sbostic } 13550990Sbostic } 13651101Sbostic break; 13751101Sbostic } 13850990Sbostic } 139