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