1 /* $Id: reckeys.c,v 1.7 2006-08-14 10:40:15 adam Exp $
2 Copyright (C) 1995-2006
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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
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,
57 const struct it_key *key)
62 for (i = 0; i<len; i++)
63 h = h * 65509 + buf[i];
64 for (j = 0; j<key->len; j++)
65 h = h * 65509 + CAST_ZINT_TO_INT(key->mem[j]);
66 return &p->entries[h % (unsigned) p->hash_size];
69 static void init_hash(zebra_rec_keys_t p)
76 p->entries = nmem_malloc(p->nmem, p->hash_size * sizeof(*p->entries));
77 for (i = 0; i<p->hash_size; i++)
82 zebra_rec_keys_t zebra_rec_keys_open()
84 zebra_rec_keys_t p = xmalloc(sizeof(*p));
89 p->owner_of_buffer = 1;
90 p->encode_handle = iscz1_start();
91 p->decode_handle = iscz1_start();
93 p->nmem = nmem_create();
102 void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz,
105 if (p->owner_of_buffer)
119 p->buf = xmalloc(sz);
120 memcpy(p->buf, buf, sz);
123 p->owner_of_buffer = copy_buf;
126 void zebra_rec_keys_get_buf(zebra_rec_keys_t p, char **buf, size_t *sz)
136 void zebra_rec_keys_close(zebra_rec_keys_t p)
141 if (p->owner_of_buffer)
143 if (p->encode_handle)
144 iscz1_stop(p->encode_handle);
145 if (p->decode_handle)
146 iscz1_stop(p->decode_handle);
147 nmem_destroy(p->nmem);
151 int zebra_rec_keys_add_hash(zebra_rec_keys_t keys,
152 const char *str, size_t slen,
153 const struct it_key *key)
155 struct zebra_rec_key_entry **kep = zebra_rec_keys_mk_hash(keys,
159 struct zebra_rec_key_entry *e = *kep;
160 if (slen == e->len && !memcmp(str, e->buf, slen) &&
161 !key_compare(key, &e->key))
167 *kep = nmem_malloc(keys->nmem, sizeof(**kep));
170 memcpy(&(*kep)->key, key, sizeof(*key));
171 (*kep)->buf = nmem_malloc(keys->nmem, slen);
172 memcpy((*kep)->buf, str, slen);
176 void zebra_rec_keys_write(zebra_rec_keys_t keys,
177 const char *str, size_t slen,
178 const struct it_key *key)
181 const char *src = (char*) key;
183 assert(keys->owner_of_buffer);
185 if (!zebra_rec_keys_add_hash(keys, str, slen, key))
186 return; /* key already there . Omit it */
187 if (keys->buf_used+1024 > keys->buf_max)
189 char *b = (char *) xmalloc (keys->buf_max += 128000);
190 if (keys->buf_used > 0)
191 memcpy (b, keys->buf, keys->buf_used);
195 dst = keys->buf + keys->buf_used;
197 iscz1_encode(keys->encode_handle, &dst, &src);
199 memcpy (dst, str, slen);
202 keys->buf_used = dst - keys->buf;
205 void zebra_rec_keys_reset(zebra_rec_keys_t keys)
210 iscz1_reset(keys->encode_handle);
216 int zebra_rec_keys_rewind(zebra_rec_keys_t keys)
219 iscz1_reset(keys->decode_handle);
220 keys->fetch_offset = 0;
221 if (keys->buf_used == 0)
226 int zebra_rec_keys_empty(zebra_rec_keys_t keys)
228 if (keys->buf_used == 0)
233 int zebra_rec_keys_read(zebra_rec_keys_t keys,
234 const char **str, size_t *slen,
238 if (keys->fetch_offset == keys->buf_used)
242 const char *src = keys->buf + keys->fetch_offset;
243 char *dst = (char*) key;
245 assert (keys->fetch_offset < keys->buf_used);
247 /* store the destination key */
248 iscz1_decode(keys->decode_handle, &dst, &src);
250 /* store pointer to string and length of it */
255 keys->fetch_offset = src - keys->buf;
262 * indent-tabs-mode: nil
264 * vim: shiftwidth=4 tabstop=8 expandtab