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