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