1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*-
3  * Copyright (c) 1990, 1993, 1994
4  *  The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. ***REMOVED*** - see
15  *    ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *  @(#)db.h    8.7 (Berkeley) 6/16/94
33  */
34 
35 #ifndef _DB_H_
36 #define _DB_H_
37 
38 #ifndef macintosh
39 #include <sys/types.h>
40 #endif
41 #include "prtypes.h"
42 
43 #if !defined(XP_BEOS) && !defined(XP_OS2) && !defined(XP_UNIX) || defined(NTO)
44 typedef PRUintn uint;
45 #endif
46 typedef PRUint8 uint8;
47 typedef PRUint16 uint16;
48 /* On AIX 5.2, sys/inttypes.h (which is included by sys/types.h)
49  * defines the types int8, int16, int32, and int64. */
50 #if !defined(AIX)
51 typedef PRInt32 int32;
52 #endif
53 typedef PRUint32 uint32;
54 
55 #include <limits.h>
56 
57 #ifdef __DBINTERFACE_PRIVATE
58 
59 #ifdef HAVE_SYS_BYTEORDER_H
60 #include <sys/byteorder.h>
61 #endif
62 
63 #if defined(__linux) || defined(__BEOS__)
64 #include <endian.h>
65 #ifndef BYTE_ORDER
66 #define BYTE_ORDER __BYTE_ORDER
67 #define BIG_ENDIAN __BIG_ENDIAN
68 #define LITTLE_ENDIAN __LITTLE_ENDIAN
69 #endif
70 #endif /* __linux */
71 
72 #ifdef __sgi
73 #define BYTE_ORDER BIG_ENDIAN
74 #define BIG_ENDIAN 4321
75 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
76 #endif
77 
78 #ifdef __sun
79 #define BIG_ENDIAN 4321
80 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
81 
82 #ifndef __SVR4
83 /* compat.h is only in 4.1.3 machines. - dp */
84 #include <compat.h>
85 #endif
86 
87 /* XXX - dp
88  * Need to find a general way of defining endian-ness in SunOS 5.3
89  * SunOS 5.4 defines _BIG_ENDIAN and _LITTLE_ENDIAN
90  * SunOS 5.3 does nothing like this.
91  */
92 
93 #ifndef BYTE_ORDER
94 
95 #if defined(_BIG_ENDIAN)
96 #define BYTE_ORDER BIG_ENDIAN
97 #elif defined(_LITTLE_ENDIAN)
98 #define BYTE_ORDER LITTLE_ENDIAN
99 #elif !defined(__SVR4)
100 /* 4.1.3 is always BIG_ENDIAN as it was released only on sparc platforms. */
101 #define BYTE_ORDER BIG_ENDIAN
102 #elif !defined(vax) && !defined(ntohl) && !defined(lint) && !defined(i386)
103 /* 5.3 big endian. Copied this above line from sys/byteorder.h */
104 /* Now we are in a 5.3 SunOS rather non 5.4 or above SunOS  */
105 #define BYTE_ORDER BIG_ENDIAN
106 #else
107 #define BYTE_ORDER LITTLE_ENDIAN
108 #endif
109 
110 #endif /* !BYTE_ORDER */
111 #endif /* __sun */
112 
113 #if defined(__hpux) || defined(__hppa)
114 #define BYTE_ORDER BIG_ENDIAN
115 #define BIG_ENDIAN 4321
116 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
117 #endif
118 
119 #if defined(AIXV3) || defined(AIX)
120 /* BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN are all defined here */
121 #include <sys/machine.h>
122 #endif
123 
124 /* Digital Unix */
125 #ifdef __osf__
126 #include <machine/endian.h>
127 #endif
128 
129 #ifdef __alpha
130 #ifndef WIN32
131 #else
132 /* Alpha NT */
133 #define BYTE_ORDER LITTLE_ENDIAN
134 #define BIG_ENDIAN 4321
135 #define LITTLE_ENDIAN 1234
136 #endif
137 #endif
138 
139 #ifdef NCR
140 #include <sys/endian.h>
141 #endif
142 
143 #ifdef __QNX__
144 #ifdef __QNXNTO__
145 #include <sys/param.h>
146 #else
147 #define LITTLE_ENDIAN 1234
148 #define BIG_ENDIAN 4321
149 #define BYTE_ORDER LITTLE_ENDIAN
150 #endif
151 #endif
152 
153 #ifdef SNI
154 /* #include <sys/hetero.h> */
155 #define BYTE_ORDER BIG_ENDIAN
156 #define BIG_ENDIAN 4321
157 #define LITTLE_ENDIAN 1234
158 #endif
159 
160 #ifdef _WINDOWS
161 #ifdef BYTE_ORDER
162 #undef BYTE_ORDER
163 #endif
164 
165 #define BYTE_ORDER LITTLE_ENDIAN
166 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
167 #define BIG_ENDIAN 4321
168 #endif
169 
170 #ifdef macintosh
171 #define BIG_ENDIAN 4321
172 #define LITTLE_ENDIAN 1234
173 #define BYTE_ORDER BIG_ENDIAN
174 #endif
175 
176 #endif /* __DBINTERFACE_PRIVATE */
177 
178 #ifdef SCO
179 #define MAXPATHLEN 1024
180 #endif
181 
182 #include <fcntl.h>
183 
184 #if defined(_WINDOWS) || defined(XP_OS2)
185 #include <stdio.h>
186 #include <io.h>
187 
188 #ifndef XP_OS2
189 #define MAXPATHLEN 1024
190 #endif
191 
192 #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */
193 
194 #ifndef STDERR_FILENO
195 #define STDIN_FILENO 0 /* ANSI C #defines */
196 #define STDOUT_FILENO 1
197 #define STDERR_FILENO 2
198 #endif
199 
200 #ifndef O_ACCMODE /* POSIX 1003.1 access mode mask. */
201 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
202 #endif
203 #endif
204 
205 #ifdef macintosh
206 #include <stdio.h>
207 #include "xp_mcom.h"
208 #define O_ACCMODE 3 /* Mask for file access modes */
209 #define EFTYPE 2000
210 PR_BEGIN_EXTERN_C
211 int mkstemp(const char *path);
212 PR_END_EXTERN_C
213 #endif /* MACINTOSH */
214 
215 #if !defined(_WINDOWS) && !defined(macintosh)
216 #include <sys/stat.h>
217 #include <errno.h>
218 #endif
219 
220 /* define EFTYPE since most don't */
221 #ifndef EFTYPE
222 #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */
223 #endif
224 
225 #define RET_ERROR -1 /* Return values. */
226 #define RET_SUCCESS 0
227 #define RET_SPECIAL 1
228 
229 #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */
230 
231 #ifndef __sgi
232 typedef uint32 pgno_t;
233 #endif
234 
235 #define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */
236 typedef uint16 indx_t;
237 #define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */
238 typedef uint32 recno_t;
239 
240 /* Key/data structure -- a Data-Base Thang. */
241 typedef struct {
242     void *data;  /* data */
243     size_t size; /* data length */
244 } DBT;
245 
246 /* Routine flags. */
247 #define R_CURSOR 1      /* del, put, seq */
248 #define __R_UNUSED 2    /* UNUSED */
249 #define R_FIRST 3       /* seq */
250 #define R_IAFTER 4      /* put (RECNO) */
251 #define R_IBEFORE 5     /* put (RECNO) */
252 #define R_LAST 6        /* seq (BTREE, RECNO) */
253 #define R_NEXT 7        /* seq */
254 #define R_NOOVERWRITE 8 /* put */
255 #define R_PREV 9        /* seq (BTREE, RECNO) */
256 #define R_SETCURSOR 10  /* put (RECNO) */
257 #define R_RECNOSYNC 11  /* sync (RECNO) */
258 
259 typedef enum { DB_BTREE,
260                DB_HASH,
261                DB_RECNO } DBTYPE;
262 
263 typedef enum { LockOutDatabase,
264                UnlockDatabase } DBLockFlagEnum;
265 
266 /*
267  * !!!
268  * The following flags are included in the dbopen(3) call as part of the
269  * open(2) flags.  In order to avoid conflicts with the open flags, start
270  * at the top of the 16 or 32-bit number space and work our way down.  If
271  * the open flags were significantly expanded in the future, it could be
272  * a problem.  Wish I'd left another flags word in the dbopen call.
273  *
274  * !!!
275  * None of this stuff is implemented yet.  The only reason that it's here
276  * is so that the access methods can skip copying the key/data pair when
277  * the DB_LOCK flag isn't set.
278  */
279 #if UINT_MAX > 65535
280 #define DB_LOCK 0x20000000  /* Do locking. */
281 #define DB_SHMEM 0x40000000 /* Use shared memory. */
282 #define DB_TXN 0x80000000   /* Do transactions. */
283 #else
284 #define DB_LOCK 0x2000  /* Do locking. */
285 #define DB_SHMEM 0x4000 /* Use shared memory. */
286 #define DB_TXN 0x8000   /* Do transactions. */
287 #endif
288 
289 /* Access method description structure. */
290 typedef struct dbm_db {
291     DBTYPE type; /* Underlying db type. */
292     int (*close)(struct dbm_db *);
293     int (*del)(const struct dbm_db *, const DBT *, uint);
294     int (*get)(const struct dbm_db *, const DBT *, DBT *, uint);
295     int (*put)(const struct dbm_db *, DBT *, const DBT *, uint);
296     int (*seq)(const struct dbm_db *, DBT *, DBT *, uint);
297     int (*sync)(const struct dbm_db *, uint);
298     void *internal; /* Access method private. */
299     int (*fd)(const struct dbm_db *);
300 } DB;
301 
302 #define BTREEMAGIC 0x053162
303 #define BTREEVERSION 3
304 
305 /* Structure used to pass parameters to the btree routines. */
306 typedef struct {
307 #define R_DUP 0x01 /* duplicate keys */
308     uint32 flags;
309     uint cachesize; /* bytes to cache */
310     int maxkeypage; /* maximum keys per page */
311     int minkeypage; /* minimum keys per page */
312     uint psize;     /* page size */
313     int(*compare)   /* comparison function */
314         (const DBT *, const DBT *);
315     size_t(*prefix) /* prefix function */
316         (const DBT *, const DBT *);
317     int lorder; /* byte order */
318 } BTREEINFO;
319 
320 #define HASHMAGIC 0x061561
321 #define HASHVERSION 2
322 
323 /* Structure used to pass parameters to the hashing routines. */
324 typedef struct {
325     uint bsize;     /* bucket size */
326     uint ffactor;   /* fill factor */
327     uint nelem;     /* number of elements */
328     uint cachesize; /* bytes to cache */
329     uint32          /* hash function */
330         (*hash)(const void *, size_t);
331     int lorder; /* byte order */
332 } HASHINFO;
333 
334 /* Structure used to pass parameters to the record routines. */
335 typedef struct {
336 #define R_FIXEDLEN 0x01 /* fixed-length records */
337 #define R_NOKEY 0x02    /* key not required */
338 #define R_SNAPSHOT 0x04 /* snapshot the input */
339     uint32 flags;
340     uint cachesize; /* bytes to cache */
341     uint psize;     /* page size */
342     int lorder;     /* byte order */
343     size_t reclen;  /* record length (fixed-length records) */
344     uint8 bval;     /* delimiting byte (variable-length records */
345     char *bfname;   /* btree file name */
346 } RECNOINFO;
347 
348 #ifdef __DBINTERFACE_PRIVATE
349 /*
350  * Little endian <==> big endian 32-bit swap macros.
351  *  M_32_SWAP   swap a memory location
352  *  P_32_SWAP   swap a referenced memory location
353  *  P_32_COPY   swap from one location to another
354  */
355 #define M_32_SWAP(a)                          \
356     {                                         \
357         uint32 _tmp = a;                      \
358         ((char *)&a)[0] = ((char *)&_tmp)[3]; \
359         ((char *)&a)[1] = ((char *)&_tmp)[2]; \
360         ((char *)&a)[2] = ((char *)&_tmp)[1]; \
361         ((char *)&a)[3] = ((char *)&_tmp)[0]; \
362     }
363 #define P_32_SWAP(a)                         \
364     {                                        \
365         uint32 _tmp = *(uint32 *)a;          \
366         ((char *)a)[0] = ((char *)&_tmp)[3]; \
367         ((char *)a)[1] = ((char *)&_tmp)[2]; \
368         ((char *)a)[2] = ((char *)&_tmp)[1]; \
369         ((char *)a)[3] = ((char *)&_tmp)[0]; \
370     }
371 #define P_32_COPY(a, b)                        \
372     {                                          \
373         ((char *)&(b))[0] = ((char *)&(a))[3]; \
374         ((char *)&(b))[1] = ((char *)&(a))[2]; \
375         ((char *)&(b))[2] = ((char *)&(a))[1]; \
376         ((char *)&(b))[3] = ((char *)&(a))[0]; \
377     }
378 
379 /*
380  * Little endian <==> big endian 16-bit swap macros.
381  *  M_16_SWAP   swap a memory location
382  *  P_16_SWAP   swap a referenced memory location
383  *  P_16_COPY   swap from one location to another
384  */
385 #define M_16_SWAP(a)                          \
386     {                                         \
387         uint16 _tmp = a;                      \
388         ((char *)&a)[0] = ((char *)&_tmp)[1]; \
389         ((char *)&a)[1] = ((char *)&_tmp)[0]; \
390     }
391 #define P_16_SWAP(a)                         \
392     {                                        \
393         uint16 _tmp = *(uint16 *)a;          \
394         ((char *)a)[0] = ((char *)&_tmp)[1]; \
395         ((char *)a)[1] = ((char *)&_tmp)[0]; \
396     }
397 #define P_16_COPY(a, b)                        \
398     {                                          \
399         ((char *)&(b))[0] = ((char *)&(a))[1]; \
400         ((char *)&(b))[1] = ((char *)&(a))[0]; \
401     }
402 #endif
403 
404 PR_BEGIN_EXTERN_C
405 
406 extern DB *
407 dbopen(const char *, int, int, DBTYPE, const void *);
408 
409 /* set or unset a global lock flag to disable the
410  * opening of any DBM file
411  */
412 void dbSetOrClearDBLock(DBLockFlagEnum type);
413 
414 #ifdef __DBINTERFACE_PRIVATE
415 DB *dbm_bt_open(const char *, int, int, const BTREEINFO *, int);
416 DB *dbm_hash_open(const char *, int, int, const HASHINFO *, int);
417 DB *dbm_rec_open(const char *, int, int, const RECNOINFO *, int);
418 void dbm_dbpanic(DB *dbp);
419 #endif
420 
421 PR_END_EXTERN_C
422 
423 #endif /* !_DB_H_ */
424