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