+struct iscz1_code_info {
+ struct it_key key;
+};
+
+void *iscz1_start (void)
+{
+ struct iscz1_code_info *p = (struct iscz1_code_info *)
+ xmalloc (sizeof(*p));
+ iscz1_reset(p);
+ return p;
+}
+
+#if IT_KEY_NEW
+void key_init(struct it_key *key)
+{
+ int i;
+ key->len = 0;
+ for (i = 0; i<IT_KEY_LEVEL_MAX; i++)
+ key->mem[i] = 0;
+}
+#endif
+
+void iscz1_reset (void *vp)
+{
+ struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
+#if IT_KEY_NEW
+ int i;
+ p->key.len = 0;
+ for (i = 0; i< IT_KEY_LEVEL_MAX; i++)
+ p->key.mem[i] = 0;
+#else
+ p->key.sysno = 0;
+ p->key.seqno = 0;
+#endif
+}
+
+void iscz1_stop (void *p)
+{
+ xfree (p);
+}
+
+#if INT_CODEC_NEW
+/* small encoder that works with unsigneds of any length */
+static CODEC_INLINE void iscz1_encode_int (zint d, char **dst)
+{
+ unsigned char *bp = (unsigned char*) *dst;
+
+ while (d > 127)
+ {
+ *bp++ = (unsigned) (128 | (d & 127));
+ d = d >> 7;
+ }
+ *bp++ = (unsigned) d;
+ *dst = (char *) bp;
+}
+
+/* small decoder that works with unsigneds of any length */
+static CODEC_INLINE zint iscz1_decode_int (unsigned char **src)
+{
+ zint d = 0;
+ unsigned char c;
+ unsigned r = 0;
+
+ while (((c = *(*src)++) & 128))
+ {
+ d += ((zint) (c&127) << r);
+ r += 7;
+ }
+ d += ((zint) c << r);
+ return d;
+}
+#else
+/* ! INT_CODEC_NEW */
+
+/* old encoder that works with unsigneds up to 30 bits */
+static CODEC_INLINE void iscz1_encode_int (unsigned d, char **dst)
+{
+ unsigned char *bp = (unsigned char*) *dst;
+
+ if (d <= 63)
+ *bp++ = d;
+ else if (d <= 16383)
+ {
+ *bp++ = 64 | (d>>8);
+ *bp++ = d & 255;
+ }
+ else if (d <= 4194303)
+ {
+ *bp++ = 128 | (d>>16);
+ *bp++ = (d>>8) & 255;
+ *bp++ = d & 255;
+ }
+ else
+ {
+ *bp++ = 192 | (d>>24);
+ *bp++ = (d>>16) & 255;
+ *bp++ = (d>>8) & 255;
+ *bp++ = d & 255;
+ }
+ *dst = (char *) bp;
+}
+
+/* old decoder that works with unsigneds up to 30 bits */
+static CODEC_INLINE int iscz1_decode_int (unsigned char **src)
+{
+ unsigned c = *(*src)++;
+ switch (c & 192)
+ {
+ case 0:
+ return c;
+ case 64:
+ return ((c & 63) << 8) + *(*src)++;
+ case 128:
+ c = ((c & 63) << 8) + *(*src)++;
+ c = (c << 8) + *(*src)++;
+ return c;
+ }
+ if (c&32) /* expand sign bit to high bits */
+ c = ((c | 63) << 8) + *(*src)++;
+ else
+ c = ((c & 63) << 8) + *(*src)++;
+ c = (c << 8) + *(*src)++;
+ c = (c << 8) + *(*src)++;
+
+ return c;
+}
+#endif
+
+void iscz1_encode (void *vp, char **dst, const char **src)
+{
+ struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
+ struct it_key tkey;
+#if IT_KEY_NEW
+ zint d;
+ int i;
+#else
+ int d;
+#endif
+
+ /* 1
+ 3, 2, 9, 12
+ 3, 2, 10, 2
+ 4, 1
+
+ if diff is 0, then there is more ...
+ if diff is non-zero, then _may_ be more
+ */
+ memcpy (&tkey, *src, sizeof(struct it_key));
+#if IT_KEY_NEW
+ /* deal with leader + delta encoding .. */
+ d = 0;
+ assert(tkey.len > 0 && tkey.len <= 4);
+ for (i = 0; i < tkey.len; i++)
+ {
+ d = tkey.mem[i] - p->key.mem[i];
+ if (d || i == tkey.len-1)
+ { /* all have been equal until now, now make delta .. */
+ p->key.mem[i] = tkey.mem[i];
+ if (d > 0)
+ {
+ iscz1_encode_int (i + (tkey.len << 3) + 64, dst);
+ i++;
+ iscz1_encode_int (d, dst);
+ }
+ else
+ {
+ iscz1_encode_int (i + (tkey.len << 3), dst);
+ }
+ break;
+ }
+ }
+ /* rest uses absolute encoding ... */
+ for (; i < tkey.len; i++)
+ {
+ iscz1_encode_int (tkey.mem[i], dst);
+ p->key.mem[i] = tkey.mem[i];
+ }
+ (*src) += sizeof(struct it_key);
+#else
+ d = tkey.sysno - p->key.sysno;
+ if (d)
+ {
+ iscz1_encode_int (2*tkey.seqno + 1, dst);
+ iscz1_encode_int (d, dst);
+ p->key.sysno += d;
+ p->key.seqno = tkey.seqno;
+ }
+ else
+ {
+ iscz1_encode_int (2*(tkey.seqno - p->key.seqno), dst);
+ p->key.seqno = tkey.seqno;
+ }
+ (*src) += sizeof(struct it_key);
+#endif
+}
+
+void iscz1_decode (void *vp, char **dst, const char **src)
+{
+ struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
+#if IT_KEY_NEW
+ int i;
+#else
+ int d;
+#endif
+
+#if IT_KEY_NEW
+ int leader = (int) iscz1_decode_int ((unsigned char **) src);
+ i = leader & 7;
+ if (leader & 64)
+ p->key.mem[i] += iscz1_decode_int ((unsigned char **) src);
+ else
+ p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
+ p->key.len = (leader >> 3) & 7;
+ while (++i < p->key.len)
+ p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
+ memcpy (*dst, &p->key, sizeof(struct it_key));
+ (*dst) += sizeof(struct it_key);
+#else
+ d = iscz1_decode_int ((unsigned char **) src);
+ if (d & 1)
+ {
+ p->key.seqno = d>>1;
+ p->key.sysno += iscz1_decode_int ((unsigned char **) src);
+ }
+ else
+ p->key.seqno += d>>1;
+ memcpy (*dst, &p->key, sizeof(struct it_key));
+ (*dst) += sizeof(struct it_key);
+#endif
+}
+
+ISAMS_M *key_isams_m (Res res, ISAMS_M *me)
+{
+ isams_getmethod (me);
+
+ me->compare_item = key_compare;
+ me->log_item = key_logdump_txt;
+
+ me->codec.start = iscz1_start;
+ me->codec.decode = iscz1_decode;
+ me->codec.encode = iscz1_encode;
+ me->codec.stop = iscz1_stop;
+ me->codec.reset = iscz1_reset;
+
+ me->debug = atoi(res_get_def (res, "isamsDebug", "0"));
+
+ return me;
+}
+
+ISAMC_M *key_isamc_m (Res res, ISAMC_M *me)