Make 25-test_gen.t and 25-test_req.t into one
[openssl.git] / crypto / objects / obj_dat.pl
1 #! /usr/bin/env perl
2 # Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (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 # fixes bug in floating point emulation on sparc64 when
10 # this script produces off-by-one output on sparc64
11
12 use integer;
13
14 sub obj_cmp
15         {
16         local(@a,@b,$_,$r);
17
18         $A=$obj_len{$obj{$nid{$a}}};
19         $B=$obj_len{$obj{$nid{$b}}};
20
21         $r=($A-$B);
22         return($r) if $r != 0;
23
24         $A=$obj_der{$obj{$nid{$a}}};
25         $B=$obj_der{$obj{$nid{$b}}};
26
27         return($A cmp $B);
28         }
29
30 sub expand_obj
31         {
32         local(*v)=@_;
33         local($k,$d);
34         local($i);
35
36         do      {
37                 $i=0;
38                 foreach $k (keys %v)
39                         {
40                         if (($v{$k} =~ s/(OBJ_[^,]+),/$v{$1},/))
41                                 { $i++; }
42                         }
43                 } while($i);
44         foreach $k (keys %v)
45                 {
46                 @a=split(/,/,$v{$k});
47                 $objn{$k}=$#a+1;
48                 }
49         return(%objn);
50         }
51
52 open (IN,"$ARGV[0]") || die "Can't open input file $ARGV[0]";
53 open (OUT,">$ARGV[1]") || die "Can't open output file $ARGV[1]";
54
55 while (<IN>)
56         {
57         next unless /^\#define\s+(\S+)\s+(.*)$/;
58         $v=$1;
59         $d=$2;
60         $d =~ s/^\"//;
61         $d =~ s/\"$//;
62         if ($v =~ /^SN_(.*)$/)
63                 {
64                 if(defined $snames{$d})
65                         {
66                         print "WARNING: Duplicate short name \"$d\"\n";
67                         }
68                 else 
69                         { $snames{$d} = "X"; }
70                 $sn{$1}=$d;
71                 }
72         elsif ($v =~ /^LN_(.*)$/)
73                 {
74                 if(defined $lnames{$d})
75                         {
76                         print "WARNING: Duplicate long name \"$d\"\n";
77                         }
78                 else 
79                         { $lnames{$d} = "X"; }
80                 $ln{$1}=$d;
81                 }
82         elsif ($v =~ /^NID_(.*)$/)
83                 { $nid{$d}=$1; }
84         elsif ($v =~ /^OBJ_(.*)$/)
85                 {
86                 $obj{$1}=$v;
87                 $objd{$v}=$d;
88                 }
89         }
90 close IN;
91
92 %ob=&expand_obj(*objd);
93
94 @a=sort { $a <=> $b } keys %nid;
95 $n=$a[$#a]+1;
96
97 @lvalues=();
98 $lvalues=0;
99
100 for ($i=0; $i<$n; $i++)
101         {
102         if (!defined($nid{$i}))
103                 {
104                 push(@out,"{NULL,NULL,NID_undef,0,NULL,0},\n");
105                 }
106         else
107                 {
108                 $sn=defined($sn{$nid{$i}})?"$sn{$nid{$i}}":"NULL";
109                 $ln=defined($ln{$nid{$i}})?"$ln{$nid{$i}}":"NULL";
110
111                 if ($sn eq "NULL") {
112                         $sn=$ln;
113                         $sn{$nid{$i}} = $ln;
114                 }
115
116                 if ($ln eq "NULL") {
117                         $ln=$sn;
118                         $ln{$nid{$i}} = $sn;
119                 }
120                         
121                 $out ="{";
122                 $out.="\"$sn\"";
123                 $out.=","."\"$ln\"";
124                 $out.=",NID_$nid{$i},";
125                 if (defined($obj{$nid{$i}}) && $objd{$obj{$nid{$i}}} =~ /,/)
126                         {
127                         $v=$objd{$obj{$nid{$i}}};
128                         $v =~ s/L//g;
129                         $v =~ s/,/ /g;
130                         $r=&der_it($v);
131                         $z="";
132                         $length=0;
133                         foreach (unpack("C*",$r))
134                                 {
135                                 $z.=sprintf("0x%02X,",$_);
136                                 $length++;
137                                 }
138                         $obj_der{$obj{$nid{$i}}}=$z;
139                         $obj_len{$obj{$nid{$i}}}=$length;
140
141                         push(@lvalues,sprintf("%-45s/* [%3d] %s */\n",
142                                 $z,$lvalues,$obj{$nid{$i}}));
143                         $out.="$length,&(lvalues[$lvalues]),0";
144                         $lvalues+=$length;
145                         }
146                 else
147                         {
148                         $out.="0,NULL,0";
149                         }
150                 $out.="},\n";
151                 push(@out,$out);
152                 }
153         }
154
155 @a=grep(defined($sn{$nid{$_}}),0 .. $n);
156 foreach (sort { $sn{$nid{$a}} cmp $sn{$nid{$b}} } @a)
157         {
158         push(@sn,sprintf("%2d,\t/* \"$sn{$nid{$_}}\" */\n",$_));
159         }
160
161 @a=grep(defined($ln{$nid{$_}}),0 .. $n);
162 foreach (sort { $ln{$nid{$a}} cmp $ln{$nid{$b}} } @a)
163         {
164         push(@ln,sprintf("%2d,\t/* \"$ln{$nid{$_}}\" */\n",$_));
165         }
166
167 @a=grep(defined($obj{$nid{$_}}),0 .. $n);
168 foreach (sort obj_cmp @a)
169         {
170         $m=$obj{$nid{$_}};
171         $v=$objd{$m};
172         $v =~ s/L//g;
173         $v =~ s/,/ /g;
174         push(@ob,sprintf("%2d,\t/* %-32s %s */\n",$_,$m,$v));
175         }
176
177 print OUT <<'EOF';
178 /*
179  * WARNING: do not edit!
180  * Generated by crypto/objects/obj_dat.pl
181  *
182  * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
183  * Licensed under the OpenSSL license (the "License").  You may not use
184  * this file except in compliance with the License.  You can obtain a copy
185  * in the file LICENSE in the source distribution or at
186  * https://www.openssl.org/source/license.html
187  */
188
189 EOF
190
191 printf OUT "#define NUM_NID %d\n",$n;
192 printf OUT "#define NUM_SN %d\n",$#sn+1;
193 printf OUT "#define NUM_LN %d\n",$#ln+1;
194 printf OUT "#define NUM_OBJ %d\n\n",$#ob+1;
195
196 printf OUT "static const unsigned char lvalues[%d]={\n",$lvalues+1;
197 print OUT @lvalues;
198 print OUT "};\n\n";
199
200 printf OUT "static const ASN1_OBJECT nid_objs[NUM_NID]={\n";
201 foreach (@out)
202         {
203         if (length($_) > 75)
204                 {
205                 $out="";
206                 foreach (split(/,/))
207                         {
208                         $t=$out.$_.",";
209                         if (length($t) > 70)
210                                 {
211                                 print OUT "$out\n";
212                                 $t="\t$_,";
213                                 }
214                         $out=$t;
215                         }
216                 chop $out;      # Get rid of the last comma
217                 print OUT "$out";
218                 }
219         else
220                 { print OUT $_; }
221         }
222 print  OUT "};\n\n";
223
224 printf OUT "static const unsigned int sn_objs[NUM_SN]={\n";
225 print  OUT @sn;
226 print  OUT "};\n\n";
227
228 printf OUT "static const unsigned int ln_objs[NUM_LN]={\n";
229 print  OUT @ln;
230 print  OUT "};\n\n";
231
232 printf OUT "static const unsigned int obj_objs[NUM_OBJ]={\n";
233 print  OUT @ob;
234 print  OUT "};\n\n";
235
236 close OUT;
237
238 sub der_it
239         {
240         local($v)=@_;
241         local(@a,$i,$ret,@r);
242
243         @a=split(/\s+/,$v);
244         $ret.=pack("C*",$a[0]*40+$a[1]);
245         shift @a;
246         shift @a;
247         foreach (@a)
248                 {
249                 @r=();
250                 $t=0;
251                 while ($_ >= 128)
252                         {
253                         $x=$_%128;
254                         $_/=128;
255                         push(@r,((($t++)?0x80:0)|$x));
256                         }
257                 push(@r,((($t++)?0x80:0)|$_));
258                 $ret.=pack("C*",reverse(@r));
259                 }
260         return($ret);
261         }