2 /* Adapted from DJB's original cdb-0.75 package */
14 static int cdb_make_write(struct cdb_make *c, char *buf, uint32 sz)
16 fwrite(buf, sz, 1, c->fp);
20 int cdb_make_start(struct cdb_make *c, FILE *f)
27 c->pos = sizeof c->final;
28 if (fseek(f, c->pos, SEEK_SET) == -1) {
29 perror("fseek failed");
35 static int posplus(struct cdb_make *c, uint32 len)
37 uint32 newpos = c->pos + len;
46 int cdb_make_addend(struct cdb_make *c, unsigned int keylen, unsigned int datalen, uint32 h)
48 struct cdb_hplist *head;
51 if (!head || (head->num >= CDB_HPLIST)) {
52 head = (struct cdb_hplist *) malloc(sizeof(struct cdb_hplist));
60 head->hp[head->num].h = h;
61 head->hp[head->num].p = c->pos;
64 if (posplus(c, 8) == -1) {
67 if (posplus(c, keylen) == -1) {
70 if (posplus(c, datalen) == -1) {
76 int cdb_make_addbegin(struct cdb_make *c, unsigned int keylen, unsigned int datalen)
80 /*if (keylen > 0xffffffff) {
84 if (datalen > 0xffffffff) {
89 uint32_pack(buf, keylen);
90 uint32_pack(buf + 4, datalen);
91 if (cdb_make_write(c, buf, 8) != 0) {
94 /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
98 int cdb_make_add(struct cdb_make *c, char *key, unsigned int keylen, char *data, unsigned int datalen)
100 if (cdb_make_addbegin(c, keylen, datalen) == -1) {
103 if (cdb_make_write(c, key, keylen) != 0) {
106 if (cdb_make_write(c, data, datalen) != 0) {
109 /* if (buffer_putalign(&c->b,key,keylen) == -1) return -1; */
110 /* if (buffer_putalign(&c->b,data,datalen) == -1) return -1; */
111 return cdb_make_addend(c, keylen, datalen, cdb_hash(key, keylen));
114 int cdb_make_finish(struct cdb_make *c)
123 struct cdb_hplist *x;
126 for (i = 0; i < 256; ++i) {
130 for (x = c->head; x; x = x->next) {
133 ++c->count[255 & x->hp[i].h];
138 for (i = 0; i < 256; ++i) {
145 memsize += c->numentries; /* no overflow possible up to now */
146 u = (uint32) 0 - (uint32) 1;
147 u /= sizeof(struct cdb_hp);
153 c->split = (struct cdb_hp *) malloc(memsize * sizeof(struct cdb_hp));
158 c->hash = c->split + c->numentries;
161 for (i = 0; i < 256; ++i) {
162 u += c->count[i]; /* bounded by numentries, so no overflow */
166 for (x = c->head; x; x = x->next) {
169 c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
173 for (i = 0; i < 256; ++i) {
176 len = count + count; /* no overflow possible */
177 uint32_pack(c->final + 8 * i, c->pos);
178 uint32_pack(c->final + 8 * i + 4, len);
180 for (u = 0; u < len; ++u) {
181 c->hash[u].h = c->hash[u].p = 0;
184 hp = c->split + c->start[i];
185 for (u = 0; u < count; ++u) {
186 where = (hp->h >> 8) % len;
187 while (c->hash[where].p)
188 if (++where == len) {
191 c->hash[where] = *hp++;
194 for (u = 0; u < len; ++u) {
195 uint32_pack(buf, c->hash[u].h);
196 uint32_pack(buf + 4, c->hash[u].p);
197 if (cdb_make_write(c, buf, 8) != 0) {
200 /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
201 if (posplus(c, 8) == -1) {
211 for (x = c->head; x; c->head = x) {
216 if (fflush(c->fp) != 0) {
219 /* if (buffer_flush(&c->b) == -1) return -1; */
221 if (ftell(c->fp) != 0) {
224 /* if (seek_begin(c->fd) == -1) return -1; */
225 if (cdb_make_write(c, c->final, sizeof c->final) != 0) {
228 return fflush(c->fp);
229 /* return buffer_putflush(&c->b,c->final,sizeof c->final); */