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