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