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