/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1990, 1993, 19944* The Regents of the University of California. All rights reserved.5*6* This code is derived from software contributed to Berkeley by7* Mike Olson.8*9* Redistribution and use in source and binary forms, with or without10* modification, are permitted provided that the following conditions11* are met:12* 1. Redistributions of source code must retain the above copyright13* notice, this list of conditions and the following disclaimer.14* 2. Redistributions in binary form must reproduce the above copyright15* notice, this list of conditions and the following disclaimer in the16* documentation and/or other materials provided with the distribution.17* 3. Neither the name of the University nor the names of its contributors18* may be used to endorse or promote products derived from this software19* without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND22* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE23* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE24* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL26* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS27* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)28* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT29* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY30* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF31* SUCH DAMAGE.32*/3334#include <sys/param.h>3536#include <stdio.h>3738#include <db.h>39#include "btree.h"4041static void mswap(PAGE *);4243/*44* __BT_BPGIN, __BT_BPGOUT --45* Convert host-specific number layout to/from the host-independent46* format stored on disk.47*48* Parameters:49* t: tree50* pg: page number51* h: page to convert52*/53void54__bt_pgin(void *t, pgno_t pg, void *pp)55{56PAGE *h;57indx_t i, top;58u_char flags;59char *p;6061if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))62return;63if (pg == P_META) {64mswap(pp);65return;66}6768h = pp;69M_32_SWAP(h->pgno);70M_32_SWAP(h->prevpg);71M_32_SWAP(h->nextpg);72M_32_SWAP(h->flags);73M_16_SWAP(h->lower);74M_16_SWAP(h->upper);7576top = NEXTINDEX(h);77if ((h->flags & P_TYPE) == P_BINTERNAL)78for (i = 0; i < top; i++) {79M_16_SWAP(h->linp[i]);80p = (char *)GETBINTERNAL(h, i);81P_32_SWAP(p);82p += sizeof(u_int32_t);83P_32_SWAP(p);84p += sizeof(pgno_t);85if (*(u_char *)p & P_BIGKEY) {86p += sizeof(u_char);87P_32_SWAP(p);88p += sizeof(pgno_t);89P_32_SWAP(p);90}91}92else if ((h->flags & P_TYPE) == P_BLEAF)93for (i = 0; i < top; i++) {94M_16_SWAP(h->linp[i]);95p = (char *)GETBLEAF(h, i);96P_32_SWAP(p);97p += sizeof(u_int32_t);98P_32_SWAP(p);99p += sizeof(u_int32_t);100flags = *(u_char *)p;101if (flags & (P_BIGKEY | P_BIGDATA)) {102p += sizeof(u_char);103if (flags & P_BIGKEY) {104P_32_SWAP(p);105p += sizeof(pgno_t);106P_32_SWAP(p);107}108if (flags & P_BIGDATA) {109p += sizeof(u_int32_t);110P_32_SWAP(p);111p += sizeof(pgno_t);112P_32_SWAP(p);113}114}115}116}117118void119__bt_pgout(void *t, pgno_t pg, void *pp)120{121PAGE *h;122indx_t i, top;123u_char flags;124char *p;125126if (!F_ISSET(((BTREE *)t), B_NEEDSWAP))127return;128if (pg == P_META) {129mswap(pp);130return;131}132133h = pp;134top = NEXTINDEX(h);135if ((h->flags & P_TYPE) == P_BINTERNAL)136for (i = 0; i < top; i++) {137p = (char *)GETBINTERNAL(h, i);138P_32_SWAP(p);139p += sizeof(u_int32_t);140P_32_SWAP(p);141p += sizeof(pgno_t);142if (*(u_char *)p & P_BIGKEY) {143p += sizeof(u_char);144P_32_SWAP(p);145p += sizeof(pgno_t);146P_32_SWAP(p);147}148M_16_SWAP(h->linp[i]);149}150else if ((h->flags & P_TYPE) == P_BLEAF)151for (i = 0; i < top; i++) {152p = (char *)GETBLEAF(h, i);153P_32_SWAP(p);154p += sizeof(u_int32_t);155P_32_SWAP(p);156p += sizeof(u_int32_t);157flags = *(u_char *)p;158if (flags & (P_BIGKEY | P_BIGDATA)) {159p += sizeof(u_char);160if (flags & P_BIGKEY) {161P_32_SWAP(p);162p += sizeof(pgno_t);163P_32_SWAP(p);164}165if (flags & P_BIGDATA) {166p += sizeof(u_int32_t);167P_32_SWAP(p);168p += sizeof(pgno_t);169P_32_SWAP(p);170}171}172M_16_SWAP(h->linp[i]);173}174175M_32_SWAP(h->pgno);176M_32_SWAP(h->prevpg);177M_32_SWAP(h->nextpg);178M_32_SWAP(h->flags);179M_16_SWAP(h->lower);180M_16_SWAP(h->upper);181}182183/*184* MSWAP -- Actually swap the bytes on the meta page.185*186* Parameters:187* p: page to convert188*/189static void190mswap(PAGE *pg)191{192char *p;193194p = (char *)pg;195P_32_SWAP(p); /* magic */196p += sizeof(u_int32_t);197P_32_SWAP(p); /* version */198p += sizeof(u_int32_t);199P_32_SWAP(p); /* psize */200p += sizeof(u_int32_t);201P_32_SWAP(p); /* free */202p += sizeof(u_int32_t);203P_32_SWAP(p); /* nrecs */204p += sizeof(u_int32_t);205P_32_SWAP(p); /* flags */206p += sizeof(u_int32_t);207}208209210