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