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