xref: /original-bsd/lib/libc/db/btree/bt_conv.c (revision 9271068d)
1 /*-
2  * Copyright (c) 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * This code is derived from software contributed to Berkeley by
6  * Mike Olson.
7  *
8  * %sccs.include.redist.c%
9  */
10 
11 #if defined(LIBC_SCCS) && !defined(lint)
12 static char sccsid[] = "@(#)bt_conv.c	5.10 (Berkeley) 05/16/93";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/param.h>
16 
17 #include <stdio.h>
18 
19 #include <db.h>
20 #include "btree.h"
21 
22 static void mswap __P((PAGE *));
23 
24 /*
25  * __BT_BPGIN, __BT_BPGOUT --
26  *	Convert host-specific number layout to/from the host-independent
27  *	format stored on disk.
28  *
29  * Parameters:
30  *	t:	tree
31  *	pg:	page number
32  *	h:	page to convert
33  */
34 void
35 __bt_pgin(t, pg, pp)
36 	void *t;
37 	pgno_t pg;
38 	void *pp;
39 {
40 	PAGE *h;
41 	int i, top;
42 	u_char flags;
43 	char *p;
44 
45 	if (!ISSET(((BTREE *)t), B_NEEDSWAP))
46 		return;
47 	if (pg == P_META) {
48 		mswap(pp);
49 		return;
50 	}
51 
52 	h = pp;
53 	BLSWAP(h->pgno);
54 	BLSWAP(h->prevpg);
55 	BLSWAP(h->nextpg);
56 	BLSWAP(h->flags);
57 	BSSWAP(h->lower);
58 	BSSWAP(h->upper);
59 
60 	top = NEXTINDEX(h);
61 	if ((h->flags & P_TYPE) == P_BINTERNAL)
62 		for (i = 0; i < top; i++) {
63 			BSSWAP(h->linp[i]);
64 			p = (char *)GETBINTERNAL(h, i);
65 			BLPSWAP(p);
66 			p += sizeof(size_t);
67 			BLPSWAP(p);
68 			p += sizeof(pgno_t);
69 			if (*(u_char *)p & P_BIGKEY) {
70 				p += sizeof(u_char);
71 				BLPSWAP(p);
72 				p += sizeof(pgno_t);
73 				BLPSWAP(p);
74 			}
75 		}
76 	else if ((h->flags & P_TYPE) == P_BLEAF)
77 		for (i = 0; i < top; i++) {
78 			BSSWAP(h->linp[i]);
79 			p = (char *)GETBLEAF(h, i);
80 			BLPSWAP(p);
81 			p += sizeof(size_t);
82 			BLPSWAP(p);
83 			p += sizeof(size_t);
84 			flags = *(u_char *)p;
85 			if (flags & (P_BIGKEY | P_BIGDATA)) {
86 				p += sizeof(u_char);
87 				if (flags & P_BIGKEY) {
88 					BLPSWAP(p);
89 					p += sizeof(pgno_t);
90 					BLPSWAP(p);
91 				}
92 				if (flags & P_BIGDATA) {
93 					p += sizeof(size_t);
94 					BLPSWAP(p);
95 					p += sizeof(pgno_t);
96 					BLPSWAP(p);
97 				}
98 			}
99 		}
100 }
101 
102 void
103 __bt_pgout(t, pg, pp)
104 	void *t;
105 	pgno_t pg;
106 	void *pp;
107 {
108 	PAGE *h;
109 	int i, top;
110 	u_char flags;
111 	char *p;
112 
113 	if (!ISSET(((BTREE *)t), B_NEEDSWAP))
114 		return;
115 	if (pg == P_META) {
116 		mswap(pp);
117 		return;
118 	}
119 
120 	h = pp;
121 	top = NEXTINDEX(h);
122 	if ((h->flags & P_TYPE) == P_BINTERNAL)
123 		for (i = 0; i < top; i++) {
124 			p = (char *)GETBINTERNAL(h, i);
125 			BLPSWAP(p);
126 			p += sizeof(size_t);
127 			BLPSWAP(p);
128 			p += sizeof(pgno_t);
129 			if (*(u_char *)p & P_BIGKEY) {
130 				p += sizeof(u_char);
131 				BLPSWAP(p);
132 				p += sizeof(pgno_t);
133 				BLPSWAP(p);
134 			}
135 			BSSWAP(h->linp[i]);
136 		}
137 	else if ((h->flags & P_TYPE) == P_BLEAF)
138 		for (i = 0; i < top; i++) {
139 			p = (char *)GETBLEAF(h, i);
140 			BLPSWAP(p);
141 			p += sizeof(size_t);
142 			BLPSWAP(p);
143 			p += sizeof(size_t);
144 			flags = *(u_char *)p;
145 			if (flags & (P_BIGKEY | P_BIGDATA)) {
146 				p += sizeof(u_char);
147 				if (flags & P_BIGKEY) {
148 					BLPSWAP(p);
149 					p += sizeof(pgno_t);
150 					BLPSWAP(p);
151 				}
152 				if (flags & P_BIGDATA) {
153 					p += sizeof(size_t);
154 					BLPSWAP(p);
155 					p += sizeof(pgno_t);
156 					BLPSWAP(p);
157 				}
158 			}
159 			BSSWAP(h->linp[i]);
160 		}
161 
162 	BLSWAP(h->pgno);
163 	BLSWAP(h->prevpg);
164 	BLSWAP(h->nextpg);
165 	BLSWAP(h->flags);
166 	BSSWAP(h->lower);
167 	BSSWAP(h->upper);
168 }
169 
170 /*
171  * MSWAP -- Actually swap the bytes on the meta page.
172  *
173  * Parameters:
174  *	p:	page to convert
175  */
176 static void
177 mswap(pg)
178 	PAGE *pg;
179 {
180 	char *p;
181 
182 	p = (char *)pg;
183 	BLPSWAP(p);		/* m_magic */
184 	p += sizeof(u_long);
185 	BLPSWAP(p);		/* m_version */
186 	p += sizeof(u_long);
187 	BLPSWAP(p);		/* m_psize */
188 	p += sizeof(u_long);
189 	BLPSWAP(p);		/* m_free */
190 	p += sizeof(u_long);
191 	BLPSWAP(p);		/* m_nrecs */
192 	p += sizeof(u_long);
193 	BLPSWAP(p);		/* m_flags */
194 	p += sizeof(u_long);
195 }
196