6523a057ee9b9cbb043eac89654352d9b95ec233
[openssl.git] / util / mkdef.pl
1 #! /usr/bin/env perl
2 # Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the OpenSSL license (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 #
10 # generate a .def file
11 #
12 # It does this by parsing the header files and looking for the
13 # prototyped functions: it then prunes the output.
14 #
15 # Intermediary files are created, call libcrypto.num and libssl.num,
16 # The format of these files is:
17 #
18 #       routine-name    nnnn    vers    info
19 #
20 # The "nnnn" and "vers" fields are the numeric id and version for the symbol
21 # respectively. The "info" part is actually a colon-separated string of fields
22 # with the following meaning:
23 #
24 #       existence:platform:kind:algorithms
25 #
26 # - "existence" can be "EXIST" or "NOEXIST" depending on if the symbol is
27 #   found somewhere in the source,
28 # - "platforms" is empty if it exists on all platforms, otherwise it contains
29 #   comma-separated list of the platform, just as they are if the symbol exists
30 #   for those platforms, or prepended with a "!" if not.  This helps resolve
31 #   symbol name variants for platforms where the names are too long for the
32 #   compiler or linker, or if the systems is case insensitive and there is a
33 #   clash, or the symbol is implemented differently (see
34 #   EXPORT_VAR_AS_FUNCTION).  This script assumes renaming of symbols is found
35 #   in the file crypto/symhacks.h.
36 #   The semantics for the platforms is that every item is checked against the
37 #   environment.  For the negative items ("!FOO"), if any of them is false
38 #   (i.e. "FOO" is true) in the environment, the corresponding symbol can't be
39 #   used.  For the positive items, if all of them are false in the environment,
40 #   the corresponding symbol can't be used.  Any combination of positive and
41 #   negative items are possible, and of course leave room for some redundancy.
42 # - "kind" is "FUNCTION" or "VARIABLE".  The meaning of that is obvious.
43 # - "algorithms" is a comma-separated list of algorithm names.  This helps
44 #   exclude symbols that are part of an algorithm that some user wants to
45 #   exclude.
46 #
47
48 use lib ".";
49 use configdata;
50 use File::Spec::Functions;
51 use File::Basename;
52 use FindBin;
53 use lib "$FindBin::Bin/perl";
54 use OpenSSL::Glob;
55
56 # When building a "variant" shared library, with a custom SONAME, also customize
57 # all the symbol versions.  This produces a shared object that can coexist
58 # without conflict in the same address space as a default build, or an object
59 # with a different variant tag.
60 #
61 # For example, with a target definition that includes:
62 #
63 #         shlib_variant => "-opt",
64 #
65 # we build the following objects:
66 #
67 # $ perl -le '
68 #     for (@ARGV) {
69 #         if ($l = readlink) {
70 #             printf "%s -> %s\n", $_, $l
71 #         } else {
72 #             print
73 #         }
74 #     }' *.so*
75 # libcrypto-opt.so.1.1
76 # libcrypto.so -> libcrypto-opt.so.1.1
77 # libssl-opt.so.1.1
78 # libssl.so -> libssl-opt.so.1.1
79 #
80 # whose SONAMEs and dependencies are:
81 #
82 # $ for l in *.so; do
83 #     echo $l
84 #     readelf -d $l | egrep 'SONAME|NEEDED.*(ssl|crypto)'
85 #   done
86 # libcrypto.so
87 #  0x000000000000000e (SONAME)             Library soname: [libcrypto-opt.so.1.1]
88 # libssl.so
89 #  0x0000000000000001 (NEEDED)             Shared library: [libcrypto-opt.so.1.1]
90 #  0x000000000000000e (SONAME)             Library soname: [libssl-opt.so.1.1]
91 #
92 # We case-fold the variant tag to upper case and replace all non-alnum
93 # characters with "_".  This yields the following symbol versions:
94 #
95 # $ nm libcrypto.so | grep -w A
96 # 0000000000000000 A OPENSSL_OPT_1_1_0
97 # 0000000000000000 A OPENSSL_OPT_1_1_0a
98 # 0000000000000000 A OPENSSL_OPT_1_1_0c
99 # 0000000000000000 A OPENSSL_OPT_1_1_0d
100 # 0000000000000000 A OPENSSL_OPT_1_1_0f
101 # 0000000000000000 A OPENSSL_OPT_1_1_0g
102 # $ nm libssl.so | grep -w A
103 # 0000000000000000 A OPENSSL_OPT_1_1_0
104 # 0000000000000000 A OPENSSL_OPT_1_1_0d
105 #
106 (my $SO_VARIANT = qq{\U$target{"shlib_variant"}}) =~ s/\W/_/g;
107
108 my $debug=0;
109 my $trace=0;
110 my $verbose=0;
111
112 my $crypto_num= catfile($config{sourcedir},"util","libcrypto.num");
113 my $ssl_num=    catfile($config{sourcedir},"util","libssl.num");
114 my $libname;
115
116 my $do_update = 0;
117 my $do_rewrite = 1;
118 my $do_crypto = 0;
119 my $do_ssl = 0;
120 my $do_ctest = 0;
121 my $do_ctestall = 0;
122 my $do_checkexist = 0;
123
124 my $VMS=0;
125 my $W32=0;
126 my $NT=0;
127 my $UNIX=0;
128 my $linux=0;
129 my $aix=0;
130 # Set this to make typesafe STACK definitions appear in DEF
131 my $safe_stack_def = 0;
132
133 my @known_platforms = ( "__FreeBSD__", "PERL5",
134                         "EXPORT_VAR_AS_FUNCTION", "ZLIB", "_WIN32"
135                         );
136 my @known_ossl_platforms = ( "UNIX", "VMS", "WIN32", "WINNT", "OS2" );
137 my @known_algorithms = ( # These are algorithms we know are guarded in relevant
138                          # header files, but aren't actually disablable.
139                          # Without these, this script will warn a lot.
140                          "RSA", "MD5",
141                          # @disablables comes from configdata.pm
142                          map { (my $x = uc $_) =~ s|-|_|g; $x; } @disablables,
143                          # Deprecated functions.  Not really algorithmss, but
144                          # treated as such here for the sake of simplicity
145                          "DEPRECATEDIN_0_9_8",
146                          "DEPRECATEDIN_1_0_0",
147                          "DEPRECATEDIN_1_1_0",
148                          "DEPRECATEDIN_1_2_0",
149                      );
150
151 # %disabled comes from configdata.pm
152 my %disabled_algorithms =
153     map { (my $x = uc $_) =~ s|-|_|g; $x => 1; } keys %disabled;
154
155 my $apiv = sprintf "%x%02x%02x", split(/\./, $config{api});
156 foreach (@known_algorithms) {
157         if (/^DEPRECATEDIN_(\d+)_(\d+)_(\d+)$/) {
158                 my $depv = sprintf "%x%02x%02x", $1, $2, $3;
159                 $disabled_algorithms{$_} = 1 if $apiv ge $depv;
160         }
161 }
162
163 my $zlib;
164
165 foreach (@ARGV, split(/ /, $config{options}))
166         {
167         $debug=1 if $_ eq "debug";
168         $trace=1 if $_ eq "trace";
169         $verbose=1 if $_ eq "verbose";
170         die "win16 not supported" if $_ eq "16";
171         if ($_ eq "32" || $_ eq "mingw") {
172                 $W32=1;
173         } elsif ($_ eq "NT") {
174                 $W32 = 1;
175                 $NT = 1;
176         } elsif ($_ eq "linux" || $_ eq "solaris") {
177                 $linux=1;
178                 $UNIX=1;
179         } elsif ($_ eq "aix") {
180                 $aix=1;
181                 $UNIX=1;
182         } elsif ($_ eq "VMS") {
183                 $VMS=1;
184         }
185         if ($_ eq "zlib" || $_ eq "enable-zlib" || $_ eq "zlib-dynamic"
186                          || $_ eq "enable-zlib-dynamic") {
187                 $zlib = 1;
188         }
189
190         $do_crypto=1 if $_ eq "libcrypto" || $_ eq "crypto";
191         $do_ssl=1 if $_ eq "libssl" || $_ eq "ssl";
192
193         $do_update=1 if $_ eq "update";
194         $do_rewrite=1 if $_ eq "rewrite";
195         $do_ctest=1 if $_ eq "ctest";
196         $do_ctestall=1 if $_ eq "ctestall";
197         $do_checkexist=1 if $_ eq "exist";
198         }
199 $libname = $unified_info{sharednames}->{libcrypto} if $do_crypto;
200 $libname = $unified_info{sharednames}->{libssl} if $do_ssl;
201
202 if (!$libname) {
203         if ($do_ssl) {
204                 $libname="LIBSSL";
205         }
206         if ($do_crypto) {
207                 $libname="LIBCRYPTO";
208         }
209 }
210
211 # If no platform is given, assume WIN32
212 if ($W32 + $VMS + $linux + $aix == 0) {
213         $W32 = 1;
214 }
215 die "Please, only one platform at a time"
216     if ($W32 + $VMS + $linux + $aix > 1);
217
218 if (!$do_ssl && !$do_crypto)
219         {
220         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 | linux | VMS ]\n";
221         exit(1);
222         }
223
224 %ssl_list=&load_numbers($ssl_num);
225 $max_ssl = $max_num;
226 %crypto_list=&load_numbers($crypto_num);
227 $max_crypto = $max_num;
228
229 my $ssl="include/openssl/ssl.h";
230 $ssl.=" include/openssl/sslerr.h";
231 $ssl.=" include/openssl/tls1.h";
232 $ssl.=" include/openssl/srtp.h";
233
234 # When scanning include/openssl, skip all SSL files and some internal ones.
235 my %skipthese;
236 foreach my $f ( split(/\s+/, $ssl) ) {
237     $skipthese{$f} = 1;
238 }
239 $skipthese{'include/openssl/conf_api.h'} = 1;
240 $skipthese{'include/openssl/ebcdic.h'} = 1;
241 $skipthese{'include/openssl/opensslconf.h'} = 1;
242
243 # We use headers found in include/openssl and include/internal only.
244 # The latter is needed so libssl.so/.dll/.exe can link properly.
245 my $crypto ="include/internal/dso.h";
246 $crypto.=" include/internal/o_dir.h";
247 $crypto.=" include/internal/o_str.h";
248 $crypto.=" include/internal/err.h";
249 $crypto.=" include/internal/sslconf.h";
250 foreach my $f ( glob(catfile($config{sourcedir},'include/openssl/*.h')) ) {
251     my $fn = "include/openssl/" . basename($f);
252     $crypto .= " $fn" if !defined $skipthese{$fn};
253 }
254
255 my $symhacks="include/openssl/symhacks.h";
256
257 my @ssl_symbols = &do_defs("LIBSSL", $ssl, $symhacks);
258 my @crypto_symbols = &do_defs("LIBCRYPTO", $crypto, $symhacks);
259
260 if ($do_update) {
261
262 if ($do_ssl == 1) {
263
264         &maybe_add_info("LIBSSL",*ssl_list,@ssl_symbols);
265         if ($do_rewrite == 1) {
266                 open(OUT, ">$ssl_num");
267                 &rewrite_numbers(*OUT,"LIBSSL",*ssl_list,@ssl_symbols);
268         } else {
269                 open(OUT, ">>$ssl_num");
270         }
271         &update_numbers(*OUT,"LIBSSL",*ssl_list,$max_ssl,@ssl_symbols);
272         close OUT;
273 }
274
275 if($do_crypto == 1) {
276
277         &maybe_add_info("LIBCRYPTO",*crypto_list,@crypto_symbols);
278         if ($do_rewrite == 1) {
279                 open(OUT, ">$crypto_num");
280                 &rewrite_numbers(*OUT,"LIBCRYPTO",*crypto_list,@crypto_symbols);
281         } else {
282                 open(OUT, ">>$crypto_num");
283         }
284         &update_numbers(*OUT,"LIBCRYPTO",*crypto_list,$max_crypto,@crypto_symbols);
285         close OUT;
286 }
287
288 } elsif ($do_checkexist) {
289         &check_existing(*ssl_list, @ssl_symbols)
290                 if $do_ssl == 1;
291         &check_existing(*crypto_list, @crypto_symbols)
292                 if $do_crypto == 1;
293 } elsif ($do_ctest || $do_ctestall) {
294
295         print <<"EOF";
296
297 /* Test file to check all DEF file symbols are present by trying
298  * to link to all of them. This is *not* intended to be run!
299  */
300
301 int main()
302 {
303 EOF
304         &print_test_file(*STDOUT,"LIBSSL",*ssl_list,$do_ctestall,@ssl_symbols)
305                 if $do_ssl == 1;
306
307         &print_test_file(*STDOUT,"LIBCRYPTO",*crypto_list,$do_ctestall,@crypto_symbols)
308                 if $do_crypto == 1;
309
310         print "}\n";
311
312 } else {
313
314         &print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols)
315                 if $do_ssl == 1;
316
317         &print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols)
318                 if $do_crypto == 1;
319
320 }
321
322
323 sub do_defs
324 {
325         my($name,$files,$symhacksfile)=@_;
326         my $file;
327         my @ret;
328         my %syms;
329         my %platform;           # For anything undefined, we assume ""
330         my %kind;               # For anything undefined, we assume "FUNCTION"
331         my %algorithm;          # For anything undefined, we assume ""
332         my %variant;
333         my %variant_cnt;        # To be able to allocate "name{n}" if "name"
334                                 # is the same name as the original.
335         my $cpp;
336         my %unknown_algorithms = ();
337         my $parens = 0;
338
339         foreach $file (split(/\s+/,$symhacksfile." ".$files))
340                 {
341                 my $fn = catfile($config{sourcedir},$file);
342                 print STDERR "DEBUG: starting on $fn:\n" if $debug;
343                 print STDERR "TRACE: start reading $fn\n" if $trace;
344                 open(IN,"<$fn") || die "Can't open $fn, $!,";
345                 my $line = "", my $def= "";
346                 my %tag = (
347                         (map { $_ => 0 } @known_platforms),
348                         (map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
349                         (map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
350                         (map { "OPENSSL_USE_".$_ => 0 } @known_algorithms),
351                         (grep /^DEPRECATED_/, @known_algorithms),
352                         NOPROTO         => 0,
353                         PERL5           => 0,
354                         _WINDLL         => 0,
355                         CONST_STRICT    => 0,
356                         TRUE            => 1,
357                 );
358                 my $symhacking = $file eq $symhacksfile;
359                 my @current_platforms = ();
360                 my @current_algorithms = ();
361
362                 # params: symbol, alias, platforms, kind
363                 # The reason to put this subroutine in a variable is that
364                 # it will otherwise create it's own, unshared, version of
365                 # %tag and %variant...
366                 my $make_variant = sub
367                 {
368                         my ($s, $a, $p, $k) = @_;
369                         my ($a1, $a2);
370
371                         print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
372                         if (defined($p))
373                         {
374                                 $a1 = join(",",$p,
375                                            grep(!/^$/,
376                                                 map { $tag{$_} == 1 ? $_ : "" }
377                                                 @known_platforms));
378                         }
379                         else
380                         {
381                                 $a1 = join(",",
382                                            grep(!/^$/,
383                                                 map { $tag{$_} == 1 ? $_ : "" }
384                                                 @known_platforms));
385                         }
386                         $a2 = join(",",
387                                    grep(!/^$/,
388                                         map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
389                                         @known_ossl_platforms));
390                         print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
391                         if ($a1 eq "") { $a1 = $a2; }
392                         elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
393                         if ($a eq $s)
394                         {
395                                 if (!defined($variant_cnt{$s}))
396                                 {
397                                         $variant_cnt{$s} = 0;
398                                 }
399                                 $variant_cnt{$s}++;
400                                 $a .= "{$variant_cnt{$s}}";
401                         }
402                         my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
403                         my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
404                         if (!grep(/^$togrep$/,
405                                   split(/;/, defined($variant{$s})?$variant{$s}:""))) {
406                                 if (defined($variant{$s})) { $variant{$s} .= ";"; }
407                                 $variant{$s} .= $toadd;
408                         }
409                         print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
410                 };
411
412                 print STDERR "DEBUG: parsing ----------\n" if $debug;
413                 while(<IN>) {
414                         s|\R$||; # Better chomp
415                         if($parens > 0) {
416                                 #Inside a DEPRECATEDIN
417                                 $stored_multiline .= $_;
418                                 print STDERR "DEBUG: Continuing multiline DEPRECATEDIN: $stored_multiline\n" if $debug;
419                                 $parens = count_parens($stored_multiline);
420                                 if ($parens == 0) {
421                                         $def .= do_deprecated($stored_multiline,
422                                                         \@current_platforms,
423                                                         \@current_algorithms);
424                                 }
425                                 next;
426                         }
427                         if (/\/\* Error codes for the \w+ functions\. \*\//)
428                                 {
429                                 undef @tag;
430                                 last;
431                                 }
432                         if ($line ne '') {
433                                 $_ = $line . $_;
434                                 $line = '';
435                         }
436
437                         if (/\\$/) {
438                                 $line = $`; # keep what was before the backslash
439                                 next;
440                         }
441
442                         if(/\/\*/) {
443                                 if (not /\*\//) {       # multi-line comment...
444                                         $line = $_;     # ... just accumulate
445                                         next;
446                                 } else {
447                                         s/\/\*.*?\*\///gs;# wipe it
448                                 }
449                         }
450
451                         if ($cpp) {
452                                 $cpp++ if /^#\s*if/;
453                                 $cpp-- if /^#\s*endif/;
454                                 next;
455                         }
456                         if (/^#.*ifdef.*cplusplus/) {
457                                 $cpp = 1;
458                                 next;
459                         }
460
461                         s/{[^{}]*}//gs;                      # ignore {} blocks
462                         print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
463                         print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
464                         if (/^\#\s*if\s+OPENSSL_API_COMPAT\s*(\S)\s*(0x[0-9a-fA-F]{8})L\s*$/) {
465                                 my $op = $1;
466                                 my $v = hex($2);
467                                 if ($op ne '<' && $op ne '>=') {
468                                     die "$file unacceptable operator $op: $_\n";
469                                 }
470                                 my ($one, $major, $minor) =
471                                     ( ($v >> 28) & 0xf,
472                                       ($v >> 20) & 0xff,
473                                       ($v >> 12) & 0xff );
474                                 my $t = "DEPRECATEDIN_${one}_${major}_${minor}";
475                                 push(@tag,"-");
476                                 push(@tag,$t);
477                                 $tag{$t}=($op eq '<' ? 1 : -1);
478                                 print STDERR "DEBUG: $file: found tag $t = $tag{$t}\n" if $debug;
479                         } elsif (/^\#\s*ifndef\s+(.*)/) {
480                                 push(@tag,"-");
481                                 push(@tag,$1);
482                                 $tag{$1}=-1;
483                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
484                         } elsif (/^\#\s*if\s+!defined\s*\(([^\)]+)\)/) {
485                                 push(@tag,"-");
486                                 if (/^\#\s*if\s+(!defined\s*\(([^\)]+)\)(\s+\&\&\s+!defined\s*\(([^\)]+)\))*)$/) {
487                                         my $tmp_1 = $1;
488                                         my $tmp_;
489                                         foreach $tmp_ (split '\&\&',$tmp_1) {
490                                                 $tmp_ =~ /!defined\s*\(([^\)]+)\)/;
491                                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
492                                                 push(@tag,$1);
493                                                 $tag{$1}=-1;
494                                         }
495                                 } else {
496                                         print STDERR "Warning: $file: taking only '!defined($1)' of complicated expression: $_" if $verbose; # because it is O...
497                                         print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
498                                         push(@tag,$1);
499                                         $tag{$1}=-1;
500                                 }
501                         } elsif (/^\#\s*ifdef\s+(\S*)/) {
502                                 push(@tag,"-");
503                                 push(@tag,$1);
504                                 $tag{$1}=1;
505                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
506                         } elsif (/^\#\s*if\s+defined\s*\(([^\)]+)\)/) {
507                                 push(@tag,"-");
508                                 if (/^\#\s*if\s+(defined\s*\(([^\)]+)\)(\s+\|\|\s+defined\s*\(([^\)]+)\))*)$/) {
509                                         my $tmp_1 = $1;
510                                         my $tmp_;
511                                         foreach $tmp_ (split '\|\|',$tmp_1) {
512                                                 $tmp_ =~ /defined\s*\(([^\)]+)\)/;
513                                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
514                                                 push(@tag,$1);
515                                                 $tag{$1}=1;
516                                         }
517                                 } else {
518                                         print STDERR "Warning: $file: taking only 'defined($1)' of complicated expression: $_\n" if $verbose; # because it is O...
519                                         print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
520                                         push(@tag,$1);
521                                         $tag{$1}=1;
522                                 }
523                         } elsif (/^\#\s*error\s+(\w+) is disabled\./) {
524                                 my $tag_i = $#tag;
525                                 while($tag[$tag_i] ne "-") {
526                                         if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
527                                                 $tag{$tag[$tag_i]}=2;
528                                                 print STDERR "DEBUG: $file: changed tag $1 = 2\n" if $debug;
529                                         }
530                                         $tag_i--;
531                                 }
532                         } elsif (/^\#\s*endif/) {
533                                 my $tag_i = $#tag;
534                                 while($tag_i > 0 && $tag[$tag_i] ne "-") {
535                                         my $t=$tag[$tag_i];
536                                         print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
537                                         if ($tag{$t}==2) {
538                                                 $tag{$t}=-1;
539                                         } else {
540                                                 $tag{$t}=0;
541                                         }
542                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
543                                         pop(@tag);
544                                         if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
545                                                 $t=$1;
546                                         } elsif($t =~ /^OPENSSL_USE_([A-Z0-9_]+)$/) {
547                                                 $t=$1;
548                                         } else {
549                                                 $t="";
550                                         }
551                                         if ($t ne ""
552                                             && !grep(/^$t$/, @known_algorithms)) {
553                                                 $unknown_algorithms{$t} = 1;
554                                                 #print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
555                                         }
556                                         $tag_i--;
557                                 }
558                                 pop(@tag);
559                         } elsif (/^\#\s*else/) {
560                                 my $tag_i = $#tag;
561                                 die "$file unmatched else\n" if $tag_i < 0;
562                                 while($tag[$tag_i] ne "-") {
563                                         my $t=$tag[$tag_i];
564                                         $tag{$t}= -$tag{$t};
565                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
566                                         $tag_i--;
567                                 }
568                         } elsif (/^\#\s*if\s+1/) {
569                                 push(@tag,"-");
570                                 # Dummy tag
571                                 push(@tag,"TRUE");
572                                 $tag{"TRUE"}=1;
573                                 print STDERR "DEBUG: $file: found 1\n" if $debug;
574                         } elsif (/^\#\s*if\s+0/) {
575                                 push(@tag,"-");
576                                 # Dummy tag
577                                 push(@tag,"TRUE");
578                                 $tag{"TRUE"}=-1;
579                                 print STDERR "DEBUG: $file: found 0\n" if $debug;
580                         } elsif (/^\#\s*if\s+/) {
581                                 #Some other unrecognized "if" style
582                                 push(@tag,"-");
583                                 print STDERR "Warning: $file: ignoring unrecognized expression: $_\n" if $verbose; # because it is O...
584                         } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
585                                  && $symhacking && $tag{'TRUE'} != -1) {
586                                 # This is for aliasing.  When we find an alias,
587                                 # we have to invert
588                                 &$make_variant($1,$2);
589                                 print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
590                         }
591                         if (/^\#/) {
592                                 @current_platforms =
593                                     grep(!/^$/,
594                                          map { $tag{$_} == 1 ? $_ :
595                                                    $tag{$_} == -1 ? "!".$_  : "" }
596                                          @known_platforms);
597                                 push @current_platforms
598                                     , grep(!/^$/,
599                                            map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
600                                                      $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
601                                            @known_ossl_platforms);
602                                 @current_algorithms = ();
603                                 @current_algorithms =
604                                     grep(!/^$/,
605                                          map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
606                                          @known_algorithms);
607                                 push @current_algorithms
608                                     , grep(!/^$/,
609                                          map { $tag{"OPENSSL_USE_".$_} == 1 ? $_ : "" }
610                                          @known_algorithms);
611                                 push @current_algorithms,
612                                     grep { /^DEPRECATEDIN_/ && $tag{$_} == 1 }
613                                     @known_algorithms;
614                                 $def .=
615                                     "#INFO:"
616                                         .join(',',@current_platforms).":"
617                                             .join(',',@current_algorithms).";";
618                                 next;
619                         }
620                         if ($tag{'TRUE'} != -1) {
621                                 if (/^\s*DEFINE_STACK_OF\s*\(\s*(\w*)\s*\)/
622                                                 || /^\s*DEFINE_STACK_OF_CONST\s*\(\s*(\w*)\s*\)/) {
623                                         next;
624                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
625                                         $def .= "int d2i_$3(void);";
626                                         $def .= "int i2d_$3(void);";
627                                         # Variant for platforms that do not
628                                         # have to access global variables
629                                         # in shared libraries through functions
630                                         $def .=
631                                             "#INFO:"
632                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
633                                                     .join(',',@current_algorithms).";";
634                                         $def .= "OPENSSL_EXTERN int $2_it;";
635                                         $def .=
636                                             "#INFO:"
637                                                 .join(',',@current_platforms).":"
638                                                     .join(',',@current_algorithms).";";
639                                         # Variant for platforms that have to
640                                         # access global variables in shared
641                                         # libraries through functions
642                                         &$make_variant("$2_it","$2_it",
643                                                       "EXPORT_VAR_AS_FUNCTION",
644                                                       "FUNCTION");
645                                         next;
646                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
647                                         $def .= "int d2i_$3(void);";
648                                         $def .= "int i2d_$3(void);";
649                                         $def .= "int $3_free(void);";
650                                         $def .= "int $3_new(void);";
651                                         # Variant for platforms that do not
652                                         # have to access global variables
653                                         # in shared libraries through functions
654                                         $def .=
655                                             "#INFO:"
656                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
657                                                     .join(',',@current_algorithms).";";
658                                         $def .= "OPENSSL_EXTERN int $2_it;";
659                                         $def .=
660                                             "#INFO:"
661                                                 .join(',',@current_platforms).":"
662                                                     .join(',',@current_algorithms).";";
663                                         # Variant for platforms that have to
664                                         # access global variables in shared
665                                         # libraries through functions
666                                         &$make_variant("$2_it","$2_it",
667                                                       "EXPORT_VAR_AS_FUNCTION",
668                                                       "FUNCTION");
669                                         next;
670                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
671                                          /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
672                                         $def .= "int d2i_$1(void);";
673                                         $def .= "int i2d_$1(void);";
674                                         $def .= "int $1_free(void);";
675                                         $def .= "int $1_new(void);";
676                                         # Variant for platforms that do not
677                                         # have to access global variables
678                                         # in shared libraries through functions
679                                         $def .=
680                                             "#INFO:"
681                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
682                                                     .join(',',@current_algorithms).";";
683                                         $def .= "OPENSSL_EXTERN int $1_it;";
684                                         $def .=
685                                             "#INFO:"
686                                                 .join(',',@current_platforms).":"
687                                                     .join(',',@current_algorithms).";";
688                                         # Variant for platforms that have to
689                                         # access global variables in shared
690                                         # libraries through functions
691                                         &$make_variant("$1_it","$1_it",
692                                                       "EXPORT_VAR_AS_FUNCTION",
693                                                       "FUNCTION");
694                                         next;
695                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
696                                         $def .= "int d2i_$2(void);";
697                                         $def .= "int i2d_$2(void);";
698                                         # Variant for platforms that do not
699                                         # have to access global variables
700                                         # in shared libraries through functions
701                                         $def .=
702                                             "#INFO:"
703                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
704                                                     .join(',',@current_algorithms).";";
705                                         $def .= "OPENSSL_EXTERN int $2_it;";
706                                         $def .=
707                                             "#INFO:"
708                                                 .join(',',@current_platforms).":"
709                                                     .join(',',@current_algorithms).";";
710                                         # Variant for platforms that have to
711                                         # access global variables in shared
712                                         # libraries through functions
713                                         &$make_variant("$2_it","$2_it",
714                                                       "EXPORT_VAR_AS_FUNCTION",
715                                                       "FUNCTION");
716                                         next;
717                                 } elsif (/^\s*DECLARE_ASN1_ALLOC_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
718                                         $def .= "int $1_free(void);";
719                                         $def .= "int $1_new(void);";
720                                         next;
721                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
722                                         $def .= "int d2i_$2(void);";
723                                         $def .= "int i2d_$2(void);";
724                                         $def .= "int $2_free(void);";
725                                         $def .= "int $2_new(void);";
726                                         # Variant for platforms that do not
727                                         # have to access global variables
728                                         # in shared libraries through functions
729                                         $def .=
730                                             "#INFO:"
731                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
732                                                     .join(',',@current_algorithms).";";
733                                         $def .= "OPENSSL_EXTERN int $2_it;";
734                                         $def .=
735                                             "#INFO:"
736                                                 .join(',',@current_platforms).":"
737                                                     .join(',',@current_algorithms).";";
738                                         # Variant for platforms that have to
739                                         # access global variables in shared
740                                         # libraries through functions
741                                         &$make_variant("$2_it","$2_it",
742                                                       "EXPORT_VAR_AS_FUNCTION",
743                                                       "FUNCTION");
744                                         next;
745                                 } elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
746                                         # Variant for platforms that do not
747                                         # have to access global variables
748                                         # in shared libraries through functions
749                                         $def .=
750                                             "#INFO:"
751                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
752                                                     .join(',',@current_algorithms).";";
753                                         $def .= "OPENSSL_EXTERN int $1_it;";
754                                         $def .=
755                                             "#INFO:"
756                                                 .join(',',@current_platforms).":"
757                                                     .join(',',@current_algorithms).";";
758                                         # Variant for platforms that have to
759                                         # access global variables in shared
760                                         # libraries through functions
761                                         &$make_variant("$1_it","$1_it",
762                                                       "EXPORT_VAR_AS_FUNCTION",
763                                                       "FUNCTION");
764                                         next;
765                                 } elsif (/^\s*DECLARE_ASN1_NDEF_FUNCTION\s*\(\s*(\w*)\s*\)/) {
766                                         $def .= "int i2d_$1_NDEF(void);";
767                                 } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
768                                         next;
769                                 } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION\s*\(\s*(\w*)\s*\)/) {
770                                         $def .= "int $1_print_ctx(void);";
771                                         next;
772                                 } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
773                                         $def .= "int $2_print_ctx(void);";
774                                         next;
775                                 } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
776                                         next;
777                                 } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
778                                          /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ||
779                                          /^DECLARE_PEM_rw_const\s*\(\s*(\w*)\s*,/ ) {
780                                         $def .=
781                                             "#INFO:"
782                                                 .join(',',@current_platforms).":"
783                                                     .join(',',"STDIO",@current_algorithms).";";
784                                         $def .= "int PEM_read_$1(void);";
785                                         $def .= "int PEM_write_$1(void);";
786                                         $def .=
787                                             "#INFO:"
788                                                 .join(',',@current_platforms).":"
789                                                     .join(',',@current_algorithms).";";
790                                         # Things that are everywhere
791                                         $def .= "int PEM_read_bio_$1(void);";
792                                         $def .= "int PEM_write_bio_$1(void);";
793                                         next;
794                                 } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
795                                         /^DECLARE_PEM_write_const\s*\(\s*(\w*)\s*,/ ||
796                                          /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
797                                         $def .=
798                                             "#INFO:"
799                                                 .join(',',@current_platforms).":"
800                                                     .join(',',"STDIO",@current_algorithms).";";
801                                         $def .= "int PEM_write_$1(void);";
802                                         $def .=
803                                             "#INFO:"
804                                                 .join(',',@current_platforms).":"
805                                                     .join(',',@current_algorithms).";";
806                                         # Things that are everywhere
807                                         $def .= "int PEM_write_bio_$1(void);";
808                                         next;
809                                 } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
810                                          /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
811                                         $def .=
812                                             "#INFO:"
813                                                 .join(',',@current_platforms).":"
814                                                     .join(',',"STDIO",@current_algorithms).";";
815                                         $def .= "int PEM_read_$1(void);";
816                                         $def .=
817                                             "#INFO:"
818                                                 .join(',',@current_platforms).":"
819                                                     .join(',',"STDIO",@current_algorithms).";";
820                                         # Things that are everywhere
821                                         $def .= "int PEM_read_bio_$1(void);";
822                                         next;
823                                 } elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
824                                         # Variant for platforms that do not
825                                         # have to access global variables
826                                         # in shared libraries through functions
827                                         $def .=
828                                             "#INFO:"
829                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
830                                                     .join(',',@current_algorithms).";";
831                                         $def .= "OPENSSL_EXTERN int _shadow_$2;";
832                                         $def .=
833                                             "#INFO:"
834                                                 .join(',',@current_platforms).":"
835                                                     .join(',',@current_algorithms).";";
836                                         # Variant for platforms that have to
837                                         # access global variables in shared
838                                         # libraries through functions
839                                         &$make_variant("_shadow_$2","_shadow_$2",
840                                                       "EXPORT_VAR_AS_FUNCTION",
841                                                       "FUNCTION");
842                                 } elsif (/^\s*DEPRECATEDIN/) {
843                                         $parens = count_parens($_);
844                                         if ($parens == 0) {
845                                                 $def .= do_deprecated($_,
846                                                         \@current_platforms,
847                                                         \@current_algorithms);
848                                         } else {
849                                                 $stored_multiline = $_;
850                                                 print STDERR "DEBUG: Found multiline DEPRECATEDIN starting with: $stored_multiline\n" if $debug;
851                                                 next;
852                                         }
853                                 } elsif ($tag{'CONST_STRICT'} != 1) {
854                                         if (/\{|\/\*|\([^\)]*$/) {
855                                                 $line = $_;
856                                         } else {
857                                                 $def .= $_;
858                                         }
859                                 }
860                         }
861                 }
862                 close(IN);
863                 die "$file: Unmatched tags\n" if $#tag >= 0;
864
865                 my $algs;
866                 my $plays;
867
868                 print STDERR "DEBUG: postprocessing ----------\n" if $debug;
869                 foreach (split /;/, $def) {
870                         my $s; my $k = "FUNCTION"; my $p; my $a;
871                         s/^[\n\s]*//g;
872                         s/[\n\s]*$//g;
873                         next if(/\#undef/);
874                         next if(/typedef\W/);
875                         next if(/\#define/);
876
877                         print STDERR "TRACE: processing $_\n" if $trace && !/^\#INFO:/;
878                         # Reduce argument lists to empty ()
879                         # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
880                         my $nsubst = 1; # prevent infinite loop, e.g., on  int fn()
881                         while($nsubst && /\(.*\)/s) {
882                                 $nsubst = s/\([^\(\)]+\)/\{\}/gs;
883                                 $nsubst+= 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
919                         $platform{$s} =
920                             &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
921                         $algorithm{$s} .= ','.$a;
922
923                         if (defined($variant{$s})) {
924                                 foreach $v (split /;/,$variant{$s}) {
925                                         (my $r, my $p, my $k) = split(/:/,$v);
926                                         my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
927                                         $syms{$r} = 1;
928                                         if (!defined($k)) { $k = $kind{$s}; }
929                                         $kind{$r} = $k."(".$s.")";
930                                         $algorithm{$r} = $algorithm{$s};
931                                         $platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
932                                         $platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
933                                         print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
934                                 }
935                         }
936                         print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
937                 }
938         }
939
940         # Info we know about
941
942         push @ret, map { $_."\\".&info_string($_,"EXIST",
943                                               $platform{$_},
944                                               $kind{$_},
945                                               $algorithm{$_}) } keys %syms;
946
947         if (keys %unknown_algorithms) {
948                 print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
949                 print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
950         }
951         return(@ret);
952 }
953
954 # Param: string of comma-separated platform-specs.
955 sub reduce_platforms
956 {
957         my ($platforms) = @_;
958         my $pl = defined($platforms) ? $platforms : "";
959         my %p = map { $_ => 0 } split /,/, $pl;
960         my $ret;
961
962         print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
963             if $debug;
964         # We do this, because if there's code like the following, it really
965         # means the function exists in all cases and should therefore be
966         # everywhere.  By increasing and decreasing, we may attain 0:
967         #
968         # ifndef WIN16
969         #    int foo();
970         # else
971         #    int _fat foo();
972         # endif
973         foreach $platform (split /,/, $pl) {
974                 if ($platform =~ /^!(.*)$/) {
975                         $p{$1}--;
976                 } else {
977                         $p{$platform}++;
978                 }
979         }
980         foreach $platform (keys %p) {
981                 if ($p{$platform} == 0) { delete $p{$platform}; }
982         }
983
984         delete $p{""};
985
986         $ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
987         print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
988             if $debug;
989         return $ret;
990 }
991
992 sub info_string
993 {
994         (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
995
996         my %a = defined($algorithms) ?
997             map { $_ => 1 } split /,/, $algorithms : ();
998         my $k = defined($kind) ? $kind : "FUNCTION";
999         my $ret;
1000         my $p = &reduce_platforms($platforms);
1001
1002         delete $a{""};
1003
1004         $ret = $exist;
1005         $ret .= ":".$p;
1006         $ret .= ":".$k;
1007         $ret .= ":".join(',',sort keys %a);
1008         return $ret;
1009 }
1010
1011 sub maybe_add_info
1012 {
1013         (my $name, *nums, my @symbols) = @_;
1014         my $sym;
1015         my $new_info = 0;
1016         my %syms=();
1017
1018         foreach $sym (@symbols) {
1019                 (my $s, my $i) = split /\\/, $sym;
1020                 if (defined($nums{$s})) {
1021                         $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
1022                         (my $n, my $vers, my $dummy) = split /\\/, $nums{$s};
1023                         if (!defined($dummy) || $i ne $dummy) {
1024                                 $nums{$s} = $n."\\".$vers."\\".$i;
1025                                 $new_info++;
1026                                 print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
1027                         }
1028                 }
1029                 $syms{$s} = 1;
1030         }
1031
1032         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
1033         foreach $sym (@s) {
1034                 (my $n, my $vers, my $i) = split /\\/, $nums{$sym};
1035                 if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
1036                         $new_info++;
1037                         print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
1038                 }
1039         }
1040         if ($new_info) {
1041                 print STDERR "$name: $new_info old symbols have updated info\n";
1042                 if (!$do_rewrite) {
1043                         print STDERR "You should do a rewrite to fix this.\n";
1044                 }
1045         } else {
1046         }
1047 }
1048
1049 # Param: string of comma-separated keywords, each possibly prefixed with a "!"
1050 sub is_valid
1051 {
1052         my ($keywords_txt,$platforms) = @_;
1053         my (@keywords) = split /,/,$keywords_txt;
1054         my ($falsesum, $truesum) = (0, 1);
1055
1056         # Param: one keyword
1057         sub recognise
1058         {
1059                 my ($keyword,$platforms) = @_;
1060
1061                 if ($platforms) {
1062                         # platforms
1063                         if ($keyword eq "UNIX" && $UNIX) { return 1; }
1064                         if ($keyword eq "VMS" && $VMS) { return 1; }
1065                         if ($keyword eq "WIN32" && $W32) { return 1; }
1066                         if ($keyword eq "_WIN32" && $W32) { return 1; }
1067                         if ($keyword eq "WINNT" && $NT) { return 1; }
1068                         # Special platforms:
1069                         # EXPORT_VAR_AS_FUNCTION means that global variables
1070                         # will be represented as functions.
1071                         if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && $W32) {
1072                                 return 1;
1073                         }
1074                         if ($keyword eq "ZLIB" && $zlib) { return 1; }
1075                         return 0;
1076                 } else {
1077                         # algorithms
1078                         if ($disabled_algorithms{$keyword}) { return 0;}
1079
1080                         # Nothing recognise as true
1081                         return 1;
1082                 }
1083         }
1084
1085         foreach $k (@keywords) {
1086                 if ($k =~ /^!(.*)$/) {
1087                         $falsesum += &recognise($1,$platforms);
1088                 } else {
1089                         $truesum *= &recognise($k,$platforms);
1090                 }
1091         }
1092         print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1093         return (!$falsesum) && $truesum;
1094 }
1095
1096 sub print_test_file
1097 {
1098         (*OUT,my $name,*nums,my $testall,my @symbols)=@_;
1099         my $n = 1; my @e; my @r;
1100         my $sym; my $prev = ""; my $prefSSLeay;
1101
1102         (@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1103         (@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1104         @symbols=((sort @e),(sort @r));
1105
1106         foreach $sym (@symbols) {
1107                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1108                 my $v = 0;
1109                 $v = 1 if $i=~ /^.*?:.*?:VARIABLE/;
1110                 my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1111                 my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1112                 if (!defined($nums{$s})) {
1113                         print STDERR "Warning: $s does not have a number assigned\n"
1114                             if(!$do_update);
1115                 } elsif (is_valid($p,1) && is_valid($a,0)) {
1116                         my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1117                         if ($prev eq $s2) {
1118                                 print OUT "\t/* The following has already appeared previously */\n";
1119                                 print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1120                         }
1121                         $prev = $s2;    # To warn about duplicates...
1122
1123                         (my $nn, my $vers, my $ni) = split /\\/, $nums{$s2};
1124                         if ($v) {
1125                                 print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n";
1126                         } else {
1127                                 print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n";
1128                         }
1129                 }
1130         }
1131 }
1132
1133 sub get_version
1134 {
1135    return $config{version};
1136 }
1137
1138 sub print_def_file
1139 {
1140         (*OUT,my $name,*nums,my @symbols)=@_;
1141         my $n = 1; my @e; my @r; my @v; my $prev="";
1142         my $liboptions="";
1143         my $libname = $name;
1144         my $http_vendor = 'www.openssl.org/';
1145         my $version = get_version();
1146         my $what = "OpenSSL: implementation of Secure Socket Layer";
1147         my $description = "$what $version, $name - http://$http_vendor";
1148         my $prevsymversion = "", $prevprevsymversion = "";
1149         # For VMS
1150         my $prevnum = 0;
1151         my $symvtextcount = 0;
1152
1153         if ($W32)
1154                 {
1155                 print OUT <<"EOF";
1156 ;
1157 ; Definition file for the DLL version of the $name library from OpenSSL
1158 ;
1159
1160 LIBRARY         $libname        $liboptions
1161
1162 EOF
1163
1164                 print "EXPORTS\n";
1165                 }
1166         elsif ($VMS)
1167                 {
1168                 print OUT <<"EOF";
1169 IDENTIFICATION=$version
1170 CASE_SENSITIVE=YES
1171 SYMBOL_VECTOR=(-
1172 EOF
1173                 $symvtextcount = 16; # length of "SYMBOL_VECTOR=(-"
1174                 }
1175
1176         (@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1177         (@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols);
1178         if ($VMS) {
1179             # VMS needs to have the symbols on slot number order
1180             @symbols=(map { $_->[1] }
1181                       sort { $a->[0] <=> $b->[0] }
1182                       map { (my $s, my $i) = $_ =~ /^(.*?)\\(.*)$/;
1183                             die "Error: $s doesn't have a number assigned\n"
1184                                 if !defined($nums{$s});
1185                             (my $n, my @rest) = split /\\/, $nums{$s};
1186                             [ $n, $_ ] } (@e, @r, @v));
1187         } else {
1188             @symbols=((sort @e),(sort @r), (sort @v));
1189         }
1190
1191         my ($baseversion, $currversion) = get_openssl_version();
1192         my $thisversion;
1193         do {
1194                 if (!defined($thisversion)) {
1195                         $thisversion = $baseversion;
1196                 } else {
1197                         $thisversion = get_next_version($thisversion);
1198                 }
1199                 foreach $sym (@symbols) {
1200                         (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1201                         my $v = 0;
1202                         $v = 1 if $i =~ /^.*?:.*?:VARIABLE/;
1203                         if (!defined($nums{$s})) {
1204                                 die "Error: $s does not have a number assigned\n"
1205                                         if(!$do_update);
1206                         } else {
1207                                 (my $n, my $symversion, my $dummy) = split /\\/, $nums{$s};
1208                                 my %pf = ();
1209                                 my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1210                                 my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1211                                 if (is_valid($p,1) && is_valid($a,0)) {
1212                                         my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1213                                         if ($prev eq $s2) {
1214                                                 print STDERR "Warning: Symbol '",$s2,
1215                                                         "' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),
1216                                                         ", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1217                                         }
1218                                         $prev = $s2;    # To warn about duplicates...
1219                                         if($linux) {
1220                                                 next if $symversion ne $thisversion;
1221                                                 if ($symversion ne $prevsymversion) {
1222                                                         if ($prevsymversion ne "") {
1223                                                                 if ($prevprevsymversion ne "") {
1224                                                                         print OUT "} OPENSSL${SO_VARIANT}_"
1225                                                                                                 ."$prevprevsymversion;\n\n";
1226                                                                 } else {
1227                                                                         print OUT "};\n\n";
1228                                                                 }
1229                                                         }
1230                                                         print OUT "OPENSSL${SO_VARIANT}_$symversion {\n    global:\n";
1231                                                         $prevprevsymversion = $prevsymversion;
1232                                                         $prevsymversion = $symversion;
1233                                                 }
1234                                                 print OUT "        $s2;\n";
1235                                         } elsif ($aix) {
1236                                                 print OUT "$s2\n";
1237                                         } elsif ($VMS) {
1238                                             while(++$prevnum < $n) {
1239                                                 my $symline=" ,SPARE -\n  ,SPARE -\n";
1240                                                 if ($symvtextcount + length($symline) - 2 > 1024) {
1241                                                     print OUT ")\nSYMBOL_VECTOR=(-\n";
1242                                                     $symvtextcount = 16; # length of "SYMBOL_VECTOR=(-"
1243                                                 }
1244                                                 if ($symvtextcount == 16) {
1245                                                     # Take away first comma
1246                                                     $symline =~ s/,//;
1247                                                 }
1248                                                 print OUT $symline;
1249                                                 $symvtextcount += length($symline) - 2;
1250                                             }
1251                                             (my $s_uc = $s) =~ tr/a-z/A-Z/;
1252                                             my $symtype=
1253                                                 $v ? "DATA" : "PROCEDURE";
1254                                             my $symline=
1255                                                 ($s_uc ne $s
1256                                                  ? " ,$s_uc/$s=$symtype -\n  ,$s=$symtype -\n"
1257                                                  : " ,$s=$symtype -\n  ,SPARE -\n");
1258                                             if ($symvtextcount + length($symline) - 2 > 1024) {
1259                                                 print OUT ")\nSYMBOL_VECTOR=(-\n";
1260                                                 $symvtextcount = 16; # length of "SYMBOL_VECTOR=(-"
1261                                             }
1262                                             if ($symvtextcount == 16) {
1263                                                 # Take away first comma
1264                                                 $symline =~ s/,//;
1265                                             }
1266                                             print OUT $symline;
1267                                             $symvtextcount += length($symline) - 2;
1268                                         } elsif($v) {
1269                                                 printf OUT "    %s%-39s DATA\n",
1270                                                                 ($W32)?"":"_",$s2;
1271                                         } else {
1272                                                 printf OUT "    %s%s\n",
1273                                                                 ($W32)?"":"_",$s2;
1274                                         }
1275                                 }
1276                         }
1277                 }
1278         } while ($linux && $thisversion ne $currversion);
1279         if ($linux) {
1280                 if ($prevprevsymversion ne "") {
1281                         print OUT "    local: *;\n} OPENSSL${SO_VARIANT}_$prevprevsymversion;\n\n";
1282                 } else {
1283                         print OUT "    local: *;\n};\n\n";
1284                 }
1285         } elsif ($VMS) {
1286             print OUT ")\n";
1287             (my $libvmaj, my $libvmin, my $libvedit) =
1288                 $currversion =~ /^(\d+)_(\d+)_(\d+)[a-z]{0,2}$/;
1289             # The reason to multiply the edit number with 100 is to make space
1290             # for the possibility that we want to encode the patch letters
1291             print OUT "GSMATCH=LEQUAL,",($libvmaj * 100 + $libvmin),",",($libvedit * 100),"\n";
1292         }
1293         printf OUT "\n";
1294 }
1295
1296 sub load_numbers
1297 {
1298         my($name)=@_;
1299         my(@a,%ret);
1300         my $prevversion;
1301
1302         $max_num = 0;
1303         $num_noinfo = 0;
1304         $prev = "";
1305         $prev_cnt = 0;
1306
1307         my ($baseversion, $currversion) = get_openssl_version();
1308
1309         open(IN,"<$name") || die "unable to open $name:$!\n";
1310         while (<IN>) {
1311                 s|\R$||;        # Better chomp
1312                 s/#.*$//;
1313                 next if /^\s*$/;
1314                 @a=split;
1315                 if (defined $ret{$a[0]}) {
1316                         # This is actually perfectly OK
1317                         #print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
1318                 }
1319                 if ($max_num > $a[1]) {
1320                         print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
1321                 }
1322                 elsif ($max_num == $a[1]) {
1323                         # This is actually perfectly OK
1324                         #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1325                         if ($a[0] eq $prev) {
1326                                 $prev_cnt++;
1327                                 $a[0] .= "{$prev_cnt}";
1328                         }
1329                 }
1330                 else {
1331                         $prev_cnt = 0;
1332                 }
1333                 if ($#a < 2) {
1334                         # Existence will be proven later, in do_defs
1335                         $ret{$a[0]}=$a[1];
1336                         $num_noinfo++;
1337                 } else {
1338                         #Sanity check the version number
1339                         if (defined $prevversion) {
1340                                 check_version_lte($prevversion, $a[2]);
1341                         }
1342                         check_version_lte($a[2], $currversion);
1343                         $prevversion = $a[2];
1344                         $ret{$a[0]}=$a[1]."\\".$a[2]."\\".$a[3]; # \\ 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." if $verbose || !$do_rewrite;
1351                 if ($do_rewrite) {
1352                         printf STDERR "  The rewrite will fix this.\n" if $verbose;
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 $v, 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         my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1378         my $r; my %r; my %rsyms;
1379         foreach $r (@r) {
1380                 (my $s, my $i) = split /\\/, $r;
1381                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1382                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1383                 $r{$a} = $s."\\".$i;
1384                 $rsyms{$s} = 1;
1385         }
1386
1387         my %syms = ();
1388         foreach $_ (@symbols) {
1389                 (my $n, my $i) = split /\\/;
1390                 $syms{$n} = 1;
1391         }
1392
1393         my @s=sort {
1394             &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1395             || $a cmp $b
1396         } keys %nums;
1397         foreach $sym (@s) {
1398                 (my $n, my $vers, my $i) = split /\\/, $nums{$sym};
1399                 next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
1400                 next if defined($rsyms{$sym});
1401                 print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1402                 $i="NOEXIST::FUNCTION:"
1403                         if !defined($i) || $i eq "" || !defined($syms{$sym});
1404                 my $s2 = $sym;
1405                 $s2 =~ s/\{[0-9]+\}$//;
1406                 printf OUT "%s%-39s %d\t%s\t%s\n","",$s2,$n,$vers,$i;
1407                 if (exists $r{$sym}) {
1408                         (my $s, $i) = split /\\/,$r{$sym};
1409                         my $s2 = $s;
1410                         $s2 =~ s/\{[0-9]+\}$//;
1411                         printf OUT "%s%-39s %d\t%s\t%s\n","",$s2,$n,$vers,$i;
1412                 }
1413         }
1414 }
1415
1416 sub update_numbers
1417 {
1418         (*OUT,$name,*nums,my $start_num, my @symbols)=@_;
1419         my $new_syms = 0;
1420         my $basevers;
1421         my $vers;
1422
1423         ($basevers, $vers) = get_openssl_version();
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\t%s\n","",$s2, ++$start_num,$vers,$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\t%s\n","",$s, $start_num,$vers,$i;
1450                         }
1451                 }
1452         }
1453         if($new_syms) {
1454                 print STDERR "$name: Added $new_syms new symbols\n";
1455         } else {
1456                 print STDERR "$name: 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
1482 sub count_parens
1483 {
1484         my $line = shift(@_);
1485
1486         my $open = $line =~ tr/\(//;
1487         my $close = $line =~ tr/\)//;
1488
1489         return $open - $close;
1490 }
1491
1492 #Parse opensslv.h to get the current version number. Also work out the base
1493 #version, i.e. the lowest version number that is binary compatible with this
1494 #version
1495 sub get_openssl_version()
1496 {
1497         my $fn = catfile($config{sourcedir},"include","openssl","opensslv.h");
1498         open (IN, "$fn") || die "Can't open opensslv.h";
1499
1500         while(<IN>) {
1501                 if (/OPENSSL_VERSION_TEXT\s+"OpenSSL (\d\.\d\.)(\d[a-z]*)(-| )/) {
1502                         my $suffix = $2;
1503                         (my $baseversion = $1) =~ s/\./_/g;
1504                         close IN;
1505                         return ($baseversion."0", $baseversion.$suffix);
1506                 }
1507         }
1508         die "Can't find OpenSSL version number\n";
1509 }
1510
1511 #Given an OpenSSL version number, calculate the next version number. If the
1512 #version number gets to a.b.czz then we go to a.b.(c+1)
1513 sub get_next_version()
1514 {
1515         my $thisversion = shift;
1516
1517         my ($base, $letter) = $thisversion =~ /^(\d_\d_\d)([a-z]{0,2})$/;
1518
1519         if ($letter eq "zz") {
1520                 my $lastnum = substr($base, -1);
1521                 return substr($base, 0, length($base)-1).(++$lastnum);
1522         }
1523         return $base.get_next_letter($letter);
1524 }
1525
1526 #Given the letters off the end of an OpenSSL version string, calculate what
1527 #the letters for the next release would be.
1528 sub get_next_letter()
1529 {
1530         my $thisletter = shift;
1531         my $baseletter = "";
1532         my $endletter;
1533
1534         if ($thisletter eq "") {
1535                 return "a";
1536         }
1537         if ((length $thisletter) > 1) {
1538                 ($baseletter, $endletter) = $thisletter =~ /([a-z]+)([a-z])/;
1539         } else {
1540                 $endletter = $thisletter;
1541         }
1542
1543         if ($endletter eq "z") {
1544                 return $thisletter."a";
1545         } else {
1546                 return $baseletter.(++$endletter);
1547         }
1548 }
1549
1550 #Check if a version is less than or equal to the current version. Its a fatal
1551 #error if not. They must also only differ in letters, or the last number (i.e.
1552 #the first two numbers must be the same)
1553 sub check_version_lte()
1554 {
1555         my ($testversion, $currversion) = @_;
1556         my $lentv;
1557         my $lencv;
1558         my $cvbase;
1559
1560         my ($cvnums) = $currversion =~ /^(\d_\d_\d)[a-z]*$/;
1561         my ($tvnums) = $testversion =~ /^(\d_\d_\d)[a-z]*$/;
1562
1563         #Die if we can't parse the version numbers or they don't look sane
1564         die "Invalid version number: $testversion and $currversion\n"
1565                 if (!defined($cvnums) || !defined($tvnums)
1566                         || length($cvnums) != 5
1567                         || length($tvnums) != 5);
1568
1569         #If the base versions (without letters) don't match check they only differ
1570         #in the last number
1571         if ($cvnums ne $tvnums) {
1572                 die "Invalid version number: $testversion "
1573                         ."for current version $currversion\n"
1574                         if (substr($cvnums, 0, 4) ne substr($tvnums, 0, 4));
1575                 return;
1576         }
1577         #If we get here then the base version (i.e. the numbers) are the same - they
1578         #only differ in the letters
1579
1580         $lentv = length $testversion;
1581         $lencv = length $currversion;
1582
1583         #If the testversion has more letters than the current version then it must
1584         #be later (or malformed)
1585         if ($lentv > $lencv) {
1586                 die "Invalid version number: $testversion "
1587                         ."is greater than $currversion\n";
1588         }
1589
1590         #Get the last letter from the current version
1591         my ($cvletter) = $currversion =~ /([a-z])$/;
1592         if (defined $cvletter) {
1593                 ($cvbase) = $currversion =~ /(\d_\d_\d[a-z]*)$cvletter$/;
1594         } else {
1595                 $cvbase = $currversion;
1596         }
1597         die "Unable to parse version number $currversion" if (!defined $cvbase);
1598         my $tvbase;
1599         my ($tvletter) = $testversion =~ /([a-z])$/;
1600         if (defined $tvletter) {
1601                 ($tvbase) = $testversion =~ /(\d_\d_\d[a-z]*)$tvletter$/;
1602         } else {
1603                 $tvbase = $testversion;
1604         }
1605         die "Unable to parse version number $testversion" if (!defined $tvbase);
1606
1607         if ($lencv > $lentv) {
1608                 #If current version has more letters than testversion then testversion
1609                 #minus the final letter must be a substring of the current version
1610                 die "Invalid version number $testversion "
1611                         ."is greater than $currversion or is invalid\n"
1612                         if (index($cvbase, $tvbase) != 0);
1613         } else {
1614                 #If both versions have the same number of letters then they must be
1615                 #equal up to the last letter, and the last letter in testversion must
1616                 #be less than or equal to the last letter in current version.
1617                 die "Invalid version number $testversion "
1618                         ."is greater than $currversion\n"
1619                         if (($cvbase ne $tvbase) && ($tvletter gt $cvletter));
1620         }
1621 }
1622
1623 sub do_deprecated()
1624 {
1625         my ($decl, $plats, $algs) = @_;
1626         $decl =~ /^\s*(DEPRECATEDIN_\d+_\d+_\d+)\s*\((.*)\)\s*$/
1627             or die "Bad DEPRECATEDIN: $decl\n";
1628         my $info1 .= "#INFO:";
1629         $info1 .= join(',', @{$plats}) . ":";
1630         my $info2 = $info1;
1631         $info1 .= join(',',@{$algs}, $1) . ";";
1632         $info2 .= join(',',@{$algs}) . ";";
1633         return $info1 . $2 . ";" . $info2;
1634 }