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