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