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