Add odr_prepend()
[yaz-moved-to-github.git] / src / ber_oid.c
1 /*
2  * Copyright (c) 1995-2003, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Id: ber_oid.c,v 1.1 2003-10-27 12:21:30 adam Exp $
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include "odr-priv.h"
13
14 int ber_oidc(ODR o, Odr_oid *p)
15 {
16     int len, lenp, end;
17     int pos, n, res, id;
18     unsigned char octs[8];
19
20     switch (o->direction)
21     {
22     case ODR_DECODE:
23         if ((res = ber_declen(o->bp, &len, odr_max(o))) < 1)
24         {
25             odr_seterror(o, OPROTO, 18);
26             return 0;
27         }
28         if (len < 0)
29         {
30             odr_seterror(o, OPROTO, 19);
31             return 0;
32         }
33         o->bp += res;
34         if (len == 0)
35         {
36             *p = -1;
37             return 1;
38         }
39         if (len > odr_max(o))
40         {
41             odr_seterror(o, OPROTO, 20);
42             return 0;
43         }
44         p[0] = *o->bp / 40;
45         if (p[0] > 2)
46             p[0] = 2;
47         p[1] = *o->bp - p[0] * 40;
48         o->bp++;
49         pos = 2;
50         len--;
51         while (len)
52         {
53             p[pos] = 0;
54             do
55             {
56                 if (!len)
57                 {
58                     odr_seterror(o, OPROTO, 21);
59                     return 0;
60                 }
61                 p[pos] <<= 7;
62                 p[pos] |= *o->bp & 0X7F;
63                 len--;
64             }
65             while (*(o->bp++) & 0X80);
66             pos++;
67         }
68         p[pos] = -1;
69         return 1;
70     case ODR_ENCODE:
71         /* we'll allow ourselves the quiet luxury of only doing encodings
72            shorter than 127 */
73         lenp = odr_tell(o);
74         if (odr_putc(o, 0) < 0)   /* dummy */
75             return 0;
76         if (p[0] < 0 && p[1] <= 0)
77         {
78             odr_seterror(o, ODATA, 23);
79             return 0;
80         }
81         for (pos = 1; p[pos] >= 0; pos++)
82         {
83             id = pos > 1 ? p[pos] : p[0] * 40 + p[1];
84             n = 0;
85             do
86             {
87                 octs[n++] = id & 0X7F;
88                 id >>= 7;
89             }
90             while (id);
91             while (n--)
92             {
93                 unsigned char p;
94
95                 p = octs[n] | ((n > 0) << 7);
96                 if (odr_putc(o, p) < 0)
97                     return 0;
98             }
99         }
100         end = odr_tell(o);
101         odr_seek(o, ODR_S_SET, lenp);
102         if (ber_enclen(o, (end - lenp) - 1, 1, 1) != 1)
103         {
104             odr_seterror(o, OOTHER, 52);
105             return 0;
106         }
107         odr_seek(o, ODR_S_END, 0);
108         return 1;
109     default: 
110         odr_seterror(o, OOTHER, 22);
111         return 0;
112     }
113 }