]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/objects/objects.pl
f4f11fa45208e1399652bf3fa7e2b6f1d68865a9
[thirdparty/openssl.git] / crypto / objects / objects.pl
1 #! /usr/bin/env perl
2 # Copyright 2000-2019 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License"). You may not use
5 # this file except in compliance with the License. You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 use Getopt::Std;
10
11 our($opt_n);
12 getopts('n');
13
14 # The year the output file is generated.
15 my $YEAR = [localtime()]->[5] + 1900;
16
17 open (NUMIN,"$ARGV[1]") || die "Can't open number file $ARGV[1]";
18 $max_nid=0;
19 $o=0;
20 while(<NUMIN>)
21 {
22 s|\R$||;
23 $o++;
24 s/#.*$//;
25 next if /^\s*$/;
26 $_ = 'X'.$_;
27 ($Cname,$mynum) = split;
28 $Cname =~ s/^X//;
29 if (defined($nidn{$mynum}))
30 { die "$ARGV[1]:$o:There's already an object with NID ",$mynum," on line ",$order{$mynum},"\n"; }
31 if (defined($nid{$Cname}))
32 { die "$ARGV[1]:$o:There's already an object with name ",$Cname," on line ",$order{$nid{$Cname}},"\n"; }
33 $nid{$Cname} = $mynum;
34 $nidn{$mynum} = $Cname;
35 $order{$mynum} = $o;
36 $max_nid = $mynum if $mynum > $max_nid;
37 }
38 close NUMIN;
39
40 open (IN,"$ARGV[0]") || die "Can't open input file $ARGV[0]";
41 $Cname="";
42 $o=0;
43 while (<IN>)
44 {
45 s|\R$||;
46 $o++;
47 if (/^!module\s+(.*)$/)
48 {
49 $module = $1."-";
50 $module =~ s/\./_/g;
51 $module =~ s/-/_/g;
52 }
53 if (/^!global$/)
54 { $module = ""; }
55 if (/^!Cname\s+(.*)$/)
56 { $Cname = $1; }
57 if (/^!Alias\s+(.+?)\s+(.*)$/)
58 {
59 $Cname = $module.$1;
60 $myoid = $2;
61 $myoid = &process_oid($myoid);
62 $Cname =~ s/-/_/g;
63 $ordern{$o} = $Cname;
64 $order{$Cname} = $o;
65 $obj{$Cname} = $myoid;
66 $_ = "";
67 $Cname = "";
68 }
69 s/!.*$//;
70 s/#.*$//;
71 next if /^\s*$/;
72 ($myoid,$mysn,$myln) = split ':';
73 $mysn =~ s/^\s*//;
74 $mysn =~ s/\s*$//;
75 $myln =~ s/^\s*//;
76 $myln =~ s/\s*$//;
77 $myoid =~ s/^\s*//;
78 $myoid =~ s/\s*$//;
79 if ($myoid ne "")
80 {
81 $myoid = &process_oid($myoid);
82 }
83
84 if ($Cname eq "" && ($myln =~ /^[_A-Za-z][\w.-]*$/ ))
85 {
86 $Cname = $myln;
87 $Cname =~ s/\./_/g;
88 $Cname =~ s/-/_/g;
89 if ($Cname ne "" && defined($ln{$module.$Cname}))
90 { die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
91 }
92 if ($Cname eq "")
93 {
94 $Cname = $mysn;
95 $Cname =~ s/-/_/g;
96 if ($Cname ne "" && defined($sn{$module.$Cname}))
97 { die "objects.txt:$o:There's already an object with short name ",$sn{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
98 }
99 if ($Cname eq "")
100 {
101 $Cname = $myln;
102 $Cname =~ s/-/_/g;
103 $Cname =~ s/\./_/g;
104 $Cname =~ s/ /_/g;
105 if ($Cname ne "" && defined($ln{$module.$Cname}))
106 { die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
107 }
108 $Cname =~ s/\./_/g;
109 $Cname =~ s/-/_/g;
110 $Cname = $module.$Cname;
111 $ordern{$o} = $Cname;
112 $order{$Cname} = $o;
113 $sn{$Cname} = $mysn;
114 $ln{$Cname} = $myln;
115 $obj{$Cname} = $myoid;
116 if (!defined($nid{$Cname}))
117 {
118 $max_nid++;
119 $nid{$Cname} = $max_nid;
120 $nidn{$max_nid} = $Cname;
121 print STDERR "Added OID $Cname\n";
122 }
123 $Cname="";
124 }
125 close IN;
126
127 if ( $opt_n ) {
128 foreach (sort { $a <=> $b } keys %nidn)
129 {
130 print $nidn{$_},"\t\t",$_,"\n";
131 }
132 exit;
133 }
134
135 print <<"EOF";
136 /*
137 * WARNING: do not edit!
138 * Generated by crypto/objects/objects.pl
139 *
140 * Copyright 2000-$YEAR The OpenSSL Project Authors. All Rights Reserved.
141 * Licensed under the Apache License 2.0 (the "License"). You may not use
142 * this file except in compliance with the License. You can obtain a copy
143 * in the file LICENSE in the source distribution or at
144 * https://www.openssl.org/source/license.html
145 */
146
147 #define SN_undef "UNDEF"
148 #define LN_undef "undefined"
149 #define NID_undef 0
150 #define OBJ_undef 0L
151 EOF
152
153 sub expand
154 {
155 my $string = shift;
156
157 1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
158
159 return $string;
160 }
161
162 foreach (sort { $a <=> $b } keys %ordern)
163 {
164 $Cname=$ordern{$_};
165 print "\n";
166 print expand("#define SN_$Cname\t\t\"$sn{$Cname}\"\n") if $sn{$Cname} ne "";
167 print expand("#define LN_$Cname\t\t\"$ln{$Cname}\"\n") if $ln{$Cname} ne "";
168 print expand("#define NID_$Cname\t\t$nid{$Cname}\n") if $nid{$Cname} ne "";
169 print expand("#define OBJ_$Cname\t\t$obj{$Cname}\n") if $obj{$Cname} ne "";
170 }
171
172 sub process_oid
173 {
174 local($oid)=@_;
175 local(@a,$oid_pref);
176
177 @a = split(/\s+/,$myoid);
178 $pref_oid = "";
179 $pref_sep = "";
180 if (!($a[0] =~ /^[0-9]+$/))
181 {
182 $a[0] =~ s/-/_/g;
183 if (!defined($obj{$a[0]}))
184 { die "$ARGV[0]:$o:Undefined identifier ",$a[0],"\n"; }
185 $pref_oid = "OBJ_" . $a[0];
186 $pref_sep = ",";
187 shift @a;
188 }
189 $oids = join('L,',@a) . "L";
190 if ($oids ne "L")
191 {
192 $oids = $pref_oid . $pref_sep . $oids;
193 }
194 else
195 {
196 $oids = $pref_oid;
197 }
198 return($oids);
199 }