xref: /original-bsd/lib/libc/db/btree/bt_conv.c (revision ca98dac2)
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.4 (Berkeley) 12/16/91";
13 #endif /* LIBC_SCCS and not lint */
14 
15 #include <sys/param.h>
16 #include <db.h>
17 #include <stdio.h>
18 #include "btree.h"
19 
20 static void kdswap __P((PAGE *));
21 
22 /*
23  * __BT_BPGIN, __BT_BPGOUT --
24  *	Convert host-specific number layout to/from the host-independent
25  *	format stored on disk.
26  *
27  * Parameters:
28  *	t:	tree
29  *	pg:	page number
30  *	h:	page to convert
31  */
32 void
33 __bt_pgin(t, pg, p)
34 	void *t;
35 	pgno_t pg;
36 	void *p;
37 {
38 	PAGE *h;
39 
40 	if (((BTREE *)t)->bt_lorder == BYTE_ORDER)
41 		return;
42 
43 	h = p;
44 	BLSWAP(h->pgno);
45 	BLSWAP(h->prevpg);
46 	BLSWAP(h->nextpg);
47 	BLSWAP(h->flags);
48 	BSSWAP(h->lower);
49 	BSSWAP(h->upper);
50 	kdswap(h);
51 }
52 
53 void
54 __bt_pgout(t, pg, p)
55 	void *t;
56 	pgno_t pg;
57 	void *p;
58 {
59 	PAGE *h;
60 
61 	if (((BTREE *)t)->bt_lorder == BYTE_ORDER)
62 		return;
63 
64 	h = p;
65 	kdswap(h);
66 	BLSWAP(h->pgno);
67 	BLSWAP(h->prevpg);
68 	BLSWAP(h->nextpg);
69 	BLSWAP(h->flags);
70 	BSSWAP(h->lower);
71 	BSSWAP(h->upper);
72 }
73 
74 /*
75  * KDSWAP -- Actually swap the bytes on the page.
76  *
77  * Parameters:
78  *	h:	page to convert
79  *
80  * Warnings:
81  *	Everywhere else in the code, the pgno_t and index_t types are
82  *	opaque.  These routines know what they really are.
83  */
84 static void
85 kdswap(h)
86 	PAGE *h;
87 {
88 	register int i, top;
89 	register char *p;			/* Really void, thanks ANSI! */
90 	u_char flags;
91 
92 	top = NEXTINDEX(h);
93 	switch (h->flags & P_TYPE) {
94 	case P_BINTERNAL:
95 		for (i = 0; i < top; i++) {
96 			BSSWAP(h->linp[i]);
97 			p = (char *)GETBINTERNAL(h, i);
98 			BLPSWAP(p);
99 			p += sizeof(size_t);
100 			BLPSWAP(p);
101 			p += sizeof(pgno_t);
102 			if (*(u_char *)p & P_BIGKEY) {
103 				p += sizeof(u_char);
104 				BLPSWAP(p);
105 				p += sizeof(pgno_t);
106 				BLPSWAP(p);
107 			}
108 		}
109 		break;
110 	case P_BLEAF:
111 		for (i = 0; i < top; i++) {
112 			BSSWAP(h->linp[i]);
113 			p = (char *)GETBLEAF(h, i);
114 			BLPSWAP(p);
115 			p += sizeof(size_t);
116 			BLPSWAP(p);
117 			p += sizeof(size_t);
118 			flags = *(u_char *)p;
119 			if (flags & (P_BIGKEY | P_BIGDATA)) {
120 				p += sizeof(u_char);
121 				if (flags & P_BIGKEY) {
122 					BLPSWAP(p);
123 					p += sizeof(pgno_t);
124 					BLPSWAP(p);
125 				}
126 				if (flags & P_BIGDATA) {
127 					p += sizeof(size_t);
128 					BLPSWAP(p);
129 					p += sizeof(pgno_t);
130 					BLPSWAP(p);
131 				}
132 			}
133 		}
134 		break;
135 	}
136 }
137