9e8162d35300836725e7e570a3ce90eb2e8bb972
[yaz-moved-to-github.git] / src / prt-ext.c
1 /*
2  * Copyright (c) 1995-2004, Index Data.
3  * See the file LICENSE for details.
4  *
5  * $Id: prt-ext.c,v 1.2 2004-09-03 18:55:19 adam Exp $
6  */
7
8 #include <yaz/proto.h>
9
10 /*
11  * The table below should be moved to the ODR structure itself and
12  * be an image of the association context: To help
13  * map indirect references when they show up. 
14  */
15 static Z_ext_typeent type_table[] =
16 {
17     {VAL_SUTRS, Z_External_sutrs, (Odr_fun) z_SUTRS},
18     {VAL_EXPLAIN, Z_External_explainRecord, (Odr_fun)z_ExplainRecord},
19     {VAL_RESOURCE1, Z_External_resourceReport1, (Odr_fun)z_ResourceReport1},
20     {VAL_RESOURCE2, Z_External_resourceReport2, (Odr_fun)z_ResourceReport2},
21     {VAL_PROMPT1, Z_External_promptObject1, (Odr_fun)z_PromptObject1 },
22     {VAL_GRS1, Z_External_grs1, (Odr_fun)z_GenericRecord},
23     {VAL_EXTENDED, Z_External_extendedService, (Odr_fun)z_TaskPackage},
24     {VAL_ITEMORDER, Z_External_itemOrder, (Odr_fun)z_IOItemOrder},
25     {VAL_DIAG1, Z_External_diag1, (Odr_fun)z_DiagnosticFormat},
26     {VAL_ESPEC1, Z_External_espec1, (Odr_fun)z_Espec1},
27     {VAL_SUMMARY, Z_External_summary, (Odr_fun)z_BriefBib},
28     {VAL_OPAC, Z_External_OPAC, (Odr_fun)z_OPACRecord},
29     {VAL_SEARCHRES1, Z_External_searchResult1, (Odr_fun)z_SearchInfoReport},
30     {VAL_DBUPDATE, Z_External_update, (Odr_fun)z_IUUpdate},
31     {VAL_DBUPDATE0, Z_External_update0, (Odr_fun)z_IU0Update},
32     {VAL_DBUPDATE1, Z_External_update0, (Odr_fun)z_IU0Update},
33     {VAL_DATETIME, Z_External_dateTime, (Odr_fun)z_DateTime},
34     {VAL_UNIVERSE_REPORT, Z_External_universeReport,(Odr_fun)z_UniverseReport},
35     {VAL_ADMINSERVICE, Z_External_ESAdmin, (Odr_fun)z_Admin},
36     {VAL_USERINFO1, Z_External_userInfo1, (Odr_fun) z_OtherInformation},
37     {VAL_CHARNEG3, Z_External_charSetandLanguageNegotiation, (Odr_fun)
38                   z_CharSetandLanguageNegotiation},
39     {VAL_PROMPT1, Z_External_acfPrompt1, (Odr_fun) z_PromptObject1},
40     {VAL_DES1, Z_External_acfDes1, (Odr_fun) z_DES_RN_Object},
41     {VAL_KRB1, Z_External_acfKrb1, (Odr_fun) z_KRBObject},
42     {VAL_MULTISRCH2, Z_External_multisrch2, (Odr_fun) z_MultipleSearchTerms_2},
43     {VAL_CQL, Z_External_CQL, (Odr_fun) z_InternationalString},
44     {VAL_NONE, 0, 0}
45 };
46
47 Z_ext_typeent *z_ext_getentbyref(oid_value val)
48 {
49     Z_ext_typeent *i;
50
51     for (i = type_table; i->dref != VAL_NONE; i++)
52         if (i->dref == val)
53             return i;
54     return 0;
55 }
56
57 int z_External(ODR o, Z_External **p, int opt, const char *name)
58 {
59     oident *oid;
60     Z_ext_typeent *type;
61
62     static Odr_arm arm[] =
63     {
64         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_single,
65          (Odr_fun)odr_any, 0},
66         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_External_octet,
67          (Odr_fun)odr_octetstring, 0},
68         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_External_arbitrary,
69          (Odr_fun)odr_bitstring, 0},
70         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_sutrs,
71          (Odr_fun)z_SUTRS, 0},
72         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_explainRecord,
73          (Odr_fun)z_ExplainRecord, 0},
74
75         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport1,
76          (Odr_fun)z_ResourceReport1, 0},
77         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport2,
78          (Odr_fun)z_ResourceReport2, 0},
79         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_promptObject1,
80          (Odr_fun)z_PromptObject1, 0},
81         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_grs1,
82          (Odr_fun)z_GenericRecord, 0},
83         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_extendedService,
84          (Odr_fun)z_TaskPackage, 0},
85
86         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_itemOrder,
87          (Odr_fun)z_IOItemOrder, 0},
88         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_diag1,
89          (Odr_fun)z_DiagnosticFormat, 0},
90         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_espec1,
91          (Odr_fun)z_Espec1, 0},
92         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_summary,
93          (Odr_fun)z_BriefBib, 0},
94         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_OPAC,
95          (Odr_fun)z_OPACRecord, 0},
96
97         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_searchResult1,
98          (Odr_fun)z_SearchInfoReport, 0},
99         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_update,
100          (Odr_fun)z_IUUpdate, 0},
101         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_dateTime,
102          (Odr_fun)z_DateTime, 0},
103         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_universeReport,
104          (Odr_fun)z_UniverseReport, 0},
105         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_ESAdmin,
106          (Odr_fun)z_Admin, 0},
107
108         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_update0,
109          (Odr_fun)z_IU0Update, 0},
110         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_userInfo1,
111          (Odr_fun)z_OtherInformation, 0},
112         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_charSetandLanguageNegotiation,
113          (Odr_fun)z_CharSetandLanguageNegotiation, 0},
114         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfPrompt1,
115          (Odr_fun)z_PromptObject1, 0},
116         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfDes1,
117          (Odr_fun)z_DES_RN_Object, 0},
118
119         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_acfKrb1,
120          (Odr_fun)z_KRBObject, 0},
121         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_multisrch2,
122          (Odr_fun)z_MultipleSearchTerms_2, 0},
123         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_CQL,
124          (Odr_fun)z_InternationalString, 0},
125         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_OCLCUserInfo,
126          (Odr_fun)z_OCLC_UserInformation, 0},
127         {-1, -1, -1, -1, 0, 0}
128     };
129     
130     odr_implicit_settag(o, ODR_UNIVERSAL, ODR_EXTERNAL);
131     if (!odr_sequence_begin(o, p, sizeof(**p), name))
132         return opt && odr_ok(o);
133     if (!(odr_oid(o, &(*p)->direct_reference, 1, 0) &&
134           odr_integer(o, &(*p)->indirect_reference, 1, 0) &&
135           odr_graphicstring(o, &(*p)->descriptor, 1, 0)))
136         return 0;
137     /*
138      * Do we know this beast?
139      */
140     if (o->direction == ODR_DECODE && (*p)->direct_reference &&
141         (oid = oid_getentbyoid((*p)->direct_reference)) &&
142         (type = z_ext_getentbyref(oid->value)))
143     {
144         int zclass, tag, cons;
145         
146         /*
147          * We know it. If it's represented as an ASN.1 type, bias the CHOICE.
148          */
149         if (!odr_peektag(o, &zclass, &tag, &cons))
150             return opt && odr_ok(o);
151         if (zclass == ODR_CONTEXT && tag == 0 && cons == 1)
152             odr_choice_bias(o, type->what);
153     }
154     return
155         odr_choice(o, arm, &(*p)->u, &(*p)->which, name) &&
156         odr_sequence_end(o);
157 }
158
159 Z_External *z_ext_record(ODR o, int format, const char *buf, int len)
160 {
161     Z_External *thisext;
162
163     thisext = (Z_External *) odr_malloc(o, sizeof(*thisext));
164     thisext->descriptor = 0;
165     thisext->indirect_reference = 0;
166
167     thisext->direct_reference = 
168         yaz_oidval_to_z3950oid (o, CLASS_RECSYN, format);    
169     if (!thisext->direct_reference)
170         return 0;
171
172     if (len < 0) /* Structured data */
173     {
174         
175         /*
176          * We cheat on the pointers here. Obviously, the record field
177          * of the backend-fetch structure should have been a union for
178          * correctness, but we're stuck with this for backwards
179          * compatibility.
180          */
181         thisext->u.grs1 = (Z_GenericRecord*) buf;
182
183         switch (format)
184         {
185         case VAL_SUTRS:
186             thisext->which = Z_External_sutrs;
187             break;
188         case VAL_GRS1:
189             thisext->which = Z_External_grs1;
190             break;
191         case VAL_EXPLAIN:
192             thisext->which = Z_External_explainRecord;
193             break;
194         case VAL_SUMMARY:
195             thisext->which = Z_External_summary;
196             break;
197         case VAL_OPAC:
198             thisext->which = Z_External_OPAC;
199             break;
200         case VAL_EXTENDED:
201             thisext->which = Z_External_extendedService;
202             break;
203         default:
204             return 0;
205         }
206     }
207     else if (format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
208     {
209         Odr_oct *sutrs = (Odr_oct *)odr_malloc(o, sizeof(*sutrs));
210         
211         thisext->which = Z_External_sutrs;
212         thisext->u.sutrs = sutrs;
213         sutrs->buf = (unsigned char *)odr_malloc(o, len);
214         sutrs->len = sutrs->size = len;
215         memcpy(sutrs->buf, buf, len);
216     }
217     else
218     {
219         thisext->which = Z_External_octet;
220         if (!(thisext->u.octet_aligned = (Odr_oct *)
221               odr_malloc(o, sizeof(Odr_oct))))
222             return 0;
223         if (!(thisext->u.octet_aligned->buf = (unsigned char *)
224               odr_malloc(o, len)))
225             return 0;
226         memcpy(thisext->u.octet_aligned->buf, buf, len);
227         thisext->u.octet_aligned->len = thisext->u.octet_aligned->size = len;
228     }
229     return thisext;
230 }
231