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