17c478bdstevel@tonic-gate/*
2159d09aMark Phalan * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
37c478bdstevel@tonic-gate * Use is subject to license terms.
47c478bdstevel@tonic-gate */
57c478bdstevel@tonic-gate
67c478bdstevel@tonic-gate#ifndef _KRB5_DB2_MISC_H
77c478bdstevel@tonic-gate#define	_KRB5_DB2_MISC_H
87c478bdstevel@tonic-gate
97c478bdstevel@tonic-gate
107c478bdstevel@tonic-gate#ifdef	__cplusplus
117c478bdstevel@tonic-gateextern "C" {
127c478bdstevel@tonic-gate#endif
137c478bdstevel@tonic-gate
147c478bdstevel@tonic-gate/*
157c478bdstevel@tonic-gate * Copyright (c) 1991, 1993
167c478bdstevel@tonic-gate *	The Regents of the University of California.  All rights reserved.
177c478bdstevel@tonic-gate *
187c478bdstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
197c478bdstevel@tonic-gate * modification, are permitted provided that the following conditions
207c478bdstevel@tonic-gate * are met:
217c478bdstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
227c478bdstevel@tonic-gate *    notice, this list of conditions and the following disclaimer.
237c478bdstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
247c478bdstevel@tonic-gate *    notice, this list of conditions and the following disclaimer in the
257c478bdstevel@tonic-gate *    documentation and/or other materials provided with the distribution.
267c478bdstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
277c478bdstevel@tonic-gate *    must display the following acknowledgement:
287c478bdstevel@tonic-gate *	This product includes software developed by the University of
297c478bdstevel@tonic-gate *	California, Berkeley and its contributors.
307c478bdstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
317c478bdstevel@tonic-gate *    may be used to endorse or promote products derived from this software
327c478bdstevel@tonic-gate *    without specific prior written permission.
337c478bdstevel@tonic-gate *
347c478bdstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
357c478bdstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
367c478bdstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
377c478bdstevel@tonic-gate * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
387c478bdstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
397c478bdstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
407c478bdstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
417c478bdstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
427c478bdstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
437c478bdstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
447c478bdstevel@tonic-gate * SUCH DAMAGE.
457c478bdstevel@tonic-gate *
467c478bdstevel@tonic-gate *	@(#)compat.h	8.13 (Berkeley) 2/21/94
477c478bdstevel@tonic-gate */
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gate#ifndef	_DB_INT_H_
507c478bdstevel@tonic-gate#define	_DB_INT_H_
517c478bdstevel@tonic-gate
52159d09aMark Phalan#include "db.h"
537c478bdstevel@tonic-gate
5456a424cmp/* deal with autoconf-based stuff */
557c478bdstevel@tonic-gate
5656a424cmp#define DB_LITTLE_ENDIAN 1234
5756a424cmp#define DB_BIG_ENDIAN 4321
5856a424cmp
5956a424cmp#include <stdlib.h>
6056a424cmp#ifdef HAVE_ENDIAN_H
6156a424cmp# include <endian.h>
6256a424cmp#endif
6356a424cmp#ifdef HAVE_MACHINE_ENDIAN_H
6456a424cmp# include <machine/endian.h>
6556a424cmp#endif
6656a424cmp#ifdef HAVE_SYS_PARAM_H
6756a424cmp# include <sys/param.h>
687c478bdstevel@tonic-gate#endif
697c478bdstevel@tonic-gate
7056a424cmp/* SUNW14resync:
7156a424cmp   The following code is disabled as it correctly determines the
7256a424cmp   endianness of the system. This would break backward compatability
7356a424cmp   for x86 as prior to this resync all architectures are treated
7456a424cmp   similarily - as big endian. See definition of "WORDS_BIGENDIAN" in
7556a424cmp   db-config.h.
7656a424cmp*/
7756a424cmp#if 0
7856a424cmp/* Handle both BIG and LITTLE defined and BYTE_ORDER matches one, or
7956a424cmp   just one defined; both with and without leading underscores.
8056a424cmp
8156a424cmp   Ignore "PDP endian" machines, this code doesn't support them
8256a424cmp   anyways.  */
8356a424cmp#if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER)
8456a424cmp# ifdef __LITTLE_ENDIAN__
8556a424cmp#  define LITTLE_ENDIAN __LITTLE_ENDIAN__
8656a424cmp# endif
8756a424cmp# ifdef __BIG_ENDIAN__
8856a424cmp#  define BIG_ENDIAN __BIG_ENDIAN__
8956a424cmp# endif
9056a424cmp#endif
9156a424cmp#if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER)
9256a424cmp# ifdef _LITTLE_ENDIAN
9356a424cmp#  define LITTLE_ENDIAN _LITTLE_ENDIAN
9456a424cmp# endif
9556a424cmp# ifdef _BIG_ENDIAN
9656a424cmp#  define BIG_ENDIAN _BIG_ENDIAN
9756a424cmp# endif
9856a424cmp# ifdef _BYTE_ORDER
9956a424cmp#  define BYTE_ORDER _BYTE_ORDER
10056a424cmp# endif
10156a424cmp#endif
10256a424cmp#if !defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN) && !defined(BYTE_ORDER)
10356a424cmp# ifdef __LITTLE_ENDIAN
10456a424cmp#  define LITTLE_ENDIAN __LITTLE_ENDIAN
10556a424cmp# endif
10656a424cmp# ifdef __BIG_ENDIAN
10756a424cmp#  define BIG_ENDIAN __BIG_ENDIAN
10856a424cmp# endif
10956a424cmp# ifdef __BYTE_ORDER
11056a424cmp#  define BYTE_ORDER __BYTE_ORDER
11156a424cmp# endif
11256a424cmp#endif
11356a424cmp
11456a424cmp#if defined(_MIPSEL) && !defined(LITTLE_ENDIAN)
11556a424cmp# define LITTLE_ENDIAN
11656a424cmp#endif
11756a424cmp#if defined(_MIPSEB) && !defined(BIG_ENDIAN)
11856a424cmp# define BIG_ENDIAN
11956a424cmp#endif
12056a424cmp
12156a424cmp#if defined(LITTLE_ENDIAN) && defined(BIG_ENDIAN) && defined(BYTE_ORDER)
12256a424cmp# if LITTLE_ENDIAN == BYTE_ORDER
12356a424cmp#  define DB_BYTE_ORDER DB_LITTLE_ENDIAN
12456a424cmp# elif BIG_ENDIAN == BYTE_ORDER
12556a424cmp#  define DB_BYTE_ORDER DB_BIG_ENDIAN
12656a424cmp# else
12756a424cmp#  error "LITTLE_ENDIAN and BIG_ENDIAN defined, but can't determine byte order"
12856a424cmp# endif
12956a424cmp#elif defined(LITTLE_ENDIAN) && !defined(BIG_ENDIAN)
13056a424cmp# define DB_BYTE_ORDER DB_LITTLE_ENDIAN
13156a424cmp#elif defined(BIG_ENDIAN) && !defined(LITTLE_ENDIAN)
13256a424cmp# define DB_BYTE_ORDER DB_BIG_ENDIAN
13356a424cmp#else
13456a424cmp# error "can't determine byte order from included system headers"
1357c478bdstevel@tonic-gate#endif
1367c478bdstevel@tonic-gate
1377c478bdstevel@tonic-gate#endif
1387c478bdstevel@tonic-gate
1397c478bdstevel@tonic-gate
1407c478bdstevel@tonic-gate#ifdef WORDS_BIGENDIAN
1417c478bdstevel@tonic-gate#define DB_BYTE_ORDER DB_BIG_ENDIAN
1427c478bdstevel@tonic-gate#else
1437c478bdstevel@tonic-gate#define DB_BYTE_ORDER DB_LITTLE_ENDIAN
1447c478bdstevel@tonic-gate#endif
1457c478bdstevel@tonic-gate
1467c478bdstevel@tonic-gate/* end autoconf-based stuff */
1477c478bdstevel@tonic-gate
1487c478bdstevel@tonic-gate/* include necessary system header files */
1497c478bdstevel@tonic-gate
1507c478bdstevel@tonic-gate#ifdef HAVE_UNISTD_H
1517c478bdstevel@tonic-gate#include <unistd.h>
1527c478bdstevel@tonic-gate#endif
1537c478bdstevel@tonic-gate#include <limits.h>
1547c478bdstevel@tonic-gate#include <fcntl.h>
1557c478bdstevel@tonic-gate#include <stdio.h>
1567c478bdstevel@tonic-gate#include <errno.h>
15756a424cmp#ifdef HAVE_STDINT_H
15856a424cmp#include <stdint.h>
15956a424cmp#endif
16056a424cmp#ifdef HAVE_INTTYPES_H
16156a424cmp/* Tru64 5.1: int8_t is defined here, and stdint.h doesn't exist.  */
16256a424cmp#include <inttypes.h>
16356a424cmp#endif
1647c478bdstevel@tonic-gate#include <sys/types.h>
1657c478bdstevel@tonic-gate#include <sys/stat.h>
1667c478bdstevel@tonic-gate#include <sys/param.h>
1677c478bdstevel@tonic-gate
1687c478bdstevel@tonic-gate/* types and constants used for database structure */
1697c478bdstevel@tonic-gate
1707c478bdstevel@tonic-gate#define	MAX_PAGE_NUMBER	0xffffffff	/* >= # of pages in a file */
1717c478bdstevel@tonic-gatetypedef u_int32_t	db_pgno_t;
1727c478bdstevel@tonic-gate#define	MAX_PAGE_OFFSET	65535		/* >= # of bytes in a page */
1737c478bdstevel@tonic-gatetypedef u_int16_t	indx_t;
1747c478bdstevel@tonic-gate#define	MAX_REC_NUMBER	0xffffffff	/* >= # of records in a tree */
1757c478bdstevel@tonic-gatetypedef u_int32_t	recno_t;
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate/*
1787c478bdstevel@tonic-gate * Little endian <==> big endian 32-bit swap macros.
1797c478bdstevel@tonic-gate *	M_32_SWAP	swap a memory location
1807c478bdstevel@tonic-gate *	P_32_SWAP	swap a referenced memory location
1817c478bdstevel@tonic-gate *	P_32_COPY	swap from one location to another
1827c478bdstevel@tonic-gate */
1837c478bdstevel@tonic-gate#define	M_32_SWAP(a) {							\
1847c478bdstevel@tonic-gate	u_int32_t _tmp = a;						\
1857c478bdstevel@tonic-gate	((char *)&a)[0] = ((char *)&_tmp)[3];				\
1867c478bdstevel@tonic-gate	((char *)&a)[1] = ((char *)&_tmp)[2];				\
1877c478bdstevel@tonic-gate	((char *)&a)[2] = ((char *)&_tmp)[1];				\
1887c478bdstevel@tonic-gate	((char *)&a)[3] = ((char *)&_tmp)[0];				\
1897c478bdstevel@tonic-gate}
1907c478bdstevel@tonic-gate#define	P_32_SWAP(a) {							\
1917c478bdstevel@tonic-gate	u_int32_t _tmp = *(u_int32_t *)a;				\
1927c478bdstevel@tonic-gate	((char *)a)[0] = ((char *)&_tmp)[3];				\
1937c478bdstevel@tonic-gate	((char *)a)[1] = ((char *)&_tmp)[2];				\
1947c478bdstevel@tonic-gate	((char *)a)[2] = ((char *)&_tmp)[1];				\
1957c478bdstevel@tonic-gate	((char *)a)[3] = ((char *)&_tmp)[0];				\
1967c478bdstevel@tonic-gate}
1977c478bdstevel@tonic-gate#define	P_32_COPY(a, b) {						\
1987c478bdstevel@tonic-gate	((char *)&(b))[0] = ((char *)&(a))[3];				\
1997c478bdstevel@tonic-gate	((char *)&(b))[1] = ((char *)&(a))[2];				\
2007c478bdstevel@tonic-gate	((char *)&(b))[2] = ((char *)&(a))[1];				\
2017c478bdstevel@tonic-gate	((char *)&(b))[3] = ((char *)&(a))[0];				\
2027c478bdstevel@tonic-gate}
2037c478bdstevel@tonic-gate
2047c478bdstevel@tonic-gate/*
2057c478bdstevel@tonic-gate * Little endian <==> big endian 16-bit swap macros.
2067c478bdstevel@tonic-gate *	M_16_SWAP	swap a memory location
2077c478bdstevel@tonic-gate *	P_16_SWAP	swap a referenced memory location
2087c478bdstevel@tonic-gate *	P_16_COPY	swap from one location to another
2097c478bdstevel@tonic-gate */
2107c478bdstevel@tonic-gate#define	M_16_SWAP(a) {							\
2117c478bdstevel@tonic-gate	u_int16_t _tmp = a;						\
2127c478bdstevel@tonic-gate	((char *)&a)[0] = ((char *)&_tmp)[1];				\
2137c478bdstevel@tonic-gate	((char *)&a)[1] = ((char *)&_tmp)[0];				\
2147c478bdstevel@tonic-gate}
2157c478bdstevel@tonic-gate#define	P_16_SWAP(a) {							\
2167c478bdstevel@tonic-gate	u_int16_t _tmp = *(u_int16_t *)a;				\
2177c478bdstevel@tonic-gate	((char *)a)[0] = ((char *)&_tmp)[1];				\
2187c478bdstevel@tonic-gate	((char *)a)[1] = ((char *)&_tmp)[0];				\
2197c478bdstevel@tonic-gate}
2207c478bdstevel@tonic-gate#define	P_16_COPY(a, b) {						\
2217c478bdstevel@tonic-gate	((char *)&(b))[0] = ((char *)&(a))[1];				\
2227c478bdstevel@tonic-gate	((char *)&(b))[1] = ((char *)&(a))[0];				\
2237c478bdstevel@tonic-gate}
2247c478bdstevel@tonic-gate
2257c478bdstevel@tonic-gate/* open functions for each database type, used in dbopen() */
2267c478bdstevel@tonic-gate
2277c478bdstevel@tonic-gate#define __bt_open	__kdb2_bt_open
2287c478bdstevel@tonic-gate#define __hash_open	__kdb2_hash_open
2297c478bdstevel@tonic-gate#define __rec_open	__kdb2_rec_open
2307c478bdstevel@tonic-gate#define __dbpanic	__kdb2_dbpanic
2317c478bdstevel@tonic-gate
2327c478bdstevel@tonic-gateDB	*__bt_open __P((const char *, int, int, const BTREEINFO *, int));
2337c478bdstevel@tonic-gateDB	*__hash_open __P((const char *, int, int, const HASHINFO *, int));
2347c478bdstevel@tonic-gateDB	*__rec_open __P((const char *, int, int, const RECNOINFO *, int));
2357c478bdstevel@tonic-gatevoid	 __dbpanic __P((DB *dbp));
2367c478bdstevel@tonic-gate
2377c478bdstevel@tonic-gate/*
2387c478bdstevel@tonic-gate * There is no portable way to figure out the maximum value of a file
2397c478bdstevel@tonic-gate * offset, so we put it here.
2407c478bdstevel@tonic-gate */
2417c478bdstevel@tonic-gate#ifdef	OFF_T_MAX
2427c478bdstevel@tonic-gate#define	DB_OFF_T_MAX	OFF_T_MAX
2437c478bdstevel@tonic-gate#else
2447c478bdstevel@tonic-gate#define	DB_OFF_T_MAX	LONG_MAX
2457c478bdstevel@tonic-gate#endif
2467c478bdstevel@tonic-gate
2477c478bdstevel@tonic-gate#ifndef O_ACCMODE			/* POSIX 1003.1 access mode mask. */
2487c478bdstevel@tonic-gate#define	O_ACCMODE	(O_RDONLY|O_WRONLY|O_RDWR)
2497c478bdstevel@tonic-gate#endif
2507c478bdstevel@tonic-gate
2517c478bdstevel@tonic-gate/*
2527c478bdstevel@tonic-gate * If you can't provide lock values in the open(2) call.  Note, this
2537c478bdstevel@tonic-gate * allows races to happen.
2547c478bdstevel@tonic-gate */
2557c478bdstevel@tonic-gate#ifndef O_EXLOCK			/* 4.4BSD extension. */
2567c478bdstevel@tonic-gate#define	O_EXLOCK	0
2577c478bdstevel@tonic-gate#endif
2587c478bdstevel@tonic-gate
2597c478bdstevel@tonic-gate#ifndef O_SHLOCK			/* 4.4BSD extension. */
2607c478bdstevel@tonic-gate#define	O_SHLOCK	0
2617c478bdstevel@tonic-gate#endif
2627c478bdstevel@tonic-gate
2637c478bdstevel@tonic-gate#ifndef EFTYPE
2647c478bdstevel@tonic-gate#define	EFTYPE		EINVAL		/* POSIX 1003.1 format errno. */
2657c478bdstevel@tonic-gate#endif
2667c478bdstevel@tonic-gate
2677c478bdstevel@tonic-gate#ifndef	STDERR_FILENO
2687c478bdstevel@tonic-gate#define	STDIN_FILENO	0		/* ANSI C #defines */
2697c478bdstevel@tonic-gate#define	STDOUT_FILENO	1
2707c478bdstevel@tonic-gate#define	STDERR_FILENO	2
2717c478bdstevel@tonic-gate#endif
2727c478bdstevel@tonic-gate
2737c478bdstevel@tonic-gate#ifndef SEEK_END
2747c478bdstevel@tonic-gate#define	SEEK_SET	0		/* POSIX 1003.1 seek values */
2757c478bdstevel@tonic-gate#define	SEEK_CUR	1
2767c478bdstevel@tonic-gate#define	SEEK_END	2
2777c478bdstevel@tonic-gate#endif
2787c478bdstevel@tonic-gate
2797c478bdstevel@tonic-gate#ifndef NULL				/* ANSI C #defines NULL everywhere. */
2807c478bdstevel@tonic-gate#define	NULL		0
2817c478bdstevel@tonic-gate#endif
2827c478bdstevel@tonic-gate
2837c478bdstevel@tonic-gate#ifndef	MAX				/* Usually found in <sys/param.h>. */
2847c478bdstevel@tonic-gate#define	MAX(_a,_b)	((_a)<(_b)?(_b):(_a))
2857c478bdstevel@tonic-gate#endif
2867c478bdstevel@tonic-gate#ifndef	MIN				/* Usually found in <sys/param.h>. */
2877c478bdstevel@tonic-gate#define	MIN(_a,_b)	((_a)<(_b)?(_a):(_b))
2887c478bdstevel@tonic-gate#endif
2897c478bdstevel@tonic-gate
2907c478bdstevel@tonic-gate#ifndef S_ISDIR				/* POSIX 1003.1 file type tests. */
2917c478bdstevel@tonic-gate#define	S_ISDIR(m)	((m & 0170000) == 0040000)	/* directory */
2927c478bdstevel@tonic-gate#define	S_ISCHR(m)	((m & 0170000) == 0020000)	/* char special */
2937c478bdstevel@tonic-gate#define	S_ISBLK(m)	((m & 0170000) == 0060000)	/* block special */
2947c478bdstevel@tonic-gate#define	S_ISREG(m)	((m & 0170000) == 0100000)	/* regular file */
2957c478bdstevel@tonic-gate#define	S_ISFIFO(m)	((m & 0170000) == 0010000)	/* fifo */
2967c478bdstevel@tonic-gate#endif
2977c478bdstevel@tonic-gate#ifndef S_ISLNK				/* BSD POSIX 1003.1 extensions */
2987c478bdstevel@tonic-gate#define	S_ISLNK(m)	((m & 0170000) == 0120000)	/* symbolic link */
2997c478bdstevel@tonic-gate#define	S_ISSOCK(m)	((m & 0170000) == 0140000)	/* socket */
3007c478bdstevel@tonic-gate#endif
3017c478bdstevel@tonic-gate
3027c478bdstevel@tonic-gate#ifndef O_BINARY
3037c478bdstevel@tonic-gate#define O_BINARY	0		/* Needed for Win32 compiles */
3047c478bdstevel@tonic-gate#endif
3057c478bdstevel@tonic-gate#endif /* _DB_INT_H_ */
3067c478bdstevel@tonic-gate
3077c478bdstevel@tonic-gate#ifdef	__cplusplus
3087c478bdstevel@tonic-gate}
3097c478bdstevel@tonic-gate#endif
3107c478bdstevel@tonic-gate
3117c478bdstevel@tonic-gate#endif	/* !_KRB5_DB2_MISC_H */
312