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