]> git.decadent.org.uk Git - ap-utils.git/blob - lib/ber.c
Update Standards-Version to 3.8.1
[ap-utils.git] / lib / ber.c
1 /* Author : Rahul G rahul@smartbridges.com */
2
3
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7
8 /* This function takes the oid in dot format ( 1.3.6.1.4.410.... ) and encodes
9  * the oid using ber rules. oid_out should be allocated by the application.. 
10  * encode_oid returns the length of the encoded buffer.
11  */
12 int encode_oid(char* oid, unsigned char* oid_out)
13 {
14         char* ptr;
15         int count=0,  oct_count=2;
16         int num_val=0, oct1=0;
17         char val[32];
18         int last=0,len=0;       
19         
20         oid_out[0]=0x06 ; /* 00000110 */
21         while(( (ptr=strchr(oid,'.')) != 0) || last == 0 )
22         {
23                 if(ptr != 0)
24                         len=ptr-oid;
25                 else
26                         len=strlen(oid);
27                 
28                 strncpy(val,oid,len);
29                 val[len]='\0';
30                 num_val=atoi(val);
31 /*get the first two sub id's*/
32                 if(count == 0)
33                 {
34                         oct1=40* num_val;
35                         count++;
36                         oid=ptr+1;
37                         continue;
38                 }
39                 if(count == 1)
40                 {
41                         oct1+=num_val;
42                         oid_out[oct_count]=oct1;
43                         oct_count++;
44                         oid=ptr+1;
45                         count++;
46                         continue;
47                 }
48                 else
49                 {
50                         int i, loop, j;
51                         for(i=31;i>= 0; i--)
52                         {
53                                 unsigned int mask= (num_val>>(i)) & ~(~0x0000<<1);
54                                 if(mask)
55                                 {
56                                         i++;
57                                         break;
58                                 }
59                         }
60                         loop=  i/ 7;
61                         for(j=loop; j>= 0; j--)
62                         {
63                                 oid_out[oct_count]= (num_val >> (j*7)) &  ~(~0x0000 << 7);
64                                 oid_out[oct_count] |= 0x80; /* turn the msb to 1 */
65                                 oct_count++;
66                         }
67                         oid_out[oct_count - 1] &= 0x7f; /* turn the msb back to zero for the last octect */
68                 }
69                 if(ptr != 0)
70                 {
71                         oid=ptr+1;
72                 }
73                 else
74                 {
75                         last=1;
76                 }
77         }
78
79 //Insert the octect length      
80         oid_out[1]=oct_count-2;
81         return oct_count;
82         
83 }
84                                         
85 /*                      
86 int main()
87 {
88
89         unsigned char oid_ut[512];
90         int k,ret;
91 //      ret=encode_oid("1.3.6.1.4.1.410.1.1.1.1.0",oid_ut);
92         ret=encode_oid("1.2.840.113549.1",oid_ut);
93
94         for(k=0; k< ret; k++)
95                 printf("%.2x ",oid_ut[k]);
96         printf("\n");
97         return 0;
98 }
99
100 */
101