1 /* $Id: kcompare.c,v 1.46 2004-06-09 13:11:49 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
31 #define CODEC_INLINE inline
32 void key_logdump_txt (int logmask, const void *p, const char *txt)
37 memcpy (&key, p, sizeof(key));
38 logf (logmask, "%7d:%-4d %s", key.sysno, key.seqno,txt);
41 logf(logmask, " (null) %s",txt);
44 void key_logdump (int logmask, const void *p)
46 key_logdump_txt(logmask,p,"");
49 int key_compare_it (const void *p1, const void *p2)
51 if (((struct it_key *) p1)->sysno != ((struct it_key *) p2)->sysno)
53 if (((struct it_key *) p1)->sysno > ((struct it_key *) p2)->sysno)
58 if (((struct it_key *) p1)->seqno != ((struct it_key *) p2)->seqno)
60 if (((struct it_key *) p1)->seqno > ((struct it_key *) p2)->seqno)
68 char *key_print_it (const void *p, char *buf)
70 const struct it_key *i = p;
71 sprintf (buf, "%d:%d", i->sysno, i->seqno);
75 int key_compare (const void *p1, const void *p2)
78 memcpy (&i1, p1, sizeof(i1));
79 memcpy (&i2, p2, sizeof(i2));
80 if (i1.sysno != i2.sysno)
82 if (i1.sysno > i2.sysno)
87 if (i1.seqno != i2.seqno)
89 if (i1.seqno > i2.seqno)
97 int key_get_seq(const void *p)
100 memcpy (&k, p, sizeof(k));
104 int key_qsort_compare (const void *p1, const void *p2)
108 char *cp1 = *(char **) p1;
109 char *cp2 = *(char **) p2;
111 if ((r = strcmp (cp1, cp2)))
114 if ((r = key_compare (cp1+l+1, cp2+l+1)))
116 return cp1[l] - cp2[l];
119 struct iscz1_code_info {
123 void *iscz1_code_start (int mode)
125 struct iscz1_code_info *p = (struct iscz1_code_info *)
126 xmalloc (sizeof(*p));
132 void iscz1_code_reset (void *vp)
134 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
139 void iscz1_code_stop (int mode, void *p)
145 static CODEC_INLINE void iscz1_encode_int (unsigned d, char **dst)
147 unsigned char *bp = (unsigned char*) *dst;
151 *bp++ = 128 | (d & 127);
158 static CODEC_INLINE int iscz1_decode_int (unsigned char **src)
164 while (((c = *(*src)++) & 128))
173 /* ! INT_CODEC_NEW */
175 static CODEC_INLINE void iscz1_encode_int (unsigned d, char **dst)
177 unsigned char *bp = (unsigned char*) *dst;
186 else if (d <= 4194303)
188 *bp++ = 128 | (d>>16);
189 *bp++ = (d>>8) & 255;
194 *bp++ = 192 | (d>>24);
195 *bp++ = (d>>16) & 255;
196 *bp++ = (d>>8) & 255;
202 static CODEC_INLINE int iscz1_decode_int (unsigned char **src)
204 unsigned c = *(*src)++;
210 return ((c & 63) << 8) + *(*src)++;
212 c = ((c & 63) << 8) + *(*src)++;
213 c = (c << 8) + *(*src)++;
216 if (c&32) /* expand sign bit to high bits */
217 c = ((c | 63) << 8) + *(*src)++;
219 c = ((c & 63) << 8) + *(*src)++;
220 c = (c << 8) + *(*src)++;
221 c = (c << 8) + *(*src)++;
227 void iscz1_code_item (int mode, void *vp, char **dst, char **src)
229 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
233 if (mode == ISAMC_ENCODE)
235 memcpy (&tkey, *src, sizeof(struct it_key));
236 d = tkey.sysno - p->key.sysno;
239 iscz1_encode_int (2*tkey.seqno + 1, dst);
240 iscz1_encode_int (d, dst);
242 p->key.seqno = tkey.seqno;
246 iscz1_encode_int (2*(tkey.seqno - p->key.seqno), dst);
247 p->key.seqno = tkey.seqno;
249 (*src) += sizeof(struct it_key);
253 d = iscz1_decode_int ((unsigned char **) src);
257 p->key.sysno += iscz1_decode_int ((unsigned char **) src);
260 p->key.seqno += d>>1;
261 memcpy (*dst, &p->key, sizeof(struct it_key));
262 (*dst) += sizeof(struct it_key);
266 ISAMS_M *key_isams_m (Res res, ISAMS_M *me)
268 isams_getmethod (me);
270 me->compare_item = key_compare;
271 me->log_item = key_logdump_txt;
273 me->code_start = iscz1_code_start;
274 me->code_item = iscz1_code_item;
275 me->code_stop = iscz1_code_stop;
277 me->debug = atoi(res_get_def (res, "isamsDebug", "0"));
282 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me)
286 me->compare_item = key_compare;
287 me->log_item = key_logdump_txt;
289 me->code_start = iscz1_code_start;
290 me->code_item = iscz1_code_item;
291 me->code_stop = iscz1_code_stop;
292 me->code_reset = iscz1_code_reset;
294 me->debug = atoi(res_get_def (res, "isamcDebug", "0"));
299 ISAMD_M *key_isamd_m (Res res, ISAMD_M *me)
301 me = isamd_getmethod (me);
303 me->compare_item = key_compare;
304 me->log_item = key_logdump_txt;
306 me->code_start = iscz1_code_start;
307 me->code_item = iscz1_code_item;
308 me->code_stop = iscz1_code_stop;
309 me->code_reset = iscz1_code_reset;
311 me->debug = atoi(res_get_def (res, "isamdDebug", "0"));
316 int key_SU_encode (int ch, char *out)
322 out[i] = 65 + (ch & 63);
341 int key_SU_decode (int *ch, const unsigned char *out)
346 for (len = 1; *out >= 65; len++, out++)
348 *ch += (*out - 65) * fact;
351 *ch += (*out - 1) * fact;