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