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