1 1.15 christos /* $NetBSD: bt_conv.c,v 1.15 2016/09/24 20:11:12 christos Exp $ */ 2 1.5 cgd 3 1.1 cgd /*- 4 1.4 cgd * Copyright (c) 1990, 1993, 1994 5 1.1 cgd * The Regents of the University of California. All rights reserved. 6 1.1 cgd * 7 1.1 cgd * This code is derived from software contributed to Berkeley by 8 1.1 cgd * Mike Olson. 9 1.1 cgd * 10 1.1 cgd * Redistribution and use in source and binary forms, with or without 11 1.1 cgd * modification, are permitted provided that the following conditions 12 1.1 cgd * are met: 13 1.1 cgd * 1. Redistributions of source code must retain the above copyright 14 1.1 cgd * notice, this list of conditions and the following disclaimer. 15 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright 16 1.1 cgd * notice, this list of conditions and the following disclaimer in the 17 1.1 cgd * documentation and/or other materials provided with the distribution. 18 1.10 agc * 3. Neither the name of the University nor the names of its contributors 19 1.1 cgd * may be used to endorse or promote products derived from this software 20 1.1 cgd * without specific prior written permission. 21 1.1 cgd * 22 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 1.1 cgd * SUCH DAMAGE. 33 1.1 cgd */ 34 1.1 cgd 35 1.11 jmc #if HAVE_NBTOOL_CONFIG_H 36 1.11 jmc #include "nbtool_config.h" 37 1.11 jmc #endif 38 1.11 jmc 39 1.7 christos #include <sys/cdefs.h> 40 1.15 christos __RCSID("$NetBSD: bt_conv.c,v 1.15 2016/09/24 20:11:12 christos Exp $"); 41 1.1 cgd 42 1.12 christos #include <assert.h> 43 1.1 cgd #include <stdio.h> 44 1.15 christos #include <memory.h> 45 1.1 cgd 46 1.1 cgd #include <db.h> 47 1.1 cgd #include "btree.h" 48 1.1 cgd 49 1.12 christos static void mswap(PAGE *); 50 1.1 cgd 51 1.1 cgd /* 52 1.1 cgd * __BT_BPGIN, __BT_BPGOUT -- 53 1.1 cgd * Convert host-specific number layout to/from the host-independent 54 1.1 cgd * format stored on disk. 55 1.1 cgd * 56 1.1 cgd * Parameters: 57 1.1 cgd * t: tree 58 1.1 cgd * pg: page number 59 1.1 cgd * h: page to convert 60 1.1 cgd */ 61 1.1 cgd void 62 1.12 christos __bt_pgin(void *t, pgno_t pg, void *pp) 63 1.1 cgd { 64 1.1 cgd PAGE *h; 65 1.4 cgd indx_t i, top; 66 1.13 joerg uint8_t flags; 67 1.1 cgd char *p; 68 1.15 christos uint32_t ksize; 69 1.1 cgd 70 1.6 cgd if (!F_ISSET(((BTREE *)t), B_NEEDSWAP)) 71 1.1 cgd return; 72 1.1 cgd if (pg == P_META) { 73 1.1 cgd mswap(pp); 74 1.1 cgd return; 75 1.1 cgd } 76 1.1 cgd 77 1.1 cgd h = pp; 78 1.4 cgd M_32_SWAP(h->pgno); 79 1.4 cgd M_32_SWAP(h->prevpg); 80 1.4 cgd M_32_SWAP(h->nextpg); 81 1.4 cgd M_32_SWAP(h->flags); 82 1.4 cgd M_16_SWAP(h->lower); 83 1.4 cgd M_16_SWAP(h->upper); 84 1.1 cgd 85 1.1 cgd top = NEXTINDEX(h); 86 1.1 cgd if ((h->flags & P_TYPE) == P_BINTERNAL) 87 1.1 cgd for (i = 0; i < top; i++) { 88 1.4 cgd M_16_SWAP(h->linp[i]); 89 1.8 christos p = (char *)(void *)GETBINTERNAL(h, i); 90 1.4 cgd P_32_SWAP(p); 91 1.13 joerg p += sizeof(uint32_t); 92 1.4 cgd P_32_SWAP(p); 93 1.1 cgd p += sizeof(pgno_t); 94 1.13 joerg if (*(uint8_t *)p & P_BIGKEY) { 95 1.13 joerg p += sizeof(uint8_t); 96 1.4 cgd P_32_SWAP(p); 97 1.1 cgd p += sizeof(pgno_t); 98 1.4 cgd P_32_SWAP(p); 99 1.1 cgd } 100 1.1 cgd } 101 1.1 cgd else if ((h->flags & P_TYPE) == P_BLEAF) 102 1.1 cgd for (i = 0; i < top; i++) { 103 1.4 cgd M_16_SWAP(h->linp[i]); 104 1.8 christos p = (char *)(void *)GETBLEAF(h, i); 105 1.4 cgd P_32_SWAP(p); 106 1.15 christos memcpy(&ksize, p, sizeof(ksize)); 107 1.13 joerg p += sizeof(uint32_t); 108 1.4 cgd P_32_SWAP(p); 109 1.13 joerg p += sizeof(uint32_t); 110 1.13 joerg flags = *(uint8_t *)p; 111 1.1 cgd if (flags & (P_BIGKEY | P_BIGDATA)) { 112 1.13 joerg p += sizeof(uint8_t); 113 1.1 cgd if (flags & P_BIGKEY) { 114 1.4 cgd P_32_SWAP(p); 115 1.1 cgd p += sizeof(pgno_t); 116 1.4 cgd P_32_SWAP(p); 117 1.1 cgd } 118 1.1 cgd if (flags & P_BIGDATA) { 119 1.15 christos p += ksize; 120 1.4 cgd P_32_SWAP(p); 121 1.1 cgd p += sizeof(pgno_t); 122 1.4 cgd P_32_SWAP(p); 123 1.1 cgd } 124 1.1 cgd } 125 1.1 cgd } 126 1.1 cgd } 127 1.1 cgd 128 1.1 cgd void 129 1.12 christos __bt_pgout(void *t, pgno_t pg, void *pp) 130 1.1 cgd { 131 1.1 cgd PAGE *h; 132 1.4 cgd indx_t i, top; 133 1.13 joerg uint8_t flags; 134 1.1 cgd char *p; 135 1.15 christos uint32_t ksize; 136 1.1 cgd 137 1.6 cgd if (!F_ISSET(((BTREE *)t), B_NEEDSWAP)) 138 1.1 cgd return; 139 1.1 cgd if (pg == P_META) { 140 1.1 cgd mswap(pp); 141 1.1 cgd return; 142 1.1 cgd } 143 1.1 cgd 144 1.1 cgd h = pp; 145 1.1 cgd top = NEXTINDEX(h); 146 1.1 cgd if ((h->flags & P_TYPE) == P_BINTERNAL) 147 1.1 cgd for (i = 0; i < top; i++) { 148 1.8 christos p = (char *)(void *)GETBINTERNAL(h, i); 149 1.4 cgd P_32_SWAP(p); 150 1.13 joerg p += sizeof(uint32_t); 151 1.4 cgd P_32_SWAP(p); 152 1.1 cgd p += sizeof(pgno_t); 153 1.13 joerg if (*(uint8_t *)p & P_BIGKEY) { 154 1.13 joerg p += sizeof(uint8_t); 155 1.4 cgd P_32_SWAP(p); 156 1.1 cgd p += sizeof(pgno_t); 157 1.4 cgd P_32_SWAP(p); 158 1.1 cgd } 159 1.4 cgd M_16_SWAP(h->linp[i]); 160 1.1 cgd } 161 1.1 cgd else if ((h->flags & P_TYPE) == P_BLEAF) 162 1.1 cgd for (i = 0; i < top; i++) { 163 1.8 christos p = (char *)(void *)GETBLEAF(h, i); 164 1.15 christos ksize = GETBLEAF(h, i)->ksize; 165 1.4 cgd P_32_SWAP(p); 166 1.13 joerg p += sizeof(uint32_t); 167 1.4 cgd P_32_SWAP(p); 168 1.13 joerg p += sizeof(uint32_t); 169 1.13 joerg flags = *(uint8_t *)p; 170 1.1 cgd if (flags & (P_BIGKEY | P_BIGDATA)) { 171 1.13 joerg p += sizeof(uint8_t); 172 1.1 cgd if (flags & P_BIGKEY) { 173 1.4 cgd P_32_SWAP(p); 174 1.1 cgd p += sizeof(pgno_t); 175 1.4 cgd P_32_SWAP(p); 176 1.1 cgd } 177 1.1 cgd if (flags & P_BIGDATA) { 178 1.15 christos p += ksize; 179 1.4 cgd P_32_SWAP(p); 180 1.1 cgd p += sizeof(pgno_t); 181 1.4 cgd P_32_SWAP(p); 182 1.1 cgd } 183 1.1 cgd } 184 1.4 cgd M_16_SWAP(h->linp[i]); 185 1.1 cgd } 186 1.1 cgd 187 1.4 cgd M_32_SWAP(h->pgno); 188 1.4 cgd M_32_SWAP(h->prevpg); 189 1.4 cgd M_32_SWAP(h->nextpg); 190 1.4 cgd M_32_SWAP(h->flags); 191 1.4 cgd M_16_SWAP(h->lower); 192 1.4 cgd M_16_SWAP(h->upper); 193 1.1 cgd } 194 1.1 cgd 195 1.1 cgd /* 196 1.1 cgd * MSWAP -- Actually swap the bytes on the meta page. 197 1.1 cgd * 198 1.1 cgd * Parameters: 199 1.1 cgd * p: page to convert 200 1.1 cgd */ 201 1.1 cgd static void 202 1.12 christos mswap(PAGE *pg) 203 1.1 cgd { 204 1.1 cgd char *p; 205 1.1 cgd 206 1.8 christos p = (char *)(void *)pg; 207 1.6 cgd P_32_SWAP(p); /* magic */ 208 1.13 joerg p += sizeof(uint32_t); 209 1.6 cgd P_32_SWAP(p); /* version */ 210 1.13 joerg p += sizeof(uint32_t); 211 1.6 cgd P_32_SWAP(p); /* psize */ 212 1.13 joerg p += sizeof(uint32_t); 213 1.6 cgd P_32_SWAP(p); /* free */ 214 1.13 joerg p += sizeof(uint32_t); 215 1.6 cgd P_32_SWAP(p); /* nrecs */ 216 1.13 joerg p += sizeof(uint32_t); 217 1.6 cgd P_32_SWAP(p); /* flags */ 218 1.13 joerg p += sizeof(uint32_t); 219 1.1 cgd } 220