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