1 /* $Id: kcompare.c,v 1.46.2.2 2004-08-06 09:36:07 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
30 #define INT_CODEC_NEW 0
32 #define CODEC_INLINE inline
37 void key_logdump_txt (int logmask, const void *p, const char *txt)
42 memcpy (&key, p, sizeof(key));
43 logf (logmask, "%7d:%-4d %s", key.sysno, key.seqno,txt);
46 logf(logmask, " (null) %s",txt);
49 void key_logdump (int logmask, const void *p)
51 key_logdump_txt(logmask,p,"");
54 int key_compare_it (const void *p1, const void *p2)
56 if (((struct it_key *) p1)->sysno != ((struct it_key *) p2)->sysno)
58 if (((struct it_key *) p1)->sysno > ((struct it_key *) p2)->sysno)
63 if (((struct it_key *) p1)->seqno != ((struct it_key *) p2)->seqno)
65 if (((struct it_key *) p1)->seqno > ((struct it_key *) p2)->seqno)
73 char *key_print_it (const void *p, char *buf)
75 const struct it_key *i = p;
76 sprintf (buf, "%d:%d", i->sysno, i->seqno);
80 int key_compare (const void *p1, const void *p2)
83 memcpy (&i1, p1, sizeof(i1));
84 memcpy (&i2, p2, sizeof(i2));
85 if (i1.sysno != i2.sysno)
87 if (i1.sysno > i2.sysno)
92 if (i1.seqno != i2.seqno)
94 if (i1.seqno > i2.seqno)
102 int key_get_seq(const void *p)
105 memcpy (&k, p, sizeof(k));
109 int key_qsort_compare (const void *p1, const void *p2)
113 char *cp1 = *(char **) p1;
114 char *cp2 = *(char **) p2;
116 if ((r = strcmp (cp1, cp2)))
119 if ((r = key_compare (cp1+l+1, cp2+l+1)))
121 return cp1[l] - cp2[l];
124 struct iscz1_code_info {
128 void *iscz1_code_start (int mode)
130 struct iscz1_code_info *p = (struct iscz1_code_info *)
131 xmalloc (sizeof(*p));
137 void iscz1_code_reset (void *vp)
139 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
144 void iscz1_code_stop (int mode, void *p)
150 static CODEC_INLINE void iscz1_encode_int (unsigned d, char **dst)
152 unsigned char *bp = (unsigned char*) *dst;
156 *bp++ = 128 | (d & 127);
163 static CODEC_INLINE int iscz1_decode_int (unsigned char **src)
169 while (((c = *(*src)++) & 128))
178 /* ! INT_CODEC_NEW */
180 static CODEC_INLINE void iscz1_encode_int (unsigned d, char **dst)
182 unsigned char *bp = (unsigned char*) *dst;
191 else if (d <= 4194303)
193 *bp++ = 128 | (d>>16);
194 *bp++ = (d>>8) & 255;
199 *bp++ = 192 | (d>>24);
200 *bp++ = (d>>16) & 255;
201 *bp++ = (d>>8) & 255;
207 static CODEC_INLINE int iscz1_decode_int (unsigned char **src)
209 unsigned c = *(*src)++;
215 return ((c & 63) << 8) + *(*src)++;
217 c = ((c & 63) << 8) + *(*src)++;
218 c = (c << 8) + *(*src)++;
221 if (c&32) /* expand sign bit to high bits */
222 c = ((c | 63) << 8) + *(*src)++;
224 c = ((c & 63) << 8) + *(*src)++;
225 c = (c << 8) + *(*src)++;
226 c = (c << 8) + *(*src)++;
232 void iscz1_code_item (int mode, void *vp, char **dst, char **src)
234 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
238 if (mode == ISAMC_ENCODE)
240 memcpy (&tkey, *src, sizeof(struct it_key));
241 d = tkey.sysno - p->key.sysno;
244 iscz1_encode_int (2*tkey.seqno + 1, dst);
245 iscz1_encode_int (d, dst);
247 p->key.seqno = tkey.seqno;
251 iscz1_encode_int (2*(tkey.seqno - p->key.seqno), dst);
252 p->key.seqno = tkey.seqno;
254 (*src) += sizeof(struct it_key);
258 d = iscz1_decode_int ((unsigned char **) src);
262 p->key.sysno += iscz1_decode_int ((unsigned char **) src);
265 p->key.seqno += d>>1;
266 memcpy (*dst, &p->key, sizeof(struct it_key));
267 (*dst) += sizeof(struct it_key);
271 ISAMS_M *key_isams_m (Res res, ISAMS_M *me)
273 isams_getmethod (me);
275 me->compare_item = key_compare;
276 me->log_item = key_logdump_txt;
278 me->code_start = iscz1_code_start;
279 me->code_item = iscz1_code_item;
280 me->code_stop = iscz1_code_stop;
282 me->debug = atoi(res_get_def (res, "isamsDebug", "0"));
287 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me)
291 me->compare_item = key_compare;
292 me->log_item = key_logdump_txt;
294 me->code_start = iscz1_code_start;
295 me->code_item = iscz1_code_item;
296 me->code_stop = iscz1_code_stop;
297 me->code_reset = iscz1_code_reset;
299 me->debug = atoi(res_get_def (res, "isamcDebug", "0"));
304 ISAMD_M *key_isamd_m (Res res, ISAMD_M *me)
306 me = isamd_getmethod (me);
308 me->compare_item = key_compare;
309 me->log_item = key_logdump_txt;
311 me->code_start = iscz1_code_start;
312 me->code_item = iscz1_code_item;
313 me->code_stop = iscz1_code_stop;
314 me->code_reset = iscz1_code_reset;
316 me->debug = atoi(res_get_def (res, "isamdDebug", "0"));
321 int key_SU_encode (int ch, char *out)
327 out[i] = 65 + (ch & 63);
346 int key_SU_decode (int *ch, const unsigned char *out)
351 for (len = 1; *out >= 65; len++, out++)
353 *ch += (*out - 65) * fact;
356 *ch += (*out - 1) * fact;