Added delete. Fixed some sequence_begins. Smallish.
[yaz-moved-to-github.git] / asn / proto.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: proto.c,v $
7  * Revision 1.22  1995-05-17 08:40:56  quinn
8  * Added delete. Fixed some sequence_begins. Smallish.
9  *
10  * Revision 1.21  1995/05/16  08:50:24  quinn
11  * License, documentation, and memory fixes
12  *
13  * Revision 1.20  1995/05/15  11:55:25  quinn
14  * Smallish.
15  *
16  * Revision 1.19  1995/04/11  11:58:35  quinn
17  * Fixed bug.
18  *
19  * Revision 1.18  1995/04/11  11:52:02  quinn
20  * Fixed possible buf in proto.c
21  *
22  * Revision 1.17  1995/04/10  10:22:22  quinn
23  * Added SCAN.
24  *
25  * Revision 1.16  1995/03/30  10:26:43  quinn
26  * Added Term structure
27  *
28  * Revision 1.15  1995/03/30  09:08:39  quinn
29  * Added Resource control protocol
30  *
31  * Revision 1.14  1995/03/29  08:06:13  quinn
32  * Added a few v3 elements
33  *
34  * Revision 1.13  1995/03/20  11:26:52  quinn
35  * *** empty log message ***
36  *
37  * Revision 1.12  1995/03/20  09:45:09  quinn
38  * Working towards v3
39  *
40  * Revision 1.11  1995/03/17  10:17:25  quinn
41  * Added memory management.
42  *
43  * Revision 1.10  1995/03/15  11:17:40  quinn
44  * Fixed some return-checks from choice.. need better ay to handle those..
45  *
46  * Revision 1.9  1995/03/15  08:37:06  quinn
47  * Fixed protocol bugs.
48  *
49  * Revision 1.8  1995/03/14  16:59:24  quinn
50  * Fixed OPTIONAL flag in attributeelement
51  *
52  * Revision 1.7  1995/03/07  16:29:33  quinn
53  * Added authentication stuff.
54  *
55  * Revision 1.6  1995/03/01  14:46:03  quinn
56  * Fixed protocol bug in 8777query.
57  *
58  * Revision 1.5  1995/02/14  11:54:22  quinn
59  * Fixing include.
60  *
61  * Revision 1.4  1995/02/10  15:54:30  quinn
62  * Small adjustments.
63  *
64  * Revision 1.3  1995/02/09  15:51:39  quinn
65  * Works better now.
66  *
67  * Revision 1.2  1995/02/06  21:26:07  quinn
68  * Repaired this evening's damages..
69  *
70  * Revision 1.1  1995/02/06  16:44:47  quinn
71  * First hack at Z/SR protocol
72  *
73  */
74
75 #include <odr.h>
76
77 #include <proto.h>
78
79 /* ---------------------- GLOBAL DEFS ------------------- */
80
81 int z_ReferenceId(ODR o, Z_ReferenceId **p, int opt)
82 {
83     return odr_implicit(o, odr_octetstring, (Odr_oct**) p, ODR_CONTEXT, 2, opt);
84 }
85
86 int z_DatabaseName(ODR o, Z_DatabaseName **p, int opt)
87 {
88     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 105,
89         opt);
90 }
91
92 int z_ResultSetId(ODR o, char **p, int opt)
93 {
94     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 31,
95         opt);
96 }
97
98 int z_UserInformationField(ODR o, Z_UserInformationField **p, int opt)
99 {
100     return odr_explicit(o, odr_external, (Odr_external **)p, ODR_CONTEXT,
101         11, opt);
102 }
103
104 /* ---------------------- INITIALIZE SERVICE ------------------- */
105
106 int z_NSRAuthentication(ODR o, Z_NSRAuthentication **p, int opt)
107 {
108     if (!odr_sequence_begin(o, p, sizeof(**p)))
109         return opt && odr_ok(o);
110     return
111         odr_visiblestring(o, &(*p)->user, 0) &&
112         odr_visiblestring(o, &(*p)->password, 0) &&
113         odr_visiblestring(o, &(*p)->account, 0) &&
114         odr_sequence_end(o);
115 }
116
117 int z_IdPass(ODR o, Z_IdPass **p, int opt)
118 {
119     if (!odr_sequence_begin(o, p, sizeof(**p)))
120         return opt && odr_ok(o);
121     return
122         odr_implicit(o, odr_visiblestring, &(*p)->groupId, ODR_CONTEXT, 0, 0) &&
123         odr_implicit(o, odr_visiblestring, &(*p)->userId, ODR_CONTEXT, 1, 0) &&
124         odr_implicit(o, odr_visiblestring, &(*p)->password, ODR_CONTEXT, 2,
125             0) &&
126         odr_sequence_end(o);
127 }
128
129 int z_StrAuthentication(ODR o, char **p, int opt)
130 {
131     return odr_visiblestring(o, p, opt);
132 }
133
134 int z_IdAuthentication(ODR o, Z_IdAuthentication **p, int opt)
135 {
136     static Odr_arm arm[] =
137     {
138         {-1, -1, -1, Z_IdAuthentication_open, z_StrAuthentication},
139         {-1, -1, -1, Z_IdAuthentication_idPass, z_NSRAuthentication},
140         {-1, -1, -1, Z_IdAuthentication_anonymous, odr_null},
141         {-1, -1, -1, Z_IdAuthentication_other, odr_external},
142         {-1, -1, -1, -1, 0}
143     };
144
145     if (o->direction == ODR_DECODE)
146         *p = odr_malloc(o, sizeof(**p));
147
148     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
149         return 1;
150     *p = 0;
151     return opt && odr_ok(o);
152 }
153
154 int z_InitRequest(ODR o, Z_InitRequest **p, int opt)
155 {
156     Z_InitRequest *pp;
157
158     if (!odr_sequence_begin(o, p, sizeof(**p)))
159         return opt && odr_ok(o);
160     pp = *p;
161     return
162         z_ReferenceId(o, &pp->referenceId, 1) &&
163         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
164             3, 0) &&
165         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
166         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
167             5, 0) &&
168         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
169             6, 0) &&
170         odr_explicit(o, z_IdAuthentication, &pp->idAuthentication, ODR_CONTEXT,
171             7, 1) &&
172         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
173             110, 1) &&
174         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
175             111, 1) &&
176         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
177             ODR_CONTEXT, 112, 1) &&
178         z_UserInformationField(o, &pp->userInformationField, 1) &&
179 #ifdef Z_OTHERINFO
180         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
181 #endif
182         odr_sequence_end(o);
183 }
184
185 int z_InitResponse(ODR o, Z_InitResponse **p, int opt)
186 {
187     Z_InitResponse *pp;
188
189     if (!odr_sequence_begin(o, p, sizeof(**p)))
190         return opt && odr_ok(o);
191     pp = *p;
192     return
193         z_ReferenceId(o, &pp->referenceId, 1) &&
194         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
195             3, 0) &&
196         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
197         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
198             5, 0) &&
199         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
200             6, 0) &&
201         odr_implicit(o, odr_bool, &pp->result, ODR_CONTEXT, 12, 0) &&
202         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
203             110, 1) &&
204         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
205             111, 1) &&
206         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
207             ODR_CONTEXT, 112, 1) &&
208         z_UserInformationField(o, &pp->userInformationField, 1) &&
209 #ifdef Z_OTHERINFO
210         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
211 #endif
212         odr_sequence_end(o);
213 }
214
215 /* ------------------ RESOURCE CONTROL ----------------*/
216
217 int z_TriggerResourceControlRequest(ODR o, Z_TriggerResourceControlRequest **p,
218                                     int opt)
219 {
220     if (!odr_sequence_begin(o, p, sizeof(**p)))
221         return opt && odr_ok(o);
222     return
223         z_ReferenceId(o, &(*p)->referenceId, 1) &&
224         odr_implicit(o, odr_integer, &(*p)->requestedAction, ODR_CONTEXT,
225             46, 0) &&
226         odr_implicit(o, odr_oid, &(*p)->prefResourceReportFormat,
227             ODR_CONTEXT, 47, 1) &&
228         odr_implicit(o, odr_bool, &(*p)->resultSetWanted, ODR_CONTEXT,
229             48, 1) &&
230 #ifdef Z_OTHERINFO
231         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
232 #endif
233         odr_sequence_end(o);
234 }
235
236 int z_ResourceControlRequest(ODR o, Z_ResourceControlRequest **p, int opt)
237 {
238     if (!odr_sequence_begin(o, p, sizeof(**p)))
239         return opt && odr_ok(o);
240     return
241         z_ReferenceId(o, &(*p)->referenceId, 1) &&
242         odr_implicit(o, odr_bool, &(*p)->suspendedFlag, ODR_CONTEXT, 39, 1)&&
243         odr_explicit(o, odr_external, &(*p)->resourceReport, ODR_CONTEXT,
244             40, 1) &&
245         odr_implicit(o, odr_integer, &(*p)->partialResultsAvailable,
246             ODR_CONTEXT, 41, 1) &&
247         odr_implicit(o, odr_bool, &(*p)->responseRequired, ODR_CONTEXT,
248             42, 0) &&
249         odr_implicit(o, odr_bool, &(*p)->triggeredRequestFlag,
250             ODR_CONTEXT, 43, 1) &&
251 #ifdef Z_OTHERINFO
252         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
253 #endif
254         odr_sequence_end(o);
255 }
256
257 int z_ResourceControlResponse(ODR o, Z_ResourceControlResponse **p, int opt)
258 {
259     if (!odr_sequence_begin(o, p, sizeof(**p)))
260         return opt && odr_ok(o);
261     return
262         z_ReferenceId(o, &(*p)->referenceId, 1) &&
263         odr_implicit(o, odr_bool, &(*p)->continueFlag, ODR_CONTEXT, 44, 0) &&
264         odr_implicit(o, odr_bool, &(*p)->resultSetWanted, ODR_CONTEXT,
265             45, 1) &&
266 #ifdef Z_OTHERINFO
267         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
268 #endif
269         odr_sequence_end(o);
270 }
271
272 /* ------------------------ SEARCH SERVICE ----------------------- */
273
274 int z_ElementSetName(ODR o, char **p, int opt)
275 {
276     return odr_implicit(o, odr_visiblestring, (char**) p, ODR_CONTEXT, 103,
277         opt);
278 }
279
280 int z_PreferredRecordSyntax(ODR o, Z_PreferredRecordSyntax **p, int opt)
281 {
282     return odr_implicit(o, odr_oid, (Odr_oid**) p, ODR_CONTEXT, 104, opt);
283 }
284
285 int z_DatabaseSpecificUnit(ODR o, Z_DatabaseSpecificUnit **p, int opt)
286 {
287     if (!odr_sequence_begin(o, p, sizeof(**p)))
288         return opt && odr_ok(o);
289     return
290         z_DatabaseName(o, &(*p)->databaseName, 0) &&
291         z_ElementSetName(o, &(*p)->elementSetName, 0) &&
292         odr_sequence_end(o);
293 }
294
295 int z_DatabaseSpecific(ODR o, Z_DatabaseSpecific **p, int opt)
296 {
297     if (o->direction == ODR_DECODE)
298         *p = odr_malloc(o, sizeof(**p));
299     else if (!*p)
300         return opt;
301
302     odr_implicit_settag(o, ODR_CONTEXT, 1);
303     if (odr_sequence_of(o, z_DatabaseSpecificUnit, &(*p)->elements,
304         &(*p)->num_elements))
305         return 1;
306     *p = 0;
307     return 0;
308 }
309
310 int z_ElementSetNames(ODR o, Z_ElementSetNames **p, int opt)
311 {
312     static Odr_arm arm[] =
313     {
314         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_ElementSetNames_generic,
315             z_ElementSetName},
316         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ElementSetNames_databaseSpecific,
317             z_DatabaseSpecific},
318         {-1, -1, -1, -1, 0}
319     };
320
321     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 19))
322         return opt && odr_ok(o);
323
324     if (o->direction == ODR_DECODE)
325         *p = odr_malloc(o, sizeof(**p));
326
327     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
328         odr_constructed_end(o))
329         return 1;
330     *p = 0;
331     return 0;
332 }
333
334 /* ----------------------- RPN QUERY -----------------------*/
335
336 int z_AttributeElement(ODR o, Z_AttributeElement **p, int opt)
337 {
338     if (!odr_sequence_begin(o, p, sizeof(**p)))
339         return opt && odr_ok(o);
340     return
341         odr_implicit(o, odr_integer, &(*p)->attributeType, ODR_CONTEXT,
342             120, 0) &&
343         odr_implicit(o, odr_integer, &(*p)->attributeValue, ODR_CONTEXT,
344             121, 0) &&
345         odr_sequence_end(o);
346 }
347
348 #ifdef Z_V3
349
350 int z_Term(ODR o, Z_Term **p, int opt)
351 {
352     static Odr_arm arm[] =
353     {
354         {ODR_IMPLICIT, ODR_CONTEXT, 45, Z_Term_general, odr_octetstring},
355         {ODR_IMPLICIT, ODR_CONTEXT, 215, Z_Term_numeric, odr_integer},
356         {ODR_IMPLICIT, ODR_CONTEXT, 216, Z_Term_characterString,
357             odr_visiblestring},
358         {ODR_IMPLICIT, ODR_CONTEXT, 217, Z_Term_oid, odr_oid},
359         {ODR_IMPLICIT, ODR_CONTEXT, 218, Z_Term_dateTime, odr_cstring},
360         {ODR_IMPLICIT, ODR_CONTEXT, 219, Z_Term_external, odr_external},
361         /* add intUnit here */
362         {ODR_IMPLICIT, ODR_CONTEXT, 221, Z_Term_null, odr_null},
363         {-1, -1, -1, -1, 0}
364     };
365
366     if (o->direction ==ODR_DECODE)
367         *p = odr_malloc(o, sizeof(**p));
368     else if (!*p)
369         return opt;
370     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
371         return 1;
372     *p = 0;
373     return opt && odr_ok(o);
374 }
375
376 #endif
377
378 int z_AttributesPlusTerm(ODR o, Z_AttributesPlusTerm **p, int opt)
379 {
380     if (!(odr_implicit_settag(o, ODR_CONTEXT, 102) &&
381         odr_sequence_begin(o, p, sizeof(**p))))
382         return opt && odr_ok(o);
383     return
384         odr_implicit_settag(o, ODR_CONTEXT, 44) &&
385         odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
386             &(*p)->num_attributes) &&
387         z_Term(o, &(*p)->term, 0) &&
388         odr_sequence_end(o);
389 }
390
391 int z_Operator(ODR o, Z_Operator **p, int opt)
392 {
393     static Odr_arm arm[] =
394     {
395         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_Operator_and, odr_null},
396         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Operator_or, odr_null},
397         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_Operator_and_not, odr_null},
398         {-1, -1, -1, -1, 0}
399     };
400     int dummy = 999;
401
402     if (!*p && o->direction != ODR_DECODE)
403         return opt;
404     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 46))
405         return opt && odr_ok(o);
406     if (o->direction == ODR_DECODE)
407         *p = odr_malloc(o, sizeof(**p));
408     else
409         (*p)->u.and = &dummy;
410
411     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
412         odr_constructed_end(o))
413         return 1;
414     *p = 0;
415     return opt && odr_ok(o);
416 }
417
418 int z_Operand(ODR o, Z_Operand **p, int opt)
419 {
420     static Odr_arm arm[] =
421     {
422         {-1, -1, -1, Z_Operand_APT, z_AttributesPlusTerm},
423         {-1, -1, -1, Z_Operand_resultSetId, z_ResultSetId},
424         {-1, -1, -1, -1, 0}
425     };
426
427     if (o->direction ==ODR_DECODE)
428         *p = odr_malloc(o, sizeof(**p));
429     else if (!*p)
430         return opt;
431     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
432         return 1;
433     *p = 0;
434     return opt && odr_ok(o);
435 }
436
437 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt);
438
439 int z_Complex(ODR o, Z_Complex **p, int opt)
440 {
441     if (!odr_sequence_begin(o, p, sizeof(**p)))
442         return opt && odr_ok(o);
443     return
444         z_RPNStructure(o, &(*p)->s1, 0) &&
445         z_RPNStructure(o, &(*p)->s2, 0) &&
446         z_Operator(o, &(*p)->operator, 0) &&
447         odr_sequence_end(o);
448 }
449
450 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt)
451 {
452     static Odr_arm arm[] = 
453     {
454         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_RPNStructure_simple, z_Operand},
455         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_RPNStructure_complex, z_Complex},
456         {-1 -1, -1, -1, 0}
457     };
458
459     if (o->direction == ODR_DECODE)
460         *p = odr_malloc(o, sizeof(**p));
461     else if (!*p)
462         return opt;
463     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
464         return 1;
465     *p = 0;
466     return opt && odr_ok(o);
467 }
468
469 int z_RPNQuery(ODR o, Z_RPNQuery **p, int opt)
470 {
471     if (!odr_sequence_begin(o, p, sizeof(**p)))
472         return opt && odr_ok(o);
473     return
474         odr_oid(o, &(*p)->attributeSetId, 0) &&
475         z_RPNStructure(o, &(*p)->RPNStructure, 0) &&
476         odr_sequence_end(o);
477 }
478
479 /* -----------------------END RPN QUERY ----------------------- */
480
481 int z_Query(ODR o, Z_Query **p, int opt)
482 {
483     static Odr_arm arm[] = 
484     {
485         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Query_type_1, z_RPNQuery},
486         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_Query_type_2, odr_octetstring},
487         {-1, -1, -1, -1, 0}
488     };
489
490     if (o->direction == ODR_DECODE)
491         *p = odr_malloc(o, sizeof(**p));
492     else if (!*p)
493         return opt;
494     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
495         return 1;
496     *p = 0;
497     return opt && odr_ok(o);
498 }
499
500 int z_SearchRequest(ODR o, Z_SearchRequest **p, int opt)
501 {
502     Z_SearchRequest *pp;
503
504     if (!odr_sequence_begin(o, p, sizeof(**p)))
505         return opt && odr_ok(o);
506     pp = *p;
507     return
508         z_ReferenceId(o, &pp->referenceId, 1) &&
509         odr_implicit(o, odr_integer, &pp->smallSetUpperBound, ODR_CONTEXT,
510             13, 0) &&
511         odr_implicit(o, odr_integer, &pp->largeSetLowerBound, ODR_CONTEXT,
512             14, 0) &&
513         odr_implicit(o, odr_integer, &pp->mediumSetPresentNumber, ODR_CONTEXT,
514             15, 0) &&
515         odr_implicit(o, odr_bool, &pp->replaceIndicator, ODR_CONTEXT, 16, 1) &&
516         odr_implicit(o, odr_visiblestring, &pp->resultSetName, ODR_CONTEXT,
517             17, 9) &&
518         odr_implicit_settag(o, ODR_CONTEXT, 18) &&
519         odr_sequence_of(o, z_DatabaseName, &pp->databaseNames,
520             &pp->num_databaseNames) &&
521         odr_explicit(o, z_ElementSetNames, &pp->smallSetElementSetNames,
522             ODR_CONTEXT, 100, 1) &&
523         odr_explicit(o, z_ElementSetNames, &pp->mediumSetElementSetNames,
524             ODR_CONTEXT, 101, 1) &&
525         odr_implicit(o, z_PreferredRecordSyntax, &pp->preferredRecordSyntax,
526             ODR_CONTEXT, 104, 1) &&
527         odr_explicit(o, z_Query, &pp->query, ODR_CONTEXT, 21, 0) &&
528 #ifdef Z_OTHERINFO
529         odr_implicit(o, z_OtherInformation, &(*p)->additionalSearchInfo,
530             ODR_CONTEXT, 203, 1) &&
531         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
532 #endif
533         odr_sequence_end(o);
534 }
535
536 /* ------------------------ RECORD ------------------------- */
537
538 int z_DatabaseRecord(ODR o, Z_DatabaseRecord **p, int opt)
539 {
540     return odr_external(o, (Odr_external **) p, opt);
541 }
542
543 int z_DiagRec(ODR o, Z_DiagRec **p, int opt)
544 {
545     if (!odr_sequence_begin(o, p, sizeof(**p)))
546         return opt && odr_ok(o);
547     return
548         odr_oid(o, &(*p)->diagnosticSetId, 1) &&       /* SHOULD NOT BE OPT */
549         odr_integer(o, &(*p)->condition, 0) &&
550         (odr_visiblestring(o, &(*p)->addinfo, 0) ||
551         odr_implicit(o, odr_cstring, &(*p)->addinfo, ODR_CONTEXT, ODR_VISIBLESTRING, 1)) &&
552         odr_sequence_end(o);
553 }
554
555 int z_NamePlusRecord(ODR o, Z_NamePlusRecord **p, int opt)
556 {
557     static Odr_arm arm[] =
558     {
559         {ODR_EXPLICIT, ODR_CONTEXT, 1, Z_NamePlusRecord_databaseRecord,
560             z_DatabaseRecord},
561         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_NamePlusRecord_surrogateDiagnostic,
562             z_DiagRec},
563         {-1, -1, -1, -1, 0}
564     };
565
566     if (!odr_sequence_begin(o, p, sizeof(**p)))
567         return opt && odr_ok(o);
568     return
569         odr_implicit(o, z_DatabaseName, &(*p)->databaseName, ODR_CONTEXT,
570             0, 1) &&
571         odr_constructed_begin(o, &(*p)->u, ODR_CONTEXT, 1) &&
572         odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
573         odr_constructed_end(o) &&
574         odr_sequence_end(o);
575 }
576
577 int z_NamePlusRecordList(ODR o, Z_NamePlusRecordList **p, int opt)
578 {
579     if (o->direction == ODR_DECODE)
580         *p = odr_malloc(o, sizeof(**p));
581     if (odr_sequence_of(o, z_NamePlusRecord, &(*p)->records,
582         &(*p)->num_records))
583         return 1;
584     *p = 0;
585     return 0;
586 }
587
588 int z_Records(ODR o, Z_Records **p, int opt)
589 {
590     Odr_arm arm[] = 
591     {
592         {ODR_IMPLICIT, ODR_CONTEXT, 28, Z_Records_DBOSD, z_NamePlusRecordList},
593         {ODR_IMPLICIT, ODR_CONTEXT, 130, Z_Records_NSD, z_DiagRec},
594         {-1, -1, -1, -1, 0}
595     };
596
597     if (o->direction == ODR_DECODE)
598         *p = odr_malloc(o, sizeof(**p));
599     else if (!*p)
600         return opt;
601     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
602         return 1;
603     *p = 0;
604     return opt && odr_ok(o);
605 }
606
607 /* ------------------------ SCAN SERVICE -------------------- */
608
609 int z_AttributeList(ODR o, Z_AttributeList **p, int opt)
610 {
611     if (o->direction == ODR_DECODE)
612         *p = odr_malloc(o, sizeof(**p));
613     else if (!*p)
614         return opt;
615
616     odr_implicit_settag(o, ODR_CONTEXT, 44);
617     if (odr_sequence_of(o, z_AttributeElement, &(*p)->attributes,
618         &(*p)->num_attributes))
619         return 1;
620     *p = 0;
621     return opt && odr_ok(o);
622 }
623
624 /*
625  * This is a temporary hack. We don't know just *what* old version of the
626  * protocol willow uses, so we'll just patiently wait for them to update
627  */
628 static int willow_scan = 0;
629
630 int z_WillowAttributesPlusTerm(ODR o, Z_AttributesPlusTerm **p, int opt)
631 {
632     if (!*p && o->direction != ODR_DECODE)
633         return opt;
634     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 4))
635     {
636         o->t_class = -1;
637         return opt && odr_ok(o);
638     }
639     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 1))
640         return 0;
641     if (!odr_constructed_begin(o, p, ODR_UNIVERSAL, ODR_SEQUENCE))
642         return 0;
643     if (!odr_implicit_settag(o, ODR_CONTEXT, 44))
644         return 0;
645     if (o->direction == ODR_DECODE)
646         *p = odr_malloc(o, sizeof(**p));
647     if (!odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
648         &(*p)->num_attributes))
649         return 0;
650     if (!odr_sequence_end(o) || !odr_sequence_end(o))
651         return 0;
652     if (!z_Term(o, &(*p)->term, 0))
653         return 0;
654     if (!odr_constructed_end(o))
655         return 0;
656     willow_scan = 1;
657     return 1;
658 }
659
660 int z_AlternativeTerm(ODR o, Z_AlternativeTerm **p, int opt)
661 {
662     if (o->direction == ODR_DECODE)
663         *p = odr_malloc(o, sizeof(**p));
664     else if (!*p)
665     {
666         o->t_class = -1;
667         return opt && odr_ok(o);
668     }
669
670     if (odr_sequence_of(o, z_AttributesPlusTerm, &(*p)->terms,
671         &(*p)->num_terms))
672         return 1;
673     *p = 0;
674     return opt && !o->error;
675 }
676
677 int z_OccurrenceByAttributes(ODR o, Z_OccurrenceByAttributes **p, int opt)
678 {
679     if (!odr_sequence_begin(o, p, sizeof(**p)))
680         return opt && odr_ok(o);
681     return
682         odr_explicit(o, z_AttributeList, &(*p)->attributes, ODR_CONTEXT, 1, 1)&&
683         odr_explicit(o, odr_integer, &(*p)->global, ODR_CONTEXT, 2, 1) &&
684         odr_sequence_end(o);
685 }
686
687 int z_TermInfo(ODR o, Z_TermInfo **p, int opt)
688 {
689     if (!odr_sequence_begin(o, p, sizeof(**p)))
690         return opt && odr_ok(o);
691     return
692         (willow_scan ? 
693             odr_implicit(o, z_Term, &(*p)->term, ODR_CONTEXT, 1, 0) :
694             z_Term(o, &(*p)->term, 0)) &&
695         z_AttributeList(o, &(*p)->suggestedAttributes, 1) &&
696         odr_implicit(o, z_AlternativeTerm, &(*p)->alternativeTerm,
697             ODR_CONTEXT, 4, 1) &&
698         odr_implicit(o, odr_integer, &(*p)->globalOccurrences, ODR_CONTEXT,
699             2, 1) &&
700         odr_implicit(o, z_OccurrenceByAttributes, &(*p)->byAttributes,
701             ODR_CONTEXT, 3, 1) &&
702         odr_sequence_end(o);
703 }
704
705 int z_Entry(ODR o, Z_Entry **p, int opt)
706 {
707     static Odr_arm arm[] =
708     {
709         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Entry_termInfo, z_TermInfo},
710         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_Entry_surrogateDiagnostic,
711             z_DiagRec},
712         {-1, -1, -1, -1, 0}
713     };
714
715     if (o->direction == ODR_DECODE)
716         *p = odr_malloc(o, sizeof(**p));
717
718     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
719         return 1;
720     *p = 0;
721     return opt && odr_ok(o);
722 }
723
724 int z_Entries(ODR o, Z_Entries **p, int opt)
725 {
726     if (o->direction == ODR_DECODE)
727         *p = odr_malloc(o, sizeof(**p));
728     else if (!*p)
729         return opt;
730
731     if (odr_sequence_of(o, z_Entry, &(*p)->entries,
732         &(*p)->num_entries))
733         return 1;
734     *p = 0;
735     return 0;
736 }
737
738 int z_DiagRecs(ODR o, Z_DiagRecs **p, int opt)
739 {
740     if (o->direction == ODR_DECODE)
741         *p = odr_malloc(o, sizeof(**p));
742     else if (!*p)
743         return opt;
744
745         if (odr_sequence_of(o, z_DiagRec, &(*p)->diagRecs,
746         &(*p)->num_diagRecs))
747         return 1;
748     *p = 0;
749     return 0;
750 }
751
752 int z_ListEntries(ODR o, Z_ListEntries **p, int opt)
753 {
754     static Odr_arm arm[] =
755     {
756         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ListEntries_entries, z_Entries},
757         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_ListEntries_nonSurrogateDiagnostics,
758             z_DiagRecs},
759         {-1, -1, -1, -1, 0}
760     };
761
762     if (o->direction == ODR_DECODE)
763         *p = odr_malloc(o, sizeof(**p));
764
765     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
766         return 1;
767     *p = 0;
768     return opt && odr_ok(o);
769 }
770
771 int z_ScanRequest(ODR o, Z_ScanRequest **p, int opt)
772 {
773     if (!odr_sequence_begin(o, p, sizeof(**p)))
774         return opt && odr_ok(o);
775     willow_scan = 0;
776     return
777         z_ReferenceId(o, &(*p)->referenceId, 1) &&
778         odr_implicit_settag(o, ODR_CONTEXT, 3) &&
779         odr_sequence_of(o, z_DatabaseName, &(*p)->databaseNames,
780             &(*p)->num_databaseNames) &&
781         odr_oid(o, &(*p)->attributeSet, 1) &&
782         (z_AttributesPlusTerm(o, &(*p)->termListAndStartPoint, 1) ?
783             ((*p)->termListAndStartPoint ? 1 : 
784         z_WillowAttributesPlusTerm(o, &(*p)->termListAndStartPoint, 0)) : 0) &&
785         odr_implicit(o, odr_integer, &(*p)->stepSize, ODR_CONTEXT, 5, 1) &&
786         odr_implicit(o, odr_integer, &(*p)->numberOfTermsRequested,
787             ODR_CONTEXT, 6, 0) &&
788         odr_implicit(o, odr_integer, &(*p)->preferredPositionInResponse,
789             ODR_CONTEXT, 7, 1) &&
790         odr_sequence_end(o);
791 }
792
793 int z_ScanResponse(ODR o, Z_ScanResponse **p, int opt)
794 {
795     if (!odr_sequence_begin(o, p, sizeof(**p)))
796         return opt && odr_ok(o);
797     return
798         z_ReferenceId(o, &(*p)->referenceId, 1) &&
799         odr_implicit(o, odr_integer, &(*p)->stepSize, ODR_CONTEXT, 3, 1) &&
800         odr_implicit(o, odr_integer, &(*p)->scanStatus, ODR_CONTEXT, 4, 0) &&
801         odr_implicit(o, odr_integer, &(*p)->numberOfEntriesReturned,
802             ODR_CONTEXT, 5, 0) &&
803         odr_implicit(o, odr_integer, &(*p)->positionOfTerm, ODR_CONTEXT, 6, 1)&&
804         odr_explicit(o, z_ListEntries, &(*p)->entries, ODR_CONTEXT, 7, 1) &&
805         odr_implicit(o, odr_oid, &(*p)->attributeSet, ODR_CONTEXT, 8, 1) &&
806         odr_sequence_end(o);
807 }
808
809 /* ------------------------ SEARCHRESPONSE ----------------*/
810
811 int z_NumberOfRecordsReturned(ODR o, int **p, int opt)
812 {
813     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 24, opt);
814 }
815
816 int z_NextResultSetPosition(ODR o, int **p, int opt)
817 {
818     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 25, opt);
819 }
820
821 int z_PresentStatus(ODR o, int **p, int opt)
822 {
823     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 27, opt);
824 }
825
826 int z_SearchResponse(ODR o, Z_SearchResponse **p, int opt)
827 {
828     Z_SearchResponse *pp;
829
830     if (!odr_sequence_begin(o, p, sizeof(**p)))
831         return opt && odr_ok(o);
832     pp = *p;
833     return
834         z_ReferenceId(o, &pp->referenceId, 1) &&
835         odr_implicit(o, odr_integer, &pp->resultCount, ODR_CONTEXT, 23, 0) &&
836         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
837         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
838         odr_implicit(o, odr_bool, &pp->searchStatus, ODR_CONTEXT, 22, 0) &&
839         odr_implicit(o, odr_integer, &pp->resultSetStatus, ODR_CONTEXT, 26, 1) &&
840         z_PresentStatus(o, &pp->presentStatus, 1) &&
841         z_Records(o, &pp->records, 1) &&
842 #ifdef Z_OTHERINFO
843         odr_implicit(o, z_OtherInformation, &(*p)->additionalSearchInfo,
844             ODR_CONTEXT, 203, 1) &&
845         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
846 #endif
847         odr_sequence_end(o);
848 }
849
850 /* --------------------- PRESENT SERVICE ---------------------- */
851
852 int z_PresentRequest(ODR o, Z_PresentRequest **p, int opt)
853 {
854     Z_PresentRequest *pp;
855
856     if (!odr_sequence_begin(o, p, sizeof(**p)))
857         return opt && odr_ok(o);
858     pp = *p;
859     return
860         z_ReferenceId(o, &pp->referenceId, 1) &&
861         z_ResultSetId(o, &pp->resultSetId, 0) &&
862         odr_implicit(o, odr_integer, &pp->resultSetStartPoint, ODR_CONTEXT,
863             30, 0) &&
864         odr_implicit(o, odr_integer, &pp->numberOfRecordsRequested, ODR_CONTEXT,
865             29, 0) &&
866         z_ElementSetNames(o, &pp->elementSetNames, 1) &&
867         z_PreferredRecordSyntax(o, &pp->preferredRecordSyntax, 1) &&
868         odr_sequence_end(o);
869 }
870
871 int z_PresentResponse(ODR o, Z_PresentResponse **p, int opt)
872 {
873     Z_PresentResponse *pp;
874
875     if (!odr_sequence_begin(o, p, sizeof(**p)))
876         return opt && odr_ok(o);
877     pp = *p;
878     return
879         z_ReferenceId(o, &pp->referenceId, 1) &&
880         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
881         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
882         z_PresentStatus(o, &pp->presentStatus, 0) &&
883         z_Records(o, &pp->records, 1) &&
884         odr_sequence_end(o);
885 }
886
887 /* ----------------------DELETE -------------------------- */
888
889 int z_DeleteSetStatus(ODR o, int **p, int opt)
890 {
891     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 33, opt);
892 }
893
894 int z_ListStatus(ODR o, Z_ListStatus **p, int opt)
895 {
896     if (!odr_sequence_begin(o, p, sizeof(**p)))
897         return opt && odr_ok(o);
898     return
899         z_ResultSetId(o, &(*p)->id, 0) &&
900         z_DeleteSetStatus(o, &(*p)->status, 0) &&
901         odr_sequence_end(o);
902 }
903
904 int z_DeleteResultSetRequest(ODR o, Z_DeleteResultSetRequest **p, int opt)
905 {
906     if (!odr_sequence_begin(o, p, sizeof(**p)))
907         return opt && odr_ok(o);
908     return
909         z_ReferenceId(o, &(*p)->referenceId, 1) &&
910         odr_implicit(o, odr_integer, &(*p)->deleteFunction, ODR_CONTEXT, 32,
911             0) &&
912         (odr_sequence_of(o, z_ListStatus, &(*p)->resultSetList,
913             &(*p)->num_ids) || odr_ok(o)) &&
914 #ifdef Z_OTHERINFO
915         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
916 #endif
917         odr_sequence_end(o);
918 }
919
920 int z_DeleteResultSetResponse(ODR o, Z_DeleteResultSetResponse **p, int opt)
921 {
922     if (!odr_sequence_begin(o, p, sizeof(**p)))
923         return opt && odr_ok(o);
924     return
925         z_ReferenceId(o, &(*p)->referenceId, 1) &&
926         odr_implicit(o, z_DeleteSetStatus, &(*p)->deleteOperationStatus,
927             ODR_CONTEXT, 0, 1) &&
928         odr_implicit_settag(o, ODR_CONTEXT, 1) &&
929         (odr_sequence_of(o, z_ListStatus, &(*p)->deleteListStatuses,
930             &(*p)->num_statuses) || odr_ok(o)) &&
931         odr_implicit(o, odr_integer, &(*p)->numberNotDeleted, ODR_CONTEXT,
932             34, 1) &&
933         odr_implicit_settag(o, ODR_CONTEXT, 35) &&
934         (odr_sequence_of(o, z_ListStatus, &(*p)->bulkStatuses,
935             &(*p)->num_bulkStatuses) || odr_ok(o)) &&
936         odr_implicit(o, odr_visiblestring, &(*p)->deleteMessage, ODR_CONTEXT,
937             36, 1) &&
938 #ifdef Z_OTHERINFO
939         z_OtherInformation(o, &(*p)->otherInfo, 1) &&
940 #endif
941         odr_sequence_end(o);
942 }
943
944 /* ------------------------ APDU ------------------------- */
945
946 int z_APDU(ODR o, Z_APDU **p, int opt)
947 {
948     static Odr_arm arm[] =
949     {
950         {ODR_IMPLICIT, ODR_CONTEXT, 20, Z_APDU_initRequest, z_InitRequest},
951         {ODR_IMPLICIT, ODR_CONTEXT, 21, Z_APDU_initResponse, z_InitResponse},
952         {ODR_IMPLICIT, ODR_CONTEXT, 22, Z_APDU_searchRequest, z_SearchRequest},
953         {ODR_IMPLICIT, ODR_CONTEXT, 23, Z_APDU_searchResponse,
954             z_SearchResponse},
955         {ODR_IMPLICIT, ODR_CONTEXT, 24, Z_APDU_presentRequest,
956             z_PresentRequest},
957         {ODR_IMPLICIT, ODR_CONTEXT, 25, Z_APDU_presentResponse,
958             z_PresentResponse},
959         {ODR_IMPLICIT, ODR_CONTEXT, 26, Z_APDU_deleteResultSetRequest,
960             z_DeleteResultSetRequest},
961         {ODR_IMPLICIT, ODR_CONTEXT, 27, Z_APDU_deleteResultSetResponse,
962             z_DeleteResultSetResponse},
963         {ODR_IMPLICIT, ODR_CONTEXT, 30, Z_APDU_resourceControlRequest,
964             z_ResourceControlRequest},
965         {ODR_IMPLICIT, ODR_CONTEXT, 31, Z_APDU_resourceControlResponse,
966             z_ResourceControlResponse},
967         {ODR_IMPLICIT, ODR_CONTEXT, 32, Z_APDU_triggerResourceControlRequest,
968             z_TriggerResourceControlRequest},
969         {ODR_IMPLICIT, ODR_CONTEXT, 35, Z_APDU_scanRequest, z_ScanRequest},
970         {ODR_IMPLICIT, ODR_CONTEXT, 36, Z_APDU_scanResponse, z_ScanResponse},
971
972         {-1, -1, -1, -1, 0}
973     };
974
975     if (o->direction == ODR_DECODE)
976         *p = odr_malloc(o, sizeof(**p));
977     if (!odr_choice(o, arm, &(*p)->u, &(*p)->which))
978     {
979         if (o->direction == ODR_DECODE)
980             *p = 0;
981         return opt && odr_ok(o);
982     }
983     return 1;
984 }