Added links to access control PDUs
[yaz-moved-to-github.git] / asn / proto.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: proto.c,v $
7  * Revision 1.41  1996-01-10 15:21:24  quinn
8  * Added links to access control PDUs
9  *
10  * Revision 1.40  1996/01/02  11:46:40  quinn
11  * Changed 'operator' to 'roperator' to avoid C++ conflict.
12  *
13  * Revision 1.39  1995/09/29  17:11:53  quinn
14  * Smallish
15  *
16  * Revision 1.38  1995/09/27  15:02:40  quinn
17  * Modified function heads & prototypes.
18  *
19  * Revision 1.37  1995/08/21  09:10:15  quinn
20  * Smallish fixes to suppport new formats.
21  *
22  * Revision 1.36  1995/08/15  11:59:39  quinn
23  * Updated External
24  *
25  * Revision 1.35  1995/08/10  08:53:59  quinn
26  * Added Explain
27  *
28  * Revision 1.34  1995/06/19  17:01:48  quinn
29  * This should bring us in sync with the version distributed as 1.0b
30  *
31  * Revision 1.33  1995/06/19  13:39:56  quinn
32  * *** empty log message ***
33  *
34  * Revision 1.32  1995/06/19  12:37:28  quinn
35  * Fixed a bug in the compspec.
36  *
37  * Revision 1.31  1995/06/16  13:15:56  quinn
38  * Fixed Defaultdiagformat.
39  *
40  * Revision 1.30  1995/06/15  15:42:01  quinn
41  * Fixed some v3 bugs
42  *
43  * Revision 1.29  1995/06/15  07:44:49  quinn
44  * Moving to v3.
45  *
46  * Revision 1.28  1995/06/14  15:26:35  quinn
47  * *** empty log message ***
48  *
49  * Revision 1.27  1995/06/07  14:36:22  quinn
50  * Added CLOSE
51  *
52  * Revision 1.26  1995/06/02  09:49:13  quinn
53  * Adding access control
54  *
55  * Revision 1.25  1995/05/25  11:00:08  quinn
56  * *** empty log message ***
57  *
58  * Revision 1.24  1995/05/22  13:58:18  quinn
59  * Fixed an ODR_NULLVAL.
60  *
61  * Revision 1.23  1995/05/22  11:30:18  quinn
62  * Adding Z39.50-1992 stuff to proto.c. Adding zget.c
63  *
64  * Revision 1.22  1995/05/17  08:40:56  quinn
65  * Added delete. Fixed some sequence_begins. Smallish.
66  *
67  * Revision 1.21  1995/05/16  08:50:24  quinn
68  * License, documentation, and memory fixes
69  *
70  * Revision 1.20  1995/05/15  11:55:25  quinn
71  * Smallish.
72  *
73  * Revision 1.19  1995/04/11  11:58:35  quinn
74  * Fixed bug.
75  *
76  * Revision 1.18  1995/04/11  11:52:02  quinn
77  * Fixed possible buf in proto.c
78  *
79  * Revision 1.17  1995/04/10  10:22:22  quinn
80  * Added SCAN.
81  *
82  * Revision 1.16  1995/03/30  10:26:43  quinn
83  * Added Term structure
84  *
85  * Revision 1.15  1995/03/30  09:08:39  quinn
86  * Added Resource control protocol
87  *
88  * Revision 1.14  1995/03/29  08:06:13  quinn
89  * Added a few v3 elements
90  *
91  * Revision 1.13  1995/03/20  11:26:52  quinn
92  * *** empty log message ***
93  *
94  * Revision 1.12  1995/03/20  09:45:09  quinn
95  * Working towards v3
96  *
97  * Revision 1.11  1995/03/17  10:17:25  quinn
98  * Added memory management.
99  *
100  * Revision 1.10  1995/03/15  11:17:40  quinn
101  * Fixed some return-checks from choice.. need better ay to handle those..
102  *
103  * Revision 1.9  1995/03/15  08:37:06  quinn
104  * Fixed protocol bugs.
105  *
106  * Revision 1.8  1995/03/14  16:59:24  quinn
107  * Fixed OPTIONAL flag in attributeelement
108  *
109  * Revision 1.7  1995/03/07  16:29:33  quinn
110  * Added authentication stuff.
111  *
112  * Revision 1.6  1995/03/01  14:46:03  quinn
113  * Fixed protocol bug in 8777query.
114  *
115  * Revision 1.5  1995/02/14  11:54:22  quinn
116  * Fixing include.
117  *
118  * Revision 1.4  1995/02/10  15:54:30  quinn
119  * Small adjustments.
120  *
121  * Revision 1.3  1995/02/09  15:51:39  quinn
122  * Works better now.
123  *
124  * Revision 1.2  1995/02/06  21:26:07  quinn
125  * Repaired this evening's damages..
126  *
127  * Revision 1.1  1995/02/06  16:44:47  quinn
128  * First hack at Z/SR protocol
129  *
130  */
131
132 #include <odr.h>
133
134 #include <proto.h>
135
136 /* ---------------------- GLOBAL DEFS ------------------- */
137
138 /*
139  * We'll use a general octetstring here, since string operations are
140  * clumsy on long strings.
141  */
142 int z_SUTRS(ODR o, Odr_oct **p, int opt)
143 {
144     return odr_implicit(o, odr_octetstring, p, ODR_UNIVERSAL,
145         ODR_GENERALSTRING, opt);
146 }
147
148 int z_ReferenceId(ODR o, Z_ReferenceId **p, int opt)
149 {
150     return odr_implicit(o, odr_octetstring, (Odr_oct**) p, ODR_CONTEXT, 2, opt);
151 }
152
153 int z_DatabaseName(ODR o, Z_DatabaseName **p, int opt)
154 {
155     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 105,
156         opt);
157 }
158
159 int z_ResultSetId(ODR o, char **p, int opt)
160 {
161     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 31,
162         opt);
163 }
164
165 int z_ElementSetName(ODR o, char **p, int opt)
166 {
167     return odr_implicit(o, odr_visiblestring, p, ODR_CONTEXT, 103, opt);
168 }
169
170 int z_UserInformationField(ODR o, Z_External **p, int opt)
171 {
172     return odr_explicit(o, z_External, (Z_External **)p, ODR_CONTEXT,
173         11, opt);
174 }
175
176 int z_InternationalString(ODR o, char **p, int opt)
177 {
178     return odr_generalstring(o, p, opt);
179 }
180
181 int z_InfoCategory(ODR o, Z_InfoCategory **p, int opt)
182 {
183     if (!odr_sequence_begin(o, p, sizeof(**p)))
184         return opt && odr_ok(o);
185     return
186         odr_implicit(o, odr_oid, &(*p)->categoryTypeId, ODR_CONTEXT, 1, 1) &&
187         odr_implicit(o, odr_integer, &(*p)->categoryValue, ODR_CONTEXT, 2, 0) &&
188         odr_sequence_end(o);
189 }
190
191 int z_OtherInformationUnit(ODR o, Z_OtherInformationUnit **p, int opt)
192 {
193     static Odr_arm arm[] =
194     {
195         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_OtherInfo_characterInfo,
196             odr_visiblestring},
197         {ODR_IMPLICIT, ODR_CONTEXT, 3, Z_OtherInfo_binaryInfo,
198             odr_octetstring},
199         {ODR_IMPLICIT, ODR_CONTEXT, 4, Z_OtherInfo_externallyDefinedInfo,
200             z_External},
201         {ODR_IMPLICIT, ODR_CONTEXT, 5, Z_OtherInfo_oid, odr_oid},
202         {-1, -1, -1, -1, 0}
203     };
204
205     if (!odr_sequence_begin(o, p, sizeof(**p)))
206         return opt && odr_ok(o);
207     return
208         odr_implicit(o, z_InfoCategory, &(*p)->category, ODR_CONTEXT, 1, 1) &&
209         odr_choice(o, arm, &(*p)->which, &(*p)->information) &&
210         odr_sequence_end(o);
211 }
212     
213 int z_OtherInformation(ODR o, Z_OtherInformation **p, int opt)
214 {
215     if (o->direction == ODR_DECODE)
216         *p = odr_malloc(o, sizeof(**p));
217     else if (!*p)
218         return opt;
219     odr_implicit_settag(o, ODR_CONTEXT, 201);
220     if (odr_sequence_of(o, z_OtherInformationUnit, &(*p)->list,
221         &(*p)->num_elements))
222         return 1;
223     *p = 0;
224     return opt && odr_ok(o);
225 }
226
227 int z_StringOrNumeric(ODR o, Z_StringOrNumeric **p, int opt)
228 {
229     static Odr_arm arm[] =
230     {
231         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_StringOrNumeric_string,
232             odr_visiblestring},
233         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_StringOrNumeric_numeric,
234             odr_integer},
235         {-1, -1, -1, -1, 0}
236     };
237
238     if (o->direction == ODR_DECODE)
239         *p = odr_malloc(o, sizeof(**p));
240     else if (!*p)
241         return opt;
242     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
243         return 1;
244     *p = 0;
245     return opt && odr_ok(o);
246 }
247
248 /*
249  * check tagging!!
250  */
251 int z_Unit(ODR o, Z_Unit **p, int opt)
252 {
253     if (!odr_sequence_begin(o, p, sizeof(**p)))
254         return opt && odr_ok(o);
255     return
256         odr_implicit(o, odr_visiblestring, &(*p)->unitSystem, ODR_CONTEXT,
257             1, 1) &&
258         odr_explicit(o, z_StringOrNumeric, &(*p)->unitType, ODR_CONTEXT,
259             2, 1) &&
260         odr_explicit(o, z_StringOrNumeric, &(*p)->unit, ODR_CONTEXT, 3, 1) &&
261         odr_implicit(o, odr_integer, &(*p)->scaleFactor, ODR_CONTEXT, 4, 1) &&
262         odr_sequence_end(o);
263 }
264
265 int z_IntUnit(ODR o, Z_IntUnit **p, int opt)
266 {
267     if (!odr_sequence_begin(o, p, sizeof(**p)))
268         return opt && odr_ok(o);
269     return
270         odr_implicit(o, odr_integer, &(*p)->value, ODR_CONTEXT, 1, 0) &&
271         odr_implicit(o, z_Unit, &(*p)->unitUsed, ODR_CONTEXT, 2, 0) &&
272         odr_sequence_end(o);
273 }
274
275 /* ---------------------- INITIALIZE SERVICE ------------------- */
276
277 #if 0
278 int z_NSRAuthentication(ODR o, Z_NSRAuthentication **p, int opt)
279 {
280     if (!odr_sequence_begin(o, p, sizeof(**p)))
281         return opt && odr_ok(o);
282     return
283         odr_visiblestring(o, &(*p)->user, 0) &&
284         odr_visiblestring(o, &(*p)->password, 0) &&
285         odr_visiblestring(o, &(*p)->account, 0) &&
286         odr_sequence_end(o);
287 }
288 #endif
289
290 int z_IdPass(ODR o, Z_IdPass **p, int opt)
291 {
292     if (!odr_sequence_begin(o, p, sizeof(**p)))
293         return opt && odr_ok(o);
294     return
295         odr_implicit(o, odr_visiblestring, &(*p)->groupId, ODR_CONTEXT, 0, 1) &&
296         odr_implicit(o, odr_visiblestring, &(*p)->userId, ODR_CONTEXT, 1, 1) &&
297         odr_implicit(o, odr_visiblestring, &(*p)->password, ODR_CONTEXT, 2,
298             1) &&
299         odr_sequence_end(o);
300 }
301
302 int z_StrAuthentication(ODR o, char **p, int opt)
303 {
304     return odr_visiblestring(o, p, opt);
305 }
306
307 int z_IdAuthentication(ODR o, Z_IdAuthentication **p, int opt)
308 {
309     static Odr_arm arm[] =
310     {
311         {-1, -1, -1, Z_IdAuthentication_open, z_StrAuthentication},
312         {-1, -1, -1, Z_IdAuthentication_idPass, z_IdPass},
313         {-1, -1, -1, Z_IdAuthentication_anonymous, odr_null},
314         {-1, -1, -1, Z_IdAuthentication_other, z_External},
315         {-1, -1, -1, -1, 0}
316     };
317
318     if (o->direction == ODR_DECODE)
319         *p = odr_malloc(o, sizeof(**p));
320     else if (!*p)
321         return opt;
322
323     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
324         return 1;
325     *p = 0;
326     return opt && odr_ok(o);
327 }
328
329 int z_InitRequest(ODR o, Z_InitRequest **p, int opt)
330 {
331     Z_InitRequest *pp;
332
333     if (!odr_sequence_begin(o, p, sizeof(**p)))
334         return opt && odr_ok(o);
335     pp = *p;
336     return
337         z_ReferenceId(o, &pp->referenceId, 1) &&
338         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
339             3, 0) &&
340         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
341         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
342             5, 0) &&
343         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
344             6, 0) &&
345         odr_explicit(o, z_IdAuthentication, &pp->idAuthentication, ODR_CONTEXT,
346             7, 1) &&
347         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
348             110, 1) &&
349         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
350             111, 1) &&
351         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
352             ODR_CONTEXT, 112, 1) &&
353         z_UserInformationField(o, &pp->userInformationField, 1) &&
354 #ifdef Z_95
355         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
356 #endif
357         odr_sequence_end(o);
358 }
359
360 int z_InitResponse(ODR o, Z_InitResponse **p, int opt)
361 {
362     Z_InitResponse *pp;
363
364     if (!odr_sequence_begin(o, p, sizeof(**p)))
365         return opt && odr_ok(o);
366     pp = *p;
367     return
368         z_ReferenceId(o, &pp->referenceId, 1) &&
369         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
370             3, 0) &&
371         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
372         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
373             5, 0) &&
374         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
375             6, 0) &&
376         odr_implicit(o, odr_bool, &pp->result, ODR_CONTEXT, 12, 0) &&
377         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
378             110, 1) &&
379         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
380             111, 1) &&
381         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
382             ODR_CONTEXT, 112, 1) &&
383         z_UserInformationField(o, &pp->userInformationField, 1) &&
384 #ifdef Z_95
385         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
386 #endif
387         odr_sequence_end(o);
388 }
389
390 /* ------------------ RESOURCE CONTROL ----------------*/
391
392 int z_TriggerResourceControlRequest(ODR o, Z_TriggerResourceControlRequest **p,
393                                     int opt)
394 {
395     if (!odr_sequence_begin(o, p, sizeof(**p)))
396         return opt && odr_ok(o);
397     return
398         z_ReferenceId(o, &(*p)->referenceId, 1) &&
399         odr_implicit(o, odr_integer, &(*p)->requestedAction, ODR_CONTEXT,
400             46, 0) &&
401         odr_implicit(o, odr_oid, &(*p)->prefResourceReportFormat,
402             ODR_CONTEXT, 47, 1) &&
403         odr_implicit(o, odr_bool, &(*p)->resultSetWanted, ODR_CONTEXT,
404             48, 1) &&
405 #ifdef Z_95
406         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
407 #endif
408         odr_sequence_end(o);
409 }
410
411 int z_ResourceControlRequest(ODR o, Z_ResourceControlRequest **p, int opt)
412 {
413     if (!odr_sequence_begin(o, p, sizeof(**p)))
414         return opt && odr_ok(o);
415     return
416         z_ReferenceId(o, &(*p)->referenceId, 1) &&
417         odr_implicit(o, odr_bool, &(*p)->suspendedFlag, ODR_CONTEXT, 39, 1)&&
418         odr_explicit(o, z_External, &(*p)->resourceReport, ODR_CONTEXT,
419             40, 1) &&
420         odr_implicit(o, odr_integer, &(*p)->partialResultsAvailable,
421             ODR_CONTEXT, 41, 1) &&
422         odr_implicit(o, odr_bool, &(*p)->responseRequired, ODR_CONTEXT,
423             42, 0) &&
424         odr_implicit(o, odr_bool, &(*p)->triggeredRequestFlag,
425             ODR_CONTEXT, 43, 1) &&
426 #ifdef Z_95
427         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
428 #endif
429         odr_sequence_end(o);
430 }
431
432 int z_ResourceControlResponse(ODR o, Z_ResourceControlResponse **p, int opt)
433 {
434     if (!odr_sequence_begin(o, p, sizeof(**p)))
435         return opt && odr_ok(o);
436     return
437         z_ReferenceId(o, &(*p)->referenceId, 1) &&
438         odr_implicit(o, odr_bool, &(*p)->continueFlag, ODR_CONTEXT, 44, 0) &&
439         odr_implicit(o, odr_bool, &(*p)->resultSetWanted, ODR_CONTEXT,
440             45, 1) &&
441 #ifdef Z_95
442         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
443 #endif
444         odr_sequence_end(o);
445 }
446
447 /* ------------------------ SEARCH SERVICE ----------------------- */
448
449 int z_DatabaseSpecificUnit(ODR o, Z_DatabaseSpecificUnit **p, int opt)
450 {
451     if (!odr_sequence_begin(o, p, sizeof(**p)))
452         return opt && odr_ok(o);
453     return
454         z_DatabaseName(o, &(*p)->databaseName, 0) &&
455         z_ElementSetName(o, &(*p)->elementSetName, 0) &&
456         odr_sequence_end(o);
457 }
458
459 int z_DatabaseSpecific(ODR o, Z_DatabaseSpecific **p, int opt)
460 {
461     if (o->direction == ODR_DECODE)
462         *p = odr_malloc(o, sizeof(**p));
463     else if (!*p)
464         return opt;
465
466     odr_implicit_settag(o, ODR_CONTEXT, 1);
467     if (odr_sequence_of(o, z_DatabaseSpecificUnit, &(*p)->elements,
468         &(*p)->num_elements))
469         return 1;
470     *p = 0;
471     return 0;
472 }
473
474 int z_ElementSetNames(ODR o, Z_ElementSetNames **p, int opt)
475 {
476     static Odr_arm arm[] =
477     {
478         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_ElementSetNames_generic,
479             z_ElementSetName},
480         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ElementSetNames_databaseSpecific,
481             z_DatabaseSpecific},
482         {-1, -1, -1, -1, 0}
483     };
484
485     if (o->direction == ODR_DECODE)
486         *p = odr_malloc(o, sizeof(**p));
487     else if (!*p)
488         return opt && odr_ok(o);
489
490     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
491         return 1;
492     *p = 0;
493     return 0;
494 }
495
496 /* ----------------------- RPN QUERY -----------------------*/
497
498 int z_ComplexAttribute(ODR o, Z_ComplexAttribute **p, int opt)
499 {
500     if (!odr_sequence_begin(o, p, sizeof(**p)))
501         return opt && odr_ok(o);
502     return
503         odr_implicit_settag(o, ODR_CONTEXT, 1) &&
504         odr_sequence_of(o, z_StringOrNumeric, &(*p)->list,
505             &(*p)->num_list) &&
506         odr_implicit_settag(o, ODR_CONTEXT, 2) &&
507         (odr_sequence_of(o, odr_integer, &(*p)->semanticAction,
508             &(*p)->num_semanticAction) || odr_ok(o)) &&
509         odr_sequence_end(o);
510 }
511
512 int z_AttributeElement(ODR o, Z_AttributeElement **p, int opt)
513 {
514 #ifdef Z_95
515     static Odr_arm arm[] =
516     {
517         {ODR_IMPLICIT, ODR_CONTEXT, 121, Z_AttributeValue_numeric,
518             odr_integer},
519         {ODR_IMPLICIT, ODR_CONTEXT, 224, Z_AttributeValue_complex,
520             z_ComplexAttribute},
521         {-1, -1, -1, -1, 0}
522     };
523 #endif
524
525     if (!odr_sequence_begin(o, p, sizeof(**p)))
526         return opt && odr_ok(o);
527     return
528 #ifdef Z_95
529         odr_implicit(o, odr_oid, &(*p)->attributeSet, ODR_CONTEXT, 1, 1) &&
530 #endif
531         odr_implicit(o, odr_integer, &(*p)->attributeType, ODR_CONTEXT,
532             120, 0) &&
533 #ifdef Z_95
534         odr_choice(o, arm, &(*p)->value, &(*p)->which) &&
535 #else
536         odr_implicit(o, odr_integer, &(*p)->attributeValue, ODR_CONTEXT,
537             121, 0) &&
538 #endif
539         odr_sequence_end(o);
540 }
541
542 int z_Term(ODR o, Z_Term **p, int opt)
543 {
544     static Odr_arm arm[] =
545     {
546         {ODR_IMPLICIT, ODR_CONTEXT, 45, Z_Term_general, odr_octetstring},
547         {ODR_IMPLICIT, ODR_CONTEXT, 215, Z_Term_numeric, odr_integer},
548         {ODR_IMPLICIT, ODR_CONTEXT, 216, Z_Term_characterString,
549             odr_visiblestring},
550         {ODR_IMPLICIT, ODR_CONTEXT, 217, Z_Term_oid, odr_oid},
551         {ODR_IMPLICIT, ODR_CONTEXT, 218, Z_Term_dateTime, odr_cstring},
552         {ODR_IMPLICIT, ODR_CONTEXT, 219, Z_Term_external, z_External},
553         /* add intUnit here */
554         {ODR_IMPLICIT, ODR_CONTEXT, 221, Z_Term_null, odr_null},
555         {-1, -1, -1, -1, 0}
556     };
557
558     if (o->direction ==ODR_DECODE)
559         *p = odr_malloc(o, sizeof(**p));
560     else if (!*p)
561         return opt;
562     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
563         return 1;
564     *p = 0;
565     return opt && odr_ok(o);
566 }
567
568 int z_AttributesPlusTerm(ODR o, Z_AttributesPlusTerm **p, int opt)
569 {
570     if (!(odr_implicit_settag(o, ODR_CONTEXT, 102) &&
571         odr_sequence_begin(o, p, sizeof(**p))))
572         return opt && odr_ok(o);
573     return
574         odr_implicit_settag(o, ODR_CONTEXT, 44) &&
575         odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
576             &(*p)->num_attributes) &&
577         z_Term(o, &(*p)->term, 0) &&
578         odr_sequence_end(o);
579 }
580
581 int z_ResultSetPlusAttributes(ODR o, Z_ResultSetPlusAttributes **p, int opt)
582 {
583     if (!(odr_implicit_settag(o, ODR_CONTEXT, 214) &&
584         odr_sequence_begin(o, p, sizeof(**p))))
585         return opt && odr_ok(o);
586     return
587         z_ResultSetId(o, &(*p)->resultSet, 0) &&
588         odr_implicit_settag(o, ODR_CONTEXT, 44) &&
589         odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
590             &(*p)->num_attributes) &&
591         odr_sequence_end(o);
592 }
593
594 int z_ProximityOperator(ODR o, Z_ProximityOperator **p, int opt)
595 {
596     static Odr_arm arm[] =
597     {
598         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ProxCode_known, odr_integer},
599         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ProxCode_private, odr_integer},
600         {-1, -1, -1, -1, 0}
601     };
602
603     if (!odr_sequence_begin(o, p, sizeof(**p)))
604         return opt && odr_ok(o);
605     return
606         odr_implicit(o, odr_bool, &(*p)->exclusion, ODR_CONTEXT, 1, 1) &&
607         odr_implicit(o, odr_integer, &(*p)->distance, ODR_CONTEXT, 2, 0) &&
608         odr_implicit(o, odr_bool, &(*p)->ordered, ODR_CONTEXT, 3, 0) &&
609         odr_implicit(o, odr_integer, &(*p)->relationType, ODR_CONTEXT, 4, 0) &&
610         odr_constructed_begin(o, &(*p)->proximityUnitCode, ODR_CONTEXT, 5) &&
611         odr_choice(o, arm, &(*p)->proximityUnitCode, &(*p)->which) &&
612         odr_constructed_end(o) &&
613         odr_sequence_end(o);
614 }
615
616 int z_Operator(ODR o, Z_Operator **p, int opt)
617 {
618     static Odr_arm arm[] =
619     {
620         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_Operator_and, odr_null},
621         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Operator_or, odr_null},
622         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_Operator_and_not, odr_null},
623         {ODR_IMPLICIT, ODR_CONTEXT, 3, Z_Operator_prox, z_ProximityOperator},
624         {-1, -1, -1, -1, 0}
625     };
626
627     if (!*p && o->direction != ODR_DECODE)
628         return opt;
629     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 46))
630         return opt && odr_ok(o);
631     if (o->direction == ODR_DECODE)
632         *p = odr_malloc(o, sizeof(**p));
633
634     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
635         odr_constructed_end(o))
636         return 1;
637     *p = 0;
638     return opt && odr_ok(o);
639 }
640
641 int z_Operand(ODR o, Z_Operand **p, int opt)
642 {
643     static Odr_arm arm[] =
644     {
645         {-1, -1, -1, Z_Operand_APT, z_AttributesPlusTerm},
646         {-1, -1, -1, Z_Operand_resultSetId, z_ResultSetId},
647         {-1, -1, -1, Z_Operand_resultAttr, z_ResultSetPlusAttributes},
648         {-1, -1, -1, -1, 0}
649     };
650
651     if (o->direction == ODR_DECODE)
652         *p = odr_malloc(o, sizeof(**p));
653     else if (!*p)
654         return opt;
655     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
656         return 1;
657     *p = 0;
658     return opt && odr_ok(o);
659 }
660
661 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt);
662
663 int z_Complex(ODR o, Z_Complex **p, int opt)
664 {
665     if (!odr_sequence_begin(o, p, sizeof(**p)))
666         return opt && odr_ok(o);
667     return
668         z_RPNStructure(o, &(*p)->s1, 0) &&
669         z_RPNStructure(o, &(*p)->s2, 0) &&
670         z_Operator(o, &(*p)->roperator, 0) &&
671         odr_sequence_end(o);
672 }
673
674 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt)
675 {
676     static Odr_arm arm[] = 
677     {
678         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_RPNStructure_simple, z_Operand},
679         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_RPNStructure_complex, z_Complex},
680         {-1 -1, -1, -1, 0}
681     };
682
683     if (o->direction == ODR_DECODE)
684         *p = odr_malloc(o, sizeof(**p));
685     else if (!*p)
686         return opt;
687     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
688         return 1;
689     *p = 0;
690     return opt && odr_ok(o);
691 }
692
693 int z_RPNQuery(ODR o, Z_RPNQuery **p, int opt)
694 {
695     if (!odr_sequence_begin(o, p, sizeof(**p)))
696         return opt && odr_ok(o);
697     return
698         odr_oid(o, &(*p)->attributeSetId, 0) &&
699         z_RPNStructure(o, &(*p)->RPNStructure, 0) &&
700         odr_sequence_end(o);
701 }
702
703 /* -----------------------END RPN QUERY ----------------------- */
704
705 int z_Query(ODR o, Z_Query **p, int opt)
706 {
707     static Odr_arm arm[] = 
708     {
709         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Query_type_1, z_RPNQuery},
710         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_Query_type_2, odr_octetstring},
711         {ODR_EXPLICIT, ODR_CONTEXT, 101, Z_Query_type_101, z_RPNQuery},
712         {-1, -1, -1, -1, 0}
713     };
714
715     if (o->direction == ODR_DECODE)
716         *p = odr_malloc(o, sizeof(**p));
717     else if (!*p)
718         return opt;
719     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
720         return 1;
721     *p = 0;
722     return opt && odr_ok(o);
723 }
724
725 int z_SearchRequest(ODR o, Z_SearchRequest **p, int opt)
726 {
727     Z_SearchRequest *pp;
728
729     if (!odr_sequence_begin(o, p, sizeof(**p)))
730         return opt && odr_ok(o);
731     pp = *p;
732     return
733         z_ReferenceId(o, &pp->referenceId, 1) &&
734         odr_implicit(o, odr_integer, &pp->smallSetUpperBound, ODR_CONTEXT,
735             13, 0) &&
736         odr_implicit(o, odr_integer, &pp->largeSetLowerBound, ODR_CONTEXT,
737             14, 0) &&
738         odr_implicit(o, odr_integer, &pp->mediumSetPresentNumber, ODR_CONTEXT,
739             15, 0) &&
740         odr_implicit(o, odr_bool, &pp->replaceIndicator, ODR_CONTEXT, 16, 1) &&
741         odr_implicit(o, odr_visiblestring, &pp->resultSetName, ODR_CONTEXT,
742             17, 9) &&
743         odr_implicit_settag(o, ODR_CONTEXT, 18) &&
744         odr_sequence_of(o, z_DatabaseName, &pp->databaseNames,
745             &pp->num_databaseNames) &&
746         odr_explicit(o, z_ElementSetNames, &pp->smallSetElementSetNames,
747             ODR_CONTEXT, 100, 1) &&
748         odr_explicit(o, z_ElementSetNames, &pp->mediumSetElementSetNames,
749             ODR_CONTEXT, 101, 1) &&
750         odr_implicit(o, odr_oid, &pp->preferredRecordSyntax,
751             ODR_CONTEXT, 104, 1) &&
752         odr_explicit(o, z_Query, &pp->query, ODR_CONTEXT, 21, 0) &&
753 #ifdef Z_95
754         odr_implicit(o, z_OtherInformation, &(*p)->additionalSearchInfo,
755             ODR_CONTEXT, 203, 1) &&
756         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
757 #endif
758         odr_sequence_end(o);
759 }
760
761 /* ------------------------ RECORD ------------------------- */
762
763 int z_DatabaseRecord(ODR o, Z_DatabaseRecord **p, int opt)
764 {
765     return z_External(o, (Z_External **) p, opt);
766 }
767
768 #ifdef Z_95
769
770 int z_DefaultDiagFormat(ODR o, Z_DefaultDiagFormat **p, int opt)
771 {
772     static Odr_arm arm[] =
773     {
774         {-1, -1, -1, Z_DiagForm_v2AddInfo, odr_visiblestring},
775         {-1, -1, -1, Z_DiagForm_v3AddInfo, z_InternationalString},
776         {ODR_IMPLICIT, ODR_CONTEXT, ODR_VISIBLESTRING, Z_DiagForm_v2AddInfo,
777             odr_visiblestring}, /* To cater to a bug in the CNIDR servers */
778         {-1, -1, -1, -1, 0}
779     };
780     if (!odr_sequence_begin(o, p, sizeof(**p)))
781         return opt && odr_ok(o);
782     return
783         odr_oid(o, &(*p)->diagnosticSetId, 1) && /* SHOULD NOT BE OPT! */
784         odr_integer(o, &(*p)->condition, 0) &&
785         /*
786          * I no longer recall what server tagged the addinfo.. but it isn't
787          * hurting anyone, so...
788          * We need to turn it into a choice, or something, because of
789          * that damn generalstring in v3.
790          */
791         odr_choice(o, arm, &(*p)->addinfo, &(*p)->which) &&
792         odr_sequence_end(o);
793 }
794
795 int z_DiagRec(ODR o, Z_DiagRec **p, int opt)
796 {
797     static Odr_arm arm[] = 
798     {
799         {-1, -1, -1, Z_DiagRec_defaultFormat, z_DefaultDiagFormat},
800         {-1, -1, -1, Z_DiagRec_externallyDefined, z_External},
801         {-1, -1, -1, -1, 0}
802     };
803
804     if (o->direction == ODR_DECODE)
805         *p = odr_malloc(o, sizeof(**p));
806     else if (!*p)
807         return opt;
808     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
809         return 1;
810     *p = 0;
811     return opt && odr_ok(o);
812 }
813
814 #else
815
816 int z_DiagRec(ODR o, Z_DiagRec **p, int opt)
817 {
818     if (!odr_sequence_begin(o, p, sizeof(**p)))
819         return opt && odr_ok(o);
820     return
821         odr_oid(o, &(*p)->diagnosticSetId, 1) && /* SHOULD NOT BE OPT! */
822         odr_integer(o, &(*p)->condition, 0) &&
823         /*
824          * I no longer recall what server tagged the addinfo.. but it isn't
825          * hurting anyone, so...
826          * We need to turn it into a choice, or something, because of
827          * that damn generalstring in v3.
828          */
829         (odr_visiblestring(o, &(*p)->addinfo, 0) ||
830             odr_implicit(o, odr_cstring, &(*p)->addinfo, ODR_CONTEXT,
831             ODR_VISIBLESTRING, 1)) &&
832         odr_sequence_end(o);
833 }
834
835 #endif
836
837 int z_DiagRecs(ODR o, Z_DiagRecs **p, int opt)
838 {
839     if (o->direction == ODR_DECODE)
840         *p = odr_malloc(o, sizeof(**p));
841     else if (!*p)
842         return opt;
843
844         if (odr_sequence_of(o, z_DiagRec, &(*p)->diagRecs,
845         &(*p)->num_diagRecs))
846         return 1;
847     *p = 0;
848     return 0;
849 }
850
851 int z_NamePlusRecord(ODR o, Z_NamePlusRecord **p, int opt)
852 {
853     static Odr_arm arm[] =
854     {
855         {ODR_EXPLICIT, ODR_CONTEXT, 1, Z_NamePlusRecord_databaseRecord,
856             z_DatabaseRecord},
857         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_NamePlusRecord_surrogateDiagnostic,
858             z_DiagRec},
859         {-1, -1, -1, -1, 0}
860     };
861
862     if (!odr_sequence_begin(o, p, sizeof(**p)))
863         return opt && odr_ok(o);
864     return
865         odr_implicit(o, z_DatabaseName, &(*p)->databaseName, ODR_CONTEXT,
866             0, 1) &&
867         odr_constructed_begin(o, &(*p)->u, ODR_CONTEXT, 1) &&
868         odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
869         odr_constructed_end(o) &&
870         odr_sequence_end(o);
871 }
872
873 int z_NamePlusRecordList(ODR o, Z_NamePlusRecordList **p, int opt)
874 {
875     if (o->direction == ODR_DECODE)
876         *p = odr_malloc(o, sizeof(**p));
877     else if (!*p)
878         return opt;
879     if (odr_sequence_of(o, z_NamePlusRecord, &(*p)->records,
880         &(*p)->num_records))
881         return 1;
882     *p = 0;
883     return 0;
884 }
885
886 int z_Records(ODR o, Z_Records **p, int opt)
887 {
888     static Odr_arm arm[] = 
889     {
890         {ODR_IMPLICIT, ODR_CONTEXT, 28, Z_Records_DBOSD, z_NamePlusRecordList},
891         {ODR_IMPLICIT, ODR_CONTEXT, 130, Z_Records_NSD, z_DiagRec},
892         {ODR_IMPLICIT, ODR_CONTEXT, 205, Z_Records_multipleNSD,
893             z_DiagRecs},
894         {-1, -1, -1, -1, 0}
895     };
896
897     if (o->direction == ODR_DECODE)
898         *p = odr_malloc(o, sizeof(**p));
899     else if (!*p)
900         return opt;
901     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
902         return 1;
903     *p = 0;
904     return opt && odr_ok(o);
905 }
906
907 /* ------------------------ ACCESS CTRL SERVICE ----------------------- */
908
909 int z_AccessControlRequest(ODR o, Z_AccessControlRequest **p, int opt)
910 {
911     static Odr_arm arm[] = 
912     {
913         {ODR_IMPLICIT, ODR_CONTEXT, 37, Z_AccessRequest_simpleForm,
914             odr_octetstring},
915         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_AccessRequest_externallyDefined,
916             z_External},
917         {-1, -1, -1, -1, 0}
918     };
919     if (!odr_sequence_begin(o, p, sizeof(**p)))
920         return opt && odr_ok(o);
921     return
922         z_ReferenceId(o, &(*p)->referenceId, 1) &&
923         odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
924 #ifdef Z_95
925         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
926 #endif
927         odr_sequence_end(o);
928 }
929
930 int z_AccessControlResponse(ODR o, Z_AccessControlResponse **p, int opt)
931 {
932     static Odr_arm arm[] = 
933     {
934         {ODR_IMPLICIT, ODR_CONTEXT, 38, Z_AccessResponse_simpleForm,
935             odr_octetstring},
936         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_AccessResponse_externallyDefined,
937             z_External},
938         {-1, -1, -1, -1, 0}
939     };
940     if (!odr_sequence_begin(o, p, sizeof(**p)))
941         return opt && odr_ok(o);
942     return
943         z_ReferenceId(o, &(*p)->referenceId, 1) &&
944         odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
945         odr_explicit(o, z_DiagRec, &(*p)->diagnostic, ODR_CONTEXT, 223, 1) &&
946 #ifdef Z_95
947         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
948 #endif
949         odr_sequence_end(o);
950 }
951
952 /* ------------------------ SCAN SERVICE -------------------- */
953
954 int z_AttributeList(ODR o, Z_AttributeList **p, int opt)
955 {
956     if (o->direction == ODR_DECODE)
957         *p = odr_malloc(o, sizeof(**p));
958     else if (!*p)
959         return opt;
960
961     odr_implicit_settag(o, ODR_CONTEXT, 44);
962     if (odr_sequence_of(o, z_AttributeElement, &(*p)->attributes,
963         &(*p)->num_attributes))
964         return 1;
965     *p = 0;
966     return opt && odr_ok(o);
967 }
968
969 /*
970  * This is a temporary hack. We don't know just *what* old version of the
971  * protocol willow uses, so we'll just patiently wait for them to update
972  */
973 static int willow_scan = 0;
974
975 int z_WillowAttributesPlusTerm(ODR o, Z_AttributesPlusTerm **p, int opt)
976 {
977     if (!*p && o->direction != ODR_DECODE)
978         return opt;
979     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 4))
980     {
981         o->t_class = -1;
982         return opt && odr_ok(o);
983     }
984     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 1))
985         return 0;
986     if (!odr_constructed_begin(o, p, ODR_UNIVERSAL, ODR_SEQUENCE))
987         return 0;
988     if (!odr_implicit_settag(o, ODR_CONTEXT, 44))
989         return 0;
990     if (o->direction == ODR_DECODE)
991         *p = odr_malloc(o, sizeof(**p));
992     if (!odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
993         &(*p)->num_attributes))
994         return 0;
995     if (!odr_sequence_end(o) || !odr_sequence_end(o))
996         return 0;
997     if (!z_Term(o, &(*p)->term, 0))
998         return 0;
999     if (!odr_constructed_end(o))
1000         return 0;
1001     willow_scan = 1;
1002     return 1;
1003 }
1004
1005 int z_AlternativeTerm(ODR o, Z_AlternativeTerm **p, int opt)
1006 {
1007     if (o->direction == ODR_DECODE)
1008         *p = odr_malloc(o, sizeof(**p));
1009     else if (!*p)
1010     {
1011         o->t_class = -1;
1012         return opt && odr_ok(o);
1013     }
1014
1015     if (odr_sequence_of(o, z_AttributesPlusTerm, &(*p)->terms,
1016         &(*p)->num_terms))
1017         return 1;
1018     *p = 0;
1019     return opt && !o->error;
1020 }
1021
1022 int z_OccurrenceByAttributes(ODR o, Z_OccurrenceByAttributes **p, int opt)
1023 {
1024     if (!odr_sequence_begin(o, p, sizeof(**p)))
1025         return opt && odr_ok(o);
1026     return
1027         odr_explicit(o, z_AttributeList, &(*p)->attributes, ODR_CONTEXT, 1, 1)&&
1028         odr_explicit(o, odr_integer, &(*p)->global, ODR_CONTEXT, 2, 1) &&
1029         odr_sequence_end(o);
1030 }
1031
1032 int z_TermInfo(ODR o, Z_TermInfo **p, int opt)
1033 {
1034     if (!odr_sequence_begin(o, p, sizeof(**p)))
1035         return opt && odr_ok(o);
1036     return
1037         (willow_scan ? 
1038             odr_implicit(o, z_Term, &(*p)->term, ODR_CONTEXT, 1, 0) :
1039             z_Term(o, &(*p)->term, 0)) &&
1040         z_AttributeList(o, &(*p)->suggestedAttributes, 1) &&
1041         odr_implicit(o, z_AlternativeTerm, &(*p)->alternativeTerm,
1042             ODR_CONTEXT, 4, 1) &&
1043         odr_implicit(o, odr_integer, &(*p)->globalOccurrences, ODR_CONTEXT,
1044             2, 1) &&
1045         odr_implicit(o, z_OccurrenceByAttributes, &(*p)->byAttributes,
1046             ODR_CONTEXT, 3, 1) &&
1047         odr_sequence_end(o);
1048 }
1049
1050 int z_Entry(ODR o, Z_Entry **p, int opt)
1051 {
1052     static Odr_arm arm[] =
1053     {
1054         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Entry_termInfo, z_TermInfo},
1055         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_Entry_surrogateDiagnostic,
1056             z_DiagRec},
1057         {-1, -1, -1, -1, 0}
1058     };
1059
1060     if (o->direction == ODR_DECODE)
1061         *p = odr_malloc(o, sizeof(**p));
1062     else if (!*p)
1063         return opt;
1064
1065     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
1066         return 1;
1067     *p = 0;
1068     return opt && odr_ok(o);
1069 }
1070
1071 int z_Entries(ODR o, Z_Entries **p, int opt)
1072 {
1073     if (o->direction == ODR_DECODE)
1074         *p = odr_malloc(o, sizeof(**p));
1075     else if (!*p)
1076         return opt;
1077
1078     if (odr_sequence_of(o, z_Entry, &(*p)->entries,
1079         &(*p)->num_entries))
1080         return 1;
1081     *p = 0;
1082     return 0;
1083 }
1084
1085 int z_ListEntries(ODR o, Z_ListEntries **p, int opt)
1086 {
1087     static Odr_arm arm[] =
1088     {
1089         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ListEntries_entries, z_Entries},
1090         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_ListEntries_nonSurrogateDiagnostics,
1091             z_DiagRecs},
1092         {-1, -1, -1, -1, 0}
1093     };
1094
1095     if (o->direction == ODR_DECODE)
1096         *p = odr_malloc(o, sizeof(**p));
1097     else if (!*p)
1098         return opt;
1099
1100     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
1101         return 1;
1102     *p = 0;
1103     return opt && odr_ok(o);
1104 }
1105
1106 int z_ScanRequest(ODR o, Z_ScanRequest **p, int opt)
1107 {
1108     if (!odr_sequence_begin(o, p, sizeof(**p)))
1109         return opt && odr_ok(o);
1110     willow_scan = 0;
1111     return
1112         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1113         odr_implicit_settag(o, ODR_CONTEXT, 3) &&
1114         odr_sequence_of(o, z_DatabaseName, &(*p)->databaseNames,
1115             &(*p)->num_databaseNames) &&
1116         odr_oid(o, &(*p)->attributeSet, 1) &&
1117         (z_AttributesPlusTerm(o, &(*p)->termListAndStartPoint, 1) ?
1118             ((*p)->termListAndStartPoint ? 1 : 
1119         z_WillowAttributesPlusTerm(o, &(*p)->termListAndStartPoint, 0)) : 0) &&
1120         odr_implicit(o, odr_integer, &(*p)->stepSize, ODR_CONTEXT, 5, 1) &&
1121         odr_implicit(o, odr_integer, &(*p)->numberOfTermsRequested,
1122             ODR_CONTEXT, 6, 0) &&
1123         odr_implicit(o, odr_integer, &(*p)->preferredPositionInResponse,
1124             ODR_CONTEXT, 7, 1) &&
1125         odr_sequence_end(o);
1126 }
1127
1128 int z_ScanResponse(ODR o, Z_ScanResponse **p, int opt)
1129 {
1130     if (!odr_sequence_begin(o, p, sizeof(**p)))
1131         return opt && odr_ok(o);
1132     return
1133         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1134         odr_implicit(o, odr_integer, &(*p)->stepSize, ODR_CONTEXT, 3, 1) &&
1135         odr_implicit(o, odr_integer, &(*p)->scanStatus, ODR_CONTEXT, 4, 0) &&
1136         odr_implicit(o, odr_integer, &(*p)->numberOfEntriesReturned,
1137             ODR_CONTEXT, 5, 0) &&
1138         odr_implicit(o, odr_integer, &(*p)->positionOfTerm, ODR_CONTEXT, 6, 1)&&
1139         odr_explicit(o, z_ListEntries, &(*p)->entries, ODR_CONTEXT, 7, 1) &&
1140         odr_implicit(o, odr_oid, &(*p)->attributeSet, ODR_CONTEXT, 8, 1) &&
1141         odr_sequence_end(o);
1142 }
1143
1144 /* ------------------------ SEARCHRESPONSE ----------------*/
1145
1146 int z_NumberOfRecordsReturned(ODR o, int **p, int opt)
1147 {
1148     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 24, opt);
1149 }
1150
1151 int z_NextResultSetPosition(ODR o, int **p, int opt)
1152 {
1153     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 25, opt);
1154 }
1155
1156 int z_PresentStatus(ODR o, int **p, int opt)
1157 {
1158     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 27, opt);
1159 }
1160
1161 int z_SearchResponse(ODR o, Z_SearchResponse **p, int opt)
1162 {
1163     Z_SearchResponse *pp;
1164
1165     if (!odr_sequence_begin(o, p, sizeof(**p)))
1166         return opt && odr_ok(o);
1167     pp = *p;
1168     return
1169         z_ReferenceId(o, &pp->referenceId, 1) &&
1170         odr_implicit(o, odr_integer, &pp->resultCount, ODR_CONTEXT, 23, 0) &&
1171         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
1172         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
1173         odr_implicit(o, odr_bool, &pp->searchStatus, ODR_CONTEXT, 22, 0) &&
1174         odr_implicit(o, odr_integer, &pp->resultSetStatus, ODR_CONTEXT, 26,
1175             1) &&
1176         z_PresentStatus(o, &pp->presentStatus, 1) &&
1177         z_Records(o, &pp->records, 1) &&
1178 #ifdef Z_95
1179         odr_implicit(o, z_OtherInformation, &(*p)->additionalSearchInfo,
1180             ODR_CONTEXT, 203, 1) &&
1181         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1182 #endif
1183         odr_sequence_end(o);
1184 }
1185
1186 /* --------------------- PRESENT SERVICE ---------------------- */
1187
1188 int z_ElementSpec(ODR o, Z_ElementSpec **p, int opt)
1189 {
1190     static Odr_arm arm[] =
1191     {
1192         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ElementSpec_elementSetName,
1193             odr_visiblestring},
1194         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_ElementSpec_externalSpec,
1195             z_External},
1196         {-1, -1, -1, -1, 0}
1197     };
1198
1199     if (o->direction == ODR_DECODE)
1200         *p = odr_malloc(o, sizeof(**p));
1201     else if (!*p)
1202         return opt;
1203
1204     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
1205         return 1;
1206     *p = 0;
1207     return opt && odr_ok(o);
1208 }
1209
1210 int z_Specification(ODR o, Z_Specification **p, int opt)
1211 {
1212     if (!odr_sequence_begin(o, p, sizeof(**p)))
1213         return opt && odr_ok(o);
1214     return
1215         odr_implicit(o, odr_oid, &(*p)->schema, ODR_CONTEXT, 1, 1) &&
1216         z_ElementSpec(o, &(*p)->elementSpec, 1) &&
1217         odr_sequence_end(o);
1218 }
1219
1220 int z_DbSpecific(ODR o, Z_DbSpecific **p, int opt)
1221 {
1222     if (!odr_sequence_begin(o, p, sizeof(**p)))
1223         return opt && odr_ok(o);
1224     return
1225         odr_explicit(o, z_DatabaseName, &(*p)->databaseName, ODR_CONTEXT,
1226             1, 0) &&
1227         odr_implicit(o, z_Specification, &(*p)->spec, ODR_CONTEXT, 2, 0) &&
1228         odr_sequence_end(o);
1229 }
1230
1231 int z_CompSpec(ODR o, Z_CompSpec **p, int opt)
1232 {
1233     if (!odr_sequence_begin(o, p, sizeof(**p)))
1234         return opt && odr_ok(o);
1235     return
1236         odr_implicit(o, odr_bool, &(*p)->selectAlternativeSyntax, ODR_CONTEXT,
1237             1, 0) &&
1238         odr_implicit(o, z_Specification, &(*p)->generic, ODR_CONTEXT, 2, 1) &&
1239         odr_implicit_settag(o, ODR_CONTEXT, 3) &&
1240         (odr_sequence_of(o, z_DbSpecific, &(*p)->dbSpecific,
1241             &(*p)->num_dbSpecific) || odr_ok(o)) &&
1242         odr_implicit_settag(o, ODR_CONTEXT, 4) &&
1243         (odr_sequence_of(o, odr_oid, &(*p)->recordSyntax,
1244             &(*p)->num_recordSyntax) || odr_ok(o)) &&
1245         odr_sequence_end(o);
1246 }
1247
1248 int z_RecordComposition(ODR o, Z_RecordComposition **p, int opt)
1249 {
1250     static Odr_arm arm[] =
1251     {
1252         {ODR_EXPLICIT, ODR_CONTEXT, 19, Z_RecordComp_simple,
1253             z_ElementSetNames},
1254         {ODR_IMPLICIT, ODR_CONTEXT, 209, Z_RecordComp_complex,
1255             z_CompSpec},
1256         {-1, -1, -1, -1, 0}
1257     };
1258
1259     if (o->direction == ODR_DECODE)
1260         *p = odr_malloc(o, sizeof(**p));
1261     else if (!*p)
1262         return opt;
1263
1264     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
1265         return 1;
1266     *p = 0;
1267     return opt && odr_ok(o);
1268 }
1269
1270 int z_Range(ODR o, Z_Range **p, int opt)
1271 {
1272     if (!odr_sequence_begin(o, p, sizeof(**p)))
1273         return opt && odr_ok(o);
1274     return
1275         odr_implicit(o, odr_integer, &(*p)->startingPosition, ODR_CONTEXT,
1276             1, 0) &&
1277         odr_implicit(o, odr_integer, &(*p)->numberOfRecords, ODR_CONTEXT,
1278             2, 0) &&
1279         odr_sequence_end(o);
1280 }
1281
1282 int z_PresentRequest(ODR o, Z_PresentRequest **p, int opt)
1283 {
1284     Z_PresentRequest *pp;
1285
1286     if (!odr_sequence_begin(o, p, sizeof(**p)))
1287         return opt && odr_ok(o);
1288     pp = *p;
1289     return
1290         z_ReferenceId(o, &pp->referenceId, 1) &&
1291         z_ResultSetId(o, &pp->resultSetId, 0) &&
1292         odr_implicit(o, odr_integer, &pp->resultSetStartPoint, ODR_CONTEXT,
1293             30, 0) &&
1294         odr_implicit(o, odr_integer, &pp->numberOfRecordsRequested, ODR_CONTEXT,
1295             29, 0) &&
1296 #ifdef Z_95
1297         odr_implicit_settag(o, ODR_CONTEXT, 212) &&
1298         (odr_sequence_of(o, z_Range, &(*p)->additionalRanges,
1299             &(*p)->num_ranges) || odr_ok(o)) &&
1300         z_RecordComposition(o, &(*p)->recordComposition, 1) &&
1301 #else
1302         odr_explicit(o, z_ElementSetNames, &pp->elementSetNames, ODR_CONTEXT,
1303             19, 1) &&
1304 #endif
1305         odr_implicit(o, odr_oid, &(*p)->preferredRecordSyntax, ODR_CONTEXT,
1306             104, 1) &&
1307 #ifdef Z_95
1308         odr_implicit(o, odr_integer, &(*p)->maxSegmentCount, ODR_CONTEXT,
1309             204, 1) &&
1310         odr_implicit(o, odr_integer, &(*p)->maxRecordSize, ODR_CONTEXT,
1311             206, 1) &&
1312         odr_implicit(o, odr_integer, &(*p)->maxSegmentSize, ODR_CONTEXT,
1313             207, 1) &&
1314         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1315 #endif
1316         odr_sequence_end(o);
1317 }
1318
1319 int z_PresentResponse(ODR o, Z_PresentResponse **p, int opt)
1320 {
1321     Z_PresentResponse *pp;
1322
1323     if (!odr_sequence_begin(o, p, sizeof(**p)))
1324         return opt && odr_ok(o);
1325     pp = *p;
1326     return
1327         z_ReferenceId(o, &pp->referenceId, 1) &&
1328         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
1329         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
1330         z_PresentStatus(o, &pp->presentStatus, 0) &&
1331         z_Records(o, &pp->records, 1) &&
1332 #ifdef Z_95
1333         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1334 #endif
1335         odr_sequence_end(o);
1336 }
1337
1338 /* ----------------------DELETE -------------------------- */
1339
1340 int z_DeleteSetStatus(ODR o, int **p, int opt)
1341 {
1342     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 33, opt);
1343 }
1344
1345 int z_ListStatus(ODR o, Z_ListStatus **p, int opt)
1346 {
1347     if (!odr_sequence_begin(o, p, sizeof(**p)))
1348         return opt && odr_ok(o);
1349     return
1350         z_ResultSetId(o, &(*p)->id, 0) &&
1351         z_DeleteSetStatus(o, &(*p)->status, 0) &&
1352         odr_sequence_end(o);
1353 }
1354
1355 int z_DeleteResultSetRequest(ODR o, Z_DeleteResultSetRequest **p, int opt)
1356 {
1357     if (!odr_sequence_begin(o, p, sizeof(**p)))
1358         return opt && odr_ok(o);
1359     return
1360         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1361         odr_implicit(o, odr_integer, &(*p)->deleteFunction, ODR_CONTEXT, 32,
1362             0) &&
1363         (odr_sequence_of(o, z_ListStatus, &(*p)->resultSetList,
1364             &(*p)->num_ids) || odr_ok(o)) &&
1365 #ifdef Z_95
1366         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1367 #endif
1368         odr_sequence_end(o);
1369 }
1370
1371 int z_DeleteResultSetResponse(ODR o, Z_DeleteResultSetResponse **p, int opt)
1372 {
1373     if (!odr_sequence_begin(o, p, sizeof(**p)))
1374         return opt && odr_ok(o);
1375     return
1376         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1377         odr_implicit(o, z_DeleteSetStatus, &(*p)->deleteOperationStatus,
1378             ODR_CONTEXT, 0, 1) &&
1379         odr_implicit_settag(o, ODR_CONTEXT, 1) &&
1380         (odr_sequence_of(o, z_ListStatus, &(*p)->deleteListStatuses,
1381             &(*p)->num_statuses) || odr_ok(o)) &&
1382         odr_implicit(o, odr_integer, &(*p)->numberNotDeleted, ODR_CONTEXT,
1383             34, 1) &&
1384         odr_implicit_settag(o, ODR_CONTEXT, 35) &&
1385         (odr_sequence_of(o, z_ListStatus, &(*p)->bulkStatuses,
1386             &(*p)->num_bulkStatuses) || odr_ok(o)) &&
1387         odr_implicit(o, odr_visiblestring, &(*p)->deleteMessage, ODR_CONTEXT,
1388             36, 1) &&
1389 #ifdef Z_95
1390         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1391 #endif
1392         odr_sequence_end(o);
1393 }
1394
1395 /* ------------------------ SEGMENT SERVICE -------------- */
1396
1397 int z_Segment(ODR o, Z_Segment **p, int opt)
1398 {
1399     if (!odr_sequence_begin(o, p, sizeof(**p)))
1400         return opt && odr_ok(o);
1401     return
1402         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1403         odr_implicit(o, odr_integer, &(*p)->numberOfRecordsReturned,
1404             ODR_CONTEXT, 24, 0) &&
1405         odr_implicit_settag(o, ODR_CONTEXT, 0) &&
1406         odr_sequence_of(o, z_NamePlusRecord, &(*p)->segmentRecords,
1407             &(*p)->num_segmentRecords) &&
1408         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1409         odr_sequence_end(o);
1410 }
1411
1412 /* ------------------------ CLOSE SERVICE ---------------- */
1413
1414 int z_Close(ODR o, Z_Close **p, int opt)
1415 {
1416     if (!odr_sequence_begin(o, p, sizeof(**p)))
1417         return opt && odr_ok(o);
1418     return
1419         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1420         odr_implicit(o, odr_integer, &(*p)->closeReason, ODR_CONTEXT, 211, 0) &&
1421         odr_implicit(o, odr_visiblestring, &(*p)->diagnosticInformation,
1422             ODR_CONTEXT, 3, 1) &&
1423         odr_implicit(o, odr_oid, &(*p)->resourceReportFormat, ODR_CONTEXT,
1424             4, 1) &&
1425         odr_implicit(o, z_External, &(*p)->resourceReport, ODR_CONTEXT,
1426             5, 1) &&
1427 #ifdef Z_95
1428         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1429 #endif
1430         odr_sequence_end(o);
1431 }
1432
1433 /* ------------------------ APDU ------------------------- */
1434
1435 int z_Permissions(ODR o, Z_Permissions **p, int opt)
1436 {
1437     if (!odr_sequence_begin(o, p, sizeof(**p)))
1438         return opt && odr_ok(o);
1439     return
1440         odr_implicit(o, z_InternationalString, &(*p)->userId, ODR_CONTEXT,
1441             1, 0) &&
1442         odr_implicit_settag(o, ODR_CONTEXT, 2) &&
1443         odr_sequence_of(o, odr_integer, &(*p)->allowableFunctions,
1444             &(*p)->num_allowableFunctions) &&
1445         odr_sequence_end(o);
1446 }
1447
1448 int z_ExtendedServicesRequest(ODR o, Z_ExtendedServicesRequest **p, int opt)
1449 {
1450     if (!odr_sequence_begin(o, p, sizeof(**p)))
1451         return opt && odr_ok(o);
1452     return
1453         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1454         odr_implicit(o, odr_integer, &(*p)->function, ODR_CONTEXT, 3, 0) &&
1455         odr_implicit(o, odr_oid, &(*p)->packageType, ODR_CONTEXT, 4, 0) &&
1456         odr_implicit(o, z_InternationalString, &(*p)->packageName, ODR_CONTEXT, 5, 1) &&
1457         odr_implicit(o, z_InternationalString, &(*p)->userId, ODR_CONTEXT, 6, 1) &&
1458         odr_implicit(o, z_IntUnit, &(*p)->retentionTime, ODR_CONTEXT, 7, 1) &&
1459         odr_implicit(o, z_Permissions, &(*p)->permissions, ODR_CONTEXT, 8, 1) &&
1460         odr_implicit(o, z_InternationalString, &(*p)->description, ODR_CONTEXT, 9, 1) &&
1461         odr_implicit(o, z_External, &(*p)->taskSpecificParameters, ODR_CONTEXT, 10, 1) &&
1462         odr_implicit(o, odr_integer, &(*p)->waitAction, ODR_CONTEXT, 11, 0) &&
1463         z_ElementSetName(o, &(*p)->elements, 1) &&
1464         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1465         odr_sequence_end(o);
1466 }
1467
1468 int z_ExtendedServicesResponse(ODR o, Z_ExtendedServicesResponse **p, int opt)
1469 {
1470     if (!odr_sequence_begin(o, p, sizeof(**p)))
1471         return opt && odr_ok(o);
1472     return
1473         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1474         odr_implicit(o, odr_integer, &(*p)->operationStatus, ODR_CONTEXT, 3, 0) &&
1475         odr_implicit_settag(o, ODR_CONTEXT, 4) &&
1476         (odr_sequence_of(o, z_DiagRec, &(*p)->diagnostics,
1477             &(*p)->num_diagnostics) || odr_ok(o)) &&
1478         odr_implicit(o, z_External, &(*p)->taskPackage, ODR_CONTEXT, 5, 1) &&
1479         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1480         odr_sequence_end(o);
1481 }
1482
1483 /* ------------------------ APDU ------------------------- */
1484
1485 int z_APDU(ODR o, Z_APDU **p, int opt)
1486 {
1487     static Odr_arm arm[] =
1488     {
1489         {ODR_IMPLICIT, ODR_CONTEXT, 20, Z_APDU_initRequest, z_InitRequest},
1490         {ODR_IMPLICIT, ODR_CONTEXT, 21, Z_APDU_initResponse, z_InitResponse},
1491         {ODR_IMPLICIT, ODR_CONTEXT, 22, Z_APDU_searchRequest, z_SearchRequest},
1492         {ODR_IMPLICIT, ODR_CONTEXT, 23, Z_APDU_searchResponse,
1493             z_SearchResponse},
1494         {ODR_IMPLICIT, ODR_CONTEXT, 24, Z_APDU_presentRequest,
1495             z_PresentRequest},
1496         {ODR_IMPLICIT, ODR_CONTEXT, 25, Z_APDU_presentResponse,
1497             z_PresentResponse},
1498         {ODR_IMPLICIT, ODR_CONTEXT, 26, Z_APDU_deleteResultSetRequest,
1499             z_DeleteResultSetRequest},
1500         {ODR_IMPLICIT, ODR_CONTEXT, 27, Z_APDU_deleteResultSetResponse,
1501             z_DeleteResultSetResponse},
1502         {ODR_IMPLICIT, ODR_CONTEXT, 28, Z_APDU_accessControlRequest,
1503             z_AccessControlRequest},
1504         {ODR_IMPLICIT, ODR_CONTEXT, 29, Z_APDU_accessControlResponse,
1505             z_AccessControlResponse},
1506         {ODR_IMPLICIT, ODR_CONTEXT, 30, Z_APDU_resourceControlRequest,
1507             z_ResourceControlRequest},
1508         {ODR_IMPLICIT, ODR_CONTEXT, 31, Z_APDU_resourceControlResponse,
1509             z_ResourceControlResponse},
1510         {ODR_IMPLICIT, ODR_CONTEXT, 32, Z_APDU_triggerResourceControlRequest,
1511             z_TriggerResourceControlRequest},
1512         {ODR_IMPLICIT, ODR_CONTEXT, 35, Z_APDU_scanRequest, z_ScanRequest},
1513         {ODR_IMPLICIT, ODR_CONTEXT, 36, Z_APDU_scanResponse, z_ScanResponse},
1514         {ODR_IMPLICIT, ODR_CONTEXT, 45, Z_APDU_segmentRequest, z_Segment},
1515         {ODR_IMPLICIT, ODR_CONTEXT, 46, Z_APDU_extendedServicesRequest,
1516             z_ExtendedServicesRequest},
1517         {ODR_IMPLICIT, ODR_CONTEXT, 47, Z_APDU_extendedServicesResponse,
1518             z_ExtendedServicesResponse},
1519         {ODR_IMPLICIT, ODR_CONTEXT, 48, Z_APDU_close, z_Close},
1520
1521         {-1, -1, -1, -1, 0}
1522     };
1523
1524     if (o->direction == ODR_DECODE)
1525         *p = odr_malloc(o, sizeof(**p));
1526     if (!odr_choice(o, arm, &(*p)->u, &(*p)->which))
1527     {
1528         if (o->direction == ODR_DECODE)
1529             *p = 0;
1530         return opt && odr_ok(o);
1531     }
1532     return 1;
1533 }