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