+ logf (LOG_WARN|LOG_ERRNO, "cannot open %s", fname);
+ return ;
+ }
+ if (!f->length)
+ {
+ if ((f->length = lseek (fd, 0L, SEEK_END)) == (off_t) -1)
+ {
+ logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
+ close (fd);
+ return ;
+ }
+ }
+ if (lseek (fd, f->offset, SEEK_SET) == -1)
+ {
+ logf (LOG_WARN|LOG_ERRNO, "cannot seek %s", fname);
+ close(fd);
+ return ;
+ }
+ while (f->chunk - nr > 0)
+ {
+ r = read (fd, f->buf + nr, f->chunk - nr);
+ if (r <= 0)
+ break;
+ nr += r;
+ }
+ if (r == -1)
+ {
+ logf (LOG_WARN|LOG_ERRNO, "read of %s", fname);
+ close (fd);
+ return;
+ }
+ f->buf_size = nr;
+ if (f->readHandler)
+ (*f->readHandler)(f, f->readInfo);
+ close (fd);
+}
+
+void key_file_destroy (struct key_file *f)
+{
+#if IT_KEY_NEW
+ iscz1_stop(f->decode_handle);
+#endif
+ xfree (f->buf);
+ xfree (f->prev_name);
+ xfree (f);
+}
+
+struct key_file *key_file_init (int no, int chunk, Res res)
+{
+ struct key_file *f;
+
+ f = (struct key_file *) xmalloc (sizeof(*f));
+ f->res = res;
+#if IT_KEY_NEW
+ f->decode_handle = iscz1_start();
+#else
+ f->sysno = 0;
+ f->seqno = 0;
+#endif
+ f->no = no;
+ f->chunk = chunk;
+ f->offset = 0;
+ f->length = 0;
+ f->readHandler = NULL;
+ f->buf = (unsigned char *) xmalloc (f->chunk);
+ f->prev_name = (char *) xmalloc (INP_NAME_MAX);
+ *f->prev_name = '\0';
+ key_file_chunk_read (f);
+ return f;
+}
+
+int key_file_getc (struct key_file *f)
+{
+ if (f->buf_ptr < f->buf_size)
+ return f->buf[(f->buf_ptr)++];
+ if (f->buf_size < f->chunk)
+ return EOF;
+ f->offset += f->buf_size;
+ key_file_chunk_read (f);
+ if (f->buf_ptr < f->buf_size)
+ return f->buf[(f->buf_ptr)++];
+ else
+ return EOF;
+}
+
+int key_file_decode (struct key_file *f)
+{
+ int c, d;
+
+ c = key_file_getc (f);
+ switch (c & 192)
+ {
+ case 0:
+ d = c;
+ break;
+ case 64:
+ d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
+ break;
+ case 128:
+ d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
+ d = (d << 8) + (key_file_getc (f) & 0xff);
+ break;
+ default: /* 192 */
+ d = ((c&63) << 8) + (key_file_getc (f) & 0xff);
+ d = (d << 8) + (key_file_getc (f) & 0xff);
+ d = (d << 8) + (key_file_getc (f) & 0xff);
+ break;
+ }
+ return d;
+}
+
+int key_file_read (struct key_file *f, char *key)
+{
+ int i, c;
+ char srcbuf[128];
+#if IT_KEY_NEW
+ const char *src = srcbuf;
+ char *dst;
+ int j;
+#else
+ struct it_key itkey;
+ int d;
+#endif
+
+ c = key_file_getc (f);
+ if (c == 0)
+ {
+ strcpy (key, f->prev_name);
+ i = 1+strlen (key);
+ }
+ else if (c == EOF)
+ return 0;
+ else
+ {
+ i = 0;
+ key[i++] = c;
+ while ((key[i++] = key_file_getc (f)))
+ ;
+ strcpy (f->prev_name, key);
+#if IT_KEY_NEW
+ iscz1_reset(f->decode_handle);
+#endif
+ }
+#if IT_KEY_NEW
+ c = key_file_getc(f); /* length + insert/delete combined */
+ key[i++] = c & 128;
+ c = c & 127;
+ for (j = 0; j < c; j++)
+ srcbuf[j] = key_file_getc(f);
+ dst = key + i;
+ iscz1_decode(f->decode_handle, &dst, &src);
+ return i + sizeof(struct it_key);
+#else
+ d = key_file_decode (f);
+ key[i++] = d & 1;
+ d = d >> 1;
+ itkey.sysno = d + f->sysno;
+ if (d)
+ {
+ f->sysno = itkey.sysno;
+ f->seqno = 0;
+ }
+ d = key_file_decode (f);
+ itkey.seqno = d + f->seqno;
+ f->seqno = itkey.seqno;
+ memcpy (key + i, &itkey, sizeof(struct it_key));
+ return i + sizeof (struct it_key);
+#endif
+}
+
+struct heap_info {
+ struct {
+ struct key_file **file;
+ char **buf;
+ } info;
+ int heapnum;
+ int *ptr;
+ int (*cmp)(const void *p1, const void *p2);
+ struct zebra_register *reg;
+ ZebraHandle zh; /* only used for raw reading that bypasses the heaps */
+ int no_diffs;
+ int no_updates;
+ int no_deletions;
+ int no_insertions;
+ int no_iterations;
+};
+
+static struct heap_info *key_heap_malloc()
+{ /* malloc and clear it */
+ struct heap_info *hi;
+ hi = (struct heap_info *) xmalloc (sizeof(*hi));
+ hi->info.file = 0;
+ hi->info.buf = 0;
+ hi->heapnum = 0;
+ hi->ptr = 0;
+ hi->zh=0;
+ hi->no_diffs = 0;
+ hi->no_diffs = 0;
+ hi->no_updates = 0;
+ hi->no_deletions = 0;
+ hi->no_insertions = 0;
+ hi->no_iterations = 0;
+ return hi;
+}
+
+struct heap_info *key_heap_init (int nkeys,
+ int (*cmp)(const void *p1, const void *p2))
+{
+ struct heap_info *hi;
+ int i;
+
+ hi = key_heap_malloc();
+ hi->info.file = (struct key_file **)
+ xmalloc (sizeof(*hi->info.file) * (1+nkeys));
+ hi->info.buf = (char **) xmalloc (sizeof(*hi->info.buf) * (1+nkeys));
+ hi->ptr = (int *) xmalloc (sizeof(*hi->ptr) * (1+nkeys));
+ hi->cmp = cmp;
+ for (i = 0; i<= nkeys; i++)
+ {
+ hi->ptr[i] = i;
+ hi->info.buf[i] = (char *) xmalloc (INP_NAME_MAX);
+ }
+ return hi;
+}
+
+struct heap_info *key_heap_init_buff ( ZebraHandle zh,
+ int (*cmp)(const void *p1, const void *p2))
+{
+ struct heap_info *hi=key_heap_malloc();
+ hi->cmp=cmp;
+ hi->zh=zh;
+ return hi;