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