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