1 /* $Id: reckeys.c,v 1.4 2006-05-10 08:13:22 adam Exp $
2 Copyright (C) 1995-2005
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
32 struct zebra_rec_key_entry {
36 struct zebra_rec_key_entry *next;
39 struct zebra_rec_keys_t_ {
50 struct zebra_rec_key_entry **entries;
54 struct zebra_rec_key_entry **zebra_rec_keys_mk_hash(zebra_rec_keys_t p,
60 for (i = 0; i<len; i++)
61 h = h * 65509 + buf[i];
62 return &p->entries[h % (unsigned) p->hash_size];
65 static void init_hash(zebra_rec_keys_t p)
72 p->entries = nmem_malloc(p->nmem, p->hash_size * sizeof(*p->entries));
73 for (i = 0; i<p->hash_size; i++)
78 zebra_rec_keys_t zebra_rec_keys_open()
80 zebra_rec_keys_t p = xmalloc(sizeof(*p));
85 p->owner_of_buffer = 1;
86 p->encode_handle = iscz1_start();
87 p->decode_handle = iscz1_start();
89 p->nmem = nmem_create();
98 void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz,
101 if (p->owner_of_buffer)
115 p->buf = xmalloc(sz);
116 memcpy(p->buf, buf, sz);
119 p->owner_of_buffer = copy_buf;
122 void zebra_rec_keys_get_buf(zebra_rec_keys_t p, char **buf, size_t *sz)
132 void zebra_rec_keys_close(zebra_rec_keys_t p)
137 if (p->owner_of_buffer)
139 if (p->encode_handle)
140 iscz1_stop(p->encode_handle);
141 if (p->decode_handle)
142 iscz1_stop(p->decode_handle);
143 nmem_destroy(p->nmem);
147 int zebra_rec_keys_add_hash(zebra_rec_keys_t keys,
148 const char *str, size_t slen,
149 const struct it_key *key)
151 struct zebra_rec_key_entry **kep = zebra_rec_keys_mk_hash(keys, str, slen);
154 struct zebra_rec_key_entry *e = *kep;
155 if (slen == e->len && !memcmp(str, e->buf, slen) &&
156 !key_compare(key, &e->key))
162 *kep = nmem_malloc(keys->nmem, sizeof(**kep));
165 memcpy(&(*kep)->key, key, sizeof(*key));
166 (*kep)->buf = nmem_malloc(keys->nmem, slen);
167 memcpy((*kep)->buf, str, slen);
171 void zebra_rec_keys_write(zebra_rec_keys_t keys,
172 const char *str, size_t slen,
173 const struct it_key *key)
176 const char *src = (char*) key;
178 assert(keys->owner_of_buffer);
180 if (!zebra_rec_keys_add_hash(keys, str, slen, key))
181 return; /* key already there . Omit it */
182 if (keys->buf_used+1024 > keys->buf_max)
184 char *b = (char *) xmalloc (keys->buf_max += 128000);
185 if (keys->buf_used > 0)
186 memcpy (b, keys->buf, keys->buf_used);
190 dst = keys->buf + keys->buf_used;
192 iscz1_encode(keys->encode_handle, &dst, &src);
194 memcpy (dst, str, slen);
197 keys->buf_used = dst - keys->buf;
200 void zebra_rec_keys_reset(zebra_rec_keys_t keys)
205 iscz1_reset(keys->encode_handle);
211 int zebra_rec_keys_rewind(zebra_rec_keys_t keys)
214 iscz1_reset(keys->decode_handle);
215 keys->fetch_offset = 0;
216 if (keys->buf_used == 0)
221 int zebra_rec_keys_empty(zebra_rec_keys_t keys)
223 if (keys->buf_used == 0)
228 int zebra_rec_keys_read(zebra_rec_keys_t keys,
229 const char **str, size_t *slen,
233 if (keys->fetch_offset == keys->buf_used)
237 const char *src = keys->buf + keys->fetch_offset;
238 char *dst = (char*) key;
240 assert (keys->fetch_offset < keys->buf_used);
242 /* store the destination key */
243 iscz1_decode(keys->decode_handle, &dst, &src);
245 /* store pointer to string and length of it */
250 keys->fetch_offset = src - keys->buf;
257 * indent-tabs-mode: nil
259 * vim: shiftwidth=4 tabstop=8 expandtab