bt_conv.c 5.3 KB

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