linux-elf bugfix
[openssl.git] / util / mkdef.pl
1 #!/usr/local/bin/perl -w
2 #
3 # generate a .def file
4 #
5 # It does this by parsing the header files and looking for the
6 # prototyped functions: it then prunes the output.
7 #
8
9 my $crypto_num="util/libeay.num";
10 my $ssl_num=   "util/ssleay.num";
11
12 my $do_update = 0;
13 my $do_crypto = 0;
14 my $do_ssl = 0;
15 my $do_ctest = 0;
16 my $rsaref = 0;
17
18 my $W32=1;
19 my $NT=0;
20 # Set this to make typesafe STACK definitions appear in DEF
21 my $safe_stack_def = 1;
22
23 my $options="";
24 open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
25 while(<IN>) {
26     $options=$1 if (/^OPTIONS=(.*)$/);
27 }
28 close(IN);
29
30 # The following ciphers may be excluded (by Configure). This means functions
31 # defined with ifndef(NO_XXX) are not included in the .def file, and everything
32 # in directory xxx is ignored.
33 my $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
34 my $no_cast; my $no_md2; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
35 my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0;
36
37 foreach (@ARGV, split(/ /, $options))
38         {
39         $W32=1 if $_ eq "32";
40         $W32=0 if $_ eq "16";
41         if($_ eq "NT") {
42                 $W32 = 1;
43                 $NT = 1;
44         }
45         $do_ssl=1 if $_ eq "ssleay";
46         $do_ssl=1 if $_ eq "ssl";
47         $do_crypto=1 if $_ eq "libeay";
48         $do_crypto=1 if $_ eq "crypto";
49         $do_update=1 if $_ eq "update";
50         $do_ctest=1 if $_ eq "ctest";
51         $rsaref=1 if $_ eq "rsaref";
52
53         if    (/^no-rc2$/)      { $no_rc2=1; }
54         elsif (/^no-rc4$/)      { $no_rc4=1; }
55         elsif (/^no-rc5$/)      { $no_rc5=1; }
56         elsif (/^no-idea$/)     { $no_idea=1; }
57         elsif (/^no-des$/)      { $no_des=1; }
58         elsif (/^no-bf$/)       { $no_bf=1; }
59         elsif (/^no-cast$/)     { $no_cast=1; }
60         elsif (/^no-md2$/)      { $no_md2=1; }
61         elsif (/^no-md5$/)      { $no_md5=1; }
62         elsif (/^no-sha$/)      { $no_sha=1; }
63         elsif (/^no-ripemd$/)   { $no_ripemd=1; }
64         elsif (/^no-mdc2$/)     { $no_mdc2=1; }
65         elsif (/^no-rsa$/)      { $no_rsa=1; }
66         elsif (/^no-dsa$/)      { $no_dsa=1; }
67         elsif (/^no-dh$/)       { $no_dh=1; }
68         elsif (/^no-hmac$/)     { $no_hmac=1; }
69         }
70
71
72 if (!$do_ssl && !$do_crypto)
73         {
74         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT ] [rsaref]\n";
75         exit(1);
76         }
77
78 %ssl_list=&load_numbers($ssl_num);
79 $max_ssl = $max_num;
80 %crypto_list=&load_numbers($crypto_num);
81 $max_crypto = $max_num;
82
83 my $ssl="ssl/ssl.h";
84
85 my $crypto ="crypto/crypto.h";
86 $crypto.=" crypto/des/des.h" unless $no_des;
87 $crypto.=" crypto/idea/idea.h" unless $no_idea;
88 $crypto.=" crypto/rc4/rc4.h" unless $no_rc4;
89 $crypto.=" crypto/rc5/rc5.h" unless $no_rc5;
90 $crypto.=" crypto/rc2/rc2.h" unless $no_rc2;
91 $crypto.=" crypto/bf/blowfish.h" unless $no_bf;
92 $crypto.=" crypto/cast/cast.h" unless $no_cast;
93 $crypto.=" crypto/md2/md2.h" unless $no_md2;
94 $crypto.=" crypto/md5/md5.h" unless $no_md5;
95 $crypto.=" crypto/mdc2/mdc2.h" unless $no_mdc2;
96 $crypto.=" crypto/sha/sha.h" unless $no_sha;
97 $crypto.=" crypto/ripemd/ripemd.h" unless $no_ripemd;
98
99 $crypto.=" crypto/bn/bn.h";
100 $crypto.=" crypto/rsa/rsa.h" unless $no_rsa;
101 $crypto.=" crypto/dsa/dsa.h" unless $no_dsa;
102 $crypto.=" crypto/dh/dh.h" unless $no_dh;
103 $crypto.=" crypto/hmac/hmac.h" unless $no_hmac;
104
105 $crypto.=" crypto/dso/dso.h";
106 $crypto.=" crypto/stack/stack.h";
107 $crypto.=" crypto/buffer/buffer.h";
108 $crypto.=" crypto/bio/bio.h";
109 $crypto.=" crypto/lhash/lhash.h";
110 $crypto.=" crypto/conf/conf.h";
111 $crypto.=" crypto/txt_db/txt_db.h";
112
113 $crypto.=" crypto/evp/evp.h";
114 $crypto.=" crypto/objects/objects.h";
115 $crypto.=" crypto/pem/pem.h";
116 #$crypto.=" crypto/meth/meth.h";
117 $crypto.=" crypto/asn1/asn1.h";
118 $crypto.=" crypto/asn1/asn1_mac.h";
119 $crypto.=" crypto/err/err.h";
120 $crypto.=" crypto/pkcs7/pkcs7.h";
121 $crypto.=" crypto/pkcs12/pkcs12.h";
122 $crypto.=" crypto/x509/x509.h";
123 $crypto.=" crypto/x509/x509_vfy.h";
124 $crypto.=" crypto/x509v3/x509v3.h";
125 $crypto.=" crypto/rand/rand.h";
126 $crypto.=" crypto/comp/comp.h";
127 $crypto.=" crypto/tmdiff.h";
128
129 my @ssl_func = &do_defs("SSLEAY", $ssl);
130 my @crypto_func = &do_defs("LIBEAY", $crypto);
131
132
133 if ($do_update) {
134
135 if ($do_ssl == 1) {
136         open(OUT, ">>$ssl_num");
137         &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl, @ssl_func);
138         close OUT;
139 }
140
141 if($do_crypto == 1) {
142         open(OUT, ">>$crypto_num");
143         &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto, @crypto_func);
144         close OUT;
145
146
147 } elsif ($do_ctest) {
148
149         print <<"EOF";
150
151 /* Test file to check all DEF file symbols are present by trying
152  * to link to all of them. This is *not* intended to be run!
153  */
154
155 int main()
156 {
157 EOF
158         &print_test_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_func)
159                 if $do_ssl == 1;
160
161         &print_test_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_func)
162                 if $do_crypto == 1;
163
164         print "}\n";
165
166 } else {
167
168         &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_func)
169                 if $do_ssl == 1;
170
171         &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_func)
172                 if $do_crypto == 1;
173
174 }
175
176
177 sub do_defs
178 {
179         my($name,$files)=@_;
180         my $file;
181         my @ret;
182         my %funcs;
183         my $cpp;
184
185         foreach $file (split(/\s+/,$files))
186                 {
187                 open(IN,"<$file") || die "unable to open $file:$!\n";
188                 my $line = "", my $def= "";
189                 my %tag = (
190                         FreeBSD         => 0,
191                         NOPROTO         => 0,
192                         WIN16           => 0,
193                         PERL5           => 0,
194                         _WINDLL         => 0,
195                         NO_FP_API       => 0,
196                         CONST_STRICT    => 0,
197                         TRUE            => 1,
198                         NO_RC2          => 0,
199                         NO_RC4          => 0,
200                         NO_RC5          => 0,
201                         NO_IDEA         => 0,
202                         NO_DES          => 0,
203                         NO_BF           => 0,
204                         NO_CAST         => 0,
205                         NO_MD2          => 0,
206                         NO_MD5          => 0,
207                         NO_SHA          => 0,
208                         NO_RIPEMD       => 0,
209                         NO_MDC2         => 0,
210                         NO_RSA          => 0,
211                         NO_DSA          => 0,
212                         NO_DH           => 0,
213                         NO_HMAC         => 0,
214                 );
215                 while(<IN>) {
216                         last if (/BEGIN ERROR CODES/);
217                         if ($line ne '') {
218                                 $_ = $line . $_;
219                                 $line = '';
220                         }
221
222                         if (/\\$/) {
223                                 $line = $_;
224                                 next;
225                         }
226
227                         $cpp = 1 if /^#.*ifdef.*cplusplus/;
228                         if ($cpp) {
229                                 $cpp = 0 if /^#.*endif/;
230                                 next;
231                         }
232
233                         s/\/\*.*?\*\///gs;                   # ignore comments
234                         s/{[^{}]*}//gs;                      # ignore {} blocks
235                         if (/^\#\s*ifndef (.*)/) {
236                                 push(@tag,$1);
237                                 $tag{$1}=-1;
238                                 next;
239                         } elsif (/^\#\s*if !defined\(([^\)]+)\)/) {
240                                 push(@tag,$1);
241                                 $tag{$1}=-1;
242                                 next;
243                         } elsif (/^\#\s*ifdef (.*)/) {
244                                 push(@tag,$1);
245                                 $tag{$1}=1;
246                                 next;
247                         } elsif (/^\#\s*if defined(.*)/) {
248                                 push(@tag,$1);
249                                 $tag{$1}=1;
250                                 next;
251                         } elsif (/^\#\s*endif/) {
252                                 $tag{$tag[$#tag]}=0;
253                                 pop(@tag);
254                                 next;
255                         } elsif (/^\#\s*else/) {
256                                 my $t=$tag[$#tag];
257                                 $tag{$t}= -$tag{$t};
258                                 next;
259                         } elsif (/^\#\s*if\s+1/) {
260                                 # Dummy tag
261                                 push(@tag,"TRUE");
262                                 $tag{"TRUE"}=1;
263                                 next;
264                         } elsif (/^\#\s*if\s+0/) {
265                                 # Dummy tag
266                                 push(@tag,"TRUE");
267                                 $tag{"TRUE"}=-1;
268                                 next;
269                         } elsif (/^\#/) {
270                                 next;
271                         }
272                         if ($safe_stack_def &&
273                                 /^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
274                                 $funcs{"sk_${1}_new"} = 1;
275                                 $funcs{"sk_${1}_new_null"} = 1;
276                                 $funcs{"sk_${1}_free"} = 1;
277                                 $funcs{"sk_${1}_num"} = 1;
278                                 $funcs{"sk_${1}_value"} = 1;
279                                 $funcs{"sk_${1}_set"} = 1;
280                                 $funcs{"sk_${1}_zero"} = 1;
281                                 $funcs{"sk_${1}_push"} = 1;
282                                 $funcs{"sk_${1}_unshift"} = 1;
283                                 $funcs{"sk_${1}_find"} = 1;
284                                 $funcs{"sk_${1}_delete"} = 1;
285                                 $funcs{"sk_${1}_delete_ptr"} = 1;
286                                 $funcs{"sk_${1}_insert"} = 1;
287                                 $funcs{"sk_${1}_set_cmp_func"} = 1;
288                                 $funcs{"sk_${1}_dup"} = 1;
289                                 $funcs{"sk_${1}_pop_free"} = 1;
290                                 $funcs{"sk_${1}_shift"} = 1;
291                                 $funcs{"sk_${1}_pop"} = 1;
292                                 $funcs{"sk_${1}_sort"} = 1;
293                         } elsif ($safe_stack_def &&
294                                 /^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
295                                 $funcs{"d2i_ASN1_SET_OF_${1}"} = 1;
296                                 $funcs{"i2d_ASN1_SET_OF_${1}"} = 1;
297                         } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
298                                      /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
299                                 if($W32) {
300                                         $funcs{"PEM_read_${1}"} = 1;
301                                         $funcs{"PEM_write_${1}"} = 1;
302                                 }
303                                 $funcs{"PEM_read_bio_${1}"} = 1;
304                                 $funcs{"PEM_write_bio_${1}"} = 1;
305                         } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
306                                      /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
307                                 if($W32) {
308                                         $funcs{"PEM_write_${1}"} = 1;
309                                 }
310                                 $funcs{"PEM_write_bio_${1}"} = 1;
311                         } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
312                                      /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
313                                 if($W32) {
314                                         $funcs{"PEM_read_${1}"} = 1;
315                                 }
316                                 $funcs{"PEM_read_bio_${1}"} = 1;
317                         } elsif (
318                                 ($tag{'TRUE'} != -1) &&
319                                 ($tag{'FreeBSD'} != 1) &&
320                                 ($tag{'CONST_STRICT'} != 1) &&
321                                 (($W32 && ($tag{'WIN16'} != 1)) ||
322                                  (!$W32 && ($tag{'WIN16'} != -1))) &&
323                                 ($tag{'PERL5'} != 1) &&
324 #                               ($tag{'_WINDLL'} != -1) &&
325                                 ((!$W32 && $tag{'_WINDLL'} != -1) ||
326                                  ($W32 && $tag{'_WINDLL'} != 1)) &&
327                                 ((($tag{'NO_FP_API'} != 1) && $W32) ||
328                                  (($tag{'NO_FP_API'} != -1) && !$W32)) &&
329                                 ($tag{'NO_RC2'} == 0  || !$no_rc2) &&
330                                 ($tag{'NO_RC4'} == 0  || !$no_rc4) &&
331                                 ($tag{'NO_RC5'} == 0  || !$no_rc5) &&
332                                 ($tag{'NO_IDEA'} == 0 || !$no_idea) &&
333                                 ($tag{'NO_DES'} == 0  || !$no_des) &&
334                                 ($tag{'NO_BF'} == 0   || !$no_bf) &&
335                                 ($tag{'NO_CAST'} == 0 || !$no_cast) &&
336                                 ($tag{'NO_MD2'} == 0  || !$no_md2) &&
337                                 ($tag{'NO_MD5'} == 0  || !$no_md5) &&
338                                 ($tag{'NO_SHA'} == 0  || !$no_sha) &&
339                                 ($tag{'NO_RIPEMD'} == 0 || !$no_ripemd) &&
340                                 ($tag{'NO_MDC2'} == 0 || !$no_mdc2) &&
341                                 ($tag{'NO_RSA'} == 0  || !$no_rsa) &&
342                                 ($tag{'NO_DSA'} == 0  || !$no_dsa) &&
343                                 ($tag{'NO_DH'} == 0   || !$no_dh) &&
344                                 ($tag{'NO_HMAC'} == 0 || !$no_hmac))
345                                 {
346                                         if (/{|\/\*/) { # }
347                                                 $line = $_;
348                                         } else {
349                                                 $def .= $_;
350                                         }
351                                 }
352                         }
353                 close(IN);
354
355                 foreach (split /;/, $def) {
356                         s/^[\n\s]*//g;
357                         s/[\n\s]*$//g;
358                         next if(/typedef\W/);
359                         next if(/EVP_bf/ and $no_bf);
360                         next if(/EVP_cast/ and $no_cast);
361                         next if(/EVP_des/ and $no_des);
362                         next if(/EVP_dss/ and $no_dsa);
363                         next if(/EVP_idea/ and $no_idea);
364                         next if(/EVP_md2/ and $no_md2);
365                         next if(/EVP_md5/ and $no_md5);
366                         next if(/EVP_rc2/ and $no_rc2);
367                         next if(/EVP_rc4/ and $no_rc4);
368                         next if(/EVP_rc5/ and $no_rc5);
369                         next if(/EVP_ripemd/ and $no_ripemd);
370                         next if(/EVP_sha/ and $no_sha);
371                         if (/\(\*(\w*)\([^\)]+/) {
372                                 $funcs{$1} = 1;
373                         } elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
374                                 # K&R C
375                                 next;
376                         } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
377                                 while (not /\(\)$/s) {
378                                         s/[^\(\)]*\)$/\)/s;
379                                         s/\([^\(\)]*\)\)$/\)/s;
380                                 }
381                                 s/\(void\)//;
382                                 /(\w+)\W*\(\)/s;
383                                 $funcs{$1} = 1;
384                         } elsif (/\(/ and not (/=/)) {
385                                 print STDERR "File $file: cannot parse: $_;\n";
386                         }
387                 }
388         }
389
390         # Prune the returned functions
391
392         delete $funcs{"SSL_add_dir_cert_subjects_to_stack"};
393         delete $funcs{"RSA_PKCS1_RSAref"} unless $rsaref;
394         delete $funcs{"bn_dump1"};
395
396         if($W32) {
397                 delete $funcs{"BIO_s_file_internal"};
398                 delete $funcs{"BIO_new_file_internal"};
399                 delete $funcs{"BIO_new_fp_internal"};
400         } else {
401                 if(exists $funcs{"ERR_load_CRYPTO_strings"}) {
402                         delete $funcs{"ERR_load_CRYPTO_strings"};
403                         $funcs{"ERR_load_CRYPTOlib_strings"} = 1;
404                 }
405                 delete $funcs{"BIO_s_file"};
406                 delete $funcs{"BIO_new_file"};
407                 delete $funcs{"BIO_new_fp"};
408         }
409         if (!$NT) {
410                 delete $funcs{"BIO_s_log"};
411         }
412
413         push @ret, keys %funcs;
414
415         return(@ret);
416 }
417
418 sub print_test_file
419 {
420         (*OUT,my $name,*nums,my @functions)=@_;
421         my $n = 1; my @e; my @r;
422         my $func;
423
424         (@e)=grep(/^SSLeay/,@functions);
425         (@r)=grep(!/^SSLeay/,@functions);
426         @functions=((sort @e),(sort @r));
427
428         foreach $func (@functions) {
429                 if (!defined($nums{$func})) {
430                         printf STDERR "$func does not have a number assigned\n"
431                                         if(!$do_update);
432                 } else {
433                         $n=$nums{$func};
434                         print OUT "\t$func();\n";
435                 }
436         }
437 }
438
439 sub print_def_file
440 {
441         (*OUT,my $name,*nums,my @functions)=@_;
442         my $n = 1; my @e; my @r;
443
444         if ($W32)
445                 { $name.="32"; }
446         else
447                 { $name.="16"; }
448
449         print OUT <<"EOF";
450 ;
451 ; Definition file for the DLL version of the $name library from OpenSSL
452 ;
453
454 LIBRARY         $name
455
456 DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
457
458 EOF
459
460         if (!$W32) {
461                 print <<"EOF";
462 CODE            PRELOAD MOVEABLE
463 DATA            PRELOAD MOVEABLE SINGLE
464
465 EXETYPE         WINDOWS
466
467 HEAPSIZE        4096
468 STACKSIZE       8192
469
470 EOF
471         }
472
473         print "EXPORTS\n";
474
475
476         (@e)=grep(/^SSLeay/,@functions);
477         (@r)=grep(!/^SSLeay/,@functions);
478         @functions=((sort @e),(sort @r));
479
480         foreach $func (@functions) {
481                 if (!defined($nums{$func})) {
482                         printf STDERR "$func does not have a number assigned\n"
483                                         if(!$do_update);
484                 } else {
485                         $n=$nums{$func};
486                         printf OUT "    %s%-40s@%d\n",($W32)?"":"_",$func,$n;
487                 }
488         }
489         printf OUT "\n";
490 }
491
492 sub load_numbers
493 {
494         my($name)=@_;
495         my(@a,%ret);
496
497         $max_num = 0;
498
499         open(IN,"<$name") || die "unable to open $name:$!\n";
500         while (<IN>) {
501                 chop;
502                 s/#.*$//;
503                 next if /^\s*$/;
504                 @a=split;
505                 $ret{$a[0]}=$a[1];
506                 $max_num = $a[1] if $a[1] > $max_num;
507         }
508         close(IN);
509         return(%ret);
510 }
511
512 sub update_numbers
513 {
514         (*OUT,$name,*nums,my $start_num, my @functions)=@_;
515         my $new_funcs = 0;
516         print STDERR "Updating $name\n";
517         foreach $func (@functions) {
518                 if (!exists $nums{$func}) {
519                         $new_funcs++;
520                         printf OUT "%s%-40s%d\n","",$func, ++$start_num;
521                 }
522         }
523         if($new_funcs) {
524                 print STDERR "$new_funcs New Functions added\n";
525         } else {
526                 print STDERR "No New Functions Added\n";
527         }
528 }