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