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