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