Bug fix: reading bad tag units could crash the library (espec).
[yaz-moved-to-github.git] / retrieval / d1_espec.c
1 /*
2  * Copyright (c) 1995-2001, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: d1_espec.c,v 1.20 2001-09-30 20:15:49 adam Exp $
7  */
8
9 #include <stdlib.h>
10 #include <assert.h>
11 #include <string.h>
12
13 #include <yaz/odr.h>
14 #include <yaz/proto.h>
15 #include <yaz/log.h>
16 #include <yaz/data1.h>
17
18 static Z_Variant *read_variant(int argc, char **argv, NMEM nmem,
19                                const char *file, int lineno)
20 {
21     Z_Variant *r = (Z_Variant *)nmem_malloc(nmem, sizeof(*r));
22     oident var1;
23     int i;
24     int oid[OID_SIZE];
25
26     var1.proto = PROTO_Z3950;
27     var1.oclass = CLASS_VARSET;
28     var1.value = VAL_VAR1;
29     r->globalVariantSetId = odr_oiddup_nmem(nmem, oid_ent_to_oid(&var1, oid));
30
31     if (argc)
32         r->triples = (Z_Triple **)nmem_malloc(nmem, sizeof(Z_Triple*) * argc);
33     else
34         r->triples = 0;
35     r->num_triples = argc;
36     for (i = 0; i < argc; i++)
37     {
38         int zclass, type;
39         char value[512];
40         Z_Triple *t;
41
42         if (sscanf(argv[i], "(%d,%d,%[^)])", &zclass, &type, value) < 3)
43         {
44             yaz_log(LOG_WARN, "%s:%d: Syntax error in variant component '%s'",
45                     file, lineno, argv[i]);
46             return 0;
47         }
48         t = r->triples[i] = (Z_Triple *)nmem_malloc(nmem, sizeof(Z_Triple));
49         t->variantSetId = 0;
50         t->zclass = (int *)nmem_malloc(nmem, sizeof(int));
51         *t->zclass = zclass;
52         t->type = (int *)nmem_malloc(nmem, sizeof(int));
53         *t->type = type;
54         /*
55          * This is wrong.. we gotta look up the correct type for the
56          * variant, I guess... damn this stuff.
57          */
58         if (*value == '@')
59         {
60             t->which = Z_Triple_null;
61             t->value.null = odr_nullval();
62         }
63         else if (d1_isdigit(*value))
64         {
65             t->which = Z_Triple_integer;
66             t->value.integer = (int *)
67                 nmem_malloc(nmem, sizeof(*t->value.integer));
68             *t->value.integer = atoi(value);
69         }
70         else
71         {
72             t->which = Z_Triple_internationalString;
73             t->value.internationalString = (char *)
74                 nmem_malloc(nmem, strlen(value)+1);
75             strcpy(t->value.internationalString, value);
76         }
77     }
78     return r;
79 }
80
81 static Z_Occurrences *read_occurrences(char *occ, NMEM nmem,
82                                        const char *file, int lineno)
83 {
84     Z_Occurrences *op = (Z_Occurrences *)nmem_malloc(nmem, sizeof(*op));
85     char *p;
86     
87     if (!occ)
88     {
89         op->which = Z_Occurrences_values;
90         op->u.values = (Z_OccurValues *)
91             nmem_malloc(nmem, sizeof(Z_OccurValues));
92         op->u.values->start = (int *)nmem_malloc(nmem, sizeof(int));
93         *op->u.values->start = 1;
94         op->u.values->howMany = 0;
95     }
96     else if (!strcmp(occ, "all"))
97     {
98         op->which = Z_Occurrences_all;
99         op->u.all = odr_nullval();
100     }
101     else if (!strcmp(occ, "last"))
102     {
103         op->which = Z_Occurrences_last;
104         op->u.all = odr_nullval();
105     }
106     else
107     {
108         Z_OccurValues *ov = (Z_OccurValues *)nmem_malloc(nmem, sizeof(*ov));
109     
110         if (!d1_isdigit(*occ))
111         {
112             yaz_log(LOG_WARN, "%s:%d: Bad occurrences-spec %s",
113                     file, lineno, occ);
114             return 0;
115         }
116         op->which = Z_Occurrences_values;
117         op->u.values = ov;
118         ov->start = (int *)nmem_malloc(nmem, sizeof(*ov->start));
119         *ov->start = atoi(occ);
120         if ((p = strchr(occ, '+')))
121         {
122             ov->howMany = (int *)nmem_malloc(nmem, sizeof(*ov->howMany));
123             *ov->howMany = atoi(p + 1);
124         }
125         else
126             ov->howMany = 0;
127     }
128     return op;
129 }
130
131
132 static Z_ETagUnit *read_tagunit(char *buf, NMEM nmem,
133                                 const char *file, int lineno)
134 {
135     Z_ETagUnit *u = (Z_ETagUnit *)nmem_malloc(nmem, sizeof(*u));
136     int terms;
137     int type;
138     char value[512], occ[512];
139     
140     if (*buf == '*')
141     {
142         u->which = Z_ETagUnit_wildPath;
143         u->u.wildPath = odr_nullval();
144     }
145     else if (*buf == '?')
146     {
147         u->which = Z_ETagUnit_wildThing;
148         if (buf[1] == ':')
149             u->u.wildThing = read_occurrences(buf+2, nmem, file, lineno);
150         else
151             u->u.wildThing = read_occurrences(0, nmem, file, lineno);
152     }
153     else if ((terms = sscanf(buf, "(%d,%[^)]):%[a-zA-Z0-9+]", &type, value,
154                              occ)) >= 2)
155     {
156         int numval;
157         Z_SpecificTag *t;
158         char *valp = value;
159         int force_string = 0;
160         
161         if (*valp == '\'')
162         {
163             valp++;
164             force_string = 1;
165         }
166         u->which = Z_ETagUnit_specificTag;
167         u->u.specificTag = t = (Z_SpecificTag *)nmem_malloc(nmem, sizeof(*t));
168         t->tagType = (int *)nmem_malloc(nmem, sizeof(*t->tagType));
169         *t->tagType = type;
170         t->tagValue = (Z_StringOrNumeric *)
171             nmem_malloc(nmem, sizeof(*t->tagValue));
172         if (!force_string && (numval = atoi(valp)))
173         {
174             t->tagValue->which = Z_StringOrNumeric_numeric;
175             t->tagValue->u.numeric = (int *)nmem_malloc(nmem, sizeof(int));
176             *t->tagValue->u.numeric = numval;
177         }
178         else
179         {
180             t->tagValue->which = Z_StringOrNumeric_string;
181             t->tagValue->u.string = (char *)nmem_malloc(nmem, strlen(valp)+1);
182             strcpy(t->tagValue->u.string, valp);
183         }
184         if (terms > 2) /* an occurrences-spec exists */
185             t->occurrences = read_occurrences(occ, nmem, file, lineno);
186         else
187             t->occurrences = 0;
188     }
189     return u;
190 }
191
192 /*
193  * Read an element-set specification from a file.
194  * NOTE: If !o, memory is allocated directly from the heap by nmem_malloc().
195  */
196 Z_Espec1 *data1_read_espec1 (data1_handle dh, const char *file)
197 {
198     FILE *f;
199     NMEM nmem = data1_nmem_get (dh);
200     int lineno = 0;
201     int argc, size_esn = 0;
202     char *argv[50], line[512];
203     Z_Espec1 *res = (Z_Espec1 *)nmem_malloc(nmem, sizeof(*res));
204     
205     if (!(f = yaz_path_fopen(data1_get_tabpath(dh), file, "r")))
206     {
207         yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
208         return 0;
209     }
210     
211     res->num_elementSetNames = 0;
212     res->elementSetNames = 0;
213     res->defaultVariantSetId = 0;
214     res->defaultVariantRequest = 0;
215     res->defaultTagType = 0;
216     res->num_elements = 0;
217     res->elements = 0;
218     
219     while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
220         if (!strcmp(argv[0], "elementsetnames"))
221         {
222             int nnames = argc-1, i;
223             
224             if (!nnames)
225             {
226                 yaz_log(LOG_WARN, "%s:%d: Empty elementsetnames directive",
227                         file, lineno);
228                 continue;
229             }
230             
231             res->elementSetNames =
232                 (char **)nmem_malloc(nmem, sizeof(char**)*nnames);
233             for (i = 0; i < nnames; i++)
234             {
235                 res->elementSetNames[i] = (char *)
236                     nmem_malloc(nmem, strlen(argv[i+1])+1);
237                 strcpy(res->elementSetNames[i], argv[i+1]);
238             }
239             res->num_elementSetNames = nnames;
240         }
241         else if (!strcmp(argv[0], "defaultvariantsetid"))
242         {
243             if (argc != 2)
244             {
245                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
246                         file, lineno, argv[0]);
247                 continue;
248             }
249             if (!(res->defaultVariantSetId =
250                   odr_getoidbystr_nmem(nmem, argv[1])))
251             {
252                 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantsetid",
253                         file, lineno);
254                 continue;
255             }
256         }
257         else if (!strcmp(argv[0], "defaulttagtype"))
258         {
259             if (argc != 2)
260             {
261                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
262                         file, lineno, argv[0]);
263                 continue;
264             }
265             res->defaultTagType = (int *)nmem_malloc(nmem, sizeof(int));
266             *res->defaultTagType = atoi(argv[1]);
267         }
268         else if (!strcmp(argv[0], "defaultvariantrequest"))
269         {
270             if (!(res->defaultVariantRequest =
271                   read_variant(argc-1, argv+1, nmem, file, lineno)))
272             {
273                 yaz_log(LOG_WARN, "%s:%d: Bad defaultvariantrequest",
274                         file, lineno);
275                 continue;
276             }
277         }
278         else if (!strcmp(argv[0], "simpleelement"))
279         {
280             Z_ElementRequest *er;
281             Z_SimpleElement *se;
282             Z_ETagPath *tp;
283             char *path = argv[1];
284             char *ep;
285             int num, i = 0;
286             
287             if (!res->elements)
288                 res->elements = (Z_ElementRequest **)
289                     nmem_malloc(nmem, size_esn = 24*sizeof(er));
290             else if (res->num_elements >= (int) (size_esn/sizeof(er)))
291             {
292                 Z_ElementRequest **oe = res->elements;
293                 size_esn *= 2;
294                 res->elements = (Z_ElementRequest **)
295                     nmem_malloc (nmem, size_esn*sizeof(er));
296                 memcpy (res->elements, oe, size_esn/2);
297             }
298             if (argc < 2)
299             {
300                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for %s",
301                         file, lineno, argv[0]);
302                 continue;
303             }
304             
305             res->elements[res->num_elements++] = er =
306                 (Z_ElementRequest *)nmem_malloc(nmem, sizeof(*er));
307             er->which = Z_ERequest_simpleElement;
308             er->u.simpleElement = se = (Z_SimpleElement *)
309                 nmem_malloc(nmem, sizeof(*se));
310             se->variantRequest = 0;
311             se->path = tp = (Z_ETagPath *)nmem_malloc(nmem, sizeof(*tp));
312             tp->num_tags = 0;
313             /*
314              * Parse the element selector.
315              */
316             for (num = 1, ep = path; (ep = strchr(ep, '/')); num++, ep++)
317                 ;
318             tp->tags = (Z_ETagUnit **)
319                 nmem_malloc(nmem, sizeof(Z_ETagUnit*)*num);
320             
321             for ((ep = strchr(path, '/')) ; path ;
322                  (void)((path = ep) && (ep = strchr(path, '/'))))
323             {
324                 Z_ETagUnit *tagunit;
325                 if (ep)
326                     ep++;
327                 
328                 assert(i<num);
329                 tagunit = read_tagunit(path, nmem, file, lineno);
330                 if (!tagunit)
331                 {
332                     yaz_log (LOG_WARN, "%s%d: Bad tag unit at %s",
333                              file, lineno, path);
334                     break;
335                 }
336                 tp->tags[tp->num_tags++] = tagunit;
337             }
338             
339             if (argc > 2 && !strcmp(argv[2], "variant"))
340                 se->variantRequest=
341                     read_variant(argc-3, argv+3, nmem, file, lineno);
342         }
343         else
344             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'",
345                     file, lineno, argv[0]);
346     fclose (f);
347     return res;
348 }