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