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