1 /* $Id: cdb.h 106 2003-12-12 17:36:49Z werner $
2  * public cdb include file
3  *
4  * This file is a part of tinycdb package by Michael Tokarev, mjt@corpit.ru.
5  * Public domain.
6  *
7  * Taken from tinycdb-0.73. By Werner Koch <wk@gnupg.org> 2003-12-12.
8  */
9 
10 #ifndef TINYCDB_VERSION
11 #define TINYCDB_VERSION 0.73
12 
13 typedef unsigned int cdbi_t; /*XXX should be at least 32 bits long */
14 
15 /* common routines */
16 cdbi_t cdb_hash(const void *buf, cdbi_t len);
17 cdbi_t cdb_unpack(const unsigned char buf[4]);
18 void cdb_pack(cdbi_t num, unsigned char buf[4]);
19 
20 struct cdb {
21   int cdb_fd;			/* file descriptor */
22   /* private members */
23 #ifdef HAVE_W32_SYSTEM
24   void *cdb_mapping;            /* Mapping handle.  */
25 #endif
26   cdbi_t cdb_fsize;		/* datafile size */
27   const unsigned char *cdb_mem; /* mmap'ed file memory */
28   cdbi_t cdb_vpos, cdb_vlen;	/* found data */
29   cdbi_t cdb_kpos, cdb_klen;    /* found key (only set if cdb_findinit
30                                    was called with KEY set to NULL). */
31 };
32 
33 #define cdb_datapos(c) ((c)->cdb_vpos)
34 #define cdb_datalen(c) ((c)->cdb_vlen)
35 #define cdb_keypos(c) ((c)->cdb_kpos)
36 #define cdb_keylen(c) ((c)->cdb_klen)
37 #define cdb_fileno(c) ((c)->cdb_fd)
38 
39 int cdb_init(struct cdb *cdbp, int fd);
40 void cdb_free(struct cdb *cdbp);
41 
42 int cdb_read(const struct cdb *cdbp,
43 	     void *buf, unsigned len, cdbi_t pos);
44 int cdb_find(struct cdb *cdbp, const void *key, unsigned klen);
45 
46 struct cdb_find {
47   struct cdb *cdb_cdbp;
48   cdbi_t cdb_hval;
49   const unsigned char *cdb_htp, *cdb_htab, *cdb_htend;
50   cdbi_t cdb_httodo;
51   const void *cdb_key;
52   cdbi_t cdb_klen;
53 };
54 
55 int cdb_findinit(struct cdb_find *cdbfp, struct cdb *cdbp,
56 		 const void *key, cdbi_t klen);
57 int cdb_findnext(struct cdb_find *cdbfp);
58 
59 /* old simple interface */
60 /* open file using standard routine, then: */
61 int cdb_seek(int fd, const void *key, unsigned klen, cdbi_t *dlenp);
62 int cdb_bread(int fd, void *buf, int len);
63 
64 /* cdb_make */
65 
66 struct cdb_make {
67   int cdb_fd;			/* file descriptor */
68   /* private */
69   cdbi_t cdb_dpos;		/* data position so far */
70   cdbi_t cdb_rcnt;		/* record count so far */
71   char cdb_buf[4096];		/* write buffer */
72   char *cdb_bpos;		/* current buf position */
73   struct cdb_rl *cdb_rec[256];	/* list of arrays of record infos */
74 };
75 
76 
77 
78 int cdb_make_start(struct cdb_make *cdbmp, int fd);
79 int cdb_make_add(struct cdb_make *cdbmp,
80 		 const void *key, cdbi_t klen,
81 		 const void *val, cdbi_t vlen);
82 int cdb_make_exists(struct cdb_make *cdbmp,
83 		    const void *key, cdbi_t klen);
84 int cdb_make_put(struct cdb_make *cdbmp,
85 		 const void *key, cdbi_t klen,
86 		 const void *val, cdbi_t vlen,
87 		 int flag);
88 #define CDB_PUT_ADD	0	/* add unconditionally, like cdb_make_add() */
89 #define CDB_PUT_REPLACE	1	/* replace: do not place to index OLD record */
90 #define CDB_PUT_INSERT	2	/* add only if not already exists */
91 #define CDB_PUT_WARN	3	/* add unconditionally but ret. 1 if exists */
92 int cdb_make_finish(struct cdb_make *cdbmp);
93 
94 #endif /* include guard */
95