Imported Upstream version 3.13.6
[platform/upstream/nss.git] / mozilla / dbm / include / mcom_db.h
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
39 #ifdef WINCE
40 #define off_t long
41 #endif
42
43 #ifndef macintosh
44 #include <sys/types.h>
45 #endif
46 #include "prtypes.h"
47
48 #include <limits.h>
49
50 #ifdef __DBINTERFACE_PRIVATE
51
52 #ifdef HAVE_SYS_CDEFS_H
53 #include <sys/cdefs.h>
54 #else
55 #include "cdefs.h"
56 #endif
57
58 #ifdef HAVE_SYS_BYTEORDER_H
59 #include <sys/byteorder.h>
60 #endif
61
62 #if defined(__linux) || defined(__BEOS__)
63 #include <endian.h>
64 #ifndef BYTE_ORDER
65 #define BYTE_ORDER    __BYTE_ORDER
66 #define BIG_ENDIAN    __BIG_ENDIAN
67 #define LITTLE_ENDIAN __LITTLE_ENDIAN
68 #endif
69 #endif /* __linux */
70
71 #ifdef __sgi
72 #define BYTE_ORDER BIG_ENDIAN
73 #define BIG_ENDIAN      4321
74 #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
75 #endif
76
77 #ifdef __sun
78 #define BIG_ENDIAN      4321
79 #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
80
81 #ifndef __SVR4
82 /* compat.h is only in 4.1.3 machines. - dp */
83 #include <compat.h>
84 #endif
85
86 /* XXX - dp
87  * Need to find a general way of defining endian-ness in SunOS 5.3
88  * SunOS 5.4 defines _BIG_ENDIAN and _LITTLE_ENDIAN
89  * SunOS 5.3 does nothing like this.
90  */
91
92 #ifndef BYTE_ORDER
93
94 #if defined(_BIG_ENDIAN)
95 #define BYTE_ORDER BIG_ENDIAN
96 #elif defined(_LITTLE_ENDIAN)
97 #define BYTE_ORDER LITTLE_ENDIAN
98 #elif !defined(__SVR4)
99 /* 4.1.3 is always BIG_ENDIAN as it was released only on sparc platforms. */
100 #define BYTE_ORDER BIG_ENDIAN
101 #elif !defined(vax) && !defined(ntohl) && !defined(lint) && !defined(i386)
102 /* 5.3 big endian. Copied this above line from sys/byteorder.h */
103 /* Now we are in a 5.3 SunOS rather non 5.4 or above SunOS  */
104 #define BYTE_ORDER BIG_ENDIAN
105 #else
106 #define BYTE_ORDER LITTLE_ENDIAN
107 #endif
108
109 #endif /* !BYTE_ORDER */
110 #endif /* __sun */
111
112 #if defined(__hpux) || defined(__hppa)
113 #define BYTE_ORDER BIG_ENDIAN
114 #define BIG_ENDIAN      4321
115 #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
116 #endif
117
118 #if defined(AIXV3) || defined(AIX)
119 /* BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN are all defined here */
120 #include <sys/machine.h>
121 #endif
122
123 /* Digital Unix */
124 #ifdef __osf__
125 #include <machine/endian.h>
126 #endif
127
128 #ifdef __alpha
129 #ifndef WIN32
130 #else
131 /* Alpha NT */
132 #define BYTE_ORDER LITTLE_ENDIAN
133 #define BIG_ENDIAN      4321
134 #define LITTLE_ENDIAN   1234 
135 #endif
136 #endif
137
138 #ifdef NCR
139 #include <sys/endian.h>
140 #endif
141
142 #ifdef __QNX__
143 #ifdef __QNXNTO__
144 #include <sys/param.h>
145 #else
146 #define LITTLE_ENDIAN   1234
147 #define BIG_ENDIAN      4321
148 #define BYTE_ORDER      LITTLE_ENDIAN
149 #endif
150 #endif
151
152 #ifdef SNI
153 /* #include <sys/hetero.h> */
154 #define BYTE_ORDER BIG_ENDIAN
155 #define BIG_ENDIAN      4321
156 #define LITTLE_ENDIAN   1234
157 #endif
158
159 #ifdef _WINDOWS
160 #ifdef BYTE_ORDER
161 #undef BYTE_ORDER
162 #endif
163
164 #define BYTE_ORDER LITTLE_ENDIAN
165 #define LITTLE_ENDIAN   1234            /* LSB first: i386, vax, all NT risc */
166 #define BIG_ENDIAN      4321
167 #endif
168
169 #ifdef macintosh
170 #define BIG_ENDIAN 4321
171 #define LITTLE_ENDIAN 1234
172 #define BYTE_ORDER BIG_ENDIAN
173 #endif
174
175 #endif  /* __DBINTERFACE_PRIVATE */
176
177 #ifdef SCO
178 #define MAXPATHLEN      1024              
179 #endif
180
181 #include <fcntl.h>
182
183 #if defined(_WINDOWS) || defined(XP_OS2)
184 #include <stdio.h>
185 #include <io.h>
186
187 #ifndef XP_OS2 
188 #define MAXPATHLEN      1024               
189 #endif
190
191 #define EFTYPE          EINVAL          /* POSIX 1003.1 format errno. */
192
193 #ifndef STDERR_FILENO
194 #define STDIN_FILENO    0               /* ANSI C #defines */
195 #define STDOUT_FILENO   1
196 #define STDERR_FILENO   2
197 #endif
198
199 #ifndef O_ACCMODE                       /* POSIX 1003.1 access mode mask. */
200 #define O_ACCMODE       (O_RDONLY|O_WRONLY|O_RDWR)
201 #endif
202 #endif
203
204 #ifdef macintosh
205 #include <stdio.h>
206 #include "xp_mcom.h"
207 #define O_ACCMODE       3       /* Mask for file access modes */
208 #define EFTYPE 2000
209 PR_BEGIN_EXTERN_C
210 int mkstemp(const char *path);
211 PR_END_EXTERN_C
212 #endif  /* MACINTOSH */
213
214 #if !defined(_WINDOWS) && !defined(macintosh)
215 #include <sys/stat.h>
216 #include <errno.h>
217 #endif
218
219 /* define EFTYPE since most don't */
220 #ifndef EFTYPE
221 #define EFTYPE      EINVAL      /* POSIX 1003.1 format errno. */
222 #endif
223
224 #define RET_ERROR       -1              /* Return values. */
225 #define RET_SUCCESS      0
226 #define RET_SPECIAL      1
227
228 #define MAX_PAGE_NUMBER 0xffffffff      /* >= # of pages in a file */
229
230 #ifndef __sgi
231 typedef uint32  pgno_t;
232 #endif
233
234 #define MAX_PAGE_OFFSET 65535           /* >= # of bytes in a page */
235 typedef uint16  indx_t;
236 #define MAX_REC_NUMBER  0xffffffff      /* >= # of records in a tree */
237 typedef uint32  recno_t;
238
239 /* Key/data structure -- a Data-Base Thang. */
240 typedef struct {
241         void    *data;                  /* data */
242         size_t   size;                  /* data length */
243 } DBT;
244
245 /* Routine flags. */
246 #define R_CURSOR        1               /* del, put, seq */
247 #define __R_UNUSED      2               /* UNUSED */
248 #define R_FIRST         3               /* seq */
249 #define R_IAFTER        4               /* put (RECNO) */
250 #define R_IBEFORE       5               /* put (RECNO) */
251 #define R_LAST          6               /* seq (BTREE, RECNO) */
252 #define R_NEXT          7               /* seq */
253 #define R_NOOVERWRITE   8               /* put */
254 #define R_PREV          9               /* seq (BTREE, RECNO) */
255 #define R_SETCURSOR     10              /* put (RECNO) */
256 #define R_RECNOSYNC     11              /* sync (RECNO) */
257
258 typedef enum { DB_BTREE, DB_HASH, DB_RECNO } DBTYPE;
259
260 typedef enum { LockOutDatabase, UnlockDatabase } DBLockFlagEnum;
261
262 /*
263  * !!!
264  * The following flags are included in the dbopen(3) call as part of the
265  * open(2) flags.  In order to avoid conflicts with the open flags, start
266  * at the top of the 16 or 32-bit number space and work our way down.  If
267  * the open flags were significantly expanded in the future, it could be
268  * a problem.  Wish I'd left another flags word in the dbopen call.
269  *
270  * !!!
271  * None of this stuff is implemented yet.  The only reason that it's here
272  * is so that the access methods can skip copying the key/data pair when
273  * the DB_LOCK flag isn't set.
274  */
275 #if UINT_MAX > 65535
276 #define DB_LOCK         0x20000000      /* Do locking. */
277 #define DB_SHMEM        0x40000000      /* Use shared memory. */
278 #define DB_TXN          0x80000000      /* Do transactions. */
279 #else
280 #define DB_LOCK             0x2000      /* Do locking. */
281 #define DB_SHMEM            0x4000      /* Use shared memory. */
282 #define DB_TXN              0x8000      /* Do transactions. */
283 #endif
284
285 /* Access method description structure. */
286 typedef struct __db {
287         DBTYPE type;                    /* Underlying db type. */
288         int (*close)    (struct __db *);
289         int (*del)      (const struct __db *, const DBT *, uint);
290         int (*get)      (const struct __db *, const DBT *, DBT *, uint);
291         int (*put)      (const struct __db *, DBT *, const DBT *, uint);
292         int (*seq)      (const struct __db *, DBT *, DBT *, uint);
293         int (*sync)     (const struct __db *, uint);
294         void *internal;                 /* Access method private. */
295         int (*fd)       (const struct __db *);
296 } DB;
297
298 #define BTREEMAGIC      0x053162
299 #define BTREEVERSION    3
300
301 /* Structure used to pass parameters to the btree routines. */
302 typedef struct {
303 #define R_DUP           0x01    /* duplicate keys */
304         uint32  flags;
305         uint    cachesize;      /* bytes to cache */
306         int     maxkeypage;     /* maximum keys per page */
307         int     minkeypage;     /* minimum keys per page */
308         uint    psize;          /* page size */
309         int     (*compare)      /* comparison function */
310             (const DBT *, const DBT *);
311         size_t  (*prefix)       /* prefix function */
312             (const DBT *, const DBT *);
313         int     lorder;         /* byte order */
314 } BTREEINFO;
315
316 #define HASHMAGIC       0x061561
317 #define HASHVERSION     2
318
319 /* Structure used to pass parameters to the hashing routines. */
320 typedef struct {
321         uint    bsize;          /* bucket size */
322         uint    ffactor;        /* fill factor */
323         uint    nelem;          /* number of elements */
324         uint    cachesize;      /* bytes to cache */
325         uint32          /* hash function */
326                 (*hash) (const void *, size_t);
327         int     lorder;         /* byte order */
328 } HASHINFO;
329
330 /* Structure used to pass parameters to the record routines. */
331 typedef struct {
332 #define R_FIXEDLEN      0x01    /* fixed-length records */
333 #define R_NOKEY         0x02    /* key not required */
334 #define R_SNAPSHOT      0x04    /* snapshot the input */
335         uint32  flags;
336         uint    cachesize;      /* bytes to cache */
337         uint    psize;          /* page size */
338         int     lorder;         /* byte order */
339         size_t  reclen;         /* record length (fixed-length records) */
340         uint8   bval;           /* delimiting byte (variable-length records */
341         char    *bfname;        /* btree file name */ 
342 } RECNOINFO;
343
344 #ifdef __DBINTERFACE_PRIVATE
345 /*
346  * Little endian <==> big endian 32-bit swap macros.
347  *      M_32_SWAP       swap a memory location
348  *      P_32_SWAP       swap a referenced memory location
349  *      P_32_COPY       swap from one location to another
350  */
351 #define M_32_SWAP(a) {                                                  \
352         uint32 _tmp = a;                                                \
353         ((char *)&a)[0] = ((char *)&_tmp)[3];                           \
354         ((char *)&a)[1] = ((char *)&_tmp)[2];                           \
355         ((char *)&a)[2] = ((char *)&_tmp)[1];                           \
356         ((char *)&a)[3] = ((char *)&_tmp)[0];                           \
357 }
358 #define P_32_SWAP(a) {                                                  \
359         uint32 _tmp = *(uint32 *)a;                             \
360         ((char *)a)[0] = ((char *)&_tmp)[3];                            \
361         ((char *)a)[1] = ((char *)&_tmp)[2];                            \
362         ((char *)a)[2] = ((char *)&_tmp)[1];                            \
363         ((char *)a)[3] = ((char *)&_tmp)[0];                            \
364 }
365 #define P_32_COPY(a, b) {                                               \
366         ((char *)&(b))[0] = ((char *)&(a))[3];                          \
367         ((char *)&(b))[1] = ((char *)&(a))[2];                          \
368         ((char *)&(b))[2] = ((char *)&(a))[1];                          \
369         ((char *)&(b))[3] = ((char *)&(a))[0];                          \
370 }
371
372 /*
373  * Little endian <==> big endian 16-bit swap macros.
374  *      M_16_SWAP       swap a memory location
375  *      P_16_SWAP       swap a referenced memory location
376  *      P_16_COPY       swap from one location to another
377  */
378 #define M_16_SWAP(a) {                                                  \
379         uint16 _tmp = a;                                                \
380         ((char *)&a)[0] = ((char *)&_tmp)[1];                           \
381         ((char *)&a)[1] = ((char *)&_tmp)[0];                           \
382 }
383 #define P_16_SWAP(a) {                                                  \
384         uint16 _tmp = *(uint16 *)a;                             \
385         ((char *)a)[0] = ((char *)&_tmp)[1];                            \
386         ((char *)a)[1] = ((char *)&_tmp)[0];                            \
387 }
388 #define P_16_COPY(a, b) {                                               \
389         ((char *)&(b))[0] = ((char *)&(a))[1];                          \
390         ((char *)&(b))[1] = ((char *)&(a))[0];                          \
391 }
392 #endif
393
394 PR_BEGIN_EXTERN_C
395
396 extern DB *
397 dbopen (const char *, int, int, DBTYPE, const void *);
398
399 /* set or unset a global lock flag to disable the
400  * opening of any DBM file
401  */
402 void dbSetOrClearDBLock(DBLockFlagEnum type);
403
404 #ifdef __DBINTERFACE_PRIVATE
405 DB      *__bt_open (const char *, int, int, const BTREEINFO *, int);
406 DB      *__hash_open (const char *, int, int, const HASHINFO *, int);
407 DB      *__rec_open (const char *, int, int, const RECNOINFO *, int);
408 void     __dbpanic (DB *dbp);
409 #endif
410
411 PR_END_EXTERN_C
412
413 #endif /* !_DB_H_ */