bt_conv.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: bt_conv.c /main/3 1996/06/11 17:12:19 cde-hal $ */
  24. /*-
  25. * Copyright (c) 1990, 1993
  26. * The Regents of the University of California. All rights reserved.
  27. *
  28. * This code is derived from software contributed to Berkeley by
  29. * Mike Olson.
  30. *
  31. * Redistribution and use in source and binary forms, with or without
  32. * modification, are permitted provided that the following conditions
  33. * are met:
  34. * 1. Redistributions of source code must retain the above copyright
  35. * notice, this list of conditions and the following disclaimer.
  36. * 2. Redistributions in binary form must reproduce the above copyright
  37. * notice, this list of conditions and the following disclaimer in the
  38. * documentation and/or other materials provided with the distribution.
  39. * 3. All advertising materials mentioning features or use of this software
  40. * must display the following acknowledgement:
  41. * This product includes software developed by the University of
  42. * California, Berkeley and its contributors.
  43. * 4. Neither the name of the University nor the names of its contributors
  44. * may be used to endorse or promote products derived from this software
  45. * without specific prior written permission.
  46. *
  47. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  48. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  49. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  50. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  51. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  52. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  53. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  54. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  55. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  56. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  57. * SUCH DAMAGE.
  58. */
  59. #if defined(LIBC_SCCS) && !defined(lint)
  60. static char sccsid[] = "@(#)bt_conv.c 8.1 (Berkeley) 6/4/93";
  61. #endif /* LIBC_SCCS and not lint */
  62. #include <sys/param.h>
  63. #include <stdio.h>
  64. #include <db.h>
  65. #include "btree.h"
  66. static void mswap __P((PAGE *));
  67. /*
  68. * __BT_BPGIN, __BT_BPGOUT --
  69. * Convert host-specific number layout to/from the host-independent
  70. * format stored on disk.
  71. *
  72. * Parameters:
  73. * t: tree
  74. * pg: page number
  75. * h: page to convert
  76. */
  77. void
  78. __bt_pgin(void *t, pgno_t pg, void *pp)
  79. {
  80. PAGE *h;
  81. int i, top;
  82. u_char flags;
  83. char *p;
  84. if (!ISSET(((BTREE *)t), B_NEEDSWAP))
  85. return;
  86. if (pg == P_META) {
  87. mswap(pp);
  88. return;
  89. }
  90. h = pp;
  91. BLSWAP(h->pgno);
  92. BLSWAP(h->prevpg);
  93. BLSWAP(h->nextpg);
  94. BLSWAP(h->flags);
  95. BSSWAP(h->lower);
  96. BSSWAP(h->upper);
  97. top = NEXTINDEX(h);
  98. if ((h->flags & P_TYPE) == P_BINTERNAL)
  99. for (i = 0; i < top; i++) {
  100. BSSWAP(h->linp[i]);
  101. p = (char *)GETBINTERNAL(h, i);
  102. BLPSWAP(p);
  103. p += sizeof(size_t);
  104. BLPSWAP(p);
  105. p += sizeof(pgno_t);
  106. if (*(u_char *)p & P_BIGKEY) {
  107. p += sizeof(u_char);
  108. BLPSWAP(p);
  109. p += sizeof(pgno_t);
  110. BLPSWAP(p);
  111. }
  112. }
  113. else if ((h->flags & P_TYPE) == P_BLEAF)
  114. for (i = 0; i < top; i++) {
  115. BSSWAP(h->linp[i]);
  116. p = (char *)GETBLEAF(h, i);
  117. BLPSWAP(p);
  118. p += sizeof(size_t);
  119. BLPSWAP(p);
  120. p += sizeof(size_t);
  121. flags = *(u_char *)p;
  122. if (flags & (P_BIGKEY | P_BIGDATA)) {
  123. p += sizeof(u_char);
  124. if (flags & P_BIGKEY) {
  125. BLPSWAP(p);
  126. p += sizeof(pgno_t);
  127. BLPSWAP(p);
  128. }
  129. if (flags & P_BIGDATA) {
  130. p += sizeof(size_t);
  131. BLPSWAP(p);
  132. p += sizeof(pgno_t);
  133. BLPSWAP(p);
  134. }
  135. }
  136. }
  137. }
  138. void
  139. __bt_pgout(void *t, pgno_t pg, void *pp)
  140. {
  141. PAGE *h;
  142. int i, top;
  143. u_char flags;
  144. char *p;
  145. if (!ISSET(((BTREE *)t), B_NEEDSWAP))
  146. return;
  147. if (pg == P_META) {
  148. mswap(pp);
  149. return;
  150. }
  151. h = pp;
  152. top = NEXTINDEX(h);
  153. if ((h->flags & P_TYPE) == P_BINTERNAL)
  154. for (i = 0; i < top; i++) {
  155. p = (char *)GETBINTERNAL(h, i);
  156. BLPSWAP(p);
  157. p += sizeof(size_t);
  158. BLPSWAP(p);
  159. p += sizeof(pgno_t);
  160. if (*(u_char *)p & P_BIGKEY) {
  161. p += sizeof(u_char);
  162. BLPSWAP(p);
  163. p += sizeof(pgno_t);
  164. BLPSWAP(p);
  165. }
  166. BSSWAP(h->linp[i]);
  167. }
  168. else if ((h->flags & P_TYPE) == P_BLEAF)
  169. for (i = 0; i < top; i++) {
  170. p = (char *)GETBLEAF(h, i);
  171. BLPSWAP(p);
  172. p += sizeof(size_t);
  173. BLPSWAP(p);
  174. p += sizeof(size_t);
  175. flags = *(u_char *)p;
  176. if (flags & (P_BIGKEY | P_BIGDATA)) {
  177. p += sizeof(u_char);
  178. if (flags & P_BIGKEY) {
  179. BLPSWAP(p);
  180. p += sizeof(pgno_t);
  181. BLPSWAP(p);
  182. }
  183. if (flags & P_BIGDATA) {
  184. p += sizeof(size_t);
  185. BLPSWAP(p);
  186. p += sizeof(pgno_t);
  187. BLPSWAP(p);
  188. }
  189. }
  190. BSSWAP(h->linp[i]);
  191. }
  192. BLSWAP(h->pgno);
  193. BLSWAP(h->prevpg);
  194. BLSWAP(h->nextpg);
  195. BLSWAP(h->flags);
  196. BSSWAP(h->lower);
  197. BSSWAP(h->upper);
  198. }
  199. /*
  200. * MSWAP -- Actually swap the bytes on the meta page.
  201. *
  202. * Parameters:
  203. * p: page to convert
  204. */
  205. static void
  206. mswap(PAGE *pg)
  207. {
  208. char *p;
  209. p = (char *)pg;
  210. BLPSWAP(p); /* m_magic */
  211. p += sizeof(u_long);
  212. BLPSWAP(p); /* m_version */
  213. p += sizeof(u_long);
  214. BLPSWAP(p); /* m_psize */
  215. p += sizeof(u_long);
  216. BLPSWAP(p); /* m_free */
  217. p += sizeof(u_long);
  218. BLPSWAP(p); /* m_nrecs */
  219. p += sizeof(u_long);
  220. BLPSWAP(p); /* m_flags */
  221. p += sizeof(u_long);
  222. }