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