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