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