2349fbc39d1eeb058c8b48db288a560ccfd1abc9
[yaz-moved-to-github.git] / zutil / prt-ext.c
1 /*
2  * Copyright (c) 1995-1999, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: prt-ext.c,v $
7  * Revision 1.4  2000-03-14 15:22:04  ian
8  * Added Admin external to choice table.
9  *
10  * Revision 1.3  2000/03/14 13:52:32  ian
11  * Added Admin Extended Service to External Choice
12  *
13  * Revision 1.2  1999/11/30 13:47:12  adam
14  * Improved installation. Moved header files to include/yaz.
15  *
16  * Revision 1.1  1999/06/08 10:10:16  adam
17  * New sub directory zutil. Moved YAZ Compiler to be part of YAZ tree.
18  *
19  * Revision 1.22  1999/05/26 15:24:26  adam
20  * Fixed minor bugs regarding DB Update (introduced by previous commit).
21  *
22  * Revision 1.21  1999/05/26 14:47:12  adam
23  * Implemented z_ext_record.
24  *
25  * Revision 1.20  1999/04/20 09:56:48  adam
26  * Added 'name' paramter to encoder/decoder routines (typedef Odr_fun).
27  * Modified all encoders/decoders to reflect this change.
28  *
29  * Revision 1.19  1998/03/31 15:13:19  adam
30  * Development towards compiled ASN.1.
31  *
32  * Revision 1.18  1998/03/31 11:07:44  adam
33  * Furhter work on UNIverse resource report.
34  * Added Extended Services handling in frontend server.
35  *
36  * Revision 1.17  1998/03/20 14:46:06  adam
37  * Added UNIverse Resource Reports.
38  *
39  * Revision 1.16  1998/02/11 11:53:32  adam
40  * Changed code so that it compiles as C++.
41  *
42  * Revision 1.15  1998/02/10 15:31:46  adam
43  * Implemented date and time structure. Changed the Update Extended
44  * Service.
45  *
46  * Revision 1.14  1998/01/05 09:04:57  adam
47  * Fixed bugs in encoders/decoders - Not operator (!) missing.
48  *
49  * Revision 1.13  1997/05/14 06:53:22  adam
50  * C++ support.
51  *
52  * Revision 1.12  1997/04/30 08:52:02  quinn
53  * Null
54  *
55  * Revision 1.11  1996/10/10  12:35:13  quinn
56  * Added Update extended service.
57  *
58  * Revision 1.10  1996/10/09  15:54:55  quinn
59  * Added SearchInfoReport
60  *
61  * Revision 1.9  1996/06/10  08:53:36  quinn
62  * Added Summary,OPAC,ResourceReport
63  *
64  * Revision 1.8  1996/02/20  12:51:44  quinn
65  * Completed SCAN. Fixed problems with EXTERNAL.
66  *
67  * Revision 1.7  1995/10/12  10:34:38  quinn
68  * Added Espec-1.
69  *
70  * Revision 1.6  1995/09/29  17:11:55  quinn
71  * Smallish
72  *
73  * Revision 1.5  1995/09/27  15:02:42  quinn
74  * Modified function heads & prototypes.
75  *
76  * Revision 1.4  1995/08/29  11:17:16  quinn
77  * *** empty log message ***
78  *
79  * Revision 1.3  1995/08/21  09:10:18  quinn
80  * Smallish fixes to suppport new formats.
81  *
82  * Revision 1.2  1995/08/17  12:45:00  quinn
83  * Fixed minor problems with GRS-1. Added support in c&s.
84  *
85  * Revision 1.1  1995/08/15  13:37:41  quinn
86  * Improved EXTERNAL
87  *
88  *
89  */
90
91 #include <yaz/proto.h>
92
93 /*
94  * The table below should be moved to the ODR structure itself and
95  * be an image of the association context: To help
96  * map indirect references when they show up. 
97  */
98 static Z_ext_typeent type_table[] =
99 {
100     {VAL_SUTRS, Z_External_sutrs, (Odr_fun) z_SUTRS},
101     {VAL_EXPLAIN, Z_External_explainRecord, (Odr_fun)z_ExplainRecord},
102     {VAL_RESOURCE1, Z_External_resourceReport1, (Odr_fun)z_ResourceReport1},
103     {VAL_RESOURCE2, Z_External_resourceReport2, (Odr_fun)z_ResourceReport2},
104     {VAL_PROMPT1, Z_External_promptObject1, (Odr_fun)z_PromptObject1 },
105     {VAL_GRS1, Z_External_grs1, (Odr_fun)z_GenericRecord},
106     {VAL_EXTENDED, Z_External_extendedService, (Odr_fun)z_TaskPackage},
107 #ifdef ASN_COMPILED
108     {VAL_ITEMORDER, Z_External_itemOrder, (Odr_fun)z_IOItemOrder},
109 #else
110     {VAL_ITEMORDER, Z_External_itemOrder, (Odr_fun)z_ItemOrder},
111 #endif
112     {VAL_DIAG1, Z_External_diag1, (Odr_fun)z_DiagnosticFormat},
113     {VAL_ESPEC1, Z_External_espec1, (Odr_fun)z_Espec1},
114     {VAL_SUMMARY, Z_External_summary, (Odr_fun)z_BriefBib},
115     {VAL_OPAC, Z_External_OPAC, (Odr_fun)z_OPACRecord},
116     {VAL_SEARCHRES1, Z_External_searchResult1, (Odr_fun)z_SearchInfoReport},
117     {VAL_DBUPDATE, Z_External_update, (Odr_fun)z_IUUpdate},
118     {VAL_DATETIME, Z_External_dateTime, (Odr_fun)z_DateTime},
119     {VAL_UNIVERSE_REPORT, Z_External_universeReport, (Odr_fun)z_UniverseReport},
120 #ifdef ASN_COMPILED
121     {VAL_ADMINSERVICE, Z_External_ESAdmin, (Odr_fun)z_Admin},
122 #endif
123     {VAL_NONE, 0, 0}
124 };
125
126 Z_ext_typeent *z_ext_getentbyref(oid_value val)
127 {
128     Z_ext_typeent *i;
129
130     for (i = type_table; i->dref != VAL_NONE; i++)
131         if (i->dref == val)
132             return i;
133     return 0;
134 }
135
136 int z_External(ODR o, Z_External **p, int opt, const char *name)
137 {
138     oident *oid;
139     Z_ext_typeent *type;
140
141     static Odr_arm arm[] =
142     {
143         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_single,
144          (Odr_fun)odr_any, 0},
145         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_External_octet,
146          (Odr_fun)odr_octetstring, 0},
147         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_External_arbitrary,
148          (Odr_fun)odr_bitstring, 0},
149         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_sutrs,
150          (Odr_fun)z_SUTRS, 0},
151         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_explainRecord,
152          (Odr_fun)z_ExplainRecord, 0},
153         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport1,
154          (Odr_fun)z_ResourceReport1, 0},
155         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_resourceReport2,
156          (Odr_fun)z_ResourceReport2, 0},
157         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_promptObject1,
158          (Odr_fun)z_PromptObject1, 0},
159         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_grs1,
160          (Odr_fun)z_GenericRecord, 0},
161         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_extendedService,
162          (Odr_fun)z_TaskPackage, 0},
163 #ifdef ASN_COMPILED
164         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_itemOrder,
165          (Odr_fun)z_IOItemOrder, 0},
166 #else
167         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_itemOrder,
168          (Odr_fun)z_ItemOrder, 0},
169 #endif
170         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_diag1,
171          (Odr_fun)z_DiagnosticFormat, 0},
172         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_espec1,
173          (Odr_fun)z_Espec1, 0},
174         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_summary,
175          (Odr_fun)z_BriefBib, 0},
176         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_OPAC,
177          (Odr_fun)z_OPACRecord, 0},
178         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_searchResult1,
179          (Odr_fun)z_SearchInfoReport, 0},
180         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_update,
181          (Odr_fun)z_IUUpdate, 0},
182         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_dateTime,
183          (Odr_fun)z_DateTime, 0},
184         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_universeReport,
185          (Odr_fun)z_UniverseReport, 0},
186 #ifdef ASN_COMPILED
187         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_External_ESAdmin,
188          (Odr_fun)z_Admin, 0},
189 #endif
190         {-1, -1, -1, -1, 0, 0}
191     };
192     
193     odr_implicit_settag(o, ODR_UNIVERSAL, ODR_EXTERNAL);
194     if (!odr_sequence_begin(o, p, sizeof(**p), name))
195         return opt && odr_ok(o);
196     if (!(odr_oid(o, &(*p)->direct_reference, 1, 0) &&
197           odr_integer(o, &(*p)->indirect_reference, 1, 0) &&
198           odr_graphicstring(o, &(*p)->descriptor, 1, 0)))
199         return 0;
200     /*
201      * Do we know this beast?
202      */
203     if (o->direction == ODR_DECODE && (*p)->direct_reference &&
204         (oid = oid_getentbyoid((*p)->direct_reference)) &&
205         (type = z_ext_getentbyref(oid->value)))
206     {
207         int zclass, tag, cons;
208         
209         /*
210          * We know it. If it's represented as an ASN.1 type, bias the CHOICE.
211          */
212         if (!odr_peektag(o, &zclass, &tag, &cons))
213             return opt && odr_ok(o);
214         if (zclass == ODR_CONTEXT && tag == 0 && cons == 1)
215             odr_choice_bias(o, type->what);
216     }
217     return
218         odr_choice(o, arm, &(*p)->u, &(*p)->which, name) &&
219         odr_sequence_end(o);
220 }
221
222 Z_External *z_ext_record(ODR o, int format, const char *buf, int len)
223 {
224     Z_External *thisext;
225     oident recform;
226     int oid[OID_SIZE];
227
228     thisext = (Z_External *) odr_malloc(o, sizeof(*thisext));
229     thisext->descriptor = 0;
230     thisext->indirect_reference = 0;
231
232     recform.proto = PROTO_Z3950;
233     recform.oclass = CLASS_RECSYN;
234     recform.value = (enum oid_value) format;
235     if (!oid_ent_to_oid(&recform, oid))
236         return 0;
237     thisext->direct_reference = odr_oiddup(o, oid);
238     
239     if (len < 0) /* Structured data */
240     {
241         switch (format)
242         {
243         case VAL_SUTRS:
244             thisext->which = Z_External_sutrs;
245             break;
246         case VAL_GRS1:
247             thisext->which = Z_External_grs1;
248             break;
249         case VAL_EXPLAIN:
250             thisext->which = Z_External_explainRecord;
251             break;
252         case VAL_SUMMARY:
253             thisext->which = Z_External_summary;
254             break;
255         case VAL_OPAC:
256             thisext->which = Z_External_OPAC;
257             break;
258         default:
259             return 0;
260         }
261         
262         /*
263          * We cheat on the pointers here. Obviously, the record field
264          * of the backend-fetch structure should have been a union for
265          * correctness, but we're stuck with this for backwards
266          * compatibility.
267          */
268         thisext->u.grs1 = (Z_GenericRecord*) buf;
269     }
270     else if (format == VAL_SUTRS) /* SUTRS is a single-ASN.1-type */
271     {
272         Odr_oct *sutrs = (Odr_oct *)odr_malloc(o, sizeof(*sutrs));
273         
274         thisext->which = Z_External_sutrs;
275         thisext->u.sutrs = sutrs;
276         sutrs->buf = (unsigned char *)odr_malloc(o, len);
277         sutrs->len = sutrs->size = len;
278         memcpy(sutrs->buf, buf, len);
279     }
280     else
281     {
282         thisext->which = Z_External_octet;
283         if (!(thisext->u.octet_aligned = (Odr_oct *)
284               odr_malloc(o, sizeof(Odr_oct))))
285             return 0;
286         if (!(thisext->u.octet_aligned->buf = (unsigned char *)
287               odr_malloc(o, len)))
288             return 0;
289         memcpy(thisext->u.octet_aligned->buf, buf, len);
290         thisext->u.octet_aligned->len = thisext->u.octet_aligned->size = len;
291     }
292     return thisext;
293 }
294