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