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