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