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