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