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