9386b763c53090a14bd9582da39f0cba161f2694
[yaz-moved-to-github.git] / asn / prt-grs.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-grs.c,v $
7  * Revision 1.9  1999-11-30 13:47:11  adam
8  * Improved installation. Moved header files to include/yaz.
9  *
10  * Revision 1.8  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.7  1998/02/11 11:53:32  adam
15  * Changed code so that it compiles as C++.
16  *
17  * Revision 1.6  1997/05/14 06:53:23  adam
18  * C++ support.
19  *
20  * Revision 1.5  1995/10/18 16:12:20  quinn
21  * Added a couple of special cases to handle the WAIS server.
22  *
23  * Revision 1.4  1995/09/29  17:11:55  quinn
24  * Smallish
25  *
26  * Revision 1.3  1995/09/27  15:02:43  quinn
27  * Modified function heads & prototypes.
28  *
29  * Revision 1.2  1995/08/28  10:58:58  quinn
30  * Added extra choice-entry to data to catch visiblestring.
31  *
32  * Revision 1.1  1995/08/17  12:47:09  quinn
33  * Added GRS-1.
34  *
35  *
36  */
37
38 #include <yaz/proto.h>
39
40 int z_TaggedElement(ODR o, Z_TaggedElement **p, int opt, const char *name);
41 int z_ElementData(ODR o, Z_ElementData **p, int opt, const char *name);
42 int z_ElementMetaData(ODR o, Z_ElementMetaData **p, int opt, const char *name);
43 int z_TagUnit(ODR o, Z_TagUnit **p, int opt, const char *name);
44 int z_TagPath(ODR o, Z_TagPath **p, int opt, const char *name);
45 int z_Order(ODR o, Z_Order **p, int opt, const char *name);
46 int z_Usage(ODR o, Z_Usage **p, int opt, const char *name);
47 int z_HitVector(ODR o, Z_HitVector **p, int opt, const char *name);
48 int z_Triple(ODR o, Z_Triple **p, int opt, const char *name);
49 int z_Variant(ODR o, Z_Variant **p, int opt, const char *name);
50
51 int z_GenericRecord(ODR o, Z_GenericRecord **p, int opt, const char *name)
52 {
53     if (o->direction == ODR_DECODE)
54         *p = (Z_GenericRecord *)odr_malloc(o, sizeof(**p));
55     else if (!*p)
56         return opt;
57     if (odr_sequence_of(o, (Odr_fun)z_TaggedElement, &(*p)->elements,
58                         &(*p)->num_elements, 0))
59         return 1;
60     *p = 0;
61     return opt && odr_ok(o);
62 }
63
64 int z_TaggedElement(ODR o, Z_TaggedElement **p, int opt, const char *name)
65 {
66     if (!odr_sequence_begin(o, p, sizeof(**p), 0))
67         return opt && odr_ok(o);
68     return
69         odr_implicit(o, odr_integer, &(*p)->tagType,
70                      ODR_CONTEXT, 1, 1) &&
71         odr_explicit(o, z_StringOrNumeric, &(*p)->tagValue,
72                      ODR_CONTEXT, 2, 0) &&
73         odr_implicit(o, odr_integer, &(*p)->tagOccurrence,
74                      ODR_CONTEXT, 3, 1) &&
75         odr_explicit(o, z_ElementData, &(*p)->content,
76                      ODR_CONTEXT, 4, 0) &&
77         odr_implicit(o, z_ElementMetaData, &(*p)->metaData,
78                      ODR_CONTEXT, 5, 1) &&
79         odr_implicit(o, z_Variant, &(*p)->appliedVariant, ODR_CONTEXT, 6, 1) &&
80         odr_sequence_end(o);
81 }
82
83 int z_ElementData(ODR o, Z_ElementData **p, int opt, const char *name)
84 {
85     static Odr_arm arm[] =
86     {
87         {ODR_NONE, -1, -1, Z_ElementData_octets,
88          (Odr_fun)odr_octetstring, 0},
89         {ODR_NONE, -1, -1, Z_ElementData_numeric,
90          (Odr_fun)odr_integer, 0},
91         {ODR_NONE, -1, -1, Z_ElementData_date,
92          (Odr_fun)odr_generalizedtime, 0},
93         {ODR_NONE, -1, -1, Z_ElementData_ext,
94          (Odr_fun)z_External, 0},
95         {ODR_NONE, -1, -1, Z_ElementData_string,
96          (Odr_fun)z_InternationalString, 0},
97         /* The entry below provides some backwards compatibility */
98         {ODR_NONE, -1, -1, Z_ElementData_string,
99          (Odr_fun)odr_visiblestring, 0},
100         {ODR_NONE, -1, -1, Z_ElementData_trueOrFalse,
101          (Odr_fun)odr_bool, 0},
102         {ODR_NONE, -1, -1, Z_ElementData_oid,
103          (Odr_fun)odr_oid, 0},
104         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ElementData_intUnit,
105          (Odr_fun)z_IntUnit, 0},
106         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_ElementData_elementNotThere,
107          (Odr_fun)odr_null, 0},
108         {ODR_IMPLICIT, ODR_CONTEXT, 3, Z_ElementData_elementEmpty,
109          (Odr_fun)odr_null, 0},
110         {ODR_IMPLICIT, ODR_CONTEXT, 4, Z_ElementData_noDataRequested,
111          (Odr_fun)odr_null, 0},
112         {ODR_IMPLICIT, ODR_CONTEXT, 5, Z_ElementData_diagnostic,
113          (Odr_fun)z_External, 0},
114         {ODR_EXPLICIT, ODR_CONTEXT, 6, Z_ElementData_subtree,
115          (Odr_fun)z_GenericRecord, 0},
116         {-1, -1, -1, -1, 0, 0}
117     };
118     
119     if (o->direction == ODR_DECODE)
120         *p = (Z_ElementData *)odr_malloc(o, sizeof(**p));
121     else if (!*p)
122         return opt;
123     if (odr_choice(o, arm, &(*p)->u, &(*p)->which, 0))
124         return 1;
125     *p = 0;
126     return opt && odr_ok(o);
127 }
128
129 int z_ElementMetaData(ODR o, Z_ElementMetaData **p, int opt, const char *name)
130 {
131     if (!odr_sequence_begin(o, p, sizeof(**p), 0))
132         return opt && odr_ok(o);
133     return
134         odr_implicit(o, z_Order, &(*p)->seriesOrder, ODR_CONTEXT, 1, 1) &&
135         odr_implicit(o, z_Usage, &(*p)->usageRight, ODR_CONTEXT, 2, 1) &&
136         odr_implicit_settag(o, ODR_CONTEXT, 3) &&
137         (odr_sequence_of(o, (Odr_fun)z_HitVector, &(*p)->hits,
138                          &(*p)->num_hits, 0) ||
139          odr_ok(o)) &&
140         odr_implicit(o, z_InternationalString, &(*p)->displayName,
141                      ODR_CONTEXT, 4, 1) &&
142         odr_implicit_settag(o, ODR_CONTEXT, 5) &&
143         (odr_sequence_of(o, (Odr_fun)z_Variant, &(*p)->supportedVariants,
144                          &(*p)->num_supportedVariants, 0) || odr_ok(o)) &&
145         odr_implicit(o, z_InternationalString, &(*p)->message,
146                      ODR_CONTEXT, 6, 1) &&
147         odr_implicit(o, odr_octetstring, &(*p)->elementDescriptor,
148                      ODR_CONTEXT, 7, 1) &&
149         odr_implicit(o, z_TagPath, &(*p)->surrogateFor, ODR_CONTEXT, 8, 1) &&
150         odr_implicit(o, z_TagPath, &(*p)->surrogateElement,
151                      ODR_CONTEXT, 9, 1) &&
152         odr_implicit(o, z_External, &(*p)->other, ODR_CONTEXT, 99, 1) &&
153         odr_sequence_end(o);
154 }
155
156 int z_TagUnit(ODR o, Z_TagUnit **p, int opt, const char *name)
157 {
158     if (!odr_sequence_begin(o, p, sizeof(**p), 0))
159         return opt && odr_ok(o);
160     return
161         odr_implicit(o, odr_integer, &(*p)->tagType, ODR_CONTEXT, 1, 1) &&
162         odr_explicit(o, z_StringOrNumeric, &(*p)->tagValue,
163                      ODR_CONTEXT, 2, 0) &&
164         odr_implicit(o, odr_integer, &(*p)->tagOccurrence,
165                      ODR_CONTEXT, 3, 1) &&
166         odr_sequence_end(o);
167 }
168
169 int z_TagPath(ODR o, Z_TagPath **p, int opt, const char *name)
170 {
171     if (o->direction == ODR_DECODE)
172         *p = (Z_TagPath *)odr_malloc(o, sizeof(**p));
173     else if (!*p)
174         return opt;
175     if (odr_sequence_of(o, (Odr_fun)z_TagUnit, &(*p)->tags,
176                         &(*p)->num_tags, 0))
177         return 1;
178     *p = 0;
179     return opt && odr_ok(o);
180 }
181
182 int z_Order(ODR o, Z_Order **p, int opt, const char *name)
183 {
184     if (!odr_sequence_begin(o, p, sizeof(**p), 0))
185         return opt && odr_ok(o);
186     return
187         odr_implicit(o, odr_bool, &(*p)->ascending, ODR_CONTEXT, 1, 0) &&
188         odr_implicit(o, odr_integer, &(*p)->order, ODR_CONTEXT, 2, 0) &&
189         odr_sequence_end(o);
190 }
191
192 int z_Usage(ODR o, Z_Usage **p, int opt, const char *name)
193 {
194     if (!odr_sequence_begin(o, p, sizeof(**p), 0))
195         return opt && odr_ok(o);
196     return
197         odr_implicit(o, odr_integer, &(*p)->type, ODR_CONTEXT, 1, 0) &&
198         odr_implicit(o, z_InternationalString, &(*p)->restriction, ODR_CONTEXT,
199                      2, 1) &&
200         odr_sequence_end(o);
201 }
202
203 int z_HitVector(ODR o, Z_HitVector **p, int opt, const char *name)
204 {
205     if (!odr_sequence_begin(o, p, sizeof(**p), 0))
206         return opt && odr_ok(o);
207     return
208         z_Term(o, &(*p)->satisfier, 1, 0) &&
209         odr_implicit(o, z_IntUnit, &(*p)->offsetIntoElement,
210                      ODR_CONTEXT, 1, 1) &&
211         odr_implicit(o, z_IntUnit, &(*p)->length, ODR_CONTEXT, 2, 1) &&
212         odr_implicit(o, odr_integer, &(*p)->hitRank, ODR_CONTEXT, 3, 1) &&
213         odr_implicit(o, odr_octetstring, &(*p)->targetToken, ODR_CONTEXT,
214                      4, 1) &&
215         odr_sequence_end(o);
216 }
217
218 int z_Triple(ODR o, Z_Triple **p, int opt, const char *name)
219 {
220     static Odr_arm arm[] =
221     {
222         {ODR_NONE, -1, -1, Z_Triple_integer,
223          (Odr_fun)odr_integer, 0},
224         {ODR_NONE, -1, -1, Z_Triple_internationalString,
225          (Odr_fun)z_InternationalString, 0},
226         /* The entry below provides some backwards compatibility */
227         {ODR_NONE, -1, -1, Z_Triple_internationalString,
228          (Odr_fun)odr_visiblestring, 0},
229         {ODR_NONE, -1, -1, Z_Triple_octetString,
230          (Odr_fun)odr_octetstring, 0},
231         {ODR_NONE, -1, -1, Z_Triple_oid,
232          (Odr_fun)odr_oid, 0},
233         {ODR_NONE, -1, -1, Z_Triple_boolean,
234          (Odr_fun)odr_bool, 0},
235         {ODR_NONE, -1, -1, Z_Triple_null,
236          (Odr_fun)odr_null, 0},
237         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Triple_unit,
238          (Odr_fun)z_Unit, 0},
239         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_Triple_valueAndUnit,
240          (Odr_fun)z_IntUnit, 0},
241         {-1, -1, -1, -1, 0, 0}
242     };
243     
244     if (!odr_sequence_begin(o, p, sizeof(**p), 0))
245         return opt && odr_ok(o);
246     return
247         odr_implicit(o, odr_oid, &(*p)->variantSetId, ODR_CONTEXT, 0, 1) &&
248         odr_implicit(o, odr_integer, &(*p)->zclass, ODR_CONTEXT, 1, 0) &&
249         odr_implicit(o, odr_integer, &(*p)->type, ODR_CONTEXT, 2, 0) &&
250         odr_constructed_begin(o, &(*p)->value, ODR_CONTEXT, 3, 0) &&
251         odr_choice(o, arm, &(*p)->value, &(*p)->which, 0) &&
252         odr_constructed_end(o) &&
253         odr_sequence_end(o);
254 }
255
256 int z_Variant(ODR o, Z_Variant **p, int opt, const char *name)
257 {
258     if (!odr_sequence_begin(o, p, sizeof(**p), 0))
259         return opt && odr_ok(o);
260     return
261         odr_implicit(o, odr_oid, &(*p)->globalVariantSetId,
262                      ODR_CONTEXT, 1, 1) &&
263         odr_implicit_settag(o, ODR_CONTEXT, 2) &&
264         odr_sequence_of(o, (Odr_fun)z_Triple, &(*p)->triples,
265                         &(*p)->num_triples, 0) &&
266         odr_sequence_end(o);
267 }