11573Srgrimes/*- 214272Spst * Copyright (c) 1990, 1993, 1994 31573Srgrimes * The Regents of the University of California. All rights reserved. 41573Srgrimes * 51573Srgrimes * This code is derived from software contributed to Berkeley by 61573Srgrimes * Mike Olson. 71573Srgrimes * 81573Srgrimes * Redistribution and use in source and binary forms, with or without 91573Srgrimes * modification, are permitted provided that the following conditions 101573Srgrimes * are met: 111573Srgrimes * 1. Redistributions of source code must retain the above copyright 121573Srgrimes * notice, this list of conditions and the following disclaimer. 131573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141573Srgrimes * notice, this list of conditions and the following disclaimer in the 151573Srgrimes * documentation and/or other materials provided with the distribution. 161573Srgrimes * 4. Neither the name of the University nor the names of its contributors 171573Srgrimes * may be used to endorse or promote products derived from this software 181573Srgrimes * without specific prior written permission. 191573Srgrimes * 201573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231573Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 301573Srgrimes * SUCH DAMAGE. 311573Srgrimes */ 321573Srgrimes 331573Srgrimes#if defined(LIBC_SCCS) && !defined(lint) 3414272Spststatic char sccsid[] = "@(#)bt_conv.c 8.5 (Berkeley) 8/17/94"; 351573Srgrimes#endif /* LIBC_SCCS and not lint */ 3692905Sobrien#include <sys/cdefs.h> 3792905Sobrien__FBSDID("$FreeBSD: releng/11.0/lib/libc/db/btree/bt_conv.c 189291 2009-03-02 23:47:18Z delphij $"); 381573Srgrimes 391573Srgrimes#include <sys/param.h> 401573Srgrimes 411573Srgrimes#include <stdio.h> 421573Srgrimes 431573Srgrimes#include <db.h> 441573Srgrimes#include "btree.h" 451573Srgrimes 4692905Sobrienstatic void mswap(PAGE *); 471573Srgrimes 481573Srgrimes/* 491573Srgrimes * __BT_BPGIN, __BT_BPGOUT -- 501573Srgrimes * Convert host-specific number layout to/from the host-independent 511573Srgrimes * format stored on disk. 521573Srgrimes * 531573Srgrimes * Parameters: 541573Srgrimes * t: tree 551573Srgrimes * pg: page number 561573Srgrimes * h: page to convert 571573Srgrimes */ 581573Srgrimesvoid 59189291Sdelphij__bt_pgin(void *t, pgno_t pg, void *pp) 601573Srgrimes{ 611573Srgrimes PAGE *h; 621573Srgrimes indx_t i, top; 631573Srgrimes u_char flags; 641573Srgrimes char *p; 651573Srgrimes 6614272Spst if (!F_ISSET(((BTREE *)t), B_NEEDSWAP)) 671573Srgrimes return; 681573Srgrimes if (pg == P_META) { 691573Srgrimes mswap(pp); 701573Srgrimes return; 711573Srgrimes } 721573Srgrimes 731573Srgrimes h = pp; 741573Srgrimes M_32_SWAP(h->pgno); 751573Srgrimes M_32_SWAP(h->prevpg); 761573Srgrimes M_32_SWAP(h->nextpg); 771573Srgrimes M_32_SWAP(h->flags); 781573Srgrimes M_16_SWAP(h->lower); 791573Srgrimes M_16_SWAP(h->upper); 801573Srgrimes 811573Srgrimes top = NEXTINDEX(h); 821573Srgrimes if ((h->flags & P_TYPE) == P_BINTERNAL) 831573Srgrimes for (i = 0; i < top; i++) { 841573Srgrimes M_16_SWAP(h->linp[i]); 851573Srgrimes p = (char *)GETBINTERNAL(h, i); 861573Srgrimes P_32_SWAP(p); 8714272Spst p += sizeof(u_int32_t); 881573Srgrimes P_32_SWAP(p); 891573Srgrimes p += sizeof(pgno_t); 901573Srgrimes if (*(u_char *)p & P_BIGKEY) { 911573Srgrimes p += sizeof(u_char); 921573Srgrimes P_32_SWAP(p); 931573Srgrimes p += sizeof(pgno_t); 941573Srgrimes P_32_SWAP(p); 951573Srgrimes } 961573Srgrimes } 971573Srgrimes else if ((h->flags & P_TYPE) == P_BLEAF) 981573Srgrimes for (i = 0; i < top; i++) { 991573Srgrimes M_16_SWAP(h->linp[i]); 1001573Srgrimes p = (char *)GETBLEAF(h, i); 1011573Srgrimes P_32_SWAP(p); 10214272Spst p += sizeof(u_int32_t); 1031573Srgrimes P_32_SWAP(p); 10414272Spst p += sizeof(u_int32_t); 1051573Srgrimes flags = *(u_char *)p; 1061573Srgrimes if (flags & (P_BIGKEY | P_BIGDATA)) { 1071573Srgrimes p += sizeof(u_char); 1081573Srgrimes if (flags & P_BIGKEY) { 1091573Srgrimes P_32_SWAP(p); 1101573Srgrimes p += sizeof(pgno_t); 1111573Srgrimes P_32_SWAP(p); 1121573Srgrimes } 1131573Srgrimes if (flags & P_BIGDATA) { 11414272Spst p += sizeof(u_int32_t); 1151573Srgrimes P_32_SWAP(p); 1161573Srgrimes p += sizeof(pgno_t); 1171573Srgrimes P_32_SWAP(p); 1181573Srgrimes } 1191573Srgrimes } 1201573Srgrimes } 1211573Srgrimes} 1221573Srgrimes 1231573Srgrimesvoid 124189291Sdelphij__bt_pgout(void *t, pgno_t pg, void *pp) 1251573Srgrimes{ 1261573Srgrimes PAGE *h; 1271573Srgrimes indx_t i, top; 1281573Srgrimes u_char flags; 1291573Srgrimes char *p; 1301573Srgrimes 13114272Spst if (!F_ISSET(((BTREE *)t), B_NEEDSWAP)) 1321573Srgrimes return; 1331573Srgrimes if (pg == P_META) { 1341573Srgrimes mswap(pp); 1351573Srgrimes return; 1361573Srgrimes } 1371573Srgrimes 1381573Srgrimes h = pp; 1391573Srgrimes top = NEXTINDEX(h); 1401573Srgrimes if ((h->flags & P_TYPE) == P_BINTERNAL) 1411573Srgrimes for (i = 0; i < top; i++) { 1421573Srgrimes p = (char *)GETBINTERNAL(h, i); 1431573Srgrimes P_32_SWAP(p); 14414272Spst p += sizeof(u_int32_t); 1451573Srgrimes P_32_SWAP(p); 1461573Srgrimes p += sizeof(pgno_t); 1471573Srgrimes if (*(u_char *)p & P_BIGKEY) { 1481573Srgrimes p += sizeof(u_char); 1491573Srgrimes P_32_SWAP(p); 1501573Srgrimes p += sizeof(pgno_t); 1511573Srgrimes P_32_SWAP(p); 1521573Srgrimes } 1531573Srgrimes M_16_SWAP(h->linp[i]); 1541573Srgrimes } 1551573Srgrimes else if ((h->flags & P_TYPE) == P_BLEAF) 1561573Srgrimes for (i = 0; i < top; i++) { 1571573Srgrimes p = (char *)GETBLEAF(h, i); 1581573Srgrimes P_32_SWAP(p); 15914272Spst p += sizeof(u_int32_t); 1601573Srgrimes P_32_SWAP(p); 16114272Spst p += sizeof(u_int32_t); 1621573Srgrimes flags = *(u_char *)p; 1631573Srgrimes if (flags & (P_BIGKEY | P_BIGDATA)) { 1641573Srgrimes p += sizeof(u_char); 1651573Srgrimes if (flags & P_BIGKEY) { 1661573Srgrimes P_32_SWAP(p); 1671573Srgrimes p += sizeof(pgno_t); 1681573Srgrimes P_32_SWAP(p); 1691573Srgrimes } 1701573Srgrimes if (flags & P_BIGDATA) { 17114272Spst p += sizeof(u_int32_t); 1721573Srgrimes P_32_SWAP(p); 1731573Srgrimes p += sizeof(pgno_t); 1741573Srgrimes P_32_SWAP(p); 1751573Srgrimes } 1761573Srgrimes } 1771573Srgrimes M_16_SWAP(h->linp[i]); 1781573Srgrimes } 1791573Srgrimes 1801573Srgrimes M_32_SWAP(h->pgno); 1811573Srgrimes M_32_SWAP(h->prevpg); 1821573Srgrimes M_32_SWAP(h->nextpg); 1831573Srgrimes M_32_SWAP(h->flags); 1841573Srgrimes M_16_SWAP(h->lower); 1851573Srgrimes M_16_SWAP(h->upper); 1861573Srgrimes} 1871573Srgrimes 1881573Srgrimes/* 1891573Srgrimes * MSWAP -- Actually swap the bytes on the meta page. 1901573Srgrimes * 1911573Srgrimes * Parameters: 1921573Srgrimes * p: page to convert 1931573Srgrimes */ 1941573Srgrimesstatic void 195189291Sdelphijmswap(PAGE *pg) 1961573Srgrimes{ 1971573Srgrimes char *p; 1981573Srgrimes 1991573Srgrimes p = (char *)pg; 20014272Spst P_32_SWAP(p); /* magic */ 2011573Srgrimes p += sizeof(u_int32_t); 20214272Spst P_32_SWAP(p); /* version */ 2031573Srgrimes p += sizeof(u_int32_t); 20414272Spst P_32_SWAP(p); /* psize */ 2051573Srgrimes p += sizeof(u_int32_t); 20614272Spst P_32_SWAP(p); /* free */ 2071573Srgrimes p += sizeof(u_int32_t); 20814272Spst P_32_SWAP(p); /* nrecs */ 2091573Srgrimes p += sizeof(u_int32_t); 21014272Spst P_32_SWAP(p); /* flags */ 2111573Srgrimes p += sizeof(u_int32_t); 2121573Srgrimes} 213