Update license year range to 2016
[openssl.git] / util / mkdef.pl
1 #!/usr/local/bin/perl -w
2 #
3 # generate a .def file
4 #
5 # It does this by parsing the header files and looking for the
6 # prototyped functions: it then prunes the output.
7 #
8 # Intermediary files are created, call libeay.num and ssleay.num,
9 # The format of these files is:
10 #
11 #       routine-name    nnnn    vers    info
12 #
13 # The "nnnn" and "vers" fields are the numeric id and version for the symbol
14 # respectively. The "info" part is actually a colon-separated string of fields
15 # with the following meaning:
16 #
17 #       existence:platform:kind:algorithms
18 #
19 # - "existence" can be "EXIST" or "NOEXIST" depending on if the symbol is
20 #   found somewhere in the source, 
21 # - "platforms" is empty if it exists on all platforms, otherwise it contains
22 #   comma-separated list of the platform, just as they are if the symbol exists
23 #   for those platforms, or prepended with a "!" if not.  This helps resolve
24 #   symbol name variants for platforms where the names are too long for the
25 #   compiler or linker, or if the systems is case insensitive and there is a
26 #   clash, or the symbol is implemented differently (see
27 #   EXPORT_VAR_AS_FUNCTION).  This script assumes renaming of symbols is found
28 #   in the file crypto/symhacks.h.
29 #   The semantics for the platforms is that every item is checked against the
30 #   environment.  For the negative items ("!FOO"), if any of them is false
31 #   (i.e. "FOO" is true) in the environment, the corresponding symbol can't be
32 #   used.  For the positive itms, if all of them are false in the environment,
33 #   the corresponding symbol can't be used.  Any combination of positive and
34 #   negative items are possible, and of course leave room for some redundancy.
35 # - "kind" is "FUNCTION" or "VARIABLE".  The meaning of that is obvious.
36 # - "algorithms" is a comma-separated list of algorithm names.  This helps
37 #   exclude symbols that are part of an algorithm that some user wants to
38 #   exclude.
39 #
40
41 my $debug=0;
42
43 my $crypto_num= "util/libeay.num";
44 my $ssl_num=    "util/ssleay.num";
45 my $libname;
46
47 my $do_update = 0;
48 my $do_rewrite = 1;
49 my $do_crypto = 0;
50 my $do_ssl = 0;
51 my $do_ctest = 0;
52 my $do_ctestall = 0;
53 my $do_checkexist = 0;
54
55 my $VMSVAX=0;
56 my $VMSNonVAX=0;
57 my $VMS=0;
58 my $W32=0;
59 my $NT=0;
60 my $OS2=0;
61 my $linux=0;
62 # Set this to make typesafe STACK definitions appear in DEF
63 my $safe_stack_def = 0;
64
65 my @known_platforms = ( "__FreeBSD__", "PERL5",
66                         "EXPORT_VAR_AS_FUNCTION", "ZLIB"
67                         );
68 my @known_ossl_platforms = ( "VMS", "WIN32", "WINNT", "OS2" );
69 my @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
70                          "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1",
71                          "SHA256", "SHA512", "RMD160",
72                          "MDC2", "WHIRLPOOL", "RSA", "DSA", "DH", "EC", "EC2M",
73                          "HMAC", "AES", "CAMELLIA", "SEED", "GOST",
74                          "SCRYPT", "CHACHA", "POLY1305",
75                          # EC_NISTP_64_GCC_128
76                          "EC_NISTP_64_GCC_128",
77                          # Envelope "algorithms"
78                          "EVP", "X509", "ASN1_TYPEDEFS",
79                          # Helper "algorithms"
80                          "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
81                          "LOCKING",
82                          # External "algorithms"
83                          "FP_API", "STDIO", "SOCK", "DGRAM",
84                          "CRYPTO_MDEBUG",
85                          # Engines
86                          "STATIC_ENGINE", "ENGINE", "HW", "GMP",
87                          # Entropy Gathering
88                          "EGD",
89                          # X.509v3 Signed Certificate Timestamps
90                          "SCT",
91                          # RFC3779
92                          "RFC3779",
93                          # TLS
94                          "PSK", "SRP", "HEARTBEATS",
95                          # CMS
96                          "CMS",
97                          # CryptoAPI Engine
98                          "CAPIENG",
99                          # SSL v3 method
100                          "SSL3_METHOD",
101                          # JPAKE
102                          "JPAKE",
103                          # NEXTPROTONEG
104                          "NEXTPROTONEG",
105                          # Deprecated functions
106                          "DEPRECATEDIN_0_9_8",
107                          "DEPRECATEDIN_1_0_0",
108                          "DEPRECATEDIN_1_1_0",
109                          # SCTP
110                          "SCTP",
111                          # SRTP
112                          "SRTP",
113                          # SSL TRACE
114                          "SSL_TRACE",
115                          # Unit testing
116                          "UNIT_TEST",
117                          # OCB mode
118                          "OCB",
119                          # APPLINK (win build feature?)
120                          "APPLINK"
121                      );
122
123 my %disabled_algorithms;
124
125 foreach (@known_algorithms) {
126     $disabled_algorithms{$_} = 0;
127 }
128 # disabled by default
129 $disabled_algorithms{"STATIC_ENGINE"} = 1;
130
131 my $options="";
132 open(IN,"<Makefile") || die "unable to open Makefile!\n";
133 while(<IN>) {
134     $options=$1 if (/^OPTIONS=(.*)$/);
135 }
136 close(IN);
137
138 my $zlib;
139
140 foreach (@ARGV, split(/ /, $options))
141         {
142         $debug=1 if $_ eq "debug";
143         $W32=1 if $_ eq "32";
144         die "win16 not supported" if $_ eq "16";
145         if($_ eq "NT") {
146                 $W32 = 1;
147                 $NT = 1;
148         }
149         if ($_ eq "VMS-VAX") {
150                 $VMS=1;
151                 $VMSVAX=1;
152         }
153         if ($_ eq "VMS-NonVAX") {
154                 $VMS=1;
155                 $VMSNonVAX=1;
156         }
157         if ($_ eq "linux") {
158                 $linux=1;
159         }
160         $VMS=$VMSNonVAX=1 if $_ eq "VMS";
161         $OS2=1 if $_ eq "OS2";
162         if ($_ eq "zlib" || $_ eq "enable-zlib" || $_ eq "zlib-dynamic"
163                          || $_ eq "enable-zlib-dynamic") {
164                 $zlib = 1;
165         }
166
167         $do_ssl=1 if $_ eq "ssleay";
168         if ($_ eq "ssl") {
169                 $do_ssl=1; 
170                 $libname=$_
171         }
172         $do_crypto=1 if $_ eq "libeay";
173         if ($_ eq "crypto") {
174                 $do_crypto=1;
175                 $libname=$_;
176         }
177         $do_update=1 if $_ eq "update";
178         $do_rewrite=1 if $_ eq "rewrite";
179         $do_ctest=1 if $_ eq "ctest";
180         $do_ctestall=1 if $_ eq "ctestall";
181         $do_checkexist=1 if $_ eq "exist";
182         #$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
183         if (/^--api=(\d+)\.(\d+)\.(\d+)$/) {
184                 my $apiv = sprintf "%x%02x%02x", $1, $2, $3;
185                 foreach (keys %disabled_algorithms) {
186                         if (/^DEPRECATEDIN_(\d+)_(\d+)_(\d+)$/) {
187                                 my $depv = sprintf "%x%02x%02x", $1, $2, $3;
188                                 $disabled_algorithms{$_} = 1 if $apiv ge $depv;
189                         }
190                 }
191         }
192         if (/^no-deprecated$/) {
193                 foreach (keys %disabled_algorithms) {
194                         if (/^DEPRECATEDIN_/) {
195                                 $disabled_algorithms{$_} = 1;
196                         }
197                 }
198         }
199         elsif (/^(enable|disable|no)-(.*)$/) {
200                 my $alg = uc $2;
201         $alg =~ tr/-/_/;
202                 if (exists $disabled_algorithms{$alg}) {
203                         $disabled_algorithms{$alg} = $1 eq "enable" ? 0 : 1;
204                 }
205         }
206
207         }
208
209 if (!$libname) { 
210         if ($do_ssl) {
211                 $libname="SSLEAY";
212         }
213         if ($do_crypto) {
214                 $libname="LIBEAY";
215         }
216 }
217
218 # If no platform is given, assume WIN32
219 if ($W32 + $VMS + $OS2 + $linux == 0) {
220         $W32 = 1;
221 }
222 die "Please, only one platform at a time"
223     if ($W32 + $VMS + $OS2 + $linux > 1);
224
225 if (!$do_ssl && !$do_crypto)
226         {
227         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 | linux | VMS ]\n";
228         exit(1);
229         }
230
231 %ssl_list=&load_numbers($ssl_num);
232 $max_ssl = $max_num;
233 %crypto_list=&load_numbers($crypto_num);
234 $max_crypto = $max_num;
235
236 my $ssl="include/openssl/ssl.h";
237 $ssl.=" include/openssl/tls1.h";
238 $ssl.=" include/openssl/srtp.h";
239
240 my $crypto ="include/openssl/crypto.h";
241 $crypto.=" crypto/include/internal/cryptlib.h";
242 $crypto.=" crypto/include/internal/chacha.h"; # unless $no_chacha;
243 $crypto.=" crypto/include/internal/poly1305.h"; # unless $no_poly1305;
244 $crypto.=" include/internal/o_dir.h";
245 $crypto.=" include/internal/o_str.h";
246 $crypto.=" include/openssl/des.h" ; # unless $no_des;
247 $crypto.=" include/openssl/idea.h" ; # unless $no_idea;
248 $crypto.=" include/openssl/rc4.h" ; # unless $no_rc4;
249 $crypto.=" include/openssl/rc5.h" ; # unless $no_rc5;
250 $crypto.=" include/openssl/rc2.h" ; # unless $no_rc2;
251 $crypto.=" include/openssl/blowfish.h" ; # unless $no_bf;
252 $crypto.=" include/openssl/cast.h" ; # unless $no_cast;
253 $crypto.=" include/openssl/whrlpool.h" ;
254 $crypto.=" include/openssl/md2.h" ; # unless $no_md2;
255 $crypto.=" include/openssl/md4.h" ; # unless $no_md4;
256 $crypto.=" include/openssl/md5.h" ; # unless $no_md5;
257 $crypto.=" include/openssl/mdc2.h" ; # unless $no_mdc2;
258 $crypto.=" include/openssl/sha.h" ; # unless $no_sha;
259 $crypto.=" include/openssl/ripemd.h" ; # unless $no_ripemd;
260 $crypto.=" include/openssl/aes.h" ; # unless $no_aes;
261 $crypto.=" include/openssl/camellia.h" ; # unless $no_camellia;
262 $crypto.=" include/openssl/seed.h"; # unless $no_seed;
263
264 $crypto.=" include/openssl/bn.h";
265 $crypto.=" include/openssl/rsa.h" ; # unless $no_rsa;
266 $crypto.=" include/openssl/dsa.h" ; # unless $no_dsa;
267 $crypto.=" include/openssl/dh.h" ; # unless $no_dh;
268 $crypto.=" include/openssl/ec.h" ; # unless $no_ec;
269 $crypto.=" include/openssl/hmac.h" ; # unless $no_hmac;
270 $crypto.=" include/openssl/cmac.h" ;
271
272 $crypto.=" include/openssl/engine.h"; # unless $no_engine;
273 $crypto.=" include/openssl/stack.h" ; # unless $no_stack;
274 $crypto.=" include/openssl/buffer.h" ; # unless $no_buffer;
275 $crypto.=" include/openssl/bio.h" ; # unless $no_bio;
276 $crypto.=" include/openssl/dso.h" ; # unless $no_dso;
277 $crypto.=" include/openssl/lhash.h" ; # unless $no_lhash;
278 $crypto.=" include/openssl/conf.h";
279 $crypto.=" include/openssl/txt_db.h";
280
281 $crypto.=" include/openssl/evp.h" ; # unless $no_evp;
282 $crypto.=" include/openssl/objects.h";
283 $crypto.=" include/openssl/pem.h";
284 #$crypto.=" include/openssl/meth.h";
285 $crypto.=" include/openssl/asn1.h";
286 $crypto.=" include/openssl/asn1t.h";
287 $crypto.=" include/openssl/err.h" ; # unless $no_err;
288 $crypto.=" include/openssl/pkcs7.h";
289 $crypto.=" include/openssl/pkcs12.h";
290 $crypto.=" include/openssl/x509.h";
291 $crypto.=" include/openssl/x509_vfy.h";
292 $crypto.=" include/openssl/x509v3.h";
293 $crypto.=" include/openssl/ts.h";
294 $crypto.=" include/openssl/rand.h";
295 $crypto.=" include/openssl/comp.h" ; # unless $no_comp;
296 $crypto.=" include/openssl/ocsp.h";
297 $crypto.=" include/openssl/ui.h";
298 #$crypto.=" include/openssl/store.h";
299 $crypto.=" include/openssl/pqueue.h";
300 $crypto.=" include/openssl/cms.h";
301 $crypto.=" include/openssl/jpake.h";
302 $crypto.=" include/openssl/srp.h";
303 $crypto.=" include/openssl/modes.h";
304 $crypto.=" include/openssl/async.h";
305
306 my $symhacks="include/openssl/symhacks.h";
307
308 my @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
309 my @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
310
311 if ($do_update) {
312
313 if ($do_ssl == 1) {
314
315         &maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
316         if ($do_rewrite == 1) {
317                 open(OUT, ">$ssl_num");
318                 &rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
319         } else {
320                 open(OUT, ">>$ssl_num");
321         }
322         &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
323         close OUT;
324 }
325
326 if($do_crypto == 1) {
327
328         &maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
329         if ($do_rewrite == 1) {
330                 open(OUT, ">$crypto_num");
331                 &rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
332         } else {
333                 open(OUT, ">>$crypto_num");
334         }
335         &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
336         close OUT;
337
338
339 } elsif ($do_checkexist) {
340         &check_existing(*ssl_list, @ssl_symbols)
341                 if $do_ssl == 1;
342         &check_existing(*crypto_list, @crypto_symbols)
343                 if $do_crypto == 1;
344 } elsif ($do_ctest || $do_ctestall) {
345
346         print <<"EOF";
347
348 /* Test file to check all DEF file symbols are present by trying
349  * to link to all of them. This is *not* intended to be run!
350  */
351
352 int main()
353 {
354 EOF
355         &print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
356                 if $do_ssl == 1;
357
358         &print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
359                 if $do_crypto == 1;
360
361         print "}\n";
362
363 } else {
364
365         &print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols)
366                 if $do_ssl == 1;
367
368         &print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols)
369                 if $do_crypto == 1;
370
371 }
372
373
374 sub do_defs
375 {
376         my($name,$files,$symhacksfile)=@_;
377         my $file;
378         my @ret;
379         my %syms;
380         my %platform;           # For anything undefined, we assume ""
381         my %kind;               # For anything undefined, we assume "FUNCTION"
382         my %algorithm;          # For anything undefined, we assume ""
383         my %variant;
384         my %variant_cnt;        # To be able to allocate "name{n}" if "name"
385                                 # is the same name as the original.
386         my $cpp;
387         my %unknown_algorithms = ();
388         my $parens = 0;
389
390         foreach $file (split(/\s+/,$symhacksfile." ".$files))
391                 {
392                 print STDERR "DEBUG: starting on $file:\n" if $debug;
393                 open(IN,"<$file") || die "unable to open $file:$!\n";
394                 my $line = "", my $def= "";
395                 my %tag = (
396                         (map { $_ => 0 } @known_platforms),
397                         (map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
398                         (map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
399                         (map { "OPENSSL_USE_".$_ => 0 } @known_algorithms),
400                         NOPROTO         => 0,
401                         PERL5           => 0,
402                         _WINDLL         => 0,
403                         CONST_STRICT    => 0,
404                         TRUE            => 1,
405                 );
406                 my $symhacking = $file eq $symhacksfile;
407                 my @current_platforms = ();
408                 my @current_algorithms = ();
409
410                 # params: symbol, alias, platforms, kind
411                 # The reason to put this subroutine in a variable is that
412                 # it will otherwise create it's own, unshared, version of
413                 # %tag and %variant...
414                 my $make_variant = sub
415                 {
416                         my ($s, $a, $p, $k) = @_;
417                         my ($a1, $a2);
418
419                         print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
420                         if (defined($p))
421                         {
422                                 $a1 = join(",",$p,
423                                            grep(!/^$/,
424                                                 map { $tag{$_} == 1 ? $_ : "" }
425                                                 @known_platforms));
426                         }
427                         else
428                         {
429                                 $a1 = join(",",
430                                            grep(!/^$/,
431                                                 map { $tag{$_} == 1 ? $_ : "" }
432                                                 @known_platforms));
433                         }
434                         $a2 = join(",",
435                                    grep(!/^$/,
436                                         map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
437                                         @known_ossl_platforms));
438                         print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
439                         if ($a1 eq "") { $a1 = $a2; }
440                         elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
441                         if ($a eq $s)
442                         {
443                                 if (!defined($variant_cnt{$s}))
444                                 {
445                                         $variant_cnt{$s} = 0;
446                                 }
447                                 $variant_cnt{$s}++;
448                                 $a .= "{$variant_cnt{$s}}";
449                         }
450                         my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
451                         my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
452                         if (!grep(/^$togrep$/,
453                                   split(/;/, defined($variant{$s})?$variant{$s}:""))) {
454                                 if (defined($variant{$s})) { $variant{$s} .= ";"; }
455                                 $variant{$s} .= $toadd;
456                         }
457                         print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
458                 };
459
460                 print STDERR "DEBUG: parsing ----------\n" if $debug;
461                 while(<IN>) {
462                         if($parens > 0) {
463                                 #Inside a DEPRECATEDIN
464                                 $stored_multiline .= $_;
465                                 chomp $stored_multiline;
466                                 print STDERR "DEBUG: Continuing multiline DEPRECATEDIN: $stored_multiline\n" if $debug;
467                                 $parens = count_parens($stored_multiline);
468                                 if ($parens == 0) {
469                                         $def .= do_deprecated($stored_multiline,
470                                                         \@current_platforms,
471                                                         \@current_algorithms);
472                                 }
473                                 next;
474                         }
475                         if (/\/\* Error codes for the \w+ functions\. \*\//)
476                                 {
477                                 undef @tag;
478                                 last;
479                                 }
480                         if ($line ne '') {
481                                 $_ = $line . $_;
482                                 $line = '';
483                         }
484
485                         if (/\\$/) {
486                                 chomp; # remove eol
487                                 chop; # remove ending backslash
488                                 $line = $_;
489                                 next;
490                         }
491
492                         if(/\/\*/) {
493                                 if (not /\*\//) {       # multiline comment...
494                                         $line = $_;     # ... just accumulate
495                                         next;
496                                 } else {
497                                         s/\/\*.*?\*\///gs;# wipe it
498                                 }
499                         }
500
501                         if ($cpp) {
502                                 $cpp++ if /^#\s*if/;
503                                 $cpp-- if /^#\s*endif/;
504                                 next;
505                         }
506                         $cpp = 1 if /^#.*ifdef.*cplusplus/;
507
508                         s/{[^{}]*}//gs;                      # ignore {} blocks
509                         print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
510                         print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
511                         if (/^\#\s*ifndef\s+(.*)/) {
512                                 push(@tag,"-");
513                                 push(@tag,$1);
514                                 $tag{$1}=-1;
515                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
516                         } elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
517                                 push(@tag,"-");
518                                 if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) {
519                                         my $tmp_1 = $1;
520                                         my $tmp_;
521                                         foreach $tmp_ (split '\&\&',$tmp_1) {
522                                                 $tmp_ =~ /!defined\(([^\)]+)\)/;
523                                                 print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
524                                                 push(@tag,$1);
525                                                 $tag{$1}=-1;
526                                         }
527                                 } else {
528                                         print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O...
529                                         print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
530                                         push(@tag,$1);
531                                         $tag{$1}=-1;
532                                 }
533                         } elsif (/^\#\s*ifdef\s+(\S*)/) {
534                                 push(@tag,"-");
535                                 push(@tag,$1);
536                                 $tag{$1}=1;
537                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
538                         } elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
539                                 push(@tag,"-");
540                                 if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) {
541                                         my $tmp_1 = $1;
542                                         my $tmp_;
543                                         foreach $tmp_ (split '\|\|',$tmp_1) {
544                                                 $tmp_ =~ /defined\(([^\)]+)\)/;
545                                                 print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
546                                                 push(@tag,$1);
547                                                 $tag{$1}=1;
548                                         }
549                                 } else {
550                                         print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O...
551                                         print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
552                                         push(@tag,$1);
553                                         $tag{$1}=1;
554                                 }
555                         } elsif (/^\#\s*error\s+(\w+) is disabled\./) {
556                                 my $tag_i = $#tag;
557                                 while($tag[$tag_i] ne "-") {
558                                         if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
559                                                 $tag{$tag[$tag_i]}=2;
560                                                 print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug;
561                                         }
562                                         $tag_i--;
563                                 }
564                         } elsif (/^\#\s*endif/) {
565                                 my $tag_i = $#tag;
566                                 while($tag_i > 0 && $tag[$tag_i] ne "-") {
567                                         my $t=$tag[$tag_i];
568                                         print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
569                                         if ($tag{$t}==2) {
570                                                 $tag{$t}=-1;
571                                         } else {
572                                                 $tag{$t}=0;
573                                         }
574                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
575                                         pop(@tag);
576                                         if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
577                                                 $t=$1;
578                                         } elsif($t =~ /^OPENSSL_USE_([A-Z0-9_]+)$/) {
579                                                 $t=$1;
580                                         } else {
581                                                 $t="";
582                                         }
583                                         if ($t ne ""
584                                             && !grep(/^$t$/, @known_algorithms)) {
585                                                 $unknown_algorithms{$t} = 1;
586                                                 #print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
587                                         }
588                                         $tag_i--;
589                                 }
590                                 pop(@tag);
591                         } elsif (/^\#\s*else/) {
592                                 my $tag_i = $#tag;
593                                 while($tag[$tag_i] ne "-") {
594                                         my $t=$tag[$tag_i];
595                                         $tag{$t}= -$tag{$t};
596                                         print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
597                                         $tag_i--;
598                                 }
599                         } elsif (/^\#\s*if\s+1/) {
600                                 push(@tag,"-");
601                                 # Dummy tag
602                                 push(@tag,"TRUE");
603                                 $tag{"TRUE"}=1;
604                                 print STDERR "DEBUG: $file: found 1\n" if $debug;
605                         } elsif (/^\#\s*if\s+0/) {
606                                 push(@tag,"-");
607                                 # Dummy tag
608                                 push(@tag,"TRUE");
609                                 $tag{"TRUE"}=-1;
610                                 print STDERR "DEBUG: $file: found 0\n" if $debug;
611                         } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
612                                  && $symhacking && $tag{'TRUE'} != -1) {
613                                 # This is for aliasing.  When we find an alias,
614                                 # we have to invert
615                                 &$make_variant($1,$2);
616                                 print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
617                         }
618                         if (/^\#/) {
619                                 @current_platforms =
620                                     grep(!/^$/,
621                                          map { $tag{$_} == 1 ? $_ :
622                                                    $tag{$_} == -1 ? "!".$_  : "" }
623                                          @known_platforms);
624                                 push @current_platforms
625                                     , grep(!/^$/,
626                                            map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
627                                                      $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
628                                            @known_ossl_platforms);
629                                 @current_algorithms = ();
630                                 @current_algorithms =
631                                     grep(!/^$/,
632                                          map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
633                                          @known_algorithms);
634                                 push @current_algorithms
635                                     , grep(!/^$/,
636                                          map { $tag{"OPENSSL_USE_".$_} == 1 ? $_ : "" }
637                                          @known_algorithms);
638                                 $def .=
639                                     "#INFO:"
640                                         .join(',',@current_platforms).":"
641                                             .join(',',@current_algorithms).";";
642                                 next;
643                         }
644                         if ($tag{'TRUE'} != -1) {
645                                 if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
646                                         next;
647                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
648                                         $def .= "int d2i_$3(void);";
649                                         $def .= "int i2d_$3(void);";
650                                         # Variant for platforms that do not
651                                         # have to access globale variables
652                                         # in shared libraries through functions
653                                         $def .=
654                                             "#INFO:"
655                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
656                                                     .join(',',@current_algorithms).";";
657                                         $def .= "OPENSSL_EXTERN int $2_it;";
658                                         $def .=
659                                             "#INFO:"
660                                                 .join(',',@current_platforms).":"
661                                                     .join(',',@current_algorithms).";";
662                                         # Variant for platforms that have to
663                                         # access globale variables in shared
664                                         # libraries through functions
665                                         &$make_variant("$2_it","$2_it",
666                                                       "EXPORT_VAR_AS_FUNCTION",
667                                                       "FUNCTION");
668                                         next;
669                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
670                                         $def .= "int d2i_$3(void);";
671                                         $def .= "int i2d_$3(void);";
672                                         $def .= "int $3_free(void);";
673                                         $def .= "int $3_new(void);";
674                                         # Variant for platforms that do not
675                                         # have to access globale variables
676                                         # in shared libraries through functions
677                                         $def .=
678                                             "#INFO:"
679                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
680                                                     .join(',',@current_algorithms).";";
681                                         $def .= "OPENSSL_EXTERN int $2_it;";
682                                         $def .=
683                                             "#INFO:"
684                                                 .join(',',@current_platforms).":"
685                                                     .join(',',@current_algorithms).";";
686                                         # Variant for platforms that have to
687                                         # access globale variables in shared
688                                         # libraries through functions
689                                         &$make_variant("$2_it","$2_it",
690                                                       "EXPORT_VAR_AS_FUNCTION",
691                                                       "FUNCTION");
692                                         next;
693                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
694                                          /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
695                                         $def .= "int d2i_$1(void);";
696                                         $def .= "int i2d_$1(void);";
697                                         $def .= "int $1_free(void);";
698                                         $def .= "int $1_new(void);";
699                                         # Variant for platforms that do not
700                                         # have to access globale variables
701                                         # in shared libraries through functions
702                                         $def .=
703                                             "#INFO:"
704                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
705                                                     .join(',',@current_algorithms).";";
706                                         $def .= "OPENSSL_EXTERN int $1_it;";
707                                         $def .=
708                                             "#INFO:"
709                                                 .join(',',@current_platforms).":"
710                                                     .join(',',@current_algorithms).";";
711                                         # Variant for platforms that have to
712                                         # access globale variables in shared
713                                         # libraries through functions
714                                         &$make_variant("$1_it","$1_it",
715                                                       "EXPORT_VAR_AS_FUNCTION",
716                                                       "FUNCTION");
717                                         next;
718                                 } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
719                                         $def .= "int d2i_$2(void);";
720                                         $def .= "int i2d_$2(void);";
721                                         # Variant for platforms that do not
722                                         # have to access globale variables
723                                         # in shared libraries through functions
724                                         $def .=
725                                             "#INFO:"
726                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
727                                                     .join(',',@current_algorithms).";";
728                                         $def .= "OPENSSL_EXTERN int $2_it;";
729                                         $def .=
730                                             "#INFO:"
731                                                 .join(',',@current_platforms).":"
732                                                     .join(',',@current_algorithms).";";
733                                         # Variant for platforms that have to
734                                         # access globale variables in shared
735                                         # libraries through functions
736                                         &$make_variant("$2_it","$2_it",
737                                                       "EXPORT_VAR_AS_FUNCTION",
738                                                       "FUNCTION");
739                                         next;
740                                 } elsif (/^\s*DECLARE_ASN1_ALLOC_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
741                                         $def .= "int $1_free(void);";
742                                         $def .= "int $1_new(void);";
743                                         next;
744                                 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
745                                         $def .= "int d2i_$2(void);";
746                                         $def .= "int i2d_$2(void);";
747                                         $def .= "int $2_free(void);";
748                                         $def .= "int $2_new(void);";
749                                         # Variant for platforms that do not
750                                         # have to access globale variables
751                                         # in shared libraries through functions
752                                         $def .=
753                                             "#INFO:"
754                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
755                                                     .join(',',@current_algorithms).";";
756                                         $def .= "OPENSSL_EXTERN int $2_it;";
757                                         $def .=
758                                             "#INFO:"
759                                                 .join(',',@current_platforms).":"
760                                                     .join(',',@current_algorithms).";";
761                                         # Variant for platforms that have to
762                                         # access globale variables in shared
763                                         # libraries through functions
764                                         &$make_variant("$2_it","$2_it",
765                                                       "EXPORT_VAR_AS_FUNCTION",
766                                                       "FUNCTION");
767                                         next;
768                                 } elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
769                                         # Variant for platforms that do not
770                                         # have to access globale variables
771                                         # in shared libraries through functions
772                                         $def .=
773                                             "#INFO:"
774                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
775                                                     .join(',',@current_algorithms).";";
776                                         $def .= "OPENSSL_EXTERN int $1_it;";
777                                         $def .=
778                                             "#INFO:"
779                                                 .join(',',@current_platforms).":"
780                                                     .join(',',@current_algorithms).";";
781                                         # Variant for platforms that have to
782                                         # access globale variables in shared
783                                         # libraries through functions
784                                         &$make_variant("$1_it","$1_it",
785                                                       "EXPORT_VAR_AS_FUNCTION",
786                                                       "FUNCTION");
787                                         next;
788                                 } elsif (/^\s*DECLARE_ASN1_NDEF_FUNCTION\s*\(\s*(\w*)\s*\)/) {
789                                         $def .= "int i2d_$1_NDEF(void);";
790                                 } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
791                                         next;
792                                 } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION\s*\(\s*(\w*)\s*\)/) {
793                                         $def .= "int $1_print_ctx(void);";
794                                         next;
795                                 } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
796                                         $def .= "int $2_print_ctx(void);";
797                                         next;
798                                 } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
799                                         next;
800                                 } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
801                                          /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ||
802                                          /^DECLARE_PEM_rw_const\s*\(\s*(\w*)\s*,/ ) {
803                                         $def .=
804                                             "#INFO:"
805                                                 .join(',',@current_platforms).":"
806                                                     .join(',',@current_algorithms).";";
807                                         $def .= "int PEM_read_$1(void);";
808                                         $def .= "int PEM_write_$1(void);";
809                                         $def .=
810                                             "#INFO:"
811                                                 .join(',',@current_platforms).":"
812                                                     .join(',',@current_algorithms).";";
813                                         # Things that are everywhere
814                                         $def .= "int PEM_read_bio_$1(void);";
815                                         $def .= "int PEM_write_bio_$1(void);";
816                                         next;
817                                 } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
818                                         /^DECLARE_PEM_write_const\s*\(\s*(\w*)\s*,/ ||
819                                          /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
820                                         $def .=
821                                             "#INFO:"
822                                                 .join(',',@current_platforms).":"
823                                                     .join(',',@current_algorithms).";";
824                                         $def .= "int PEM_write_$1(void);";
825                                         $def .=
826                                             "#INFO:"
827                                                 .join(',',@current_platforms).":"
828                                                     .join(',',@current_algorithms).";";
829                                         # Things that are everywhere
830                                         $def .= "int PEM_write_bio_$1(void);";
831                                         next;
832                                 } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
833                                          /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
834                                         $def .=
835                                             "#INFO:"
836                                                 .join(',',@current_platforms).":"
837                                                     .join(',',@current_algorithms).";";
838                                         $def .= "int PEM_read_$1(void);";
839                                         $def .=
840                                             "#INFO:"
841                                                 .join(',',@current_platforms).":"
842                                                     .join(',',@current_algorithms).";";
843                                         # Things that are everywhere
844                                         $def .= "int PEM_read_bio_$1(void);";
845                                         next;
846                                 } elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
847                                         # Variant for platforms that do not
848                                         # have to access globale variables
849                                         # in shared libraries through functions
850                                         $def .=
851                                             "#INFO:"
852                                                 .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
853                                                     .join(',',@current_algorithms).";";
854                                         $def .= "OPENSSL_EXTERN int _shadow_$2;";
855                                         $def .=
856                                             "#INFO:"
857                                                 .join(',',@current_platforms).":"
858                                                     .join(',',@current_algorithms).";";
859                                         # Variant for platforms that have to
860                                         # access globale variables in shared
861                                         # libraries through functions
862                                         &$make_variant("_shadow_$2","_shadow_$2",
863                                                       "EXPORT_VAR_AS_FUNCTION",
864                                                       "FUNCTION");
865                                 } elsif (/^\s*DEPRECATEDIN/) {
866                                         $parens = count_parens($_);
867                                         if ($parens == 0) {
868                                                 $def .= do_deprecated($_,
869                                                         \@current_platforms,
870                                                         \@current_algorithms);
871                                         } else {
872                                                 $stored_multiline = $_;
873                                                 chomp $stored_multiline;
874                                                 print STDERR "DEBUG: Found multiline DEPRECATEDIN starting with: $stored_multiline\n" if $debug;
875                                                 next;
876                                         }
877                                 } elsif ($tag{'CONST_STRICT'} != 1) {
878                                         if (/\{|\/\*|\([^\)]*$/) {
879                                                 $line = $_;
880                                         } else {
881                                                 $def .= $_;
882                                         }
883                                 }
884                         }
885                 }
886                 close(IN);
887
888                 my $algs;
889                 my $plays;
890
891                 print STDERR "DEBUG: postprocessing ----------\n" if $debug;
892                 foreach (split /;/, $def) {
893                         my $s; my $k = "FUNCTION"; my $p; my $a;
894                         s/^[\n\s]*//g;
895                         s/[\n\s]*$//g;
896                         next if(/\#undef/);
897                         next if(/typedef\W/);
898                         next if(/\#define/);
899
900                         # Reduce argument lists to empty ()
901                         # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
902                         while(/\(.*\)/s) {
903                                 s/\([^\(\)]+\)/\{\}/gs;
904                                 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;    #(*f{}) -> f
905                         }
906                         # pretend as we didn't use curly braces: {} -> ()
907                         s/\{\}/\(\)/gs;
908
909                         s/STACK_OF\(\)/void/gs;
910                         s/LHASH_OF\(\)/void/gs;
911
912                         print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug;
913                         if (/^\#INFO:([^:]*):(.*)$/) {
914                                 $plats = $1;
915                                 $algs = $2;
916                                 print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug;
917                                 next;
918                         } elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) {
919                                 $s = $1;
920                                 $k = "VARIABLE";
921                                 print STDERR "DEBUG: found external variable $s\n" if $debug;
922                         } elsif (/TYPEDEF_\w+_OF/s) {
923                                 next;
924                         } elsif (/(\w+)\s*\(\).*/s) {   # first token prior [first] () is
925                                 $s = $1;                # a function name!
926                                 print STDERR "DEBUG: found function $s\n" if $debug;
927                         } elsif (/\(/ and not (/=/)) {
928                                 print STDERR "File $file: cannot parse: $_;\n";
929                                 next;
930                         } else {
931                                 next;
932                         }
933
934                         $syms{$s} = 1;
935                         $kind{$s} = $k;
936
937                         $p = $plats;
938                         $a = $algs;
939
940                         $platform{$s} =
941                             &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
942                         $algorithm{$s} .= ','.$a;
943
944                         if (defined($variant{$s})) {
945                                 foreach $v (split /;/,$variant{$s}) {
946                                         (my $r, my $p, my $k) = split(/:/,$v);
947                                         my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
948                                         $syms{$r} = 1;
949                                         if (!defined($k)) { $k = $kind{$s}; }
950                                         $kind{$r} = $k."(".$s.")";
951                                         $algorithm{$r} = $algorithm{$s};
952                                         $platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
953                                         $platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
954                                         print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
955                                 }
956                         }
957                         print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
958                 }
959         }
960
961         # Prune the returned symbols
962
963         delete $syms{"bn_dump1"};
964         $platform{"BIO_s_log"} .= ",!WIN32,!macintosh";
965
966         $platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
967         $platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
968         $platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
969         $platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
970         $platform{"EVP_sha384"} = "!VMSVAX";
971         $platform{"EVP_sha512"} = "!VMSVAX";
972         $platform{"SHA384_Init"} = "!VMSVAX";
973         $platform{"SHA384_Transform"} = "!VMSVAX";
974         $platform{"SHA384_Update"} = "!VMSVAX";
975         $platform{"SHA384_Final"} = "!VMSVAX";
976         $platform{"SHA384"} = "!VMSVAX";
977         $platform{"SHA512_Init"} = "!VMSVAX";
978         $platform{"SHA512_Transform"} = "!VMSVAX";
979         $platform{"SHA512_Update"} = "!VMSVAX";
980         $platform{"SHA512_Final"} = "!VMSVAX";
981         $platform{"SHA512"} = "!VMSVAX";
982
983
984         # Info we know about
985
986         push @ret, map { $_."\\".&info_string($_,"EXIST",
987                                               $platform{$_},
988                                               $kind{$_},
989                                               $algorithm{$_}) } keys %syms;
990
991         if (keys %unknown_algorithms) {
992                 print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
993                 print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
994         }
995         return(@ret);
996 }
997
998 # Param: string of comma-separated platform-specs.
999 sub reduce_platforms
1000 {
1001         my ($platforms) = @_;
1002         my $pl = defined($platforms) ? $platforms : "";
1003         my %p = map { $_ => 0 } split /,/, $pl;
1004         my $ret;
1005
1006         print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
1007             if $debug;
1008         # We do this, because if there's code like the following, it really
1009         # means the function exists in all cases and should therefore be
1010         # everywhere.  By increasing and decreasing, we may attain 0:
1011         #
1012         # ifndef WIN16
1013         #    int foo();
1014         # else
1015         #    int _fat foo();
1016         # endif
1017         foreach $platform (split /,/, $pl) {
1018                 if ($platform =~ /^!(.*)$/) {
1019                         $p{$1}--;
1020                 } else {
1021                         $p{$platform}++;
1022                 }
1023         }
1024         foreach $platform (keys %p) {
1025                 if ($p{$platform} == 0) { delete $p{$platform}; }
1026         }
1027
1028         delete $p{""};
1029
1030         $ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
1031         print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
1032             if $debug;
1033         return $ret;
1034 }
1035
1036 sub info_string {
1037         (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
1038
1039         my %a = defined($algorithms) ?
1040             map { $_ => 1 } split /,/, $algorithms : ();
1041         my $k = defined($kind) ? $kind : "FUNCTION";
1042         my $ret;
1043         my $p = &reduce_platforms($platforms);
1044
1045         delete $a{""};
1046
1047         $ret = $exist;
1048         $ret .= ":".$p;
1049         $ret .= ":".$k;
1050         $ret .= ":".join(',',sort keys %a);
1051         return $ret;
1052 }
1053
1054 sub maybe_add_info {
1055         (my $name, *nums, my @symbols) = @_;
1056         my $sym;
1057         my $new_info = 0;
1058         my %syms=();
1059
1060         print STDERR "Updating $name info\n";
1061         foreach $sym (@symbols) {
1062                 (my $s, my $i) = split /\\/, $sym;
1063                 if (defined($nums{$s})) {
1064                         $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
1065                         (my $n, my $vers, my $dummy) = split /\\/, $nums{$s};
1066                         if (!defined($dummy) || $i ne $dummy) {
1067                                 $nums{$s} = $n."\\".$vers."\\".$i;
1068                                 $new_info++;
1069                                 print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
1070                         }
1071                 }
1072                 $syms{$s} = 1;
1073         }
1074
1075         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
1076         foreach $sym (@s) {
1077                 (my $n, my $vers, my $i) = split /\\/, $nums{$sym};
1078                 if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
1079                         $new_info++;
1080                         print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
1081                 }
1082         }
1083         if ($new_info) {
1084                 print STDERR "$new_info old symbols got an info update\n";
1085                 if (!$do_rewrite) {
1086                         print STDERR "You should do a rewrite to fix this.\n";
1087                 }
1088         } else {
1089                 print STDERR "No old symbols needed info update\n";
1090         }
1091 }
1092
1093 # Param: string of comma-separated keywords, each possibly prefixed with a "!"
1094 sub is_valid
1095 {
1096         my ($keywords_txt,$platforms) = @_;
1097         my (@keywords) = split /,/,$keywords_txt;
1098         my ($falsesum, $truesum) = (0, 1);
1099
1100         # Param: one keyword
1101         sub recognise
1102         {
1103                 my ($keyword,$platforms) = @_;
1104
1105                 if ($platforms) {
1106                         # platforms
1107                         if ($keyword eq "VMSVAX" && $VMSVAX) { return 1; }
1108                         if ($keyword eq "VMSNonVAX" && $VMSNonVAX) { return 1; }
1109                         if ($keyword eq "VMS" && $VMS) { return 1; }
1110                         if ($keyword eq "WIN32" && $W32) { return 1; }
1111                         if ($keyword eq "WINNT" && $NT) { return 1; }
1112                         if ($keyword eq "OS2" && $OS2) { return 1; }
1113                         # Special platforms:
1114                         # EXPORT_VAR_AS_FUNCTION means that global variables
1115                         # will be represented as functions.  This currently
1116                         # only happens on VMS-VAX.
1117                         if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && ($VMSVAX || $W32)) {
1118                                 return 1;
1119                         }
1120                         if ($keyword eq "ZLIB" && $zlib) { return 1; }
1121                         return 0;
1122                 } else {
1123                         # algorithms
1124                         if ($disabled_algorithms{$keyword} == 1) { return 0;}
1125
1126                         # Nothing recognise as true
1127                         return 1;
1128                 }
1129         }
1130
1131         foreach $k (@keywords) {
1132                 if ($k =~ /^!(.*)$/) {
1133                         $falsesum += &recognise($1,$platforms);
1134                 } else {
1135                         $truesum *= &recognise($k,$platforms);
1136                 }
1137         }
1138         print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1139         return (!$falsesum) && $truesum;
1140 }
1141
1142 sub print_test_file
1143 {
1144         (*OUT,my $name,*nums,my $testall,my @symbols)=@_;
1145         my $n = 1; my @e; my @r;
1146         my $sym; my $prev = ""; my $prefSSLeay;
1147
1148         (@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1149         (@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1150         @symbols=((sort @e),(sort @r));
1151
1152         foreach $sym (@symbols) {
1153                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1154                 my $v = 0;
1155                 $v = 1 if $i=~ /^.*?:.*?:VARIABLE/;
1156                 my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1157                 my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1158                 if (!defined($nums{$s})) {
1159                         print STDERR "Warning: $s does not have a number assigned\n"
1160                             if(!$do_update);
1161                 } elsif (is_valid($p,1) && is_valid($a,0)) {
1162                         my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1163                         if ($prev eq $s2) {
1164                                 print OUT "\t/* The following has already appeared previously */\n";
1165                                 print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1166                         }
1167                         $prev = $s2;    # To warn about duplicates...
1168
1169                         (my $nn, my $vers, my $ni) = split /\\/, $nums{$s2};
1170                         if ($v) {
1171                                 print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n";
1172                         } else {
1173                                 print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n";
1174                         }
1175                 }
1176         }
1177 }
1178
1179 sub get_version {
1180    local *MF;
1181    my $v = '?';
1182    open MF, 'Makefile' or return $v;
1183    while (<MF>) {
1184      $v = $1, last if /^VERSION=(.*?)\s*$/;
1185    }
1186    close MF;
1187    return $v;
1188 }
1189
1190 sub print_def_file
1191 {
1192         (*OUT,my $name,*nums,my @symbols)=@_;
1193         my $n = 1; my @e; my @r; my @v; my $prev="";
1194         my $liboptions="";
1195         my $libname = $name;
1196         my $http_vendor = 'www.openssl.org/';
1197         my $version = get_version();
1198         my $what = "OpenSSL: implementation of Secure Socket Layer";
1199         my $description = "$what $version, $name - http://$http_vendor";
1200         my $prevsymversion = "", $prevprevsymversion = "";
1201         # For VMS
1202         my $prevnum = 0;
1203         my $symvtextcount = 0;
1204
1205         if ($W32)
1206                 { $libname.="32"; }
1207         elsif ($OS2)
1208                 { # DLL names should not clash on the whole system.
1209                   # However, they should not have any particular relationship
1210                   # to the name of the static library.  Chose descriptive names
1211                   # (must be at most 8 chars).
1212                   my %translate = (ssl => 'open_ssl', crypto => 'cryptssl');
1213                   $libname = $translate{$name} || $name;
1214                   $liboptions = <<EOO;
1215 INITINSTANCE
1216 DATA MULTIPLE NONSHARED
1217 EOO
1218                   # Vendor field can't contain colon, drat; so we omit http://
1219                   $description = "\@#$http_vendor:$version#\@$what; DLL for library $name.  Build for EMX -Zmtd";
1220                 }
1221
1222         if ($W32 || $OS2)
1223                 {
1224                 print OUT <<"EOF";
1225 ;
1226 ; Definition file for the DLL version of the $name library from OpenSSL
1227 ;
1228
1229 LIBRARY         $libname        $liboptions
1230
1231 EOF
1232
1233                 print "EXPORTS\n";
1234                 }
1235         elsif ($VMS)
1236                 {
1237                 my $libref = $name eq "ssl" ? "LIBCRYPTO.EXE /SHARE" : "";
1238                 print OUT <<"EOF";
1239 IDENTIFICATION="V$version"
1240 CASE_SENSITIVE=YES
1241 LIB$libname.OLB /LIBRARY
1242 $libref
1243 SYMBOL_VECTOR=(-
1244 EOF
1245                 $symvtextcount = 16; # length of "SYMBOL_VECTOR=(-"
1246                 }
1247
1248         (@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1249         (@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols);
1250         if ($VMS) {
1251             # VMS needs to have the symbols on slot number order
1252             @symbols=(map { $_->[1] }
1253                       sort { $a->[0] <=> $b->[0] }
1254                       map { (my $s, my $i) = $_ =~ /^(.*?)\\(.*)$/;
1255                             die "Error: $s doesn't have a number assigned\n"
1256                                 if !defined($nums{$s});
1257                             (my $n, my @rest) = split /\\/, $nums{$s};
1258                             [ $n, $_ ] } (@e, @r, @v));
1259         } else {
1260             @symbols=((sort @e),(sort @r), (sort @v));
1261         }
1262
1263         my ($baseversion, $currversion) = get_openssl_version();
1264         my $thisversion;
1265         do {
1266                 if (!defined($thisversion)) {
1267                         $thisversion = $baseversion;
1268                 } else {
1269                         $thisversion = get_next_version($thisversion);
1270                 }
1271                 foreach $sym (@symbols) {
1272                         (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1273                         my $v = 0;
1274                         $v = 1 if $i =~ /^.*?:.*?:VARIABLE/;
1275                         if (!defined($nums{$s})) {
1276                                 die "Error: $s does not have a number assigned\n"
1277                                         if(!$do_update);
1278                         } else {
1279                                 (my $n, my $symversion, my $dummy) = split /\\/, $nums{$s};
1280                                 next if $symversion ne $thisversion;
1281                                 my %pf = ();
1282                                 my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1283                                 my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1284                                 if (is_valid($p,1) && is_valid($a,0)) {
1285                                         my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1286                                         if ($prev eq $s2) {
1287                                                 print STDERR "Warning: Symbol '",$s2,
1288                                                         "' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),
1289                                                         ", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1290                                         }
1291                                         $prev = $s2;    # To warn about duplicates...
1292                                         if($linux) {
1293                                                 if ($symversion ne $prevsymversion) {
1294                                                         if ($prevsymversion ne "") {
1295                                                                 if ($prevprevsymversion ne "") {
1296                                                                         print OUT "} OPENSSL_"
1297                                                                                                 ."$prevprevsymversion;\n\n";
1298                                                                 } else {
1299                                                                         print OUT "};\n\n";
1300                                                                 }
1301                                                         }
1302                                                         print OUT "OPENSSL_$symversion {\n    global:\n";
1303                                                         $prevprevsymversion = $prevsymversion;
1304                                                         $prevsymversion = $symversion;
1305                                                 }
1306                                                 print OUT "        $s2;\n";
1307                                         } elsif ($VMS) {
1308                                             while(++$prevnum < $n) {
1309                                                 my $symline="SPARE, SPARE -";
1310                                                 if ($symvtextcount + length($symline) + 1 > 1024) {
1311                                                     print OUT ")\nSYMBOL_VECTOR=(-\n";
1312                                                     $symvtextcount = 16; # length of "SYMBOL_VECTOR=(-"
1313                                                 }
1314                                                 if ($symvtextcount > 16) {
1315                                                     $symline = ",".$symline;
1316                                                 }
1317                                                 print OUT "    $symline\n";
1318                                                 $symvtextcount += length($symline);
1319                                             }
1320                                             (my $s_uc = $s) =~ tr/a-z/A-Z/;
1321                                             my $symtype=
1322                                                 $v ? "DATA" : "PROCEDURE";
1323                                             my $symline=
1324                                                 ($s_uc ne $s
1325                                                  ? "$s_uc/$s=$symtype, $s=$symtype"
1326                                                  : "$s=$symtype, SPARE")
1327                                                 ." -";
1328                                             if ($symvtextcount + length($symline) + 1 > 1024) {
1329                                                 print OUT ")\nSYMBOL_VECTOR=(-\n";
1330                                                 $symvtextcount = 16; # length of "SYMBOL_VECTOR=(-"
1331                                             }
1332                                             if ($symvtextcount > 16) {
1333                                                 $symline = ",".$symline;
1334                                             }
1335                                             print OUT "    $symline\n";
1336                                             $symvtextcount += length($symline);
1337                                         } elsif($v && !$OS2) {
1338                                                 printf OUT "    %s%-39s @%-8d DATA\n",
1339                                                                 ($W32)?"":"_",$s2,$n;
1340                                         } else {
1341                                                 printf OUT "    %s%-39s @%d\n",
1342                                                                 ($W32||$OS2)?"":"_",$s2,$n;
1343                                         }
1344                                 }
1345                         }
1346                 }
1347         } while ($thisversion ne $currversion);
1348         if ($linux) {
1349                 if ($prevprevsymversion ne "") {
1350                         print OUT "    local: *;\n} OPENSSL_$prevprevsymversion;\n\n";
1351                 } else {
1352                         print OUT "    local: *;\n};\n\n";
1353                 }
1354         } elsif ($VMS) {
1355             print OUT ")\n";
1356             (my $libvmaj, my $libvmin, my $libvedit) =
1357                 $currversion =~ /^(\d+)_(\d+)_(\d+)$/;
1358             # The reason to multiply the edit number with 100 is to make space
1359             # for the possibility that we want to encode the patch letters
1360             print OUT "GSMATCH=LEQUAL,",($libvmaj * 100 + $libvmin),",",($libvedit * 100),"\n";
1361         }
1362         printf OUT "\n";
1363 }
1364
1365 sub load_numbers
1366 {
1367         my($name)=@_;
1368         my(@a,%ret);
1369         my $prevversion;
1370
1371         $max_num = 0;
1372         $num_noinfo = 0;
1373         $prev = "";
1374         $prev_cnt = 0;
1375
1376         my ($baseversion, $currversion) = get_openssl_version();
1377
1378         open(IN,"<$name") || die "unable to open $name:$!\n";
1379         while (<IN>) {
1380                 chop;
1381                 s/#.*$//;
1382                 next if /^\s*$/;
1383                 @a=split;
1384                 if (defined $ret{$a[0]}) {
1385                         # This is actually perfectly OK
1386                         #print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
1387                 }
1388                 if ($max_num > $a[1]) {
1389                         print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
1390                 }
1391                 elsif ($max_num == $a[1]) {
1392                         # This is actually perfectly OK
1393                         #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1394                         if ($a[0] eq $prev) {
1395                                 $prev_cnt++;
1396                                 $a[0] .= "{$prev_cnt}";
1397                         }
1398                 }
1399                 else {
1400                         $prev_cnt = 0;
1401                 }
1402                 if ($#a < 2) {
1403                         # Existence will be proven later, in do_defs
1404                         $ret{$a[0]}=$a[1];
1405                         $num_noinfo++;
1406                 } else {
1407                         #Sanity check the version number
1408                         if (defined $prevversion) {
1409                                 check_version_lte($prevversion, $a[2]);
1410                         }
1411                         check_version_lte($a[2], $currversion);
1412                         $prevversion = $a[2];
1413                         $ret{$a[0]}=$a[1]."\\".$a[2]."\\".$a[3]; # \\ is a special marker
1414                 }
1415                 $max_num = $a[1] if $a[1] > $max_num;
1416                 $prev=$a[0];
1417         }
1418         if ($num_noinfo) {
1419                 print STDERR "Warning: $num_noinfo symbols were without info.";
1420                 if ($do_rewrite) {
1421                         printf STDERR "  The rewrite will fix this.\n";
1422                 } else {
1423                         printf STDERR "  You should do a rewrite to fix this.\n";
1424                 }
1425         }
1426         close(IN);
1427         return(%ret);
1428 }
1429
1430 sub parse_number
1431 {
1432         (my $str, my $what) = @_;
1433         (my $n, my $v, my $i) = split(/\\/,$str);
1434         if ($what eq "n") {
1435                 return $n;
1436         } else {
1437                 return $i;
1438         }
1439 }
1440
1441 sub rewrite_numbers
1442 {
1443         (*OUT,$name,*nums,@symbols)=@_;
1444         my $thing;
1445
1446         print STDERR "Rewriting $name\n";
1447
1448         my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1449         my $r; my %r; my %rsyms;
1450         foreach $r (@r) {
1451                 (my $s, my $i) = split /\\/, $r;
1452                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1453                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1454                 $r{$a} = $s."\\".$i;
1455                 $rsyms{$s} = 1;
1456         }
1457
1458         my %syms = ();
1459         foreach $_ (@symbols) {
1460                 (my $n, my $i) = split /\\/;
1461                 $syms{$n} = 1;
1462         }
1463
1464         my @s=sort {
1465             &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1466             || $a cmp $b
1467         } keys %nums;
1468         foreach $sym (@s) {
1469                 (my $n, my $vers, my $i) = split /\\/, $nums{$sym};
1470                 next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
1471                 next if defined($rsyms{$sym});
1472                 print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1473                 $i="NOEXIST::FUNCTION:"
1474                         if !defined($i) || $i eq "" || !defined($syms{$sym});
1475                 my $s2 = $sym;
1476                 $s2 =~ s/\{[0-9]+\}$//;
1477                 printf OUT "%s%-39s %d\t%s\t%s\n","",$s2,$n,$vers,$i;
1478                 if (exists $r{$sym}) {
1479                         (my $s, $i) = split /\\/,$r{$sym};
1480                         my $s2 = $s;
1481                         $s2 =~ s/\{[0-9]+\}$//;
1482                         printf OUT "%s%-39s %d\t%s\t%s\n","",$s2,$n,$vers,$i;
1483                 }
1484         }
1485 }
1486
1487 sub update_numbers
1488 {
1489         (*OUT,$name,*nums,my $start_num, my @symbols)=@_;
1490         my $new_syms = 0;
1491         my $basevers;
1492         my $vers;
1493
1494         ($basevers, $vers) = get_openssl_version();
1495
1496         print STDERR "Updating $name numbers\n";
1497
1498         my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1499         my $r; my %r; my %rsyms;
1500         foreach $r (@r) {
1501                 (my $s, my $i) = split /\\/, $r;
1502                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1503                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1504                 $r{$a} = $s."\\".$i;
1505                 $rsyms{$s} = 1;
1506         }
1507
1508         foreach $sym (@symbols) {
1509                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1510                 next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
1511                 next if defined($rsyms{$sym});
1512                 die "ERROR: Symbol $sym had no info attached to it."
1513                     if $i eq "";
1514                 if (!exists $nums{$s}) {
1515                         $new_syms++;
1516                         my $s2 = $s;
1517                         $s2 =~ s/\{[0-9]+\}$//;
1518                         printf OUT "%s%-39s %d\t%s\t%s\n","",$s2, ++$start_num,$vers,$i;
1519                         if (exists $r{$s}) {
1520                                 ($s, $i) = split /\\/,$r{$s};
1521                                 $s =~ s/\{[0-9]+\}$//;
1522                                 printf OUT "%s%-39s %d\t%s\t%s\n","",$s, $start_num,$vers,$i;
1523                         }
1524                 }
1525         }
1526         if($new_syms) {
1527                 print STDERR "$new_syms New symbols added\n";
1528         } else {
1529                 print STDERR "No New symbols Added\n";
1530         }
1531 }
1532
1533 sub check_existing
1534 {
1535         (*nums, my @symbols)=@_;
1536         my %existing; my @remaining;
1537         @remaining=();
1538         foreach $sym (@symbols) {
1539                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1540                 $existing{$s}=1;
1541         }
1542         foreach $sym (keys %nums) {
1543                 if (!exists $existing{$sym}) {
1544                         push @remaining, $sym;
1545                 }
1546         }
1547         if(@remaining) {
1548                 print STDERR "The following symbols do not seem to exist:\n";
1549                 foreach $sym (@remaining) {
1550                         print STDERR "\t",$sym,"\n";
1551                 }
1552         }
1553 }
1554
1555 sub count_parens
1556 {
1557         my $line = shift(@_);
1558
1559         my $open = $line =~ tr/\(//;
1560         my $close = $line =~ tr/\)//;
1561
1562         return $open - $close;
1563 }
1564
1565 #Parse opensslv.h to get the current version number. Also work out the base
1566 #version, i.e. the lowest version number that is binary compatible with this
1567 #version
1568 sub get_openssl_version()
1569 {
1570         open (IN, "include/openssl/opensslv.h") || die "Can't open opensslv.h";
1571
1572         while(<IN>) {
1573                 if (/OPENSSL_VERSION_TEXT\s+"OpenSSL (\d\.\d\.)(\d[a-z]*)(-| )/) {
1574                         my $suffix = $2;
1575                         (my $baseversion = $1) =~ s/\./_/g;
1576                         close IN;
1577                         return ($baseversion."0", $baseversion.$suffix);
1578                 }
1579         }
1580         die "Can't find OpenSSL version number\n";
1581 }
1582
1583 #Given an OpenSSL version number, calculate the next version number. If the
1584 #version number gets to a.b.czz then we go to a.b.(c+1)
1585 sub get_next_version()
1586 {
1587         my $thisversion = shift;
1588
1589         my ($base, $letter) = $thisversion =~ /^(\d_\d_\d)([a-z]{0,2})$/;
1590
1591         if ($letter eq "zz") {
1592                 my $lastnum = substr($base, -1);
1593                 return substr($base, 0, length($base)-1).(++$lastnum);
1594         }
1595         return $base.get_next_letter($letter);
1596 }
1597
1598 #Given the letters off the end of an OpenSSL version string, calculate what
1599 #the letters for the next release would be.
1600 sub get_next_letter()
1601 {
1602         my $thisletter = shift;
1603         my $baseletter = "";
1604         my $endletter;
1605
1606         if ($thisletter eq "") {
1607                 return "a";
1608         }
1609         if ((length $thisletter) > 1) {
1610                 ($baseletter, $endletter) = $thisletter =~ /([a-z]+)([a-z])/;
1611         } else {
1612                 $endletter = $thisletter;
1613         }
1614
1615         if ($endletter eq "z") {
1616                 return $thisletter."a";
1617         } else {
1618                 return $baseletter.(++$endletter);
1619         }
1620 }
1621
1622 #Check if a version is less than or equal to the current version. Its a fatal
1623 #error if not. They must also only differ in letters, or the last number (i.e.
1624 #the first two numbers must be the same)
1625 sub check_version_lte()
1626 {
1627         my ($testversion, $currversion) = @_;
1628         my $lentv;
1629         my $lencv;
1630         my $cvbase;
1631
1632         my ($cvnums) = $currversion =~ /^(\d_\d_\d)[a-z]*$/;
1633         my ($tvnums) = $testversion =~ /^(\d_\d_\d)[a-z]*$/;
1634
1635         #Die if we can't parse the version numbers or they don't look sane
1636         die "Invalid version number: $testversion and $currversion\n"
1637                 if (!defined($cvnums) || !defined($tvnums)
1638                         || length($cvnums) != 5
1639                         || length($tvnums) != 5);
1640
1641         #If the base versions (without letters) don't match check they only differ
1642         #in the last number
1643         if ($cvnums ne $tvnums) {
1644                 die "Invalid version number: $testversion "
1645                         ."for current version $currversion\n"
1646                         if (substr($cvnums, -1) < substr($tvnums, -1)
1647                                 || substr($cvnums, 0, 4) ne substr($tvnums, 0, 4));
1648                 return;
1649         }
1650         #If we get here then the base version (i.e. the numbers) are the same - they
1651         #only differ in the letters
1652
1653         $lentv = length $testversion;
1654         $lencv = length $currversion;
1655
1656         #If the testversion has more letters than the current version then it must
1657         #be later (or malformed)
1658         if ($lentv > $lencv) {
1659                 die "Invalid version number: $testversion "
1660                         ."is greater than $currversion\n";
1661         }
1662
1663         #Get the last letter from the current version
1664         my ($cvletter) = $currversion =~ /([a-z])$/;
1665         if (defined $cvletter) {
1666                 ($cvbase) = $currversion =~ /(\d_\d_\d[a-z]*)$cvletter$/;
1667         } else {
1668                 $cvbase = $currversion;
1669         }
1670         die "Unable to parse version number $currversion" if (!defined $cvbase);
1671         my $tvbase;
1672         my ($tvletter) = $testversion =~ /([a-z])$/;
1673         if (defined $tvletter) {
1674                 ($tvbase) = $testversion =~ /(\d_\d_\d[a-z]*)$tvletter$/;
1675         } else {
1676                 $tvbase = $testversion;
1677         }
1678         die "Unable to parse version number $testversion" if (!defined $tvbase);
1679
1680         if ($lencv > $lentv) {
1681                 #If current version has more letters than testversion then testversion
1682                 #minus the final letter must be a substring of the current version
1683                 die "Invalid version number $testversion "
1684                         ."is greater than $currversion or is invalid\n"
1685                         if (index($cvbase, $tvbase) != 0);
1686         } else {
1687                 #If both versions have the same number of letters then they must be
1688                 #equal up to the last letter, and the last letter in testversion must
1689                 #be less than or equal to the last letter in current version.
1690                 die "Invalid version number $testversion "
1691                         ."is greater than $currversion\n"
1692                         if (($cvbase ne $tvbase) && ($tvletter gt $cvletter));
1693         }
1694 }
1695
1696 sub do_deprecated()
1697 {
1698         my ($decl, $plats, $algs) = @_;
1699         $decl =~ /^\s*(DEPRECATEDIN_\d+_\d+_\d+)\s*\((.*)\)\s*$/
1700             or die "Bad DEPRECTEDIN: $decl\n";
1701         my $info1 .= "#INFO:";
1702         $info1 .= join(',', @{$plats}) . ":";
1703         my $info2 = $info1;
1704         $info1 .= join(',',@{$algs}, $1) . ";";
1705         $info2 .= join(',',@{$algs}) . ";";
1706         return $info1 . $2 . ";" . $info2;
1707 }