1 /* $Id: kcompare.c,v 1.42 2004-05-30 18:35:12 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
34 void key_logdump (int logmask, const void *p)
38 memcpy (&key, p, sizeof(key));
39 logf (logmask, "%7d s=%-4d", key.sysno, key.seqno);
42 int key_compare_it (const void *p1, const void *p2)
44 if (((struct it_key *) p1)->sysno != ((struct it_key *) p2)->sysno)
46 if (((struct it_key *) p1)->sysno > ((struct it_key *) p2)->sysno)
51 if (((struct it_key *) p1)->seqno != ((struct it_key *) p2)->seqno)
53 if (((struct it_key *) p1)->seqno > ((struct it_key *) p2)->seqno)
61 char *key_print_it (const void *p, char *buf)
63 const struct it_key *i = p;
64 sprintf (buf, "%d:%d", i->sysno, i->seqno);
68 int key_compare (const void *p1, const void *p2)
71 memcpy (&i1, p1, sizeof(i1));
72 memcpy (&i2, p2, sizeof(i2));
73 if (i1.sysno != i2.sysno)
75 if (i1.sysno > i2.sysno)
80 if (i1.seqno != i2.seqno)
82 if (i1.seqno > i2.seqno)
90 int key_qsort_compare (const void *p1, const void *p2)
94 char *cp1 = *(char **) p1;
95 char *cp2 = *(char **) p2;
97 if ((r = strcmp (cp1, cp2)))
100 if ((r = key_compare (cp1+l+1, cp2+l+1)))
102 return cp1[l] - cp2[l];
105 struct iscz1_code_info {
110 static void *iscz1_code_start (int mode)
112 struct iscz1_code_info *p = (struct iscz1_code_info *)
113 xmalloc (sizeof(*p));
119 static void iscz1_code_reset (void *vp)
121 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
126 static void iscz1_code_stop (int mode, void *p)
132 CODEC_INLINE void iscz1_encode_int (unsigned d, char **dst)
134 unsigned char *bp = (unsigned char*) *dst;
138 *bp++ = 128 | (d & 127);
145 CODEC_INLINE int iscz1_decode_int (unsigned char **src)
151 while (((c = *(*src)++) & 128))
160 /* ! INT_CODEC_NEW */
162 CODEC_INLINE void iscz1_encode_int (unsigned d, char **dst)
164 unsigned char *bp = (unsigned char*) *dst;
173 else if (d <= 4194303)
175 *bp++ = 128 | (d>>16);
176 *bp++ = (d>>8) & 255;
181 *bp++ = 192 | (d>>24);
182 *bp++ = (d>>16) & 255;
183 *bp++ = (d>>8) & 255;
189 CODEC_INLINE int iscz1_decode_int (unsigned char **src)
191 unsigned c = *(*src)++;
197 return ((c & 63) << 8) + *(*src)++;
199 c = ((c & 63) << 8) + *(*src)++;
200 c = (c << 8) + *(*src)++;
203 if (c&32) /* expand sign bit to high bits */
204 c = ((c | 63) << 8) + *(*src)++;
206 c = ((c & 63) << 8) + *(*src)++;
207 c = (c << 8) + *(*src)++;
208 c = (c << 8) + *(*src)++;
214 static void iscz1_code_item (int mode, void *vp, char **dst, char **src)
216 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
220 if (mode == ISAMC_ENCODE)
222 memcpy (&tkey, *src, sizeof(struct it_key));
223 d = tkey.sysno - p->key.sysno;
226 iscz1_encode_int (2*tkey.seqno + 1, dst);
227 iscz1_encode_int (d, dst);
229 p->key.seqno = tkey.seqno;
233 iscz1_encode_int (2*(tkey.seqno - p->key.seqno), dst);
234 p->key.seqno = tkey.seqno;
236 (*src) += sizeof(struct it_key);
240 d = iscz1_decode_int ((unsigned char **) src);
244 p->key.sysno += iscz1_decode_int ((unsigned char **) src);
247 p->key.seqno += d>>1;
248 memcpy (*dst, &p->key, sizeof(struct it_key));
249 (*dst) += sizeof(struct it_key);
253 ISAMS_M *key_isams_m (Res res, ISAMS_M *me)
255 isams_getmethod (me);
257 me->compare_item = key_compare;
259 me->code_start = iscz1_code_start;
260 me->code_item = iscz1_code_item;
261 me->code_stop = iscz1_code_stop;
263 me->debug = atoi(res_get_def (res, "isamsDebug", "0"));
268 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me)
272 me->compare_item = key_compare;
274 me->code_start = iscz1_code_start;
275 me->code_item = iscz1_code_item;
276 me->code_stop = iscz1_code_stop;
277 me->code_reset = iscz1_code_reset;
279 me->debug = atoi(res_get_def (res, "isamcDebug", "0"));
284 ISAMD_M *key_isamd_m (Res res, ISAMD_M *me)
286 me = isamd_getmethod (me);
288 me->compare_item = key_compare;
290 me->code_start = iscz1_code_start;
291 me->code_item = iscz1_code_item;
292 me->code_stop = iscz1_code_stop;
293 me->code_reset = iscz1_code_reset;
295 me->debug = atoi(res_get_def (res, "isamdDebug", "0"));
300 int key_SU_encode (int ch, char *out)
306 out[i] = 65 + (ch & 63);
325 int key_SU_decode (int *ch, const unsigned char *out)
330 for (len = 1; *out >= 65; len++, out++)
332 *ch += (*out - 65) * fact;
335 *ch += (*out - 1) * fact;