1 /* This file is part of the Zebra server.
2 Copyright (C) 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
28 #include <yaz/xmalloc.h>
32 #define CODEC_INLINE inline
37 void key_logdump_txt(int logmask, const void *p, const char *txt)
47 memcpy (&key, p, sizeof(key));
48 assert(key.len > 0 && key.len <= IT_KEY_LEVEL_MAX);
50 for (i = 0; i<key.len; i++)
54 sprintf(formstr + strlen(formstr), ZINT_FORMAT, key.mem[i]);
56 yaz_log(logmask, "%s %s", formstr, txt);
59 yaz_log(logmask, " (no key) %s",txt);
62 void key_logdump(int logmask, const void *p)
64 key_logdump_txt(logmask, p, "");
67 char *key_print_it (const void *p, char *buf)
73 int key_compare (const void *p1, const void *p2)
77 memcpy (&i1, p1, sizeof(i1));
78 memcpy (&i2, p2, sizeof(i2));
82 assert (l <= IT_KEY_LEVEL_MAX && l > 0);
83 for (i = 0; i < l; i++)
85 if (i1.mem[i] != i2.mem[i])
87 if (i1.mem[i] > i2.mem[i])
96 zint key_get_seq(const void *p)
99 memcpy (&k, p, sizeof(k));
100 return k.mem[k.len-1];
103 zint key_get_segment(const void *p)
106 memcpy (&k, p, sizeof(k));
107 return k.mem[k.len-2];
110 int key_qsort_compare (const void *p1, const void *p2)
114 char *cp1 = *(char **) p1;
115 char *cp2 = *(char **) p2;
117 if ((r = strcmp (cp1, cp2)))
120 if ((r = key_compare (cp1+l+1, cp2+l+1)))
122 return cp1[l] - cp2[l];
125 struct iscz1_code_info {
129 void *iscz1_start (void)
131 struct iscz1_code_info *p = (struct iscz1_code_info *)
132 xmalloc (sizeof(*p));
137 void key_init(struct it_key *key)
141 for (i = 0; i < IT_KEY_LEVEL_MAX; i++)
145 void iscz1_reset (void *vp)
147 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
150 for (i = 0; i < IT_KEY_LEVEL_MAX; i++)
154 void iscz1_stop (void *p)
159 /* small encoder that works with unsigneds of any length */
160 static CODEC_INLINE void iscz1_encode_int (zint d, char **dst)
162 unsigned char *bp = (unsigned char*) *dst;
166 *bp++ = (unsigned) (128 | (d & 127));
169 *bp++ = (unsigned) d;
173 /* small decoder that works with unsigneds of any length */
174 static CODEC_INLINE zint iscz1_decode_int (unsigned char **src)
180 while (((c = *(*src)++) & 128))
182 d += ((zint) (c&127) << r);
185 d += ((zint) c << r);
189 void iscz1_encode (void *vp, char **dst, const char **src)
191 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
201 if diff is 0, then there is more ...
202 if diff is non-zero, then _may_ be more
204 memcpy (&tkey, *src, sizeof(struct it_key));
206 /* deal with leader + delta encoding .. */
208 assert(tkey.len > 0 && tkey.len <= IT_KEY_LEVEL_MAX);
209 for (i = 0; i < tkey.len; i++)
211 d = tkey.mem[i] - p->key.mem[i];
212 if (d || i == tkey.len-1)
213 { /* all have been equal until now, now make delta .. */
214 p->key.mem[i] = tkey.mem[i];
217 iscz1_encode_int (i + (tkey.len << 3) + 64, dst);
219 iscz1_encode_int (d, dst);
223 iscz1_encode_int (i + (tkey.len << 3), dst);
228 /* rest uses absolute encoding ... */
229 for (; i < tkey.len; i++)
231 iscz1_encode_int (tkey.mem[i], dst);
232 p->key.mem[i] = tkey.mem[i];
234 (*src) += sizeof(struct it_key);
237 void iscz1_decode (void *vp, char **dst, const char **src)
239 struct iscz1_code_info *p = (struct iscz1_code_info *) vp;
242 int leader = (int) iscz1_decode_int ((unsigned char **) src);
245 p->key.mem[i] += iscz1_decode_int ((unsigned char **) src);
247 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
248 p->key.len = (leader >> 3) & 7;
249 while (++i < p->key.len)
250 p->key.mem[i] = iscz1_decode_int ((unsigned char **) src);
251 memcpy (*dst, &p->key, sizeof(struct it_key));
252 (*dst) += sizeof(struct it_key);
258 * c-file-style: "Stroustrup"
259 * indent-tabs-mode: nil
261 * vim: shiftwidth=4 tabstop=8 expandtab