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*59631Sbostic static char sccsid[] = "@(#)bt_conv.c 5.8 (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 *)); 23*59631Sbostic 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 36*59631Sbostic __bt_pgin(t, pg, pp) 3750990Sbostic void *t; 3850990Sbostic pgno_t pg; 39*59631Sbostic void *pp; 4050990Sbostic { 4150990Sbostic PAGE *h; 42*59631Sbostic int i, top; 43*59631Sbostic u_char flags; 44*59631Sbostic char *p; 4550990Sbostic 46*59631Sbostic if (!ISSET(((BTREE *)t), BTF_NEEDSWAP)) 4750990Sbostic return; 48*59631Sbostic if (pg == P_META) { 49*59631Sbostic mswap(pp); 50*59631Sbostic return; 51*59631Sbostic } 5250990Sbostic 53*59631Sbostic 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); 60*59631Sbostic 61*59631Sbostic top = NEXTINDEX(h); 62*59631Sbostic if ((h->flags & P_TYPE) == P_BINTERNAL) 63*59631Sbostic for (i = 0; i < top; i++) { 64*59631Sbostic BSSWAP(h->linp[i]); 65*59631Sbostic p = (char *)GETBINTERNAL(h, i); 66*59631Sbostic BLPSWAP(p); 67*59631Sbostic p += sizeof(size_t); 68*59631Sbostic BLPSWAP(p); 69*59631Sbostic p += sizeof(pgno_t); 70*59631Sbostic if (*(u_char *)p & P_BIGKEY) { 71*59631Sbostic p += sizeof(u_char); 72*59631Sbostic BLPSWAP(p); 73*59631Sbostic p += sizeof(pgno_t); 74*59631Sbostic BLPSWAP(p); 75*59631Sbostic } 76*59631Sbostic } 77*59631Sbostic else if ((h->flags & P_TYPE) == P_BLEAF) 78*59631Sbostic for (i = 0; i < top; i++) { 79*59631Sbostic BSSWAP(h->linp[i]); 80*59631Sbostic p = (char *)GETBLEAF(h, i); 81*59631Sbostic BLPSWAP(p); 82*59631Sbostic p += sizeof(size_t); 83*59631Sbostic BLPSWAP(p); 84*59631Sbostic p += sizeof(size_t); 85*59631Sbostic flags = *(u_char *)p; 86*59631Sbostic if (flags & (P_BIGKEY | P_BIGDATA)) { 87*59631Sbostic p += sizeof(u_char); 88*59631Sbostic if (flags & P_BIGKEY) { 89*59631Sbostic BLPSWAP(p); 90*59631Sbostic p += sizeof(pgno_t); 91*59631Sbostic BLPSWAP(p); 92*59631Sbostic } 93*59631Sbostic if (flags & P_BIGDATA) { 94*59631Sbostic p += sizeof(size_t); 95*59631Sbostic BLPSWAP(p); 96*59631Sbostic p += sizeof(pgno_t); 97*59631Sbostic BLPSWAP(p); 98*59631Sbostic } 99*59631Sbostic } 100*59631Sbostic } 10150990Sbostic } 10250990Sbostic 10350990Sbostic void 104*59631Sbostic __bt_pgout(t, pg, pp) 10550990Sbostic void *t; 10650990Sbostic pgno_t pg; 107*59631Sbostic void *pp; 10850990Sbostic { 10950990Sbostic PAGE *h; 110*59631Sbostic int i, top; 111*59631Sbostic u_char flags; 112*59631Sbostic char *p; 11350990Sbostic 114*59631Sbostic if (!ISSET(((BTREE *)t), BTF_NEEDSWAP)) 11550990Sbostic return; 116*59631Sbostic if (pg == P_META) { 117*59631Sbostic mswap(pp); 118*59631Sbostic return; 119*59631Sbostic } 12050990Sbostic 121*59631Sbostic h = pp; 12250990Sbostic top = NEXTINDEX(h); 123*59631Sbostic 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 } 136*59631Sbostic BSSWAP(h->linp[i]); 13750990Sbostic } 138*59631Sbostic 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 } 160*59631Sbostic BSSWAP(h->linp[i]); 16150990Sbostic } 162*59631Sbostic 163*59631Sbostic BLSWAP(h->pgno); 164*59631Sbostic BLSWAP(h->prevpg); 165*59631Sbostic BLSWAP(h->nextpg); 166*59631Sbostic BLSWAP(h->flags); 167*59631Sbostic BSSWAP(h->lower); 168*59631Sbostic BSSWAP(h->upper); 16950990Sbostic } 170*59631Sbostic 171*59631Sbostic /* 172*59631Sbostic * MSWAP -- Actually swap the bytes on the meta page. 173*59631Sbostic * 174*59631Sbostic * Parameters: 175*59631Sbostic * p: page to convert 176*59631Sbostic */ 177*59631Sbostic void 178*59631Sbostic mswap(pg) 179*59631Sbostic PAGE *pg; 180*59631Sbostic { 181*59631Sbostic char *p; 182*59631Sbostic 183*59631Sbostic p = (char *)pg; 184*59631Sbostic BLPSWAP(p); /* m_magic */ 185*59631Sbostic p += sizeof(u_long); 186*59631Sbostic BLPSWAP(p); /* m_version */ 187*59631Sbostic p += sizeof(u_long); 188*59631Sbostic BLPSWAP(p); /* m_psize */ 189*59631Sbostic p += sizeof(u_long); 190*59631Sbostic BLPSWAP(p); /* m_free */ 191*59631Sbostic p += sizeof(u_long); 192*59631Sbostic BLPSWAP(p); /* m_nrecs */ 193*59631Sbostic p += sizeof(u_long); 194*59631Sbostic BLPSWAP(p); /* m_flags */ 195*59631Sbostic p += sizeof(u_long); 196*59631Sbostic } 197