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