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