xref: /illumos-gate/usr/src/cmd/sendmail/db/db/db_conv.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*-
2*7c478bd9Sstevel@tonic-gate  * See the file LICENSE for redistribution information.
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996, 1997, 1998
5*7c478bd9Sstevel@tonic-gate  *	Sleepycat Software.  All rights reserved.
6*7c478bd9Sstevel@tonic-gate  */
7*7c478bd9Sstevel@tonic-gate /*
8*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993, 1994, 1995, 1996
9*7c478bd9Sstevel@tonic-gate  *	Keith Bostic.  All rights reserved.
10*7c478bd9Sstevel@tonic-gate  */
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1990, 1993, 1994, 1995
13*7c478bd9Sstevel@tonic-gate  *	The Regents of the University of California.  All rights reserved.
14*7c478bd9Sstevel@tonic-gate  *
15*7c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
16*7c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
17*7c478bd9Sstevel@tonic-gate  * are met:
18*7c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
19*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
20*7c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
21*7c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
22*7c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
23*7c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
24*7c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
25*7c478bd9Sstevel@tonic-gate  *	This product includes software developed by the University of
26*7c478bd9Sstevel@tonic-gate  *	California, Berkeley and its contributors.
27*7c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
28*7c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
29*7c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
30*7c478bd9Sstevel@tonic-gate  *
31*7c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32*7c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33*7c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34*7c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35*7c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*7c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*7c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*7c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39*7c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40*7c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41*7c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
42*7c478bd9Sstevel@tonic-gate  */
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate #include "config.h"
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #ifndef lint
47*7c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)db_conv.c	10.13 (Sleepycat) 4/26/98";
48*7c478bd9Sstevel@tonic-gate #endif /* not lint */
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
51*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate #include <errno.h>
54*7c478bd9Sstevel@tonic-gate #include <string.h>
55*7c478bd9Sstevel@tonic-gate #endif
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #include "db_int.h"
58*7c478bd9Sstevel@tonic-gate #include "db_page.h"
59*7c478bd9Sstevel@tonic-gate #include "db_swap.h"
60*7c478bd9Sstevel@tonic-gate #include "db_am.h"
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static int __db_convert __P((db_pgno_t, void *, size_t, int));
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * __db_pgin --
66*7c478bd9Sstevel@tonic-gate  *
67*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_pgin __P((db_pgno_t, size_t, void *));
68*7c478bd9Sstevel@tonic-gate  */
69*7c478bd9Sstevel@tonic-gate int
__db_pgin(pg,pagesize,pp)70*7c478bd9Sstevel@tonic-gate __db_pgin(pg, pagesize, pp)
71*7c478bd9Sstevel@tonic-gate 	db_pgno_t pg;
72*7c478bd9Sstevel@tonic-gate 	size_t pagesize;
73*7c478bd9Sstevel@tonic-gate 	void *pp;
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	return (__db_convert(pg, pp, pagesize, 1));
76*7c478bd9Sstevel@tonic-gate }
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate /*
79*7c478bd9Sstevel@tonic-gate  * __db_pgout --
80*7c478bd9Sstevel@tonic-gate  *
81*7c478bd9Sstevel@tonic-gate  * PUBLIC: int __db_pgout __P((db_pgno_t, size_t, void *));
82*7c478bd9Sstevel@tonic-gate  */
83*7c478bd9Sstevel@tonic-gate int
__db_pgout(pg,pagesize,pp)84*7c478bd9Sstevel@tonic-gate __db_pgout(pg, pagesize, pp)
85*7c478bd9Sstevel@tonic-gate 	db_pgno_t pg;
86*7c478bd9Sstevel@tonic-gate 	size_t pagesize;
87*7c478bd9Sstevel@tonic-gate 	void *pp;
88*7c478bd9Sstevel@tonic-gate {
89*7c478bd9Sstevel@tonic-gate 	return (__db_convert(pg, pp, pagesize, 0));
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate /*
93*7c478bd9Sstevel@tonic-gate  * __db_convert --
94*7c478bd9Sstevel@tonic-gate  *	Actually convert a page.
95*7c478bd9Sstevel@tonic-gate  */
96*7c478bd9Sstevel@tonic-gate static int
__db_convert(pg,pp,pagesize,pgin)97*7c478bd9Sstevel@tonic-gate __db_convert(pg, pp, pagesize, pgin)
98*7c478bd9Sstevel@tonic-gate 	db_pgno_t pg;
99*7c478bd9Sstevel@tonic-gate 	void *pp;
100*7c478bd9Sstevel@tonic-gate 	size_t pagesize;
101*7c478bd9Sstevel@tonic-gate 	int pgin;
102*7c478bd9Sstevel@tonic-gate {
103*7c478bd9Sstevel@tonic-gate 	BINTERNAL *bi;
104*7c478bd9Sstevel@tonic-gate 	BKEYDATA *bk;
105*7c478bd9Sstevel@tonic-gate 	BOVERFLOW *bo;
106*7c478bd9Sstevel@tonic-gate 	PAGE *h;
107*7c478bd9Sstevel@tonic-gate 	RINTERNAL *ri;
108*7c478bd9Sstevel@tonic-gate 	db_indx_t i, len, tmp;
109*7c478bd9Sstevel@tonic-gate 	u_int8_t *p, *end;
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	COMPQUIET(pg, 0);
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	h = pp;
114*7c478bd9Sstevel@tonic-gate 	if (pgin) {
115*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->lsn.file);
116*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->lsn.offset);
117*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->pgno);
118*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->prev_pgno);
119*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->next_pgno);
120*7c478bd9Sstevel@tonic-gate 		M_16_SWAP(h->entries);
121*7c478bd9Sstevel@tonic-gate 		M_16_SWAP(h->hf_offset);
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	switch (h->type) {
125*7c478bd9Sstevel@tonic-gate 	case P_HASH:
126*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < NUM_ENT(h); i++) {
127*7c478bd9Sstevel@tonic-gate 			if (pgin)
128*7c478bd9Sstevel@tonic-gate 				M_16_SWAP(h->inp[i]);
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 			switch (HPAGE_TYPE(h, i)) {
131*7c478bd9Sstevel@tonic-gate 			case H_KEYDATA:
132*7c478bd9Sstevel@tonic-gate 				break;
133*7c478bd9Sstevel@tonic-gate 			case H_DUPLICATE:
134*7c478bd9Sstevel@tonic-gate 				len = LEN_HKEYDATA(h, pagesize, i);
135*7c478bd9Sstevel@tonic-gate 				p = HKEYDATA_DATA(P_ENTRY(h, i));
136*7c478bd9Sstevel@tonic-gate 				for (end = p + len; p < end;) {
137*7c478bd9Sstevel@tonic-gate 					if (pgin) {
138*7c478bd9Sstevel@tonic-gate 						P_16_SWAP(p);
139*7c478bd9Sstevel@tonic-gate 						memcpy(&tmp,
140*7c478bd9Sstevel@tonic-gate 						    p, sizeof(db_indx_t));
141*7c478bd9Sstevel@tonic-gate 						p += sizeof(db_indx_t);
142*7c478bd9Sstevel@tonic-gate 					} else {
143*7c478bd9Sstevel@tonic-gate 						memcpy(&tmp,
144*7c478bd9Sstevel@tonic-gate 						    p, sizeof(db_indx_t));
145*7c478bd9Sstevel@tonic-gate 						SWAP16(p);
146*7c478bd9Sstevel@tonic-gate 					}
147*7c478bd9Sstevel@tonic-gate 					p += tmp;
148*7c478bd9Sstevel@tonic-gate 					SWAP16(p);
149*7c478bd9Sstevel@tonic-gate 				}
150*7c478bd9Sstevel@tonic-gate 				break;
151*7c478bd9Sstevel@tonic-gate 			case H_OFFDUP:
152*7c478bd9Sstevel@tonic-gate 				p = HOFFPAGE_PGNO(P_ENTRY(h, i));
153*7c478bd9Sstevel@tonic-gate 				SWAP32(p);			/* pgno */
154*7c478bd9Sstevel@tonic-gate 				break;
155*7c478bd9Sstevel@tonic-gate 			case H_OFFPAGE:
156*7c478bd9Sstevel@tonic-gate 				p = HOFFPAGE_PGNO(P_ENTRY(h, i));
157*7c478bd9Sstevel@tonic-gate 				SWAP32(p);			/* pgno */
158*7c478bd9Sstevel@tonic-gate 				SWAP32(p);			/* tlen */
159*7c478bd9Sstevel@tonic-gate 				break;
160*7c478bd9Sstevel@tonic-gate 			}
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 		}
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 		/*
165*7c478bd9Sstevel@tonic-gate 		 * The offsets in the inp array are used to determine
166*7c478bd9Sstevel@tonic-gate 		 * the size of entries on a page; therefore they
167*7c478bd9Sstevel@tonic-gate 		 * cannot be converted until we've done all the
168*7c478bd9Sstevel@tonic-gate 		 * entries.
169*7c478bd9Sstevel@tonic-gate 		 */
170*7c478bd9Sstevel@tonic-gate 		if (!pgin)
171*7c478bd9Sstevel@tonic-gate 			for (i = 0; i < NUM_ENT(h); i++)
172*7c478bd9Sstevel@tonic-gate 				M_16_SWAP(h->inp[i]);
173*7c478bd9Sstevel@tonic-gate 		break;
174*7c478bd9Sstevel@tonic-gate 	case P_LBTREE:
175*7c478bd9Sstevel@tonic-gate 	case P_LRECNO:
176*7c478bd9Sstevel@tonic-gate 	case P_DUPLICATE:
177*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < NUM_ENT(h); i++) {
178*7c478bd9Sstevel@tonic-gate 			if (pgin)
179*7c478bd9Sstevel@tonic-gate 				M_16_SWAP(h->inp[i]);
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 			bk = GET_BKEYDATA(h, i);
182*7c478bd9Sstevel@tonic-gate 			switch (B_TYPE(bk->type)) {
183*7c478bd9Sstevel@tonic-gate 			case B_KEYDATA:
184*7c478bd9Sstevel@tonic-gate 				M_16_SWAP(bk->len);
185*7c478bd9Sstevel@tonic-gate 				break;
186*7c478bd9Sstevel@tonic-gate 			case B_DUPLICATE:
187*7c478bd9Sstevel@tonic-gate 			case B_OVERFLOW:
188*7c478bd9Sstevel@tonic-gate 				bo = (BOVERFLOW *)bk;
189*7c478bd9Sstevel@tonic-gate 				M_32_SWAP(bo->pgno);
190*7c478bd9Sstevel@tonic-gate 				M_32_SWAP(bo->tlen);
191*7c478bd9Sstevel@tonic-gate 				break;
192*7c478bd9Sstevel@tonic-gate 			}
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 			if (!pgin)
195*7c478bd9Sstevel@tonic-gate 				M_16_SWAP(h->inp[i]);
196*7c478bd9Sstevel@tonic-gate 		}
197*7c478bd9Sstevel@tonic-gate 		break;
198*7c478bd9Sstevel@tonic-gate 	case P_IBTREE:
199*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < NUM_ENT(h); i++) {
200*7c478bd9Sstevel@tonic-gate 			if (pgin)
201*7c478bd9Sstevel@tonic-gate 				M_16_SWAP(h->inp[i]);
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 			bi = GET_BINTERNAL(h, i);
204*7c478bd9Sstevel@tonic-gate 			M_16_SWAP(bi->len);
205*7c478bd9Sstevel@tonic-gate 			M_32_SWAP(bi->pgno);
206*7c478bd9Sstevel@tonic-gate 			M_32_SWAP(bi->nrecs);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 			switch (B_TYPE(bi->type)) {
209*7c478bd9Sstevel@tonic-gate 			case B_KEYDATA:
210*7c478bd9Sstevel@tonic-gate 				break;
211*7c478bd9Sstevel@tonic-gate 			case B_DUPLICATE:
212*7c478bd9Sstevel@tonic-gate 			case B_OVERFLOW:
213*7c478bd9Sstevel@tonic-gate 				bo = (BOVERFLOW *)bi->data;
214*7c478bd9Sstevel@tonic-gate 				M_32_SWAP(bo->pgno);
215*7c478bd9Sstevel@tonic-gate 				M_32_SWAP(bo->tlen);
216*7c478bd9Sstevel@tonic-gate 				break;
217*7c478bd9Sstevel@tonic-gate 			}
218*7c478bd9Sstevel@tonic-gate 
219*7c478bd9Sstevel@tonic-gate 			if (!pgin)
220*7c478bd9Sstevel@tonic-gate 				M_16_SWAP(h->inp[i]);
221*7c478bd9Sstevel@tonic-gate 		}
222*7c478bd9Sstevel@tonic-gate 		break;
223*7c478bd9Sstevel@tonic-gate 	case P_IRECNO:
224*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < NUM_ENT(h); i++) {
225*7c478bd9Sstevel@tonic-gate 			if (pgin)
226*7c478bd9Sstevel@tonic-gate 				M_16_SWAP(h->inp[i]);
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 			ri = GET_RINTERNAL(h, i);
229*7c478bd9Sstevel@tonic-gate 			M_32_SWAP(ri->pgno);
230*7c478bd9Sstevel@tonic-gate 			M_32_SWAP(ri->nrecs);
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 			if (!pgin)
233*7c478bd9Sstevel@tonic-gate 				M_16_SWAP(h->inp[i]);
234*7c478bd9Sstevel@tonic-gate 		}
235*7c478bd9Sstevel@tonic-gate 		break;
236*7c478bd9Sstevel@tonic-gate 	case P_OVERFLOW:
237*7c478bd9Sstevel@tonic-gate 	case P_INVALID:
238*7c478bd9Sstevel@tonic-gate 		/* Nothing to do. */
239*7c478bd9Sstevel@tonic-gate 		break;
240*7c478bd9Sstevel@tonic-gate 	default:
241*7c478bd9Sstevel@tonic-gate 		return (EINVAL);
242*7c478bd9Sstevel@tonic-gate 	}
243*7c478bd9Sstevel@tonic-gate 
244*7c478bd9Sstevel@tonic-gate 	if (!pgin) {
245*7c478bd9Sstevel@tonic-gate 		/* Swap the header information. */
246*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->lsn.file);
247*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->lsn.offset);
248*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->pgno);
249*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->prev_pgno);
250*7c478bd9Sstevel@tonic-gate 		M_32_SWAP(h->next_pgno);
251*7c478bd9Sstevel@tonic-gate 		M_16_SWAP(h->entries);
252*7c478bd9Sstevel@tonic-gate 		M_16_SWAP(h->hf_offset);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate 	return (0);
255*7c478bd9Sstevel@tonic-gate }
256