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