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