29cbe7dbc02e433c5ed0fdf0a773bb7c1e2803f4
[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 variants 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, or the symbol is implemented differently (see
43 #   EXPORT_VAR_AS_FUNCTION).  This script assumes renaming of symbols is found
44 #   in the file crypto/symhacks.h.
45 #   The semantics for the platforms is that every item is checked against the
46 #   environment.  For the negative items ("!FOO"), if any of them is false
47 #   (i.e. "FOO" is true) in the environment, the corresponding symbol can't be
48 #   used.  For the positive itms, if all of them are false in the environment,
49 #   the corresponding symbol can't be used.  Any combination of positive and
50 #   negative items are possible, and of course leave room for some redundancy.
51 # - "kind" is "FUNCTION" or "VARIABLE".  The meaning of that is obvious.
52 # - "algorithms" is a comma-separated list of algorithm names.  This helps
53 #   exclude symbols that are part of an algorithm that some user wants to
54 #   exclude.
55 #
56
57 my $debug=0;
58
59 my $crypto_num= "util/libeay.num";
60 my $ssl_num=    "util/ssleay.num";
61 my $libname;
62
63 my $do_update = 0;
64 my $do_rewrite = 1;
65 my $do_crypto = 0;
66 my $do_ssl = 0;
67 my $do_ctest = 0;
68 my $do_ctestall = 0;
69 my $do_checkexist = 0;
70
71 my $VMSVAX=0;
72 my $VMSNonVAX=0;
73 my $VMS=0;
74 my $W32=0;
75 my $W16=0;
76 my $NT=0;
77 my $OS2=0;
78 # Set this to make typesafe STACK definitions appear in DEF
79 my $safe_stack_def = 0;
80
81 my @known_platforms = ( "__FreeBSD__", "PERL5", "NeXT",
82                         "EXPORT_VAR_AS_FUNCTION", "ZLIB" );
83 my @known_ossl_platforms = ( "VMS", "WIN16", "WIN32", "WINNT", "OS2" );
84 my @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
85                          "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1",
86                          "SHA256", "SHA512", "RIPEMD",
87                          "MDC2", "WHIRLPOOL", "RSA", "DSA", "DH", "EC", "ECDH", "ECDSA",
88                          "HMAC", "AES", "CAMELLIA", "SEED", "GOST",
89                          # Envelope "algorithms"
90                          "EVP", "X509", "ASN1_TYPEDEFS",
91                          # Helper "algorithms"
92                          "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
93                          "LOCKING",
94                          # External "algorithms"
95                          "FP_API", "STDIO", "SOCK", "KRB5", "DGRAM",
96                          # Engines
97                          "STATIC_ENGINE", "ENGINE", "HW", "GMP",
98                          # RFC3779
99                          "RFC3779",
100                          # TLS
101                          "TLSEXT", "PSK", "SRP",
102                          # CMS
103                          "CMS",
104                          # CryptoAPI Engine
105                          "CAPIENG",
106                          # SSL v2
107                          "SSL2",
108                          # JPAKE
109                          "JPAKE",
110                          # Deprecated functions
111                          "DEPRECATED" );
112
113 my $options="";
114 open(IN,"<Makefile") || die "unable to open Makefile!\n";
115 while(<IN>) {
116     $options=$1 if (/^OPTIONS=(.*)$/);
117 }
118 close(IN);
119
120 # The following ciphers may be excluded (by Configure). This means functions
121 # defined with ifndef(NO_XXX) are not included in the .def file, and everything
122 # in directory xxx is ignored.
123 my $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
124 my $no_cast; my $no_whirlpool; my $no_camellia; my $no_seed;
125 my $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
126 my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_aes; my $no_krb5;
127 my $no_ec; my $no_ecdsa; my $no_ecdh; my $no_engine; my $no_hw;
128 my $no_fp_api; my $no_static_engine=1; my $no_gmp; my $no_deprecated;
129 my $no_rfc3779; my $no_psk; my $no_tlsext; my $no_cms; my $no_capieng;
130 my $no_jpake; my $no_srp; my $no_ssl2;
131
132 my $zlib;
133
134
135 foreach (@ARGV, split(/ /, $options))
136         {
137         $debug=1 if $_ eq "debug";
138         $W32=1 if $_ eq "32";
139         $W16=1 if $_ eq "16";
140         if($_ eq "NT") {
141                 $W32 = 1;
142                 $NT = 1;
143         }
144         if ($_ eq "VMS-VAX") {
145                 $VMS=1;
146                 $VMSVAX=1;
147         }
148         if ($_ eq "VMS-NonVAX") {
149                 $VMS=1;
150                 $VMSNonVAX=1;
151         }
152         $VMS=1 if $_ eq "VMS";
153         $OS2=1 if $_ eq "OS2";
154         if ($_ eq "zlib" || $_ eq "enable-zlib" || $_ eq "zlib-dynamic"
155                          || $_ eq "enable-zlib-dynamic") {
156                 $zlib = 1;
157         }
158
159         $do_ssl=1 if $_ eq "ssleay";
160         if ($_ eq "ssl") {
161                 $do_ssl=1; 
162                 $libname=$_
163         }
164         $do_crypto=1 if $_ eq "libeay";
165         if ($_ eq "crypto") {
166                 $do_crypto=1;
167                 $libname=$_;
168         }
169         $no_static_engine=1 if $_ eq "no-static-engine";
170         $no_static_engine=0 if $_ eq "enable-static-engine";
171         $do_update=1 if $_ eq "update";
172         $do_rewrite=1 if $_ eq "rewrite";
173         $do_ctest=1 if $_ eq "ctest";
174         $do_ctestall=1 if $_ eq "ctestall";
175         $do_checkexist=1 if $_ eq "exist";
176         #$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
177
178         if    (/^no-rc2$/)      { $no_rc2=1; }
179         elsif (/^no-rc4$/)      { $no_rc4=1; }
180         elsif (/^no-rc5$/)      { $no_rc5=1; }
181         elsif (/^no-idea$/)     { $no_idea=1; }
182         elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
183         elsif (/^no-bf$/)       { $no_bf=1; }
184         elsif (/^no-cast$/)     { $no_cast=1; }
185         elsif (/^no-whirlpool$/)     { $no_whirlpool=1; }
186         elsif (/^no-md2$/)      { $no_md2=1; }
187         elsif (/^no-md4$/)      { $no_md4=1; }
188         elsif (/^no-md5$/)      { $no_md5=1; }
189         elsif (/^no-sha$/)      { $no_sha=1; }
190         elsif (/^no-ripemd$/)   { $no_ripemd=1; }
191         elsif (/^no-mdc2$/)     { $no_mdc2=1; }
192         elsif (/^no-rsa$/)      { $no_rsa=1; }
193         elsif (/^no-dsa$/)      { $no_dsa=1; }
194         elsif (/^no-dh$/)       { $no_dh=1; }
195         elsif (/^no-ec$/)       { $no_ec=1; }
196         elsif (/^no-ecdsa$/)    { $no_ecdsa=1; }
197         elsif (/^no-ecdh$/)     { $no_ecdh=1; }
198         elsif (/^no-hmac$/)     { $no_hmac=1; }
199         elsif (/^no-aes$/)      { $no_aes=1; }
200         elsif (/^no-camellia$/) { $no_camellia=1; }
201         elsif (/^no-seed$/)     { $no_seed=1; }
202         elsif (/^no-evp$/)      { $no_evp=1; }
203         elsif (/^no-lhash$/)    { $no_lhash=1; }
204         elsif (/^no-stack$/)    { $no_stack=1; }
205         elsif (/^no-err$/)      { $no_err=1; }
206         elsif (/^no-buffer$/)   { $no_buffer=1; }
207         elsif (/^no-bio$/)      { $no_bio=1; }
208         #elsif (/^no-locking$/) { $no_locking=1; }
209         elsif (/^no-comp$/)     { $no_comp=1; }
210         elsif (/^no-dso$/)      { $no_dso=1; }
211         elsif (/^no-krb5$/)     { $no_krb5=1; }
212         elsif (/^no-engine$/)   { $no_engine=1; }
213         elsif (/^no-hw$/)       { $no_hw=1; }
214         elsif (/^no-gmp$/)      { $no_gmp=1; }
215         elsif (/^no-rfc3779$/)  { $no_rfc3779=1; }
216         elsif (/^no-tlsext$/)   { $no_tlsext=1; }
217         elsif (/^no-cms$/)      { $no_cms=1; }
218         elsif (/^no-ssl2$/)     { $no_ssl2=1; }
219         elsif (/^no-capieng$/)  { $no_capieng=1; }
220         elsif (/^no-jpake$/)    { $no_jpake=1; }
221         elsif (/^no-srp$/)      { $no_srp=1; }
222         }
223
224
225 if (!$libname) { 
226         if ($do_ssl) {
227                 $libname="SSLEAY";
228         }
229         if ($do_crypto) {
230                 $libname="LIBEAY";
231         }
232 }
233
234 # If no platform is given, assume WIN32
235 if ($W32 + $W16 + $VMS + $OS2 == 0) {
236         $W32 = 1;
237 }
238
239 # Add extra knowledge
240 if ($W16) {
241         $no_fp_api=1;
242 }
243
244 if (!$do_ssl && !$do_crypto)
245         {
246         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 ]\n";
247         exit(1);
248         }
249
250 %ssl_list=&load_numbers($ssl_num);
251 $max_ssl = $max_num;
252 %crypto_list=&load_numbers($crypto_num);
253 $max_crypto = $max_num;
254
255 my $ssl="ssl/ssl.h";
256 $ssl.=" ssl/kssl.h";
257 $ssl.=" ssl/tls1.h";
258
259 my $crypto ="crypto/crypto.h";
260 $crypto.=" crypto/o_dir.h";
261 $crypto.=" crypto/des/des.h crypto/des/des_old.h" ; # unless $no_des;
262 $crypto.=" crypto/idea/idea.h" ; # unless $no_idea;
263 $crypto.=" crypto/rc4/rc4.h" ; # unless $no_rc4;
264 $crypto.=" crypto/rc5/rc5.h" ; # unless $no_rc5;
265 $crypto.=" crypto/rc2/rc2.h" ; # unless $no_rc2;
266 $crypto.=" crypto/bf/blowfish.h" ; # unless $no_bf;
267 $crypto.=" crypto/cast/cast.h" ; # unless $no_cast;
268 $crypto.=" crypto/whrlpool/whrlpool.h" ;
269 $crypto.=" crypto/md2/md2.h" ; # unless $no_md2;
270 $crypto.=" crypto/md4/md4.h" ; # unless $no_md4;
271 $crypto.=" crypto/md5/md5.h" ; # unless $no_md5;
272 $crypto.=" crypto/mdc2/mdc2.h" ; # unless $no_mdc2;
273 $crypto.=" crypto/sha/sha.h" ; # unless $no_sha;
274 $crypto.=" crypto/ripemd/ripemd.h" ; # unless $no_ripemd;
275 $crypto.=" crypto/aes/aes.h" ; # unless $no_aes;
276 $crypto.=" crypto/camellia/camellia.h" ; # unless $no_camellia;
277 $crypto.=" crypto/seed/seed.h"; # unless $no_seed;
278
279 $crypto.=" crypto/bn/bn.h";
280 $crypto.=" crypto/rsa/rsa.h" ; # unless $no_rsa;
281 $crypto.=" crypto/dsa/dsa.h" ; # unless $no_dsa;
282 $crypto.=" crypto/dh/dh.h" ; # unless $no_dh;
283 $crypto.=" crypto/ec/ec.h" ; # unless $no_ec;
284 $crypto.=" crypto/ecdsa/ecdsa.h" ; # unless $no_ecdsa;
285 $crypto.=" crypto/ecdh/ecdh.h" ; # unless $no_ecdh;
286 $crypto.=" crypto/hmac/hmac.h" ; # unless $no_hmac;
287
288 $crypto.=" crypto/engine/engine.h"; # unless $no_engine;
289 $crypto.=" crypto/stack/stack.h" ; # unless $no_stack;
290 $crypto.=" crypto/buffer/buffer.h" ; # unless $no_buffer;
291 $crypto.=" crypto/bio/bio.h" ; # unless $no_bio;
292 $crypto.=" crypto/dso/dso.h" ; # unless $no_dso;
293 $crypto.=" crypto/lhash/lhash.h" ; # unless $no_lhash;
294 $crypto.=" crypto/conf/conf.h";
295 $crypto.=" crypto/txt_db/txt_db.h";
296
297 $crypto.=" crypto/evp/evp.h" ; # unless $no_evp;
298 $crypto.=" crypto/objects/objects.h";
299 $crypto.=" crypto/pem/pem.h";
300 #$crypto.=" crypto/meth/meth.h";
301 $crypto.=" crypto/asn1/asn1.h";
302 $crypto.=" crypto/asn1/asn1t.h";
303 $crypto.=" crypto/asn1/asn1_mac.h";
304 $crypto.=" crypto/err/err.h" ; # unless $no_err;
305 $crypto.=" crypto/pkcs7/pkcs7.h";
306 $crypto.=" crypto/pkcs12/pkcs12.h";
307 $crypto.=" crypto/x509/x509.h";
308 $crypto.=" crypto/x509/x509_vfy.h";
309 $crypto.=" crypto/x509v3/x509v3.h";
310 $crypto.=" crypto/ts/ts.h";
311 $crypto.=" crypto/rand/rand.h";
312 $crypto.=" crypto/comp/comp.h" ; # unless $no_comp;
313 $crypto.=" crypto/ocsp/ocsp.h";
314 $crypto.=" crypto/ui/ui.h crypto/ui/ui_compat.h";
315 $crypto.=" crypto/krb5/krb5_asn.h";
316 #$crypto.=" crypto/store/store.h";
317 $crypto.=" crypto/pqueue/pqueue.h";
318 $crypto.=" crypto/cms/cms.h";
319 $crypto.=" crypto/jpake/jpake.h";
320 $crypto.=" crypto/modes/modes.h";
321 $crypto.=" crypto/srp/srp.h";
322
323 my $symhacks="crypto/symhacks.h";
324
325 my @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
326 my @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
327
328 if ($do_update) {
329
330 if ($do_ssl == 1) {
331
332         &maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
333         if ($do_rewrite == 1) {
334                 open(OUT, ">$ssl_num");
335                 &rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
336         } else {
337                 open(OUT, ">>$ssl_num");
338         }
339         &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
340         close OUT;
341 }
342
343 if($do_crypto == 1) {
344
345         &maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
346         if ($do_rewrite == 1) {
347                 open(OUT, ">$crypto_num");
348                 &rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
349         } else {
350                 open(OUT, ">>$crypto_num");
351         }
352         &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
353         close OUT;
354
355
356 } elsif ($do_checkexist) {
357         &check_existing(*ssl_list, @ssl_symbols)
358                 if $do_ssl == 1;
359         &check_existing(*crypto_list, @crypto_symbols)
360                 if $do_crypto == 1;
361 } elsif ($do_ctest || $do_ctestall) {
362
363         print <<"EOF";
364
365 /* Test file to check all DEF file symbols are present by trying
366  * to link to all of them. This is *not* intended to be run!
367  */
368
369 int main()
370 {
371 EOF
372         &print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
373                 if $do_ssl == 1;
374
375         &print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
376                 if $do_crypto == 1;
377
378         print "}\n";
379
380 } else {
381
382         &print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols)
383                 if $do_ssl == 1;
384
385         &print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols)
386                 if $do_crypto == 1;
387
388 }
389
390
391 sub do_defs
392 {
393         my($name,$files,$symhacksfile)=@_;
394         my $file;
395         my @ret;
396         my %syms;
397         my %platform;           # For anything undefined, we assume ""
398         my %kind;               # For anything undefined, we assume "FUNCTION"
399         my %algorithm;          # For anything undefined, we assume ""
400         my %variant;
401         my %variant_cnt;        # To be able to allocate "name{n}" if "name"
402                                 # is the same name as the original.
403         my $cpp;
404         my %unknown_algorithms = ();
405
406         foreach $file (split(/\s+/,$symhacksfile." ".$files))
407                 {
408                 print STDERR "DEBUG: starting on $file:\n" if $debug;
409                 open(IN,"<$file") || die "unable to open $file:$!\n";
410                 my $line = "", my $def= "";
411                 my %tag = (
412                         (map { $_ => 0 } @known_platforms),
413                         (map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
414                         (map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
415                         NOPROTO         => 0,
416                         PERL5           => 0,
417                         _WINDLL         => 0,
418                         CONST_STRICT    => 0,
419                         TRUE            => 1,
420                 );
421                 my $symhacking = $file eq $symhacksfile;
422                 my @current_platforms = ();
423                 my @current_algorithms = ();
424
425                 # params: symbol, alias, platforms, kind
426                 # The reason to put this subroutine in a variable is that
427                 # it will otherwise create it's own, unshared, version of
428                 # %tag and %variant...
429                 my $make_variant = sub
430                 {
431                         my ($s, $a, $p, $k) = @_;
432                         my ($a1, $a2);
433
434                         print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
435                         if (defined($p))
436                         {
437                                 $a1 = join(",",$p,
438                                            grep(!/^$/,
439                                                 map { $tag{$_} == 1 ? $_ : "" }
440                                                 @known_platforms));
441                         }
442                         else
443                         {
444                                 $a1 = join(",",
445                                            grep(!/^$/,
446                                                 map { $tag{$_} == 1 ? $_ : "" }
447                                                 @known_platforms));
448                         }
449                         $a2 = join(",",
450                                    grep(!/^$/,
451                                         map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
452                                         @known_ossl_platforms));
453                         print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
454                         if ($a1 eq "") { $a1 = $a2; }
455                         elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
456                         if ($a eq $s)
457                         {
458                                 if (!defined($variant_cnt{$s}))
459                                 {
460                                         $variant_cnt{$s} = 0;
461                                 }
462                                 $variant_cnt{$s}++;
463                                 $a .= "{$variant_cnt{$s}}";
464                         }
465                         my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
466                         my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
467                         if (!grep(/^$togrep$/,
468                                   split(/;/, defined($variant{$s})?$variant{$s}:""))) {
469                                 if (defined($variant{$s})) { $variant{$s} .= ";"; }
470                                 $variant{$s} .= $toadd;
471                         }
472                         print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
473                 };
474
475                 print STDERR "DEBUG: parsing ----------\n" if $debug;
476                 while(<IN>) {
477                         if (/\/\* Error codes for the \w+ functions\. \*\//)
478                                 {
479                                 undef @tag;
480                                 last;
481                                 }
482                         if ($line ne '') {
483                                 $_ = $line . $_;
484                                 $line = '';
485                         }
486
487                         if (/\\$/) {
488                                 chomp; # remove eol
489                                 chop; # remove ending backslash
490                                 $line = $_;
491                                 next;
492                         }
493
494                         if(/\/\*/) {
495                                 if (not /\*\//) {       # multiline comment...
496                                         $line = $_;     # ... just accumulate
497                                         next;
498                                 } else {
499                                         s/\/\*.*?\*\///gs;# wipe it
500                                 }
501                         }
502
503                         if ($cpp) {
504                                 $cpp++ if /^#\s*if/;
505                                 $cpp-- if /^#\s*endif/;
506                                 next;
507                         }
508                         $cpp = 1 if /^#.*ifdef.*cplusplus/;
509
510                         s/{[^{}]*}//gs;                      # ignore {} blocks
511                         print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
512                         print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
513                         if (/^\#\s*ifndef\s+(.*)/) {
514                                 push(@tag,"-");
515                                 push(@tag,$1);
516                                 $tag{$1}=-1;
517                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
518                         } elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
519                                 push(@tag,"-");
520                                 if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) {
521                                         my $tmp_1 = $1;
522                                         my $tmp_;
523                                         foreach $tmp_ (split '\&\&',$tmp_1) {
524                                                 $tmp_ =~ /!defined\(([^\)]+)\)/;
525                                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
526                                                 push(@tag,$1);
527                                                 $tag{$1}=-1;
528                                         }
529                                 } else {
530                                         print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O...
531                                         print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
532                                         push(@tag,$1);
533                                         $tag{$1}=-1;
534                                 }
535                         } elsif (/^\#\s*ifdef\s+(\S*)/) {
536                                 push(@tag,"-");
537                                 push(@tag,$1);
538                                 $tag{$1}=1;
539                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
540                         } elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
541                                 push(@tag,"-");
542                                 if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) {
543                                         my $tmp_1 = $1;
544                                         my $tmp_;
545                                         foreach $tmp_ (split '\|\|',$tmp_1) {
546                                                 $tmp_ =~ /defined\(([^\)]+)\)/;
547                                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
548                                                 push(@tag,$1);
549                                                 $tag{$1}=1;
550                                         }
551                                 } else {
552                                         print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O...
553                                         print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
554                                         push(@tag,$1);
555                                         $tag{$1}=1;
556                                 }
557                         } elsif (/^\#\s*error\s+(\w+) is disabled\./) {
558                                 my $tag_i = $#tag;
559                                 while($tag[$tag_i] ne "-") {
560                                         if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
561                                                 $tag{$tag[$tag_i]}=2;
562                                                 print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug;
563                                         }
564                                         $tag_i--;
565                                 }
566                         } elsif (/^\#\s*endif/) {
567                                 my $tag_i = $#tag;
568                                 while($tag_i > 0 && $tag[$tag_i] ne "-") {
569                                         my $t=$tag[$tag_i];
570                                         print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
571                                         if ($tag{$t}==2) {
572                                                 $tag{$t}=-1;
573                                         } else {
574                                                 $tag{$t}=0;
575                                         }
576                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
577                                         pop(@tag);
578                                         if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
579                                                 $t=$1;
580                                         } else {
581                                                 $t="";
582                                         }
583                                         if ($t ne ""
584                                             && !grep(/^$t$/, @known_algorithms)) {
585                                                 $unknown_algorithms{$t} = 1;
586                                                 #print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
587                                         }
588                                         $tag_i--;
589                                 }
590                                 pop(@tag);
591                         } elsif (/^\#\s*else/) {
592                                 my $tag_i = $#tag;
593                                 while($tag[$tag_i] ne "-") {
594                                         my $t=$tag[$tag_i];
595                                         $tag{$t}= -$tag{$t};
596                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
597                                         $tag_i--;
598                                 }
599                         } elsif (/^\#\s*if\s+1/) {
600                                 push(@tag,"-");
601                                 # Dummy tag
602                                 push(@tag,"TRUE");
603                                 $tag{"TRUE"}=1;
604                                 print STDERR "DEBUG: $file: found 1\n" if $debug;
605                         } elsif (/^\#\s*if\s+0/) {
606                                 push(@tag,"-");
607                                 # Dummy tag
608                                 push(@tag,"TRUE");
609                                 $tag{"TRUE"}=-1;
610                                 print STDERR "DEBUG: $file: found 0\n" if $debug;
611                         } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
612                                  && $symhacking && $tag{'TRUE'} != -1) {
613                                 # This is for aliasing.  When we find an alias,
614                                 # we have to invert
615                                 &$make_variant($1,$2);
616                                 print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
617                         }
618                         if (/^\#/) {
619                                 @current_platforms =
620                                     grep(!/^$/,
621                                          map { $tag{$_} == 1 ? $_ :
622                                                    $tag{$_} == -1 ? "!".$_  : "" }
623                                          @known_platforms);
624                                 push @current_platforms
625                                     , grep(!/^$/,
626                                            map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
627                                                      $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
628                                            @known_ossl_platforms);
629                                 @current_algorithms =
630                                     grep(!/^$/,
631                                          map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
632                                          @known_algorithms);
633                                 $def .=
634                                     "#INFO:"
635                                         .join(',',@current_platforms).":"
636                                             .join(',',@current_algorithms).";";
637                                 next;
638                         }
639                         if ($tag{'TRUE'} != -1) {
640                                 if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
641                                         next;
642                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
643                                         $def .= "int d2i_$3(void);";
644                                         $def .= "int i2d_$3(void);";
645                                         # Variant for platforms that do not
646                                         # have to access globale variables
647                                         # in shared libraries through functions
648                                         $def .=
649                                             "#INFO:"
650                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
651                                                     .join(',',@current_algorithms).";";
652                                         $def .= "OPENSSL_EXTERN int $2_it;";
653                                         $def .=
654                                             "#INFO:"
655                                                 .join(',',@current_platforms).":"
656                                                     .join(',',@current_algorithms).";";
657                                         # Variant for platforms that have to
658                                         # access globale variables in shared
659                                         # libraries through functions
660                                         &$make_variant("$2_it","$2_it",
661                                                       "EXPORT_VAR_AS_FUNCTION",
662                                                       "FUNCTION");
663                                         next;
664                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
665                                         $def .= "int d2i_$3(void);";
666                                         $def .= "int i2d_$3(void);";
667                                         $def .= "int $3_free(void);";
668                                         $def .= "int $3_new(void);";
669                                         # Variant for platforms that do not
670                                         # have to access globale variables
671                                         # in shared libraries through functions
672                                         $def .=
673                                             "#INFO:"
674                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
675                                                     .join(',',@current_algorithms).";";
676                                         $def .= "OPENSSL_EXTERN int $2_it;";
677                                         $def .=
678                                             "#INFO:"
679                                                 .join(',',@current_platforms).":"
680                                                     .join(',',@current_algorithms).";";
681                                         # Variant for platforms that have to
682                                         # access globale variables in shared
683                                         # libraries through functions
684                                         &$make_variant("$2_it","$2_it",
685                                                       "EXPORT_VAR_AS_FUNCTION",
686                                                       "FUNCTION");
687                                         next;
688                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
689                                          /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
690                                         $def .= "int d2i_$1(void);";
691                                         $def .= "int i2d_$1(void);";
692                                         $def .= "int $1_free(void);";
693                                         $def .= "int $1_new(void);";
694                                         # Variant for platforms that do not
695                                         # have to access globale variables
696                                         # in shared libraries through functions
697                                         $def .=
698                                             "#INFO:"
699                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
700                                                     .join(',',@current_algorithms).";";
701                                         $def .= "OPENSSL_EXTERN int $1_it;";
702                                         $def .=
703                                             "#INFO:"
704                                                 .join(',',@current_platforms).":"
705                                                     .join(',',@current_algorithms).";";
706                                         # Variant for platforms that have to
707                                         # access globale variables in shared
708                                         # libraries through functions
709                                         &$make_variant("$1_it","$1_it",
710                                                       "EXPORT_VAR_AS_FUNCTION",
711                                                       "FUNCTION");
712                                         next;
713                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
714                                         $def .= "int d2i_$2(void);";
715                                         $def .= "int i2d_$2(void);";
716                                         # Variant for platforms that do not
717                                         # have to access globale variables
718                                         # in shared libraries through functions
719                                         $def .=
720                                             "#INFO:"
721                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
722                                                     .join(',',@current_algorithms).";";
723                                         $def .= "OPENSSL_EXTERN int $2_it;";
724                                         $def .=
725                                             "#INFO:"
726                                                 .join(',',@current_platforms).":"
727                                                     .join(',',@current_algorithms).";";
728                                         # Variant for platforms that have to
729                                         # access globale variables in shared
730                                         # libraries through functions
731                                         &$make_variant("$2_it","$2_it",
732                                                       "EXPORT_VAR_AS_FUNCTION",
733                                                       "FUNCTION");
734                                         next;
735                                 } elsif (/^\s*DECLARE_ASN1_ALLOC_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
736                                         $def .= "int $1_free(void);";
737                                         $def .= "int $1_new(void);";
738                                         next;
739                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
740                                         $def .= "int d2i_$2(void);";
741                                         $def .= "int i2d_$2(void);";
742                                         $def .= "int $2_free(void);";
743                                         $def .= "int $2_new(void);";
744                                         # Variant for platforms that do not
745                                         # have to access globale variables
746                                         # in shared libraries through functions
747                                         $def .=
748                                             "#INFO:"
749                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
750                                                     .join(',',@current_algorithms).";";
751                                         $def .= "OPENSSL_EXTERN int $2_it;";
752                                         $def .=
753                                             "#INFO:"
754                                                 .join(',',@current_platforms).":"
755                                                     .join(',',@current_algorithms).";";
756                                         # Variant for platforms that have to
757                                         # access globale variables in shared
758                                         # libraries through functions
759                                         &$make_variant("$2_it","$2_it",
760                                                       "EXPORT_VAR_AS_FUNCTION",
761                                                       "FUNCTION");
762                                         next;
763                                 } elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
764                                         # Variant for platforms that do not
765                                         # have to access globale variables
766                                         # in shared libraries through functions
767                                         $def .=
768                                             "#INFO:"
769                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
770                                                     .join(',',@current_algorithms).";";
771                                         $def .= "OPENSSL_EXTERN int $1_it;";
772                                         $def .=
773                                             "#INFO:"
774                                                 .join(',',@current_platforms).":"
775                                                     .join(',',@current_algorithms).";";
776                                         # Variant for platforms that have to
777                                         # access globale variables in shared
778                                         # libraries through functions
779                                         &$make_variant("$1_it","$1_it",
780                                                       "EXPORT_VAR_AS_FUNCTION",
781                                                       "FUNCTION");
782                                         next;
783                                 } elsif (/^\s*DECLARE_ASN1_NDEF_FUNCTION\s*\(\s*(\w*)\s*\)/) {
784                                         $def .= "int i2d_$1_NDEF(void);";
785                                 } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
786                                         next;
787                                 } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION\s*\(\s*(\w*)\s*\)/) {
788                                         $def .= "int $1_print_ctx(void);";
789                                         next;
790                                 } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
791                                         $def .= "int $2_print_ctx(void);";
792                                         next;
793                                 } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
794                                         next;
795                                 } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
796                                          /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ||
797                                          /^DECLARE_PEM_rw_const\s*\(\s*(\w*)\s*,/ ) {
798                                         # Things not in Win16
799                                         $def .=
800                                             "#INFO:"
801                                                 .join(',',"!WIN16",@current_platforms).":"
802                                                     .join(',',@current_algorithms).";";
803                                         $def .= "int PEM_read_$1(void);";
804                                         $def .= "int PEM_write_$1(void);";
805                                         $def .=
806                                             "#INFO:"
807                                                 .join(',',@current_platforms).":"
808                                                     .join(',',@current_algorithms).";";
809                                         # Things that are everywhere
810                                         $def .= "int PEM_read_bio_$1(void);";
811                                         $def .= "int PEM_write_bio_$1(void);";
812                                         next;
813                                 } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
814                                          /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
815                                         # Things not in Win16
816                                         $def .=
817                                             "#INFO:"
818                                                 .join(',',"!WIN16",@current_platforms).":"
819                                                     .join(',',@current_algorithms).";";
820                                         $def .= "int PEM_write_$1(void);";
821                                         $def .=
822                                             "#INFO:"
823                                                 .join(',',@current_platforms).":"
824                                                     .join(',',@current_algorithms).";";
825                                         # Things that are everywhere
826                                         $def .= "int PEM_write_bio_$1(void);";
827                                         next;
828                                 } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
829                                          /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
830                                         # Things not in Win16
831                                         $def .=
832                                             "#INFO:"
833                                                 .join(',',"!WIN16",@current_platforms).":"
834                                                     .join(',',@current_algorithms).";";
835                                         $def .= "int PEM_read_$1(void);";
836                                         $def .=
837                                             "#INFO:"
838                                                 .join(',',@current_platforms).":"
839                                                     .join(',',@current_algorithms).";";
840                                         # Things that are everywhere
841                                         $def .= "int PEM_read_bio_$1(void);";
842                                         next;
843                                 } elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
844                                         # Variant for platforms that do not
845                                         # have to access globale variables
846                                         # in shared libraries through functions
847                                         $def .=
848                                             "#INFO:"
849                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
850                                                     .join(',',@current_algorithms).";";
851                                         $def .= "OPENSSL_EXTERN int _shadow_$2;";
852                                         $def .=
853                                             "#INFO:"
854                                                 .join(',',@current_platforms).":"
855                                                     .join(',',@current_algorithms).";";
856                                         # Variant for platforms that have to
857                                         # access globale variables in shared
858                                         # libraries through functions
859                                         &$make_variant("_shadow_$2","_shadow_$2",
860                                                       "EXPORT_VAR_AS_FUNCTION",
861                                                       "FUNCTION");
862                                 } elsif ($tag{'CONST_STRICT'} != 1) {
863                                         if (/\{|\/\*|\([^\)]*$/) {
864                                                 $line = $_;
865                                         } else {
866                                                 $def .= $_;
867                                         }
868                                 }
869                         }
870                 }
871                 close(IN);
872
873                 my $algs;
874                 my $plays;
875
876                 print STDERR "DEBUG: postprocessing ----------\n" if $debug;
877                 foreach (split /;/, $def) {
878                         my $s; my $k = "FUNCTION"; my $p; my $a;
879                         s/^[\n\s]*//g;
880                         s/[\n\s]*$//g;
881                         next if(/\#undef/);
882                         next if(/typedef\W/);
883                         next if(/\#define/);
884
885                         # Reduce argument lists to empty ()
886                         # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
887                         while(/\(.*\)/s) {
888                                 s/\([^\(\)]+\)/\{\}/gs;
889                                 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;    #(*f{}) -> f
890                         }
891                         # pretend as we didn't use curly braces: {} -> ()
892                         s/\{\}/\(\)/gs;
893
894                         s/STACK_OF\(\)/void/gs;
895                         s/LHASH_OF\(\)/void/gs;
896
897                         print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug;
898                         if (/^\#INFO:([^:]*):(.*)$/) {
899                                 $plats = $1;
900                                 $algs = $2;
901                                 print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug;
902                                 next;
903                         } elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) {
904                                 $s = $1;
905                                 $k = "VARIABLE";
906                                 print STDERR "DEBUG: found external variable $s\n" if $debug;
907                         } elsif (/TYPEDEF_\w+_OF/s) {
908                                 next;
909                         } elsif (/(\w+)\s*\(\).*/s) {   # first token prior [first] () is
910                                 $s = $1;                # a function name!
911                                 print STDERR "DEBUG: found function $s\n" if $debug;
912                         } elsif (/\(/ and not (/=/)) {
913                                 print STDERR "File $file: cannot parse: $_;\n";
914                                 next;
915                         } else {
916                                 next;
917                         }
918
919                         $syms{$s} = 1;
920                         $kind{$s} = $k;
921
922                         $p = $plats;
923                         $a = $algs;
924                         $a .= ",BF" if($s =~ /EVP_bf/);
925                         $a .= ",CAST" if($s =~ /EVP_cast/);
926                         $a .= ",DES" if($s =~ /EVP_des/);
927                         $a .= ",DSA" if($s =~ /EVP_dss/);
928                         $a .= ",IDEA" if($s =~ /EVP_idea/);
929                         $a .= ",MD2" if($s =~ /EVP_md2/);
930                         $a .= ",MD4" if($s =~ /EVP_md4/);
931                         $a .= ",MD5" if($s =~ /EVP_md5/);
932                         $a .= ",RC2" if($s =~ /EVP_rc2/);
933                         $a .= ",RC4" if($s =~ /EVP_rc4/);
934                         $a .= ",RC5" if($s =~ /EVP_rc5/);
935                         $a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
936                         $a .= ",SHA" if($s =~ /EVP_sha/);
937                         $a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
938                         $a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
939                         $a .= ",RSA" if($s =~ /RSAPrivateKey/);
940                         $a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
941
942                         $platform{$s} =
943                             &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
944                         $algorithm{$s} .= ','.$a;
945
946                         if (defined($variant{$s})) {
947                                 foreach $v (split /;/,$variant{$s}) {
948                                         (my $r, my $p, my $k) = split(/:/,$v);
949                                         my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
950                                         $syms{$r} = 1;
951                                         if (!defined($k)) { $k = $kind{$s}; }
952                                         $kind{$r} = $k."(".$s.")";
953                                         $algorithm{$r} = $algorithm{$s};
954                                         $platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
955                                         $platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
956                                         print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
957                                 }
958                         }
959                         print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
960                 }
961         }
962
963         # Prune the returned symbols
964
965         delete $syms{"bn_dump1"};
966         $platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
967
968         $platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
969         $platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
970         $platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
971         $platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
972         $platform{"EVP_sha384"} = "!VMSVAX";
973         $platform{"EVP_sha512"} = "!VMSVAX";
974         $platform{"SHA384_Init"} = "!VMSVAX";
975         $platform{"SHA384_Transform"} = "!VMSVAX";
976         $platform{"SHA384_Update"} = "!VMSVAX";
977         $platform{"SHA384_Final"} = "!VMSVAX";
978         $platform{"SHA384"} = "!VMSVAX";
979         $platform{"SHA512_Init"} = "!VMSVAX";
980         $platform{"SHA512_Transform"} = "!VMSVAX";
981         $platform{"SHA512_Update"} = "!VMSVAX";
982         $platform{"SHA512_Final"} = "!VMSVAX";
983         $platform{"SHA512"} = "!VMSVAX";
984         $platform{"WHIRLPOOL_Init"} = "!VMSVAX";
985         $platform{"WHIRLPOOL"} = "!VMSVAX";
986         $platform{"WHIRLPOOL_BitUpdate"} = "!VMSVAX";
987         $platform{"EVP_whirlpool"} = "!VMSVAX";
988         $platform{"WHIRLPOOL_Final"} = "!VMSVAX";
989         $platform{"WHIRLPOOL_Update"} = "!VMSVAX";
990
991
992         # Info we know about
993
994         push @ret, map { $_."\\".&info_string($_,"EXIST",
995                                               $platform{$_},
996                                               $kind{$_},
997                                               $algorithm{$_}) } keys %syms;
998
999         if (keys %unknown_algorithms) {
1000                 print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
1001                 print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
1002         }
1003         return(@ret);
1004 }
1005
1006 # Param: string of comma-separated platform-specs.
1007 sub reduce_platforms
1008 {
1009         my ($platforms) = @_;
1010         my $pl = defined($platforms) ? $platforms : "";
1011         my %p = map { $_ => 0 } split /,/, $pl;
1012         my $ret;
1013
1014         print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
1015             if $debug;
1016         # We do this, because if there's code like the following, it really
1017         # means the function exists in all cases and should therefore be
1018         # everywhere.  By increasing and decreasing, we may attain 0:
1019         #
1020         # ifndef WIN16
1021         #    int foo();
1022         # else
1023         #    int _fat foo();
1024         # endif
1025         foreach $platform (split /,/, $pl) {
1026                 if ($platform =~ /^!(.*)$/) {
1027                         $p{$1}--;
1028                 } else {
1029                         $p{$platform}++;
1030                 }
1031         }
1032         foreach $platform (keys %p) {
1033                 if ($p{$platform} == 0) { delete $p{$platform}; }
1034         }
1035
1036         delete $p{""};
1037
1038         $ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
1039         print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
1040             if $debug;
1041         return $ret;
1042 }
1043
1044 sub info_string {
1045         (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
1046
1047         my %a = defined($algorithms) ?
1048             map { $_ => 1 } split /,/, $algorithms : ();
1049         my $k = defined($kind) ? $kind : "FUNCTION";
1050         my $ret;
1051         my $p = &reduce_platforms($platforms);
1052
1053         delete $a{""};
1054
1055         $ret = $exist;
1056         $ret .= ":".$p;
1057         $ret .= ":".$k;
1058         $ret .= ":".join(',',sort keys %a);
1059         return $ret;
1060 }
1061
1062 sub maybe_add_info {
1063         (my $name, *nums, my @symbols) = @_;
1064         my $sym;
1065         my $new_info = 0;
1066         my %syms=();
1067
1068         print STDERR "Updating $name info\n";
1069         foreach $sym (@symbols) {
1070                 (my $s, my $i) = split /\\/, $sym;
1071                 if (defined($nums{$s})) {
1072                         $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
1073                         (my $n, my $dummy) = split /\\/, $nums{$s};
1074                         if (!defined($dummy) || $i ne $dummy) {
1075                                 $nums{$s} = $n."\\".$i;
1076                                 $new_info++;
1077                                 print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
1078                         }
1079                 }
1080                 $syms{$s} = 1;
1081         }
1082
1083         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
1084         foreach $sym (@s) {
1085                 (my $n, my $i) = split /\\/, $nums{$sym};
1086                 if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
1087                         $new_info++;
1088                         print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
1089                 }
1090         }
1091         if ($new_info) {
1092                 print STDERR "$new_info old symbols got an info update\n";
1093                 if (!$do_rewrite) {
1094                         print STDERR "You should do a rewrite to fix this.\n";
1095                 }
1096         } else {
1097                 print STDERR "No old symbols needed info update\n";
1098         }
1099 }
1100
1101 # Param: string of comma-separated keywords, each possibly prefixed with a "!"
1102 sub is_valid
1103 {
1104         my ($keywords_txt,$platforms) = @_;
1105         my (@keywords) = split /,/,$keywords_txt;
1106         my ($falsesum, $truesum) = (0, 1);
1107
1108         # Param: one keyword
1109         sub recognise
1110         {
1111                 my ($keyword,$platforms) = @_;
1112
1113                 if ($platforms) {
1114                         # platforms
1115                         if ($keyword eq "VMSVAX" && $VMSVAX) { return 1; }
1116                         if ($keyword eq "VMSNonVAX" && $VMSNonVAX) { return 1; }
1117                         if ($keyword eq "VMS" && $VMS) { return 1; }
1118                         if ($keyword eq "WIN32" && $W32) { return 1; }
1119                         if ($keyword eq "WIN16" && $W16) { return 1; }
1120                         if ($keyword eq "WINNT" && $NT) { return 1; }
1121                         if ($keyword eq "OS2" && $OS2) { return 1; }
1122                         # Special platforms:
1123                         # EXPORT_VAR_AS_FUNCTION means that global variables
1124                         # will be represented as functions.  This currently
1125                         # only happens on VMS-VAX.
1126                         if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && ($VMSVAX || $W32 || $W16)) {
1127                                 return 1;
1128                         }
1129                         if ($keyword eq "ZLIB" && $zlib) { return 1; }
1130                         return 0;
1131                 } else {
1132                         # algorithms
1133                         if ($keyword eq "RC2" && $no_rc2) { return 0; }
1134                         if ($keyword eq "RC4" && $no_rc4) { return 0; }
1135                         if ($keyword eq "RC5" && $no_rc5) { return 0; }
1136                         if ($keyword eq "IDEA" && $no_idea) { return 0; }
1137                         if ($keyword eq "DES" && $no_des) { return 0; }
1138                         if ($keyword eq "BF" && $no_bf) { return 0; }
1139                         if ($keyword eq "CAST" && $no_cast) { return 0; }
1140                         if ($keyword eq "MD2" && $no_md2) { return 0; }
1141                         if ($keyword eq "MD4" && $no_md4) { return 0; }
1142                         if ($keyword eq "MD5" && $no_md5) { return 0; }
1143                         if ($keyword eq "SHA" && $no_sha) { return 0; }
1144                         if ($keyword eq "RIPEMD" && $no_ripemd) { return 0; }
1145                         if ($keyword eq "MDC2" && $no_mdc2) { return 0; }
1146                         if ($keyword eq "WHIRLPOOL" && $no_whirlpool) { return 0; }
1147                         if ($keyword eq "RSA" && $no_rsa) { return 0; }
1148                         if ($keyword eq "DSA" && $no_dsa) { return 0; }
1149                         if ($keyword eq "DH" && $no_dh) { return 0; }
1150                         if ($keyword eq "EC" && $no_ec) { return 0; }
1151                         if ($keyword eq "ECDSA" && $no_ecdsa) { return 0; }
1152                         if ($keyword eq "ECDH" && $no_ecdh) { return 0; }
1153                         if ($keyword eq "HMAC" && $no_hmac) { return 0; }
1154                         if ($keyword eq "AES" && $no_aes) { return 0; }
1155                         if ($keyword eq "CAMELLIA" && $no_camellia) { return 0; }
1156                         if ($keyword eq "SEED" && $no_seed) { return 0; }
1157                         if ($keyword eq "EVP" && $no_evp) { return 0; }
1158                         if ($keyword eq "LHASH" && $no_lhash) { return 0; }
1159                         if ($keyword eq "STACK" && $no_stack) { return 0; }
1160                         if ($keyword eq "ERR" && $no_err) { return 0; }
1161                         if ($keyword eq "BUFFER" && $no_buffer) { return 0; }
1162                         if ($keyword eq "BIO" && $no_bio) { return 0; }
1163                         if ($keyword eq "COMP" && $no_comp) { return 0; }
1164                         if ($keyword eq "DSO" && $no_dso) { return 0; }
1165                         if ($keyword eq "KRB5" && $no_krb5) { return 0; }
1166                         if ($keyword eq "ENGINE" && $no_engine) { return 0; }
1167                         if ($keyword eq "HW" && $no_hw) { return 0; }
1168                         if ($keyword eq "FP_API" && $no_fp_api) { return 0; }
1169                         if ($keyword eq "STATIC_ENGINE" && $no_static_engine) { return 0; }
1170                         if ($keyword eq "GMP" && $no_gmp) { return 0; }
1171                         if ($keyword eq "RFC3779" && $no_rfc3779) { return 0; }
1172                         if ($keyword eq "TLSEXT" && $no_tlsext) { return 0; }
1173                         if ($keyword eq "PSK" && $no_psk) { return 0; }
1174                         if ($keyword eq "CMS" && $no_cms) { return 0; }
1175                         if ($keyword eq "SSL2" && $no_ssl2) { return 0; }
1176                         if ($keyword eq "CAPIENG" && $no_capieng) { return 0; }
1177                         if ($keyword eq "JPAKE" && $no_jpake) { return 0; }
1178                         if ($keyword eq "SRP" && $no_srp) { return 0; }
1179                         if ($keyword eq "DEPRECATED" && $no_deprecated) { return 0; }
1180
1181                         # Nothing recognise as true
1182                         return 1;
1183                 }
1184         }
1185
1186         foreach $k (@keywords) {
1187                 if ($k =~ /^!(.*)$/) {
1188                         $falsesum += &recognise($1,$platforms);
1189                 } else {
1190                         $truesum *= &recognise($k,$platforms);
1191                 }
1192         }
1193         print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1194         return (!$falsesum) && $truesum;
1195 }
1196
1197 sub print_test_file
1198 {
1199         (*OUT,my $name,*nums,my $testall,my @symbols)=@_;
1200         my $n = 1; my @e; my @r;
1201         my $sym; my $prev = ""; my $prefSSLeay;
1202
1203         (@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1204         (@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1205         @symbols=((sort @e),(sort @r));
1206
1207         foreach $sym (@symbols) {
1208                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1209                 my $v = 0;
1210                 $v = 1 if $i=~ /^.*?:.*?:VARIABLE/;
1211                 my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1212                 my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1213                 if (!defined($nums{$s})) {
1214                         print STDERR "Warning: $s does not have a number assigned\n"
1215                             if(!$do_update);
1216                 } elsif (is_valid($p,1) && is_valid($a,0)) {
1217                         my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1218                         if ($prev eq $s2) {
1219                                 print OUT "\t/* The following has already appeared previously */\n";
1220                                 print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1221                         }
1222                         $prev = $s2;    # To warn about duplicates...
1223
1224                         ($nn,$ni)=($nums{$s2} =~ /^(.*?)\\(.*)$/);
1225                         if ($v) {
1226                                 print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n";
1227                         } else {
1228                                 print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n";
1229                         }
1230                 }
1231         }
1232 }
1233
1234 sub get_version {
1235    local *MF;
1236    my $v = '?';
1237    open MF, 'Makefile' or return $v;
1238    while (<MF>) {
1239      $v = $1, last if /^VERSION=(.*?)\s*$/;
1240    }
1241    close MF;
1242    return $v;
1243 }
1244
1245 sub print_def_file
1246 {
1247         (*OUT,my $name,*nums,my @symbols)=@_;
1248         my $n = 1; my @e; my @r; my @v; my $prev="";
1249         my $liboptions="";
1250         my $libname = $name;
1251         my $http_vendor = 'www.openssl.org/';
1252         my $version = get_version();
1253         my $what = "OpenSSL: implementation of Secure Socket Layer";
1254         my $description = "$what $version, $name - http://$http_vendor";
1255
1256         if ($W32)
1257                 { $libname.="32"; }
1258         elsif ($W16)
1259                 { $libname.="16"; }
1260         elsif ($OS2)
1261                 { # DLL names should not clash on the whole system.
1262                   # However, they should not have any particular relationship
1263                   # to the name of the static library.  Chose descriptive names
1264                   # (must be at most 8 chars).
1265                   my %translate = (ssl => 'open_ssl', crypto => 'cryptssl');
1266                   $libname = $translate{$name} || $name;
1267                   $liboptions = <<EOO;
1268 INITINSTANCE
1269 DATA MULTIPLE NONSHARED
1270 EOO
1271                   # Vendor field can't contain colon, drat; so we omit http://
1272                   $description = "\@#$http_vendor:$version#\@$what; DLL for library $name.  Build for EMX -Zmtd";
1273                 }
1274
1275         print OUT <<"EOF";
1276 ;
1277 ; Definition file for the DLL version of the $name library from OpenSSL
1278 ;
1279
1280 LIBRARY         $libname        $liboptions
1281
1282 EOF
1283
1284         if ($W16) {
1285                 print <<"EOF";
1286 CODE            PRELOAD MOVEABLE
1287 DATA            PRELOAD MOVEABLE SINGLE
1288
1289 EXETYPE         WINDOWS
1290
1291 HEAPSIZE        4096
1292 STACKSIZE       8192
1293
1294 EOF
1295         }
1296
1297         print "EXPORTS\n";
1298
1299         (@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1300         (@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1301         (@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols);
1302         @symbols=((sort @e),(sort @r), (sort @v));
1303
1304
1305         foreach $sym (@symbols) {
1306                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1307                 my $v = 0;
1308                 $v = 1 if $i =~ /^.*?:.*?:VARIABLE/;
1309                 if (!defined($nums{$s})) {
1310                         printf STDERR "Warning: $s does not have a number assigned\n"
1311                             if(!$do_update);
1312                 } else {
1313                         (my $n, my $dummy) = split /\\/, $nums{$s};
1314                         my %pf = ();
1315                         my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1316                         my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1317                         if (is_valid($p,1) && is_valid($a,0)) {
1318                                 my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1319                                 if ($prev eq $s2) {
1320                                         print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1321                                 }
1322                                 $prev = $s2;    # To warn about duplicates...
1323                                 if($v && !$OS2) {
1324                                         printf OUT "    %s%-39s @%-8d DATA\n",($W32)?"":"_",$s2,$n;
1325                                 } else {
1326                                         printf OUT "    %s%-39s @%d\n",($W32||$OS2)?"":"_",$s2,$n;
1327                                 }
1328                         }
1329                 }
1330         }
1331         printf OUT "\n";
1332 }
1333
1334 sub load_numbers
1335 {
1336         my($name)=@_;
1337         my(@a,%ret);
1338
1339         $max_num = 0;
1340         $num_noinfo = 0;
1341         $prev = "";
1342         $prev_cnt = 0;
1343
1344         open(IN,"<$name") || die "unable to open $name:$!\n";
1345         while (<IN>) {
1346                 chop;
1347                 s/#.*$//;
1348                 next if /^\s*$/;
1349                 @a=split;
1350                 if (defined $ret{$a[0]}) {
1351                         # This is actually perfectly OK
1352                         #print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
1353                 }
1354                 if ($max_num > $a[1]) {
1355                         print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
1356                 }
1357                 elsif ($max_num == $a[1]) {
1358                         # This is actually perfectly OK
1359                         #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1360                         if ($a[0] eq $prev) {
1361                                 $prev_cnt++;
1362                                 $a[0] .= "{$prev_cnt}";
1363                         }
1364                 }
1365                 else {
1366                         $prev_cnt = 0;
1367                 }
1368                 if ($#a < 2) {
1369                         # Existence will be proven later, in do_defs
1370                         $ret{$a[0]}=$a[1];
1371                         $num_noinfo++;
1372                 } else {
1373                         $ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
1374                 }
1375                 $max_num = $a[1] if $a[1] > $max_num;
1376                 $prev=$a[0];
1377         }
1378         if ($num_noinfo) {
1379                 print STDERR "Warning: $num_noinfo symbols were without info.";
1380                 if ($do_rewrite) {
1381                         printf STDERR "  The rewrite will fix this.\n";
1382                 } else {
1383                         printf STDERR "  You should do a rewrite to fix this.\n";
1384                 }
1385         }
1386         close(IN);
1387         return(%ret);
1388 }
1389
1390 sub parse_number
1391 {
1392         (my $str, my $what) = @_;
1393         (my $n, my $i) = split(/\\/,$str);
1394         if ($what eq "n") {
1395                 return $n;
1396         } else {
1397                 return $i;
1398         }
1399 }
1400
1401 sub rewrite_numbers
1402 {
1403         (*OUT,$name,*nums,@symbols)=@_;
1404         my $thing;
1405
1406         print STDERR "Rewriting $name\n";
1407
1408         my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1409         my $r; my %r; my %rsyms;
1410         foreach $r (@r) {
1411                 (my $s, my $i) = split /\\/, $r;
1412                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1413                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1414                 $r{$a} = $s."\\".$i;
1415                 $rsyms{$s} = 1;
1416         }
1417
1418         my %syms = ();
1419         foreach $_ (@symbols) {
1420                 (my $n, my $i) = split /\\/;
1421                 $syms{$n} = 1;
1422         }
1423
1424         my @s=sort {
1425             &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1426             || $a cmp $b
1427         } keys %nums;
1428         foreach $sym (@s) {
1429                 (my $n, my $i) = split /\\/, $nums{$sym};
1430                 next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
1431                 next if defined($rsyms{$sym});
1432                 print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1433                 $i="NOEXIST::FUNCTION:"
1434                         if !defined($i) || $i eq "" || !defined($syms{$sym});
1435                 my $s2 = $sym;
1436                 $s2 =~ s/\{[0-9]+\}$//;
1437                 printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
1438                 if (exists $r{$sym}) {
1439                         (my $s, $i) = split /\\/,$r{$sym};
1440                         my $s2 = $s;
1441                         $s2 =~ s/\{[0-9]+\}$//;
1442                         printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
1443                 }
1444         }
1445 }
1446
1447 sub update_numbers
1448 {
1449         (*OUT,$name,*nums,my $start_num, my @symbols)=@_;
1450         my $new_syms = 0;
1451
1452         print STDERR "Updating $name numbers\n";
1453
1454         my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1455         my $r; my %r; my %rsyms;
1456         foreach $r (@r) {
1457                 (my $s, my $i) = split /\\/, $r;
1458                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1459                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1460                 $r{$a} = $s."\\".$i;
1461                 $rsyms{$s} = 1;
1462         }
1463
1464         foreach $sym (@symbols) {
1465                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1466                 next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
1467                 next if defined($rsyms{$sym});
1468                 die "ERROR: Symbol $sym had no info attached to it."
1469                     if $i eq "";
1470                 if (!exists $nums{$s}) {
1471                         $new_syms++;
1472                         my $s2 = $s;
1473                         $s2 =~ s/\{[0-9]+\}$//;
1474                         printf OUT "%s%-39s %d\t%s\n","",$s2, ++$start_num,$i;
1475                         if (exists $r{$s}) {
1476                                 ($s, $i) = split /\\/,$r{$s};
1477                                 $s =~ s/\{[0-9]+\}$//;
1478                                 printf OUT "%s%-39s %d\t%s\n","",$s, $start_num,$i;
1479                         }
1480                 }
1481         }
1482         if($new_syms) {
1483                 print STDERR "$new_syms New symbols added\n";
1484         } else {
1485                 print STDERR "No New symbols Added\n";
1486         }
1487 }
1488
1489 sub check_existing
1490 {
1491         (*nums, my @symbols)=@_;
1492         my %existing; my @remaining;
1493         @remaining=();
1494         foreach $sym (@symbols) {
1495                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1496                 $existing{$s}=1;
1497         }
1498         foreach $sym (keys %nums) {
1499                 if (!exists $existing{$sym}) {
1500                         push @remaining, $sym;
1501                 }
1502         }
1503         if(@remaining) {
1504                 print STDERR "The following symbols do not seem to exist:\n";
1505                 foreach $sym (@remaining) {
1506                         print STDERR "\t",$sym,"\n";
1507                 }
1508         }
1509 }
1510