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