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