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*51101Sbostic static char sccsid[] = "@(#)bt_conv.c 5.2 (Berkeley) 09/12/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 20*51101Sbostic static void kdswap __P((PAGE *)); 21*51101Sbostic 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: 28*51101Sbostic * t: tree 29*51101Sbostic * 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); 48*51101Sbostic BSSWAP(h->lower); 49*51101Sbostic BSSWAP(h->upper); 50*51101Sbostic 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; 65*51101Sbostic kdswap(h); 66*51101Sbostic BLSWAP(h->pgno); 67*51101Sbostic BLSWAP(h->prevpg); 68*51101Sbostic BLSWAP(h->nextpg); 69*51101Sbostic BLSWAP(h->flags); 70*51101Sbostic BSSWAP(h->lower); 71*51101Sbostic BSSWAP(h->upper); 72*51101Sbostic } 73*51101Sbostic 74*51101Sbostic /* 75*51101Sbostic * KDSWAP -- Actually swap the bytes on the page. 76*51101Sbostic * 77*51101Sbostic * Parameters: 78*51101Sbostic * h: page to convert 79*51101Sbostic * 80*51101Sbostic * Warnings: 81*51101Sbostic * Everywhere else in the code, the pgno_t and index_t types are 82*51101Sbostic * opaque. These routines know what they really are. 83*51101Sbostic */ 84*51101Sbostic static void 85*51101Sbostic kdswap(h) 86*51101Sbostic PAGE *h; 87*51101Sbostic { 88*51101Sbostic register int i, top; 89*51101Sbostic register void *p; 90*51101Sbostic u_char flags; 91*51101Sbostic 9250990Sbostic top = NEXTINDEX(h); 93*51101Sbostic switch (h->flags & P_TYPE) { 94*51101Sbostic case P_BINTERNAL: 9550990Sbostic for (i = 0; i < top; i++) { 96*51101Sbostic BSSWAP(h->linp[i]); 97*51101Sbostic p = GETBINTERNAL(h, i); 98*51101Sbostic BLPSWAP(p); 99*51101Sbostic p += sizeof(size_t); 100*51101Sbostic BLPSWAP(p); 101*51101Sbostic p += sizeof(pgno_t); 102*51101Sbostic if (*(u_char *)p & P_BIGKEY) { 103*51101Sbostic p += sizeof(u_char); 104*51101Sbostic BLPSWAP(p); 105*51101Sbostic p += sizeof(pgno_t); 106*51101Sbostic BSPSWAP(p); 107*51101Sbostic } 10850990Sbostic } 109*51101Sbostic break; 110*51101Sbostic case P_BLEAF: 11150990Sbostic for (i = 0; i < top; i++) { 112*51101Sbostic BSSWAP(h->linp[i]); 113*51101Sbostic p = GETBLEAF(h, i); 114*51101Sbostic BSPSWAP(p); 115*51101Sbostic p += sizeof(size_t); 116*51101Sbostic BSPSWAP(p); 117*51101Sbostic p += sizeof(size_t); 118*51101Sbostic flags = *(u_char *)p; 119*51101Sbostic if (flags & (P_BIGKEY | P_BIGDATA)) { 120*51101Sbostic p += sizeof(u_char); 121*51101Sbostic if (flags & P_BIGKEY) { 122*51101Sbostic BLPSWAP(p); 123*51101Sbostic p += sizeof(pgno_t); 124*51101Sbostic BSPSWAP(p); 125*51101Sbostic } 126*51101Sbostic if (flags & P_BIGDATA) { 127*51101Sbostic p += sizeof(size_t); 128*51101Sbostic BLPSWAP(p); 129*51101Sbostic p += sizeof(pgno_t); 130*51101Sbostic BSPSWAP(p); 131*51101Sbostic } 132*51101Sbostic } 13350990Sbostic } 134*51101Sbostic break; 135*51101Sbostic } 13650990Sbostic } 137