tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

mcom_db.h (12392B)


      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_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)
     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 #ifndef BYTE_ORDER
    115 #define BYTE_ORDER BIG_ENDIAN
    116 #define BIG_ENDIAN 4321
    117 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
    118 #endif
    119 #endif
    120 
    121 #if defined(AIXV3) || defined(AIX)
    122 /* BYTE_ORDER, LITTLE_ENDIAN, BIG_ENDIAN are all defined here */
    123 #include <sys/machine.h>
    124 #endif
    125 
    126 /* Digital Unix */
    127 #ifdef __osf__
    128 #include <machine/endian.h>
    129 #endif
    130 
    131 #ifdef __alpha
    132 #ifndef WIN32
    133 #else
    134 /* Alpha NT */
    135 #define BYTE_ORDER LITTLE_ENDIAN
    136 #define BIG_ENDIAN 4321
    137 #define LITTLE_ENDIAN 1234
    138 #endif
    139 #endif
    140 
    141 #ifdef NCR
    142 #include <sys/endian.h>
    143 #endif
    144 
    145 #ifdef __QNX__
    146 #ifdef __QNXNTO__
    147 #include <sys/param.h>
    148 #else
    149 #define LITTLE_ENDIAN 1234
    150 #define BIG_ENDIAN 4321
    151 #define BYTE_ORDER LITTLE_ENDIAN
    152 #endif
    153 #endif
    154 
    155 #ifdef SNI
    156 /* #include <sys/hetero.h> */
    157 #define BYTE_ORDER BIG_ENDIAN
    158 #define BIG_ENDIAN 4321
    159 #define LITTLE_ENDIAN 1234
    160 #endif
    161 
    162 #ifdef _WINDOWS
    163 #ifdef BYTE_ORDER
    164 #undef BYTE_ORDER
    165 #endif
    166 
    167 #define BYTE_ORDER LITTLE_ENDIAN
    168 #define LITTLE_ENDIAN 1234 /* LSB first: i386, vax, all NT risc */
    169 #define BIG_ENDIAN 4321
    170 #endif
    171 
    172 #ifdef macintosh
    173 #define BIG_ENDIAN 4321
    174 #define LITTLE_ENDIAN 1234
    175 #define BYTE_ORDER BIG_ENDIAN
    176 #endif
    177 
    178 #endif /* __DBINTERFACE_PRIVATE */
    179 
    180 #include <fcntl.h>
    181 
    182 #if defined(_WINDOWS)
    183 #include <stdio.h>
    184 #include <io.h>
    185 
    186 #define MAXPATHLEN 1024
    187 #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */
    188 
    189 #ifndef STDERR_FILENO
    190 #define STDIN_FILENO 0 /* ANSI C #defines */
    191 #define STDOUT_FILENO 1
    192 #define STDERR_FILENO 2
    193 #endif
    194 
    195 #ifndef O_ACCMODE /* POSIX 1003.1 access mode mask. */
    196 #define O_ACCMODE (O_RDONLY | O_WRONLY | O_RDWR)
    197 #endif
    198 #endif
    199 
    200 #ifdef macintosh
    201 #include <stdio.h>
    202 #include "xp_mcom.h"
    203 #define O_ACCMODE 3 /* Mask for file access modes */
    204 #define EFTYPE 2000
    205 PR_BEGIN_EXTERN_C
    206 int mkstemp(const char *path);
    207 PR_END_EXTERN_C
    208 #endif /* MACINTOSH */
    209 
    210 #if !defined(_WINDOWS) && !defined(macintosh)
    211 #include <sys/stat.h>
    212 #include <errno.h>
    213 #endif
    214 
    215 /* define EFTYPE since most don't */
    216 #ifndef EFTYPE
    217 #define EFTYPE EINVAL /* POSIX 1003.1 format errno. */
    218 #endif
    219 
    220 #define RET_ERROR -1 /* Return values. */
    221 #define RET_SUCCESS 0
    222 #define RET_SPECIAL 1
    223 
    224 #define MAX_PAGE_NUMBER 0xffffffff /* >= # of pages in a file */
    225 
    226 #ifndef __sgi
    227 typedef uint32 pgno_t;
    228 #endif
    229 
    230 #define MAX_PAGE_OFFSET 65535 /* >= # of bytes in a page */
    231 typedef uint16 indx_t;
    232 #define MAX_REC_NUMBER 0xffffffff /* >= # of records in a tree */
    233 typedef uint32 recno_t;
    234 
    235 /* Key/data structure -- a Data-Base Thang. */
    236 typedef struct {
    237    void *data;  /* data */
    238    size_t size; /* data length */
    239 } DBT;
    240 
    241 /* Routine flags. */
    242 #define R_CURSOR 1      /* del, put, seq */
    243 #define __R_UNUSED 2    /* UNUSED */
    244 #define R_FIRST 3       /* seq */
    245 #define R_IAFTER 4      /* put (RECNO) */
    246 #define R_IBEFORE 5     /* put (RECNO) */
    247 #define R_LAST 6        /* seq (BTREE, RECNO) */
    248 #define R_NEXT 7        /* seq */
    249 #define R_NOOVERWRITE 8 /* put */
    250 #define R_PREV 9        /* seq (BTREE, RECNO) */
    251 #define R_SETCURSOR 10  /* put (RECNO) */
    252 #define R_RECNOSYNC 11  /* sync (RECNO) */
    253 
    254 typedef enum { DB_BTREE,
    255               DB_HASH,
    256               DB_RECNO } DBTYPE;
    257 
    258 typedef enum { LockOutDatabase,
    259               UnlockDatabase } DBLockFlagEnum;
    260 
    261 /*
    262 * !!!
    263 * The following flags are included in the dbopen(3) call as part of the
    264 * open(2) flags.  In order to avoid conflicts with the open flags, start
    265 * at the top of the 16 or 32-bit number space and work our way down.  If
    266 * the open flags were significantly expanded in the future, it could be
    267 * a problem.  Wish I'd left another flags word in the dbopen call.
    268 *
    269 * !!!
    270 * None of this stuff is implemented yet.  The only reason that it's here
    271 * is so that the access methods can skip copying the key/data pair when
    272 * the DB_LOCK flag isn't set.
    273 */
    274 #if UINT_MAX > 65535
    275 #define DB_LOCK 0x20000000  /* Do locking. */
    276 #define DB_SHMEM 0x40000000 /* Use shared memory. */
    277 #define DB_TXN 0x80000000   /* Do transactions. */
    278 #else
    279 #define DB_LOCK 0x2000  /* Do locking. */
    280 #define DB_SHMEM 0x4000 /* Use shared memory. */
    281 #define DB_TXN 0x8000   /* Do transactions. */
    282 #endif
    283 
    284 /* Access method description structure. */
    285 typedef struct dbm_db {
    286    DBTYPE type; /* Underlying db type. */
    287    int (*close)(struct dbm_db *);
    288    int (*del)(const struct dbm_db *, const DBT *, uint);
    289    int (*get)(const struct dbm_db *, const DBT *, DBT *, uint);
    290    int (*put)(const struct dbm_db *, DBT *, const DBT *, uint);
    291    int (*seq)(const struct dbm_db *, DBT *, DBT *, uint);
    292    int (*sync)(const struct dbm_db *, uint);
    293    void *internal; /* Access method private. */
    294    int (*fd)(const struct dbm_db *);
    295 } DB;
    296 
    297 #define BTREEMAGIC 0x053162
    298 #define BTREEVERSION 3
    299 
    300 /* Structure used to pass parameters to the btree routines. */
    301 typedef struct {
    302 #define R_DUP 0x01 /* duplicate keys */
    303    uint32 flags;
    304    uint cachesize; /* bytes to cache */
    305    int maxkeypage; /* maximum keys per page */
    306    int minkeypage; /* minimum keys per page */
    307    uint psize;     /* page size */
    308    int(*compare)   /* comparison function */
    309        (const DBT *, const DBT *);
    310    size_t(*prefix) /* prefix function */
    311        (const DBT *, const DBT *);
    312    int lorder; /* byte order */
    313 } BTREEINFO;
    314 
    315 #define HASHMAGIC 0x061561
    316 #define HASHVERSION 2
    317 
    318 /* Structure used to pass parameters to the hashing routines. */
    319 typedef struct {
    320    uint bsize;     /* bucket size */
    321    uint ffactor;   /* fill factor */
    322    uint nelem;     /* number of elements */
    323    uint cachesize; /* bytes to cache */
    324    uint32          /* hash function */
    325        (*hash)(const void *, size_t);
    326    int lorder; /* byte order */
    327 } HASHINFO;
    328 
    329 /* Structure used to pass parameters to the record routines. */
    330 typedef struct {
    331 #define R_FIXEDLEN 0x01 /* fixed-length records */
    332 #define R_NOKEY 0x02    /* key not required */
    333 #define R_SNAPSHOT 0x04 /* snapshot the input */
    334    uint32 flags;
    335    uint cachesize; /* bytes to cache */
    336    uint psize;     /* page size */
    337    int lorder;     /* byte order */
    338    size_t reclen;  /* record length (fixed-length records) */
    339    uint8 bval;     /* delimiting byte (variable-length records */
    340    char *bfname;   /* btree file name */
    341 } RECNOINFO;
    342 
    343 #ifdef __DBINTERFACE_PRIVATE
    344 /*
    345 * Little endian <==> big endian 32-bit swap macros.
    346 *  M_32_SWAP   swap a memory location
    347 *  P_32_SWAP   swap a referenced memory location
    348 *  P_32_COPY   swap from one location to another
    349 */
    350 #define M_32_SWAP(a)                          \
    351    {                                         \
    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    {                                        \
    360        uint32 _tmp = *(uint32 *)a;          \
    361        ((char *)a)[0] = ((char *)&_tmp)[3]; \
    362        ((char *)a)[1] = ((char *)&_tmp)[2]; \
    363        ((char *)a)[2] = ((char *)&_tmp)[1]; \
    364        ((char *)a)[3] = ((char *)&_tmp)[0]; \
    365    }
    366 #define P_32_COPY(a, b)                        \
    367    {                                          \
    368        ((char *)&(b))[0] = ((char *)&(a))[3]; \
    369        ((char *)&(b))[1] = ((char *)&(a))[2]; \
    370        ((char *)&(b))[2] = ((char *)&(a))[1]; \
    371        ((char *)&(b))[3] = ((char *)&(a))[0]; \
    372    }
    373 
    374 /*
    375 * Little endian <==> big endian 16-bit swap macros.
    376 *  M_16_SWAP   swap a memory location
    377 *  P_16_SWAP   swap a referenced memory location
    378 *  P_16_COPY   swap from one location to another
    379 */
    380 #define M_16_SWAP(a)                          \
    381    {                                         \
    382        uint16 _tmp = a;                      \
    383        ((char *)&a)[0] = ((char *)&_tmp)[1]; \
    384        ((char *)&a)[1] = ((char *)&_tmp)[0]; \
    385    }
    386 #define P_16_SWAP(a)                         \
    387    {                                        \
    388        uint16 _tmp = *(uint16 *)a;          \
    389        ((char *)a)[0] = ((char *)&_tmp)[1]; \
    390        ((char *)a)[1] = ((char *)&_tmp)[0]; \
    391    }
    392 #define P_16_COPY(a, b)                        \
    393    {                                          \
    394        ((char *)&(b))[0] = ((char *)&(a))[1]; \
    395        ((char *)&(b))[1] = ((char *)&(a))[0]; \
    396    }
    397 #endif
    398 
    399 PR_BEGIN_EXTERN_C
    400 
    401 extern DB *
    402 dbopen(const char *, int, int, DBTYPE, const void *);
    403 
    404 /* set or unset a global lock flag to disable the
    405 * opening of any DBM file
    406 */
    407 void dbSetOrClearDBLock(DBLockFlagEnum type);
    408 
    409 #ifdef __DBINTERFACE_PRIVATE
    410 DB *dbm_bt_open(const char *, int, int, const BTREEINFO *, int);
    411 DB *dbm_hash_open(const char *, int, int, const HASHINFO *, int);
    412 DB *dbm_rec_open(const char *, int, int, const RECNOINFO *, int);
    413 void dbm_dbpanic(DB *dbp);
    414 #endif
    415 
    416 PR_END_EXTERN_C
    417 
    418 #endif /* !_DB_H_ */