Fixed protocol bugs.
[yaz-moved-to-github.git] / asn / proto.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: proto.c,v $
7  * Revision 1.9  1995-03-15 08:37:06  quinn
8  * Fixed protocol bugs.
9  *
10  * Revision 1.8  1995/03/14  16:59:24  quinn
11  * Fixed OPTIONAL flag in attributeelement
12  *
13  * Revision 1.7  1995/03/07  16:29:33  quinn
14  * Added authentication stuff.
15  *
16  * Revision 1.6  1995/03/01  14:46:03  quinn
17  * Fixed protocol bug in 8777query.
18  *
19  * Revision 1.5  1995/02/14  11:54:22  quinn
20  * Fixing include.
21  *
22  * Revision 1.4  1995/02/10  15:54:30  quinn
23  * Small adjustments.
24  *
25  * Revision 1.3  1995/02/09  15:51:39  quinn
26  * Works better now.
27  *
28  * Revision 1.2  1995/02/06  21:26:07  quinn
29  * Repaired this evening's damages..
30  *
31  * Revision 1.1  1995/02/06  16:44:47  quinn
32  * First hack at Z/SR protocol
33  *
34  */
35
36 #include <odr.h>
37
38 #include <proto.h>
39
40 /* ---------------------- GLOBAL DEFS ------------------- */
41
42 int z_ReferenceId(ODR o, Z_ReferenceId **p, int opt)
43 {
44     return odr_implicit(o, odr_octetstring, (Odr_oct**) p, ODR_CONTEXT, 2, opt);
45 }
46
47 int z_DatabaseName(ODR o, Z_DatabaseName **p, int opt)
48 {
49     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 105,
50         opt);
51 }
52
53 int z_ResultSetId(ODR o, char **p, int opt)
54 {
55     return odr_implicit(o, odr_visiblestring, (char **) p, ODR_CONTEXT, 31,
56         opt);
57 }
58
59 int z_UserInformationField(ODR o, Z_UserInformationField **p, int opt)
60 {
61     return odr_explicit(o, odr_external, (Odr_external **)p, ODR_CONTEXT,
62         11, opt);
63 }
64
65 /* ---------------------- INITIALIZE SERVICE ------------------- */
66
67 int z_InitRequest(ODR o, Z_InitRequest **p, int opt)
68 {
69     Z_InitRequest *pp;
70
71     if (!odr_sequence_begin(o, p, sizeof(**p)))
72         return opt;
73     pp = *p;
74     return
75         z_ReferenceId(o, &pp->referenceId, 1) &&
76         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
77             3, 0) &&
78         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
79         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
80             5, 0) &&
81         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
82             6, 0) &&
83         odr_explicit(o, odr_any, &pp->idAuthentication, ODR_CONTEXT,
84             7, 1) &&
85         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
86             110, 1) &&
87         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
88             111, 1) &&
89         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
90             ODR_CONTEXT, 112, 1) &&
91         z_UserInformationField(o, &pp->userInformationField, 1) &&
92         odr_sequence_end(o);
93 }
94
95 int z_InitResponse(ODR o, Z_InitResponse **p, int opt)
96 {
97     Z_InitResponse *pp;
98
99     if (!odr_sequence_begin(o, p, sizeof(**p)))
100         return opt;
101     pp = *p;
102     return
103         z_ReferenceId(o, &pp->referenceId, 1) &&
104         odr_implicit(o, odr_bitstring, &pp->protocolVersion, ODR_CONTEXT, 
105             3, 0) &&
106         odr_implicit(o, odr_bitstring, &pp->options, ODR_CONTEXT, 4, 0) &&
107         odr_implicit(o, odr_integer, &pp->preferredMessageSize, ODR_CONTEXT,
108             5, 0) &&
109         odr_implicit(o, odr_integer, &pp->maximumRecordSize, ODR_CONTEXT,
110             6, 0) &&
111         odr_implicit(o, odr_bool, &pp->result, ODR_CONTEXT, 12, 0) &&
112         odr_implicit(o, odr_visiblestring, &pp->implementationId, ODR_CONTEXT,
113             110, 1) &&
114         odr_implicit(o, odr_visiblestring, &pp->implementationName, ODR_CONTEXT,
115             111, 1) &&
116         odr_implicit(o, odr_visiblestring, &pp->implementationVersion,
117             ODR_CONTEXT, 112, 1) &&
118         z_UserInformationField(o, &pp->userInformationField, 1) &&
119         odr_sequence_end(o);
120 }
121
122 int z_NSRAuthentication(ODR o, Z_NSRAuthentication **p, int opt)
123 {
124     if (!odr_sequence_begin(o, p, sizeof(**p)))
125         return opt;
126     return
127         odr_visiblestring(o, &(*p)->user, 0) &&
128         odr_visiblestring(o, &(*p)->password, 0) &&
129         odr_visiblestring(o, &(*p)->account, 0) &&
130         odr_sequence_end(o);
131 }
132
133 int z_StrAuthentication(ODR o, char **p, int opt)
134 {
135     return odr_visiblestring(o, p, opt);
136 }
137
138 /* ------------------------ SEARCH SERVICE ----------------------- */
139
140 int z_ElementSetName(ODR o, char **p, int opt)
141 {
142     return odr_implicit(o, odr_visiblestring, (char**) p, ODR_CONTEXT, 103,
143         opt);
144 }
145
146 int z_PreferredRecordSyntax(ODR o, Z_PreferredRecordSyntax **p, int opt)
147 {
148     return odr_implicit(o, odr_oid, (Odr_oid**) p, ODR_CONTEXT, 104, opt);
149 }
150
151 int z_DatabaseSpecificUnit(ODR o, Z_DatabaseSpecificUnit **p, int opt)
152 {
153     if (!odr_sequence_begin(o, p, sizeof(**p)))
154         return opt;
155     return
156         z_DatabaseName(o, &(*p)->databaseName, 0) &&
157         z_ElementSetName(o, &(*p)->elementSetName, 0) &&
158         odr_sequence_end(o);
159 }
160
161 int z_DatabaseSpecific(ODR o, Z_DatabaseSpecific **p, int opt)
162 {
163     if (o->direction == ODR_DECODE)
164         *p = nalloc(o, sizeof(**p));
165     else if (!*p)
166         return opt;
167
168     odr_implicit_settag(o, ODR_CONTEXT, 1);
169     if (odr_sequence_of(o, z_DatabaseSpecificUnit, &(*p)->elements,
170         &(*p)->num_elements))
171         return 1;
172     *p = 0;
173     return 0;
174 }
175
176 int z_ElementSetNames(ODR o, Z_ElementSetNames **p, int opt)
177 {
178     static Odr_arm arm[] =
179     {
180         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_ElementSetNames_generic,
181             z_ElementSetName},
182         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_ElementSetNames_databaseSpecific,
183             z_DatabaseSpecific},
184         {-1, -1, -1, -1, 0}
185     };
186
187     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 19))
188         return opt;
189
190     if (o->direction == ODR_DECODE)
191         *p = nalloc(o, sizeof(**p));
192
193     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
194         odr_constructed_end(o))
195         return 1;
196     *p = 0;
197     return 0;
198 }
199
200 /* ----------------------- RPN QUERY -----------------------*/
201
202 int z_AttributeElement(ODR o, Z_AttributeElement **p, int opt)
203 {
204     if (!odr_sequence_begin(o, p, sizeof(**p)))
205         return opt;
206     return
207         odr_implicit(o, odr_integer, &(*p)->attributeType, ODR_CONTEXT,
208             120, 0) &&
209         odr_implicit(o, odr_integer, &(*p)->attributeValue, ODR_CONTEXT,
210             121, 0) &&
211         odr_sequence_end(o);
212 }
213
214 int z_AttributesPlusTerm(ODR o, Z_AttributesPlusTerm **p, int opt)
215 {
216     if (!(odr_implicit_settag(o, ODR_CONTEXT, 102) &&
217         odr_sequence_begin(o, p, sizeof(**p))))
218         return opt;
219     return
220         odr_implicit_settag(o, ODR_CONTEXT, 44) &&
221         odr_sequence_of(o, z_AttributeElement, &(*p)->attributeList,
222             &(*p)->num_attributes) &&
223         odr_implicit(o, odr_octetstring, &(*p)->term, ODR_CONTEXT, 45, 0) &&
224         odr_sequence_end(o);
225 }
226
227 int z_Operator(ODR o, Z_Operator **p, int opt)
228 {
229     static Odr_arm arm[] =
230     {
231         {ODR_IMPLICIT, ODR_CONTEXT, 0, Z_Operator_and, odr_null},
232         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Operator_or, odr_null},
233         {ODR_IMPLICIT, ODR_CONTEXT, 2, Z_Operator_and_not, odr_null},
234         {-1, -1, -1, -1, 0}
235     };
236     int dummy = 999;
237
238     if (!*p && o->direction != ODR_DECODE)
239         return opt;
240     if (!odr_constructed_begin(o, p, ODR_CONTEXT, 46))
241         return opt;
242     if (o->direction == ODR_DECODE)
243         *p = nalloc(o, sizeof(**p));
244     else
245         (*p)->u.and = &dummy;
246
247     if (odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
248         odr_constructed_end(o))
249         return 1;
250     *p = 0;
251     return opt;
252 }
253
254 int z_Operand(ODR o, Z_Operand **p, int opt)
255 {
256     static Odr_arm arm[] =
257     {
258         {-1, -1, -1, Z_Operand_APT, z_AttributesPlusTerm},
259         {-1, -1, -1, Z_Operand_resultSetId, z_ResultSetId},
260         {-1, -1, -1, -1, 0}
261     };
262
263     if (o->direction ==ODR_DECODE)
264         *p = nalloc(o, sizeof(**p));
265     else if (!*p)
266         return opt;
267     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
268         return 1;
269     *p = 0;
270     return opt;
271 }
272
273 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt);
274
275 int z_Complex(ODR o, Z_Complex **p, int opt)
276 {
277     if (!odr_sequence_begin(o, p, sizeof(**p)))
278         return opt;
279     return
280         z_RPNStructure(o, &(*p)->s1, 0) &&
281         z_RPNStructure(o, &(*p)->s2, 0) &&
282         z_Operator(o, &(*p)->operator, 0) &&
283         odr_sequence_end(o);
284 }
285
286 int z_RPNStructure(ODR o, Z_RPNStructure **p, int opt)
287 {
288     static Odr_arm arm[] = 
289     {
290         {ODR_EXPLICIT, ODR_CONTEXT, 0, Z_RPNStructure_simple, z_Operand},
291         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_RPNStructure_complex, z_Complex},
292         {-1 -1, -1, -1, 0}
293     };
294
295     if (o->direction == ODR_DECODE)
296         *p = nalloc(o, sizeof(**p));
297     else if (!*p)
298         return opt;
299     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
300         return 1;
301     *p = 0;
302     return opt;
303 }
304
305 int z_RPNQuery(ODR o, Z_RPNQuery **p, int opt)
306 {
307     if (!odr_sequence_begin(o, p, sizeof(**p)))
308         return opt;
309     return
310         odr_oid(o, &(*p)->attributeSetId, 0) &&
311         z_RPNStructure(o, &(*p)->RPNStructure, 0) &&
312         odr_sequence_end(o);
313 }
314
315 /* -----------------------END RPN QUERY ----------------------- */
316
317 int z_Query(ODR o, Z_Query **p, int opt)
318 {
319     static Odr_arm arm[] = 
320     {
321         {ODR_IMPLICIT, ODR_CONTEXT, 1, Z_Query_type_1, z_RPNQuery},
322         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_Query_type_2, odr_octetstring},
323         {-1, -1, -1, -1, 0}
324     };
325
326     if (o->direction == ODR_DECODE)
327         *p = nalloc(o, sizeof(**p));
328     else if (!*p)
329         return opt;
330     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
331         return 1;
332     *p = 0;
333     return opt;
334 }
335
336 int z_SearchRequest(ODR o, Z_SearchRequest **p, int opt)
337 {
338     Z_SearchRequest *pp;
339
340     if (!odr_sequence_begin(o, p, sizeof(**p)))
341         return opt;
342     pp = *p;
343     return
344         z_ReferenceId(o, &pp->referenceId, 1) &&
345         odr_implicit(o, odr_integer, &pp->smallSetUpperBound, ODR_CONTEXT,
346             13, 0) &&
347         odr_implicit(o, odr_integer, &pp->largeSetLowerBound, ODR_CONTEXT,
348             14, 0) &&
349         odr_implicit(o, odr_integer, &pp->mediumSetPresentNumber, ODR_CONTEXT,
350             15, 0) &&
351         odr_implicit(o, odr_bool, &pp->replaceIndicator, ODR_CONTEXT, 16, 1) &&
352         odr_implicit(o, odr_visiblestring, &pp->resultSetName, ODR_CONTEXT,
353             17, 9) &&
354         odr_implicit_settag(o, ODR_CONTEXT, 18) &&
355         odr_sequence_of(o, z_DatabaseName, &pp->databaseNames,
356             &pp->num_databaseNames) &&
357         odr_implicit(o, z_ElementSetNames, &pp->smallSetElementSetNames,
358             ODR_CONTEXT, 100, 1) &&
359         odr_implicit(o, z_ElementSetNames, &pp->mediumSetElementSetNames,
360             ODR_CONTEXT, 101, 1) &&
361         z_PreferredRecordSyntax(o, &pp->preferredRecordSyntax, 1) &&
362         odr_explicit(o, z_Query, &pp->query, ODR_CONTEXT, 21, 0) &&
363         odr_sequence_end(o);
364 }
365
366 /* ------------------------ RECORD ------------------------- */
367
368 int z_DatabaseRecord(ODR o, Z_DatabaseRecord **p, int opt)
369 {
370     return odr_external(o, (Odr_external **) p, opt);
371 }
372
373 int z_DiagRec(ODR o, Z_DiagRec **p, int opt)
374 {
375     if (!odr_sequence_begin(o, p, sizeof(**p)))
376         return opt;
377     return
378         odr_oid(o, &(*p)->diagnosticSetId, 1) &&       /* SHOULD NOT BE OPT */
379         odr_integer(o, &(*p)->condition, 0) &&
380         (odr_visiblestring(o, &(*p)->addinfo, 0) ||
381         odr_implicit(o, odr_cstring, &(*p)->addinfo, ODR_CONTEXT, ODR_VISIBLESTRING, 1)) &&
382         odr_sequence_end(o);
383 }
384
385 int z_NamePlusRecord(ODR o, Z_NamePlusRecord **p, int opt)
386 {
387     static Odr_arm arm[] =
388     {
389         {ODR_EXPLICIT, ODR_CONTEXT, 1, Z_NamePlusRecord_databaseRecord,
390             z_DatabaseRecord},
391         {ODR_EXPLICIT, ODR_CONTEXT, 2, Z_NamePlusRecord_surrogateDiagnostic,
392             z_DiagRec},
393         {-1, -1, -1, -1, 0}
394     };
395
396     if (!odr_sequence_begin(o, p, sizeof(**p)))
397         return opt;
398     return
399         odr_implicit(o, z_DatabaseName, &(*p)->databaseName, ODR_CONTEXT,
400             0, 1) &&
401         odr_constructed_begin(o, &(*p)->u, ODR_CONTEXT, 1) &&
402         odr_choice(o, arm, &(*p)->u, &(*p)->which) &&
403         odr_constructed_end(o) &&
404         odr_sequence_end(o);
405 }
406
407 int z_NamePlusRecordList(ODR o, Z_NamePlusRecordList **p, int opt)
408 {
409     if (o->direction == ODR_DECODE)
410         *p = nalloc(o, sizeof(**p));
411     if (odr_sequence_of(o, z_NamePlusRecord, &(*p)->records,
412         &(*p)->num_records))
413         return 1;
414     *p = 0;
415     return 0;
416 }
417
418 int z_Records(ODR o, Z_Records **p, int opt)
419 {
420     Odr_arm arm[] = 
421     {
422         {ODR_IMPLICIT, ODR_CONTEXT, 28, Z_Records_DBOSD, z_NamePlusRecordList},
423         {ODR_IMPLICIT, ODR_CONTEXT, 130, Z_Records_NSD, z_DiagRec},
424         {-1, -1, -1, -1, 0}
425     };
426
427     if (o->direction == ODR_DECODE)
428         *p = nalloc(o, sizeof(**p));
429     else if (!*p)
430         return opt;
431     if (odr_choice(o, arm, &(*p)->u, &(*p)->which))
432         return 1;
433     *p = 0;
434     return opt;
435 }
436
437 /* ------------------------ SEARCHRESPONSE ----------------*/
438
439 int z_NumberOfRecordsReturned(ODR o, int **p, int opt)
440 {
441     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 24, opt);
442 }
443
444 int z_NextResultSetPosition(ODR o, int **p, int opt)
445 {
446     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 25, opt);
447 }
448
449 int z_PresentStatus(ODR o, int **p, int opt)
450 {
451     return odr_implicit(o, odr_integer, p, ODR_CONTEXT, 27, opt);
452 }
453
454 int z_SearchResponse(ODR o, Z_SearchResponse **p, int opt)
455 {
456     Z_SearchResponse *pp;
457
458     if (!odr_sequence_begin(o, p, sizeof(**p)))
459         return opt;
460     pp = *p;
461     return
462         z_ReferenceId(o, &pp->referenceId, 1) &&
463         odr_implicit(o, odr_integer, &pp->resultCount, ODR_CONTEXT, 23, 0) &&
464         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
465         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
466         odr_implicit(o, odr_bool, &pp->searchStatus, ODR_CONTEXT, 22, 0) &&
467         odr_implicit(o, odr_integer, &pp->resultSetStatus, ODR_CONTEXT, 26, 1) &&
468         z_PresentStatus(o, &pp->presentStatus, 1) &&
469         z_Records(o, &pp->records, 1) &&
470         odr_sequence_end(o);
471 }
472
473 /* --------------------- PRESENT SERVICE ---------------------- */
474
475 int z_PresentRequest(ODR o, Z_PresentRequest **p, int opt)
476 {
477     Z_PresentRequest *pp;
478
479     if (!odr_sequence_begin(o, p, sizeof(**p)))
480         return opt;
481     pp = *p;
482     return
483         z_ReferenceId(o, &pp->referenceId, 1) &&
484         z_ResultSetId(o, &pp->resultSetId, 0) &&
485         odr_implicit(o, odr_integer, &pp->resultSetStartPoint, ODR_CONTEXT,
486             30, 0) &&
487         odr_implicit(o, odr_integer, &pp->numberOfRecordsRequested, ODR_CONTEXT,
488             29, 0) &&
489         z_ElementSetNames(o, &pp->elementSetNames, 1) &&
490         z_PreferredRecordSyntax(o, &pp->preferredRecordSyntax, 1) &&
491         odr_sequence_end(o);
492 }
493
494 int z_PresentResponse(ODR o, Z_PresentResponse **p, int opt)
495 {
496     Z_PresentResponse *pp;
497
498     if (!odr_sequence_begin(o, p, sizeof(**p)))
499         return opt;
500     pp = *p;
501     return
502         z_ReferenceId(o, &pp->referenceId, 1) &&
503         z_NumberOfRecordsReturned(o, &pp->numberOfRecordsReturned, 0) &&
504         z_NextResultSetPosition(o, &pp->nextResultSetPosition, 0) &&
505         z_PresentStatus(o, &pp->presentStatus, 0) &&
506         z_Records(o, &pp->records, 1) &&
507         odr_sequence_end(o);
508 }
509
510 /* ------------------------ APDU ------------------------- */
511
512 int z_APDU(ODR o, Z_APDU **p, int opt)
513 {
514     static Odr_arm arm[] =
515     {
516         {ODR_IMPLICIT, ODR_CONTEXT, 20, Z_APDU_initRequest, z_InitRequest},
517         {ODR_IMPLICIT, ODR_CONTEXT, 21, Z_APDU_initResponse, z_InitResponse},
518         {ODR_IMPLICIT, ODR_CONTEXT, 22, Z_APDU_searchRequest, z_SearchRequest},
519         {ODR_IMPLICIT, ODR_CONTEXT, 23, Z_APDU_searchResponse, z_SearchResponse},
520         {ODR_IMPLICIT, ODR_CONTEXT, 24, Z_APDU_presentRequest, z_PresentRequest},
521         {ODR_IMPLICIT, ODR_CONTEXT, 25, Z_APDU_presentResponse, z_PresentResponse},
522
523         {-1, -1, -1, -1, 0}
524     };
525
526     if (o->direction == ODR_DECODE)
527         *p = nalloc(o, sizeof(**p));
528     if (!odr_choice(o, arm, &(*p)->u, &(*p)->which))
529     {
530         if (o->direction == ODR_DECODE)
531             *p = 0;
532         return opt;
533     }
534     return 1;
535 }