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