+ 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;
+}
+
+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;
+ }
+ c = ((c & 63) << 8) + *(*src)++;
+ c = (c << 8) + *(*src)++;
+ c = (c << 8) + *(*src)++;
+ return c;
+}
+
+static void iscz1_code_item (int mode, void *vp, char **dst, char **src)
+{
+ struct iscz1_code_info *p = vp;
+ struct it_key tkey;
+ int d;
+
+ if (mode == ISAMC_ENCODE)
+ {
+ memcpy (&tkey, *src, sizeof(struct it_key));
+ 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);
+ }
+ 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);
+ }
+}
+
+ISAMC_M key_isamc_m (Res res)
+{
+ static ISAMC_M me = NULL;
+
+ if (me)
+ return me;
+
+ me = isc_getmethod ();
+
+ me->compare_item = key_compare;
+
+ me->code_start = iscz1_code_start;
+ me->code_item = iscz1_code_item;
+ me->code_stop = iscz1_code_stop;
+
+ me->debug = atoi(res_get_def (res, "isamcDebug", "0"));
+
+ return me;
+}
+
+int key_SU_code (int ch, char *out)
+{
+ int i;
+ for (i = 0; ch; i++)
+ {
+ if (ch > 63)
+ out[i] = 128 + (ch & 63);
+ else
+ out[i] = 1 + ch;
+ ch = ch >> 6;
+ }
+ return i;