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