2 /* Adapted from DJB's original cdb-0.75 package */
13 static int cdb_make_write(struct cdb_make *c, char *buf, uint32 sz) {
14 fwrite(buf, sz, 1, c->fp);
18 int cdb_make_start(struct cdb_make *c, FILE * f)
25 c->pos = sizeof c->final;
26 if (fseek(f,c->pos,SEEK_SET) == -1) {
27 perror("fseek failed");
33 static int posplus(struct cdb_make *c,uint32 len)
35 uint32 newpos = c->pos + len;
36 if (newpos < len) { errno = ENOMEM; return -1; }
41 int cdb_make_addend(struct cdb_make *c,unsigned int keylen,unsigned int datalen,uint32 h)
43 struct cdb_hplist *head;
46 if (!head || (head->num >= CDB_HPLIST)) {
47 head = (struct cdb_hplist *) malloc(sizeof(struct cdb_hplist));
53 head->hp[head->num].h = h;
54 head->hp[head->num].p = c->pos;
57 if (posplus(c,8) == -1) return -1;
58 if (posplus(c,keylen) == -1) return -1;
59 if (posplus(c,datalen) == -1) return -1;
63 int cdb_make_addbegin(struct cdb_make *c,unsigned int keylen,unsigned int datalen)
67 if (keylen > 0xffffffff) { errno = ENOMEM; return -1; }
68 if (datalen > 0xffffffff) { errno = ENOMEM; return -1; }
70 uint32_pack(buf,keylen);
71 uint32_pack(buf + 4,datalen);
72 if (cdb_make_write(c,buf,8) != 0) return -1;
73 /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
77 int cdb_make_add(struct cdb_make *c,char *key,unsigned int keylen,char *data,unsigned int datalen)
79 if (cdb_make_addbegin(c,keylen,datalen) == -1) return -1;
80 if (cdb_make_write(c,key,keylen) != 0) return -1;
81 if (cdb_make_write(c,data,datalen) != 0) return -1;
82 /* if (buffer_putalign(&c->b,key,keylen) == -1) return -1; */
83 /* if (buffer_putalign(&c->b,data,datalen) == -1) return -1; */
84 return cdb_make_addend(c,keylen,datalen,cdb_hash(key,keylen));
87 int cdb_make_finish(struct cdb_make *c)
99 for (i = 0;i < 256;++i)
102 for (x = c->head;x;x = x->next) {
105 ++c->count[255 & x->hp[i].h];
109 for (i = 0;i < 256;++i) {
115 memsize += c->numentries; /* no overflow possible up to now */
116 u = (uint32) 0 - (uint32) 1;
117 u /= sizeof(struct cdb_hp);
118 if (memsize > u) { errno = ENOMEM; return -1; }
120 c->split = (struct cdb_hp *) malloc(memsize * sizeof(struct cdb_hp));
121 if (!c->split) return -1;
123 c->hash = c->split + c->numentries;
126 for (i = 0;i < 256;++i) {
127 u += c->count[i]; /* bounded by numentries, so no overflow */
131 for (x = c->head;x;x = x->next) {
134 c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
137 for (i = 0;i < 256;++i) {
140 len = count + count; /* no overflow possible */
141 uint32_pack(c->final + 8 * i,c->pos);
142 uint32_pack(c->final + 8 * i + 4,len);
144 for (u = 0;u < len;++u)
145 c->hash[u].h = c->hash[u].p = 0;
147 hp = c->split + c->start[i];
148 for (u = 0;u < count;++u) {
149 where = (hp->h >> 8) % len;
150 while (c->hash[where].p)
153 c->hash[where] = *hp++;
156 for (u = 0;u < len;++u) {
157 uint32_pack(buf,c->hash[u].h);
158 uint32_pack(buf + 4,c->hash[u].p);
159 if (cdb_make_write(c,buf,8) != 0) return -1;
160 /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
161 if (posplus(c,8) == -1) return -1;
165 if (c->split) free(c->split);
167 for (x = c->head;x;c->head = x) {
172 if (fflush(c->fp) != 0) return -1;
173 /* if (buffer_flush(&c->b) == -1) return -1; */
175 if (ftell(c->fp) != 0) return -1;
176 /* if (seek_begin(c->fd) == -1) return -1; */
177 if (cdb_make_write(c,c->final,sizeof c->final) != 0) return -1;
178 return fflush(c->fp);
179 /* return buffer_putflush(&c->b,c->final,sizeof c->final); */