- char buf[8];
- int i;
- uint32 len;
- uint32 u;
- uint32 memsize;
- uint32 count;
- uint32 where;
- struct cdb_hplist *x;
- struct cdb_hp *hp;
-
- for (i = 0;i < 256;++i)
- c->count[i] = 0;
-
- for (x = c->head;x;x = x->next) {
- i = x->num;
- while (i--)
- ++c->count[255 & x->hp[i].h];
- }
-
- memsize = 1;
- for (i = 0;i < 256;++i) {
- u = c->count[i] * 2;
- if (u > memsize)
- memsize = u;
- }
-
- memsize += c->numentries; /* no overflow possible up to now */
- u = (uint32) 0 - (uint32) 1;
- u /= sizeof(struct cdb_hp);
- if (memsize > u) { errno = ENOMEM; return -1; }
-
- c->split = (struct cdb_hp *) malloc(memsize * sizeof(struct cdb_hp));
- if (!c->split) return -1;
-
- c->hash = c->split + c->numentries;
-
- u = 0;
- for (i = 0;i < 256;++i) {
- u += c->count[i]; /* bounded by numentries, so no overflow */
- c->start[i] = u;
- }
-
- for (x = c->head;x;x = x->next) {
- i = x->num;
- while (i--)
- c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
- }
-
- for (i = 0;i < 256;++i) {
- count = c->count[i];
-
- len = count + count; /* no overflow possible */
- uint32_pack(c->final + 8 * i,c->pos);
- uint32_pack(c->final + 8 * i + 4,len);
-
- for (u = 0;u < len;++u)
- c->hash[u].h = c->hash[u].p = 0;
-
- hp = c->split + c->start[i];
- for (u = 0;u < count;++u) {
- where = (hp->h >> 8) % len;
- while (c->hash[where].p)
- if (++where == len)
- where = 0;
- c->hash[where] = *hp++;
- }
-
- for (u = 0;u < len;++u) {
- uint32_pack(buf,c->hash[u].h);
- uint32_pack(buf + 4,c->hash[u].p);
- if (cdb_make_write(c,buf,8) != 0) return -1;
- /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
- if (posplus(c,8) == -1) return -1;
- }
- }
-
- if (c->split) free(c->split);
-
- for (x = c->head;x;c->head = x) {
- x = x->next;
- free(c->head);
- }
-
- if (fflush(c->fp) != 0) return -1;
- /* if (buffer_flush(&c->b) == -1) return -1; */
- rewind(c->fp);
- if (ftell(c->fp) != 0) return -1;
- /* if (seek_begin(c->fd) == -1) return -1; */
- if (cdb_make_write(c,c->final,sizeof c->final) != 0) return -1;
- return fflush(c->fp);
- /* return buffer_putflush(&c->b,c->final,sizeof c->final); */
+ char buf[8];
+ int i;
+ uint32 len;
+ uint32 u;
+ uint32 memsize;
+ uint32 count;
+ uint32 where;
+ struct cdb_hplist *x;
+ struct cdb_hp *hp;
+
+ for (i = 0; i < 256; ++i) {
+ c->count[i] = 0;
+ }
+
+ for (x = c->head; x; x = x->next) {
+ i = x->num;
+ while (i--) {
+ ++c->count[255 & x->hp[i].h];
+ }
+ }
+
+ memsize = 1;
+ for (i = 0; i < 256; ++i) {
+ u = c->count[i] * 2;
+ if (u > memsize) {
+ memsize = u;
+ }
+ }
+
+ memsize += c->numentries; /* no overflow possible up to now */
+ u = (uint32) 0 - (uint32) 1;
+ u /= sizeof(struct cdb_hp);
+ if (memsize > u) {
+ errno = ENOMEM;
+ return -1;
+ }
+
+ c->split = (struct cdb_hp *) malloc(memsize * sizeof(struct cdb_hp));
+ if (!c->split) {
+ return -1;
+ }
+
+ c->hash = c->split + c->numentries;
+
+ u = 0;
+ for (i = 0; i < 256; ++i) {
+ u += c->count[i]; /* bounded by numentries, so no overflow */
+ c->start[i] = u;
+ }
+
+ for (x = c->head; x; x = x->next) {
+ i = x->num;
+ while (i--) {
+ c->split[--c->start[255 & x->hp[i].h]] = x->hp[i];
+ }
+ }
+
+ for (i = 0; i < 256; ++i) {
+ count = c->count[i];
+
+ len = count + count; /* no overflow possible */
+ uint32_pack(c->final + 8 * i, c->pos);
+ uint32_pack(c->final + 8 * i + 4, len);
+
+ for (u = 0; u < len; ++u) {
+ c->hash[u].h = c->hash[u].p = 0;
+ }
+
+ hp = c->split + c->start[i];
+ for (u = 0; u < count; ++u) {
+ where = (hp->h >> 8) % len;
+ while (c->hash[where].p)
+ if (++where == len) {
+ where = 0;
+ }
+ c->hash[where] = *hp++;
+ }
+
+ for (u = 0; u < len; ++u) {
+ uint32_pack(buf, c->hash[u].h);
+ uint32_pack(buf + 4, c->hash[u].p);
+ if (cdb_make_write(c, buf, 8) != 0) {
+ return -1;
+ }
+ /* if (buffer_putalign(&c->b,buf,8) == -1) return -1; */
+ if (posplus(c, 8) == -1) {
+ return -1;
+ }
+ }
+ }
+
+ if (c->split) {
+ free(c->split);
+ }
+
+ for (x = c->head; x; c->head = x) {
+ x = x->next;
+ free(c->head);
+ }
+
+ if (fflush(c->fp) != 0) {
+ return -1;
+ }
+ /* if (buffer_flush(&c->b) == -1) return -1; */
+ rewind(c->fp);
+ if (ftell(c->fp) != 0) {
+ return -1;
+ }
+ /* if (seek_begin(c->fd) == -1) return -1; */
+ if (cdb_make_write(c, c->final, sizeof c->final) != 0) {
+ return -1;
+ }
+ return fflush(c->fp);
+ /* return buffer_putflush(&c->b,c->final,sizeof c->final); */