Fixed simple bug in the code to handle untagged choice elements.
[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.7  1995-06-19 13:06:50  quinn
8  * Fixed simple bug in the code to handle untagged choice elements.
9  *
10  * Revision 1.6  1995/05/16  08:50:53  quinn
11  * License, documentation, and memory fixes
12  *
13  * Revision 1.5  1995/03/18  12:16:31  quinn
14  * Minor changes.
15  *
16  * Revision 1.4  1995/03/14  16:59:38  quinn
17  * Added odr_constructed_more check
18  *
19  * Revision 1.3  1995/03/08  12:12:22  quinn
20  * Added better error checking.
21  *
22  * Revision 1.2  1995/02/09  15:51:48  quinn
23  * Works better now.
24  *
25  * Revision 1.1  1995/02/07  17:52:59  quinn
26  * A damn mess, but now things work, I think.
27  *
28  */
29
30 #include <odr.h>
31
32 int odr_choice(ODR o, Odr_arm arm[], void *p, void *whichp)
33 {
34     int i, cl = -1, tg, cn, *which = whichp;
35
36     if (o->error)
37         return 0;
38     if (o->direction != ODR_DECODE && !*(char**)p)
39         return 0;
40     for (i = 0; arm[i].fun; i++)
41     {
42         if (o->direction == ODR_DECODE)
43             *which = arm[i].which;
44         else if (*which != arm[i].which)
45             continue;
46
47         if (arm[i].tagmode != ODR_NONE)
48         {
49             if (o->direction == ODR_DECODE && cl < 0)
50             {
51                 if (o->stackp > -1 && !odr_constructed_more(o))
52                     return 0;
53                 if (ber_dectag(o->bp, &cl, &tg, &cn) <= 0)
54                     return 0;
55             }
56             else if (o->direction != ODR_DECODE)
57             {
58                 cl = arm[i].class;
59                 tg = arm[i].tag;
60             }
61             if (tg == arm[i].tag && cl == arm[i].class)
62             {
63                 if (arm[i].tagmode == ODR_IMPLICIT)
64                 {
65                     odr_implicit_settag(o, cl, tg);
66                     return (*arm[i].fun)(o, p, 0);
67                 }
68                 /* explicit */
69                 if (!odr_constructed_begin(o, p, cl, tg))
70                     return 0;
71                 return (*arm[i].fun)(o, p, 0) &&
72                     odr_constructed_end(o);
73             }
74         }
75         else  /* no tagging. Have to poll type */
76         {
77             if ((*arm[i].fun)(o, p, 1) && *(char*)p)
78                 return 1;
79         }
80     }
81     *which = -1;
82     *(char*)p = 0;
83     return 0;
84 }