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