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