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