6cf6d173056e3a48e6ae418be1e3b328329a9d52
[yaz-moved-to-github.git] / asn / proto.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: proto.c,v $
7  * Revision 1.2  1995-02-06 21:26:07  quinn
8  * Repaired this evening's damages..
9  *
10  * Revision 1.1  1995/02/06  16:44:47  quinn
11  * First hack at Z/SR protocol
12  *
13  */
14
15 #include <odr.h>
16
17 #include <proto.h>
18
19 /* ---------------------- INITIALIZE SERVICE ------------------- */
20
21 int z_ReferenceId(ODR o, Z_ReferenceId **p, int opt)
22 {
23     return odr_implicit(o, odr_octetstring, (Odr_oct**) p, ODR_CONTEXT, 2, opt);
24 }
25
26 int z_DatabaseName(Odr o, Z_DatabaseName **p, int opt)
27 {
28     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 105,
29         opt);
30 }
31
32 /* ---------------------- INITIALIZE SERVICE ------------------- */
33
34 int z_InitRequest(ODR o, Z_InitRequest **p, int opt)
35 {
36     Z_InitRequest *pp;
37
38     if (!odr_sequence_begin(o, p, sizeof(**p)))
39         return opt;
40     pp = *p;
41     return
42         z_ReferenceId(o, &pp->referenceId, 1) &&
43         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
44             3, 0) &&
45         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
46         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
47             5, 0) &&
48         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
49             6, 0) &&
50         odr_implicit(o, odr_visiblestring, &pp->idAuthentication, ODR_CONTEXT,
51             7, 1) &&
52         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
53             110, 1) &&
54         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
55             111, 1) &&
56         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
57             ODR_CONTEXT, 112, 1) &&
58         odr_sequence_end(o);
59 }
60
61 int z_InitResponse(ODR o, Z_InitResponse **p, int opt)
62 {
63     Z_InitResponse *pp;
64
65     if (!odr_sequence_begin(o, p, sizeof(**p)))
66         return opt;
67     pp = *p;
68     return
69         z_ReferenceId(o, &pp->referenceId, 1) &&
70         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
71             3, 0) &&
72         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
73         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
74             5, 0) &&
75         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
76             6, 0) &&
77         odr_implicit(o, odr_bool, &pp->result, ODR_CONTEXT, 12, 0) &&
78         odr_implicit(o, odr_visiblestring, &pp->idAuthentication, ODR_CONTEXT,
79             7, 1) &&
80         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
81             110, 1) &&
82         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
83             111, 1) &&
84         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
85             ODR_CONTEXT, 112, 1) &&
86         odr_sequence_end(o);
87 }
88
89 /* ------------------------ SEARCH SERVICE ----------------------- */
90
91 int z_ElementSetName(ODR o, Z_ElementSetName **p, int opt)
92 {
93     return odr_implicit(o, odr_visiblestring, (char**) p, ODR_CONTEXT, 103,
94         opt);
95 }
96
97 int z_PreferredRecordSyntax(ODR o, Z_PreferredRecordSyntax **p, int opt)
98 {
99     return odr_implicit(o, odr_oid, (Odr_oid**) p, ODR_CONTEXT, 104, opt);
100 }
101
102 int z_DatabaseSpecificUnit(ODR o, Z_DatabaseSpecificUnit **p, int opt)
103 {
104     if (!odr_sequence_begin(o, p, sizeof(**p)))
105         return opt;
106     return
107         z_DatabaseName(o, &(*p)->databaseName, 0) &&
108         z_ElementSetName(o, &(*p)->elementSetName, 0) &&
109         odr_sequence_end(o);
110 }
111
112 int z_DatabaseSpecific(ODR o, Z_DatabaseSpecific **p, int opt)
113 {
114     if (o->direction == ODR_DECODE && !*p)
115         *p = nalloc(o, sizeof(**p));
116     else if (!*p)
117         return opt;
118
119     odr_implicit_settag(o, ODR_CONTEXT, 1);
120     if (odr_sequence_of(o, z_DatabaseSpecificUnit, &(*p)->elements,
121         &(*p)->num_elements))
122         return 1;
123     *p = 0;
124     return 0;
125 }
126
127 int z_ElementSetNames(ODR o, Z_ElementSetNames **p, int opt)
128 {
129     static Odr_arm arm[] =
130     {
131         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_ElementSetNames_generic,
132             z_ElementSetName},
133         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ElementSetNames_databaseSpecific,
134             z_DatabaseSpecific},
135         {-1, -1, -1, -1, 0}
136     };
137
138     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 19, 0))
139         return opt;
140
141     if (o->direction == ODR_DECODE && !*p)
142         *p = nalloc(o, sizeof(**p));
143
144     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
145         odr_constructed_enmd(o))
146         return 1;
147     *p = 0;
148     return 0;
149 }
150
151 /* ----------------------- RPN QUERY -----------------------*/
152
153 int z_RPNStructure(ODR o, Z_RPNStructure, int opt);
154
155 int z_Operand(ODR o, Z_Operand **p, int opt)
156 {
157     Odr_arm arm[] =
158     {
159         {-1, -1, -1, Z_Operand_APT, z_AttributesPlusTerm},
160         {-1, -1, -1, Z_Operand_resultSetId, z_ResultSetId},
161         {-1, -1, -1, -1, 0}
162     };
163
164     if (o->direction ==ODR_DECODE && !*p)
165         *p = nalloc(o, sizeof(**p));
166     else if (!*p)
167         return opt;
168     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
169         return 1;
170     *p = 0;
171     return opt;
172 }
173
174 int z_Complex(ODR o, Z_Complex **p, int opt)
175 {
176     if (!odr_sequence_begin(o, p, sizeof(**p)))
177         return opt;
178     return
179         z_RPNStructure(o, &(*p)->s1, 0) &&
180         z_RPNStructure(o, &(*p)->s2, 0) &&
181         z_Operator(o, &(*p)->operator) &&
182         odr_sequence_end(o);
183 }
184
185 int z_RPNStructure(ODR o, Z_RPNStructure, int opt)
186 {
187     Odr_arm arm[] = 
188     {
189         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_RPNStructure_simple, z_Operand),
190         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_RPNStructure_complex, z_Complex},
191         {-1 -1, -1, -1, 0}
192     };
193
194     if (o->direction == ODR_DECODE && !*p)
195         *p = nalloc(o, sizeof(**p));
196     else if (!*p)
197         return opt;
198     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
199         return 1;
200     *p = 0;
201     return opt;
202 }
203
204 int z_RPNQuery(ODR o, Z_RPNQuery **p, int opt)
205 {
206     if (!odr_sequence_begin(o, p, sizeof(**p))
207         return opt;
208     return
209         odr_oid(o, &(*p)->attributeSetId, 0) &&
210         z_RPNStructure(o, &(*p)->RPNStructure, 0) &&
211         odr_sequence_end(o);
212 }
213
214 /* -----------------------END RPN QUERY ----------------------- */
215
216 int z_Query(ODR o, Z_Query **p, int opt)
217 {
218     Odr_arm arm[] = 
219     {
220         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Query_type_1, z_RPNQuery},
221         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_Query_type_2, odr_oct},
222         {-1, -1, -1, -1, 0}
223     };
224
225     if (o->direction == ODR_DECODE && !*p)
226         *p = nalloc(o, sizeof(**p);
227     else if (!*p)
228         return opt;
229     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
230         return 1;
231     *p = 0;
232     return opt;
233 }
234
235 int z_SearchRequest(ODR o, Z_SearchRequest **p, int opt)
236 {
237     Z_SearchRequest *pp;
238
239     if (!odr_sequence_begin(o, p, sizeof(**p)))
240         return opt;
241     pp = *p;
242     return
243         z_ReferenceId(o, &pp->referenceId, 1) &&
244         odr_implicit(o, odr_integer, &pp->smallSetUpperBound, ODR_CONTEXT,
245             13, 0) &&
246         odr_implicit(o, odr_integer, &pp->largeSetLowerBound, ODR_CONTEXT,
247             14, 0) &&
248         odr_implicit(o, odr_integer, &pp->mediumSetPresentNumber, ODR_CONTEXT,
249             15, 0) &&
250         odr_implicit(o, odr_bool, &pp->replaceIndicator, ODR_CONTEXT, 16, 1) &&
251         odr_implicit(o, odr_visiblestring, &pp->resultSetName, ODR_CONTEXT,
252             17, 9) &&
253         odr_implicit(o, z_ElementSetNames, &pp->smallSetElementSetNames,
254             ODR_CONTEXT, 100, 1) &&
255         odr_implicit(o, z_ElementSetNames, &pp->mediumSetElementSetNames,
256             ODR_CONTEXT, 101, 1) &&
257         z_PreferredRecordSyntax(o, &pp->preferredRecordSyntax, 1) &&
258         odr_explicit(o, z_query, ODR_CONTEXT, 21, 0) &&
259         odr_sequence_end(o);
260 }
261
262 int z_APDU(ODR o, Z_APDU **p, int opt)
263 {
264     static Odr_arm arm[] =
265     {
266         {ODR_IMPLICIT, ODR_CONTEXT, 20, Z_APDU_InitRequest, z_InitRequest},
267         {ODR_IMPLICIT, ODR_CONTEXT, 21, Z_APDU_InitResponse, z_InitResponse},
268         {ODR_IMPLICIT, ODR_CONTEXT, 22, Z_APDU_SearchRequest, z_SearchRequest},
269
270         {-1, -1, -1, -1, 0}
271     };
272
273     if (o->direction == ODR_DECODE && !*p)
274         *p = nalloc(o, sizeof(**p));
275     if (!odr_choice(o, arm, &(*p)->u, &(*p)->which))
276     {
277         if (o->direction == ODR_DECODE)
278         {
279             *p = 0;
280             return opt;
281         }
282     }
283     return 1;
284 }