0bf1e4878f66dc854df12fb808234395f7efe83b
[openssl.git] / crypto / objects / obj_dat.pl
1 #!/usr/local/bin/perl
2
3 # fixes bug in floating point emulation on sparc64 when
4 # this script produces off-by-one output on sparc64
5 use integer;
6
7 sub obj_cmp
8         {
9         local(@a,@b,$_,$r);
10
11         $A=$obj_len{$obj{$nid{$a}}};
12         $B=$obj_len{$obj{$nid{$b}}};
13
14         $r=($A-$B);
15         return($r) if $r != 0;
16
17         $A=$obj_der{$obj{$nid{$a}}};
18         $B=$obj_der{$obj{$nid{$b}}};
19
20         return($A cmp $B);
21         }
22
23 sub expand_obj
24         {
25         local(*v)=@_;
26         local($k,$d);
27         local($i);
28
29         do      {
30                 $i=0;
31                 foreach $k (keys %v)
32                         {
33                         if (($v{$k} =~ s/(OBJ_[^,]+),/$v{$1},/))
34                                 { $i++; }
35                         }
36                 } while($i);
37         foreach $k (keys %v)
38                 {
39                 @a=split(/,/,$v{$k});
40                 $objn{$k}=$#a+1;
41                 }
42         return(%objn);
43         }
44
45 open (IN,"$ARGV[0]") || die "Can't open input file $ARGV[0]";
46 open (OUT,">$ARGV[1]") || die "Can't open output file $ARGV[1]";
47
48 while (<IN>)
49         {
50         next unless /^\#define\s+(\S+)\s+(.*)$/;
51         $v=$1;
52         $d=$2;
53         $d =~ s/^\"//;
54         $d =~ s/\"$//;
55         if ($v =~ /^SN_(.*)$/)
56                 {
57                 if(defined $snames{$d})
58                         {
59                         print "WARNING: Duplicate short name \"$d\"\n";
60                         }
61                 else 
62                         { $snames{$d} = "X"; }
63                 $sn{$1}=$d;
64                 }
65         elsif ($v =~ /^LN_(.*)$/)
66                 {
67                 if(defined $lnames{$d})
68                         {
69                         print "WARNING: Duplicate long name \"$d\"\n";
70                         }
71                 else 
72                         { $lnames{$d} = "X"; }
73                 $ln{$1}=$d;
74                 }
75         elsif ($v =~ /^NID_(.*)$/)
76                 { $nid{$d}=$1; }
77         elsif ($v =~ /^OBJ_(.*)$/)
78                 {
79                 $obj{$1}=$v;
80                 $objd{$v}=$d;
81                 }
82         }
83 close IN;
84
85 %ob=&expand_obj(*objd);
86
87 @a=sort { $a <=> $b } keys %nid;
88 $n=$a[$#a]+1;
89
90 @lvalues=();
91 $lvalues=0;
92
93 for ($i=0; $i<$n; $i++)
94         {
95         if (!defined($nid{$i}))
96                 {
97                 push(@out,"{NULL,NULL,NID_undef,0,NULL,0},\n");
98                 }
99         else
100                 {
101                 $sn=defined($sn{$nid{$i}})?"$sn{$nid{$i}}":"NULL";
102                 $ln=defined($ln{$nid{$i}})?"$ln{$nid{$i}}":"NULL";
103
104                 if ($sn eq "NULL") {
105                         $sn=$ln;
106                         $sn{$nid{$i}} = $ln;
107                 }
108
109                 if ($ln eq "NULL") {
110                         $ln=$sn;
111                         $ln{$nid{$i}} = $sn;
112                 }
113                         
114                 $out ="{";
115                 $out.="\"$sn\"";
116                 $out.=","."\"$ln\"";
117                 $out.=",NID_$nid{$i},";
118                 if (defined($obj{$nid{$i}}) && $objd{$obj{$nid{$i}}} =~ /,/)
119                         {
120                         $v=$objd{$obj{$nid{$i}}};
121                         $v =~ s/L//g;
122                         $v =~ s/,/ /g;
123                         $r=&der_it($v);
124                         $z="";
125                         $length=0;
126                         foreach (unpack("C*",$r))
127                                 {
128                                 $z.=sprintf("0x%02X,",$_);
129                                 $length++;
130                                 }
131                         $obj_der{$obj{$nid{$i}}}=$z;
132                         $obj_len{$obj{$nid{$i}}}=$length;
133
134                         push(@lvalues,sprintf("%-45s/* [%3d] %s */\n",
135                                 $z,$lvalues,$obj{$nid{$i}}));
136                         $out.="$length,&(lvalues[$lvalues]),0";
137                         $lvalues+=$length;
138                         }
139                 else
140                         {
141                         $out.="0,NULL,0";
142                         }
143                 $out.="},\n";
144                 push(@out,$out);
145                 }
146         }
147
148 @a=grep(defined($sn{$nid{$_}}),0 .. $n);
149 foreach (sort { $sn{$nid{$a}} cmp $sn{$nid{$b}} } @a)
150         {
151         push(@sn,sprintf("%2d,\t/* \"$sn{$nid{$_}}\" */\n",$_));
152         }
153
154 @a=grep(defined($ln{$nid{$_}}),0 .. $n);
155 foreach (sort { $ln{$nid{$a}} cmp $ln{$nid{$b}} } @a)
156         {
157         push(@ln,sprintf("%2d,\t/* \"$ln{$nid{$_}}\" */\n",$_));
158         }
159
160 @a=grep(defined($obj{$nid{$_}}),0 .. $n);
161 foreach (sort obj_cmp @a)
162         {
163         $m=$obj{$nid{$_}};
164         $v=$objd{$m};
165         $v =~ s/L//g;
166         $v =~ s/,/ /g;
167         push(@ob,sprintf("%2d,\t/* %-32s %s */\n",$_,$m,$v));
168         }
169
170 print OUT <<'EOF';
171 /* THIS FILE IS GENERATED FROM objects.h by obj_dat.pl via the
172  * following command:
173  * perl obj_dat.pl obj_mac.h obj_dat.h
174  */
175
176 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
177  * All rights reserved.
178  *
179  * This package is an SSL implementation written
180  * by Eric Young (eay@cryptsoft.com).
181  * The implementation was written so as to conform with Netscapes SSL.
182  * 
183  * This library is free for commercial and non-commercial use as long as
184  * the following conditions are aheared to.  The following conditions
185  * apply to all code found in this distribution, be it the RC4, RSA,
186  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
187  * included with this distribution is covered by the same copyright terms
188  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
189  * 
190  * Copyright remains Eric Young's, and as such any Copyright notices in
191  * the code are not to be removed.
192  * If this package is used in a product, Eric Young should be given attribution
193  * as the author of the parts of the library used.
194  * This can be in the form of a textual message at program startup or
195  * in documentation (online or textual) provided with the package.
196  * 
197  * Redistribution and use in source and binary forms, with or without
198  * modification, are permitted provided that the following conditions
199  * are met:
200  * 1. Redistributions of source code must retain the copyright
201  *    notice, this list of conditions and the following disclaimer.
202  * 2. Redistributions in binary form must reproduce the above copyright
203  *    notice, this list of conditions and the following disclaimer in the
204  *    documentation and/or other materials provided with the distribution.
205  * 3. All advertising materials mentioning features or use of this software
206  *    must display the following acknowledgement:
207  *    "This product includes cryptographic software written by
208  *     Eric Young (eay@cryptsoft.com)"
209  *    The word 'cryptographic' can be left out if the rouines from the library
210  *    being used are not cryptographic related :-).
211  * 4. If you include any Windows specific code (or a derivative thereof) from 
212  *    the apps directory (application code) you must include an acknowledgement:
213  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
214  * 
215  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
216  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
218  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
219  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
220  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
221  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
222  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
223  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
224  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
225  * SUCH DAMAGE.
226  * 
227  * The licence and distribution terms for any publically available version or
228  * derivative of this code cannot be changed.  i.e. this code cannot simply be
229  * copied and put under another distribution licence
230  * [including the GNU Public Licence.]
231  */
232
233 EOF
234
235 printf OUT "#define NUM_NID %d\n",$n;
236 printf OUT "#define NUM_SN %d\n",$#sn+1;
237 printf OUT "#define NUM_LN %d\n",$#ln+1;
238 printf OUT "#define NUM_OBJ %d\n\n",$#ob+1;
239
240 printf OUT "static const unsigned char lvalues[%d]={\n",$lvalues+1;
241 print OUT @lvalues;
242 print OUT "};\n\n";
243
244 printf OUT "static const ASN1_OBJECT nid_objs[NUM_NID]={\n";
245 foreach (@out)
246         {
247         if (length($_) > 75)
248                 {
249                 $out="";
250                 foreach (split(/,/))
251                         {
252                         $t=$out.$_.",";
253                         if (length($t) > 70)
254                                 {
255                                 print OUT "$out\n";
256                                 $t="\t$_,";
257                                 }
258                         $out=$t;
259                         }
260                 chop $out;      # Get rid of the last comma
261                 print OUT "$out";
262                 }
263         else
264                 { print OUT $_; }
265         }
266 print  OUT "};\n\n";
267
268 printf OUT "static const unsigned int sn_objs[NUM_SN]={\n";
269 print  OUT @sn;
270 print  OUT "};\n\n";
271
272 printf OUT "static const unsigned int ln_objs[NUM_LN]={\n";
273 print  OUT @ln;
274 print  OUT "};\n\n";
275
276 printf OUT "static const unsigned int obj_objs[NUM_OBJ]={\n";
277 print  OUT @ob;
278 print  OUT "};\n\n";
279
280 close OUT;
281
282 sub der_it
283         {
284         local($v)=@_;
285         local(@a,$i,$ret,@r);
286
287         @a=split(/\s+/,$v);
288         $ret.=pack("C*",$a[0]*40+$a[1]);
289         shift @a;
290         shift @a;
291         foreach (@a)
292                 {
293                 @r=();
294                 $t=0;
295                 while ($_ >= 128)
296                         {
297                         $x=$_%128;
298                         $_/=128;
299                         push(@r,((($t++)?0x80:0)|$x));
300                         }
301                 push(@r,((($t++)?0x80:0)|$_));
302                 $ret.=pack("C*",reverse(@r));
303                 }
304         return($ret);
305         }