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