Fixed Defaultdiagformat.
[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.31  1995-06-16 13:15:56  quinn
8  * Fixed Defaultdiagformat.
9  *
10  * Revision 1.30  1995/06/15  15:42:01  quinn
11  * Fixed some v3 bugs
12  *
13  * Revision 1.29  1995/06/15  07:44:49  quinn
14  * Moving to v3.
15  *
16  * Revision 1.28  1995/06/14  15:26:35  quinn
17  * *** empty log message ***
18  *
19  * Revision 1.27  1995/06/07  14:36:22  quinn
20  * Added CLOSE
21  *
22  * Revision 1.26  1995/06/02  09:49:13  quinn
23  * Adding access control
24  *
25  * Revision 1.25  1995/05/25  11:00:08  quinn
26  * *** empty log message ***
27  *
28  * Revision 1.24  1995/05/22  13:58:18  quinn
29  * Fixed an ODR_NULLVAL.
30  *
31  * Revision 1.23  1995/05/22  11:30:18  quinn
32  * Adding Z39.50-1992 stuff to proto.c. Adding zget.c
33  *
34  * Revision 1.22  1995/05/17  08:40:56  quinn
35  * Added delete. Fixed some sequence_begins. Smallish.
36  *
37  * Revision 1.21  1995/05/16  08:50:24  quinn
38  * License, documentation, and memory fixes
39  *
40  * Revision 1.20  1995/05/15  11:55:25  quinn
41  * Smallish.
42  *
43  * Revision 1.19  1995/04/11  11:58:35  quinn
44  * Fixed bug.
45  *
46  * Revision 1.18  1995/04/11  11:52:02  quinn
47  * Fixed possible buf in proto.c
48  *
49  * Revision 1.17  1995/04/10  10:22:22  quinn
50  * Added SCAN.
51  *
52  * Revision 1.16  1995/03/30  10:26:43  quinn
53  * Added Term structure
54  *
55  * Revision 1.15  1995/03/30  09:08:39  quinn
56  * Added Resource control protocol
57  *
58  * Revision 1.14  1995/03/29  08:06:13  quinn
59  * Added a few v3 elements
60  *
61  * Revision 1.13  1995/03/20  11:26:52  quinn
62  * *** empty log message ***
63  *
64  * Revision 1.12  1995/03/20  09:45:09  quinn
65  * Working towards v3
66  *
67  * Revision 1.11  1995/03/17  10:17:25  quinn
68  * Added memory management.
69  *
70  * Revision 1.10  1995/03/15  11:17:40  quinn
71  * Fixed some return-checks from choice.. need better ay to handle those..
72  *
73  * Revision 1.9  1995/03/15  08:37:06  quinn
74  * Fixed protocol bugs.
75  *
76  * Revision 1.8  1995/03/14  16:59:24  quinn
77  * Fixed OPTIONAL flag in attributeelement
78  *
79  * Revision 1.7  1995/03/07  16:29:33  quinn
80  * Added authentication stuff.
81  *
82  * Revision 1.6  1995/03/01  14:46:03  quinn
83  * Fixed protocol bug in 8777query.
84  *
85  * Revision 1.5  1995/02/14  11:54:22  quinn
86  * Fixing include.
87  *
88  * Revision 1.4  1995/02/10  15:54:30  quinn
89  * Small adjustments.
90  *
91  * Revision 1.3  1995/02/09  15:51:39  quinn
92  * Works better now.
93  *
94  * Revision 1.2  1995/02/06  21:26:07  quinn
95  * Repaired this evening's damages..
96  *
97  * Revision 1.1  1995/02/06  16:44:47  quinn
98  * First hack at Z/SR protocol
99  *
100  */
101
102 #include <odr.h>
103
104 #include <proto.h>
105
106 /* ---------------------- GLOBAL DEFS ------------------- */
107
108 int z_ReferenceId(ODR o, Z_ReferenceId **p, int opt)
109 {
110     return odr_implicit(o, odr_octetstring, (Odr_oct**) p, ODR_CONTEXT, 2, opt);
111 }
112
113 int z_DatabaseName(ODR o, Z_DatabaseName **p, int opt)
114 {
115     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 105,
116         opt);
117 }
118
119 int z_ResultSetId(ODR o, char **p, int opt)
120 {
121     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 31,
122         opt);
123 }
124
125 int z_UserInformationField(ODR o, Odr_external **p, int opt)
126 {
127     return odr_explicit(o, odr_external, (Odr_external **)p, ODR_CONTEXT,
128         11, opt);
129 }
130
131 int z_InternationalString(ODR o, char **p, int opt)
132 {
133     return odr_generalstring(o, p, opt);
134 }
135
136 int z_InfoCategory(ODR o, Z_InfoCategory **p, int opt)
137 {
138     if (!odr_sequence_begin(o, p, sizeof(**p)))
139         return opt && odr_ok(o);
140     return
141         odr_implicit(o, odr_oid, &(*p)->categoryTypeId, ODR_CONTEXT, 1, 1) &&
142         odr_implicit(o, odr_integer, &(*p)->categoryValue, ODR_CONTEXT, 2, 0) &&
143         odr_sequence_end(o);
144 }
145
146 int z_OtherInformationUnit(ODR o, Z_OtherInformationUnit **p, int opt)
147 {
148     static Odr_arm arm[] =
149     {
150         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_OtherInfo_characterInfo,
151             odr_visiblestring},
152         {ODR_IMPLICIT, ODR_CONTEXT, 3, Z_OtherInfo_binaryInfo,
153             odr_octetstring},
154         {ODR_IMPLICIT, ODR_CONTEXT, 4, Z_OtherInfo_externallyDefinedInfo,
155             odr_external},
156         {ODR_IMPLICIT, ODR_CONTEXT, 5, Z_OtherInfo_oid, odr_oid},
157         {-1, -1, -1, -1, 0}
158     };
159
160     if (!odr_sequence_begin(o, p, sizeof(**p)))
161         return opt && odr_ok(o);
162     return
163         odr_implicit(o, z_InfoCategory, &(*p)->category, ODR_CONTEXT, 1, 1) &&
164         odr_choice(o, arm, &(*p)->which, &(*p)->information) &&
165         odr_sequence_end(o);
166 }
167     
168 int z_OtherInformation(ODR o, Z_OtherInformation **p, int opt)
169 {
170     if (o->direction == ODR_DECODE)
171         *p = odr_malloc(o, sizeof(**p));
172     else if (!*p)
173         return opt;
174     odr_implicit_settag(o, ODR_CONTEXT, 201);
175     if (odr_sequence_of(o, z_OtherInformationUnit, &(*p)->list,
176         &(*p)->num_elements))
177         return 1;
178     *p = 0;
179     return opt && odr_ok(o);
180 }
181
182 int z_StringOrNumeric(ODR o, Z_StringOrNumeric **p, int opt)
183 {
184     static Odr_arm arm[] =
185     {
186         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_StringOrNumeric_string,
187             odr_visiblestring},
188         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_StringOrNumeric_numeric,
189             odr_integer},
190         {-1, -1, -1, -1, 0}
191     };
192
193     if (o->direction == ODR_DECODE)
194         *p = odr_malloc(o, sizeof(**p));
195     else if (!*p)
196         return opt;
197     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
198         return 1;
199     *p = 0;
200     return opt && odr_ok(o);
201 }
202
203 /*
204  * check tagging!!
205  */
206 int z_Unit(ODR o, Z_Unit **p, int opt)
207 {
208     if (!odr_sequence_begin(o, p, sizeof(**p)))
209         return opt && odr_ok(o);
210     return
211         odr_implicit(o, odr_visiblestring, &(*p)->unitSystem, ODR_CONTEXT,
212             1, 1) &&
213         odr_explicit(o, z_StringOrNumeric, &(*p)->unitType, ODR_CONTEXT,
214             2, 1) &&
215         odr_explicit(o, z_StringOrNumeric, &(*p)->unit, ODR_CONTEXT, 3, 1) &&
216         odr_implicit(o, odr_integer, &(*p)->scaleFactor, ODR_CONTEXT, 4, 1) &&
217         odr_sequence_end(o);
218 }
219
220 int z_IntUnit(ODR o, Z_IntUnit **p, int opt)
221 {
222     if (!odr_sequence_begin(o, p, sizeof(**p)))
223         return opt && odr_ok(o);
224     return
225         odr_implicit(o, odr_integer, &(*p)->value, ODR_CONTEXT, 1, 0) &&
226         odr_implicit(o, z_Unit, &(*p)->unitUsed, ODR_CONTEXT, 2, 0) &&
227         odr_sequence_end(o);
228 }
229
230 /* ---------------------- INITIALIZE SERVICE ------------------- */
231
232 #if 0
233 int z_NSRAuthentication(ODR o, Z_NSRAuthentication **p, int opt)
234 {
235     if (!odr_sequence_begin(o, p, sizeof(**p)))
236         return opt && odr_ok(o);
237     return
238         odr_visiblestring(o, &(*p)->user, 0) &&
239         odr_visiblestring(o, &(*p)->password, 0) &&
240         odr_visiblestring(o, &(*p)->account, 0) &&
241         odr_sequence_end(o);
242 }
243 #endif
244
245 int z_IdPass(ODR o, Z_IdPass **p, int opt)
246 {
247     if (!odr_sequence_begin(o, p, sizeof(**p)))
248         return opt && odr_ok(o);
249     return
250         odr_implicit(o, odr_visiblestring, &(*p)->groupId, ODR_CONTEXT, 0, 1) &&
251         odr_implicit(o, odr_visiblestring, &(*p)->userId, ODR_CONTEXT, 1, 1) &&
252         odr_implicit(o, odr_visiblestring, &(*p)->password, ODR_CONTEXT, 2,
253             1) &&
254         odr_sequence_end(o);
255 }
256
257 int z_StrAuthentication(ODR o, char **p, int opt)
258 {
259     return odr_visiblestring(o, p, opt);
260 }
261
262 int z_IdAuthentication(ODR o, Z_IdAuthentication **p, int opt)
263 {
264     static Odr_arm arm[] =
265     {
266         {-1, -1, -1, Z_IdAuthentication_open, z_StrAuthentication},
267         {-1, -1, -1, Z_IdAuthentication_idPass, z_IdPass},
268         {-1, -1, -1, Z_IdAuthentication_anonymous, odr_null},
269         {-1, -1, -1, Z_IdAuthentication_other, odr_external},
270         {-1, -1, -1, -1, 0}
271     };
272
273     if (o->direction == ODR_DECODE)
274         *p = odr_malloc(o, sizeof(**p));
275     else if (!*p)
276         return opt;
277
278     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
279         return 1;
280     *p = 0;
281     return opt && odr_ok(o);
282 }
283
284 int z_InitRequest(ODR o, Z_InitRequest **p, int opt)
285 {
286     Z_InitRequest *pp;
287
288     if (!odr_sequence_begin(o, p, sizeof(**p)))
289         return opt && odr_ok(o);
290     pp = *p;
291     return
292         z_ReferenceId(o, &pp->referenceId, 1) &&
293         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
294             3, 0) &&
295         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
296         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
297             5, 0) &&
298         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
299             6, 0) &&
300         odr_explicit(o, z_IdAuthentication, &pp->idAuthentication, ODR_CONTEXT,
301             7, 1) &&
302         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
303             110, 1) &&
304         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
305             111, 1) &&
306         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
307             ODR_CONTEXT, 112, 1) &&
308         z_UserInformationField(o, &pp->userInformationField, 1) &&
309 #ifdef Z_95
310         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
311 #endif
312         odr_sequence_end(o);
313 }
314
315 int z_InitResponse(ODR o, Z_InitResponse **p, int opt)
316 {
317     Z_InitResponse *pp;
318
319     if (!odr_sequence_begin(o, p, sizeof(**p)))
320         return opt && odr_ok(o);
321     pp = *p;
322     return
323         z_ReferenceId(o, &pp->referenceId, 1) &&
324         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
325             3, 0) &&
326         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
327         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
328             5, 0) &&
329         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
330             6, 0) &&
331         odr_implicit(o, odr_bool, &pp->result, ODR_CONTEXT, 12, 0) &&
332         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
333             110, 1) &&
334         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
335             111, 1) &&
336         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
337             ODR_CONTEXT, 112, 1) &&
338         z_UserInformationField(o, &pp->userInformationField, 1) &&
339 #ifdef Z_95
340         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
341 #endif
342         odr_sequence_end(o);
343 }
344
345 /* ------------------ RESOURCE CONTROL ----------------*/
346
347 int z_TriggerResourceControlRequest(ODR o, Z_TriggerResourceControlRequest **p,
348                                     int opt)
349 {
350     if (!odr_sequence_begin(o, p, sizeof(**p)))
351         return opt && odr_ok(o);
352     return
353         z_ReferenceId(o, &(*p)->referenceId, 1) &&
354         odr_implicit(o, odr_integer, &(*p)->requestedAction, ODR_CONTEXT,
355             46, 0) &&
356         odr_implicit(o, odr_oid, &(*p)->prefResourceReportFormat,
357             ODR_CONTEXT, 47, 1) &&
358         odr_implicit(o, odr_bool, &(*p)->resultSetWanted, ODR_CONTEXT,
359             48, 1) &&
360 #ifdef Z_95
361         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
362 #endif
363         odr_sequence_end(o);
364 }
365
366 int z_ResourceControlRequest(ODR o, Z_ResourceControlRequest **p, int opt)
367 {
368     if (!odr_sequence_begin(o, p, sizeof(**p)))
369         return opt && odr_ok(o);
370     return
371         z_ReferenceId(o, &(*p)->referenceId, 1) &&
372         odr_implicit(o, odr_bool, &(*p)->suspendedFlag, ODR_CONTEXT, 39, 1)&&
373         odr_explicit(o, odr_external, &(*p)->resourceReport, ODR_CONTEXT,
374             40, 1) &&
375         odr_implicit(o, odr_integer, &(*p)->partialResultsAvailable,
376             ODR_CONTEXT, 41, 1) &&
377         odr_implicit(o, odr_bool, &(*p)->responseRequired, ODR_CONTEXT,
378             42, 0) &&
379         odr_implicit(o, odr_bool, &(*p)->triggeredRequestFlag,
380             ODR_CONTEXT, 43, 1) &&
381 #ifdef Z_95
382         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
383 #endif
384         odr_sequence_end(o);
385 }
386
387 int z_ResourceControlResponse(ODR o, Z_ResourceControlResponse **p, int opt)
388 {
389     if (!odr_sequence_begin(o, p, sizeof(**p)))
390         return opt && odr_ok(o);
391     return
392         z_ReferenceId(o, &(*p)->referenceId, 1) &&
393         odr_implicit(o, odr_bool, &(*p)->continueFlag, ODR_CONTEXT, 44, 0) &&
394         odr_implicit(o, odr_bool, &(*p)->resultSetWanted, ODR_CONTEXT,
395             45, 1) &&
396 #ifdef Z_95
397         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
398 #endif
399         odr_sequence_end(o);
400 }
401
402 /* ------------------------ SEARCH SERVICE ----------------------- */
403
404 int z_ElementSetName(ODR o, char **p, int opt)
405 {
406     return odr_implicit(o, odr_visiblestring, (char**) p, ODR_CONTEXT, 103,
407         opt);
408 }
409
410 int z_DatabaseSpecificUnit(ODR o, Z_DatabaseSpecificUnit **p, int opt)
411 {
412     if (!odr_sequence_begin(o, p, sizeof(**p)))
413         return opt && odr_ok(o);
414     return
415         z_DatabaseName(o, &(*p)->databaseName, 0) &&
416         z_ElementSetName(o, &(*p)->elementSetName, 0) &&
417         odr_sequence_end(o);
418 }
419
420 int z_DatabaseSpecific(ODR o, Z_DatabaseSpecific **p, int opt)
421 {
422     if (o->direction == ODR_DECODE)
423         *p = odr_malloc(o, sizeof(**p));
424     else if (!*p)
425         return opt;
426
427     odr_implicit_settag(o, ODR_CONTEXT, 1);
428     if (odr_sequence_of(o, z_DatabaseSpecificUnit, &(*p)->elements,
429         &(*p)->num_elements))
430         return 1;
431     *p = 0;
432     return 0;
433 }
434
435 int z_ElementSetNames(ODR o, Z_ElementSetNames **p, int opt)
436 {
437     static Odr_arm arm[] =
438     {
439         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_ElementSetNames_generic,
440             z_ElementSetName},
441         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ElementSetNames_databaseSpecific,
442             z_DatabaseSpecific},
443         {-1, -1, -1, -1, 0}
444     };
445
446     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 19))
447         return opt && odr_ok(o);
448
449     if (o->direction == ODR_DECODE)
450         *p = odr_malloc(o, sizeof(**p));
451
452     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
453         odr_constructed_end(o))
454         return 1;
455     *p = 0;
456     return 0;
457 }
458
459 /* ----------------------- RPN QUERY -----------------------*/
460
461 int z_ComplexAttribute(ODR o, Z_ComplexAttribute **p, int opt)
462 {
463     if (!odr_sequence_begin(o, p, sizeof(**p)))
464         return opt && odr_ok(o);
465     return
466         odr_implicit_settag(o, ODR_CONTEXT, 1) &&
467         odr_sequence_of(o, z_StringOrNumeric, &(*p)->list,
468             &(*p)->num_list) &&
469         odr_implicit_settag(o, ODR_CONTEXT, 2) &&
470         (odr_sequence_of(o, odr_integer, &(*p)->semanticAction,
471             &(*p)->num_semanticAction) || odr_ok(o)) &&
472         odr_sequence_end(o);
473 }
474
475 int z_AttributeElement(ODR o, Z_AttributeElement **p, int opt)
476 {
477 #ifdef Z_95
478     static Odr_arm arm[] =
479     {
480         {ODR_IMPLICIT, ODR_CONTEXT, 121, Z_AttributeValue_numeric,
481             odr_integer},
482         {ODR_IMPLICIT, ODR_CONTEXT, 224, Z_AttributeValue_complex,
483             z_ComplexAttribute},
484         {-1, -1, -1, -1, 0}
485     };
486 #endif
487
488     if (!odr_sequence_begin(o, p, sizeof(**p)))
489         return opt && odr_ok(o);
490     return
491 #ifdef Z_95
492         odr_implicit(o, odr_oid, &(*p)->attributeSet, ODR_CONTEXT, 1, 1) &&
493 #endif
494         odr_implicit(o, odr_integer, &(*p)->attributeType, ODR_CONTEXT,
495             120, 0) &&
496 #ifdef Z_95
497         odr_choice(o, arm, &(*p)->value, &(*p)->which) &&
498 #else
499         odr_implicit(o, odr_integer, &(*p)->attributeValue, ODR_CONTEXT,
500             121, 0) &&
501 #endif
502         odr_sequence_end(o);
503 }
504
505 int z_Term(ODR o, Z_Term **p, int opt)
506 {
507     static Odr_arm arm[] =
508     {
509         {ODR_IMPLICIT, ODR_CONTEXT, 45, Z_Term_general, odr_octetstring},
510         {ODR_IMPLICIT, ODR_CONTEXT, 215, Z_Term_numeric, odr_integer},
511         {ODR_IMPLICIT, ODR_CONTEXT, 216, Z_Term_characterString,
512             odr_visiblestring},
513         {ODR_IMPLICIT, ODR_CONTEXT, 217, Z_Term_oid, odr_oid},
514         {ODR_IMPLICIT, ODR_CONTEXT, 218, Z_Term_dateTime, odr_cstring},
515         {ODR_IMPLICIT, ODR_CONTEXT, 219, Z_Term_external, odr_external},
516         /* add intUnit here */
517         {ODR_IMPLICIT, ODR_CONTEXT, 221, Z_Term_null, odr_null},
518         {-1, -1, -1, -1, 0}
519     };
520
521     if (o->direction ==ODR_DECODE)
522         *p = odr_malloc(o, sizeof(**p));
523     else if (!*p)
524         return opt;
525     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
526         return 1;
527     *p = 0;
528     return opt && odr_ok(o);
529 }
530
531 int z_AttributesPlusTerm(ODR o, Z_AttributesPlusTerm **p, int opt)
532 {
533     if (!(odr_implicit_settag(o, ODR_CONTEXT, 102) &&
534         odr_sequence_begin(o, p, sizeof(**p))))
535         return opt && odr_ok(o);
536     return
537         odr_implicit_settag(o, ODR_CONTEXT, 44) &&
538         odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
539             &(*p)->num_attributes) &&
540         z_Term(o, &(*p)->term, 0) &&
541         odr_sequence_end(o);
542 }
543
544 int z_ResultSetPlusAttributes(ODR o, Z_ResultSetPlusAttributes **p, int opt)
545 {
546     if (!(odr_implicit_settag(o, ODR_CONTEXT, 214) &&
547         odr_sequence_begin(o, p, sizeof(**p))))
548         return opt && odr_ok(o);
549     return
550         z_ResultSetId(o, &(*p)->resultSet, 0) &&
551         odr_implicit_settag(o, ODR_CONTEXT, 44) &&
552         odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
553             &(*p)->num_attributes) &&
554         odr_sequence_end(o);
555 }
556
557 int z_ProximityOperator(ODR o, Z_ProximityOperator **p, int opt)
558 {
559     static Odr_arm arm[] =
560     {
561         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ProxCode_known, odr_integer},
562         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ProxCode_private, odr_integer},
563         {-1, -1, -1, -1, 0}
564     };
565
566     if (!odr_sequence_begin(o, p, sizeof(**p)))
567         return opt && odr_ok(o);
568     return
569         odr_implicit(o, odr_bool, &(*p)->exclusion, ODR_CONTEXT, 1, 1) &&
570         odr_implicit(o, odr_integer, &(*p)->distance, ODR_CONTEXT, 2, 0) &&
571         odr_implicit(o, odr_bool, &(*p)->ordered, ODR_CONTEXT, 3, 0) &&
572         odr_implicit(o, odr_integer, &(*p)->relationType, ODR_CONTEXT, 4, 0) &&
573         odr_constructed_begin(o, &(*p)->proximityUnitCode, ODR_CONTEXT, 5) &&
574         odr_choice(o, arm, &(*p)->proximityUnitCode, &(*p)->which) &&
575         odr_constructed_end(o) &&
576         odr_sequence_end(o);
577 }
578
579 int z_Operator(ODR o, Z_Operator **p, int opt)
580 {
581     static Odr_arm arm[] =
582     {
583         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_Operator_and, odr_null},
584         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Operator_or, odr_null},
585         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_Operator_and_not, odr_null},
586         {ODR_IMPLICIT, ODR_CONTEXT, 3, Z_Operator_prox, z_ProximityOperator},
587         {-1, -1, -1, -1, 0}
588     };
589
590     if (!*p && o->direction != ODR_DECODE)
591         return opt;
592     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 46))
593         return opt && odr_ok(o);
594     if (o->direction == ODR_DECODE)
595         *p = odr_malloc(o, sizeof(**p));
596
597     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
598         odr_constructed_end(o))
599         return 1;
600     *p = 0;
601     return opt && odr_ok(o);
602 }
603
604 int z_Operand(ODR o, Z_Operand **p, int opt)
605 {
606     static Odr_arm arm[] =
607     {
608         {-1, -1, -1, Z_Operand_APT, z_AttributesPlusTerm},
609         {-1, -1, -1, Z_Operand_resultSetId, z_ResultSetId},
610         {-1, -1, -1, Z_Operand_resultAttr, z_ResultSetPlusAttributes},
611         {-1, -1, -1, -1, 0}
612     };
613
614     if (o->direction == ODR_DECODE)
615         *p = odr_malloc(o, sizeof(**p));
616     else if (!*p)
617         return opt;
618     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
619         return 1;
620     *p = 0;
621     return opt && odr_ok(o);
622 }
623
624 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt);
625
626 int z_Complex(ODR o, Z_Complex **p, int opt)
627 {
628     if (!odr_sequence_begin(o, p, sizeof(**p)))
629         return opt && odr_ok(o);
630     return
631         z_RPNStructure(o, &(*p)->s1, 0) &&
632         z_RPNStructure(o, &(*p)->s2, 0) &&
633         z_Operator(o, &(*p)->operator, 0) &&
634         odr_sequence_end(o);
635 }
636
637 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt)
638 {
639     static Odr_arm arm[] = 
640     {
641         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_RPNStructure_simple, z_Operand},
642         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_RPNStructure_complex, z_Complex},
643         {-1 -1, -1, -1, 0}
644     };
645
646     if (o->direction == ODR_DECODE)
647         *p = odr_malloc(o, sizeof(**p));
648     else if (!*p)
649         return opt;
650     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
651         return 1;
652     *p = 0;
653     return opt && odr_ok(o);
654 }
655
656 int z_RPNQuery(ODR o, Z_RPNQuery **p, int opt)
657 {
658     if (!odr_sequence_begin(o, p, sizeof(**p)))
659         return opt && odr_ok(o);
660     return
661         odr_oid(o, &(*p)->attributeSetId, 0) &&
662         z_RPNStructure(o, &(*p)->RPNStructure, 0) &&
663         odr_sequence_end(o);
664 }
665
666 /* -----------------------END RPN QUERY ----------------------- */
667
668 int z_Query(ODR o, Z_Query **p, int opt)
669 {
670     static Odr_arm arm[] = 
671     {
672         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Query_type_1, z_RPNQuery},
673         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_Query_type_2, odr_octetstring},
674         {ODR_EXPLICIT, ODR_CONTEXT, 101, Z_Query_type_101, z_RPNQuery},
675         {-1, -1, -1, -1, 0}
676     };
677
678     if (o->direction == ODR_DECODE)
679         *p = odr_malloc(o, sizeof(**p));
680     else if (!*p)
681         return opt;
682     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
683         return 1;
684     *p = 0;
685     return opt && odr_ok(o);
686 }
687
688 int z_SearchRequest(ODR o, Z_SearchRequest **p, int opt)
689 {
690     Z_SearchRequest *pp;
691
692     if (!odr_sequence_begin(o, p, sizeof(**p)))
693         return opt && odr_ok(o);
694     pp = *p;
695     return
696         z_ReferenceId(o, &pp->referenceId, 1) &&
697         odr_implicit(o, odr_integer, &pp->smallSetUpperBound, ODR_CONTEXT,
698             13, 0) &&
699         odr_implicit(o, odr_integer, &pp->largeSetLowerBound, ODR_CONTEXT,
700             14, 0) &&
701         odr_implicit(o, odr_integer, &pp->mediumSetPresentNumber, ODR_CONTEXT,
702             15, 0) &&
703         odr_implicit(o, odr_bool, &pp->replaceIndicator, ODR_CONTEXT, 16, 1) &&
704         odr_implicit(o, odr_visiblestring, &pp->resultSetName, ODR_CONTEXT,
705             17, 9) &&
706         odr_implicit_settag(o, ODR_CONTEXT, 18) &&
707         odr_sequence_of(o, z_DatabaseName, &pp->databaseNames,
708             &pp->num_databaseNames) &&
709         odr_explicit(o, z_ElementSetNames, &pp->smallSetElementSetNames,
710             ODR_CONTEXT, 100, 1) &&
711         odr_explicit(o, z_ElementSetNames, &pp->mediumSetElementSetNames,
712             ODR_CONTEXT, 101, 1) &&
713         odr_implicit(o, odr_oid, &pp->preferredRecordSyntax,
714             ODR_CONTEXT, 104, 1) &&
715         odr_explicit(o, z_Query, &pp->query, ODR_CONTEXT, 21, 0) &&
716 #ifdef Z_95
717         odr_implicit(o, z_OtherInformation, &(*p)->additionalSearchInfo,
718             ODR_CONTEXT, 203, 1) &&
719         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
720 #endif
721         odr_sequence_end(o);
722 }
723
724 /* ------------------------ RECORD ------------------------- */
725
726 int z_DatabaseRecord(ODR o, Z_DatabaseRecord **p, int opt)
727 {
728     return odr_external(o, (Odr_external **) p, opt);
729 }
730
731 #ifdef Z_95
732
733 int z_DefaultDiagFormat(ODR o, Z_DefaultDiagFormat **p, int opt)
734 {
735     static Odr_arm arm[] =
736     {
737         {-1, -1, -1, Z_DiagForm_v2AddInfo, odr_visiblestring},
738         {-1, -1, -1, Z_DiagForm_v3AddInfo, z_InternationalString},
739         {ODR_IMPLICIT, ODR_CONTEXT, ODR_VISIBLESTRING, Z_DiagForm_v2AddInfo,
740             odr_visiblestring}, /* to match some weird server.. */
741         {-1, -1, -1, -1, 0}
742     };
743     if (!odr_sequence_begin(o, p, sizeof(**p)))
744         return opt && odr_ok(o);
745     return
746         odr_oid(o, &(*p)->diagnosticSetId, 1) && /* SHOULD NOT BE OPT! */
747         odr_integer(o, &(*p)->condition, 0) &&
748         /*
749          * I no longer recall what server tagged the addinfo.. but it isn't
750          * hurting anyone, so...
751          * We need to turn it into a choice, or something, because of
752          * that damn generalstring in v3.
753          */
754         odr_choice(o, arm, &(*p)->addinfo, &(*p)->which) &&
755         odr_sequence_end(o);
756 }
757
758 int z_DiagRec(ODR o, Z_DiagRec **p, int opt)
759 {
760     static Odr_arm arm[] = 
761     {
762         {-1, -1, -1, Z_DiagRec_defaultFormat, z_DefaultDiagFormat},
763         {-1, -1, -1, Z_DiagRec_externallyDefined, odr_external},
764         {-1, -1, -1, -1, 0}
765     };
766
767     if (o->direction == ODR_DECODE)
768         *p = odr_malloc(o, sizeof(**p));
769     else if (!*p)
770         return opt;
771     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
772         return 1;
773     *p = 0;
774     return opt && odr_ok(o);
775 }
776
777 #else
778
779 int z_DiagRec(ODR o, Z_DiagRec **p, int opt)
780 {
781     if (!odr_sequence_begin(o, p, sizeof(**p)))
782         return opt && odr_ok(o);
783     return
784         odr_oid(o, &(*p)->diagnosticSetId, 1) && /* SHOULD NOT BE OPT! */
785         odr_integer(o, &(*p)->condition, 0) &&
786         /*
787          * I no longer recall what server tagged the addinfo.. but it isn't
788          * hurting anyone, so...
789          * We need to turn it into a choice, or something, because of
790          * that damn generalstring in v3.
791          */
792         (odr_visiblestring(o, &(*p)->addinfo, 0) ||
793             odr_implicit(o, odr_cstring, &(*p)->addinfo, ODR_CONTEXT,
794             ODR_VISIBLESTRING, 1)) &&
795         odr_sequence_end(o);
796 }
797
798 #endif
799
800 int z_DiagRecs(ODR o, Z_DiagRecs **p, int opt)
801 {
802     if (o->direction == ODR_DECODE)
803         *p = odr_malloc(o, sizeof(**p));
804     else if (!*p)
805         return opt;
806
807         if (odr_sequence_of(o, z_DiagRec, &(*p)->diagRecs,
808         &(*p)->num_diagRecs))
809         return 1;
810     *p = 0;
811     return 0;
812 }
813
814 int z_NamePlusRecord(ODR o, Z_NamePlusRecord **p, int opt)
815 {
816     static Odr_arm arm[] =
817     {
818         {ODR_EXPLICIT, ODR_CONTEXT, 1, Z_NamePlusRecord_databaseRecord,
819             z_DatabaseRecord},
820         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_NamePlusRecord_surrogateDiagnostic,
821             z_DiagRec},
822         {-1, -1, -1, -1, 0}
823     };
824
825     if (!odr_sequence_begin(o, p, sizeof(**p)))
826         return opt && odr_ok(o);
827     return
828         odr_implicit(o, z_DatabaseName, &(*p)->databaseName, ODR_CONTEXT,
829             0, 1) &&
830         odr_constructed_begin(o, &(*p)->u, ODR_CONTEXT, 1) &&
831         odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
832         odr_constructed_end(o) &&
833         odr_sequence_end(o);
834 }
835
836 int z_NamePlusRecordList(ODR o, Z_NamePlusRecordList **p, int opt)
837 {
838     if (o->direction == ODR_DECODE)
839         *p = odr_malloc(o, sizeof(**p));
840     else if (!*p)
841         return opt;
842     if (odr_sequence_of(o, z_NamePlusRecord, &(*p)->records,
843         &(*p)->num_records))
844         return 1;
845     *p = 0;
846     return 0;
847 }
848
849 int z_Records(ODR o, Z_Records **p, int opt)
850 {
851     static Odr_arm arm[] = 
852     {
853         {ODR_IMPLICIT, ODR_CONTEXT, 28, Z_Records_DBOSD, z_NamePlusRecordList},
854         {ODR_IMPLICIT, ODR_CONTEXT, 130, Z_Records_NSD, z_DiagRec},
855         {ODR_IMPLICIT, ODR_CONTEXT, 205, Z_Records_multipleNSD,
856             z_DiagRecs},
857         {-1, -1, -1, -1, 0}
858     };
859
860     if (o->direction == ODR_DECODE)
861         *p = odr_malloc(o, sizeof(**p));
862     else if (!*p)
863         return opt;
864     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
865         return 1;
866     *p = 0;
867     return opt && odr_ok(o);
868 }
869
870 /* ------------------------ ACCESS CTRL SERVICE ----------------------- */
871
872 int z_AccessControlRequest(ODR o, Z_AccessControlRequest **p, int opt)
873 {
874     static Odr_arm arm[] = 
875     {
876         {ODR_IMPLICIT, ODR_CONTEXT, 37, Z_AccessRequest_simpleForm,
877             odr_octetstring},
878         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_AccessRequest_externallyDefined,
879             odr_external},
880         {-1, -1, -1, -1, 0}
881     };
882     if (!odr_sequence_begin(o, p, sizeof(**p)))
883         return opt && odr_ok(o);
884     return
885         z_ReferenceId(o, &(*p)->referenceId, 1) &&
886         odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
887 #ifdef Z_95
888         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
889 #endif
890         odr_sequence_end(o);
891 }
892
893 int z_AccessControlResponse(ODR o, Z_AccessControlResponse **p, int opt)
894 {
895     static Odr_arm arm[] = 
896     {
897         {ODR_IMPLICIT, ODR_CONTEXT, 38, Z_AccessResponse_simpleForm,
898             odr_octetstring},
899         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_AccessResponse_externallyDefined,
900             odr_external},
901         {-1, -1, -1, -1, 0}
902     };
903     if (!odr_sequence_begin(o, p, sizeof(**p)))
904         return opt && odr_ok(o);
905     return
906         z_ReferenceId(o, &(*p)->referenceId, 1) &&
907         odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
908         odr_explicit(o, z_DiagRec, &(*p)->diagnostic, ODR_CONTEXT, 223, 1) &&
909 #ifdef Z_95
910         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
911 #endif
912         odr_sequence_end(o);
913 }
914
915 /* ------------------------ SCAN SERVICE -------------------- */
916
917 int z_AttributeList(ODR o, Z_AttributeList **p, int opt)
918 {
919     if (o->direction == ODR_DECODE)
920         *p = odr_malloc(o, sizeof(**p));
921     else if (!*p)
922         return opt;
923
924     odr_implicit_settag(o, ODR_CONTEXT, 44);
925     if (odr_sequence_of(o, z_AttributeElement, &(*p)->attributes,
926         &(*p)->num_attributes))
927         return 1;
928     *p = 0;
929     return opt && odr_ok(o);
930 }
931
932 /*
933  * This is a temporary hack. We don't know just *what* old version of the
934  * protocol willow uses, so we'll just patiently wait for them to update
935  */
936 static int willow_scan = 0;
937
938 int z_WillowAttributesPlusTerm(ODR o, Z_AttributesPlusTerm **p, int opt)
939 {
940     if (!*p && o->direction != ODR_DECODE)
941         return opt;
942     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 4))
943     {
944         o->t_class = -1;
945         return opt && odr_ok(o);
946     }
947     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 1))
948         return 0;
949     if (!odr_constructed_begin(o, p, ODR_UNIVERSAL, ODR_SEQUENCE))
950         return 0;
951     if (!odr_implicit_settag(o, ODR_CONTEXT, 44))
952         return 0;
953     if (o->direction == ODR_DECODE)
954         *p = odr_malloc(o, sizeof(**p));
955     if (!odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
956         &(*p)->num_attributes))
957         return 0;
958     if (!odr_sequence_end(o) || !odr_sequence_end(o))
959         return 0;
960     if (!z_Term(o, &(*p)->term, 0))
961         return 0;
962     if (!odr_constructed_end(o))
963         return 0;
964     willow_scan = 1;
965     return 1;
966 }
967
968 int z_AlternativeTerm(ODR o, Z_AlternativeTerm **p, int opt)
969 {
970     if (o->direction == ODR_DECODE)
971         *p = odr_malloc(o, sizeof(**p));
972     else if (!*p)
973     {
974         o->t_class = -1;
975         return opt && odr_ok(o);
976     }
977
978     if (odr_sequence_of(o, z_AttributesPlusTerm, &(*p)->terms,
979         &(*p)->num_terms))
980         return 1;
981     *p = 0;
982     return opt && !o->error;
983 }
984
985 int z_OccurrenceByAttributes(ODR o, Z_OccurrenceByAttributes **p, int opt)
986 {
987     if (!odr_sequence_begin(o, p, sizeof(**p)))
988         return opt && odr_ok(o);
989     return
990         odr_explicit(o, z_AttributeList, &(*p)->attributes, ODR_CONTEXT, 1, 1)&&
991         odr_explicit(o, odr_integer, &(*p)->global, ODR_CONTEXT, 2, 1) &&
992         odr_sequence_end(o);
993 }
994
995 int z_TermInfo(ODR o, Z_TermInfo **p, int opt)
996 {
997     if (!odr_sequence_begin(o, p, sizeof(**p)))
998         return opt && odr_ok(o);
999     return
1000         (willow_scan ? 
1001             odr_implicit(o, z_Term, &(*p)->term, ODR_CONTEXT, 1, 0) :
1002             z_Term(o, &(*p)->term, 0)) &&
1003         z_AttributeList(o, &(*p)->suggestedAttributes, 1) &&
1004         odr_implicit(o, z_AlternativeTerm, &(*p)->alternativeTerm,
1005             ODR_CONTEXT, 4, 1) &&
1006         odr_implicit(o, odr_integer, &(*p)->globalOccurrences, ODR_CONTEXT,
1007             2, 1) &&
1008         odr_implicit(o, z_OccurrenceByAttributes, &(*p)->byAttributes,
1009             ODR_CONTEXT, 3, 1) &&
1010         odr_sequence_end(o);
1011 }
1012
1013 int z_Entry(ODR o, Z_Entry **p, int opt)
1014 {
1015     static Odr_arm arm[] =
1016     {
1017         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Entry_termInfo, z_TermInfo},
1018         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_Entry_surrogateDiagnostic,
1019             z_DiagRec},
1020         {-1, -1, -1, -1, 0}
1021     };
1022
1023     if (o->direction == ODR_DECODE)
1024         *p = odr_malloc(o, sizeof(**p));
1025     else if (!*p)
1026         return opt;
1027
1028     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
1029         return 1;
1030     *p = 0;
1031     return opt && odr_ok(o);
1032 }
1033
1034 int z_Entries(ODR o, Z_Entries **p, int opt)
1035 {
1036     if (o->direction == ODR_DECODE)
1037         *p = odr_malloc(o, sizeof(**p));
1038     else if (!*p)
1039         return opt;
1040
1041     if (odr_sequence_of(o, z_Entry, &(*p)->entries,
1042         &(*p)->num_entries))
1043         return 1;
1044     *p = 0;
1045     return 0;
1046 }
1047
1048 int z_ListEntries(ODR o, Z_ListEntries **p, int opt)
1049 {
1050     static Odr_arm arm[] =
1051     {
1052         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ListEntries_entries, z_Entries},
1053         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_ListEntries_nonSurrogateDiagnostics,
1054             z_DiagRecs},
1055         {-1, -1, -1, -1, 0}
1056     };
1057
1058     if (o->direction == ODR_DECODE)
1059         *p = odr_malloc(o, sizeof(**p));
1060     else if (!*p)
1061         return opt;
1062
1063     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
1064         return 1;
1065     *p = 0;
1066     return opt && odr_ok(o);
1067 }
1068
1069 int z_ScanRequest(ODR o, Z_ScanRequest **p, int opt)
1070 {
1071     if (!odr_sequence_begin(o, p, sizeof(**p)))
1072         return opt && odr_ok(o);
1073     willow_scan = 0;
1074     return
1075         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1076         odr_implicit_settag(o, ODR_CONTEXT, 3) &&
1077         odr_sequence_of(o, z_DatabaseName, &(*p)->databaseNames,
1078             &(*p)->num_databaseNames) &&
1079         odr_oid(o, &(*p)->attributeSet, 1) &&
1080         (z_AttributesPlusTerm(o, &(*p)->termListAndStartPoint, 1) ?
1081             ((*p)->termListAndStartPoint ? 1 : 
1082         z_WillowAttributesPlusTerm(o, &(*p)->termListAndStartPoint, 0)) : 0) &&
1083         odr_implicit(o, odr_integer, &(*p)->stepSize, ODR_CONTEXT, 5, 1) &&
1084         odr_implicit(o, odr_integer, &(*p)->numberOfTermsRequested,
1085             ODR_CONTEXT, 6, 0) &&
1086         odr_implicit(o, odr_integer, &(*p)->preferredPositionInResponse,
1087             ODR_CONTEXT, 7, 1) &&
1088         odr_sequence_end(o);
1089 }
1090
1091 int z_ScanResponse(ODR o, Z_ScanResponse **p, int opt)
1092 {
1093     if (!odr_sequence_begin(o, p, sizeof(**p)))
1094         return opt && odr_ok(o);
1095     return
1096         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1097         odr_implicit(o, odr_integer, &(*p)->stepSize, ODR_CONTEXT, 3, 1) &&
1098         odr_implicit(o, odr_integer, &(*p)->scanStatus, ODR_CONTEXT, 4, 0) &&
1099         odr_implicit(o, odr_integer, &(*p)->numberOfEntriesReturned,
1100             ODR_CONTEXT, 5, 0) &&
1101         odr_implicit(o, odr_integer, &(*p)->positionOfTerm, ODR_CONTEXT, 6, 1)&&
1102         odr_explicit(o, z_ListEntries, &(*p)->entries, ODR_CONTEXT, 7, 1) &&
1103         odr_implicit(o, odr_oid, &(*p)->attributeSet, ODR_CONTEXT, 8, 1) &&
1104         odr_sequence_end(o);
1105 }
1106
1107 /* ------------------------ SEARCHRESPONSE ----------------*/
1108
1109 int z_NumberOfRecordsReturned(ODR o, int **p, int opt)
1110 {
1111     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 24, opt);
1112 }
1113
1114 int z_NextResultSetPosition(ODR o, int **p, int opt)
1115 {
1116     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 25, opt);
1117 }
1118
1119 int z_PresentStatus(ODR o, int **p, int opt)
1120 {
1121     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 27, opt);
1122 }
1123
1124 int z_SearchResponse(ODR o, Z_SearchResponse **p, int opt)
1125 {
1126     Z_SearchResponse *pp;
1127
1128     if (!odr_sequence_begin(o, p, sizeof(**p)))
1129         return opt && odr_ok(o);
1130     pp = *p;
1131     return
1132         z_ReferenceId(o, &pp->referenceId, 1) &&
1133         odr_implicit(o, odr_integer, &pp->resultCount, ODR_CONTEXT, 23, 0) &&
1134         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
1135         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
1136         odr_implicit(o, odr_bool, &pp->searchStatus, ODR_CONTEXT, 22, 0) &&
1137         odr_implicit(o, odr_integer, &pp->resultSetStatus, ODR_CONTEXT, 26,
1138             1) &&
1139         z_PresentStatus(o, &pp->presentStatus, 1) &&
1140         z_Records(o, &pp->records, 1) &&
1141 #ifdef Z_95
1142         odr_implicit(o, z_OtherInformation, &(*p)->additionalSearchInfo,
1143             ODR_CONTEXT, 203, 1) &&
1144         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1145 #endif
1146         odr_sequence_end(o);
1147 }
1148
1149 /* --------------------- PRESENT SERVICE ---------------------- */
1150
1151 int z_ElementSpec(ODR o, Z_ElementSpec **p, int opt)
1152 {
1153     static Odr_arm arm[] =
1154     {
1155         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ElementSpec_elementSetName,
1156             odr_visiblestring},
1157         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_ElementSpec_externalSpec,
1158             odr_external},
1159         {-1, -1, -1, -1, 0}
1160     };
1161
1162     if (o->direction == ODR_DECODE)
1163         *p = odr_malloc(o, sizeof(**p));
1164     else if (!*p)
1165         return opt;
1166
1167     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
1168         return 1;
1169     *p = 0;
1170     return opt && odr_ok(o);
1171 }
1172
1173 int z_Specification(ODR o, Z_Specification **p, int opt)
1174 {
1175     if (!odr_sequence_begin(o, p, sizeof(**p)))
1176         return opt && odr_ok(o);
1177     return
1178         odr_implicit(o, odr_oid, &(*p)->schema, ODR_CONTEXT, 1, 1) &&
1179         z_ElementSpec(o, &(*p)->elementSpec, 1) &&
1180         odr_sequence_end(o);
1181 }
1182
1183 int z_DbSpecific(ODR o, Z_DbSpecific **p, int opt)
1184 {
1185     if (!odr_sequence_begin(o, p, sizeof(**p)))
1186         return opt && odr_ok(o);
1187     return
1188         odr_explicit(o, z_DatabaseName, &(*p)->databaseName, ODR_CONTEXT,
1189             1, 0) &&
1190         odr_implicit(o, z_Specification, &(*p)->spec, ODR_CONTEXT, 2, 0) &&
1191         odr_sequence_end(o);
1192 }
1193
1194 int z_CompSpec(ODR o, Z_CompSpec **p, int opt)
1195 {
1196     if (!odr_sequence_begin(o, p, sizeof(**p)))
1197         return opt && odr_ok(o);
1198     return
1199         odr_implicit(o, odr_bool, &(*p)->selectAlternativeSyntax, ODR_CONTEXT,
1200             1, 0) &&
1201         odr_implicit(o, z_Specification, &(*p)->generic, ODR_CONTEXT, 2, 1) &&
1202         odr_implicit_settag(o, ODR_CONTEXT, 3) &&
1203         (odr_sequence_of(o, z_DbSpecific, &(*p)->dbSpecific,
1204             &(*p)->num_dbSpecific) || odr_ok(o)) &&
1205         odr_implicit_settag(o, ODR_CONTEXT, 4) &&
1206         (odr_sequence_of(o, odr_oid, &(*p)->recordSyntax,
1207             &(*p)->num_recordSyntax) || odr_ok(o)) &&
1208         odr_sequence_end(o);
1209 }
1210
1211 int z_RecordComposition(ODR o, Z_RecordComposition **p, int opt)
1212 {
1213     static Odr_arm arm[] =
1214     {
1215         {ODR_EXPLICIT, ODR_CONTEXT, 1, Z_RecordComp_simple,
1216             z_ElementSetNames},
1217         {ODR_IMPLICIT, ODR_CONTEXT, 209, Z_RecordComp_complex,
1218             z_CompSpec},
1219         {-1, -1, -1, -1, 0}
1220     };
1221
1222     if (o->direction == ODR_DECODE)
1223         *p = odr_malloc(o, sizeof(**p));
1224     else if (!*p)
1225         return opt;
1226
1227     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
1228         return 1;
1229     *p = 0;
1230     return opt && odr_ok(o);
1231 }
1232
1233 int z_Range(ODR o, Z_Range **p, int opt)
1234 {
1235     if (!odr_sequence_begin(o, p, sizeof(**p)))
1236         return opt && odr_ok(o);
1237     return
1238         odr_implicit(o, odr_integer, &(*p)->startingPosition, ODR_CONTEXT,
1239             1, 0) &&
1240         odr_implicit(o, odr_integer, &(*p)->numberOfRecords, ODR_CONTEXT,
1241             2, 0) &&
1242         odr_sequence_end(o);
1243 }
1244
1245 int z_PresentRequest(ODR o, Z_PresentRequest **p, int opt)
1246 {
1247     Z_PresentRequest *pp;
1248
1249     if (!odr_sequence_begin(o, p, sizeof(**p)))
1250         return opt && odr_ok(o);
1251     pp = *p;
1252     return
1253         z_ReferenceId(o, &pp->referenceId, 1) &&
1254         z_ResultSetId(o, &pp->resultSetId, 0) &&
1255         odr_implicit(o, odr_integer, &pp->resultSetStartPoint, ODR_CONTEXT,
1256             30, 0) &&
1257         odr_implicit(o, odr_integer, &pp->numberOfRecordsRequested, ODR_CONTEXT,
1258             29, 0) &&
1259 #ifdef Z_95
1260         odr_implicit_settag(o, ODR_CONTEXT, 212) &&
1261         (odr_sequence_of(o, z_Range, &(*p)->additionalRanges,
1262             &(*p)->num_ranges) || odr_ok(o)) &&
1263         z_RecordComposition(o, &(*p)->recordComposition, 1) &&
1264 #else
1265         z_ElementSetNames(o, &pp->elementSetNames, 1) &&
1266 #endif
1267         odr_implicit(o, odr_oid, &(*p)->preferredRecordSyntax, ODR_CONTEXT,
1268             104, 1) &&
1269 #ifdef Z_95
1270         odr_implicit(o, odr_integer, &(*p)->maxSegmentCount, ODR_CONTEXT,
1271             204, 1) &&
1272         odr_implicit(o, odr_integer, &(*p)->maxRecordSize, ODR_CONTEXT,
1273             206, 1) &&
1274         odr_implicit(o, odr_integer, &(*p)->maxSegmentSize, ODR_CONTEXT,
1275             207, 1) &&
1276         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1277 #endif
1278         odr_sequence_end(o);
1279 }
1280
1281 int z_PresentResponse(ODR o, Z_PresentResponse **p, int opt)
1282 {
1283     Z_PresentResponse *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         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
1291         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
1292         z_PresentStatus(o, &pp->presentStatus, 0) &&
1293         z_Records(o, &pp->records, 1) &&
1294 #ifdef Z_95
1295         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1296 #endif
1297         odr_sequence_end(o);
1298 }
1299
1300 /* ----------------------DELETE -------------------------- */
1301
1302 int z_DeleteSetStatus(ODR o, int **p, int opt)
1303 {
1304     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 33, opt);
1305 }
1306
1307 int z_ListStatus(ODR o, Z_ListStatus **p, int opt)
1308 {
1309     if (!odr_sequence_begin(o, p, sizeof(**p)))
1310         return opt && odr_ok(o);
1311     return
1312         z_ResultSetId(o, &(*p)->id, 0) &&
1313         z_DeleteSetStatus(o, &(*p)->status, 0) &&
1314         odr_sequence_end(o);
1315 }
1316
1317 int z_DeleteResultSetRequest(ODR o, Z_DeleteResultSetRequest **p, int opt)
1318 {
1319     if (!odr_sequence_begin(o, p, sizeof(**p)))
1320         return opt && odr_ok(o);
1321     return
1322         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1323         odr_implicit(o, odr_integer, &(*p)->deleteFunction, ODR_CONTEXT, 32,
1324             0) &&
1325         (odr_sequence_of(o, z_ListStatus, &(*p)->resultSetList,
1326             &(*p)->num_ids) || odr_ok(o)) &&
1327 #ifdef Z_95
1328         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1329 #endif
1330         odr_sequence_end(o);
1331 }
1332
1333 int z_DeleteResultSetResponse(ODR o, Z_DeleteResultSetResponse **p, int opt)
1334 {
1335     if (!odr_sequence_begin(o, p, sizeof(**p)))
1336         return opt && odr_ok(o);
1337     return
1338         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1339         odr_implicit(o, z_DeleteSetStatus, &(*p)->deleteOperationStatus,
1340             ODR_CONTEXT, 0, 1) &&
1341         odr_implicit_settag(o, ODR_CONTEXT, 1) &&
1342         (odr_sequence_of(o, z_ListStatus, &(*p)->deleteListStatuses,
1343             &(*p)->num_statuses) || odr_ok(o)) &&
1344         odr_implicit(o, odr_integer, &(*p)->numberNotDeleted, ODR_CONTEXT,
1345             34, 1) &&
1346         odr_implicit_settag(o, ODR_CONTEXT, 35) &&
1347         (odr_sequence_of(o, z_ListStatus, &(*p)->bulkStatuses,
1348             &(*p)->num_bulkStatuses) || odr_ok(o)) &&
1349         odr_implicit(o, odr_visiblestring, &(*p)->deleteMessage, ODR_CONTEXT,
1350             36, 1) &&
1351 #ifdef Z_95
1352         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1353 #endif
1354         odr_sequence_end(o);
1355 }
1356
1357 /* ------------------------ SEGMENT SERVICE -------------- */
1358
1359 int z_Segment(ODR o, Z_Segment **p, int opt)
1360 {
1361     if (!odr_sequence_begin(o, p, sizeof(**p)))
1362         return opt && odr_ok(o);
1363     return
1364         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1365         odr_implicit(o, odr_integer, &(*p)->numberOfRecordsReturned,
1366             ODR_CONTEXT, 24, 0) &&
1367         odr_implicit_settag(o, ODR_CONTEXT, 0) &&
1368         odr_sequence_of(o, z_NamePlusRecord, &(*p)->segmentRecords,
1369             &(*p)->num_segmentRecords) &&
1370         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1371         odr_sequence_end(o);
1372 }
1373
1374 /* ------------------------ CLOSE SERVICE ---------------- */
1375
1376 int z_Close(ODR o, Z_Close **p, int opt)
1377 {
1378     if (!odr_sequence_begin(o, p, sizeof(**p)))
1379         return opt && odr_ok(o);
1380     return
1381         z_ReferenceId(o, &(*p)->referenceId, 1) &&
1382         odr_implicit(o, odr_integer, &(*p)->closeReason, ODR_CONTEXT, 211, 0) &&
1383         odr_implicit(o, odr_visiblestring, &(*p)->diagnosticInformation,
1384             ODR_CONTEXT, 3, 1) &&
1385         odr_implicit(o, odr_oid, &(*p)->resourceReportFormat, ODR_CONTEXT,
1386             4, 1) &&
1387         odr_implicit(o, odr_external, &(*p)->resourceReport, ODR_CONTEXT,
1388             5, 1) &&
1389 #ifdef Z_95
1390         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
1391 #endif
1392         odr_sequence_end(o);
1393 }
1394
1395 /* ------------------------ APDU ------------------------- */
1396
1397 int z_APDU(ODR o, Z_APDU **p, int opt)
1398 {
1399     static Odr_arm arm[] =
1400     {
1401         {ODR_IMPLICIT, ODR_CONTEXT, 20, Z_APDU_initRequest, z_InitRequest},
1402         {ODR_IMPLICIT, ODR_CONTEXT, 21, Z_APDU_initResponse, z_InitResponse},
1403         {ODR_IMPLICIT, ODR_CONTEXT, 22, Z_APDU_searchRequest, z_SearchRequest},
1404         {ODR_IMPLICIT, ODR_CONTEXT, 23, Z_APDU_searchResponse,
1405             z_SearchResponse},
1406         {ODR_IMPLICIT, ODR_CONTEXT, 24, Z_APDU_presentRequest,
1407             z_PresentRequest},
1408         {ODR_IMPLICIT, ODR_CONTEXT, 25, Z_APDU_presentResponse,
1409             z_PresentResponse},
1410         {ODR_IMPLICIT, ODR_CONTEXT, 26, Z_APDU_deleteResultSetRequest,
1411             z_DeleteResultSetRequest},
1412         {ODR_IMPLICIT, ODR_CONTEXT, 27, Z_APDU_deleteResultSetResponse,
1413             z_DeleteResultSetResponse},
1414         {ODR_IMPLICIT, ODR_CONTEXT, 30, Z_APDU_resourceControlRequest,
1415             z_ResourceControlRequest},
1416         {ODR_IMPLICIT, ODR_CONTEXT, 31, Z_APDU_resourceControlResponse,
1417             z_ResourceControlResponse},
1418         {ODR_IMPLICIT, ODR_CONTEXT, 32, Z_APDU_triggerResourceControlRequest,
1419             z_TriggerResourceControlRequest},
1420         {ODR_IMPLICIT, ODR_CONTEXT, 35, Z_APDU_scanRequest, z_ScanRequest},
1421         {ODR_IMPLICIT, ODR_CONTEXT, 36, Z_APDU_scanResponse, z_ScanResponse},
1422         {ODR_IMPLICIT, ODR_CONTEXT, 45, Z_APDU_segmentRequest, z_Segment},
1423         {ODR_IMPLICIT, ODR_CONTEXT, 48, Z_APDU_close, z_Close},
1424
1425         {-1, -1, -1, -1, 0}
1426     };
1427
1428     if (o->direction == ODR_DECODE)
1429         *p = odr_malloc(o, sizeof(**p));
1430     if (!odr_choice(o, arm, &(*p)->u, &(*p)->which))
1431     {
1432         if (o->direction == ODR_DECODE)
1433             *p = 0;
1434         return opt && odr_ok(o);
1435     }
1436     return 1;
1437 }