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