nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

cdbmss.c (1591B)


      1 #include "cdbmss.h"
      2 
      3 #include "readwrite.h"
      4 #include "seek.h"
      5 #include "alloc.h"
      6 
      7 int cdbmss_start(c,fd)
      8 struct cdbmss *c;
      9 int fd;
     10 {
     11   cdbmake_init(&c->cdbm);
     12   c->fd = fd;
     13   c->pos = sizeof(c->cdbm.final);
     14   substdio_fdbuf(&c->ss,write,fd,c->ssbuf,sizeof(c->ssbuf));
     15   return seek_set(fd,(seek_pos) c->pos);
     16 }
     17 
     18 int cdbmss_add(c,key,keylen,data,datalen)
     19 struct cdbmss *c;
     20 unsigned char *key;
     21 unsigned int keylen;
     22 unsigned char *data;
     23 unsigned int datalen;
     24 {
     25   uint32 h;
     26   int i;
     27 
     28   cdbmake_pack(c->packbuf,(uint32) keylen);
     29   cdbmake_pack(c->packbuf + 4,(uint32) datalen);
     30   if (substdio_put(&c->ss,c->packbuf,8) == -1) return -1;
     31   if (substdio_put(&c->ss,(char *)key,keylen) == -1) return -1;
     32   if (substdio_put(&c->ss,(char *)data,datalen) == -1) return -1;
     33 
     34   h = CDBMAKE_HASHSTART;
     35   for (i = 0;i < keylen;++i)
     36     h = cdbmake_hashadd(h,(unsigned int) key[i]);
     37 
     38   if (!cdbmake_add(&c->cdbm,h,c->pos)) return -1;
     39 
     40   c->pos += 8 + keylen + datalen; /* XXX: overflow? */
     41   return 0;
     42 }
     43 
     44 int cdbmss_finish(c)
     45 struct cdbmss *c;
     46 {
     47   int i;
     48   uint32 len;
     49   uint32 u;
     50 
     51   if (!cdbmake_split(&c->cdbm)) return -1;
     52 
     53   for (i = 0;i < 256;++i) {
     54     len = cdbmake_throw(&c->cdbm,c->pos,i);
     55     for (u = 0;u < len;++u) {
     56       cdbmake_pack(c->packbuf,c->cdbm.hash[u].h);
     57       cdbmake_pack(c->packbuf + 4,c->cdbm.hash[u].p);
     58       if (substdio_put(&c->ss,c->packbuf,8) == -1) return -1;
     59       c->pos += 8; /* XXX: overflow? */
     60     }
     61   }
     62 
     63   if (substdio_flush(&c->ss) == -1) return -1;
     64   if (seek_begin(c->fd) == -1) return -1;
     65   return substdio_putflush(&c->ss,c->cdbm.final,sizeof(c->cdbm.final));
     66 }