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