07bf4103ea8c4d4c4bf33f33703d1f8e1a235088
[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 $crypto_num= "util/libeay.num";
58 my $ssl_num=    "util/ssleay.num";
59
60 my $do_update = 0;
61 my $do_rewrite = 0;
62 my $do_crypto = 0;
63 my $do_ssl = 0;
64 my $do_ctest = 0;
65 my $do_ctestall = 0;
66 my $rsaref = 0;
67
68 my $VMS=0;
69 my $W32=0;
70 my $W16=0;
71 my $NT=0;
72 # Set this to make typesafe STACK definitions appear in DEF
73 my $safe_stack_def = 0;
74
75 my @known_platforms = ( "__FreeBSD__", "VMS", "WIN16", "WIN32",
76                         "WINNT", "PERL5", "NeXT" );
77 my @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
78                          "CAST", "MD2", "MD4", "MD5", "SHA", "RIPEMD",
79                          "MDC2", "RSA", "DSA", "DH", "HMAC", "FP_API",
80                          "RIJNDAEL" );
81
82 my $options="";
83 open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
84 while(<IN>) {
85     $options=$1 if (/^OPTIONS=(.*)$/);
86 }
87 close(IN);
88
89 # The following ciphers may be excluded (by Configure). This means functions
90 # defined with ifndef(NO_XXX) are not included in the .def file, and everything
91 # in directory xxx is ignored.
92 my $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
93 my $no_cast;
94 my $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
95 my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_rijndael;
96 my $no_fp_api;
97
98 foreach (@ARGV, split(/ /, $options))
99         {
100         $W32=1 if $_ eq "32";
101         $W16=1 if $_ eq "16";
102         if($_ eq "NT") {
103                 $W32 = 1;
104                 $NT = 1;
105         }
106         $VMS=1 if $_ eq "VMS";
107         #$rsaref=1 if $_ eq "rsaref";
108
109         $do_ssl=1 if $_ eq "ssleay";
110         $do_ssl=1 if $_ eq "ssl";
111         $do_crypto=1 if $_ eq "libeay";
112         $do_crypto=1 if $_ eq "crypto";
113         $do_update=1 if $_ eq "update";
114         $do_rewrite=1 if $_ eq "rewrite";
115         $do_ctest=1 if $_ eq "ctest";
116         $do_ctestall=1 if $_ eq "ctestall";
117         #$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
118
119         if    (/^no-rc2$/)      { $no_rc2=1; }
120         elsif (/^no-rc4$/)      { $no_rc4=1; }
121         elsif (/^no-rc5$/)      { $no_rc5=1; }
122         elsif (/^no-idea$/)     { $no_idea=1; }
123         elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
124         elsif (/^no-bf$/)       { $no_bf=1; }
125         elsif (/^no-cast$/)     { $no_cast=1; }
126         elsif (/^no-md2$/)      { $no_md2=1; }
127         elsif (/^no-md4$/)      { $no_md4=1; }
128         elsif (/^no-md5$/)      { $no_md5=1; }
129         elsif (/^no-sha$/)      { $no_sha=1; }
130         elsif (/^no-ripemd$/)   { $no_ripemd=1; }
131         elsif (/^no-mdc2$/)     { $no_mdc2=1; }
132         elsif (/^no-rsa$/)      { $no_rsa=1; }
133         elsif (/^no-dsa$/)      { $no_dsa=1; }
134         elsif (/^no-dh$/)       { $no_dh=1; }
135         elsif (/^no-hmac$/)     { $no_hmac=1; }
136         elsif (/^no-rijndael$/) { $no_rijndael=1; }
137         }
138
139
140 # If no platform is given, assume WIN32
141 if ($W32 + $W16 + $VMS == 0) {
142         $W32 = 1;
143 }
144
145 # Add extra knowledge
146 if ($W16) {
147         $no_fp_api=1;
148 }
149
150 if (!$do_ssl && !$do_crypto)
151         {
152         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT ]\n";
153         exit(1);
154         }
155
156 %ssl_list=&load_numbers($ssl_num);
157 $max_ssl = $max_num;
158 %crypto_list=&load_numbers($crypto_num);
159 $max_crypto = $max_num;
160
161 my $ssl="ssl/ssl.h";
162
163 my $crypto ="crypto/crypto.h";
164 $crypto.=" crypto/des/des.h" unless $no_des;
165 $crypto.=" crypto/idea/idea.h" unless $no_idea;
166 $crypto.=" crypto/rc4/rc4.h" unless $no_rc4;
167 $crypto.=" crypto/rc5/rc5.h" unless $no_rc5;
168 $crypto.=" crypto/rc2/rc2.h" unless $no_rc2;
169 $crypto.=" crypto/bf/blowfish.h" unless $no_bf;
170 $crypto.=" crypto/cast/cast.h" unless $no_cast;
171 $crypto.=" crypto/md2/md2.h" unless $no_md2;
172 $crypto.=" crypto/md4/md4.h" unless $no_md4;
173 $crypto.=" crypto/md5/md5.h" unless $no_md5;
174 $crypto.=" crypto/mdc2/mdc2.h" unless $no_mdc2;
175 $crypto.=" crypto/sha/sha.h" unless $no_sha;
176 $crypto.=" crypto/ripemd/ripemd.h" unless $no_ripemd;
177 $crypto.=" crypto/rijndael/rijndael.h" unless $no_rijndael;
178
179 $crypto.=" crypto/bn/bn.h";
180 $crypto.=" crypto/rsa/rsa.h" unless $no_rsa;
181 $crypto.=" crypto/dsa/dsa.h" unless $no_dsa;
182 $crypto.=" crypto/dh/dh.h" unless $no_dh;
183 $crypto.=" crypto/hmac/hmac.h" unless $no_hmac;
184
185 $crypto.=" crypto/engine/engine.h";
186 $crypto.=" crypto/stack/stack.h";
187 $crypto.=" crypto/buffer/buffer.h";
188 $crypto.=" crypto/bio/bio.h";
189 $crypto.=" crypto/dso/dso.h";
190 $crypto.=" crypto/lhash/lhash.h";
191 $crypto.=" crypto/conf/conf.h";
192 $crypto.=" crypto/txt_db/txt_db.h";
193
194 $crypto.=" crypto/evp/evp.h";
195 $crypto.=" crypto/objects/objects.h";
196 $crypto.=" crypto/pem/pem.h";
197 #$crypto.=" crypto/meth/meth.h";
198 $crypto.=" crypto/asn1/asn1.h";
199 $crypto.=" crypto/asn1/asn1_mac.h";
200 $crypto.=" crypto/err/err.h";
201 $crypto.=" crypto/pkcs7/pkcs7.h";
202 $crypto.=" crypto/pkcs12/pkcs12.h";
203 $crypto.=" crypto/x509/x509.h";
204 $crypto.=" crypto/x509/x509_vfy.h";
205 $crypto.=" crypto/x509v3/x509v3.h";
206 $crypto.=" crypto/rand/rand.h";
207 $crypto.=" crypto/comp/comp.h";
208 $crypto.=" crypto/ocsp/ocsp.h";
209 $crypto.=" crypto/tmdiff.h";
210
211 my $symhacks="crypto/symhacks.h";
212
213 my @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
214 my @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
215
216 if ($do_update) {
217
218 if ($do_ssl == 1) {
219
220         &maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
221         if ($do_rewrite == 1) {
222                 open(OUT, ">$ssl_num");
223                 &rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
224                 close OUT;
225         } else {
226                 open(OUT, ">>$ssl_num");
227         }
228         &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
229         close OUT;
230 }
231
232 if($do_crypto == 1) {
233
234         &maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
235         if ($do_rewrite == 1) {
236                 open(OUT, ">$crypto_num");
237                 &rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
238         } else {
239                 open(OUT, ">>$crypto_num");
240         }
241         &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
242         close OUT;
243
244
245 } elsif ($do_ctest || $do_ctestall) {
246
247         print <<"EOF";
248
249 /* Test file to check all DEF file symbols are present by trying
250  * to link to all of them. This is *not* intended to be run!
251  */
252
253 int main()
254 {
255 EOF
256         &print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
257                 if $do_ssl == 1;
258
259         &print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
260                 if $do_crypto == 1;
261
262         print "}\n";
263
264 } else {
265
266         &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_symbols)
267                 if $do_ssl == 1;
268
269         &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_symbols)
270                 if $do_crypto == 1;
271
272 }
273
274
275 sub do_defs
276 {
277         my($name,$files,$symhacksfile)=@_;
278         my $file;
279         my @ret;
280         my %syms;
281         my %platform;           # For anything undefined, we assume ""
282         my %kind;               # For anything undefined, we assume "FUNCTION"
283         my %algorithm;          # For anything undefined, we assume ""
284         my %rename;
285         my $cpp;
286
287         foreach $file (split(/\s+/,$symhacksfile." ".$files))
288                 {
289                 open(IN,"<$file") || die "unable to open $file:$!\n";
290                 my $line = "", my $def= "";
291                 my %tag = (
292                         (map { $_ => 0 } @known_platforms),
293                         (map { "NO_".$_ => 0 } @known_algorithms),
294                         NOPROTO         => 0,
295                         PERL5           => 0,
296                         _WINDLL         => 0,
297                         CONST_STRICT    => 0,
298                         TRUE            => 1,
299                 );
300                 my $symhacking = $file eq $symhacksfile;
301                 while(<IN>) {
302                         last if (/BEGIN ERROR CODES/);
303                         if ($line ne '') {
304                                 $_ = $line . $_;
305                                 $line = '';
306                         }
307
308                         if (/\\$/) {
309                                 $line = $_;
310                                 next;
311                         }
312
313                         $cpp = 1 if /^\#.*ifdef.*cplusplus/;
314                         if ($cpp) {
315                                 $cpp = 0 if /^\#.*endif/;
316                                 next;
317                         }
318
319                         s/\/\*.*?\*\///gs;                   # ignore comments
320                         s/{[^{}]*}//gs;                      # ignore {} blocks
321                         if (/^\#\s*ifndef (.*)/) {
322                                 push(@tag,$1);
323                                 $tag{$1}=-1;
324                         } elsif (/^\#\s*if !defined\(([^\)]+)\)/) {
325                                 push(@tag,$1);
326                                 $tag{$1}=-1;
327                         } elsif (/^\#\s*ifdef (.*)/) {
328                                 push(@tag,$1);
329                                 $tag{$1}=1;
330                         } elsif (/^\#\s*if defined\(([^\)]+)\)/) {
331                                 push(@tag,$1);
332                                 $tag{$1}=1;
333                         } elsif (/^\#\s*error\s+(\w+) is disabled\./) {
334                                 if ($tag[$#tag] eq "NO_".$1) {
335                                         $tag{$tag[$#tag]}=2;
336                                 }
337                         } elsif (/^\#\s*endif/) {
338                                 if ($tag{$tag[$#tag]}==2) {
339                                         $tag{$tag[$#tag]}=-1;
340                                 } else {
341                                         $tag{$tag[$#tag]}=0;
342                                 }
343                                 pop(@tag);
344                         } elsif (/^\#\s*else/) {
345                                 my $t=$tag[$#tag];
346                                 $tag{$t}= -$tag{$t};
347                         } elsif (/^\#\s*if\s+1/) {
348                                 # Dummy tag
349                                 push(@tag,"TRUE");
350                                 $tag{"TRUE"}=1;
351                         } elsif (/^\#\s*if\s+0/) {
352                                 # Dummy tag
353                                 push(@tag,"TRUE");
354                                 $tag{"TRUE"}=-1;
355                         } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
356                                  && $symhacking) {
357                                 my $s = $1;
358                                 my $a =
359                                     $2.":".join(",", grep(!/^$/,
360                                                           map { $tag{$_} == 1 ?
361                                                                     $_ : "" }
362                                                           @known_platforms));
363                                 $rename{$s} = $a;
364                         }
365                         if (/^\#/) {
366                                 my @p = grep(!/^$/,
367                                              map { $tag{$_} == 1 ? $_ :
368                                                        $tag{$_} == -1 ? "!".$_  : "" }
369                                              @known_platforms);
370                                 my @a = grep(!/^$/,
371                                              map { $tag{"NO_".$_} == -1 ? $_ : "" }
372                                              @known_algorithms);
373                                 $def .= "#INFO:".join(',',@p).":".join(',',@a).";";
374                                 next;
375                         }
376                         if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
377                                 next;
378                         } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
379                                 next;
380                         } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
381                                 next;
382                         } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
383                                  /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
384                                 # Things not in Win16
385                                 $syms{"PEM_read_${1}"} = 1;
386                                 $platform{"PEM_read_${1}"} = "!WIN16";
387                                 $syms{"PEM_write_${1}"} = 1;
388                                 $platform{"PEM_write_${1}"} = "!WIN16";
389                                 # Things that are everywhere
390                                 $syms{"PEM_read_bio_${1}"} = 1;
391                                 $syms{"PEM_write_bio_${1}"} = 1;
392                                 if ($1 eq "RSAPrivateKey" ||
393                                     $1 eq "RSAPublicKey" ||
394                                     $1 eq "RSA_PUBKEY") {
395                                         $algorithm{"PEM_read_${1}"} = "RSA";
396                                         $algorithm{"PEM_write_${1}"} = "RSA";
397                                         $algorithm{"PEM_read_bio_${1}"} = "RSA";
398                                         $algorithm{"PEM_write_bio_${1}"} = "RSA";
399                                 }
400                                 elsif ($1 eq "DSAPrivateKey" ||
401                                        $1 eq "DSAparams" ||
402                                        $1 eq "RSA_PUBKEY") {
403                                         $algorithm{"PEM_read_${1}"} = "DSA";
404                                         $algorithm{"PEM_write_${1}"} = "DSA";
405                                         $algorithm{"PEM_read_bio_${1}"} = "DSA";
406                                         $algorithm{"PEM_write_bio_${1}"} = "DSA";
407                                 }
408                                 elsif ($1 eq "DHparams") {
409                                         $algorithm{"PEM_read_${1}"} = "DH";
410                                         $algorithm{"PEM_write_${1}"} = "DH";
411                                         $algorithm{"PEM_read_bio_${1}"} = "DH";
412                                         $algorithm{"PEM_write_bio_${1}"} = "DH";
413                                 }
414                         } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
415                                      /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
416                                 # Things not in Win16
417                                 $syms{"PEM_write_${1}"} = 1;
418                                 $platform{"PEM_write_${1}"} .= ",!WIN16";
419                                 # Things that are everywhere
420                                 $syms{"PEM_write_bio_${1}"} = 1;
421                                 if ($1 eq "RSAPrivateKey" ||
422                                     $1 eq "RSAPublicKey" ||
423                                     $1 eq "RSA_PUBKEY") {
424                                         $algorithm{"PEM_write_${1}"} = "RSA";
425                                         $algorithm{"PEM_write_bio_${1}"} = "RSA";
426                                 }
427                                 elsif ($1 eq "DSAPrivateKey" ||
428                                        $1 eq "DSAparams" ||
429                                        $1 eq "RSA_PUBKEY") {
430                                         $algorithm{"PEM_write_${1}"} = "DSA";
431                                         $algorithm{"PEM_write_bio_${1}"} = "DSA";
432                                 }
433                                 elsif ($1 eq "DHparams") {
434                                         $algorithm{"PEM_write_${1}"} = "DH";
435                                         $algorithm{"PEM_write_bio_${1}"} = "DH";
436                                 }
437                         } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
438                                      /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
439                                 # Things not in Win16
440                                 $syms{"PEM_read_${1}"} = 1;
441                                 $platform{"PEM_read_${1}"} .= ",!WIN16";
442                                 # Things that are everywhere
443                                 $syms{"PEM_read_bio_${1}"} = 1;
444                         } elsif (
445                                 ($tag{'TRUE'} != -1)
446                                 && ($tag{'CONST_STRICT'} != 1)
447                                  )
448                                 {
449                                         if (/\{|\/\*|\([^\)]*$/) {
450                                                 $line = $_;
451                                         } else {
452                                                 $def .= $_;
453                                         }
454                                 }
455                         }
456                 close(IN);
457
458                 my $algs;
459                 my $plays;
460
461                 foreach (split /;/, $def) {
462                         my $s; my $k = "FUNCTION"; my $p; my $a;
463                         s/^[\n\s]*//g;
464                         s/[\n\s]*$//g;
465                         next if(/\#undef/);
466                         next if(/typedef\W/);
467                         next if(/\#define/);
468
469                         if (/^\#INFO:([^:]*):(.*)$/) {
470                                 $plats = $1;
471                                 $algs = $2;
472                                 next;
473                         } elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+)(\[[0-9]*\])*\s*$/) {
474                                 $s = $1;
475                                 $k = "VARIABLE";
476                         } elsif (/\(\*(\w*)\([^\)]+/) {
477                                 $s = $1;
478                         } elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
479                                 # K&R C
480                                 next;
481                         } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
482                                 while (not /\(\)$/s) {
483                                         s/[^\(\)]*\)$/\)/s;
484                                         s/\([^\(\)]*\)\)$/\)/s;
485                                 }
486                                 s/\(void\)//;
487                                 /(\w+)\W*\(\)/s;
488                                 $s = $1;
489                         } elsif (/\(/ and not (/=/)) {
490                                 print STDERR "File $file: cannot parse: $_;\n";
491                                 next;
492                         } else {
493                                 next;
494                         }
495
496                         $syms{$s} = 1;
497                         $kind{$s} = $k;
498
499                         $p = $plats;
500                         $a = $algs;
501                         $a .= ",BF" if($s =~ /EVP_bf/);
502                         $a .= ",CAST" if($s =~ /EVP_cast/);
503                         $a .= ",DES" if($s =~ /EVP_des/);
504                         $a .= ",DSA" if($s =~ /EVP_dss/);
505                         $a .= ",IDEA" if($s =~ /EVP_idea/);
506                         $a .= ",MD2" if($s =~ /EVP_md2/);
507                         $a .= ",MD4" if($s =~ /EVP_md4/);
508                         $a .= ",MD5" if($s =~ /EVP_md5/);
509                         $a .= ",RC2" if($s =~ /EVP_rc2/);
510                         $a .= ",RC4" if($s =~ /EVP_rc4/);
511                         $a .= ",RC5" if($s =~ /EVP_rc5/);
512                         $a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
513                         $a .= ",SHA" if($s =~ /EVP_sha/);
514                         $a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
515                         $a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
516                         $a .= ",RSA" if($s =~ /RSAPrivateKey/);
517                         $a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
518
519                         $platform{$s} .= ','.$p;
520                         $algorithm{$s} .= ','.$a;
521
522                         if (defined($rename{$s})) {
523                                 (my $r, my $p) = split(/:/,$rename{$s});
524                                 my @ip = map { /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p;
525                                 $syms{$r} = 1;
526                                 $kind{$r} = $kind{$s}."(".$s.")";
527                                 $algorithm{$r} = $algorithm{$s};
528                                 $platform{$r} = $platform{$s}.",".$p;
529                                 $platform{$s} .= ','.join(',', @ip).','.join(',', @ip);
530                         }
531                 }
532         }
533
534         # Prune the returned symbols
535
536         $platform{"crypt"} .= ",!PERL5,!__FreeBSD__,!NeXT";
537
538         delete $syms{"SSL_add_dir_cert_subjects_to_stack"};
539         delete $syms{"bn_dump1"};
540
541         $platform{"BIO_s_file_internal"} .= ",WIN16";
542         $platform{"BIO_new_file_internal"} .= ",WIN16";
543         $platform{"BIO_new_fp_internal"} .= ",WIN16";
544
545         $platform{"BIO_s_file"} .= ",!WIN16";
546         $platform{"BIO_new_file"} .= ",!WIN16";
547         $platform{"BIO_new_fp"} .= ",!WIN16";
548
549         $platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
550
551         if(exists $syms{"ERR_load_CRYPTO_strings"}) {
552                 $platform{"ERR_load_CRYPTO_strings"} .= ",!VMS,!WIN16";
553                 $syms{"ERR_load_CRYPTOlib_strings"} = 1;
554                 $platform{"ERR_load_CRYPTOlib_strings"} .= ",VMS,WIN16";
555         }
556
557         # Info we know about
558
559         $platform{"RSA_PKCS1_RSAref"} = "RSAREF";
560         $algorithm{"RSA_PKCS1_RSAref"} = "RSA";
561
562         push @ret, map { $_."\\".&info_string($_,"EXIST",
563                                               $platform{$_},
564                                               $kind{$_},
565                                               $algorithm{$_}) } keys %syms;
566
567         return(@ret);
568 }
569
570 sub info_string {
571         (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
572
573         my %a = defined($algorithms) ?
574             map { $_ => 1 } split /,/, $algorithms : ();
575         my $pl = defined($platforms) ? $platforms : "";
576         my %p = map { $_ => 0 } split /,/, $pl;
577         my $k = defined($kind) ? $kind : "FUNCTION";
578         my $ret;
579
580         # We do this, because if there's code like the following, it really
581         # means the function exists in all cases and should therefore be
582         # everywhere.  By increasing and decreasing, we may attain 0:
583         #
584         # ifndef WIN16
585         #    int foo();
586         # else
587         #    int _fat foo();
588         # endif
589         foreach $platform (split /,/, $pl) {
590                 if ($platform =~ /^!(.*)$/) {
591                         $p{$1}--;
592                 } else {
593                         $p{$platform}++;
594                 }
595         }
596         foreach $platform (keys %p) {
597                 if ($p{$platform} == 0) { delete $p{$platform}; }
598         }
599
600         delete $p{""};
601         delete $a{""};
602
603         $ret = $exist;
604         $ret .= ":".join(',',map { $p{$_} < 0 ? "!".$_ : $_ } keys %p);
605         $ret .= ":".$k;
606         $ret .= ":".join(',',keys %a);
607         return $ret;
608 }
609
610 sub maybe_add_info {
611         (my $name, *nums, my @symbols) = @_;
612         my $sym;
613         my $new_info = 0;
614         my %syms=();
615
616         print STDERR "Updating $name info\n";
617         foreach $sym (@symbols) {
618                 (my $s, my $i) = split /\\/, $sym;
619                 $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
620                 if (defined($nums{$s})) {
621                         (my $n, my $dummy) = split /\\/, $nums{$s};
622                         if (!defined($dummy) || $i ne $dummy) {
623                                 $nums{$s} = $n."\\".$i;
624                                 $new_info++;
625                                 #print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n";
626                         }
627                 }
628                 $syms{sym} = 1;
629         }
630
631         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
632         foreach $sym (@s) {
633                 (my $n, my $i) = split /\\/, $nums{$sym};
634                 if (!defined($syms{sym})) {
635                         $new_info++;
636                         #print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n";
637                 }
638         }
639         if ($new_info) {
640                 print STDERR "$new_info old symbols got an info update\n";
641                 if (!$do_rewrite) {
642                         print STDERR "You should do a rewrite to fix this.\n";
643                 }
644         } else {
645                 print STDERR "No old symbols needed info update\n";
646         }
647 }
648
649 sub print_test_file
650 {
651         (*OUT,my $name,*nums,my @symbols)=@_;
652         my $n = 1; my @e; my @r;
653         my $sym; my $prev = ""; my $prefSSLeay;
654
655         (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
656         (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
657         @symbols=((sort @e),(sort @r));
658
659         foreach $sym (@symbols) {
660                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
661                 if ($s ne $prev) {
662                         if (!defined($nums{$sym})) {
663                                 printf STDERR "Warning: $sym does not have a number assigned\n"
664                                                 if(!$do_update);
665                         } else {
666                                 $n=$nums{$s};
667                                 print OUT "\t$s();\n";
668                         }
669                 }
670                 $prev = $s;     # To avoid duplicates...
671         }
672 }
673
674 sub print_def_file
675 {
676         (*OUT,my $name,*nums,my @symbols)=@_;
677         my $n = 1; my @e; my @r;
678
679         if ($W32)
680                 { $name.="32"; }
681         else
682                 { $name.="16"; }
683
684         print OUT <<"EOF";
685 ;
686 ; Definition file for the DLL version of the $name library from OpenSSL
687 ;
688
689 LIBRARY         $name
690
691 DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
692
693 EOF
694
695         if (!$W32) {
696                 print <<"EOF";
697 CODE            PRELOAD MOVEABLE
698 DATA            PRELOAD MOVEABLE SINGLE
699
700 EXETYPE         WINDOWS
701
702 HEAPSIZE        4096
703 STACKSIZE       8192
704
705 EOF
706         }
707
708         print "EXPORTS\n";
709
710         (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
711         (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
712         @symbols=((sort @e),(sort @r));
713
714
715         foreach $sym (@symbols) {
716                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
717                 if (!defined($nums{$s})) {
718                         printf STDERR "Warning: $s does not have a number assigned\n"
719                                         if(!$do_update);
720                 } else {
721                         (my $n, my $i) = split /\\/, $nums{$s};
722                         my %pf = ();
723                         my @p = split(/,/, ($i =~ /^[^:]*:([^:]*):/,$1));
724                         my @a = split(/,/, ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1));
725                         # @p_purged must contain hardware platforms only
726                         my @p_purged = ();
727                         foreach $ptmp (@p) {
728                                 next if $ptmp =~ /^!?RSAREF$/;
729                                 push @p_purged, $ptmp;
730                         }
731                         my $negatives = !!grep(/^!/,@p);
732                         # It is very important to check NT before W32
733                         if ((($NT && (!@p_purged
734                                       || (!$negatives && grep(/^WINNT$/,@p))
735                                       || ($negatives && !grep(/^!WINNT$/,@p))))
736                              || ($W32 && (!@p_purged
737                                           || (!$negatives && grep(/^WIN32$/,@p))
738                                           || ($negatives && !grep(/^!WIN32$/,@p))))
739                              || ($W16 && (!@p_purged
740                                           || (!$negatives && grep(/^WIN16$/,@p))
741                                           || ($negatives && !grep(/^!WIN16$/,@p)))))
742                             && (!@p
743                                 || (!$negatives
744                                     && ($rsaref || !grep(/^RSAREF$/,@p)))
745                                 || ($negatives
746                                     && (!$rsaref || !grep(/^!RSAREF$/,@p))))
747                             && (!@a || (!$no_rc2 || !grep(/^RC2$/,@a)))
748                             && (!@a || (!$no_rc4 || !grep(/^RC4$/,@a)))
749                             && (!@a || (!$no_rc5 || !grep(/^RC5$/,@a)))
750                             && (!@a || (!$no_idea || !grep(/^IDEA$/,@a)))
751                             && (!@a || (!$no_des || !grep(/^DES$/,@a)))
752                             && (!@a || (!$no_bf || !grep(/^BF$/,@a)))
753                             && (!@a || (!$no_cast || !grep(/^CAST$/,@a)))
754                             && (!@a || (!$no_md2 || !grep(/^MD2$/,@a)))
755                             && (!@a || (!$no_md4 || !grep(/^MD4$/,@a)))
756                             && (!@a || (!$no_md5 || !grep(/^MD5$/,@a)))
757                             && (!@a || (!$no_sha || !grep(/^SHA$/,@a)))
758                             && (!@a || (!$no_ripemd || !grep(/^RIPEMD$/,@a)))
759                             && (!@a || (!$no_mdc2 || !grep(/^MDC2$/,@a)))
760                             && (!@a || (!$no_rsa || !grep(/^RSA$/,@a)))
761                             && (!@a || (!$no_dsa || !grep(/^DSA$/,@a)))
762                             && (!@a || (!$no_dh || !grep(/^DH$/,@a)))
763                             && (!@a || (!$no_hmac || !grep(/^HMAC$/,@a)))
764                             && (!@a || (!$no_rijndael || !grep(/^RIJNDAEL$/,@a)))
765                             && (!@a || (!$no_fp_api || !grep(/^FP_API$/,@a)))
766                             ) {
767                                 printf OUT "    %s%-40s@%d\n",($W32)?"":"_",$s,$n;
768 #                       } else {
769 #                               print STDERR "DEBUG: \"$sym\" (@p):",
770 #                               " rsaref:", !!(!@p
771 #                                              || (!$negatives
772 #                                                  && ($rsaref || !grep(/^RSAREF$/,@p)))
773 #                                              || ($negatives
774 #                                                  && (!$rsaref || !grep(/^!RSAREF$/,@p))))?1:0,
775 #                               " 16:", !!($W16 && (!@p_purged
776 #                                                   || (!$negatives && grep(/^WIN16$/,@p))
777 #                                                   || ($negatives && !grep(/^!WIN16$/,@p)))),
778 #                               " 32:", !!($W32 && (!@p_purged
779 #                                                   || (!$negatives && grep(/^WIN32$/,@p))
780 #                                                   || ($negatives && !grep(/^!WIN32$/,@p)))),
781 #                               " NT:", !!($NT && (!@p_purged
782 #                                                  || (!$negatives && grep(/^WINNT$/,@p))
783 #                                                  || ($negatives && !grep(/^!WINNT$/,@p)))),
784 #                               "\n";
785                         }
786                 }
787         }
788         printf OUT "\n";
789 }
790
791 sub load_numbers
792 {
793         my($name)=@_;
794         my(@a,%ret);
795
796         $max_num = 0;
797         $num_noinfo = 0;
798         $prev = "";
799
800         open(IN,"<$name") || die "unable to open $name:$!\n";
801         while (<IN>) {
802                 chop;
803                 s/#.*$//;
804                 next if /^\s*$/;
805                 @a=split;
806                 if (defined $ret{$a[0]}) {
807                         print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
808                 }
809                 if ($max_num > $a[1]) {
810                         print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
811                 }
812                 if ($max_num == $a[1]) {
813                         # This is actually perfectly OK
814                         #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
815                 }
816                 if ($#a < 2) {
817                         # Existence will be proven later, in do_defs
818                         $ret{$a[0]}=$a[1];
819                         $num_noinfo++;
820                 } else {
821                         $ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
822                 }
823                 $max_num = $a[1] if $a[1] > $max_num;
824                 $prev=$a[0];
825         }
826         if ($num_noinfo) {
827                 print STDERR "Warning: $num_noinfo symbols were without info.";
828                 if ($do_rewrite) {
829                         printf STDERR "  The rewrite will fix this.\n";
830                 } else {
831                         printf STDERR "  You should do a rewrite to fix this.\n";
832                 }
833         }
834         close(IN);
835         return(%ret);
836 }
837
838 sub parse_number
839 {
840         (my $str, my $what) = @_;
841         (my $n, my $i) = split(/\\/,$str);
842         if ($what eq "n") {
843                 return $n;
844         } else {
845                 return $i;
846         }
847 }
848
849 sub rewrite_numbers
850 {
851         (*OUT,$name,*nums,@symbols)=@_;
852         my $thing;
853
854         print STDERR "Rewriting $name\n";
855
856         my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
857         my $r; my %r; my %rsyms;
858         foreach $r (@r) {
859                 (my $s, my $i) = split /\\/, $r;
860                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
861                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
862                 $r{$a} = $s."\\".$i;
863                 $rsyms{$s} = 1;
864         }
865
866         my %syms = ();
867         foreach $_ (@symbols) {
868                 (my $n, my $i) = split /\\/;
869                 $syms{$n} = 1;
870         }
871
872         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
873         foreach $sym (@s) {
874                 (my $n, my $i) = split /\\/, $nums{$sym};
875                 next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
876                 next if defined($rsyms{$sym});
877                 $i="NOEXIST::FUNCTION:"
878                         if !defined($i) || $i eq "" || !defined($syms{$sym});
879                 printf OUT "%s%-40s%d\t%s\n","",$sym,$n,$i;
880                 if (exists $r{$sym}) {
881                         (my $s, $i) = split /\\/,$r{$sym};
882                         printf OUT "%s%-40s%d\t%s\n","",$s,$n,$i;
883                 }
884         }
885 }
886
887 sub update_numbers
888 {
889         (*OUT,$name,*nums,my $start_num, my @symbols)=@_;
890         my $new_syms = 0;
891
892         print STDERR "Updating $name numbers\n";
893
894         my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
895         my $r; my %r; my %rsyms;
896         foreach $r (@r) {
897                 (my $s, my $i) = split /\\/, $r;
898                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
899                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
900                 $r{$a} = $s."\\".$i;
901                 $rsyms{$s} = 1;
902         }
903
904         foreach $sym (@symbols) {
905                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
906                 next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
907                 next if defined($rsyms{$sym});
908                 die "ERROR: Symbol $sym had no info attached to it."
909                     if $i eq "";
910                 if (!exists $nums{$s}) {
911                         $new_syms++;
912                         printf OUT "%s%-40s%d\t%s\n","",$s, ++$start_num,$i;
913                         if (exists $r{$s}) {
914                                 ($s, $i) = split /\\/,$r{$s};
915                                 printf OUT "%s%-40s%d\t%s\n","",$s, $start_num,$i;
916                         }
917                 }
918         }
919         if($new_syms) {
920                 print STDERR "$new_syms New symbols added\n";
921         } else {
922                 print STDERR "No New symbols Added\n";
923         }
924 }
925
926 sub check_existing
927 {
928         (*nums, my @symbols)=@_;
929         my %existing; my @remaining;
930         @remaining=();
931         foreach $sym (@symbols) {
932                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
933                 $existing{$s}=1;
934         }
935         foreach $sym (keys %nums) {
936                 if (!exists $existing{$sym}) {
937                         push @remaining, $sym;
938                 }
939         }
940         if(@remaining) {
941                 print STDERR "The following symbols do not seem to exist:\n";
942                 foreach $sym (@remaining) {
943                         print STDERR "\t",$sym,"\n";
944                 }
945         }
946 }
947