xref: /original-bsd/lib/libc/db/btree/bt_conv.c (revision 8034f188)
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.7 (Berkeley) 02/14/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 
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, p)
36 	void *t;
37 	pgno_t pg;
38 	void *p;
39 {
40 	PAGE *h;
41 
42 	if (((BTREE *)t)->bt_lorder == BYTE_ORDER)
43 		return;
44 
45 	h = p;
46 	BLSWAP(h->pgno);
47 	BLSWAP(h->prevpg);
48 	BLSWAP(h->nextpg);
49 	BLSWAP(h->flags);
50 	BSSWAP(h->lower);
51 	BSSWAP(h->upper);
52 	kdswap(h);
53 }
54 
55 void
56 __bt_pgout(t, pg, p)
57 	void *t;
58 	pgno_t pg;
59 	void *p;
60 {
61 	PAGE *h;
62 
63 	if (((BTREE *)t)->bt_lorder == BYTE_ORDER)
64 		return;
65 
66 	h = p;
67 	kdswap(h);
68 	BLSWAP(h->pgno);
69 	BLSWAP(h->prevpg);
70 	BLSWAP(h->nextpg);
71 	BLSWAP(h->flags);
72 	BSSWAP(h->lower);
73 	BSSWAP(h->upper);
74 }
75 
76 /*
77  * KDSWAP -- Actually swap the bytes on the page.
78  *
79  * Parameters:
80  *	h:	page to convert
81  *
82  * Warnings:
83  *	Everywhere else in the code, the pgno_t and indx_t types are
84  *	opaque.  These routines know what they really are.
85  */
86 static void
87 kdswap(h)
88 	PAGE *h;
89 {
90 	register int i, top;
91 	register char *p;			/* Really void, thanks ANSI! */
92 	u_char flags;
93 
94 	top = NEXTINDEX(h);
95 	switch (h->flags & P_TYPE) {
96 	case P_BINTERNAL:
97 		for (i = 0; i < top; i++) {
98 			BSSWAP(h->linp[i]);
99 			p = (char *)GETBINTERNAL(h, i);
100 			BLPSWAP(p);
101 			p += sizeof(size_t);
102 			BLPSWAP(p);
103 			p += sizeof(pgno_t);
104 			if (*(u_char *)p & P_BIGKEY) {
105 				p += sizeof(u_char);
106 				BLPSWAP(p);
107 				p += sizeof(pgno_t);
108 				BLPSWAP(p);
109 			}
110 		}
111 		break;
112 	case P_BLEAF:
113 		for (i = 0; i < top; i++) {
114 			BSSWAP(h->linp[i]);
115 			p = (char *)GETBLEAF(h, i);
116 			BLPSWAP(p);
117 			p += sizeof(size_t);
118 			BLPSWAP(p);
119 			p += sizeof(size_t);
120 			flags = *(u_char *)p;
121 			if (flags & (P_BIGKEY | P_BIGDATA)) {
122 				p += sizeof(u_char);
123 				if (flags & P_BIGKEY) {
124 					BLPSWAP(p);
125 					p += sizeof(pgno_t);
126 					BLPSWAP(p);
127 				}
128 				if (flags & P_BIGDATA) {
129 					p += sizeof(size_t);
130 					BLPSWAP(p);
131 					p += sizeof(pgno_t);
132 					BLPSWAP(p);
133 				}
134 			}
135 		}
136 		break;
137 	}
138 }
139