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