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