merging changes from portability branch back to trunk
[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 "#include <sys/types.h>\n\n",
36 "#ifndef OID_H_\n",
37 "#define OID_H_\n\n",
38 "typedef struct {\n",
39 " u_char octet;\n",
40 " u_int next;\n",
41 " u_int down;\n",
42 " u_int level;\n",
43 " const u_char *name;\n",
44 "} oid_t;\n",
45 "\n",
46 "extern const oid_t oid_names[];\n",
47 "\n",
48 "#define OID_UNKNOWN -1\n";
49
50 # parse oid.txt
51
52 open(SRC, "<oid.txt")
53 or die "could not open 'oid.txt': $!";
54
55 $counter = 0;
56 $max_name = 0;
57 $max_order = 0;
58
59 while ($line = <SRC>)
60 {
61 $line =~ m/( *?)(0x\w{2})\s+(".*?")[ \t]*?([\w_]*?)\Z/;
62
63 @order[$counter] = length($1);
64 @octet[$counter] = $2;
65 @name[$counter] = $3;
66
67 if (length($1) > $max_order)
68 {
69 $max_order = length($1);
70 }
71 if (length($3) > $max_name)
72 {
73 $max_name = length($3);
74 }
75 if (length($4) > 0)
76 {
77 printf OID_H "#define %s%s%d\n", $4, "\t" x ((39-length($4))/4), $counter;
78 }
79 $counter++;
80 }
81
82 printf OID_H "\n#define OID_MAX%s%d\n", "\t" x 8, $counter;
83
84 print OID_H "\n#endif /* OID_H_ */\n";
85
86 close SRC;
87 close OID_H;
88
89 # Generate oid.c
90
91 open(OID_C, ">oid.c")
92 or die "could not open 'oid.c': $!";
93
94 print OID_C "/* List of some useful object identifiers (OIDs)\n",
95 " * ", $copyright, "\n",
96 " * \n",
97 " * ", $automatic, "\n",
98 " * ", $warning, "\n",
99 " */\n",
100 "\n",
101 "#include <stdlib.h>\n",
102 "\n",
103 "#include \"oid.h\"\n",
104 "\n",
105 "const oid_t oid_names[] = {\n";
106
107 for ($c = 0; $c < $counter; $c++)
108 {
109 $next = 0;
110
111 for ($d = $c+1; $d < $counter && @order[$d] >= @order[$c]; $d++)
112 {
113 if (@order[$d] == @order[$c])
114 {
115 @next[$c] = $d;
116 last;
117 }
118 }
119
120 printf OID_C " {%s%s,%s%3d, %d, %2d, %s%s}%s /* %3d */\n"
121 ,' ' x @order[$c]
122 , @octet[$c]
123 , ' ' x (1 + $max_order - @order[$c])
124 , @next[$c]
125 , @order[$c+1] > @order[$c]
126 , @order[$c] / 2
127 , @name[$c]
128 , ' ' x ($max_name - length(@name[$c]))
129 , $c != $counter-1 ? "," : " "
130 , $c;
131 }
132
133 print OID_C "};\n" ;
134 close OID_C;