implemented asn1_get_known_oid(), mapping OID index to ASN1 OID
[strongswan.git] / src / libstrongswan / asn1 / oid.pl
1 #!/usr/bin/perl
2 # Generates oid.h and oid.c out of oid.txt
3 #
4 # Copyright (C) 2003-2008 Andreas Steffen
5 # Hochschule fuer Technik Rapperswil
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 2 of the License, or (at your
10 # option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 # for more details.
16 #
17
18 $copyright="Copyright (C) 2003-2008 Andreas Steffen, Hochschule fuer Technik Rapperswil";
19 $automatic="This file has been automatically generated by the script oid.pl";
20 $warning="Do not edit manually!";
21
22 print "oid.pl generating oid.h and oid.c\n";
23
24 # Generate oid.h
25
26 open(OID_H, ">oid.h")
27 or die "could not open 'oid.h': $!";
28
29 print OID_H "/* Object identifiers (OIDs) used by strongSwan\n",
30 " * ", $copyright, "\n",
31 " * \n",
32 " * ", $automatic, "\n",
33 " * ", $warning, "\n",
34 " */\n\n",
35 "#ifndef OID_H_\n",
36 "#define OID_H_\n\n",
37 "typedef struct {\n",
38 " u_char octet;\n",
39 " u_int next;\n",
40 " u_int down;\n",
41 " u_int level;\n",
42 " const u_char *name;\n",
43 "} oid_t;\n",
44 "\n",
45 "extern const oid_t oid_names[];\n",
46 "\n",
47 "#define OID_UNKNOWN -1\n";
48
49 # parse oid.txt
50
51 open(SRC, "<oid.txt")
52 or die "could not open 'oid.txt': $!";
53
54 $counter = 0;
55 $max_name = 0;
56 $max_order = 0;
57
58 while ($line = <SRC>)
59 {
60 $line =~ m/( *?)(0x\w{2})\s+(".*?")[ \t]*?([\w_]*?)\Z/;
61
62 @order[$counter] = length($1);
63 @octet[$counter] = $2;
64 @name[$counter] = $3;
65
66 if (length($1) > $max_order)
67 {
68 $max_order = length($1);
69 }
70 if (length($3) > $max_name)
71 {
72 $max_name = length($3);
73 }
74 if (length($4) > 0)
75 {
76 printf OID_H "#define %s%s%d\n", $4, "\t" x ((39-length($4))/4), $counter;
77 }
78 $counter++;
79 }
80
81 printf OID_H "\n#define OID_MAX%s%d\n", "\t" x 8, $counter;
82
83 print OID_H "\n#endif /* OID_H_ */\n";
84
85 close SRC;
86 close OID_H;
87
88 # Generate oid.c
89
90 open(OID_C, ">oid.c")
91 or die "could not open 'oid.c': $!";
92
93 print OID_C "/* List of some useful object identifiers (OIDs)\n",
94 " * ", $copyright, "\n",
95 " * \n",
96 " * ", $automatic, "\n",
97 " * ", $warning, "\n",
98 " */\n",
99 "\n",
100 "#include <stdlib.h>\n",
101 "\n",
102 "#include \"oid.h\"\n",
103 "\n",
104 "const oid_t oid_names[] = {\n";
105
106 for ($c = 0; $c < $counter; $c++)
107 {
108 $next = 0;
109
110 for ($d = $c+1; $d < $counter && @order[$d] >= @order[$c]; $d++)
111 {
112 if (@order[$d] == @order[$c])
113 {
114 @next[$c] = $d;
115 last;
116 }
117 }
118
119 printf OID_C " {%s%s,%s%3d, %d, %2d, %s%s}%s /* %3d */\n"
120 ,' ' x @order[$c]
121 , @octet[$c]
122 , ' ' x (1 + $max_order - @order[$c])
123 , @next[$c]
124 , @order[$c+1] > @order[$c]
125 , @order[$c] / 2
126 , @name[$c]
127 , ' ' x ($max_name - length(@name[$c]))
128 , $c != $counter-1 ? "," : " "
129 , $c;
130 }
131
132 print OID_C "};\n" ;
133 close OID_C;