Move ZLIB from 'platforms' to 'features'
[openssl.git] / util / mknum.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 my $debug=0;
57 my $trace=0;
58 my $verbose=0;
59
60 my $crypto_num= catfile($config{sourcedir},"util","libcrypto.num");
61 my $ssl_num=    catfile($config{sourcedir},"util","libssl.num");
62 my $libname;
63
64 my $do_update = 0;
65 my $do_rewrite = 1;
66 my $do_crypto = 0;
67 my $do_ssl = 0;
68 my $do_ctest = 0;
69 my $do_ctestall = 0;
70 my $do_checkexist = 0;
71
72 my $VMS=0;
73 my $W32=0;
74 my $NT=0;
75 my $UNIX=0;
76 my $linux=0;
77 my $aix=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",
82                         "EXPORT_VAR_AS_FUNCTION", "_WIN32"
83                         );
84 my @known_ossl_platforms = ( "UNIX", "VMS", "WIN32", "WINNT", "OS2" );
85 my @known_algorithms = ( # These are algorithms we know are guarded in relevant
86                          # header files, but aren't actually disablable.
87                          # Without these, this script will warn a lot.
88                          "RSA", "MD5",
89                          # @disablables comes from configdata.pm
90                          map { (my $x = uc $_) =~ s|-|_|g; $x; } @disablables,
91                          # Deprecated functions.  Not really algorithmss, but
92                          # treated as such here for the sake of simplicity
93                          "DEPRECATEDIN_0_9_8",
94                          "DEPRECATEDIN_1_0_0",
95                          "DEPRECATEDIN_1_1_0",
96                          "DEPRECATEDIN_1_2_0",
97                      );
98
99 # %disabled comes from configdata.pm
100 my %disabled_algorithms =
101     map { (my $x = uc $_) =~ s|-|_|g; $x => 1; } keys %disabled;
102
103 my $apiv = sprintf "%x%02x%02x", split(/\./, $config{api});
104 foreach (@known_algorithms) {
105         if (/^DEPRECATEDIN_(\d+)_(\d+)_(\d+)$/) {
106                 my $depv = sprintf "%x%02x%02x", $1, $2, $3;
107                 $disabled_algorithms{$_} = 1 if $apiv ge $depv;
108         }
109 }
110
111 my $zlib;
112
113 foreach (@ARGV, split(/ /, $config{options}))
114         {
115         $debug=1 if $_ eq "debug";
116         $trace=1 if $_ eq "trace";
117         $verbose=1 if $_ eq "verbose";
118         $W32=1 if $_ eq "32";
119         die "win16 not supported" if $_ eq "16";
120         if($_ eq "NT") {
121                 $W32 = 1;
122                 $NT = 1;
123         } elsif ($_ eq "linux") {
124                 $linux=1;
125                 $UNIX=1;
126         } elsif ($_ eq "aix") {
127                 $aix=1;
128                 $UNIX=1;
129         } elsif ($_ eq "VMS") {
130                 $VMS=1;
131         }
132         if ($_ eq "zlib" || $_ eq "enable-zlib" || $_ eq "zlib-dynamic"
133                          || $_ eq "enable-zlib-dynamic") {
134                 $zlib = 1;
135         }
136
137         $do_crypto=1 if $_ eq "libcrypto" || $_ eq "crypto";
138         $do_ssl=1 if $_ eq "libssl" || $_ eq "ssl";
139
140         $do_update=1 if $_ eq "update";
141         $do_rewrite=1 if $_ eq "rewrite";
142         $do_checkexist=1 if $_ eq "exist";
143         }
144 $libname = $unified_info{sharednames}->{libcrypto} if $do_crypto;
145 $libname = $unified_info{sharednames}->{libssl} if $do_ssl;
146
147 if (!$libname) {
148         if ($do_ssl) {
149                 $libname="LIBSSL";
150         }
151         if ($do_crypto) {
152                 $libname="LIBCRYPTO";
153         }
154 }
155
156 if (!$do_ssl && !$do_crypto)
157         {
158         print STDERR "usage: $0 ( ssl | crypto )\n";
159         exit(1);
160         }
161
162 %ssl_list=&load_numbers($ssl_num);
163 $max_ssl = $max_num;
164 %crypto_list=&load_numbers($crypto_num);
165 $max_crypto = $max_num;
166
167 my $ssl="include/openssl/ssl.h";
168 $ssl.=" include/openssl/sslerr.h";
169 $ssl.=" include/openssl/tls1.h";
170 $ssl.=" include/openssl/srtp.h";
171
172 # When scanning include/openssl, skip all SSL files and some internal ones.
173 my %skipthese;
174 foreach my $f ( split(/\s+/, $ssl) ) {
175     $skipthese{$f} = 1;
176 }
177 $skipthese{'include/openssl/conf_api.h'} = 1;
178 $skipthese{'include/openssl/ebcdic.h'} = 1;
179 $skipthese{'include/openssl/opensslconf.h'} = 1;
180
181 # We use headers found in include/openssl and include/internal only.
182 # The latter is needed so libssl.so/.dll/.exe can link properly.
183 my $crypto ="include/internal/dso.h";
184 $crypto.=" include/internal/o_dir.h";
185 $crypto.=" include/internal/o_str.h";
186 $crypto.=" include/internal/err.h";
187 $crypto.=" include/internal/sslconf.h";
188 foreach my $f ( glob(catfile($config{sourcedir},'include/openssl/*.h')) ) {
189     my $fn = "include/openssl/" . lc(basename($f));
190     $crypto .= " $fn" if !defined $skipthese{$fn};
191 }
192
193 my $symhacks="include/openssl/symhacks.h";
194
195 my @ssl_symbols = &do_defs("LIBSSL", $ssl, $symhacks);
196 my @crypto_symbols = &do_defs("LIBCRYPTO", $crypto, $symhacks);
197
198 if ($do_update) {
199
200 if ($do_ssl == 1) {
201
202         &maybe_add_info("LIBSSL",*ssl_list,@ssl_symbols);
203         if ($do_rewrite == 1) {
204                 open(OUT, ">$ssl_num");
205                 &rewrite_numbers(*OUT,"LIBSSL",*ssl_list,@ssl_symbols);
206         } else {
207                 open(OUT, ">>$ssl_num");
208         }
209         &update_numbers(*OUT,"LIBSSL",*ssl_list,$max_ssl,@ssl_symbols);
210         close OUT;
211 }
212
213 if($do_crypto == 1) {
214
215         &maybe_add_info("LIBCRYPTO",*crypto_list,@crypto_symbols);
216         if ($do_rewrite == 1) {
217                 open(OUT, ">$crypto_num");
218                 &rewrite_numbers(*OUT,"LIBCRYPTO",*crypto_list,@crypto_symbols);
219         } else {
220                 open(OUT, ">>$crypto_num");
221         }
222         &update_numbers(*OUT,"LIBCRYPTO",*crypto_list,$max_crypto,@crypto_symbols);
223         close OUT;
224 }
225
226 } elsif ($do_checkexist) {
227         &check_existing(*ssl_list, @ssl_symbols)
228                 if $do_ssl == 1;
229         &check_existing(*crypto_list, @crypto_symbols)
230                 if $do_crypto == 1;
231 } else {
232         print STDERR "Nothing to do\n";
233 }
234
235
236 sub do_defs
237 {
238         my($name,$files,$symhacksfile)=@_;
239         my $file;
240         my @ret;
241         my %syms;
242         my %platform;           # For anything undefined, we assume ""
243         my %kind;               # For anything undefined, we assume "FUNCTION"
244         my %algorithm;          # For anything undefined, we assume ""
245         my %variant;
246         my %variant_cnt;        # To be able to allocate "name{n}" if "name"
247                                 # is the same name as the original.
248         my $cpp;
249         my %unknown_algorithms = ();
250         my $parens = 0;
251
252         foreach $file (split(/\s+/,$symhacksfile." ".$files))
253                 {
254                 my $fn = catfile($config{sourcedir},$file);
255                 print STDERR "DEBUG: starting on $fn:\n" if $debug;
256                 print STDERR "TRACE: start reading $fn\n" if $trace;
257                 open(IN,"<$fn") || die "Can't open $fn, $!,";
258                 my $line = "", my $def= "";
259                 my %tag = (
260                         (map { $_ => 0 } @known_platforms),
261                         (map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
262                         (map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
263                         (map { "OPENSSL_USE_".$_ => 0 } @known_algorithms),
264                         (grep /^DEPRECATED_/, @known_algorithms),
265                         NOPROTO         => 0,
266                         PERL5           => 0,
267                         _WINDLL         => 0,
268                         CONST_STRICT    => 0,
269                         TRUE            => 1,
270                         ZLIB            => 0,
271                 );
272                 my $symhacking = $file eq $symhacksfile;
273                 my @current_platforms = ();
274                 my @current_algorithms = ();
275
276                 # params: symbol, alias, platforms, kind
277                 # The reason to put this subroutine in a variable is that
278                 # it will otherwise create it's own, unshared, version of
279                 # %tag and %variant...
280                 my $make_variant = sub
281                 {
282                         my ($s, $a, $p, $k) = @_;
283                         my ($a1, $a2);
284
285                         print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
286                         if (defined($p))
287                         {
288                                 $a1 = join(",",$p,
289                                            grep(!/^$/,
290                                                 map { $tag{$_} == 1 ? $_ : "" }
291                                                 @known_platforms));
292                         }
293                         else
294                         {
295                                 $a1 = join(",",
296                                            grep(!/^$/,
297                                                 map { $tag{$_} == 1 ? $_ : "" }
298                                                 @known_platforms));
299                         }
300                         $a2 = join(",",
301                                    grep(!/^$/,
302                                         map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
303                                         @known_ossl_platforms));
304                         print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
305                         if ($a1 eq "") { $a1 = $a2; }
306                         elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
307                         if ($a eq $s)
308                         {
309                                 if (!defined($variant_cnt{$s}))
310                                 {
311                                         $variant_cnt{$s} = 0;
312                                 }
313                                 $variant_cnt{$s}++;
314                                 $a .= "{$variant_cnt{$s}}";
315                         }
316                         my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
317                         my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
318                         if (!grep(/^$togrep$/,
319                                   split(/;/, defined($variant{$s})?$variant{$s}:""))) {
320                                 if (defined($variant{$s})) { $variant{$s} .= ";"; }
321                                 $variant{$s} .= $toadd;
322                         }
323                         print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
324                 };
325
326                 print STDERR "DEBUG: parsing ----------\n" if $debug;
327                 while(<IN>) {
328                         s|\R$||; # Better chomp
329                         if($parens > 0) {
330                                 #Inside a DEPRECATEDIN
331                                 $stored_multiline .= $_;
332                                 print STDERR "DEBUG: Continuing multiline DEPRECATEDIN: $stored_multiline\n" if $debug;
333                                 $parens = count_parens($stored_multiline);
334                                 if ($parens == 0) {
335                                         $def .= do_deprecated($stored_multiline,
336                                                         \@current_platforms,
337                                                         \@current_algorithms);
338                                 }
339                                 next;
340                         }
341                         if (/\/\* Error codes for the \w+ functions\. \*\//)
342                                 {
343                                 undef @tag;
344                                 last;
345                                 }
346                         if ($line ne '') {
347                                 $_ = $line . $_;
348                                 $line = '';
349                         }
350
351                         if (/\\$/) {
352                                 $line = $`; # keep what was before the backslash
353                                 next;
354                         }
355
356                         if(/\/\*/) {
357                                 if (not /\*\//) {       # multi-line comment...
358                                         $line = $_;     # ... just accumulate
359                                         next;
360                                 } else {
361                                         s/\/\*.*?\*\///gs;# wipe it
362                                 }
363                         }
364
365                         if ($cpp) {
366                                 $cpp++ if /^#\s*if/;
367                                 $cpp-- if /^#\s*endif/;
368                                 next;
369                         }
370                         if (/^#.*ifdef.*cplusplus/) {
371                                 $cpp = 1;
372                                 next;
373                         }
374
375                         s/{[^{}]*}//gs;                      # ignore {} blocks
376                         print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
377                         print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
378                         if (/^\#\s*if\s+OPENSSL_API_COMPAT\s*(\S)\s*(0x[0-9a-fA-F]{8})L\s*$/) {
379                                 my $op = $1;
380                                 my $v = hex($2);
381                                 if ($op ne '<' && $op ne '>=') {
382                                     die "$file unacceptable operator $op: $_\n";
383                                 }
384                                 my ($one, $major, $minor) =
385                                     ( ($v >> 28) & 0xf,
386                                       ($v >> 20) & 0xff,
387                                       ($v >> 12) & 0xff );
388                                 my $t = "DEPRECATEDIN_${one}_${major}_${minor}";
389                                 push(@tag,"-");
390                                 push(@tag,$t);
391                                 $tag{$t}=($op eq '<' ? 1 : -1);
392                                 print STDERR "DEBUG: $file: found tag $t = $tag{$t}\n" if $debug;
393                         } elsif (/^\#\s*ifndef\s+(.*)/) {
394                                 push(@tag,"-");
395                                 push(@tag,$1);
396                                 $tag{$1}=-1;
397                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
398                         } elsif (/^\#\s*if\s+!defined\s*\(([^\)]+)\)/) {
399                                 push(@tag,"-");
400                                 if (/^\#\s*if\s+(!defined\s*\(([^\)]+)\)(\s+\&\&\s+!defined\s*\(([^\)]+)\))*)$/) {
401                                         my $tmp_1 = $1;
402                                         my $tmp_;
403                                         foreach $tmp_ (split '\&\&',$tmp_1) {
404                                                 $tmp_ =~ /!defined\s*\(([^\)]+)\)/;
405                                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
406                                                 push(@tag,$1);
407                                                 $tag{$1}=-1;
408                                         }
409                                 } else {
410                                         print STDERR "Warning: $file: taking only '!defined($1)' of complicated expression: $_" if $verbose; # because it is O...
411                                         print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
412                                         push(@tag,$1);
413                                         $tag{$1}=-1;
414                                 }
415                         } elsif (/^\#\s*ifdef\s+(\S*)/) {
416                                 push(@tag,"-");
417                                 push(@tag,$1);
418                                 $tag{$1}=1;
419                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
420                         } elsif (/^\#\s*if\s+defined\s*\(([^\)]+)\)/) {
421                                 push(@tag,"-");
422                                 if (/^\#\s*if\s+(defined\s*\(([^\)]+)\)(\s+\|\|\s+defined\s*\(([^\)]+)\))*)$/) {
423                                         my $tmp_1 = $1;
424                                         my $tmp_;
425                                         foreach $tmp_ (split '\|\|',$tmp_1) {
426                                                 $tmp_ =~ /defined\s*\(([^\)]+)\)/;
427                                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
428                                                 push(@tag,$1);
429                                                 $tag{$1}=1;
430                                         }
431                                 } else {
432                                         print STDERR "Warning: $file: taking only 'defined($1)' of complicated expression: $_\n" if $verbose; # because it is O...
433                                         print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
434                                         push(@tag,$1);
435                                         $tag{$1}=1;
436                                 }
437                         } elsif (/^\#\s*error\s+(\w+) is disabled\./) {
438                                 my $tag_i = $#tag;
439                                 while($tag[$tag_i] ne "-") {
440                                         if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
441                                                 $tag{$tag[$tag_i]}=2;
442                                                 print STDERR "DEBUG: $file: changed tag $1 = 2\n" if $debug;
443                                         }
444                                         $tag_i--;
445                                 }
446                         } elsif (/^\#\s*endif/) {
447                                 my $tag_i = $#tag;
448                                 while($tag_i > 0 && $tag[$tag_i] ne "-") {
449                                         my $t=$tag[$tag_i];
450                                         print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
451                                         if ($tag{$t}==2) {
452                                                 $tag{$t}=-1;
453                                         } else {
454                                                 $tag{$t}=0;
455                                         }
456                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
457                                         pop(@tag);
458                                         if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
459                                                 $t=$1;
460                                         } elsif($t =~ /^OPENSSL_USE_([A-Z0-9_]+)$/) {
461                                                 $t=$1;
462                                         } else {
463                                                 $t="";
464                                         }
465                                         if ($t ne ""
466                                             && !grep(/^$t$/, @known_algorithms)) {
467                                                 $unknown_algorithms{$t} = 1;
468                                                 #print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
469                                         }
470                                         $tag_i--;
471                                 }
472                                 pop(@tag);
473                         } elsif (/^\#\s*else/) {
474                                 my $tag_i = $#tag;
475                                 die "$file unmatched else\n" if $tag_i < 0;
476                                 while($tag[$tag_i] ne "-") {
477                                         my $t=$tag[$tag_i];
478                                         $tag{$t}= -$tag{$t};
479                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
480                                         $tag_i--;
481                                 }
482                         } elsif (/^\#\s*if\s+1/) {
483                                 push(@tag,"-");
484                                 # Dummy tag
485                                 push(@tag,"TRUE");
486                                 $tag{"TRUE"}=1;
487                                 print STDERR "DEBUG: $file: found 1\n" if $debug;
488                         } elsif (/^\#\s*if\s+0/) {
489                                 push(@tag,"-");
490                                 # Dummy tag
491                                 push(@tag,"TRUE");
492                                 $tag{"TRUE"}=-1;
493                                 print STDERR "DEBUG: $file: found 0\n" if $debug;
494                         } elsif (/^\#\s*if\s+/) {
495                                 #Some other unrecognized "if" style
496                                 push(@tag,"-");
497                                 print STDERR "Warning: $file: ignoring unrecognized expression: $_\n" if $verbose; # because it is O...
498                         } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
499                                  && $symhacking && $tag{'TRUE'} != -1) {
500                                 # This is for aliasing.  When we find an alias,
501                                 # we have to invert
502                                 &$make_variant($1,$2);
503                                 print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
504                         }
505                         if (/^\#/) {
506                                 @current_platforms =
507                                     grep(!/^$/,
508                                          map { $tag{$_} == 1 ? $_ :
509                                                    $tag{$_} == -1 ? "!".$_  : "" }
510                                          @known_platforms);
511                                 push @current_platforms
512                                     , grep(!/^$/,
513                                            map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
514                                                      $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
515                                            @known_ossl_platforms);
516                                 @current_algorithms = ();
517                                 @current_algorithms =
518                                     grep(!/^$/,
519                                          map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
520                                          @known_algorithms);
521                                 push @current_algorithms
522                                     , grep(!/^$/,
523                                          map { $tag{"OPENSSL_USE_".$_} == 1 ? $_ : "" }
524                                          @known_algorithms);
525                                 push @current_algorithms,
526                                     grep { /^DEPRECATEDIN_/ && $tag{$_} == 1 }
527                                     @known_algorithms;
528                                 push @current_algorithms, "ZLIB"
529                                     if $tag{ZLIB} == 1;
530                                 $def .=
531                                     "#INFO:"
532                                         .join(',',@current_platforms).":"
533                                             .join(',',@current_algorithms).";";
534                                 next;
535                         }
536                         if ($tag{'TRUE'} != -1) {
537                                 if (/^\s*DEFINE_STACK_OF\s*\(\s*(\w*)\s*\)/
538                                                 || /^\s*DEFINE_STACK_OF_CONST\s*\(\s*(\w*)\s*\)/) {
539                                         next;
540                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
541                                         $def .= "int d2i_$3(void);";
542                                         $def .= "int i2d_$3(void);";
543                                         # Variant for platforms that do not
544                                         # have to access global variables
545                                         # in shared libraries through functions
546                                         $def .=
547                                             "#INFO:"
548                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
549                                                     .join(',',@current_algorithms).";";
550                                         $def .= "OPENSSL_EXTERN int $2_it;";
551                                         $def .=
552                                             "#INFO:"
553                                                 .join(',',@current_platforms).":"
554                                                     .join(',',@current_algorithms).";";
555                                         # Variant for platforms that have to
556                                         # access global variables in shared
557                                         # libraries through functions
558                                         &$make_variant("$2_it","$2_it",
559                                                       "EXPORT_VAR_AS_FUNCTION",
560                                                       "FUNCTION");
561                                         next;
562                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
563                                         $def .= "int d2i_$3(void);";
564                                         $def .= "int i2d_$3(void);";
565                                         $def .= "int $3_free(void);";
566                                         $def .= "int $3_new(void);";
567                                         # Variant for platforms that do not
568                                         # have to access global variables
569                                         # in shared libraries through functions
570                                         $def .=
571                                             "#INFO:"
572                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
573                                                     .join(',',@current_algorithms).";";
574                                         $def .= "OPENSSL_EXTERN int $2_it;";
575                                         $def .=
576                                             "#INFO:"
577                                                 .join(',',@current_platforms).":"
578                                                     .join(',',@current_algorithms).";";
579                                         # Variant for platforms that have to
580                                         # access global variables in shared
581                                         # libraries through functions
582                                         &$make_variant("$2_it","$2_it",
583                                                       "EXPORT_VAR_AS_FUNCTION",
584                                                       "FUNCTION");
585                                         next;
586                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
587                                          /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
588                                         $def .= "int d2i_$1(void);";
589                                         $def .= "int i2d_$1(void);";
590                                         $def .= "int $1_free(void);";
591                                         $def .= "int $1_new(void);";
592                                         # Variant for platforms that do not
593                                         # have to access global variables
594                                         # in shared libraries through functions
595                                         $def .=
596                                             "#INFO:"
597                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
598                                                     .join(',',@current_algorithms).";";
599                                         $def .= "OPENSSL_EXTERN int $1_it;";
600                                         $def .=
601                                             "#INFO:"
602                                                 .join(',',@current_platforms).":"
603                                                     .join(',',@current_algorithms).";";
604                                         # Variant for platforms that have to
605                                         # access global variables in shared
606                                         # libraries through functions
607                                         &$make_variant("$1_it","$1_it",
608                                                       "EXPORT_VAR_AS_FUNCTION",
609                                                       "FUNCTION");
610                                         next;
611                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
612                                         $def .= "int d2i_$2(void);";
613                                         $def .= "int i2d_$2(void);";
614                                         # Variant for platforms that do not
615                                         # have to access global variables
616                                         # in shared libraries through functions
617                                         $def .=
618                                             "#INFO:"
619                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
620                                                     .join(',',@current_algorithms).";";
621                                         $def .= "OPENSSL_EXTERN int $2_it;";
622                                         $def .=
623                                             "#INFO:"
624                                                 .join(',',@current_platforms).":"
625                                                     .join(',',@current_algorithms).";";
626                                         # Variant for platforms that have to
627                                         # access global variables in shared
628                                         # libraries through functions
629                                         &$make_variant("$2_it","$2_it",
630                                                       "EXPORT_VAR_AS_FUNCTION",
631                                                       "FUNCTION");
632                                         next;
633                                 } elsif (/^\s*DECLARE_ASN1_ALLOC_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
634                                         $def .= "int $1_free(void);";
635                                         $def .= "int $1_new(void);";
636                                         next;
637                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
638                                         $def .= "int d2i_$2(void);";
639                                         $def .= "int i2d_$2(void);";
640                                         $def .= "int $2_free(void);";
641                                         $def .= "int $2_new(void);";
642                                         # Variant for platforms that do not
643                                         # have to access global variables
644                                         # in shared libraries through functions
645                                         $def .=
646                                             "#INFO:"
647                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
648                                                     .join(',',@current_algorithms).";";
649                                         $def .= "OPENSSL_EXTERN int $2_it;";
650                                         $def .=
651                                             "#INFO:"
652                                                 .join(',',@current_platforms).":"
653                                                     .join(',',@current_algorithms).";";
654                                         # Variant for platforms that have to
655                                         # access global variables in shared
656                                         # libraries through functions
657                                         &$make_variant("$2_it","$2_it",
658                                                       "EXPORT_VAR_AS_FUNCTION",
659                                                       "FUNCTION");
660                                         next;
661                                 } elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
662                                         # Variant for platforms that do not
663                                         # have to access global variables
664                                         # in shared libraries through functions
665                                         $def .=
666                                             "#INFO:"
667                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
668                                                     .join(',',@current_algorithms).";";
669                                         $def .= "OPENSSL_EXTERN int $1_it;";
670                                         $def .=
671                                             "#INFO:"
672                                                 .join(',',@current_platforms).":"
673                                                     .join(',',@current_algorithms).";";
674                                         # Variant for platforms that have to
675                                         # access global variables in shared
676                                         # libraries through functions
677                                         &$make_variant("$1_it","$1_it",
678                                                       "EXPORT_VAR_AS_FUNCTION",
679                                                       "FUNCTION");
680                                         next;
681                                 } elsif (/^\s*DECLARE_ASN1_NDEF_FUNCTION\s*\(\s*(\w*)\s*\)/) {
682                                         $def .= "int i2d_$1_NDEF(void);";
683                                 } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
684                                         next;
685                                 } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION\s*\(\s*(\w*)\s*\)/) {
686                                         $def .= "int $1_print_ctx(void);";
687                                         next;
688                                 } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
689                                         $def .= "int $2_print_ctx(void);";
690                                         next;
691                                 } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
692                                         next;
693                                 } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
694                                          /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ||
695                                          /^DECLARE_PEM_rw_const\s*\(\s*(\w*)\s*,/ ) {
696                                         $def .=
697                                             "#INFO:"
698                                                 .join(',',@current_platforms).":"
699                                                     .join(',',"STDIO",@current_algorithms).";";
700                                         $def .= "int PEM_read_$1(void);";
701                                         $def .= "int PEM_write_$1(void);";
702                                         $def .=
703                                             "#INFO:"
704                                                 .join(',',@current_platforms).":"
705                                                     .join(',',@current_algorithms).";";
706                                         # Things that are everywhere
707                                         $def .= "int PEM_read_bio_$1(void);";
708                                         $def .= "int PEM_write_bio_$1(void);";
709                                         next;
710                                 } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
711                                         /^DECLARE_PEM_write_const\s*\(\s*(\w*)\s*,/ ||
712                                          /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
713                                         $def .=
714                                             "#INFO:"
715                                                 .join(',',@current_platforms).":"
716                                                     .join(',',"STDIO",@current_algorithms).";";
717                                         $def .= "int PEM_write_$1(void);";
718                                         $def .=
719                                             "#INFO:"
720                                                 .join(',',@current_platforms).":"
721                                                     .join(',',@current_algorithms).";";
722                                         # Things that are everywhere
723                                         $def .= "int PEM_write_bio_$1(void);";
724                                         next;
725                                 } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
726                                          /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
727                                         $def .=
728                                             "#INFO:"
729                                                 .join(',',@current_platforms).":"
730                                                     .join(',',"STDIO",@current_algorithms).";";
731                                         $def .= "int PEM_read_$1(void);";
732                                         $def .=
733                                             "#INFO:"
734                                                 .join(',',@current_platforms).":"
735                                                     .join(',',"STDIO",@current_algorithms).";";
736                                         # Things that are everywhere
737                                         $def .= "int PEM_read_bio_$1(void);";
738                                         next;
739                                 } elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
740                                         # Variant for platforms that do not
741                                         # have to access global variables
742                                         # in shared libraries through functions
743                                         $def .=
744                                             "#INFO:"
745                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
746                                                     .join(',',@current_algorithms).";";
747                                         $def .= "OPENSSL_EXTERN int _shadow_$2;";
748                                         $def .=
749                                             "#INFO:"
750                                                 .join(',',@current_platforms).":"
751                                                     .join(',',@current_algorithms).";";
752                                         # Variant for platforms that have to
753                                         # access global variables in shared
754                                         # libraries through functions
755                                         &$make_variant("_shadow_$2","_shadow_$2",
756                                                       "EXPORT_VAR_AS_FUNCTION",
757                                                       "FUNCTION");
758                                 } elsif (/^\s*DEPRECATEDIN/) {
759                                         $parens = count_parens($_);
760                                         if ($parens == 0) {
761                                                 $def .= do_deprecated($_,
762                                                         \@current_platforms,
763                                                         \@current_algorithms);
764                                         } else {
765                                                 $stored_multiline = $_;
766                                                 print STDERR "DEBUG: Found multiline DEPRECATEDIN starting with: $stored_multiline\n" if $debug;
767                                                 next;
768                                         }
769                                 } elsif ($tag{'CONST_STRICT'} != 1) {
770                                         if (/\{|\/\*|\([^\)]*$/) {
771                                                 $line = $_;
772                                         } else {
773                                                 $def .= $_;
774                                         }
775                                 }
776                         }
777                 }
778                 close(IN);
779                 die "$file: Unmatched tags\n" if $#tag >= 0;
780
781                 my $algs;
782                 my $plays;
783
784                 print STDERR "DEBUG: postprocessing ----------\n" if $debug;
785                 foreach (split /;/, $def) {
786                         my $s; my $k = "FUNCTION"; my $p; my $a;
787                         s/^[\n\s]*//g;
788                         s/[\n\s]*$//g;
789                         next if(/\#undef/);
790                         next if(/typedef\W/);
791                         next if(/\#define/);
792
793                         print STDERR "TRACE: processing $_\n" if $trace && !/^\#INFO:/;
794                         # Reduce argument lists to empty ()
795                         # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
796                         my $nsubst = 1; # prevent infinite loop, e.g., on  int fn()
797                         while($nsubst && /\(.*\)/s) {
798                                 $nsubst = s/\([^\(\)]+\)/\{\}/gs;
799                                 $nsubst+= s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;  #(*f{}) -> f
800                         }
801                         # pretend as we didn't use curly braces: {} -> ()
802                         s/\{\}/\(\)/gs;
803
804                         s/STACK_OF\(\)/void/gs;
805                         s/LHASH_OF\(\)/void/gs;
806
807                         print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug;
808                         if (/^\#INFO:([^:]*):(.*)$/) {
809                                 $plats = $1;
810                                 $algs = $2;
811                                 print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug;
812                                 next;
813                         } elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) {
814                                 $s = $1;
815                                 $k = "VARIABLE";
816                                 print STDERR "DEBUG: found external variable $s\n" if $debug;
817                         } elsif (/TYPEDEF_\w+_OF/s) {
818                                 next;
819                         } elsif (/(\w+)\s*\(\).*/s) {   # first token prior [first] () is
820                                 $s = $1;                # a function name!
821                                 print STDERR "DEBUG: found function $s\n" if $debug;
822                         } elsif (/\(/ and not (/=/)) {
823                                 print STDERR "File $file: cannot parse: $_;\n";
824                                 next;
825                         } else {
826                                 next;
827                         }
828
829                         $syms{$s} = 1;
830                         $kind{$s} = $k;
831
832                         $p = $plats;
833                         $a = $algs;
834
835                         $platform{$s} =
836                             &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
837                         $algorithm{$s} .= ','.$a;
838
839                         if (defined($variant{$s})) {
840                                 foreach $v (split /;/,$variant{$s}) {
841                                         (my $r, my $p, my $k) = split(/:/,$v);
842                                         my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
843                                         $syms{$r} = 1;
844                                         if (!defined($k)) { $k = $kind{$s}; }
845                                         $kind{$r} = $k."(".$s.")";
846                                         $algorithm{$r} = $algorithm{$s};
847                                         $platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
848                                         $platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
849                                         print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
850                                 }
851                         }
852                         print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
853                 }
854         }
855
856         # Prune the returned symbols
857
858         delete $syms{"bn_dump1"};
859         $platform{"BIO_s_log"} .= ",!WIN32,!macintosh";
860
861         $platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
862         $platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
863         $platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
864         $platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
865
866         # Info we know about
867
868         push @ret, map { $_."\\".&info_string($_,"EXIST",
869                                               $platform{$_},
870                                               $kind{$_},
871                                               $algorithm{$_}) } keys %syms;
872
873         if (keys %unknown_algorithms) {
874                 print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
875                 print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
876         }
877         return(@ret);
878 }
879
880 # Param: string of comma-separated platform-specs.
881 sub reduce_platforms
882 {
883         my ($platforms) = @_;
884         my $pl = defined($platforms) ? $platforms : "";
885         my %p = map { $_ => 0 } split /,/, $pl;
886         my $ret;
887
888         print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
889             if $debug;
890         # We do this, because if there's code like the following, it really
891         # means the function exists in all cases and should therefore be
892         # everywhere.  By increasing and decreasing, we may attain 0:
893         #
894         # ifndef WIN16
895         #    int foo();
896         # else
897         #    int _fat foo();
898         # endif
899         foreach $platform (split /,/, $pl) {
900                 if ($platform =~ /^!(.*)$/) {
901                         $p{$1}--;
902                 } else {
903                         $p{$platform}++;
904                 }
905         }
906         foreach $platform (keys %p) {
907                 if ($p{$platform} == 0) { delete $p{$platform}; }
908         }
909
910         delete $p{""};
911
912         $ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
913         print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
914             if $debug;
915         return $ret;
916 }
917
918 sub info_string
919 {
920         (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
921
922         my %a = defined($algorithms) ?
923             map { $_ => 1 } split /,/, $algorithms : ();
924         my $k = defined($kind) ? $kind : "FUNCTION";
925         my $ret;
926         my $p = &reduce_platforms($platforms);
927
928         delete $a{""};
929
930         $ret = $exist;
931         $ret .= ":".$p;
932         $ret .= ":".$k;
933         $ret .= ":".join(',',sort keys %a);
934         return $ret;
935 }
936
937 sub maybe_add_info
938 {
939         (my $name, *nums, my @symbols) = @_;
940         my $sym;
941         my $new_info = 0;
942         my %syms=();
943
944         foreach $sym (@symbols) {
945                 (my $s, my $i) = split /\\/, $sym;
946                 if (defined($nums{$s})) {
947                         $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
948                         (my $n, my $vers, my $dummy) = split /\\/, $nums{$s};
949                         if (!defined($dummy) || $i ne $dummy) {
950                                 $nums{$s} = $n."\\".$vers."\\".$i;
951                                 $new_info++;
952                                 print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
953                         }
954                 }
955                 $syms{$s} = 1;
956         }
957
958         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
959         foreach $sym (@s) {
960                 (my $n, my $vers, my $i) = split /\\/, $nums{$sym};
961                 if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
962                         $new_info++;
963                         print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
964                 }
965         }
966         if ($new_info) {
967                 print STDERR "$name: $new_info old symbols have updated info\n";
968                 if (!$do_rewrite) {
969                         print STDERR "You should do a rewrite to fix this.\n";
970                 }
971         } else {
972         }
973 }
974
975 # Param: string of comma-separated keywords, each possibly prefixed with a "!"
976 sub is_valid
977 {
978         my ($keywords_txt,$platforms) = @_;
979         my (@keywords) = split /,/,$keywords_txt;
980         my ($falsesum, $truesum) = (0, 1);
981
982         # Param: one keyword
983         sub recognise
984         {
985                 my ($keyword,$platforms) = @_;
986
987                 if ($platforms) {
988                         # platforms
989                         if ($keyword eq "UNIX" && $UNIX) { return 1; }
990                         if ($keyword eq "VMS" && $VMS) { return 1; }
991                         if ($keyword eq "WIN32" && $W32) { return 1; }
992                         if ($keyword eq "_WIN32" && $W32) { return 1; }
993                         if ($keyword eq "WINNT" && $NT) { return 1; }
994                         # Special platforms:
995                         # EXPORT_VAR_AS_FUNCTION means that global variables
996                         # will be represented as functions.
997                         if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && $W32) {
998                                 return 1;
999                         }
1000                         return 0;
1001                 } else {
1002                         # algorithms
1003                         if ($disabled_algorithms{$keyword}) { return 0;}
1004                         if ($keyword eq "ZLIB" && $zlib) { return 1; }
1005                         # Nothing recognise as true
1006                         return 1;
1007                 }
1008         }
1009
1010         foreach $k (@keywords) {
1011                 if ($k =~ /^!(.*)$/) {
1012                         $falsesum += &recognise($1,$platforms);
1013                 } else {
1014                         $truesum *= &recognise($k,$platforms);
1015                 }
1016         }
1017         print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1018         return (!$falsesum) && $truesum;
1019 }
1020
1021 sub load_numbers
1022 {
1023         my($name)=@_;
1024         my(@a,%ret);
1025         my $prevversion;
1026
1027         $max_num = 0;
1028         $num_noinfo = 0;
1029         $prev = "";
1030         $prev_cnt = 0;
1031
1032         my ($baseversion, $currversion) = get_openssl_version();
1033
1034         open(IN,"<$name") || die "unable to open $name:$!\n";
1035         while (<IN>) {
1036                 s|\R$||;        # Better chomp
1037                 s/#.*$//;
1038                 next if /^\s*$/;
1039                 @a=split;
1040                 if (defined $ret{$a[0]}) {
1041                         # This is actually perfectly OK
1042                         #print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
1043                 }
1044                 if ($max_num > $a[1]) {
1045                         print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
1046                 }
1047                 elsif ($max_num == $a[1]) {
1048                         # This is actually perfectly OK
1049                         #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1050                         if ($a[0] eq $prev) {
1051                                 $prev_cnt++;
1052                                 $a[0] .= "{$prev_cnt}";
1053                         }
1054                 }
1055                 else {
1056                         $prev_cnt = 0;
1057                 }
1058                 if ($#a < 2) {
1059                         # Existence will be proven later, in do_defs
1060                         $ret{$a[0]}=$a[1];
1061                         $num_noinfo++;
1062                 } else {
1063                         #Sanity check the version number
1064                         if (defined $prevversion) {
1065                                 check_version_lte($prevversion, $a[2]);
1066                         }
1067                         check_version_lte($a[2], $currversion);
1068                         $prevversion = $a[2];
1069                         $ret{$a[0]}=$a[1]."\\".$a[2]."\\".$a[3]; # \\ is a special marker
1070                 }
1071                 $max_num = $a[1] if $a[1] > $max_num;
1072                 $prev=$a[0];
1073         }
1074         if ($num_noinfo) {
1075                 print STDERR "Warning: $num_noinfo symbols were without info." if $verbose || !$do_rewrite;
1076                 if ($do_rewrite) {
1077                         printf STDERR "  The rewrite will fix this.\n" if $verbose;
1078                 } else {
1079                         printf STDERR "  You should do a rewrite to fix this.\n";
1080                 }
1081         }
1082         close(IN);
1083         return(%ret);
1084 }
1085
1086 sub parse_number
1087 {
1088         (my $str, my $what) = @_;
1089         (my $n, my $v, my $i) = split(/\\/,$str);
1090         if ($what eq "n") {
1091                 return $n;
1092         } else {
1093                 return $i;
1094         }
1095 }
1096
1097 sub rewrite_numbers
1098 {
1099         (*OUT,$name,*nums,@symbols)=@_;
1100         my $thing;
1101
1102         my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1103         my $r; my %r; my %rsyms;
1104         foreach $r (@r) {
1105                 (my $s, my $i) = split /\\/, $r;
1106                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1107                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1108                 $r{$a} = $s."\\".$i;
1109                 $rsyms{$s} = 1;
1110         }
1111
1112         my %syms = ();
1113         foreach $_ (@symbols) {
1114                 (my $n, my $i) = split /\\/;
1115                 $syms{$n} = 1;
1116         }
1117
1118         my @s=sort {
1119             &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1120             || $a cmp $b
1121         } keys %nums;
1122         foreach $sym (@s) {
1123                 (my $n, my $vers, my $i) = split /\\/, $nums{$sym};
1124                 next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
1125                 next if defined($rsyms{$sym});
1126                 print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1127                 $i="NOEXIST::FUNCTION:"
1128                         if !defined($i) || $i eq "" || !defined($syms{$sym});
1129                 my $s2 = $sym;
1130                 $s2 =~ s/\{[0-9]+\}$//;
1131                 printf OUT "%s%-39s %d\t%s\t%s\n","",$s2,$n,$vers,$i;
1132                 if (exists $r{$sym}) {
1133                         (my $s, $i) = split /\\/,$r{$sym};
1134                         my $s2 = $s;
1135                         $s2 =~ s/\{[0-9]+\}$//;
1136                         printf OUT "%s%-39s %d\t%s\t%s\n","",$s2,$n,$vers,$i;
1137                 }
1138         }
1139 }
1140
1141 sub update_numbers
1142 {
1143         (*OUT,$name,*nums,my $start_num, my @symbols)=@_;
1144         my $new_syms = 0;
1145         my $basevers;
1146         my $vers;
1147
1148         ($basevers, $vers) = get_openssl_version();
1149
1150         my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1151         my $r; my %r; my %rsyms;
1152         foreach $r (@r) {
1153                 (my $s, my $i) = split /\\/, $r;
1154                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1155                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1156                 $r{$a} = $s."\\".$i;
1157                 $rsyms{$s} = 1;
1158         }
1159
1160         foreach $sym (@symbols) {
1161                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1162                 next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
1163                 next if defined($rsyms{$sym});
1164                 die "ERROR: Symbol $sym had no info attached to it."
1165                     if $i eq "";
1166                 if (!exists $nums{$s}) {
1167                         $new_syms++;
1168                         my $s2 = $s;
1169                         $s2 =~ s/\{[0-9]+\}$//;
1170                         printf OUT "%s%-39s %d\t%s\t%s\n","",$s2, ++$start_num,$vers,$i;
1171                         if (exists $r{$s}) {
1172                                 ($s, $i) = split /\\/,$r{$s};
1173                                 $s =~ s/\{[0-9]+\}$//;
1174                                 printf OUT "%s%-39s %d\t%s\t%s\n","",$s, $start_num,$vers,$i;
1175                         }
1176                 }
1177         }
1178         if($new_syms) {
1179                 print STDERR "$name: Added $new_syms new symbols\n";
1180         } else {
1181                 print STDERR "$name: No new symbols added\n";
1182         }
1183 }
1184
1185 sub check_existing
1186 {
1187         (*nums, my @symbols)=@_;
1188         my %existing; my @remaining;
1189         @remaining=();
1190         foreach $sym (@symbols) {
1191                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1192                 $existing{$s}=1;
1193         }
1194         foreach $sym (keys %nums) {
1195                 if (!exists $existing{$sym}) {
1196                         push @remaining, $sym;
1197                 }
1198         }
1199         if(@remaining) {
1200                 print STDERR "The following symbols do not seem to exist:\n";
1201                 foreach $sym (@remaining) {
1202                         print STDERR "\t",$sym,"\n";
1203                 }
1204         }
1205 }
1206
1207 sub count_parens
1208 {
1209         my $line = shift(@_);
1210
1211         my $open = $line =~ tr/\(//;
1212         my $close = $line =~ tr/\)//;
1213
1214         return $open - $close;
1215 }
1216
1217 #Parse opensslv.h to get the current version number. Also work out the base
1218 #version, i.e. the lowest version number that is binary compatible with this
1219 #version
1220 sub get_openssl_version()
1221 {
1222         my $fn = catfile($config{sourcedir},"include","openssl","opensslv.h");
1223         open (IN, "$fn") || die "Can't open opensslv.h";
1224
1225         while(<IN>) {
1226                 if (/OPENSSL_VERSION_TEXT\s+"OpenSSL (\d\.\d\.)(\d[a-z]*)(-| )/) {
1227                         my $suffix = $2;
1228                         (my $baseversion = $1) =~ s/\./_/g;
1229                         close IN;
1230                         return ($baseversion."0", $baseversion.$suffix);
1231                 }
1232         }
1233         die "Can't find OpenSSL version number\n";
1234 }
1235
1236 #Given an OpenSSL version number, calculate the next version number. If the
1237 #version number gets to a.b.czz then we go to a.b.(c+1)
1238 sub get_next_version()
1239 {
1240         my $thisversion = shift;
1241
1242         my ($base, $letter) = $thisversion =~ /^(\d_\d_\d)([a-z]{0,2})$/;
1243
1244         if ($letter eq "zz") {
1245                 my $lastnum = substr($base, -1);
1246                 return substr($base, 0, length($base)-1).(++$lastnum);
1247         }
1248         return $base.get_next_letter($letter);
1249 }
1250
1251 #Given the letters off the end of an OpenSSL version string, calculate what
1252 #the letters for the next release would be.
1253 sub get_next_letter()
1254 {
1255         my $thisletter = shift;
1256         my $baseletter = "";
1257         my $endletter;
1258
1259         if ($thisletter eq "") {
1260                 return "a";
1261         }
1262         if ((length $thisletter) > 1) {
1263                 ($baseletter, $endletter) = $thisletter =~ /([a-z]+)([a-z])/;
1264         } else {
1265                 $endletter = $thisletter;
1266         }
1267
1268         if ($endletter eq "z") {
1269                 return $thisletter."a";
1270         } else {
1271                 return $baseletter.(++$endletter);
1272         }
1273 }
1274
1275 #Check if a version is less than or equal to the current version. Its a fatal
1276 #error if not. They must also only differ in letters, or the last number (i.e.
1277 #the first two numbers must be the same)
1278 sub check_version_lte()
1279 {
1280         my ($testversion, $currversion) = @_;
1281         my $lentv;
1282         my $lencv;
1283         my $cvbase;
1284
1285         my ($cvnums) = $currversion =~ /^(\d_\d_\d)[a-z]*$/;
1286         my ($tvnums) = $testversion =~ /^(\d_\d_\d)[a-z]*$/;
1287
1288         #Die if we can't parse the version numbers or they don't look sane
1289         die "Invalid version number: $testversion and $currversion\n"
1290                 if (!defined($cvnums) || !defined($tvnums)
1291                         || length($cvnums) != 5
1292                         || length($tvnums) != 5);
1293
1294         #If the base versions (without letters) don't match check they only differ
1295         #in the last number
1296         if ($cvnums ne $tvnums) {
1297                 die "Invalid version number: $testversion "
1298                         ."for current version $currversion\n"
1299                         if (substr($cvnums, 0, 4) ne substr($tvnums, 0, 4));
1300                 return;
1301         }
1302         #If we get here then the base version (i.e. the numbers) are the same - they
1303         #only differ in the letters
1304
1305         $lentv = length $testversion;
1306         $lencv = length $currversion;
1307
1308         #If the testversion has more letters than the current version then it must
1309         #be later (or malformed)
1310         if ($lentv > $lencv) {
1311                 die "Invalid version number: $testversion "
1312                         ."is greater than $currversion\n";
1313         }
1314
1315         #Get the last letter from the current version
1316         my ($cvletter) = $currversion =~ /([a-z])$/;
1317         if (defined $cvletter) {
1318                 ($cvbase) = $currversion =~ /(\d_\d_\d[a-z]*)$cvletter$/;
1319         } else {
1320                 $cvbase = $currversion;
1321         }
1322         die "Unable to parse version number $currversion" if (!defined $cvbase);
1323         my $tvbase;
1324         my ($tvletter) = $testversion =~ /([a-z])$/;
1325         if (defined $tvletter) {
1326                 ($tvbase) = $testversion =~ /(\d_\d_\d[a-z]*)$tvletter$/;
1327         } else {
1328                 $tvbase = $testversion;
1329         }
1330         die "Unable to parse version number $testversion" if (!defined $tvbase);
1331
1332         if ($lencv > $lentv) {
1333                 #If current version has more letters than testversion then testversion
1334                 #minus the final letter must be a substring of the current version
1335                 die "Invalid version number $testversion "
1336                         ."is greater than $currversion or is invalid\n"
1337                         if (index($cvbase, $tvbase) != 0);
1338         } else {
1339                 #If both versions have the same number of letters then they must be
1340                 #equal up to the last letter, and the last letter in testversion must
1341                 #be less than or equal to the last letter in current version.
1342                 die "Invalid version number $testversion "
1343                         ."is greater than $currversion\n"
1344                         if (($cvbase ne $tvbase) && ($tvletter gt $cvletter));
1345         }
1346 }
1347
1348 sub do_deprecated()
1349 {
1350         my ($decl, $plats, $algs) = @_;
1351         $decl =~ /^\s*(DEPRECATEDIN_\d+_\d+_\d+)\s*\((.*)\)\s*$/
1352             or die "Bad DEPRECATEDIN: $decl\n";
1353         my $info1 .= "#INFO:";
1354         $info1 .= join(',', @{$plats}) . ":";
1355         my $info2 = $info1;
1356         $info1 .= join(',',@{$algs}, $1) . ";";
1357         $info2 .= join(',',@{$algs}) . ";";
1358         return $info1 . $2 . ";" . $info2;
1359 }