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