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