Fixed bug #446: iso2709 filter SEGVs on bad input data.
[idzebra-moved-to-github.git] / rset / rsbool.c
1 /* $Id: rsbool.c,v 1.32.2.1 2005-01-23 15:06:21 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include <rsbool.h>
29 #include <zebrautl.h>
30
31 #ifndef RSET_DEBUG
32 #define RSET_DEBUG 0
33 #endif
34
35 static void *r_create(RSET ct, const struct rset_control *sel, void *parms);
36 static RSFD r_open (RSET ct, int flag);
37 static void r_close (RSFD rfd);
38 static void r_delete (RSET ct);
39 static void r_rewind (RSFD rfd);
40 static int r_forward(RSET ct, RSFD rfd, void *buf, int *term_index,
41                      int (*cmpfunc)(const void *p1, const void *p2),
42                      const void *untilbuf);
43 /* static void r_pos (RSFD rfd, int *current, int *total);  */
44 static int r_read_and_forward (RSFD rfd, void *buf, int *term_index);
45 static int r_read_and (RSFD rfd, void *buf, int *term_index);
46 static int r_read_or (RSFD rfd, void *buf, int *term_index);
47 static int r_read_not (RSFD rfd, void *buf, int *term_index);
48 static int r_write (RSFD rfd, const void *buf);
49
50 static const struct rset_control control_and = 
51 {
52     "and",
53     r_create,
54     r_open,
55     r_close,
56     r_delete,
57     r_rewind,
58     r_forward,
59     rset_default_pos,
60     r_read_and,
61     r_write,
62 };
63
64 static const struct rset_control control_and_forward = 
65 {
66     "and",
67     r_create,
68     r_open,
69     r_close,
70     r_delete,
71     r_rewind,
72     r_forward,
73     rset_default_pos,
74     r_read_and_forward,
75     r_write,
76 };
77
78 static const struct rset_control control_or = 
79 {
80     "or",
81     r_create,
82     r_open,
83     r_close,
84     r_delete,
85     r_rewind,
86     r_forward, 
87     rset_default_pos,
88     r_read_or,
89     r_write,
90 };
91
92 static const struct rset_control control_not = 
93 {
94     "not",
95     r_create,
96     r_open,
97     r_close,
98     r_delete,
99     r_rewind,
100     r_forward, 
101     rset_default_pos,
102     r_read_not,
103     r_write,
104 };
105
106
107 const struct rset_control *rset_kind_and = &control_and;
108 const struct rset_control *rset_kind_and_forward = &control_and_forward;
109 const struct rset_control *rset_kind_or = &control_or;
110 const struct rset_control *rset_kind_not = &control_not;
111
112 struct rset_bool_info {
113     int key_size;
114     RSET rset_l;
115     RSET rset_r;
116     int term_index_s;
117     int (*cmp)(const void *p1, const void *p2);
118     void (*log_item)(int logmask, const void *p, const char *txt);
119     struct rset_bool_rfd *rfd_list;
120 };
121
122 struct rset_bool_rfd {
123     RSFD rfd_l;
124     RSFD rfd_r;
125     int  more_l;
126     int  more_r;
127     int term_index_l;
128     int term_index_r;
129     void *buf_l;
130     void *buf_r;
131     int tail;
132     struct rset_bool_rfd *next;
133     struct rset_bool_info *info;
134 };    
135
136 static void *r_create (RSET ct, const struct rset_control *sel, void *parms)
137 {
138     rset_bool_parms *bool_parms = (rset_bool_parms *) parms;
139     struct rset_bool_info *info;
140
141     info = (struct rset_bool_info *) xmalloc (sizeof(*info));
142     info->key_size = bool_parms->key_size;
143     info->rset_l = bool_parms->rset_l;
144     info->rset_r = bool_parms->rset_r;
145     if (rset_is_volatile(info->rset_l) || rset_is_volatile(info->rset_r))
146         ct->flags |= RSET_FLAG_VOLATILE;
147     info->cmp = bool_parms->cmp;
148     info->log_item = bool_parms->log_item;
149     info->rfd_list = NULL;
150     
151     info->term_index_s = info->rset_l->no_rset_terms;
152     ct->no_rset_terms =
153         info->rset_l->no_rset_terms + info->rset_r->no_rset_terms;
154     ct->rset_terms = (RSET_TERM *)
155         xmalloc (sizeof (*ct->rset_terms) * ct->no_rset_terms);
156
157     memcpy (ct->rset_terms, info->rset_l->rset_terms,
158             info->rset_l->no_rset_terms * sizeof(*ct->rset_terms));
159     memcpy (ct->rset_terms + info->rset_l->no_rset_terms,
160             info->rset_r->rset_terms,
161             info->rset_r->no_rset_terms * sizeof(*ct->rset_terms));
162     return info;
163 }
164
165 static RSFD r_open (RSET ct, int flag)
166 {
167     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
168     struct rset_bool_rfd *rfd;
169
170     if (flag & RSETF_WRITE)
171     {
172         logf (LOG_FATAL, "bool set type is read-only");
173         return NULL;
174     }
175     rfd = (struct rset_bool_rfd *) xmalloc (sizeof(*rfd));
176     logf(LOG_DEBUG,"rsbool (%s) open [%p]", ct->control->desc, rfd);
177     rfd->next = info->rfd_list;
178     info->rfd_list = rfd;
179     rfd->info = info;
180
181     rfd->buf_l = xmalloc (info->key_size);
182     rfd->buf_r = xmalloc (info->key_size);
183     rfd->rfd_l = rset_open (info->rset_l, RSETF_READ);
184     rfd->rfd_r = rset_open (info->rset_r, RSETF_READ);
185     rfd->more_l = rset_read (info->rset_l, rfd->rfd_l, rfd->buf_l,
186                              &rfd->term_index_l);
187     rfd->more_r = rset_read (info->rset_r, rfd->rfd_r, rfd->buf_r,
188                              &rfd->term_index_r);
189     rfd->tail = 0;
190     return rfd;
191 }
192
193 static void r_close (RSFD rfd)
194 {
195     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
196     struct rset_bool_rfd **rfdp;
197     
198     for (rfdp = &info->rfd_list; *rfdp; rfdp = &(*rfdp)->next)
199         if (*rfdp == rfd)
200         {
201             xfree ((*rfdp)->buf_l);
202             xfree ((*rfdp)->buf_r);
203             rset_close (info->rset_l, (*rfdp)->rfd_l);
204             rset_close (info->rset_r, (*rfdp)->rfd_r);
205             *rfdp = (*rfdp)->next;
206             xfree (rfd);
207             return;
208         }
209     logf (LOG_FATAL, "r_close but no rfd match!");
210     assert (0);
211 }
212
213 static void r_delete (RSET ct)
214 {
215     struct rset_bool_info *info = (struct rset_bool_info *) ct->buf;
216
217     assert (info->rfd_list == NULL);
218     xfree (ct->rset_terms);
219     rset_delete (info->rset_l);
220     rset_delete (info->rset_r);
221     xfree (info);
222 }
223
224 static void r_rewind (RSFD rfd)
225 {
226     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
227     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
228
229     logf (LOG_DEBUG, "rsbool_rewind");
230     rset_rewind (info->rset_l, p->rfd_l);
231     rset_rewind (info->rset_r, p->rfd_r);
232     p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l, &p->term_index_l);
233     p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r, &p->term_index_r);
234 }
235
236 static int r_forward (RSET ct, RSFD rfd, void *buf, int *term_index,
237                      int (*cmpfunc)(const void *p1, const void *p2),
238                      const void *untilbuf)
239 {
240     struct rset_bool_info *info = ((struct rset_bool_rfd*)rfd)->info;
241     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
242     int rc;
243
244 #if RSET_DEBUG
245     logf (LOG_DEBUG, "rsbool_forward (L) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
246                       rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
247 #endif
248     if ( p->more_l && ((cmpfunc)(untilbuf,p->buf_l)==2) )
249         p->more_l = rset_forward(info->rset_l, p->rfd_l, p->buf_l,
250                         &p->term_index_l, info->cmp, untilbuf);
251 #if RSET_DEBUG
252     logf (LOG_DEBUG, "rsbool_forward (R) [%p] '%s' (ct=%p rfd=%p m=%d,%d)",
253                       rfd, ct->control->desc, ct, rfd, p->more_l, p->more_r);
254 #endif
255     if ( p->more_r && ((cmpfunc)(untilbuf,p->buf_r)==2))
256         p->more_r = rset_forward(info->rset_r, p->rfd_r, p->buf_r,
257                         &p->term_index_r, info->cmp, untilbuf);
258 #if RSET_DEBUG
259     logf (LOG_DEBUG, "rsbool_forward [%p] calling read, m=%d,%d t=%d", 
260                        rfd, p->more_l, p->more_r, p->tail);
261 #endif
262     
263     p->tail=0; 
264     rc = rset_read(ct,rfd,buf,term_index); 
265 #if RSET_DEBUG
266     logf (LOG_DEBUG, "rsbool_forward returning [%p] %d m=%d,%d", 
267                        rfd, rc, p->more_l, p->more_r);
268 #endif
269     return rc;
270 }
271
272
273 /*
274     1,1         1,3
275     1,9         2,1
276     1,11        3,1
277     2,9
278
279   1,1     1,1
280   1,3     1,3
281           1,9
282           1,11
283   2,1     2,1
284           2,9
285           3,1
286 */
287
288 static int r_read_and (RSFD rfd, void *buf, int *term_index)
289 {
290     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
291     struct rset_bool_info *info = p->info;
292
293     while (p->more_l || p->more_r)
294     {
295         int cmp;
296
297         if (p->more_l && p->more_r)
298             cmp = (*info->cmp)(p->buf_l, p->buf_r);
299         else if (p->more_l)
300             cmp = -2;
301         else
302             cmp = 2;
303 #if RSET_DEBUG
304         logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
305                         rfd, p->more_l, p->more_r, cmp, p->tail);
306         (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
307         (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
308 #endif
309         if (!cmp)
310         {
311             memcpy (buf, p->buf_l, info->key_size);
312                 *term_index = p->term_index_l;
313             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
314                                    &p->term_index_l);
315             p->tail = 1;
316         }
317         else if (cmp == 1)
318         {
319             memcpy (buf, p->buf_r, info->key_size);
320                 *term_index = p->term_index_r + info->term_index_s;
321             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
322                                    &p->term_index_r);
323             p->tail = 1;
324 #if RSET_DEBUG
325             logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
326                     rfd, p->more_l, p->more_r, cmp);
327             key_logdump(LOG_DEBUG,buf);
328             (*info->log_item)(LOG_DEBUG, buf, "");
329 #endif
330             return 1;
331         }
332         else if (cmp == -1)
333         {
334             memcpy (buf, p->buf_l, info->key_size);
335                 *term_index = p->term_index_l;
336             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
337                                    &p->term_index_l);
338             p->tail = 1;
339 #if RSET_DEBUG
340             logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
341                     rfd, p->more_l, p->more_r, cmp);
342             (*info->log_item)(LOG_DEBUG, buf, "");
343 #endif
344             return 1;
345         }
346         else if (cmp > 1)  /* cmp == 2 */
347         {
348             memcpy (buf, p->buf_r, info->key_size);
349             *term_index = p->term_index_r + info->term_index_s;
350             
351             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
352                                    &p->term_index_r);
353             if (p->tail)
354             {
355                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
356                     p->tail = 0;
357 #if RSET_DEBUG
358                 logf (LOG_DEBUG, "r_read_and returning C m=%d/%d c=%d",
359                         p->more_l, p->more_r, cmp);
360                 (*info->log_item)(LOG_DEBUG, buf, "");
361 #endif
362                 return 1;
363             }
364         }
365         else  /* cmp == -2 */
366         {
367              memcpy (buf, p->buf_l, info->key_size);
368              *term_index = p->term_index_l;
369              p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
370                                     &p->term_index_l);
371              if (p->tail)
372              {
373                  if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
374                      p->tail = 0;
375 #if RSET_DEBUG
376                  logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
377                         rfd, p->more_l, p->more_r, cmp);
378                  (*info->log_item)(LOG_DEBUG, buf, "");
379 #endif
380                  return 1;
381              }
382         }
383     }
384 #if RSET_DEBUG
385     logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
386 #endif
387     return 0;
388 }
389
390 static int r_read_and_forward (RSFD rfd, void *buf, int *term_index)
391 {
392     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
393     struct rset_bool_info *info = p->info;
394
395     while (p->more_l || p->more_r)
396     {
397         int cmp;
398
399         if (p->more_l && p->more_r)
400             cmp = (*info->cmp)(p->buf_l, p->buf_r);
401         else if (p->more_l)
402             cmp = -2;
403         else
404             cmp = 2;
405 #if RSET_DEBUG
406         logf (LOG_DEBUG, "r_read_and [%p] looping: m=%d/%d c=%d t=%d",
407                         rfd, p->more_l, p->more_r, cmp, p->tail);
408         (*info->log_item)(LOG_DEBUG, p->buf_l, "left ");
409         (*info->log_item)(LOG_DEBUG, p->buf_r, "right ");
410 #endif
411         if (!cmp)
412         {
413             memcpy (buf, p->buf_l, info->key_size);
414                 *term_index = p->term_index_l;
415             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
416                                    &p->term_index_l);
417             p->tail = 1;
418         }
419         else if (cmp == 1)
420         {
421             memcpy (buf, p->buf_r, info->key_size);
422                 *term_index = p->term_index_r + info->term_index_s;
423             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
424                                    &p->term_index_r);
425             p->tail = 1;
426 #if RSET_DEBUG
427             logf (LOG_DEBUG, "r_read_and [%p] returning R m=%d/%d c=%d",
428                     rfd, p->more_l, p->more_r, cmp);
429             key_logdump(LOG_DEBUG,buf);
430             (*info->log_item)(LOG_DEBUG, buf, "");
431 #endif
432             return 1;
433         }
434         else if (cmp == -1)
435         {
436             memcpy (buf, p->buf_l, info->key_size);
437                 *term_index = p->term_index_l;
438             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
439                                    &p->term_index_l);
440             p->tail = 1;
441 #if RSET_DEBUG
442             logf (LOG_DEBUG, "r_read_and [%p] returning L m=%d/%d c=%d",
443                     rfd, p->more_l, p->more_r, cmp);
444             (*info->log_item)(LOG_DEBUG, buf, "");
445 #endif
446             return 1;
447         }
448         else if (cmp > 1)  /* cmp == 2 */
449         {
450             if (p->tail)
451             {
452                 memcpy (buf, p->buf_r, info->key_size);
453                 *term_index = p->term_index_r + info->term_index_s;
454                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
455                                    &p->term_index_r);
456                 if (!p->more_r || (*info->cmp)(p->buf_r, buf) > 1)
457                     p->tail = 0;
458 #if RSET_DEBUG
459                 logf (LOG_DEBUG, "r_read_and [%p] returning R tail m=%d/%d c=%d",
460                         rfd, p->more_l, p->more_r, cmp);
461                 (*info->log_item)(LOG_DEBUG, buf, "");
462 #endif
463                 return 1;
464             }
465             else
466             {
467 #if RSET_DEBUG
468                 logf (LOG_DEBUG, "r_read_and [%p] about to forward R m=%d/%d c=%d",
469                         rfd, p->more_l, p->more_r, cmp);
470 #endif
471                 if (p->more_r && p->more_l)
472                     p->more_r = rset_forward( 
473                                     info->rset_r, p->rfd_r, 
474                                     p->buf_r, &p->term_index_r, 
475                                     (info->cmp), p->buf_l);
476                 else 
477                 {
478                     return 0; /* no point in reading further */
479                 }
480             }
481         }
482         else  /* cmp == -2 */
483         {
484             if (p->tail)
485             {
486                 memcpy (buf, p->buf_l, info->key_size);
487                     *term_index = p->term_index_l;
488                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
489                                    &p->term_index_l);
490                 if (!p->more_l || (*info->cmp)(p->buf_l, buf) > 1)
491                     p->tail = 0;
492 #if RSET_DEBUG
493                 logf (LOG_DEBUG, "r_read_and [%p] returning L tail m=%d/%d c=%d",
494                         rfd, p->more_l, p->more_r, cmp);
495                 (*info->log_item)(LOG_DEBUG, buf, "");
496 #endif
497                 return 1;
498             }
499             else
500             {
501 #if RSET_DEBUG
502                 logf (LOG_DEBUG, "r_read_and [%p] about to forward L m=%d/%d c=%d",
503                         rfd, p->more_l, p->more_r, cmp);
504 #endif
505                 if (p->more_r && p->more_l)
506                     p->more_l = rset_forward( 
507                     /* p->more_l = rset_default_forward( */
508                                     info->rset_l, p->rfd_l, 
509                                     p->buf_l, &p->term_index_l, 
510                                     (info->cmp), p->buf_r);
511                 else 
512                 {
513                     return 0; /* no point in reading further */
514                 }
515             }
516         }
517     }
518 #if RSET_DEBUG
519     logf (LOG_DEBUG, "r_read_and [%p] reached its end",rfd);
520 #endif
521     return 0;
522 }
523
524 static int r_read_or (RSFD rfd, void *buf, int *term_index)
525 {
526     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
527     struct rset_bool_info *info = p->info;
528
529     while (p->more_l || p->more_r)
530     {
531         int cmp;
532
533         if (p->more_l && p->more_r)
534             cmp = (*info->cmp)(p->buf_l, p->buf_r);
535         else if (p->more_r)
536             cmp = 2;
537         else
538             cmp = -2;
539         if (!cmp)
540         {
541             memcpy (buf, p->buf_l, info->key_size);
542                 *term_index = p->term_index_l;
543             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
544                                    &p->term_index_l);
545             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
546                                    &p->term_index_r);
547 #if RSET_DEBUG
548             logf (LOG_DEBUG, "r_read_or returning A m=%d/%d c=%d",
549                     p->more_l, p->more_r, cmp);
550             (*info->log_item)(LOG_DEBUG, buf, "");
551 #endif
552             return 1;
553         }
554         else if (cmp > 0)
555         {
556             memcpy (buf, p->buf_r, info->key_size);
557                 *term_index = p->term_index_r + info->term_index_s;
558             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
559                                    &p->term_index_r);
560 #if RSET_DEBUG
561             logf (LOG_DEBUG, "r_read_or returning B m=%d/%d c=%d",
562                     p->more_l, p->more_r, cmp);
563             (*info->log_item)(LOG_DEBUG, buf, "");
564 #endif
565             return 1;
566         }
567         else
568         {
569             memcpy (buf, p->buf_l, info->key_size);
570                 *term_index = p->term_index_l;
571             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
572                                    &p->term_index_l);
573 #if RSET_DEBUG
574             logf (LOG_DEBUG, "r_read_or returning C m=%d/%d c=%d",
575                     p->more_l, p->more_r, cmp);
576             (*info->log_item)(LOG_DEBUG, buf, "");
577 #endif
578             return 1;
579         }
580     }
581     return 0;
582 }
583
584 static int r_read_not (RSFD rfd, void *buf, int *term_index)
585 {
586     struct rset_bool_rfd *p = (struct rset_bool_rfd *) rfd;
587     struct rset_bool_info *info = p->info;
588
589     while (p->more_l || p->more_r)
590     {
591         int cmp;
592
593         if (p->more_l && p->more_r)
594             cmp = (*info->cmp)(p->buf_l, p->buf_r);
595         else if (p->more_r)
596             cmp = 2;
597         else
598             cmp = -2;
599         if (cmp < -1)
600         {
601             memcpy (buf, p->buf_l, info->key_size);
602                 *term_index = p->term_index_l;
603             p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
604                                    &p->term_index_l);
605             return 1;
606         }
607         else if (cmp > 1)
608         {
609 #if 0
610             p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
611                                    &p->term_index_r);
612 #else
613             p->more_r = rset_forward( 
614                 info->rset_r, p->rfd_r, 
615                 p->buf_r, &p->term_index_r, 
616                 (info->cmp), p->buf_l);
617 #endif
618         }
619         else
620         {
621             memcpy (buf, p->buf_l, info->key_size);
622             do
623             { 
624                 p->more_l = rset_read (info->rset_l, p->rfd_l, p->buf_l,
625                                        &p->term_index_l);
626                 if (!p->more_l)
627                     break;
628                 cmp = (*info->cmp)(p->buf_l, buf);
629             } while (cmp >= -1 && cmp <= 1);
630             do
631             {
632                 p->more_r = rset_read (info->rset_r, p->rfd_r, p->buf_r,
633                                        &p->term_index_r);
634                 if (!p->more_r)
635                     break;
636                 cmp = (*info->cmp)(p->buf_r, buf);
637             } while (cmp >= -1 && cmp <= 1);
638         }
639     }
640     return 0;
641 }
642
643
644 static int r_write (RSFD rfd, const void *buf)
645 {
646     logf (LOG_FATAL, "bool set type is read-only");
647     return -1;
648 }
649