Added odr_constructed_more check
[yaz-moved-to-github.git] / odr / odr_choice.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: odr_choice.c,v $
7  * Revision 1.4  1995-03-14 16:59:38  quinn
8  * Added odr_constructed_more check
9  *
10  * Revision 1.3  1995/03/08  12:12:22  quinn
11  * Added better error checking.
12  *
13  * Revision 1.2  1995/02/09  15:51:48  quinn
14  * Works better now.
15  *
16  * Revision 1.1  1995/02/07  17:52:59  quinn
17  * A damn mess, but now things work, I think.
18  *
19  */
20
21 #include <odr.h>
22
23 int odr_choice(ODR o, Odr_arm arm[], void *p, int *which)
24 {
25     int i, cl = -1, tg, cn;
26
27     if (o->error)
28         return 0;
29     if (o->direction != ODR_DECODE && !*(char**)p)
30         return 0;
31     for (i = 0; arm[i].fun; i++)
32     {
33         if (o->direction == ODR_DECODE)
34             *which = arm[i].which;
35         else if (*which != arm[i].which)
36             continue;
37
38         if (arm[i].tagmode != ODR_NONE)
39         {
40             if (o->direction == ODR_DECODE && cl < 0)
41             {
42                 if (o->stackp > -1 && !odr_constructed_more(o))
43                     return 0;
44                 if (ber_dectag(o->bp, &cl, &tg, &cn) <= 0)
45                     return 0;
46             }
47             else if (o->direction != ODR_DECODE)
48             {
49                 cl = arm[i].class;
50                 tg = arm[i].tag;
51             }
52             if (tg == arm[i].tag && cl == arm[i].class)
53             {
54                 if (arm[i].tagmode == ODR_IMPLICIT)
55                 {
56                     odr_implicit_settag(o, cl, tg);
57                     return (*arm[i].fun)(o, p, 0);
58                 }
59                 /* explicit */
60                 if (!odr_constructed_begin(o, p, cl, tg))
61                     return 0;
62                 return (*arm[i].fun)(o, p, 0) &&
63                     odr_constructed_end(o);
64             }
65         }
66         else  /* no tagging. Have to poll type */
67             if ((*arm[i].fun)(o, p, 0))
68                 return 1;
69     }
70     *which = -1;
71     *(char*)p = 0;
72     return 0;
73 }