Added better error checking.
[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.3  1995-03-08 12:12:22  quinn
8  * Added better error checking.
9  *
10  * Revision 1.2  1995/02/09  15:51:48  quinn
11  * Works better now.
12  *
13  * Revision 1.1  1995/02/07  17:52:59  quinn
14  * A damn mess, but now things work, I think.
15  *
16  */
17
18 #include <odr.h>
19
20 int odr_choice(ODR o, Odr_arm arm[], void *p, int *which)
21 {
22     int i, cl = -1, tg, cn;
23
24     if (o->error)
25         return 0;
26     if (o->direction != ODR_DECODE && !*(char**)p)
27         return 0;
28     for (i = 0; arm[i].fun; i++)
29     {
30         if (o->direction == ODR_DECODE)
31             *which = arm[i].which;
32         else if (*which != arm[i].which)
33             continue;
34
35         if (arm[i].tagmode != ODR_NONE)
36         {
37             if (o->direction == ODR_DECODE && cl < 0)
38             {
39                 if (ber_dectag(o->bp, &cl, &tg, &cn) <= 0)
40                     return 0;
41             }
42             else if (o->direction != ODR_DECODE)
43             {
44                 cl = arm[i].class;
45                 tg = arm[i].tag;
46             }
47             if (tg == arm[i].tag && cl == arm[i].class)
48             {
49                 if (arm[i].tagmode == ODR_IMPLICIT)
50                 {
51                     odr_implicit_settag(o, cl, tg);
52                     return (*arm[i].fun)(o, p, 0);
53                 }
54                 /* explicit */
55                 if (!odr_constructed_begin(o, p, cl, tg))
56                     return 0;
57                 return (*arm[i].fun)(o, p, 0) &&
58                     odr_constructed_end(o);
59             }
60         }
61         else  /* no tagging. Have to poll type */
62             if ((*arm[i].fun)(o, p, 0))
63                 return 1;
64     }
65     *which = -1;
66     *(char*)p = 0;
67     return 0;
68 }