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