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