Fixed bug #446: iso2709 filter SEGVs on bad input data.
[idzebra-moved-to-github.git] / rset / rsisamb.c
1 /* $Id: rsisamb.c,v 1.10.2.2 2005-01-14 14:32:25 adam Exp $
2    Copyright (C) 1995-2005
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
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
10 version.
11
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
15 for more details.
16
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
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <zebrautl.h>
26 #include <rsisamb.h>
27 #include <string.h>
28 #include <../index/index.h> /* for log_keydump. Debugging only */
29
30 #ifndef RSET_DEBUG
31 #define RSET_DEBUG 0
32 #endif
33
34 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
35 static RSFD r_open (RSET ct, int flag);
36 static void r_close (RSFD rfd);
37 static void r_delete (RSET ct);
38 static void r_rewind (RSFD rfd);
39 static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index,
40                      int (*cmpfunc)(const void *p1, const void *p2),
41                      const void *untilbuf);
42 static void r_pos (RSFD rfd, int *current, int *total);
43 static int r_read (RSFD rfd, void *buf, int *term_index);
44 static int r_write (RSFD rfd, const void *buf);
45
46 static const struct rset_control control = 
47 {
48     "isamb",
49     r_create,
50     r_open,
51     r_close,
52     r_delete,
53     r_rewind,
54     rset_default_forward,
55     r_pos,
56     r_read,
57     r_write,
58 };
59
60 static const struct rset_control control_forward = 
61 {
62     "isamb",
63     r_create,
64     r_open,
65     r_close,
66     r_delete,
67     r_rewind,
68     r_forward,
69     r_pos,
70     r_read,
71     r_write,
72 };
73
74 /* FIXME - using the default forward reads all items from the isam */
75 /* and thus makes the term counts work OK. On the other hand, it   */
76 /* negates the speedup from forwarding */
77
78 const struct rset_control *rset_kind_isamb = &control;
79 const struct rset_control *rset_kind_isamb_forward = &control_forward;
80
81 struct rset_pp_info {
82     ISAMB_PP pt;
83     struct rset_pp_info *next;
84     struct rset_isamb_info *info;
85     int *countp;
86     void *buf;
87 };
88
89 struct rset_isamb_info {
90     ISAMB   is;
91     ISAMB_P pos;
92     int key_size;
93     int (*cmp)(const void *p1, const void *p2);
94     struct rset_pp_info *ispt_list;
95 };
96
97 static void *r_create(RSET ct, const struct rset_control *sel, void *parms)
98 {
99     rset_isamb_parms *pt = (rset_isamb_parms *) parms;
100     struct rset_isamb_info *info;
101
102     ct->flags |= RSET_FLAG_VOLATILE;
103     info = (struct rset_isamb_info *) xmalloc (sizeof(*info));
104     info->is = pt->is;
105     info->pos = pt->pos;
106     info->key_size = pt->key_size;
107     info->cmp = pt->cmp;
108     info->ispt_list = NULL;
109     ct->no_rset_terms = 1;
110     ct->rset_terms = (RSET_TERM *) xmalloc (sizeof(*ct->rset_terms));
111     ct->rset_terms[0] = pt->rset_term;
112     return info;
113 }
114
115 RSFD r_open (RSET ct, int flag)
116 {
117     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->buf;
118     struct rset_pp_info *ptinfo;
119
120     logf (LOG_DEBUG, "risamb_open");
121     if (flag & RSETF_WRITE)
122     {
123         logf (LOG_FATAL, "ISAMB set type is read-only");
124         return NULL;
125     }
126     ptinfo = (struct rset_pp_info *) xmalloc (sizeof(*ptinfo));
127     ptinfo->next = info->ispt_list;
128     info->ispt_list = ptinfo;
129     ptinfo->pt = isamb_pp_open (info->is, info->pos);
130     ptinfo->info = info;
131     if (ct->rset_terms[0]->nn < 0)
132         ct->rset_terms[0]->nn = isamb_pp_num (ptinfo->pt);
133     ct->rset_terms[0]->count = 0;
134     ptinfo->countp = &ct->rset_terms[0]->count;
135     ptinfo->buf = xmalloc (info->key_size);
136     return ptinfo;
137 }
138
139 static void r_close (RSFD rfd)
140 {
141     struct rset_isamb_info *info = ((struct rset_pp_info*) rfd)->info;
142     struct rset_pp_info **ptinfop;
143
144     for (ptinfop = &info->ispt_list; *ptinfop; ptinfop = &(*ptinfop)->next)
145         if (*ptinfop == rfd)
146         {
147             xfree ((*ptinfop)->buf);
148             isamb_pp_close ((*ptinfop)->pt);
149             *ptinfop = (*ptinfop)->next;
150             xfree (rfd);
151             return;
152         }
153     logf (LOG_FATAL, "r_close but no rfd match!");
154     assert (0);
155 }
156
157 static void r_delete (RSET ct)
158 {
159     struct rset_isamb_info *info = (struct rset_isamb_info *) ct->buf;
160
161     logf (LOG_DEBUG, "rsisamb_delete");
162     assert (info->ispt_list == NULL);
163     rset_term_destroy (ct->rset_terms[0]);
164     xfree (ct->rset_terms);
165     xfree (info);
166 }
167
168 static void r_rewind (RSFD rfd)
169 {   
170     logf (LOG_DEBUG, "rsisamb_rewind");
171     abort ();
172 }
173
174 static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index,
175                      int (*cmpfunc)(const void *p1, const void *p2),
176                      const void *untilbuf)
177 {
178     int i; 
179     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
180 #if RSET_DEBUG
181     logf (LOG_DEBUG, "rset_rsisamb_forward starting '%s' (ct=%p rfd=%p)",
182                       ct->control->desc, ct,rfd);
183     key_logdump(LOG_DEBUG, untilbuf);
184     key_logdump(LOG_DEBUG, buf);
185 #endif
186
187     i=isamb_pp_forward(pinfo->pt, buf, untilbuf);
188 #if RSET_DEBUG
189     logf (LOG_DEBUG, "rset_rsisamb_forward returning %d",i);
190 #endif
191     return i;
192 }
193
194 static void r_pos (RSFD rfd, int *current, int *total)
195 {
196     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
197     assert(rfd);
198     isamb_pp_pos(pinfo->pt, current, total);
199 }
200
201 static int r_read (RSFD rfd, void *buf, int *term_index)
202 {
203     struct rset_pp_info *pinfo = (struct rset_pp_info *) rfd;
204     int r;
205     *term_index = 0;
206     r = isamb_pp_read(pinfo->pt, buf);
207     if (r > 0)
208     {
209         if (*pinfo->countp == 0 || (*pinfo->info->cmp)(buf, pinfo->buf) > 1)
210         {
211             memcpy (pinfo->buf, buf, pinfo->info->key_size);
212             (*pinfo->countp)++;
213         }
214     }
215     return r;
216 }
217
218 static int r_write (RSFD rfd, const void *buf)
219 {
220     logf (LOG_FATAL, "ISAMB set type is read-only");
221     return -1;
222 }