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