Smallish
[yaz-moved-to-github.git] / odr / odr_choice.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_choice.c,v $
7  * Revision 1.11  1995-09-29 17:12:23  quinn
8  * Smallish
9  *
10  * Revision 1.10  1995/09/27  15:02:58  quinn
11  * Modified function heads & prototypes.
12  *
13  * Revision 1.9  1995/08/15  12:00:23  quinn
14  * Updated External
15  *
16  * Revision 1.8  1995/06/19  17:01:51  quinn
17  * This should bring us in sync with the version distributed as 1.0b
18  *
19  * Revision 1.7  1995/06/19  13:06:50  quinn
20  * Fixed simple bug in the code to handle untagged choice elements.
21  *
22  * Revision 1.6  1995/05/16  08:50:53  quinn
23  * License, documentation, and memory fixes
24  *
25  * Revision 1.5  1995/03/18  12:16:31  quinn
26  * Minor changes.
27  *
28  * Revision 1.4  1995/03/14  16:59:38  quinn
29  * Added odr_constructed_more check
30  *
31  * Revision 1.3  1995/03/08  12:12:22  quinn
32  * Added better error checking.
33  *
34  * Revision 1.2  1995/02/09  15:51:48  quinn
35  * Works better now.
36  *
37  * Revision 1.1  1995/02/07  17:52:59  quinn
38  * A damn mess, but now things work, I think.
39  *
40  */
41
42 #include <odr.h>
43
44 int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp)
45 {
46     int i, cl = -1, tg, cn, *which = whichp, bias = o->choice_bias;
47
48     if (o->error)
49         return 0;
50     if (o->direction != ODR_DECODE && !*(char**)p)
51         return 0;
52     o->choice_bias = -1;
53     for (i = 0; arm[i].fun; i++)
54     {
55         if (o->direction == ODR_DECODE)
56         {
57             if (bias >= 0 && bias != arm[i].which)
58                 continue;
59             *which = arm[i].which;
60         }
61         else if (*which != arm[i].which)
62             continue;
63
64         if (arm[i].tagmode != ODR_NONE)
65         {
66             if (o->direction == ODR_DECODE && cl < 0)
67             {
68                 if (o->stackp > -1 && !odr_constructed_more(o))
69                     return 0;
70                 if (ber_dectag(o->bp, &cl, &tg, &cn) <= 0)
71                     return 0;
72             }
73             else if (o->direction != ODR_DECODE)
74             {
75                 cl = arm[i].class;
76                 tg = arm[i].tag;
77             }
78             if (tg == arm[i].tag && cl == arm[i].class)
79             {
80                 if (arm[i].tagmode == ODR_IMPLICIT)
81                 {
82                     odr_implicit_settag(o, cl, tg);
83                     return (*arm[i].fun)(o, p, 0);
84                 }
85                 /* explicit */
86                 if (!odr_constructed_begin(o, p, cl, tg))
87                     return 0;
88                 return (*arm[i].fun)(o, p, 0) &&
89                     odr_constructed_end(o);
90             }
91         }
92         else  /* no tagging. Have to poll type */
93         {
94             if ((*arm[i].fun)(o, p, 1) && *(char**)p)
95                 return 1;
96         }
97     }
98     *which = -1;
99     *(char*)p = 0;
100     return 0;
101 }
102
103 void odr_choice_bias(ODR o, int what)
104 {
105     o->choice_bias = what;
106 }