1 /*
2  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 #ifndef _KRB5_DB2_MISC_H
7 #define	_KRB5_DB2_MISC_H
8 
9 #pragma ident	"%Z%%M%	%I%	%E% SMI"
10 
11 #ifdef	__cplusplus
12 extern "C" {
13 #endif
14 
15 /*
16  * Copyright (c) 1991, 1993
17  *	The Regents of the University of California.  All rights reserved.
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions
21  * are met:
22  * 1. Redistributions of source code must retain the above copyright
23  *    notice, this list of conditions and the following disclaimer.
24  * 2. Redistributions in binary form must reproduce the above copyright
25  *    notice, this list of conditions and the following disclaimer in the
26  *    documentation and/or other materials provided with the distribution.
27  * 3. All advertising materials mentioning features or use of this software
28  *    must display the following acknowledgement:
29  *	This product includes software developed by the University of
30  *	California, Berkeley and its contributors.
31  * 4. Neither the name of the University nor the names of its contributors
32  *    may be used to endorse or promote products derived from this software
33  *    without specific prior written permission.
34  *
35  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  *
47  *	@(#)compat.h	8.13 (Berkeley) 2/21/94
48  */
49 
50 #ifndef	_DB_INT_H_
51 #define	_DB_INT_H_
52 
53 #include <db.h>
54 
55 /* deal with autoconf-based stuff */
56 
57 #define DB_LITTLE_ENDIAN 1234
58 #define DB_BIG_ENDIAN 4321
59 
60 #include <stdlib.h>
61 #ifdef HAVE_ENDIAN_H
62 # include <endian.h>
63 #endif
64 #ifdef HAVE_MACHINE_ENDIAN_H
65 # include <machine/endian.h>
66 #endif
67 #ifdef HAVE_SYS_PARAM_H
68 # include <sys/param.h>
69 #endif
70 
71 /* SUNW14resync:
72    The following code is disabled as it correctly determines the
73    endianness of the system. This would break backward compatability
74    for x86 as prior to this resync all architectures are treated
75    similarily - as big endian. See definition of "WORDS_BIGENDIAN" in
76    db-config.h.
77 */
78 #if 0
79 /* Handle both BIG and LITTLE defined and BYTE_ORDER matches one, or
80    just one defined; both with and without leading underscores.
81 
82    Ignore "PDP endian" machines, this code doesn't support them
83    anyways.  */
84 #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER)
85 # ifdef __LITTLE_ENDIAN__
86 #  define LITTLE_ENDIAN __LITTLE_ENDIAN__
87 # endif
88 # ifdef __BIG_ENDIAN__
89 #  define BIG_ENDIAN __BIG_ENDIAN__
90 # endif
91 #endif
92 #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER)
93 # ifdef _LITTLE_ENDIAN
94 #  define LITTLE_ENDIAN _LITTLE_ENDIAN
95 # endif
96 # ifdef _BIG_ENDIAN
97 #  define BIG_ENDIAN _BIG_ENDIAN
98 # endif
99 # ifdef _BYTE_ORDER
100 #  define BYTE_ORDER _BYTE_ORDER
101 # endif
102 #endif
103 #if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER)
104 # ifdef __LITTLE_ENDIAN
105 #  define LITTLE_ENDIAN __LITTLE_ENDIAN
106 # endif
107 # ifdef __BIG_ENDIAN
108 #  define BIG_ENDIAN __BIG_ENDIAN
109 # endif
110 # ifdef __BYTE_ORDER
111 #  define BYTE_ORDER __BYTE_ORDER
112 # endif
113 #endif
114 
115 #if defined(_MIPSEL) && !defined(LITTLE_ENDIAN)
116 # define LITTLE_ENDIAN
117 #endif
118 #if defined(_MIPSEB) && !defined(BIG_ENDIAN)
119 # define BIG_ENDIAN
120 #endif
121 
122 #if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) && defined(BYTE_ORDER)
123 # if LITTLE_ENDIAN == BYTE_ORDER
124 #  define DB_BYTE_ORDER DB_LITTLE_ENDIAN
125 # elif BIG_ENDIAN == BYTE_ORDER
126 #  define DB_BYTE_ORDER DB_BIG_ENDIAN
127 # else
128 #  error "LITTLE_ENDIAN and BIG_ENDIAN defined, but can't determine byte order"
129 # endif
130 #elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
131 # define DB_BYTE_ORDER DB_LITTLE_ENDIAN
132 #elif defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN)
133 # define DB_BYTE_ORDER DB_BIG_ENDIAN
134 #else
135 # error "can't determine byte order from included system headers"
136 #endif
137 
138 #endif
139 
140 
141 #ifdef WORDS_BIGENDIAN
142 #define DB_BYTE_ORDER DB_BIG_ENDIAN
143 #else
144 #define DB_BYTE_ORDER DB_LITTLE_ENDIAN
145 #endif
146 
147 /* end autoconf-based stuff */
148 
149 /* include necessary system header files */
150 
151 #ifdef HAVE_UNISTD_H
152 #include <unistd.h>
153 #endif
154 #include <limits.h>
155 #include <fcntl.h>
156 #include <stdio.h>
157 #include <errno.h>
158 #ifdef HAVE_STDINT_H
159 #include <stdint.h>
160 #endif
161 #ifdef HAVE_INTTYPES_H
162 /* Tru64 5.1: int8_t is defined here, and stdint.h doesn't exist.  */
163 #include <inttypes.h>
164 #endif
165 #include <sys/types.h>
166 #include <sys/stat.h>
167 #include <sys/param.h>
168 
169 /* types and constants used for database structure */
170 
171 #define	MAX_PAGE_NUMBER	0xffffffff	/* >= # of pages in a file */
172 typedef u_int32_t	db_pgno_t;
173 #define	MAX_PAGE_OFFSET	65535		/* >= # of bytes in a page */
174 typedef u_int16_t	indx_t;
175 #define	MAX_REC_NUMBER	0xffffffff	/* >= # of records in a tree */
176 typedef u_int32_t	recno_t;
177 
178 /*
179  * Little endian <==> big endian 32-bit swap macros.
180  *	M_32_SWAP	swap a memory location
181  *	P_32_SWAP	swap a referenced memory location
182  *	P_32_COPY	swap from one location to another
183  */
184 #define	M_32_SWAP(a) {							\
185 	u_int32_t _tmp = a;						\
186 	((char *)&a)[0] = ((char *)&_tmp)[3];				\
187 	((char *)&a)[1] = ((char *)&_tmp)[2];				\
188 	((char *)&a)[2] = ((char *)&_tmp)[1];				\
189 	((char *)&a)[3] = ((char *)&_tmp)[0];				\
190 }
191 #define	P_32_SWAP(a) {							\
192 	u_int32_t _tmp = *(u_int32_t *)a;				\
193 	((char *)a)[0] = ((char *)&_tmp)[3];				\
194 	((char *)a)[1] = ((char *)&_tmp)[2];				\
195 	((char *)a)[2] = ((char *)&_tmp)[1];				\
196 	((char *)a)[3] = ((char *)&_tmp)[0];				\
197 }
198 #define	P_32_COPY(a, b) {						\
199 	((char *)&(b))[0] = ((char *)&(a))[3];				\
200 	((char *)&(b))[1] = ((char *)&(a))[2];				\
201 	((char *)&(b))[2] = ((char *)&(a))[1];				\
202 	((char *)&(b))[3] = ((char *)&(a))[0];				\
203 }
204 
205 /*
206  * Little endian <==> big endian 16-bit swap macros.
207  *	M_16_SWAP	swap a memory location
208  *	P_16_SWAP	swap a referenced memory location
209  *	P_16_COPY	swap from one location to another
210  */
211 #define	M_16_SWAP(a) {							\
212 	u_int16_t _tmp = a;						\
213 	((char *)&a)[0] = ((char *)&_tmp)[1];				\
214 	((char *)&a)[1] = ((char *)&_tmp)[0];				\
215 }
216 #define	P_16_SWAP(a) {							\
217 	u_int16_t _tmp = *(u_int16_t *)a;				\
218 	((char *)a)[0] = ((char *)&_tmp)[1];				\
219 	((char *)a)[1] = ((char *)&_tmp)[0];				\
220 }
221 #define	P_16_COPY(a, b) {						\
222 	((char *)&(b))[0] = ((char *)&(a))[1];				\
223 	((char *)&(b))[1] = ((char *)&(a))[0];				\
224 }
225 
226 /* open functions for each database type, used in dbopen() */
227 
228 #define __bt_open	__kdb2_bt_open
229 #define __hash_open	__kdb2_hash_open
230 #define __rec_open	__kdb2_rec_open
231 #define __dbpanic	__kdb2_dbpanic
232 
233 DB	*__bt_open __P((const char *, int, int, const BTREEINFO *, int));
234 DB	*__hash_open __P((const char *, int, int, const HASHINFO *, int));
235 DB	*__rec_open __P((const char *, int, int, const RECNOINFO *, int));
236 void	 __dbpanic __P((DB *dbp));
237 
238 /*
239  * There is no portable way to figure out the maximum value of a file
240  * offset, so we put it here.
241  */
242 #ifdef	OFF_T_MAX
243 #define	DB_OFF_T_MAX	OFF_T_MAX
244 #else
245 #define	DB_OFF_T_MAX	LONG_MAX
246 #endif
247 
248 #ifndef O_ACCMODE			/* POSIX 1003.1 access mode mask. */
249 #define	O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
250 #endif
251 
252 /*
253  * If you can't provide lock values in the open(2) call.  Note, this
254  * allows races to happen.
255  */
256 #ifndef O_EXLOCK			/* 4.4BSD extension. */
257 #define	O_EXLOCK	0
258 #endif
259 
260 #ifndef O_SHLOCK			/* 4.4BSD extension. */
261 #define	O_SHLOCK	0
262 #endif
263 
264 #ifndef EFTYPE
265 #define	EFTYPE		EINVAL		/* POSIX 1003.1 format errno. */
266 #endif
267 
268 #ifndef	STDERR_FILENO
269 #define	STDIN_FILENO	0		/* ANSI C #defines */
270 #define	STDOUT_FILENO	1
271 #define	STDERR_FILENO	2
272 #endif
273 
274 #ifndef SEEK_END
275 #define	SEEK_SET	0		/* POSIX 1003.1 seek values */
276 #define	SEEK_CUR	1
277 #define	SEEK_END	2
278 #endif
279 
280 #ifndef NULL				/* ANSI C #defines NULL everywhere. */
281 #define	NULL		0
282 #endif
283 
284 #ifndef	MAX				/* Usually found in <sys/param.h>. */
285 #define	MAX(_a,_b)	((_a)<(_b)?(_b):(_a))
286 #endif
287 #ifndef	MIN				/* Usually found in <sys/param.h>. */
288 #define	MIN(_a,_b)	((_a)<(_b)?(_a):(_b))
289 #endif
290 
291 #ifndef S_ISDIR				/* POSIX 1003.1 file type tests. */
292 #define	S_ISDIR(m)	((m & 0170000) == 0040000)	/* directory */
293 #define	S_ISCHR(m)	((m & 0170000) == 0020000)	/* char special */
294 #define	S_ISBLK(m)	((m & 0170000) == 0060000)	/* block special */
295 #define	S_ISREG(m)	((m & 0170000) == 0100000)	/* regular file */
296 #define	S_ISFIFO(m)	((m & 0170000) == 0010000)	/* fifo */
297 #endif
298 #ifndef S_ISLNK				/* BSD POSIX 1003.1 extensions */
299 #define	S_ISLNK(m)	((m & 0170000) == 0120000)	/* symbolic link */
300 #define	S_ISSOCK(m)	((m & 0170000) == 0140000)	/* socket */
301 #endif
302 
303 #ifndef O_BINARY
304 #define O_BINARY	0		/* Needed for Win32 compiles */
305 #endif
306 #endif /* _DB_INT_H_ */
307 
308 #ifdef	__cplusplus
309 }
310 #endif
311 
312 #endif	/* !_KRB5_DB2_MISC_H */
313