1506e5899eb425e373b85cea6999db6d69addbfc
[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.3  1995-02-09 15:51:39  quinn
8  * Works better now.
9  *
10  * Revision 1.2  1995/02/06  21:26:07  quinn
11  * Repaired this evening's damages..
12  *
13  * Revision 1.1  1995/02/06  16:44:47  quinn
14  * First hack at Z/SR protocol
15  *
16  */
17
18 #include <odr.h>
19
20 #include <proto.h>
21
22 /* ---------------------- GLOBAL DEFS ------------------- */
23
24 int z_ReferenceId(ODR o, Z_ReferenceId **p, int opt)
25 {
26     return odr_implicit(o, odr_octetstring, (Odr_oct**) p, ODR_CONTEXT, 2, opt);
27 }
28
29 int z_DatabaseName(ODR o, Z_DatabaseName **p, int opt)
30 {
31     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 105,
32         opt);
33 }
34
35 int z_ResultSetId(ODR o, char **p, int opt)
36 {
37     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 31,
38         opt);
39 }
40
41 int z_UserInformationField(ODR o, Z_UserInformationField **p, int opt)
42 {
43     return odr_explicit(o, odr_external, (Odr_external **)p, ODR_CONTEXT,
44         11, opt);
45 }
46
47 /* ---------------------- INITIALIZE SERVICE ------------------- */
48
49 int z_InitRequest(ODR o, Z_InitRequest **p, int opt)
50 {
51     Z_InitRequest *pp;
52
53     if (!odr_sequence_begin(o, p, sizeof(**p)))
54         return opt;
55     pp = *p;
56     return
57         z_ReferenceId(o, &pp->referenceId, 1) &&
58         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
59             3, 0) &&
60         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
61         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
62             5, 0) &&
63         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
64             6, 0) &&
65         odr_implicit(o, odr_visiblestring, &pp->idAuthentication, ODR_CONTEXT,
66             7, 1) &&
67         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
68             110, 1) &&
69         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
70             111, 1) &&
71         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
72             ODR_CONTEXT, 112, 1) &&
73         z_UserInformationField(o, &pp->userInformationField, 1) &&
74         odr_sequence_end(o);
75 }
76
77 int z_InitResponse(ODR o, Z_InitResponse **p, int opt)
78 {
79     Z_InitResponse *pp;
80
81     if (!odr_sequence_begin(o, p, sizeof(**p)))
82         return opt;
83     pp = *p;
84     return
85         z_ReferenceId(o, &pp->referenceId, 1) &&
86         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
87             3, 0) &&
88         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
89         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
90             5, 0) &&
91         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
92             6, 0) &&
93         odr_implicit(o, odr_bool, &pp->result, ODR_CONTEXT, 12, 0) &&
94         odr_implicit(o, odr_visiblestring, &pp->idAuthentication, ODR_CONTEXT,
95             7, 1) &&
96         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
97             110, 1) &&
98         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
99             111, 1) &&
100         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
101             ODR_CONTEXT, 112, 1) &&
102         z_UserInformationField(o, &pp->userInformationField, 1) &&
103         odr_sequence_end(o);
104 }
105
106 /* ------------------------ SEARCH SERVICE ----------------------- */
107
108 int z_ElementSetName(ODR o, char **p, int opt)
109 {
110     return odr_implicit(o, odr_visiblestring, (char**) p, ODR_CONTEXT, 103,
111         opt);
112 }
113
114 int z_PreferredRecordSyntax(ODR o, Z_PreferredRecordSyntax **p, int opt)
115 {
116     return odr_implicit(o, odr_oid, (Odr_oid**) p, ODR_CONTEXT, 104, opt);
117 }
118
119 int z_DatabaseSpecificUnit(ODR o, Z_DatabaseSpecificUnit **p, int opt)
120 {
121     if (!odr_sequence_begin(o, p, sizeof(**p)))
122         return opt;
123     return
124         z_DatabaseName(o, &(*p)->databaseName, 0) &&
125         z_ElementSetName(o, &(*p)->elementSetName, 0) &&
126         odr_sequence_end(o);
127 }
128
129 int z_DatabaseSpecific(ODR o, Z_DatabaseSpecific **p, int opt)
130 {
131     if (o->direction == ODR_DECODE)
132         *p = nalloc(o, sizeof(**p));
133     else if (!*p)
134         return opt;
135
136     odr_implicit_settag(o, ODR_CONTEXT, 1);
137     if (odr_sequence_of(o, z_DatabaseSpecificUnit, &(*p)->elements,
138         &(*p)->num_elements))
139         return 1;
140     *p = 0;
141     return 0;
142 }
143
144 int z_ElementSetNames(ODR o, Z_ElementSetNames **p, int opt)
145 {
146     static Odr_arm arm[] =
147     {
148         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_ElementSetNames_generic,
149             z_ElementSetName},
150         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ElementSetNames_databaseSpecific,
151             z_DatabaseSpecific},
152         {-1, -1, -1, -1, 0}
153     };
154
155     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 19))
156         return opt;
157
158     if (o->direction == ODR_DECODE)
159         *p = nalloc(o, sizeof(**p));
160
161     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
162         odr_constructed_end(o))
163         return 1;
164     *p = 0;
165     return 0;
166 }
167
168 /* ----------------------- RPN QUERY -----------------------*/
169
170 int z_AttributeElement(ODR o, Z_AttributeElement **p, int opt)
171 {
172     if (!odr_sequence_begin(o, p, sizeof(**p)))
173         return opt;
174     return
175         odr_implicit(o, odr_integer, &(*p)->attributeType, ODR_CONTEXT,
176             120, 1) &&
177         odr_implicit(o, odr_integer, &(*p)->attributeValue, ODR_CONTEXT,
178             121, 1) &&
179         odr_sequence_end(o);
180 }
181
182 int z_AttributesPlusTerm(ODR o, Z_AttributesPlusTerm **p, int opt)
183 {
184     if (!(odr_implicit_settag(o, ODR_CONTEXT, 102) &&
185         odr_sequence_begin(o, p, sizeof(**p))))
186         return opt;
187     return
188         odr_implicit_settag(o, ODR_CONTEXT, 44) &&
189         odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
190             &(*p)->num_attributes) &&
191         odr_implicit(o, odr_octetstring, &(*p)->term, ODR_CONTEXT, 45, 0) &&
192         odr_sequence_end(o);
193 }
194
195 int z_Operator(ODR o, Z_Operator **p, int opt)
196 {
197     static Odr_arm arm[] =
198     {
199         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_Operator_and, odr_null},
200         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Operator_or, odr_null},
201         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_Operator_and_not, odr_null},
202         {-1, -1, -1, -1, 0}
203     };
204     int dummy = 999;
205
206     if (!*p && o->direction != ODR_DECODE)
207         return opt;
208     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 46))
209         return opt;
210     if (o->direction == ODR_DECODE)
211         *p = nalloc(o, sizeof(**p));
212     else
213         (*p)->u.and = &dummy;
214
215     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
216         odr_constructed_end(o))
217         return 1;
218     *p = 0;
219     return opt;
220 }
221
222 int z_Operand(ODR o, Z_Operand **p, int opt)
223 {
224     static Odr_arm arm[] =
225     {
226         {-1, -1, -1, Z_Operand_APT, z_AttributesPlusTerm},
227         {-1, -1, -1, Z_Operand_resultSetId, z_ResultSetId},
228         {-1, -1, -1, -1, 0}
229     };
230
231     if (o->direction ==ODR_DECODE)
232         *p = nalloc(o, sizeof(**p));
233     else if (!*p)
234         return opt;
235     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
236         return 1;
237     *p = 0;
238     return opt;
239 }
240
241 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt);
242
243 int z_Complex(ODR o, Z_Complex **p, int opt)
244 {
245     if (!odr_sequence_begin(o, p, sizeof(**p)))
246         return opt;
247     return
248         z_RPNStructure(o, &(*p)->s1, 0) &&
249         z_RPNStructure(o, &(*p)->s2, 0) &&
250         z_Operator(o, &(*p)->operator, 0) &&
251         odr_sequence_end(o);
252 }
253
254 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt)
255 {
256     static Odr_arm arm[] = 
257     {
258         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_RPNStructure_simple, z_Operand},
259         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_RPNStructure_complex, z_Complex},
260         {-1 -1, -1, -1, 0}
261     };
262
263     if (o->direction == ODR_DECODE)
264         *p = nalloc(o, sizeof(**p));
265     else if (!*p)
266         return opt;
267     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
268         return 1;
269     *p = 0;
270     return opt;
271 }
272
273 int z_RPNQuery(ODR o, Z_RPNQuery **p, int opt)
274 {
275     if (!odr_sequence_begin(o, p, sizeof(**p)))
276         return opt;
277     return
278         odr_oid(o, &(*p)->attributeSetId, 0) &&
279         z_RPNStructure(o, &(*p)->RPNStructure, 0) &&
280         odr_sequence_end(o);
281 }
282
283 /* -----------------------END RPN QUERY ----------------------- */
284
285 int z_Query(ODR o, Z_Query **p, int opt)
286 {
287     static Odr_arm arm[] = 
288     {
289         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Query_type_1, z_RPNQuery},
290         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_Query_type_2, odr_octetstring},
291         {-1, -1, -1, -1, 0}
292     };
293
294     if (o->direction == ODR_DECODE)
295         *p = nalloc(o, sizeof(**p));
296     else if (!*p)
297         return opt;
298     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
299         return 1;
300     *p = 0;
301     return opt;
302 }
303
304 int z_SearchRequest(ODR o, Z_SearchRequest **p, int opt)
305 {
306     Z_SearchRequest *pp;
307
308     if (!odr_sequence_begin(o, p, sizeof(**p)))
309         return opt;
310     pp = *p;
311     return
312         z_ReferenceId(o, &pp->referenceId, 1) &&
313         odr_implicit(o, odr_integer, &pp->smallSetUpperBound, ODR_CONTEXT,
314             13, 0) &&
315         odr_implicit(o, odr_integer, &pp->largeSetLowerBound, ODR_CONTEXT,
316             14, 0) &&
317         odr_implicit(o, odr_integer, &pp->mediumSetPresentNumber, ODR_CONTEXT,
318             15, 0) &&
319         odr_implicit(o, odr_bool, &pp->replaceIndicator, ODR_CONTEXT, 16, 1) &&
320         odr_implicit(o, odr_visiblestring, &pp->resultSetName, ODR_CONTEXT,
321             17, 9) &&
322         odr_implicit_settag(o, ODR_CONTEXT, 18) &&
323         odr_sequence_of(o, z_DatabaseName, &pp->databaseNames,
324             &pp->num_databaseNames) &&
325         odr_implicit(o, z_ElementSetNames, &pp->smallSetElementSetNames,
326             ODR_CONTEXT, 100, 1) &&
327         odr_implicit(o, z_ElementSetNames, &pp->mediumSetElementSetNames,
328             ODR_CONTEXT, 101, 1) &&
329         z_PreferredRecordSyntax(o, &pp->preferredRecordSyntax, 1) &&
330         odr_explicit(o, z_Query, &pp->query, ODR_CONTEXT, 21, 0) &&
331         odr_sequence_end(o);
332 }
333
334 /* ------------------------ RECORD ------------------------- */
335
336 int z_DatabaseRecord(ODR o, Z_DatabaseRecord **p, int opt)
337 {
338     return odr_external(o, (Odr_external **) p, opt);
339 }
340
341 int z_DiagRec(ODR o, Z_DiagRec **p, int opt)
342 {
343     if (!odr_sequence_begin(o, p, sizeof(**p)))
344         return opt;
345     return
346         odr_oid(o, &(*p)->diagnosticSetId, 1) &&       /* SHOULD NOT BE OPT */
347         odr_integer(o, &(*p)->condition, 0) &&
348         (odr_visiblestring(o, &(*p)->addinfo, 0) ||
349         odr_implicit(o, odr_cstring, &(*p)->addinfo, ODR_CONTEXT, ODR_VISIBLESTRING, 1)) &&
350         odr_sequence_end(o);
351 }
352
353 int z_NamePlusRecord(ODR o, Z_NamePlusRecord **p, int opt)
354 {
355     static Odr_arm arm[] =
356     {
357         {ODR_EXPLICIT, ODR_CONTEXT, 1, Z_NamePlusRecord_databaseRecord,
358             z_DatabaseRecord},
359         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_NamePlusRecord_surrogateDiagnostic,
360             z_DiagRec},
361         {-1, -1, -1, -1, 0}
362     };
363
364     if (!odr_sequence_begin(o, p, sizeof(**p)))
365         return opt;
366     return
367         odr_implicit(o, z_DatabaseName, &(*p)->databaseName, ODR_CONTEXT,
368             0, 1) &&
369         odr_constructed_begin(o, &(*p)->u, ODR_CONTEXT, 1) &&
370         odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
371         odr_constructed_end(o) &&
372         odr_sequence_end(o);
373 }
374
375 int z_NamePlusRecordList(ODR o, Z_NamePlusRecordList **p, int opt)
376 {
377     if (o->direction == ODR_DECODE)
378         *p = nalloc(o, sizeof(**p));
379     if (odr_sequence_of(o, z_NamePlusRecordList, &(*p)->records,
380         &(*p)->num_records))
381         return 1;
382     *p = 0;
383     return 0;
384 }
385
386 int z_Records(ODR o, Z_Records **p, int opt)
387 {
388     Odr_arm arm[] = 
389     {
390         {ODR_IMPLICIT, ODR_CONTEXT, 28, Z_Records_DBOSD, z_NamePlusRecordList},
391         {ODR_IMPLICIT, ODR_CONTEXT, 130, Z_Records_NSD, z_DiagRec},
392         {-1, -1, -1, -1, 0}
393     };
394
395     if (o->direction == ODR_DECODE)
396         *p = nalloc(o, sizeof(**p));
397     else if (!*p)
398         return opt;
399     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
400         return 1;
401     *p = 0;
402     return opt;
403 }
404
405 /* ------------------------ SEARCHRESPONSE ----------------*/
406
407 int z_NumberOfRecordsReturned(ODR o, int **p, int opt)
408 {
409     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 24, opt);
410 }
411
412 int z_NextResultSetPosition(ODR o, int **p, int opt)
413 {
414     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 25, opt);
415 }
416
417 int z_PresentStatus(ODR o, int **p, int opt)
418 {
419     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 27, opt);
420 }
421
422 int z_SearchResponse(ODR o, Z_SearchResponse **p, int opt)
423 {
424     Z_SearchResponse *pp;
425
426     if (!odr_sequence_begin(o, p, sizeof(**p)))
427         return opt;
428     pp = *p;
429     return
430         z_ReferenceId(o, &pp->referenceId, 1) &&
431         odr_implicit(o, odr_integer, &pp->resultCount, ODR_CONTEXT, 23, 0) &&
432         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
433         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
434         odr_implicit(o, odr_bool, &pp->searchStatus, ODR_CONTEXT, 22, 0) &&
435         odr_implicit(o, odr_integer, &pp->resultSetStatus, ODR_CONTEXT, 26, 1) &&
436         z_PresentStatus(o, &pp->presentStatus, 1) &&
437         z_Records(o, &pp->records, 1) &&
438         odr_sequence_end(o);
439 }
440
441 /* --------------------- PRESENT SERVICE ---------------------- */
442
443 int z_PresentRequest(ODR o, Z_PresentRequest **p, int opt)
444 {
445     Z_PresentRequest *pp;
446
447     if (!odr_sequence_begin(o, p, sizeof(**p)))
448         return opt;
449     pp = *p;
450     return
451         z_ReferenceId(o, &pp->referenceId, 1) &&
452         z_ResultSetId(o, &pp->resultSetId, 0) &&
453         odr_implicit(o, odr_integer, &pp->resultSetStartPoint, ODR_CONTEXT,
454             30, 0) &&
455         odr_implicit(o, odr_integer, &pp->numberOfRecordsRequested, ODR_CONTEXT,
456             29, 0) &&
457         z_ElementSetNames(o, &pp->elementSetNames, 1) &&
458         z_PreferredRecordSyntax(o, &pp->preferredRecordSyntax, 1) &&
459         odr_sequence_end(o);
460 }
461
462 int z_PresentResponse(ODR o, Z_PresentResponse **p, int opt)
463 {
464     Z_PresentResponse *pp;
465
466     if (!odr_sequence_begin(o, p, sizeof(**p)))
467         return opt;
468     pp = *p;
469     return
470         z_ReferenceId(o, &pp->referenceId, 1) &&
471         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
472         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
473         z_PresentStatus(o, &pp->presentStatus, 0) &&
474         z_Records(o, &pp->records, 1) &&
475         odr_sequence_end(o);
476 }
477
478 /* ------------------------ APDU ------------------------- */
479
480 int z_APDU(ODR o, Z_APDU **p, int opt)
481 {
482     static Odr_arm arm[] =
483     {
484         {ODR_IMPLICIT, ODR_CONTEXT, 20, Z_APDU_initRequest, z_InitRequest},
485         {ODR_IMPLICIT, ODR_CONTEXT, 21, Z_APDU_initResponse, z_InitResponse},
486         {ODR_IMPLICIT, ODR_CONTEXT, 22, Z_APDU_searchRequest, z_SearchRequest},
487         {ODR_IMPLICIT, ODR_CONTEXT, 23, Z_APDU_searchResponse, z_SearchResponse},
488         {ODR_IMPLICIT, ODR_CONTEXT, 24, Z_APDU_presentRequest, z_PresentRequest},
489         {ODR_IMPLICIT, ODR_CONTEXT, 25, Z_APDU_presentResponse, z_PresentResponse},
490
491         {-1, -1, -1, -1, 0}
492     };
493
494     if (o->direction == ODR_DECODE)
495         *p = nalloc(o, sizeof(**p));
496     if (!odr_choice(o, arm, &(*p)->u, &(*p)->which))
497     {
498         if (o->direction == ODR_DECODE)
499         {
500             *p = 0;
501             return opt;
502         }
503     }
504     return 1;
505 }