1 /* $Id: kcompare.c,v 1.41 2003-06-23 15:35:25 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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
33 void key_logdump (int logmask, const void *p)
37 memcpy (&key, p, sizeof(key));
38 logf (logmask, "%7d s=%-4d", key.sysno, key.seqno);
41 int key_compare_it (const void *p1, const void *p2)
43 if (((struct it_key *) p1)->sysno != ((struct it_key *) p2)->sysno)
45 if (((struct it_key *) p1)->sysno > ((struct it_key *) p2)->sysno)
50 if (((struct it_key *) p1)->seqno != ((struct it_key *) p2)->seqno)
52 if (((struct it_key *) p1)->seqno > ((struct it_key *) p2)->seqno)
60 char *key_print_it (const void *p, char *buf)
62 const struct it_key *i = p;
63 sprintf (buf, "%d:%d", i->sysno, i->seqno);
67 int key_compare (const void *p1, const void *p2)
70 memcpy (&i1, p1, sizeof(i1));
71 memcpy (&i2, p2, sizeof(i2));
72 if (i1.sysno != i2.sysno)
74 if (i1.sysno > i2.sysno)
79 if (i1.seqno != i2.seqno)
81 if (i1.seqno > i2.seqno)
89 int key_qsort_compare (const void *p1, const void *p2)
93 char *cp1 = *(char **) p1;
94 char *cp2 = *(char **) p2;
96 if ((r = strcmp (cp1, cp2)))
99 if ((r = key_compare (cp1+l+1, cp2+l+1)))
101 return cp1[l] - cp2[l];
104 int key_get_pos (const void *p)
107 memcpy (&key, p, sizeof(key));
111 struct iscz1_code_info {
115 static void *iscz1_code_start (int mode)
117 struct iscz1_code_info *p = (struct iscz1_code_info *)
118 xmalloc (sizeof(*p));
124 static void iscz1_code_reset (void *vp)
126 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
131 static void iscz1_code_stop (int mode, void *p)
136 void iscz1_encode_int (unsigned d, char **dst)
138 unsigned char *bp = (unsigned char*) *dst;
147 else if (d <= 4194303)
149 *bp++ = 128 | (d>>16);
150 *bp++ = (d>>8) & 255;
155 *bp++ = 192 | (d>>24);
156 *bp++ = (d>>16) & 255;
157 *bp++ = (d>>8) & 255;
163 int iscz1_decode_int (unsigned char **src)
165 unsigned c = *(*src)++;
171 return ((c & 63) << 8) + *(*src)++;
173 c = ((c & 63) << 8) + *(*src)++;
174 c = (c << 8) + *(*src)++;
177 if (c&32) /* expand sign bit to high bits */
178 c = ((c | 63) << 8) + *(*src)++;
180 c = ((c & 63) << 8) + *(*src)++;
181 c = (c << 8) + *(*src)++;
182 c = (c << 8) + *(*src)++;
187 static void iscz1_code_item (int mode, void *vp, char **dst, char **src)
189 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
193 if (mode == ISAMC_ENCODE)
195 memcpy (&tkey, *src, sizeof(struct it_key));
196 d = tkey.sysno - p->key.sysno;
199 iscz1_encode_int (2*tkey.seqno + 1, dst);
200 iscz1_encode_int (d, dst);
202 p->key.seqno = tkey.seqno;
206 iscz1_encode_int (2*(tkey.seqno - p->key.seqno), dst);
207 p->key.seqno = tkey.seqno;
209 (*src) += sizeof(struct it_key);
213 d = iscz1_decode_int ((unsigned char **) src);
217 p->key.sysno += iscz1_decode_int ((unsigned char **) src);
220 p->key.seqno += d>>1;
221 memcpy (*dst, &p->key, sizeof(struct it_key));
222 (*dst) += sizeof(struct it_key);
226 ISAMS_M *key_isams_m (Res res, ISAMS_M *me)
228 isams_getmethod (me);
230 me->compare_item = key_compare;
232 me->code_start = iscz1_code_start;
233 me->code_item = iscz1_code_item;
234 me->code_stop = iscz1_code_stop;
236 me->debug = atoi(res_get_def (res, "isamsDebug", "0"));
241 ISAMC_M *key_isamc_m (Res res, ISAMC_M *me)
245 me->compare_item = key_compare;
247 me->code_start = iscz1_code_start;
248 me->code_item = iscz1_code_item;
249 me->code_stop = iscz1_code_stop;
250 me->code_reset = iscz1_code_reset;
252 me->debug = atoi(res_get_def (res, "isamcDebug", "0"));
257 ISAMD_M *key_isamd_m (Res res, ISAMD_M *me)
259 me = isamd_getmethod (me);
261 me->compare_item = key_compare;
263 me->code_start = iscz1_code_start;
264 me->code_item = iscz1_code_item;
265 me->code_stop = iscz1_code_stop;
266 me->code_reset = iscz1_code_reset;
268 me->debug = atoi(res_get_def (res, "isamdDebug", "0"));
273 int key_SU_encode (int ch, char *out)
279 out[i] = 65 + (ch & 63);
298 int key_SU_decode (int *ch, const unsigned char *out)
303 for (len = 1; *out >= 65; len++, out++)
305 *ch += (*out - 65) * fact;
308 *ch += (*out - 1) * fact;