Added better error checking.
[yaz-moved-to-github.git] / odr / ber_oid.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: ber_oid.c,v $
7  * Revision 1.4  1995-03-08 12:12:11  quinn
8  * Added better error checking.
9  *
10  * Revision 1.3  1995/03/01  08:40:56  quinn
11  * Smallish changes.
12  *
13  * Revision 1.2  1995/02/14  20:39:55  quinn
14  * Fixed bugs in completeBER and (serious one in) ber_oid.
15  *
16  * Revision 1.1  1995/02/03  17:04:36  quinn
17  * Initial revision
18  *
19  */
20
21 #include <odr.h>
22
23 int ber_oidc(ODR o, Odr_oid *p)
24 {
25     int len;
26     unsigned char *lenp;
27     int pos, n, res, id;
28     unsigned char octs[8];
29
30     switch (o->direction)
31     {
32         case ODR_DECODE:
33             if ((res = ber_declen(o->bp, &len)) < 1)
34             {
35                 o->error = OPROTO;
36                 return 0;
37             }
38             if (len < 0)
39             {
40                 o->error = OPROTO;
41                 return 0;
42             }
43             o->bp += res;
44             o->left -= res;
45             if (len == 0)
46             {
47                 *p = -1;
48                 return 1;
49             }
50             p[0] = *o->bp / 40;
51             if (p[0] > 2)
52                 p[0] = 2;
53             p[1] = *o->bp - p[0] * 40;
54             o->bp++;
55             o->left--;
56             pos = 2;
57             len--;
58             while (len)
59             {
60                 p[pos] = 0;
61                 do
62                 {
63                     if (!len)
64                     {
65                         o->error = OPROTO;
66                         return 0;
67                     }
68                     p[pos] <<= 7;
69                     p[pos] |= *o->bp & 0X7F;
70                     len--;
71                     o->left--;
72                 }
73                 while (*(o->bp++) & 0X80);
74                 pos++;
75             }
76             p[pos] = -1;
77             return 1;
78         case ODR_ENCODE:
79             /* we'll allow ourselves the quiet luxury of only doing encodings
80                shorter than 127 */
81             lenp = o->bp;
82             o->bp++;
83             o->left--;
84             if (p[0] < 0 && p[1] <= 0)
85             {
86                 o->error = ODATA;
87                 return 0;
88             }
89             p[1] = p[0] * 40 + p[1];
90             for (pos = 1; p[pos] >= 0; pos++)
91             {
92                 id = p[pos];
93                 n = 0;
94                 do
95                 {
96                     octs[n++] = id & 0X7F;
97                     id >>= 7;
98                 }
99                 while (id);
100                 if (n > o->left)
101                 {
102                     o->error = OSPACE;
103                     return 0;
104                 }
105                 o->left -= n;
106                 while (n--)
107                     *(o->bp++) = octs[n] | ((n > 0) << 7);
108             }
109             if (ber_enclen(lenp, (o->bp - lenp) - 1, 1, 1) != 1)
110             {
111                 o->error = OOTHER;
112                 return 0;
113             }
114             return 1;
115         default: o->error = OOTHER; return 0;
116     }
117 }