Fix leak for odr_print of ZOOM connection.
[yaz-moved-to-github.git] / src / prt-ext.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2009 Index Data
3  * See the file LICENSE for details.
4  */
5
6 /**
7  * \file prt-ext.c
8  * \brief Implements handling of various Z39.50 Externals
9  */
10
11 #include <yaz/proto.h>
12
13 #include <yaz/oid_db.h>
14 #define PRT_EXT_DEBUG 0
15
16 #if PRT_EXT_DEBUG
17 #include <yaz/log.h>
18 #endif
19
20 /*
21  * The table below should be moved to the ODR structure itself and
22  * be an image of the association context: To help
23  * map indirect references when they show up. 
24  */
25 static Z_ext_typeent type_table[] =
26 {
27     {{1, 2, 840, 10003, 5, 101,-1}, Z_External_sutrs, (Odr_fun) z_SUTRS},
28     {{1, 2, 840, 10003, 5, 100,-1}, Z_External_explainRecord, (Odr_fun)z_ExplainRecord},
29     {{1, 2, 840, 10003, 7, 1,-1}, Z_External_resourceReport1, (Odr_fun)z_ResourceReport1},
30     {{1, 2, 840, 10003, 7, 2,-1}, Z_External_resourceReport2, (Odr_fun)z_ResourceReport2},
31     {{1, 2, 840, 10003, 8, 1,-1}, Z_External_promptObject1, (Odr_fun)z_PromptObject1 },
32     {{1, 2, 840, 10003, 5, 105,-1}, Z_External_grs1, (Odr_fun)z_GenericRecord},
33     {{1, 2, 840, 10003, 5, 106,-1}, Z_External_extendedService, (Odr_fun)z_TaskPackage},
34     {{1, 2, 840, 10003, 9, 4,-1}, Z_External_itemOrder, (Odr_fun)z_IOItemOrder},
35     {{1, 2, 840, 10003, 4, 2,-1}, Z_External_diag1, (Odr_fun)z_DiagnosticFormat},
36     {{1, 2, 840, 10003, 11, 1,-1}, Z_External_espec1, (Odr_fun)z_Espec1},
37     {{1, 2, 840, 10003, 5, 103,-1}, Z_External_summary, (Odr_fun)z_BriefBib},
38     {{1, 2, 840, 10003, 5, 102,-1}, Z_External_OPAC, (Odr_fun)z_OPACRecord},
39     {{1, 2, 840, 10003, 10, 1,-1}, Z_External_searchResult1, (Odr_fun)z_SearchInfoReport},
40     {{1, 2, 840, 10003, 9, 5,-1}, Z_External_update0, (Odr_fun)z_IU0Update},
41     {{1, 2, 840, 10003, 9, 5, 1,-1}, Z_External_update0, (Odr_fun)z_IU0Update},
42     {{1, 2, 840, 10003, 9, 5, 1, 1,-1}, Z_External_update, (Odr_fun)z_IUUpdate},
43     {{1, 2, 840, 10003, 10, 6,-1}, Z_External_dateTime, (Odr_fun)z_DateTime},
44     {{1, 2, 840, 10003, 7, 1000, 81, 1,-1}, Z_External_universeReport,(Odr_fun)z_UniverseReport},
45     {{1, 2, 840, 10003, 9, 1000, 81, 1,-1}, Z_External_ESAdmin, (Odr_fun)z_Admin},
46     {{1, 2, 840, 10003, 10, 3,-1}, Z_External_userInfo1, (Odr_fun) z_OtherInformation},
47     {{1, 2, 840, 10003, 15, 3,-1}, Z_External_charSetandLanguageNegotiation, (Odr_fun)
48                   z_CharSetandLanguageNegotiation},
49     {{1, 2, 840, 10003, 8, 1,-1}, Z_External_acfPrompt1, (Odr_fun) z_PromptObject1},
50     {{1, 2, 840, 10003, 8, 2,-1}, Z_External_acfDes1, (Odr_fun) z_DES_RN_Object},
51     {{1, 2, 840, 10003, 8, 3,-1}, Z_External_acfKrb1, (Odr_fun) z_KRBObject},
52     {{1, 2, 840, 10003, 10, 5,-1}, Z_External_multisrch2, (Odr_fun) z_MultipleSearchTerms_2},
53     {{1, 2, 840, 10003, 16,  2, -1}, Z_External_CQL, (Odr_fun) z_InternationalString},
54     {{1, 2, 840, 10003, 9, 1,-1}, Z_External_persistentResultSet, (Odr_fun)z_PRPersistentResultSet},
55     {{1, 2, 840, 10003, 9, 2,-1}, Z_External_persistentQuery, (Odr_fun)z_PQueryPersistentQuery},
56     {{1, 2, 840, 10003, 9, 3,-1}, Z_External_periodicQuerySchedule, (Odr_fun)z_PQSPeriodicQuerySchedule},
57     {{1, 2, 840, 10003, 9, 6,-1}, Z_External_exportSpecification, (Odr_fun)z_ESExportSpecification},
58     {{1, 2, 840, 10003, 9, 7,-1}, Z_External_exportInvocation, (Odr_fun)z_EIExportInvocation},
59     {{-1}, 0, 0}
60 };
61
62 Z_ext_typeent *z_ext_getentbyref(const Odr_oid *oid)
63 {
64     Z_ext_typeent *p;
65
66     for (p = type_table; p->oid[0] != -1; p++)
67         if (!oid_oidcmp(oid, p->oid))
68             return p;
69     return 0;
70 }
71
72 /**
73   This routine is the BER codec for the EXTERNAL type.
74   It handles information in single-ASN1-type and octet-aligned
75   for known structures.
76
77   <pre>
78     [UNIVERSAL 8] IMPLICIT SEQUENCE {
79     direct-reference      OBJECT IDENTIFIER OPTIONAL,
80     indirect-reference    INTEGER OPTIONAL,
81     data-value-descriptor ObjectDescriptor OPTIONAL,
82     encoding              CHOICE {
83       single-ASN1-type   [0] ABSTRACT_SYNTAX.&Type,
84       octet-aligned      [1] IMPLICIT OCTET STRING,
85       arbitrary          [2] IMPLICIT BIT STRING 
86       }
87     }
88   </pre>
89   arbitrary BIT STRING not handled yet.
90 */
91 int z_External(ODR o, Z_External **p, int opt, const char *name)
92 {
93     Z_ext_typeent *type;
94
95     static Odr_arm arm[] =
96     {
97         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_single,
98          (Odr_fun)odr_any, 0},
99         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_External_octet,
100          (Odr_fun)odr_octetstring, 0},
101         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_External_arbitrary,
102          (Odr_fun)odr_bitstring, 0},
103         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_sutrs,
104          (Odr_fun)z_SUTRS, 0},
105         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_explainRecord,
106          (Odr_fun)z_ExplainRecord, 0},
107
108         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport1,
109          (Odr_fun)z_ResourceReport1, 0},
110         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport2,
111          (Odr_fun)z_ResourceReport2, 0},
112         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_promptObject1,
113          (Odr_fun)z_PromptObject1, 0},
114         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_grs1,
115          (Odr_fun)z_GenericRecord, 0},
116         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_extendedService,
117          (Odr_fun)z_TaskPackage, 0},
118
119         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_itemOrder,
120          (Odr_fun)z_IOItemOrder, 0},
121         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_diag1,
122          (Odr_fun)z_DiagnosticFormat, 0},
123         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_espec1,
124          (Odr_fun)z_Espec1, 0},
125         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_summary,
126          (Odr_fun)z_BriefBib, 0},
127         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_OPAC,
128          (Odr_fun)z_OPACRecord, 0},
129
130         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_searchResult1,
131          (Odr_fun)z_SearchInfoReport, 0},
132         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_update,
133          (Odr_fun)z_IUUpdate, 0},
134         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_dateTime,
135          (Odr_fun)z_DateTime, 0},
136         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_universeReport,
137          (Odr_fun)z_UniverseReport, 0},
138         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_ESAdmin,
139          (Odr_fun)z_Admin, 0},
140
141         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_update0,
142          (Odr_fun)z_IU0Update, 0},
143         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_userInfo1,
144          (Odr_fun)z_OtherInformation, 0},
145         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_charSetandLanguageNegotiation,
146          (Odr_fun)z_CharSetandLanguageNegotiation, 0},
147         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfPrompt1,
148          (Odr_fun)z_PromptObject1, 0},
149         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfDes1,
150          (Odr_fun)z_DES_RN_Object, 0},
151
152         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfKrb1,
153          (Odr_fun)z_KRBObject, 0},
154         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_multisrch2,
155          (Odr_fun)z_MultipleSearchTerms_2, 0},
156         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_CQL,
157          (Odr_fun)z_InternationalString, 0},
158         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_OCLCUserInfo,
159          (Odr_fun)z_OCLC_UserInformation, 0},
160         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_persistentResultSet,
161          (Odr_fun)z_PRPersistentResultSet, 0},
162
163         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_persistentQuery,
164          (Odr_fun)z_PQueryPersistentQuery, 0},
165         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_periodicQuerySchedule,
166          (Odr_fun)z_PQSPeriodicQuerySchedule, 0},
167         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_exportSpecification,
168          (Odr_fun)z_ESExportSpecification, 0},
169         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_exportInvocation,
170          (Odr_fun)z_EIExportInvocation, 0},
171         {-1, -1, -1, -1, 0, 0}
172     };
173
174     odr_implicit_settag(o, ODR_UNIVERSAL, ODR_EXTERNAL);
175     if (!odr_sequence_begin(o, p, sizeof(**p), name))
176         return opt && odr_ok(o);
177     if (!(odr_oid(o, &(*p)->direct_reference, 1, 0) &&
178           odr_integer(o, &(*p)->indirect_reference, 1, 0) &&
179           odr_graphicstring(o, &(*p)->descriptor, 1, 0)))
180         return 0;
181 #if PRT_EXT_DEBUG
182     /* debugging purposes only */
183     if (o->direction == ODR_DECODE)
184     {
185         yaz_log(YLOG_LOG, "z_external decode");
186         if ((*p)->direct_reference)
187         {
188             yaz_log(YLOG_LOG, "direct reference");
189             if ((oid = oid_getentbyoid((*p)->direct_reference)))
190             {
191                 yaz_log(YLOG_LOG, "oid %s", oid->desc);
192                 if ((type = z_ext_getentbyref(oid->value)))
193                     yaz_log(YLOG_LOG, "type");
194             }
195         }
196     }
197 #endif
198     /* Do we know this beast? */
199     if (o->direction == ODR_DECODE && (*p)->direct_reference &&
200         (type = z_ext_getentbyref((*p)->direct_reference)))
201     {
202         int zclass, tag, cons;
203         /* OID is present and we know it */
204
205         if (!odr_peektag(o, &zclass, &tag, &cons))
206             return opt && odr_ok(o);
207 #if PRT_EXT_DEBUG
208         yaz_log(YLOG_LOG, "odr_peektag OK tag=%d cons=%d zclass=%d what=%d",
209                 tag, cons, zclass, type->what);
210 #endif
211         if (zclass == ODR_CONTEXT && tag == 1 && cons == 0)
212         {
213             /* we have an OCTET STRING. decode BER contents from it */
214             const unsigned char *o_bp;
215             unsigned char *o_buf;
216             int o_size;
217             char *voidp = 0;
218             Odr_oct *oct;
219             int r;
220             if (!odr_implicit_tag(o, odr_octetstring, &oct,
221                                  ODR_CONTEXT, 1, 0, "octetaligned"))
222                 return 0;
223
224             /* Save our decoding ODR members */
225             o_bp = o->bp; 
226             o_buf = o->buf;
227             o_size = o->size;
228
229             /* Set up the OCTET STRING buffer */
230             o->bp = o->buf = oct->buf;
231             o->size = oct->len;
232
233             /* and decode that */
234             r = (*type->fun)(o, &voidp, 0, 0);
235             (*p)->which = type->what;
236             (*p)->u.single_ASN1_type = (Odr_any*) voidp;
237                 
238             /* Restore our decoding ODR member */
239             o->bp = o_bp; 
240             o->buf = o_buf;
241             o->size = o_size;
242
243             return r && odr_sequence_end(o);
244         }
245         if (zclass == ODR_CONTEXT && tag == 0 && cons == 1)
246         { 
247             /* It's single ASN.1 type, bias the CHOICE. */
248             odr_choice_bias(o, type->what);
249         }
250     }
251     return
252         odr_choice(o, arm, &(*p)->u, &(*p)->which, name) &&
253         odr_sequence_end(o);
254 }
255
256 Z_External *z_ext_record_oid(ODR o, const Odr_oid *oid, const char *buf, int len)
257 {
258     Z_External *thisext;
259     char oid_str_buf[OID_STR_MAX];
260     const char *oid_str;
261     oid_class oclass;
262
263     if (!oid)
264         return 0;
265     thisext = (Z_External *) odr_malloc(o, sizeof(*thisext));
266     thisext->descriptor = 0;
267     thisext->indirect_reference = 0;
268
269     oid_str = yaz_oid_to_string_buf(oid, &oclass, oid_str_buf);
270
271     thisext->direct_reference = odr_oiddup(o, oid);
272
273     if (len < 0) /* Structured data */
274     {
275         /*
276          * We cheat on the pointers here. Obviously, the record field
277          * of the backend-fetch structure should have been a union for
278          * correctness, but we're stuck with this for backwards
279          * compatibility.
280          */
281         thisext->u.grs1 = (Z_GenericRecord*) buf;
282
283         if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs))
284         {
285             thisext->which = Z_External_sutrs;
286         }
287         else if (!oid_oidcmp(oid, yaz_oid_recsyn_grs_1))
288         {
289             thisext->which = Z_External_grs1;
290         }
291         else if (!oid_oidcmp(oid, yaz_oid_recsyn_explain))
292         {
293             thisext->which = Z_External_explainRecord;
294         }
295         else if (!oid_oidcmp(oid, yaz_oid_recsyn_summary))
296         {
297             thisext->which = Z_External_summary;
298         }
299         else if (!oid_oidcmp(oid, yaz_oid_recsyn_opac))
300         {
301             thisext->which = Z_External_OPAC;
302         }
303         else if (!oid_oidcmp(oid, yaz_oid_recsyn_extended))
304         {
305             thisext->which = Z_External_extendedService;
306         }
307         else
308         {
309             return 0;
310         }
311     }
312     else if (!oid_oidcmp(oid, yaz_oid_recsyn_sutrs)) /* SUTRS is a single-ASN.1-type */
313     {
314         Odr_oct *sutrs = (Odr_oct *)odr_malloc(o, sizeof(*sutrs));
315         
316         thisext->which = Z_External_sutrs;
317         thisext->u.sutrs = sutrs;
318         sutrs->buf = (unsigned char *)odr_malloc(o, len);
319         sutrs->len = sutrs->size = len;
320         memcpy(sutrs->buf, buf, len);
321     }
322     else
323     {
324         thisext->which = Z_External_octet;
325         if (!(thisext->u.octet_aligned = (Odr_oct *)
326               odr_malloc(o, sizeof(Odr_oct))))
327             return 0;
328         if (!(thisext->u.octet_aligned->buf = (unsigned char *)
329               odr_malloc(o, len)))
330             return 0;
331         memcpy(thisext->u.octet_aligned->buf, buf, len);
332         thisext->u.octet_aligned->len = thisext->u.octet_aligned->size = len;
333     }
334     return thisext;
335 }
336
337 Z_External *z_ext_record_oid_any(ODR o, const Odr_oid *oid,
338                                  const char *buf, int len)
339 {
340     Z_External *thisext;
341     char oid_str_buf[OID_STR_MAX];
342     const char *oid_str;
343     oid_class oclass;
344
345     if (!oid)
346         return 0;
347     thisext = (Z_External *) odr_malloc(o, sizeof(*thisext));
348     thisext->descriptor = 0;
349     thisext->indirect_reference = 0;
350
351     oid_str = yaz_oid_to_string_buf(oid, &oclass, oid_str_buf);
352
353     thisext->direct_reference = odr_oiddup(o, oid);
354
355     thisext->which = Z_External_single;
356     thisext->u.single_ASN1_type = (Odr_any *) odr_malloc(o, sizeof(Odr_any));
357     if (!thisext->u.single_ASN1_type)
358         return 0;
359     thisext->u.single_ASN1_type->buf = (unsigned char *) odr_malloc(o, len);
360     if (!thisext->u.single_ASN1_type->buf)
361         return 0;
362     memcpy(thisext->u.single_ASN1_type->buf, buf, len);
363     thisext->u.single_ASN1_type->len = thisext->u.single_ASN1_type->size = len;
364
365     return thisext;
366 }
367
368 Z_External *z_ext_record_xml(ODR o, const char *buf, int len)
369 {
370     return z_ext_record_oid(o, yaz_oid_recsyn_xml, buf, len);
371 }
372
373 Z_External *z_ext_record_sutrs(ODR o, const char *buf, int len)
374 {
375     return z_ext_record_oid(o, yaz_oid_recsyn_sutrs, buf, len);
376 }
377
378 Z_External *z_ext_record_usmarc(ODR o, const char *buf, int len)
379 {
380     return z_ext_record_oid(o, yaz_oid_recsyn_usmarc, buf, len);
381 }
382
383 /*
384  * Local variables:
385  * c-basic-offset: 4
386  * c-file-style: "Stroustrup"
387  * indent-tabs-mode: nil
388  * End:
389  * vim: shiftwidth=4 tabstop=8 expandtab
390  */
391