summaryrefslogtreecommitdiffstats
path: root/dirmngr/cdblib.c
blob: c4012639693bdeeb1a8d96165b5f1b33309725da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
/* cdblib.c - all CDB library functions.
 *
 * This file is a part of tinycdb package by Michael Tokarev, mjt@corpit.ru.
 * Public domain.
 *
 * Taken from tinycdb-0.73 and merged into one file for easier
 * inclusion into Dirmngr.  By Werner Koch <wk@gnupg.org> 2003-12-12.
 */

/* A cdb database is a single file used to map 'keys' to 'values',
   having records of (key,value) pairs.  File consists of 3 parts: toc
   (table of contents), data and index (hash tables).

   Toc has fixed length of 2048 bytes, containing 256 pointers to hash
   tables inside index sections.  Every pointer consists of position
   of a hash table in bytes from the beginning of a file, and a size
   of a hash table in entries, both are 4-bytes (32 bits) unsigned
   integers in little-endian form.  Hash table length may have zero
   length, meaning that corresponding hash table is empty.

   Right after toc section, data section follows without any
   alignment.  It consists of series of records, each is a key length,
   value (data) length, key and value.  Again, key and value length
   are 4-byte unsigned integers.  Each next record follows previous
   without any special alignment.

   After data section, index (hash tables) section follows.  It should
   be looked to in conjunction with toc section, where each of max 256
   hash tables are defined.  Index section consists of series of hash
   tables, with starting position and length defined in toc section.
   Every hash table is a sequence of records each holds two numbers:
   key's hash value and record position inside data section (bytes
   from the beginning of a file to first byte of key length starting
   data record).  If record position is zero, then this is an empty
   hash table slot, pointed to nowhere.

   CDB hash function is
     hv = ((hv << 5) + hv) ^ c
   for every single c byte of a key, starting with hv = 5381.

   Toc section indexed by (hv % 256), i.e. hash value modulo 256
   (number of entries in toc section).

   In order to find a record, one should: first, compute the hash
   value (hv) of a key.  Second, look to hash table number hv modulo
   256.  If it is empty, then there is no such key exists.  If it is
   not empty, then third, loop by slots inside that hash table,
   starting from slot with number hv divided by 256 modulo length of
   that table, or ((hv / 256) % htlen), searching for this hv in hash
   table.  Stop search on empty slot (if record position is zero) or
   when all slots was probed (note cyclic search, jumping from end to
   beginning of a table).  When hash value in question is found in
   hash table, look to key of corresponding record, comparing it with
   key in question.  If them of the same length and equals to each
   other, then record is found, otherwise, repeat with next hash table
   slot.  Note that there may be several records with the same key.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#ifdef _WIN32
# include <windows.h>
#else
# include <sys/mman.h>
# ifndef MAP_FAILED
#  define MAP_FAILED ((void*)-1)
# endif
#endif
#include <sys/stat.h>

#include "dirmngr-err.h"
#include "cdb.h"

#ifndef EPROTO
# define EPROTO EINVAL
#endif
#ifndef SEEK_SET
# define SEEK_SET 0
#endif


struct cdb_rec {
  cdbi_t hval;
  cdbi_t rpos;
};

struct cdb_rl {
  struct cdb_rl *next;
  cdbi_t cnt;
  struct cdb_rec rec[254];
};

static int make_find(struct cdb_make *cdbmp,
		   const void *key, cdbi_t klen, cdbi_t hval,
		   struct cdb_rl **rlp);
static int make_write(struct cdb_make *cdbmp,
		    const char *ptr, cdbi_t len);



/* Initializes structure given by CDBP pointer and associates it with
   the open file descriptor FD.  Allocate memory for the structure
   itself if needed and file open operation should be done by
   application.  File FD should be opened at least read-only, and
   should be seekable.  Routine returns 0 on success or negative value
   on error. */
int
cdb_init(struct cdb *cdbp, int fd)
{
  struct stat st;
  unsigned char *mem;
#ifdef _WIN32
  HANDLE hFile, hMapping;
#else
  unsigned int fsize;
#endif

  /* get file size */
  if (fstat(fd, &st) < 0)
    return -1;
  /* trivial sanity check: at least toc should be here */
  if (st.st_size < 2048) {
    gpg_err_set_errno (EPROTO);
    return -1;
  }
  /* memory-map file */
#ifdef _WIN32
# ifdef __MINGW32CE__
  hFile = fd;
# else
  hFile = (HANDLE) _get_osfhandle(fd);
# endif
  if (hFile == (HANDLE) -1)
    return -1;
  hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
  if (!hMapping)
    return -1;
  mem = (unsigned char *)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
  if (!mem)
    return -1;
  cdbp->cdb_mapping = hMapping;
#else /*!_WIN32*/
  fsize = (unsigned int)(st.st_size & 0xffffffffu);
  mem = (unsigned char*)mmap(NULL, fsize, PROT_READ, MAP_SHARED, fd, 0);
  if (mem == MAP_FAILED)
    return -1;
#endif /*!_WIN32*/

  cdbp->cdb_fd = fd;
  cdbp->cdb_fsize = st.st_size;
  cdbp->cdb_mem = mem;

#if 0
  /* XXX don't know well about madvise syscall -- is it legal
     to set different options for parts of one mmap() region?
     There is also posix_madvise() exist, with POSIX_MADV_RANDOM etc...
  */
#ifdef MADV_RANDOM
  /* set madvise() parameters. Ignore errors for now if system
     doesn't support it */
  madvise(mem, 2048, MADV_WILLNEED);
  madvise(mem + 2048, cdbp->cdb_fsize - 2048, MADV_RANDOM);
#endif
#endif

  cdbp->cdb_vpos = cdbp->cdb_vlen = 0;

  return 0;
}


/* Frees the internal resources held by structure.  Note that this
   routine does not close the file. */
void
cdb_free(struct cdb *cdbp)
{
  if (cdbp->cdb_mem) {
#ifdef _WIN32
    UnmapViewOfFile ((void*) cdbp->cdb_mem);
    CloseHandle (cdbp->cdb_mapping);
    cdbp->cdb_mapping = NULL;
#else
    munmap((void*)cdbp->cdb_mem, cdbp->cdb_fsize);
#endif /* _WIN32 */
    cdbp->cdb_mem = NULL;
  }
  cdbp->cdb_fsize = 0;
}


/* Read data from cdb file, starting at position pos of length len,
   placing result to buf.  This routine may be used to get actual
   value found by cdb_find() or other routines that returns position
   and length of a data.  Returns 0 on success or negative value on
   error. */
int
cdb_read(const struct cdb *cdbp, void *buf, unsigned len, cdbi_t pos)
{
  if (pos > cdbp->cdb_fsize || cdbp->cdb_fsize - pos < len) {
    gpg_err_set_errno (EPROTO);
    return -1;
  }
  memcpy(buf, cdbp->cdb_mem + pos, len);
  return 0;
}


/* Attempts to find a key given by (key,klen) parameters.  If key
   exists in database, routine returns 1 and places position and
   length of value associated with this key to internal fields inside
   cdbp structure, to be accessible by cdb_datapos() and
   cdb_datalen().  If key is not in database, routines returns 0.  On
   error, negative value is returned.  Note that using cdb_find() it
   is possible to lookup only first record with a given key. */
int
cdb_find(struct cdb *cdbp, const void *key, cdbi_t klen)
{
  const unsigned char *htp;	/* hash table pointer */
  const unsigned char *htab;	/* hash table */
  const unsigned char *htend;	/* end of hash table */
  cdbi_t httodo;		/* ht bytes left to look */
  cdbi_t pos, n;

  cdbi_t hval;

  if (klen > cdbp->cdb_fsize)	/* if key size is larger than file */
    return 0;

  hval = cdb_hash(key, klen);

  /* find (pos,n) hash table to use */
  /* first 2048 bytes (toc) are always available */
  /* (hval % 256) * 8 */
  htp = cdbp->cdb_mem + ((hval << 3) & 2047); /* index in toc (256x8) */
  n = cdb_unpack(htp + 4);	/* table size */
  if (!n)			/* empty table */
    return 0;			/* not found */
  httodo = n << 3;		/* bytes of htab to lookup */
  pos = cdb_unpack(htp);	/* htab position */
  if (n > (cdbp->cdb_fsize >> 3) /* overflow of httodo ? */
      || pos > cdbp->cdb_fsize /* htab start within file ? */
      || httodo > cdbp->cdb_fsize - pos) /* htab entry within file ? */
  {
    gpg_err_set_errno (EPROTO);
    return -1;
  }

  htab = cdbp->cdb_mem + pos;	/* htab pointer */
  htend = htab + httodo;	/* after end of htab */
  /* htab starting position: rest of hval modulo htsize, 8bytes per elt */
  htp = htab + (((hval >> 8) % n) << 3);

  for(;;) {
    pos = cdb_unpack(htp + 4);	/* record position */
    if (!pos)
      return 0;
    if (cdb_unpack(htp) == hval) {
      if (pos > cdbp->cdb_fsize - 8) { /* key+val lengths */
	gpg_err_set_errno (EPROTO);
	return -1;
      }
      if (cdb_unpack(cdbp->cdb_mem + pos) == klen) {
	if (cdbp->cdb_fsize - klen < pos + 8) {
	  gpg_err_set_errno (EPROTO);
	  return -1;
	}
	if (memcmp(key, cdbp->cdb_mem + pos + 8, klen) == 0) {
	  n = cdb_unpack(cdbp->cdb_mem + pos + 4);
	  pos += 8 + klen;
	  if (cdbp->cdb_fsize < n || cdbp->cdb_fsize - n < pos) {
	    gpg_err_set_errno (EPROTO);
	    return -1;
	  }
	  cdbp->cdb_vpos = pos;
	  cdbp->cdb_vlen = n;
	  return 1;
	}
      }
    }
    httodo -= 8;
    if (!httodo)
      return 0;
    if ((htp += 8) >= htend)
      htp = htab;
  }

}



/* Sequential-find routines that used separate structure.  It is
   possible to have many than one record with the same key in a
   database, and these routines allow enumeration of all of them.
   cdb_findinit() initializes search structure pointed to by cdbfp.
   It will return negative value on error or 0 on success.
   cdb_findnext() attempts to find next matching key, setting value
   position and length in cdbfp structure.  It will return positive
   value if given key was found, 0 if there is no more such key(s), or
   negative value on error.  To access value position and length after
   successeful call to cdb_findnext() (when it returned positive
   result), use cdb_datapos() and cdb_datalen() macros with cdbp
   pointer.  It is error to use cdb_findnext() after it returned 0 or
   error condition.  These routines is a bit slower than cdb_find().

   Setting KEY to NULL will start a sequential search through the
   entire DB.
*/
int
cdb_findinit(struct cdb_find *cdbfp, struct cdb *cdbp,
             const void *key, cdbi_t klen)
{
  cdbi_t n, pos;

  cdbfp->cdb_cdbp = cdbp;
  cdbfp->cdb_key  = key;
  cdbfp->cdb_klen = klen;
  cdbfp->cdb_hval = key? cdb_hash(key, klen) : 0;

  if (key)
    {
      cdbfp->cdb_htp = cdbp->cdb_mem + ((cdbfp->cdb_hval << 3) & 2047);
      n = cdb_unpack(cdbfp->cdb_htp + 4);
      cdbfp->cdb_httodo = n << 3; /* Set to size of hash table. */
      if (!n)
        return 0; /* The hash table is empry. */
      pos = cdb_unpack(cdbfp->cdb_htp);
      if (n > (cdbp->cdb_fsize >> 3)
          || pos > cdbp->cdb_fsize
          || cdbfp->cdb_httodo > cdbp->cdb_fsize - pos)
        {
          gpg_err_set_errno (EPROTO);
          return -1;
        }

      cdbfp->cdb_htab = cdbp->cdb_mem + pos;
      cdbfp->cdb_htend = cdbfp->cdb_htab + cdbfp->cdb_httodo;
      cdbfp->cdb_htp = cdbfp->cdb_htab + (((cdbfp->cdb_hval >> 8) % n) << 3);
    }
  else /* Walk over all entries. */
    {
      cdbfp->cdb_hval = 0;
      /* Force stepping in findnext. */
      cdbfp->cdb_htp = cdbfp->cdb_htend = cdbp->cdb_mem;
    }
  return 0;
}


/* See cdb_findinit. */
int
cdb_findnext(struct cdb_find *cdbfp)
{
  cdbi_t pos, n;
  struct cdb *cdbp = cdbfp->cdb_cdbp;

  if (cdbfp->cdb_key)
    {
      while(cdbfp->cdb_httodo) {
        pos = cdb_unpack(cdbfp->cdb_htp + 4);
        if (!pos)
          return 0;
        n = cdb_unpack(cdbfp->cdb_htp) == cdbfp->cdb_hval;
        if ((cdbfp->cdb_htp += 8) >= cdbfp->cdb_htend)
          cdbfp->cdb_htp = cdbfp->cdb_htab;
        cdbfp->cdb_httodo -= 8;
        if (n) {
          if (pos > cdbp->cdb_fsize - 8) {
            gpg_err_set_errno (EPROTO);
            return -1;
          }
          if (cdb_unpack(cdbp->cdb_mem + pos) == cdbfp->cdb_klen) {
            if (cdbp->cdb_fsize - cdbfp->cdb_klen < pos + 8) {
              gpg_err_set_errno (EPROTO);
              return -1;
            }
            if (memcmp(cdbfp->cdb_key,
                       cdbp->cdb_mem + pos + 8, cdbfp->cdb_klen) == 0) {
              n = cdb_unpack(cdbp->cdb_mem + pos + 4);
              pos += 8 + cdbfp->cdb_klen;
              if (cdbp->cdb_fsize < n || cdbp->cdb_fsize - n < pos) {
                gpg_err_set_errno (EPROTO);
                return -1;
              }
              cdbp->cdb_vpos = pos;
              cdbp->cdb_vlen = n;
              return 1;
            }
          }
        }
      }
    }
  else /* Walk over all entries. */
    {
      do
        {
          while (cdbfp->cdb_htp >= cdbfp->cdb_htend)
            {
              if (cdbfp->cdb_hval > 255)
                return 0; /* No more items. */

              cdbfp->cdb_htp = cdbp->cdb_mem + cdbfp->cdb_hval * 8;
              cdbfp->cdb_hval++; /* Advance for next round. */
              pos = cdb_unpack (cdbfp->cdb_htp);     /* Offset of table. */
              n   = cdb_unpack (cdbfp->cdb_htp + 4); /* Number of entries. */
              cdbfp->cdb_httodo = n * 8;             /* Size of table. */
              if (n > (cdbp->cdb_fsize / 8)
                  || pos > cdbp->cdb_fsize
                  || cdbfp->cdb_httodo > cdbp->cdb_fsize - pos)
                {
                  gpg_err_set_errno (EPROTO);
                  return -1;
                }

              cdbfp->cdb_htab  = cdbp->cdb_mem + pos;
              cdbfp->cdb_htend = cdbfp->cdb_htab + cdbfp->cdb_httodo;
              cdbfp->cdb_htp   = cdbfp->cdb_htab;
            }

          pos = cdb_unpack (cdbfp->cdb_htp + 4); /* Offset of record. */
          cdbfp->cdb_htp += 8;
        }
      while (!pos);
      if (pos > cdbp->cdb_fsize - 8)
        {
          gpg_err_set_errno (EPROTO);
          return -1;
        }

      cdbp->cdb_kpos = pos + 8;
      cdbp->cdb_klen = cdb_unpack(cdbp->cdb_mem + pos);
      cdbp->cdb_vpos = pos + 8 + cdbp->cdb_klen;
      cdbp->cdb_vlen = cdb_unpack(cdbp->cdb_mem + pos + 4);
      n = 8 + cdbp->cdb_klen + cdbp->cdb_vlen;
      if ( pos > cdbp->cdb_fsize || pos > cdbp->cdb_fsize - n)
        {
          gpg_err_set_errno (EPROTO);
          return -1;
        }
      return 1; /* Found. */
    }
  return 0;
}

/* Read a chunk from file, ignoring interrupts (EINTR) */
int
cdb_bread(int fd, void *buf, int len)
{
  int l;
  while(len > 0) {
    do l = read(fd, buf, len);
    while(l < 0 && errno == EINTR);
    if (l <= 0) {
      if (!l)
        gpg_err_set_errno (EIO);
      return -1;
    }
    buf = (char*)buf + l;
    len -= l;
  }
  return 0;
}

/* Find a given key in cdb file, seek a file pointer to it's value and
   place data length to *dlenp. */
int
cdb_seek(int fd, const void *key, unsigned klen, cdbi_t *dlenp)
{
  cdbi_t htstart;		/* hash table start position */
  cdbi_t htsize;		/* number of elements in a hash table */
  cdbi_t httodo;		/* hash table elements left to look */
  cdbi_t hti;			/* hash table index */
  cdbi_t pos;			/* position in a file */
  cdbi_t hval;			/* key's hash value */
  unsigned char rbuf[64];	/* read buffer */
  int needseek = 1;		/* if we should seek to a hash slot */

  hval = cdb_hash(key, klen);
  pos = (hval & 0xff) << 3; /* position in TOC */
  /* read the hash table parameters */
  if (lseek(fd, pos, SEEK_SET) < 0 || cdb_bread(fd, rbuf, 8) < 0)
    return -1;
  if ((htsize = cdb_unpack(rbuf + 4)) == 0)
    return 0;
  hti = (hval >> 8) % htsize;	/* start position in hash table */
  httodo = htsize;
  htstart = cdb_unpack(rbuf);

  for(;;) {
    if (needseek && lseek(fd, htstart + (hti << 3), SEEK_SET) < 0)
      return -1;
    if (cdb_bread(fd, rbuf, 8) < 0)
      return -1;
    if ((pos = cdb_unpack(rbuf + 4)) == 0) /* not found */
      return 0;

    if (cdb_unpack(rbuf) != hval) /* hash value not matched */
      needseek = 0;
    else { /* hash value matched */
      if (lseek(fd, pos, SEEK_SET) < 0 || cdb_bread(fd, rbuf, 8) < 0)
	return -1;
      if (cdb_unpack(rbuf) == klen) { /* key length matches */
	/* read the key from file and compare with wanted */
	cdbi_t l = klen, c;
	const char *k = (const char*)key;
	if (*dlenp)
	  *dlenp = cdb_unpack(rbuf + 4); /* save value length */
	for(;;) {
	  if (!l) /* the whole key read and matches, return */
	    return 1;
	  c = l > sizeof(rbuf) ? sizeof(rbuf) : l;
	  if (cdb_bread(fd, rbuf, c) < 0)
	    return -1;
	  if (memcmp(rbuf, k, c) != 0) /* no, it differs, stop here */
	    break;
	  k += c; l -= c;
	}
      }
      needseek = 1; /* we're looked to other place, should seek back */
    }
    if (!--httodo)
      return 0;
    if (++hti == htsize) {
      hti = htstart;
      needseek = 1;
    }
  }
}

cdbi_t
cdb_unpack(const unsigned char buf[4])
{
  cdbi_t n = buf[3];
  n <<= 8; n |= buf[2];
  n <<= 8; n |= buf[1];
  n <<= 8; n |= buf[0];
  return n;
}

/* Add record with key (KEY,KLEN) and value (VAL,VLEN) to a database.
   Returns 0 on success or negative value on error.  Note that this
   routine does not checks if given key already exists, but cdb_find()
   will not see second record with the same key.  It is not possible
   to continue building a database if cdb_make_add() returned an error
   indicator. */
int
cdb_make_add(struct cdb_make *cdbmp,
	     const void *key, cdbi_t klen,
	     const void *val, cdbi_t vlen)
{
  unsigned char rlen[8];
  cdbi_t hval;
  struct cdb_rl *rl;
  if (klen > 0xffffffff - (cdbmp->cdb_dpos + 8) ||
      vlen > 0xffffffff - (cdbmp->cdb_dpos + klen + 8)) {
    gpg_err_set_errno (ENOMEM);
    return -1;
  }
  hval = cdb_hash(key, klen);
  rl = cdbmp->cdb_rec[hval&255];
  if (!rl || rl->cnt >= sizeof(rl->rec)/sizeof(rl->rec[0])) {
    rl = (struct cdb_rl*)malloc(sizeof(struct cdb_rl));
    if (!rl) {
      gpg_err_set_errno (ENOMEM);
      return -1;
    }
    rl->cnt = 0;
    rl->next = cdbmp->cdb_rec[hval&255];
    cdbmp->cdb_rec[hval&255] = rl;
  }
  rl->rec[rl->cnt].hval = hval;
  rl->rec[rl->cnt].rpos = cdbmp->cdb_dpos;
  ++rl->cnt;
  ++cdbmp->cdb_rcnt;
  cdb_pack(klen, rlen);
  cdb_pack(vlen, rlen + 4);
  if (make_write(cdbmp, rlen, 8) < 0 ||
      make_write(cdbmp, key, klen) < 0 ||
      make_write(cdbmp, val, vlen) < 0)
    return -1;
  return 0;
}

int
cdb_make_put(struct cdb_make *cdbmp,
	     const void *key, cdbi_t klen,
	     const void *val, cdbi_t vlen,
	     int flags)
{
  unsigned char rlen[8];
  cdbi_t hval = cdb_hash(key, klen);
  struct cdb_rl *rl;
  int c, r;

  switch(flags) {
    case CDB_PUT_REPLACE:
    case CDB_PUT_INSERT:
    case CDB_PUT_WARN:
      c = make_find(cdbmp, key, klen, hval, &rl);
      if (c < 0)
	return -1;
      if (c) {
	if (flags == CDB_PUT_INSERT) {
	  gpg_err_set_errno (EEXIST);
	  return 1;
	}
	else if (flags == CDB_PUT_REPLACE) {
	  --c;
	  r = 1;
	  break;
	}
	else
	  r = 1;
      }
      /* fall through */

    case CDB_PUT_ADD:
      rl = cdbmp->cdb_rec[hval&255];
      if (!rl || rl->cnt >= sizeof(rl->rec)/sizeof(rl->rec[0])) {
 	rl = (struct cdb_rl*)malloc(sizeof(struct cdb_rl));
	if (!rl) {
	  gpg_err_set_errno (ENOMEM);
	  return -1;
	}
	rl->cnt = 0;
	rl->next = cdbmp->cdb_rec[hval&255];
	cdbmp->cdb_rec[hval&255] = rl;
      }
      c = rl->cnt;
      r = 0;
      break;

    default:
      gpg_err_set_errno (EINVAL);
      return -1;
  }

  if (klen > 0xffffffff - (cdbmp->cdb_dpos + 8) ||
      vlen > 0xffffffff - (cdbmp->cdb_dpos + klen + 8)) {
    gpg_err_set_errno (ENOMEM);
    return -1;
  }
  rl->rec[c].hval = hval;
  rl->rec[c].rpos = cdbmp->cdb_dpos;
  if (c == rl->cnt) {
    ++rl->cnt;
    ++cdbmp->cdb_rcnt;
  }
  cdb_pack(klen, rlen);
  cdb_pack(vlen, rlen + 4);
  if (make_write(cdbmp, rlen, 8) < 0 ||
      make_write(cdbmp, key, klen) < 0 ||
      make_write(cdbmp, val, vlen) < 0)
    return -1;
  return r;
}


static int
match(int fd, cdbi_t pos, const char *key, cdbi_t klen)
{
  unsigned char buf[64]; /*XXX cdb_buf may be used here instead */
  if (lseek(fd, pos, SEEK_SET) < 0 || read(fd, buf, 8) != 8)
    return -1;
  if (cdb_unpack(buf) != klen)
    return 0;

  while(klen > sizeof(buf)) {
    if (read(fd, buf, sizeof(buf)) != sizeof(buf))
      return -1;
    if (memcmp(buf, key, sizeof(buf)) != 0)
      return 0;
    key += sizeof(buf);
    klen -= sizeof(buf);
  }
  if (klen) {
    if (read(fd, buf, klen) != klen)
      return -1;
    if (memcmp(buf, key, klen) != 0)
      return 0;
  }
  return 1;
}


static int
make_find (struct cdb_make *cdbmp,
           const void *key, cdbi_t klen, cdbi_t hval,
           struct cdb_rl **rlp)
{
  struct cdb_rl *rl = cdbmp->cdb_rec[hval&255];
  int r, i;
  int sought = 0;
  while(rl) {
    for(i = rl->cnt - 1; i >= 0; --i) { /* search backward */
      if (rl->rec[i].hval != hval)
	continue;
      /*XXX this explicit flush may be unnecessary having
       * smarter match() that looks to cdb_buf too, but
       * most of a time here spent in finding hash values
       * (above), not keys */
      if (cdbmp->cdb_bpos != cdbmp->cdb_buf) {
        if (write(cdbmp->cdb_fd, cdbmp->cdb_buf,
	          cdbmp->cdb_bpos - cdbmp->cdb_buf) < 0)
          return -1;
        cdbmp->cdb_bpos = cdbmp->cdb_buf;
      }
      sought = 1;
      r = match(cdbmp->cdb_fd, rl->rec[i].rpos, key, klen);
      if (!r)
	continue;
      if (r < 0)
	return -1;
      if (lseek(cdbmp->cdb_fd, cdbmp->cdb_dpos, SEEK_SET) < 0)
        return -1;
      if (rlp)
	*rlp = rl;
      return i + 1;
    }
    rl = rl->next;
  }
  if (sought && lseek(cdbmp->cdb_fd, cdbmp->cdb_dpos, SEEK_SET) < 0)
    return -1;
  return 0;
}

int
cdb_make_exists(struct cdb_make *cdbmp,
                const void *key, cdbi_t klen)
{
  return make_find(cdbmp, key, klen, cdb_hash(key, klen), NULL);
}


void
cdb_pack(cdbi_t num, unsigned char buf[4])
{
  buf[0] = num & 255; num >>= 8;
  buf[1] = num & 255; num >>= 8;
  buf[2] = num & 255;
  buf[3] = num >> 8;
}


/* Initializes structure to create a database.  File FD should be
   opened read-write and should be seekable.  Returns 0 on success or
   negative value on error. */
int
cdb_make_start(struct cdb_make *cdbmp, int fd)
{
  memset (cdbmp, 0, sizeof *cdbmp);
  cdbmp->cdb_fd = fd;
  cdbmp->cdb_dpos = 2048;
  cdbmp->cdb_bpos = cdbmp->cdb_buf + 2048;
  return 0;
}


static int
ewrite(int fd, const char *buf, int len)
{
  while(len) {
    int l = write(fd, buf, len);
    if (l < 0 && errno != EINTR)
      return -1;
    if (l > 0)
      {
        len -= l;
        buf += l;
      }
  }
  return 0;
}

static int
make_write(struct cdb_make *cdbmp, const char *ptr, cdbi_t len)
{
  cdbi_t l = sizeof(cdbmp->cdb_buf) - (cdbmp->cdb_bpos - cdbmp->cdb_buf);
  cdbmp->cdb_dpos += len;
  if (len > l) {
    memcpy(cdbmp->cdb_bpos, ptr, l);
    if (ewrite(cdbmp->cdb_fd, cdbmp->cdb_buf, sizeof(cdbmp->cdb_buf)) < 0)
      return -1;
    ptr += l; len -= l;
    l = len / sizeof(cdbmp->cdb_buf);
    if (l) {
      l *= sizeof(cdbmp->cdb_buf);
      if (ewrite(cdbmp->cdb_fd, ptr, l) < 0)
	return -1;
      ptr += l; len -= l;
    }
    cdbmp->cdb_bpos = cdbmp->cdb_buf;
  }
  if (len) {
    memcpy(cdbmp->cdb_bpos, ptr, len);
    cdbmp->cdb_bpos += len;
  }
  return 0;
}

static int
cdb_make_finish_internal(struct cdb_make *cdbmp)
{
  cdbi_t hcnt[256];		/* hash table counts */
  cdbi_t hpos[256];		/* hash table positions */
  struct cdb_rec *htab;
  unsigned char *p;
  struct cdb_rl *rl;
  cdbi_t hsize;
  unsigned t, i;

  if (((0xffffffff - cdbmp->cdb_dpos) >> 3) < cdbmp->cdb_rcnt) {
    gpg_err_set_errno (ENOMEM);
    return -1;
  }

  /* count htab sizes and reorder reclists */
  hsize = 0;
  for (t = 0; t < 256; ++t) {
    struct cdb_rl *rlt = NULL;
    i = 0;
    rl = cdbmp->cdb_rec[t];
    while(rl) {
      struct cdb_rl *rln = rl->next;
      rl->next = rlt;
      rlt = rl;
      i += rl->cnt;
      rl = rln;
    }
    cdbmp->cdb_rec[t] = rlt;
    if (hsize < (hcnt[t] = i << 1))
      hsize = hcnt[t];
  }

  /* allocate memory to hold max htable */
  htab = (struct cdb_rec*)malloc((hsize + 2) * sizeof(struct cdb_rec));
  if (!htab) {
    gpg_err_set_errno (ENOENT);
    return -1;
  }
  p = (unsigned char *)htab;
  htab += 2;

  /* build hash tables */
  for (t = 0; t < 256; ++t) {
    cdbi_t len, hi;
    hpos[t] = cdbmp->cdb_dpos;
    if ((len = hcnt[t]) == 0)
      continue;
    for (i = 0; i < len; ++i)
      htab[i].hval = htab[i].rpos = 0;
    for (rl = cdbmp->cdb_rec[t]; rl; rl = rl->next)
      for (i = 0; i < rl->cnt; ++i) {
	hi = (rl->rec[i].hval >> 8) % len;
	while(htab[hi].rpos)
	  if (++hi == len)
	    hi = 0;
	htab[hi] = rl->rec[i];
      }
    for (i = 0; i < len; ++i) {
      cdb_pack(htab[i].hval, p + (i << 3));
      cdb_pack(htab[i].rpos, p + (i << 3) + 4);
    }
    if (make_write(cdbmp, p, len << 3) < 0) {
      free(p);
      return -1;
    }
  }
  free(p);
  if (cdbmp->cdb_bpos != cdbmp->cdb_buf &&
      ewrite(cdbmp->cdb_fd, cdbmp->cdb_buf,
	     cdbmp->cdb_bpos - cdbmp->cdb_buf) != 0)
      return -1;
  p = cdbmp->cdb_buf;
  for (t = 0; t < 256; ++t) {
    cdb_pack(hpos[t], p + (t << 3));
    cdb_pack(hcnt[t], p + (t << 3) + 4);
  }
  if (lseek(cdbmp->cdb_fd, 0, 0) != 0 ||
      ewrite(cdbmp->cdb_fd, p, 2048) != 0)
    return -1;

  return 0;
}

static void
cdb_make_free(struct cdb_make *cdbmp)
{
  unsigned t;
  for(t = 0; t < 256; ++t) {
    struct cdb_rl *rl = cdbmp->cdb_rec[t];
    while(rl) {
      struct cdb_rl *tm = rl;
      rl = rl->next;
      free(tm);
    }
  }
}



/* Finalizes database file, constructing all needed indexes, and frees
   memory structures.  It does not close the file descriptor.  Returns
   0 on success or a negative value on error. */
int
cdb_make_finish(struct cdb_make *cdbmp)
{
  int r = cdb_make_finish_internal(cdbmp);
  cdb_make_free(cdbmp);
  return r;
}


cdbi_t
cdb_hash(const void *buf, cdbi_t len)
{
  register const unsigned char *p = (const unsigned char *)buf;
  register const unsigned char *end = p + len;
  register cdbi_t hash = 5381;	/* start value */
  while (p < end)
    hash = (hash + (hash << 5)) ^ *p++;
  return hash;
}