1 /* This file is part of the Zebra server.
2 Copyright (C) 1995-2008 Index Data
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <yaz/xmalloc.h>
29 #define CODEC_INLINE inline
34 void key_logdump_txt(int logmask, const void *p, const char *txt)
44 memcpy (&key, p, sizeof(key));
45 assert(key.len > 0 && key.len <= IT_KEY_LEVEL_MAX);
47 for (i = 0; i<key.len; i++)
51 sprintf(formstr + strlen(formstr), ZINT_FORMAT, key.mem[i]);
53 yaz_log(logmask, "%s %s", formstr, txt);
56 yaz_log(logmask, " (no key) %s",txt);
59 void key_logdump(int logmask, const void *p)
61 key_logdump_txt(logmask, p, "");
64 int key_compare_it (const void *p1, const void *p2)
66 int i, l = ((struct it_key *) p1)->len;
67 if (((struct it_key *) p2)->len > l)
68 l = ((struct it_key *) p2)->len;
69 assert (l <= IT_KEY_LEVEL_MAX && l > 0);
70 for (i = 0; i < l; i++)
72 if (((struct it_key *) p1)->mem[i] != ((struct it_key *) p2)->mem[i])
74 if (((struct it_key *) p1)->mem[i] > ((struct it_key *) p2)->mem[i])
83 char *key_print_it (const void *p, char *buf)
89 int key_compare (const void *p1, const void *p2)
93 memcpy (&i1, p1, sizeof(i1));
94 memcpy (&i2, p2, sizeof(i2));
98 assert (l <= IT_KEY_LEVEL_MAX && l > 0);
99 for (i = 0; i < l; i++)
101 if (i1.mem[i] != i2.mem[i])
103 if (i1.mem[i] > i2.mem[i])
112 zint key_get_seq(const void *p)
115 memcpy (&k, p, sizeof(k));
116 return k.mem[k.len-1];
119 zint key_get_segment(const void *p)
122 memcpy (&k, p, sizeof(k));
123 return k.mem[k.len-2];
126 int key_qsort_compare (const void *p1, const void *p2)
130 char *cp1 = *(char **) p1;
131 char *cp2 = *(char **) p2;
133 if ((r = strcmp (cp1, cp2)))
136 if ((r = key_compare (cp1+l+1, cp2+l+1)))
138 return cp1[l] - cp2[l];
141 struct iscz1_code_info {
145 void *iscz1_start (void)
147 struct iscz1_code_info *p = (struct iscz1_code_info *)
148 xmalloc (sizeof(*p));
153 void key_init(struct it_key *key)
157 for (i = 0; i < IT_KEY_LEVEL_MAX; i++)
161 void iscz1_reset (void *vp)
163 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
166 for (i = 0; i < IT_KEY_LEVEL_MAX; i++)
170 void iscz1_stop (void *p)
175 /* small encoder that works with unsigneds of any length */
176 static CODEC_INLINE void iscz1_encode_int (zint d, char **dst)
178 unsigned char *bp = (unsigned char*) *dst;
182 *bp++ = (unsigned) (128 | (d & 127));
185 *bp++ = (unsigned) d;
189 /* small decoder that works with unsigneds of any length */
190 static CODEC_INLINE zint iscz1_decode_int (unsigned char **src)
196 while (((c = *(*src)++) & 128))
198 d += ((zint) (c&127) << r);
201 d += ((zint) c << r);
205 void iscz1_encode (void *vp, char **dst, const char **src)
207 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
217 if diff is 0, then there is more ...
218 if diff is non-zero, then _may_ be more
220 memcpy (&tkey, *src, sizeof(struct it_key));
222 /* deal with leader + delta encoding .. */
224 assert(tkey.len > 0 && tkey.len <= IT_KEY_LEVEL_MAX);
225 for (i = 0; i < tkey.len; i++)
227 d = tkey.mem[i] - p->key.mem[i];
228 if (d || i == tkey.len-1)
229 { /* all have been equal until now, now make delta .. */
230 p->key.mem[i] = tkey.mem[i];
233 iscz1_encode_int (i + (tkey.len << 3) + 64, dst);
235 iscz1_encode_int (d, dst);
239 iscz1_encode_int (i + (tkey.len << 3), dst);
244 /* rest uses absolute encoding ... */
245 for (; i < tkey.len; i++)
247 iscz1_encode_int (tkey.mem[i], dst);
248 p->key.mem[i] = tkey.mem[i];
250 (*src) += sizeof(struct it_key);
253 void iscz1_decode (void *vp, char **dst, const char **src)
255 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
258 int leader = (int) iscz1_decode_int ((unsigned char **) src);
261 p->key.mem[i] += iscz1_decode_int ((unsigned char **) src);
263 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
264 p->key.len = (leader >> 3) & 7;
265 while (++i < p->key.len)
266 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
267 memcpy (*dst, &p->key, sizeof(struct it_key));
268 (*dst) += sizeof(struct it_key);
274 * indent-tabs-mode: nil
276 * vim: shiftwidth=4 tabstop=8 expandtab