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