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