make depend.
[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 # Intermediary files are created, call libeay.num and ssleay.num,...
9 # Previously, they had the following format:
10 #
11 #       routine-name    nnnn
12 #
13 # But that isn't enough for a number of reasons, the first on being that
14 # this format is (needlessly) very Win32-centric, and even then...
15 # One of the biggest problems is that there's no information about what
16 # routines should actually be used, which varies with what crypto algorithms
17 # are disabled.  Also, some operating systems (for example VMS with VAX C)
18 # need to keep track of the global variables as well as the functions.
19 #
20 # So, a remake of this script is done so as to include information on the
21 # kind of symbol it is (function or variable) and what algorithms they're
22 # part of.  This will allow easy translating to .def files or the corresponding
23 # file in other operating systems (a .opt file for VMS, possibly with a .mar
24 # file).
25 #
26 # The format now becomes:
27 #
28 #       routine-name    nnnn    info
29 #
30 # and the "info" part is actually a colon-separated string of fields with
31 # the following meaning:
32 #
33 #       existence:platform:kind:algorithms
34 #
35 # - "existence" can be "EXIST" or "NOEXIST" depending on if the symbol is
36 #   found somewhere in the source, 
37 # - "platforms" is empty if it exists on all platforms, otherwise it contains
38 #   comma-separated list of the platform, just as they are if the symbol exists
39 #   for those platforms, or prepended with a "!" if not.  This helps resolve
40 #   symbol name replacements for platforms where the names are too long for the
41 #   compiler or linker, or if the systems is case insensitive and there is a
42 #   clash.  This script assumes those redefinitions are place in the file
43 #   crypto/symhacks.h.
44 #   The semantics for the platforms list is a bit complicated.  The rule of
45 #   thumb is that the list is exclusive, but it seems to mean different things.
46 #   So, if the list is all negatives (like "!VMS,!WIN16"), the symbol exists
47 #   on all platforms except those listed.  If the list is all positives (like
48 #   "VMS,WIN16"), the symbol exists only on those platforms and nowhere else.
49 #   The combination of positives and negatives will act as if the positives
50 #   weren't there.
51 # - "kind" is "FUNCTION" or "VARIABLE".  The meaning of that is obvious.
52 # - "algorithms" is a comma-separated list of algorithm names.  This helps
53 #   exclude symbols that are part of an algorithm that some user wants to
54 #   exclude.
55 #
56
57 my $debug=0;
58
59 my $crypto_num= "util/libeay.num";
60 my $ssl_num=    "util/ssleay.num";
61
62 my $do_update = 0;
63 my $do_rewrite = 0;
64 my $do_crypto = 0;
65 my $do_ssl = 0;
66 my $do_ctest = 0;
67 my $do_ctestall = 0;
68 my $do_checkexist = 0;
69
70 my $VMS=0;
71 my $W32=0;
72 my $W16=0;
73 my $NT=0;
74 # Set this to make typesafe STACK definitions appear in DEF
75 my $safe_stack_def = 0;
76
77 my @known_platforms = ( "__FreeBSD__", "PERL5", "NeXT" );
78 my @known_ossl_platforms = ( "VMS", "WIN16", "WIN32", "WINNT" );
79 my @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
80                          "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1",
81                          "RIPEMD",
82                          "MDC2", "RSA", "DSA", "DH", "HMAC", "AES",
83                          # Envelope "algorithms"
84                          "EVP", "X509", "ASN1_TYPEDEFS",
85                          # Helper "algorithms"
86                          "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
87                          "LOCKING",
88                          # External "algorithms"
89                          "FP_API", "STDIO", "SOCK", "KRB5" );
90
91 my $options="";
92 open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
93 while(<IN>) {
94     $options=$1 if (/^OPTIONS=(.*)$/);
95 }
96 close(IN);
97
98 # The following ciphers may be excluded (by Configure). This means functions
99 # defined with ifndef(NO_XXX) are not included in the .def file, and everything
100 # in directory xxx is ignored.
101 my $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
102 my $no_cast;
103 my $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
104 my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_aes; my $no_krb5;
105 my $no_fp_api;
106
107 foreach (@ARGV, split(/ /, $options))
108         {
109         $W32=1 if $_ eq "32";
110         $W16=1 if $_ eq "16";
111         if($_ eq "NT") {
112                 $W32 = 1;
113                 $NT = 1;
114         }
115         $VMS=1 if $_ eq "VMS";
116
117         $do_ssl=1 if $_ eq "ssleay";
118         $do_ssl=1 if $_ eq "ssl";
119         $do_crypto=1 if $_ eq "libeay";
120         $do_crypto=1 if $_ eq "crypto";
121         $do_update=1 if $_ eq "update";
122         $do_rewrite=1 if $_ eq "rewrite";
123         $do_ctest=1 if $_ eq "ctest";
124         $do_ctestall=1 if $_ eq "ctestall";
125         $do_checkexist=1 if $_ eq "exist";
126         #$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
127
128         if    (/^no-rc2$/)      { $no_rc2=1; }
129         elsif (/^no-rc4$/)      { $no_rc4=1; }
130         elsif (/^no-rc5$/)      { $no_rc5=1; }
131         elsif (/^no-idea$/)     { $no_idea=1; }
132         elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
133         elsif (/^no-bf$/)       { $no_bf=1; }
134         elsif (/^no-cast$/)     { $no_cast=1; }
135         elsif (/^no-md2$/)      { $no_md2=1; }
136         elsif (/^no-md4$/)      { $no_md4=1; }
137         elsif (/^no-md5$/)      { $no_md5=1; }
138         elsif (/^no-sha$/)      { $no_sha=1; }
139         elsif (/^no-ripemd$/)   { $no_ripemd=1; }
140         elsif (/^no-mdc2$/)     { $no_mdc2=1; }
141         elsif (/^no-rsa$/)      { $no_rsa=1; }
142         elsif (/^no-dsa$/)      { $no_dsa=1; }
143         elsif (/^no-dh$/)       { $no_dh=1; }
144         elsif (/^no-hmac$/)     { $no_hmac=1; }
145         elsif (/^no-aes$/)      { $no_aes=1; }
146         elsif (/^no-evp$/)      { $no_evp=1; }
147         elsif (/^no-lhash$/)    { $no_lhash=1; }
148         elsif (/^no-stack$/)    { $no_stack=1; }
149         elsif (/^no-err$/)      { $no_err=1; }
150         elsif (/^no-buffer$/)   { $no_buffer=1; }
151         elsif (/^no-bio$/)      { $no_bio=1; }
152         #elsif (/^no-locking$/) { $no_locking=1; }
153         elsif (/^no-comp$/)     { $no_comp=1; }
154         elsif (/^no-dso$/)      { $no_dso=1; }
155         elsif (/^no-krb5$/)     { $no_krb5=1; }
156         }
157
158
159 # If no platform is given, assume WIN32
160 if ($W32 + $W16 + $VMS == 0) {
161         $W32 = 1;
162 }
163
164 # Add extra knowledge
165 if ($W16) {
166         $no_fp_api=1;
167 }
168
169 if (!$do_ssl && !$do_crypto)
170         {
171         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT ]\n";
172         exit(1);
173         }
174
175 %ssl_list=&load_numbers($ssl_num);
176 $max_ssl = $max_num;
177 %crypto_list=&load_numbers($crypto_num);
178 $max_crypto = $max_num;
179
180 my $ssl="ssl/ssl.h";
181 $ssl.=" ssl/kssl.h";
182
183 my $crypto ="crypto/crypto.h";
184 $crypto.=" crypto/des/des.h" unless $no_des;
185 $crypto.=" crypto/idea/idea.h" unless $no_idea;
186 $crypto.=" crypto/rc4/rc4.h" unless $no_rc4;
187 $crypto.=" crypto/rc5/rc5.h" unless $no_rc5;
188 $crypto.=" crypto/rc2/rc2.h" unless $no_rc2;
189 $crypto.=" crypto/bf/blowfish.h" unless $no_bf;
190 $crypto.=" crypto/cast/cast.h" unless $no_cast;
191 $crypto.=" crypto/md2/md2.h" unless $no_md2;
192 $crypto.=" crypto/md4/md4.h" unless $no_md4;
193 $crypto.=" crypto/md5/md5.h" unless $no_md5;
194 $crypto.=" crypto/mdc2/mdc2.h" unless $no_mdc2;
195 $crypto.=" crypto/sha/sha.h" unless $no_sha;
196 $crypto.=" crypto/ripemd/ripemd.h" unless $no_ripemd;
197 $crypto.=" crypto/rijndael/rijndael.h" unless $no_aes;
198 $crypto.=" crypto/rijndael/rd_fst.h" unless $no_aes;
199
200 $crypto.=" crypto/bn/bn.h";
201 $crypto.=" crypto/rsa/rsa.h" unless $no_rsa;
202 $crypto.=" crypto/dsa/dsa.h" unless $no_dsa;
203 $crypto.=" crypto/dh/dh.h" unless $no_dh;
204 $crypto.=" crypto/hmac/hmac.h" unless $no_hmac;
205
206 $crypto.=" crypto/engine/engine.h";
207 $crypto.=" crypto/stack/stack.h" unless $no_stack;
208 $crypto.=" crypto/buffer/buffer.h" unless $no_buffer;
209 $crypto.=" crypto/bio/bio.h" unless $no_bio;
210 $crypto.=" crypto/dso/dso.h" unless $no_dso;
211 $crypto.=" crypto/lhash/lhash.h" unless $no_lhash;
212 $crypto.=" crypto/conf/conf.h";
213 $crypto.=" crypto/txt_db/txt_db.h";
214
215 $crypto.=" crypto/evp/evp.h" unless $no_evp;
216 $crypto.=" crypto/objects/objects.h";
217 $crypto.=" crypto/pem/pem.h";
218 #$crypto.=" crypto/meth/meth.h";
219 $crypto.=" crypto/asn1/asn1.h";
220 $crypto.=" crypto/asn1/asn1t.h";
221 $crypto.=" crypto/asn1/asn1_mac.h";
222 $crypto.=" crypto/err/err.h" unless $no_err;
223 $crypto.=" crypto/pkcs7/pkcs7.h";
224 $crypto.=" crypto/pkcs12/pkcs12.h";
225 $crypto.=" crypto/x509/x509.h";
226 $crypto.=" crypto/x509/x509_vfy.h";
227 $crypto.=" crypto/x509v3/x509v3.h";
228 $crypto.=" crypto/rand/rand.h";
229 $crypto.=" crypto/comp/comp.h" unless $no_comp;
230 $crypto.=" crypto/ocsp/ocsp.h";
231 $crypto.=" crypto/tmdiff.h";
232
233 my $symhacks="crypto/symhacks.h";
234
235 my @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
236 my @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
237
238 if ($do_update) {
239
240 if ($do_ssl == 1) {
241
242         &maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
243         if ($do_rewrite == 1) {
244                 open(OUT, ">$ssl_num");
245                 &rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
246                 close OUT;
247         } else {
248                 open(OUT, ">>$ssl_num");
249         }
250         &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
251         close OUT;
252 }
253
254 if($do_crypto == 1) {
255
256         &maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
257         if ($do_rewrite == 1) {
258                 open(OUT, ">$crypto_num");
259                 &rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
260         } else {
261                 open(OUT, ">>$crypto_num");
262         }
263         &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
264         close OUT;
265
266
267 } elsif ($do_checkexist) {
268         &check_existing(*ssl_list, @ssl_symbols)
269                 if $do_ssl == 1;
270         &check_existing(*crypto_list, @crypto_symbols)
271                 if $do_crypto == 1;
272 } elsif ($do_ctest || $do_ctestall) {
273
274         print <<"EOF";
275
276 /* Test file to check all DEF file symbols are present by trying
277  * to link to all of them. This is *not* intended to be run!
278  */
279
280 int main()
281 {
282 EOF
283         &print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
284                 if $do_ssl == 1;
285
286         &print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
287                 if $do_crypto == 1;
288
289         print "}\n";
290
291 } else {
292
293         &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_symbols)
294                 if $do_ssl == 1;
295
296         &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_symbols)
297                 if $do_crypto == 1;
298
299 }
300
301
302 sub do_defs
303 {
304         my($name,$files,$symhacksfile)=@_;
305         my $file;
306         my @ret;
307         my %syms;
308         my %platform;           # For anything undefined, we assume ""
309         my %kind;               # For anything undefined, we assume "FUNCTION"
310         my %algorithm;          # For anything undefined, we assume ""
311         my %rename;
312         my $cpp;
313         my %unknown_algorithms = ();
314
315         foreach $file (split(/\s+/,$symhacksfile." ".$files))
316                 {
317                 open(IN,"<$file") || die "unable to open $file:$!\n";
318                 my $line = "", my $def= "";
319                 my %tag = (
320                         (map { $_ => 0 } @known_platforms),
321                         (map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
322                         (map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
323                         NOPROTO         => 0,
324                         PERL5           => 0,
325                         _WINDLL         => 0,
326                         CONST_STRICT    => 0,
327                         TRUE            => 1,
328                 );
329                 my $symhacking = $file eq $symhacksfile;
330                 my @current_platforms = ();
331                 my @current_algorithms = ();
332
333                 while(<IN>) {
334                         last if (/BEGIN ERROR CODES/);
335                         if ($line ne '') {
336                                 $_ = $line . $_;
337                                 $line = '';
338                         }
339
340                         if (/\\$/) {
341                                 chomp; # remove eol
342                                 chop; # remove ending backslash
343                                 $line = $_;
344                                 next;
345                         }
346
347                         $cpp = 1 if /^\#.*ifdef.*cplusplus/;
348                         if ($cpp) {
349                                 $cpp = 0 if /^\#.*endif/;
350                                 next;
351                         }
352
353                         s/\/\*.*?\*\///gs;                   # ignore comments
354                         s/{[^{}]*}//gs;                      # ignore {} blocks
355                         print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
356                         if (/^\#\s*ifndef\s+(.*)/) {
357                                 push(@tag,"-");
358                                 push(@tag,$1);
359                                 $tag{$1}=-1;
360                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
361                         } elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
362                                 push(@tag,"-");
363                                 if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) {
364                                         my $tmp_1 = $1;
365                                         my $tmp_;
366                                         foreach $tmp_ (split '\&\&',$tmp_1) {
367                                                 $tmp_ =~ /!defined\(([^\)]+)\)/;
368                                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
369                                                 push(@tag,$1);
370                                                 $tag{$1}=-1;
371                                         }
372                                 } else {
373                                         print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O...
374                                         print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
375                                         push(@tag,$1);
376                                         $tag{$1}=-1;
377                                 }
378                         } elsif (/^\#\s*ifdef\s+(.*)/) {
379                                 push(@tag,"-");
380                                 push(@tag,$1);
381                                 $tag{$1}=1;
382                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
383                         } elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
384                                 push(@tag,"-");
385                                 if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) {
386                                         my $tmp_1 = $1;
387                                         my $tmp_;
388                                         foreach $tmp_ (split '\|\|',$tmp_1) {
389                                                 $tmp_ =~ /defined\(([^\)]+)\)/;
390                                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
391                                                 push(@tag,$1);
392                                                 $tag{$1}=1;
393                                         }
394                                 } else {
395                                         print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O...
396                                         print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
397                                         push(@tag,$1);
398                                         $tag{$1}=1;
399                                 }
400                         } elsif (/^\#\s*error\s+(\w+) is disabled\./) {
401                                 my $tag_i = $#tag;
402                                 while($tag[$tag_i] ne "-") {
403                                         if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
404                                                 $tag{$tag[$tag_i]}=2;
405                                                 print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug;
406                                         }
407                                         $tag_i--;
408                                 }
409                         } elsif (/^\#\s*endif/) {
410                                 my $tag_i = $#tag;
411                                 while($tag[$tag_i] ne "-") {
412                                         my $t=$tag[$tag_i];
413                                         print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
414                                         if ($tag{$t}==2) {
415                                                 $tag{$t}=-1;
416                                         } else {
417                                                 $tag{$t}=0;
418                                         }
419                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
420                                         pop(@tag);
421                                         if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
422                                                 $t=$1;
423                                         } else {
424                                                 $t="";
425                                         }
426                                         if ($t ne ""
427                                             && !grep(/^$t$/, @known_algorithms)) {
428                                                 $unknown_algorithms{$t} = 1;
429                                                 #print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
430                                         }
431                                         $tag_i--;
432                                 }
433                                 pop(@tag);
434                         } elsif (/^\#\s*else/) {
435                                 my $tag_i = $#tag;
436                                 while($tag[$tag_i] ne "-") {
437                                         my $t=$tag[$tag_i];
438                                         $tag{$t}= -$tag{$t};
439                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
440                                         $tag_i--;
441                                 }
442                         } elsif (/^\#\s*if\s+1/) {
443                                 push(@tag,"-");
444                                 # Dummy tag
445                                 push(@tag,"TRUE");
446                                 $tag{"TRUE"}=1;
447                                 print STDERR "DEBUG: $file: found 1\n" if $debug;
448                         } elsif (/^\#\s*if\s+0/) {
449                                 push(@tag,"-");
450                                 # Dummy tag
451                                 push(@tag,"TRUE");
452                                 $tag{"TRUE"}=-1;
453                                 print STDERR "DEBUG: $file: found 0\n" if $debug;
454                         } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
455                                  && $symhacking && $tag{'TRUE'} != -1) {
456                                 my $s = $1;
457                                 my $a = $2;
458                                 my $a1 = join(",",
459                                               grep(!/^$/,
460                                                    map { $tag{$_} == 1 ?
461                                                              $_ : "" }
462                                                    @known_platforms));
463                                 my $a2 = join(",",
464                                               grep(!/^$/,
465                                                    map { $tag{"OPENSSL_SYS_".$_} == 1 ?
466                                                              $_ : "" }
467                                                    @known_ossl_platforms));
468                                 if ($a1 eq "") { $a1 = $a2; }
469                                 elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
470                                 $rename{$s} = $a.":".$a1;
471                                 print STDERR "DEBUG: $file: defined $s = $a\n" if $debug;
472                         }
473                         if (/^\#/) {
474                                 @current_platforms =
475                                     grep(!/^$/,
476                                          map { $tag{$_} == 1 ? $_ :
477                                                    $tag{$_} == -1 ? "!".$_  : "" }
478                                          @known_platforms);
479                                 push @current_platforms
480                                     , grep(!/^$/,
481                                            map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
482                                                      $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
483                                            @known_ossl_platforms);
484                                 @current_algorithms =
485                                     grep(!/^$/,
486                                          map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
487                                          @known_algorithms);
488                                 $def .=
489                                     "#INFO:"
490                                         .join(',',@current_platforms).":"
491                                             .join(',',@current_algorithms).";";
492                                 next;
493                         }
494                         if ($tag{'TRUE'} != -1) {
495                                 if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
496                                         next;
497                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
498                                         $def .= "int d2i_$3(void);";
499                                         $def .= "int i2d_$3(void);";
500                                         $def .= "OPENSSL_EXTERN int $2_it;";
501                                         next;
502                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
503                                         $def .= "int d2i_$3(void);";
504                                         $def .= "int i2d_$3(void);";
505                                         $def .= "int $3_free(void);";
506                                         $def .= "int $3_new(void);";
507                                         $def .= "OPENSSL_EXTERN int $2_it;";
508                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
509                                          /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
510                                         $def .= "int d2i_$1(void);";
511                                         $def .= "int i2d_$1(void);";
512                                         $def .= "int $1_free(void);";
513                                         $def .= "int $1_new(void);";
514                                         $def .= "OPENSSL_EXTERN int $1_it;";
515                                         next;
516                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
517                                         $def .= "int d2i_$2(void);";
518                                         $def .= "int i2d_$2(void);";
519                                         $def .= "OPENSSL_EXTERN int $2_it;";
520                                         next;
521                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
522                                         $def .= "int d2i_$2(void);";
523                                         $def .= "int i2d_$2(void);";
524                                         $def .= "int $2_free(void);";
525                                         $def .= "int $2_new(void);";
526                                         $def .= "OPENSSL_EXTERN int $2_it;";
527                                         next;
528                                 } elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*,(\w*)\s*\)/) {
529                                         $def .= "OPENSSL_EXTERN int $1_it;";
530                                         next;
531                                 } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
532                                         next;
533                                 } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
534                                         next;
535                                 } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
536                                          /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
537                                         # Things not in Win16
538                                         $def .=
539                                             "#INFO:"
540                                                 .join(',',"!WIN16",@current_platforms).":"
541                                                     .join(',',@current_algorithms).";";
542                                         $def .= "int PEM_read_$1(void);";
543                                         $def .= "int PEM_write_$1(void);";
544                                         $def .=
545                                             "#INFO:"
546                                                 .join(',',@current_platforms).":"
547                                                     .join(',',@current_algorithms).";";
548                                         # Things that are everywhere
549                                         $def .= "int PEM_read_bio_$1(void);";
550                                         $def .= "int PEM_write_bio_$1(void);";
551                                         next;
552                                 } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
553                                          /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
554                                         # Things not in Win16
555                                         $def .=
556                                             "#INFO:"
557                                                 .join(',',"!WIN16",@current_platforms).":"
558                                                     .join(',',@current_algorithms).";";
559                                         $def .= "int PEM_write_$1(void);";
560                                         $def .=
561                                             "#INFO:"
562                                                 .join(',',@current_platforms).":"
563                                                     .join(',',@current_algorithms).";";
564                                         # Things that are everywhere
565                                         $def .= "int PEM_write_bio_$1(void);";
566                                         next;
567                                 } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
568                                          /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
569                                         # Things not in Win16
570                                         $def .=
571                                             "#INFO:"
572                                                 .join(',',"!WIN16",@current_platforms).":"
573                                                     .join(',',@current_algorithms).";";
574                                         $def .= "int PEM_read_$1(void);";
575                                         $def .=
576                                             "#INFO:"
577                                                 .join(',',@current_platforms).":"
578                                                     .join(',',@current_algorithms).";";
579                                         # Things that are everywhere
580                                         $def .= "int PEM_read_bio_$1(void);";
581                                         next;
582                                 } elsif ($tag{'CONST_STRICT'} != 1) {
583                                         if (/\{|\/\*|\([^\)]*$/) {
584                                                 $line = $_;
585                                         } else {
586                                                 $def .= $_;
587                                         }
588                                 }
589                         }
590                 }
591                 close(IN);
592
593                 my $algs;
594                 my $plays;
595
596                 foreach (split /;/, $def) {
597                         my $s; my $k = "FUNCTION"; my $p; my $a;
598                         s/^[\n\s]*//g;
599                         s/[\n\s]*$//g;
600                         next if(/\#undef/);
601                         next if(/typedef\W/);
602                         next if(/\#define/);
603
604                         if (/^\#INFO:([^:]*):(.*)$/) {
605                                 $plats = $1;
606                                 $algs = $2;
607                                 next;
608                         } elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+)(\[[0-9]*\])*\s*$/) {
609                                 $s = $1;
610                                 $k = "VARIABLE";
611                         } elsif (/\(\*(\w*)\([^\)]+/) {
612                                 $s = $1;
613                         } elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
614                                 # K&R C
615                                 next;
616                         } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
617                                 while (not /\(\)$/s) {
618                                         s/[^\(\)]*\)$/\)/s;
619                                         s/\([^\(\)]*\)\)$/\)/s;
620                                 }
621                                 s/\(void\)//;
622                                 /(\w+)\W*\(\)/s;
623                                 $s = $1;
624                         } elsif (/\(/ and not (/=/)) {
625                                 print STDERR "File $file: cannot parse: $_;\n";
626                                 next;
627                         } else {
628                                 next;
629                         }
630
631                         $syms{$s} = 1;
632                         $kind{$s} = $k;
633
634                         $p = $plats;
635                         $a = $algs;
636                         $a .= ",BF" if($s =~ /EVP_bf/);
637                         $a .= ",CAST" if($s =~ /EVP_cast/);
638                         $a .= ",DES" if($s =~ /EVP_des/);
639                         $a .= ",DSA" if($s =~ /EVP_dss/);
640                         $a .= ",IDEA" if($s =~ /EVP_idea/);
641                         $a .= ",MD2" if($s =~ /EVP_md2/);
642                         $a .= ",MD4" if($s =~ /EVP_md4/);
643                         $a .= ",MD5" if($s =~ /EVP_md5/);
644                         $a .= ",RC2" if($s =~ /EVP_rc2/);
645                         $a .= ",RC4" if($s =~ /EVP_rc4/);
646                         $a .= ",RC5" if($s =~ /EVP_rc5/);
647                         $a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
648                         $a .= ",SHA" if($s =~ /EVP_sha/);
649                         $a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
650                         $a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
651                         $a .= ",RSA" if($s =~ /RSAPrivateKey/);
652                         $a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
653
654                         $platform{$s} .= ','.$p;
655                         $algorithm{$s} .= ','.$a;
656
657                         if (defined($rename{$s})) {
658                                 (my $r, my $p) = split(/:/,$rename{$s});
659                                 my @ip = map { /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p;
660                                 $syms{$r} = 1;
661                                 $kind{$r} = $kind{$s}."(".$s.")";
662                                 $algorithm{$r} = $algorithm{$s};
663                                 $platform{$r} = $platform{$s}.",".$p;
664                                 $platform{$s} .= ','.join(',', @ip).','.join(',', @ip);
665                         }
666                 }
667         }
668
669         # Prune the returned symbols
670
671         delete $syms{"bn_dump1"};
672         $platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
673
674         $platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
675         $platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
676         $platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
677         $platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
678
679         # Info we know about
680
681         push @ret, map { $_."\\".&info_string($_,"EXIST",
682                                               $platform{$_},
683                                               $kind{$_},
684                                               $algorithm{$_}) } keys %syms;
685
686         if (keys %unknown_algorithms) {
687                 print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
688                 print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
689         }
690         return(@ret);
691 }
692
693 sub info_string {
694         (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
695
696         my %a = defined($algorithms) ?
697             map { $_ => 1 } split /,/, $algorithms : ();
698         my $pl = defined($platforms) ? $platforms : "";
699         my %p = map { $_ => 0 } split /,/, $pl;
700         my $k = defined($kind) ? $kind : "FUNCTION";
701         my $ret;
702
703         # We do this, because if there's code like the following, it really
704         # means the function exists in all cases and should therefore be
705         # everywhere.  By increasing and decreasing, we may attain 0:
706         #
707         # ifndef WIN16
708         #    int foo();
709         # else
710         #    int _fat foo();
711         # endif
712         foreach $platform (split /,/, $pl) {
713                 if ($platform =~ /^!(.*)$/) {
714                         $p{$1}--;
715                 } else {
716                         $p{$platform}++;
717                 }
718         }
719         foreach $platform (keys %p) {
720                 if ($p{$platform} == 0) { delete $p{$platform}; }
721         }
722
723         delete $p{""};
724         delete $a{""};
725
726         $ret = $exist;
727         $ret .= ":".join(',',map { $p{$_} < 0 ? "!".$_ : $_ } keys %p);
728         $ret .= ":".$k;
729         $ret .= ":".join(',',keys %a);
730         return $ret;
731 }
732
733 sub maybe_add_info {
734         (my $name, *nums, my @symbols) = @_;
735         my $sym;
736         my $new_info = 0;
737         my %syms=();
738
739         print STDERR "Updating $name info\n";
740         foreach $sym (@symbols) {
741                 (my $s, my $i) = split /\\/, $sym;
742                 $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
743                 if (defined($nums{$s})) {
744                         (my $n, my $dummy) = split /\\/, $nums{$s};
745                         if (!defined($dummy) || $i ne $dummy) {
746                                 $nums{$s} = $n."\\".$i;
747                                 $new_info++;
748                                 print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
749                         }
750                 }
751                 $syms{sym} = 1;
752         }
753
754         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
755         foreach $sym (@s) {
756                 (my $n, my $i) = split /\\/, $nums{$sym};
757                 if (!defined($syms{sym})) {
758                         $new_info++;
759                         print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n if $debug";
760                 }
761         }
762         if ($new_info) {
763                 print STDERR "$new_info old symbols got an info update\n";
764                 if (!$do_rewrite) {
765                         print STDERR "You should do a rewrite to fix this.\n";
766                 }
767         } else {
768                 print STDERR "No old symbols needed info update\n";
769         }
770 }
771
772 sub print_test_file
773 {
774         (*OUT,my $name,*nums,my @symbols)=@_;
775         my $n = 1; my @e; my @r;
776         my $sym; my $prev = ""; my $prefSSLeay;
777
778         (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
779         (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
780         @symbols=((sort @e),(sort @r));
781
782         foreach $sym (@symbols) {
783                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
784                 if ($s ne $prev) {
785                         if (!defined($nums{$s})) {
786                                 print STDERR "Warning: $s does not have a number assigned\n"
787                                                 if(!$do_update);
788                         } else {
789                                 $n=$nums{$s};
790                                 print OUT "\t$s();\n";
791                         }
792                 }
793                 $prev = $s;     # To avoid duplicates...
794         }
795 }
796
797 sub print_def_file
798 {
799         (*OUT,my $name,*nums,my @symbols)=@_;
800         my $n = 1; my @e; my @r; my @v;
801
802         if ($W32)
803                 { $name.="32"; }
804         else
805                 { $name.="16"; }
806
807         print OUT <<"EOF";
808 ;
809 ; Definition file for the DLL version of the $name library from OpenSSL
810 ;
811
812 LIBRARY         $name
813
814 DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
815
816 EOF
817
818         if (!$W32) {
819                 print <<"EOF";
820 CODE            PRELOAD MOVEABLE
821 DATA            PRELOAD MOVEABLE SINGLE
822
823 EXETYPE         WINDOWS
824
825 HEAPSIZE        4096
826 STACKSIZE       8192
827
828 EOF
829         }
830
831         print "EXPORTS\n";
832
833         (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
834         (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
835         (@v)=grep(/^\w+\\.*?:.*?:VARIABLE/,@symbols);
836         @symbols=((sort @e),(sort @r), (sort @v));
837
838
839         foreach $sym (@symbols) {
840                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
841                 my $v = 0;
842                 $v = 1 if $sym=~ /^\w+\\.*?:.*?:VARIABLE/;
843                 if (!defined($nums{$s})) {
844                         printf STDERR "Warning: $s does not have a number assigned\n"
845                                         if(!$do_update);
846                 } else {
847                         (my $n, my $i) = split /\\/, $nums{$s};
848                         my %pf = ();
849                         my @p = split(/,/, ($i =~ /^[^:]*:([^:]*):/,$1));
850                         my @a = split(/,/, ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1));
851                         # @p_purged must contain hardware platforms only
852                         my @p_purged = ();
853                         foreach $ptmp (@p) {
854                                 push @p_purged, $ptmp;
855                         }
856                         my $negatives = !!grep(/^!/,@p);
857                         # It is very important to check NT before W32
858                         if ((($NT && (!@p_purged
859                                       || (!$negatives && grep(/^WINNT$/,@p))
860                                       || ($negatives && !grep(/^!WINNT$/,@p))))
861                              || ($W32 && (!@p_purged
862                                           || (!$negatives && grep(/^WIN32$/,@p))
863                                           || ($negatives && !grep(/^!WIN32$/,@p))))
864                              || ($W16 && (!@p_purged
865                                           || (!$negatives && grep(/^WIN16$/,@p))
866                                           || ($negatives && !grep(/^!WIN16$/,@p)))))
867                             && (!@a || (!$no_rc2 || !grep(/^RC2$/,@a)))
868                             && (!@a || (!$no_rc4 || !grep(/^RC4$/,@a)))
869                             && (!@a || (!$no_rc5 || !grep(/^RC5$/,@a)))
870                             && (!@a || (!$no_idea || !grep(/^IDEA$/,@a)))
871                             && (!@a || (!$no_des || !grep(/^DES$/,@a)))
872                             && (!@a || (!$no_bf || !grep(/^BF$/,@a)))
873                             && (!@a || (!$no_cast || !grep(/^CAST$/,@a)))
874                             && (!@a || (!$no_md2 || !grep(/^MD2$/,@a)))
875                             && (!@a || (!$no_md4 || !grep(/^MD4$/,@a)))
876                             && (!@a || (!$no_md5 || !grep(/^MD5$/,@a)))
877                             && (!@a || (!$no_sha || !grep(/^SHA$/,@a)))
878                             && (!@a || (!$no_ripemd || !grep(/^RIPEMD$/,@a)))
879                             && (!@a || (!$no_mdc2 || !grep(/^MDC2$/,@a)))
880                             && (!@a || (!$no_rsa || !grep(/^RSA$/,@a)))
881                             && (!@a || (!$no_dsa || !grep(/^DSA$/,@a)))
882                             && (!@a || (!$no_dh || !grep(/^DH$/,@a)))
883                             && (!@a || (!$no_hmac || !grep(/^HMAC$/,@a)))
884                             && (!@a || (!$no_aes || !grep(/^AES$/,@a)))
885                             && (!@a || (!$no_krb5 || !grep(/^KRB5$/,@a)))
886                             && (!@a || (!$no_fp_api || !grep(/^FP_API$/,@a)))
887                             ) {
888                                 if($v) {
889                                         printf OUT "    %s%-40s@%-8d DATA\n",($W32)?"":"_",$s,$n;
890                                 } else {
891                                         printf OUT "    %s%-40s@%d\n",($W32)?"":"_",$s,$n;
892                                 }
893                         }
894 #                       print STDERR "DEBUG: \"$sym\" (@p):",
895 #                       " negatives:", $negatives,
896 #                       " 16:", !!($W16 && (!@p_purged
897 #                                           || (!$negatives && grep(/^WIN16$/,@p))
898 #                                           || ($negatives && !grep(/^!WIN16$/,@p)))),
899 #                       " 32:", !!($W32 && (!@p_purged
900 #                                           || (!$negatives && grep(/^WIN32$/,@p))
901 #                                           || ($negatives && !grep(/^!WIN32$/,@p)))),
902 #                       " NT:", !!($NT && (!@p_purged
903 #                                          || (!$negatives && grep(/^WINNT$/,@p))
904 #                                          || ($negatives && !grep(/^!WINNT$/,@p)))),
905 #                       "\n";
906                 }
907         }
908         printf OUT "\n";
909 }
910
911 sub load_numbers
912 {
913         my($name)=@_;
914         my(@a,%ret);
915
916         $max_num = 0;
917         $num_noinfo = 0;
918         $prev = "";
919
920         open(IN,"<$name") || die "unable to open $name:$!\n";
921         while (<IN>) {
922                 chop;
923                 s/#.*$//;
924                 next if /^\s*$/;
925                 @a=split;
926                 if (defined $ret{$a[0]}) {
927                         print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
928                 }
929                 if ($max_num > $a[1]) {
930                         print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
931                 }
932                 if ($max_num == $a[1]) {
933                         # This is actually perfectly OK
934                         #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
935                 }
936                 if ($#a < 2) {
937                         # Existence will be proven later, in do_defs
938                         $ret{$a[0]}=$a[1];
939                         $num_noinfo++;
940                 } else {
941                         $ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
942                 }
943                 $max_num = $a[1] if $a[1] > $max_num;
944                 $prev=$a[0];
945         }
946         if ($num_noinfo) {
947                 print STDERR "Warning: $num_noinfo symbols were without info.";
948                 if ($do_rewrite) {
949                         printf STDERR "  The rewrite will fix this.\n";
950                 } else {
951                         printf STDERR "  You should do a rewrite to fix this.\n";
952                 }
953         }
954         close(IN);
955         return(%ret);
956 }
957
958 sub parse_number
959 {
960         (my $str, my $what) = @_;
961         (my $n, my $i) = split(/\\/,$str);
962         if ($what eq "n") {
963                 return $n;
964         } else {
965                 return $i;
966         }
967 }
968
969 sub rewrite_numbers
970 {
971         (*OUT,$name,*nums,@symbols)=@_;
972         my $thing;
973
974         print STDERR "Rewriting $name\n";
975
976         my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
977         my $r; my %r; my %rsyms;
978         foreach $r (@r) {
979                 (my $s, my $i) = split /\\/, $r;
980                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
981                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
982                 $r{$a} = $s."\\".$i;
983                 $rsyms{$s} = 1;
984         }
985
986         my %syms = ();
987         foreach $_ (@symbols) {
988                 (my $n, my $i) = split /\\/;
989                 $syms{$n} = 1;
990         }
991
992         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
993         foreach $sym (@s) {
994                 (my $n, my $i) = split /\\/, $nums{$sym};
995                 print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
996                 next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
997                 next if defined($rsyms{$sym});
998                 $i="NOEXIST::FUNCTION:"
999                         if !defined($i) || $i eq "" || !defined($syms{$sym});
1000                 printf OUT "%s%-40s%d\t%s\n","",$sym,$n,$i;
1001                 if (exists $r{$sym}) {
1002                         (my $s, $i) = split /\\/,$r{$sym};
1003                         printf OUT "%s%-40s%d\t%s\n","",$s,$n,$i;
1004                 }
1005         }
1006 }
1007
1008 sub update_numbers
1009 {
1010         (*OUT,$name,*nums,my $start_num, my @symbols)=@_;
1011         my $new_syms = 0;
1012
1013         print STDERR "Updating $name numbers\n";
1014
1015         my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
1016         my $r; my %r; my %rsyms;
1017         foreach $r (@r) {
1018                 (my $s, my $i) = split /\\/, $r;
1019                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1020                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1021                 $r{$a} = $s."\\".$i;
1022                 $rsyms{$s} = 1;
1023         }
1024
1025         foreach $sym (@symbols) {
1026                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1027                 next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
1028                 next if defined($rsyms{$sym});
1029                 die "ERROR: Symbol $sym had no info attached to it."
1030                     if $i eq "";
1031                 if (!exists $nums{$s}) {
1032                         $new_syms++;
1033                         printf OUT "%s%-40s%d\t%s\n","",$s, ++$start_num,$i;
1034                         if (exists $r{$s}) {
1035                                 ($s, $i) = split /\\/,$r{$s};
1036                                 printf OUT "%s%-40s%d\t%s\n","",$s, $start_num,$i;
1037                         }
1038                 }
1039         }
1040         if($new_syms) {
1041                 print STDERR "$new_syms New symbols added\n";
1042         } else {
1043                 print STDERR "No New symbols Added\n";
1044         }
1045 }
1046
1047 sub check_existing
1048 {
1049         (*nums, my @symbols)=@_;
1050         my %existing; my @remaining;
1051         @remaining=();
1052         foreach $sym (@symbols) {
1053                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1054                 $existing{$s}=1;
1055         }
1056         foreach $sym (keys %nums) {
1057                 if (!exists $existing{$sym}) {
1058                         push @remaining, $sym;
1059                 }
1060         }
1061         if(@remaining) {
1062                 print STDERR "The following symbols do not seem to exist:\n";
1063                 foreach $sym (@remaining) {
1064                         print STDERR "\t",$sym,"\n";
1065                 }
1066         }
1067 }
1068