mkdef.pl revision 290207
144348Simp#!/usr/local/bin/perl -w
2208737Sjmallett#
3208737Sjmallett# generate a .def file
477298Sobrien#
544348Simp# It does this by parsing the header files and looking for the
644348Simp# prototyped functions: it then prunes the output.
744348Simp#
844348Simp# Intermediary files are created, call libeay.num and ssleay.num,...
944348Simp# Previously, they had the following format:
1044348Simp#
1144348Simp#	routine-name	nnnn
1244348Simp#
1344348Simp# But that isn't enough for a number of reasons, the first on being that
1444348Simp# this format is (needlessly) very Win32-centric, and even then...
1544348Simp# One of the biggest problems is that there's no information about what
1644348Simp# routines should actually be used, which varies with what crypto algorithms
1744348Simp# are disabled.  Also, some operating systems (for example VMS with VAX C)
1844348Simp# need to keep track of the global variables as well as the functions.
1944348Simp#
2044348Simp# So, a remake of this script is done so as to include information on the
2144348Simp# kind of symbol it is (function or variable) and what algorithms they're
2244348Simp# part of.  This will allow easy translating to .def files or the corresponding
2344348Simp# file in other operating systems (a .opt file for VMS, possibly with a .mar
24208737Sjmallett# file).
2544348Simp#
2644348Simp# The format now becomes:
2744348Simp#
2844348Simp#	routine-name	nnnn	info
2944348Simp#
3044348Simp# and the "info" part is actually a colon-separated string of fields with
3144348Simp# the following meaning:
3260484Sobrien#
3360484Sobrien#	existence:platform:kind:algorithms
3460484Sobrien#
3560484Sobrien# - "existence" can be "EXIST" or "NOEXIST" depending on if the symbol is
3660484Sobrien#   found somewhere in the source,
3760484Sobrien# - "platforms" is empty if it exists on all platforms, otherwise it contains
3878828Sobrien#   comma-separated list of the platform, just as they are if the symbol exists
3978828Sobrien#   for those platforms, or prepended with a "!" if not.  This helps resolve
4060484Sobrien#   symbol name variants for platforms where the names are too long for the
4160484Sobrien#   compiler or linker, or if the systems is case insensitive and there is a
4260484Sobrien#   clash, or the symbol is implemented differently (see
4378828Sobrien#   EXPORT_VAR_AS_FUNCTION).  This script assumes renaming of symbols is found
4460484Sobrien#   in the file crypto/symhacks.h.
4578828Sobrien#   The semantics for the platforms is that every item is checked against the
4660484Sobrien#   environment.  For the negative items ("!FOO"), if any of them is false
4778828Sobrien#   (i.e. "FOO" is true) in the environment, the corresponding symbol can't be
4860484Sobrien#   used.  For the positive itms, if all of them are false in the environment,
4960484Sobrien#   the corresponding symbol can't be used.  Any combination of positive and
5060484Sobrien#   negative items are possible, and of course leave room for some redundancy.
5160484Sobrien# - "kind" is "FUNCTION" or "VARIABLE".  The meaning of that is obvious.
5260484Sobrien# - "algorithms" is a comma-separated list of algorithm names.  This helps
5360484Sobrien#   exclude symbols that are part of an algorithm that some user wants to
5460484Sobrien#   exclude.
5560484Sobrien#
5660484Sobrien
5760484Sobrienmy $debug=0;
5860484Sobrien
5960484Sobrienmy $crypto_num= "util/libeay.num";
6060484Sobrienmy $ssl_num=    "util/ssleay.num";
6160484Sobrienmy $libname;
6260484Sobrien
6360484Sobrienmy $do_update = 0;
6460484Sobrienmy $do_rewrite = 1;
6560484Sobrienmy $do_crypto = 0;
6660484Sobrienmy $do_ssl = 0;
6760484Sobrienmy $do_ctest = 0;
6860484Sobrienmy $do_ctestall = 0;
6960484Sobrienmy $do_checkexist = 0;
7060484Sobrien
7160484Sobrienmy $VMSVAX=0;
7260484Sobrienmy $VMSNonVAX=0;
7360484Sobrienmy $VMS=0;
7460484Sobrienmy $W32=0;
7560484Sobrienmy $W16=0;
76218822Sdimmy $NT=0;
77218822Sdimmy $OS2=0;
78218822Sdim# Set this to make typesafe STACK definitions appear in DEF
79218822Sdimmy $safe_stack_def = 0;
80218822Sdim
81218822Sdimmy @known_platforms = ( "__FreeBSD__", "PERL5", "NeXT",
82218822Sdim			"EXPORT_VAR_AS_FUNCTION", "ZLIB", "OPENSSL_FIPS" );
83218822Sdimmy @known_ossl_platforms = ( "VMS", "WIN16", "WIN32", "WINNT", "OS2" );
84218822Sdimmy @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
85218822Sdim			 "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1",
86218822Sdim			 "SHA256", "SHA512", "RIPEMD",
87218822Sdim			 "MDC2", "WHIRLPOOL", "RSA", "DSA", "DH", "EC", "ECDH", "ECDSA", "EC2M",
88218822Sdim			 "HMAC", "AES", "CAMELLIA", "SEED", "GOST",
89218822Sdim			 # EC_NISTP_64_GCC_128
90218822Sdim			 "EC_NISTP_64_GCC_128",
91218822Sdim			 # Envelope "algorithms"
9260484Sobrien			 "EVP", "X509", "ASN1_TYPEDEFS",
93218822Sdim			 # Helper "algorithms"
9460484Sobrien			 "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
9560484Sobrien			 "LOCKING",
96218822Sdim			 # External "algorithms"
97218822Sdim			 "FP_API", "STDIO", "SOCK", "KRB5", "DGRAM",
98218822Sdim			 # Engines
99218822Sdim			 "STATIC_ENGINE", "ENGINE", "HW", "GMP",
100218822Sdim			 # RFC3779
101218822Sdim			 "RFC3779",
102218822Sdim			 # TLS
103218822Sdim			 "TLSEXT", "PSK", "SRP", "HEARTBEATS",
104218822Sdim			 # CMS
105218822Sdim			 "CMS",
106218822Sdim			 # CryptoAPI Engine
107218822Sdim			 "CAPIENG",
10860484Sobrien			 # SSL v2
109218822Sdim			 "SSL2",
11060484Sobrien			 # SSL v3 method
11160484Sobrien			 "SSL3_METHOD",
11260484Sobrien			 # JPAKE
11360484Sobrien			 "JPAKE",
11477298Sobrien			 # NEXTPROTONEG
11560484Sobrien			 "NEXTPROTONEG",
11644348Simp			 # Deprecated functions
11744348Simp			 "DEPRECATED",
11844348Simp			 # Hide SSL internals
11944348Simp			 "SSL_INTERN",
12044348Simp			 # SCTP
12144348Simp		 	 "SCTP",
12244348Simp			 # SRTP
12344348Simp			 "SRTP",
12444348Simp			 # SSL TRACE
12544348Simp		 	 "SSL_TRACE",
12644348Simp			 # Unit testing
12744348Simp			 "UNIT_TEST");
128130561Sobrien
129130561Sobrienmy $options="";
130130561Sobrienopen(IN,"<Makefile") || die "unable to open Makefile!\n";
13178828Sobrienwhile(<IN>) {
13278828Sobrien    $options=$1 if (/^OPTIONS=(.*)$/);
13378828Sobrien}
13444348Simpclose(IN);
13544348Simp
13644348Simp# The following ciphers may be excluded (by Configure). This means functions
13778828Sobrien# defined with ifndef(NO_XXX) are not included in the .def file, and everything
13878828Sobrien# in directory xxx is ignored.
13978828Sobrienmy $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
14078828Sobrienmy $no_cast; my $no_whirlpool; my $no_camellia; my $no_seed;
14178828Sobrienmy $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
14278828Sobrienmy $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_aes; my $no_krb5;
14378828Sobrienmy $no_ec; my $no_ecdsa; my $no_ecdh; my $no_engine; my $no_hw;
14478828Sobrienmy $no_fp_api; my $no_static_engine=1; my $no_gmp; my $no_deprecated;
14578828Sobrienmy $no_rfc3779; my $no_psk; my $no_tlsext; my $no_cms; my $no_capieng;
14678828Sobrienmy $no_jpake; my $no_srp; my $no_ssl2; my $no_ec2m; my $no_nistp_gcc;
14778828Sobrienmy $no_nextprotoneg; my $no_sctp; my $no_srtp; my $no_ssl_trace;
14878828Sobrienmy $no_unit_test; my $no_ssl3_method;
14978828Sobrien
15060484Sobrienmy $fips;
15160484Sobrien
15260484Sobrienmy $zlib;
15344348Simp
15444348Simp
15544348Simpforeach (@ARGV, split(/ /, $options))
15644348Simp	{
15744348Simp	$debug=1 if $_ eq "debug";
15844348Simp	$W32=1 if $_ eq "32";
15944348Simp	$W16=1 if $_ eq "16";
16044348Simp	if($_ eq "NT") {
16144348Simp		$W32 = 1;
16244348Simp		$NT = 1;
16344348Simp	}
16444348Simp	if ($_ eq "VMS-VAX") {
16544348Simp		$VMS=1;
16644348Simp		$VMSVAX=1;
16760484Sobrien	}
16877298Sobrien	if ($_ eq "VMS-NonVAX") {
16977298Sobrien		$VMS=1;
17077298Sobrien		$VMSNonVAX=1;
17177298Sobrien	}
17277298Sobrien	$VMS=1 if $_ eq "VMS";
17377298Sobrien	$OS2=1 if $_ eq "OS2";
17477298Sobrien	$fips=1 if /^fips/;
17577298Sobrien	if ($_ eq "zlib" || $_ eq "enable-zlib" || $_ eq "zlib-dynamic"
17677298Sobrien			 || $_ eq "enable-zlib-dynamic") {
177130561Sobrien		$zlib = 1;
178130561Sobrien	}
179130561Sobrien
180130561Sobrien	$do_ssl=1 if $_ eq "ssleay";
181130561Sobrien	if ($_ eq "ssl") {
182130561Sobrien		$do_ssl=1;
18360484Sobrien		$libname=$_
18460484Sobrien	}
18560484Sobrien	$do_crypto=1 if $_ eq "libeay";
18660484Sobrien	if ($_ eq "crypto") {
18760484Sobrien		$do_crypto=1;
18860484Sobrien		$libname=$_;
18960484Sobrien	}
19060484Sobrien	$no_static_engine=1 if $_ eq "no-static-engine";
19160484Sobrien	$no_static_engine=0 if $_ eq "enable-static-engine";
19260484Sobrien	$do_update=1 if $_ eq "update";
19360484Sobrien	$do_rewrite=1 if $_ eq "rewrite";
19460484Sobrien	$do_ctest=1 if $_ eq "ctest";
19560484Sobrien	$do_ctestall=1 if $_ eq "ctestall";
19660484Sobrien	$do_checkexist=1 if $_ eq "exist";
19760484Sobrien	#$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
19860484Sobrien
19960484Sobrien	if    (/^no-rc2$/)      { $no_rc2=1; }
20060484Sobrien	elsif (/^no-rc4$/)      { $no_rc4=1; }
20160484Sobrien	elsif (/^no-rc5$/)      { $no_rc5=1; }
20260484Sobrien	elsif (/^no-idea$/)     { $no_idea=1; }
20360484Sobrien	elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
20460484Sobrien	elsif (/^no-bf$/)       { $no_bf=1; }
20560484Sobrien	elsif (/^no-cast$/)     { $no_cast=1; }
20660484Sobrien	elsif (/^no-whirlpool$/)     { $no_whirlpool=1; }
20760484Sobrien	elsif (/^no-md2$/)      { $no_md2=1; }
20860484Sobrien	elsif (/^no-md4$/)      { $no_md4=1; }
20960484Sobrien	elsif (/^no-md5$/)      { $no_md5=1; }
21060484Sobrien	elsif (/^no-sha$/)      { $no_sha=1; }
21160484Sobrien	elsif (/^no-ripemd$/)   { $no_ripemd=1; }
21260484Sobrien	elsif (/^no-mdc2$/)     { $no_mdc2=1; }
213130561Sobrien	elsif (/^no-rsa$/)      { $no_rsa=1; }
21460484Sobrien	elsif (/^no-dsa$/)      { $no_dsa=1; }
21577298Sobrien	elsif (/^no-dh$/)       { $no_dh=1; }
216208737Sjmallett	elsif (/^no-ec$/)       { $no_ec=1; }
217130561Sobrien	elsif (/^no-ecdsa$/)	{ $no_ecdsa=1; }
218130561Sobrien	elsif (/^no-ecdh$/) 	{ $no_ecdh=1; }
219208737Sjmallett	elsif (/^no-hmac$/)	{ $no_hmac=1; }
22044348Simp	elsif (/^no-aes$/)	{ $no_aes=1; }
22144348Simp	elsif (/^no-camellia$/)	{ $no_camellia=1; }
22244348Simp	elsif (/^no-seed$/)     { $no_seed=1; }
22344348Simp	elsif (/^no-evp$/)	{ $no_evp=1; }
22444348Simp	elsif (/^no-lhash$/)	{ $no_lhash=1; }
22544348Simp	elsif (/^no-stack$/)	{ $no_stack=1; }
22644348Simp	elsif (/^no-err$/)	{ $no_err=1; }
22744348Simp	elsif (/^no-buffer$/)	{ $no_buffer=1; }
22844348Simp	elsif (/^no-bio$/)	{ $no_bio=1; }
22944348Simp	#elsif (/^no-locking$/)	{ $no_locking=1; }
23044348Simp	elsif (/^no-comp$/)	{ $no_comp=1; }
23144348Simp	elsif (/^no-dso$/)	{ $no_dso=1; }
23244348Simp	elsif (/^no-krb5$/)	{ $no_krb5=1; }
23344348Simp	elsif (/^no-engine$/)	{ $no_engine=1; }
23444348Simp	elsif (/^no-hw$/)	{ $no_hw=1; }
23544348Simp	elsif (/^no-gmp$/)	{ $no_gmp=1; }
23644348Simp	elsif (/^no-rfc3779$/)	{ $no_rfc3779=1; }
23744348Simp	elsif (/^no-tlsext$/)	{ $no_tlsext=1; }
23844348Simp	elsif (/^no-cms$/)	{ $no_cms=1; }
23944348Simp	elsif (/^no-ec2m$/)	{ $no_ec2m=1; }
24044348Simp	elsif (/^no-ec_nistp_64_gcc_128$/)	{ $no_nistp_gcc=1; }
24144348Simp	elsif (/^no-nextprotoneg$/)	{ $no_nextprotoneg=1; }
24244348Simp	elsif (/^no-ssl2$/)	{ $no_ssl2=1; }
24344348Simp	elsif (/^no-ssl3-method$/) { $no_ssl3_method=1; }
24444348Simp	elsif (/^no-ssl-trace$/) { $no_ssl_trace=1; }
24544348Simp	elsif (/^no-capieng$/)	{ $no_capieng=1; }
24644348Simp	elsif (/^no-jpake$/)	{ $no_jpake=1; }
24744348Simp	elsif (/^no-srp$/)	{ $no_srp=1; }
24844348Simp	elsif (/^no-sctp$/)	{ $no_sctp=1; }
24944348Simp	elsif (/^no-srtp$/)	{ $no_srtp=1; }
25044348Simp	elsif (/^no-unit-test$/){ $no_unit_test=1; }
25144348Simp	}
25244348Simp
25344348Simp
25444348Simpif (!$libname) {
25544348Simp	if ($do_ssl) {
25644348Simp		$libname="SSLEAY";
25744348Simp	}
25844348Simp	if ($do_crypto) {
25944348Simp		$libname="LIBEAY";
26044348Simp	}
26144348Simp}
26244348Simp
26344348Simp# If no platform is given, assume WIN32
26444348Simpif ($W32 + $W16 + $VMS + $OS2 == 0) {
26544348Simp	$W32 = 1;
26644348Simp}
26744348Simp
26844348Simp# Add extra knowledge
26944348Simpif ($W16) {
27060484Sobrien	$no_fp_api=1;
27160484Sobrien}
27260484Sobrien
27360484Sobrienif (!$do_ssl && !$do_crypto)
27460484Sobrien	{
27560484Sobrien	print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 ]\n";
27660484Sobrien	exit(1);
27760484Sobrien	}
27860484Sobrien
27944348Simp%ssl_list=&load_numbers($ssl_num);
28044348Simp$max_ssl = $max_num;
28144348Simp%crypto_list=&load_numbers($crypto_num);
28244348Simp$max_crypto = $max_num;
28344348Simp
28444348Simpmy $ssl="ssl/ssl.h";
28544348Simp$ssl.=" ssl/kssl.h";
28644348Simp$ssl.=" ssl/tls1.h";
28744348Simp$ssl.=" ssl/srtp.h";
28860484Sobrien
28960484Sobrienmy $crypto ="crypto/crypto.h";
29060484Sobrien$crypto.=" crypto/cryptlib.h";
29160484Sobrien$crypto.=" crypto/o_dir.h";
29260484Sobrien$crypto.=" crypto/o_str.h";
29360484Sobrien$crypto.=" crypto/o_time.h";
29460484Sobrien$crypto.=" crypto/des/des.h crypto/des/des_old.h" ; # unless $no_des;
29560484Sobrien$crypto.=" crypto/idea/idea.h" ; # unless $no_idea;
29660484Sobrien$crypto.=" crypto/rc4/rc4.h" ; # unless $no_rc4;
29760484Sobrien$crypto.=" crypto/rc5/rc5.h" ; # unless $no_rc5;
29860484Sobrien$crypto.=" crypto/rc2/rc2.h" ; # unless $no_rc2;
29960484Sobrien$crypto.=" crypto/bf/blowfish.h" ; # unless $no_bf;
30060484Sobrien$crypto.=" crypto/cast/cast.h" ; # unless $no_cast;
30160484Sobrien$crypto.=" crypto/whrlpool/whrlpool.h" ;
30260484Sobrien$crypto.=" crypto/md2/md2.h" ; # unless $no_md2;
30360484Sobrien$crypto.=" crypto/md4/md4.h" ; # unless $no_md4;
30460484Sobrien$crypto.=" crypto/md5/md5.h" ; # unless $no_md5;
30560484Sobrien$crypto.=" crypto/mdc2/mdc2.h" ; # unless $no_mdc2;
30660484Sobrien$crypto.=" crypto/sha/sha.h" ; # unless $no_sha;
30760484Sobrien$crypto.=" crypto/ripemd/ripemd.h" ; # unless $no_ripemd;
30860484Sobrien$crypto.=" crypto/aes/aes.h" ; # unless $no_aes;
30960484Sobrien$crypto.=" crypto/camellia/camellia.h" ; # unless $no_camellia;
31060484Sobrien$crypto.=" crypto/seed/seed.h"; # unless $no_seed;
31160484Sobrien
31260484Sobrien$crypto.=" crypto/bn/bn.h";
31360484Sobrien$crypto.=" crypto/rsa/rsa.h" ; # unless $no_rsa;
31460484Sobrien$crypto.=" crypto/dsa/dsa.h" ; # unless $no_dsa;
31560484Sobrien$crypto.=" crypto/dh/dh.h" ; # unless $no_dh;
31660484Sobrien$crypto.=" crypto/ec/ec.h" ; # unless $no_ec;
31760484Sobrien$crypto.=" crypto/ecdsa/ecdsa.h" ; # unless $no_ecdsa;
31860484Sobrien$crypto.=" crypto/ecdh/ecdh.h" ; # unless $no_ecdh;
31960484Sobrien$crypto.=" crypto/hmac/hmac.h" ; # unless $no_hmac;
32060484Sobrien$crypto.=" crypto/cmac/cmac.h" ; # unless $no_hmac;
32178828Sobrien
32260484Sobrien$crypto.=" crypto/engine/engine.h"; # unless $no_engine;
32360484Sobrien$crypto.=" crypto/stack/stack.h" ; # unless $no_stack;
32478828Sobrien$crypto.=" crypto/buffer/buffer.h" ; # unless $no_buffer;
32560484Sobrien$crypto.=" crypto/bio/bio.h" ; # unless $no_bio;
32660484Sobrien$crypto.=" crypto/dso/dso.h" ; # unless $no_dso;
32778828Sobrien$crypto.=" crypto/lhash/lhash.h" ; # unless $no_lhash;
32860484Sobrien$crypto.=" crypto/conf/conf.h";
32960484Sobrien$crypto.=" crypto/txt_db/txt_db.h";
33044348Simp
33144348Simp$crypto.=" crypto/evp/evp.h" ; # unless $no_evp;
33244348Simp$crypto.=" crypto/objects/objects.h";
33378828Sobrien$crypto.=" crypto/pem/pem.h";
33460484Sobrien#$crypto.=" crypto/meth/meth.h";
33560484Sobrien$crypto.=" crypto/asn1/asn1.h";
33660484Sobrien$crypto.=" crypto/asn1/asn1t.h";
33760484Sobrien$crypto.=" crypto/asn1/asn1_mac.h";
33844348Simp$crypto.=" crypto/err/err.h" ; # unless $no_err;
33944348Simp$crypto.=" crypto/pkcs7/pkcs7.h";
34044348Simp$crypto.=" crypto/pkcs12/pkcs12.h";
34144348Simp$crypto.=" crypto/x509/x509.h";
34244348Simp$crypto.=" crypto/x509/x509_vfy.h";
34360484Sobrien$crypto.=" crypto/x509v3/x509v3.h";
34460484Sobrien$crypto.=" crypto/ts/ts.h";
34560484Sobrien$crypto.=" crypto/rand/rand.h";
34678828Sobrien$crypto.=" crypto/comp/comp.h" ; # unless $no_comp;
34760484Sobrien$crypto.=" crypto/ocsp/ocsp.h";
34860484Sobrien$crypto.=" crypto/ui/ui.h crypto/ui/ui_compat.h";
34978828Sobrien$crypto.=" crypto/krb5/krb5_asn.h";
35060484Sobrien#$crypto.=" crypto/store/store.h";
35160484Sobrien$crypto.=" crypto/pqueue/pqueue.h";
35278828Sobrien$crypto.=" crypto/cms/cms.h";
35360484Sobrien$crypto.=" crypto/jpake/jpake.h";
35460484Sobrien$crypto.=" crypto/modes/modes.h";
35578828Sobrien$crypto.=" crypto/srp/srp.h";
35660484Sobrien
35760484Sobrienmy $symhacks="crypto/symhacks.h";
35878828Sobrien
35960484Sobrienmy @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
36060484Sobrienmy @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
36178828Sobrien
36260484Sobrienif ($do_update) {
36360484Sobrien
36478828Sobrienif ($do_ssl == 1) {
36560484Sobrien
36660484Sobrien	&maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
36760484Sobrien	if ($do_rewrite == 1) {
36844348Simp		open(OUT, ">$ssl_num");
36944348Simp		&rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
37044348Simp	} else {
37144348Simp		open(OUT, ">>$ssl_num");
37244348Simp	}
37344348Simp	&update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
37444348Simp	close OUT;
37544348Simp}
37644348Simp
37744348Simpif($do_crypto == 1) {
37844348Simp
37944348Simp	&maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
38044348Simp	if ($do_rewrite == 1) {
38144348Simp		open(OUT, ">$crypto_num");
38244348Simp		&rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
38344348Simp	} else {
38444348Simp		open(OUT, ">>$crypto_num");
38544348Simp	}
38660484Sobrien	&update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
38760484Sobrien	close OUT;
38860484Sobrien}
38960484Sobrien
39060484Sobrien} elsif ($do_checkexist) {
39160484Sobrien	&check_existing(*ssl_list, @ssl_symbols)
39260484Sobrien		if $do_ssl == 1;
39360484Sobrien	&check_existing(*crypto_list, @crypto_symbols)
39460484Sobrien		if $do_crypto == 1;
39560484Sobrien} elsif ($do_ctest || $do_ctestall) {
39644348Simp
39744348Simp	print <<"EOF";
39844348Simp
39944348Simp/* Test file to check all DEF file symbols are present by trying
40044348Simp * to link to all of them. This is *not* intended to be run!
40144348Simp */
40244348Simp
40344348Simpint main()
40444348Simp{
40560484SobrienEOF
40660484Sobrien	&print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
40760484Sobrien		if $do_ssl == 1;
40860484Sobrien
40960484Sobrien	&print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
41060484Sobrien		if $do_crypto == 1;
41160484Sobrien
41260484Sobrien	print "}\n";
41360484Sobrien
41460484Sobrien} else {
41560484Sobrien
41660484Sobrien	&print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols)
41760484Sobrien		if $do_ssl == 1;
41844348Simp
41944348Simp	&print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols)
42044348Simp		if $do_crypto == 1;
42160484Sobrien
42244348Simp}
42360484Sobrien
42460484Sobrien
42560484Sobriensub do_defs
42644348Simp{
42744348Simp	my($name,$files,$symhacksfile)=@_;
42844348Simp	my $file;
42944348Simp	my @ret;
43044348Simp	my %syms;
43144348Simp	my %platform;		# For anything undefined, we assume ""
43244348Simp	my %kind;		# For anything undefined, we assume "FUNCTION"
43344348Simp	my %algorithm;		# For anything undefined, we assume ""
43444348Simp	my %variant;
43544348Simp	my %variant_cnt;	# To be able to allocate "name{n}" if "name"
43644348Simp				# is the same name as the original.
43744348Simp	my $cpp;
43844348Simp	my %unknown_algorithms = ();
43944348Simp
44044348Simp	foreach $file (split(/\s+/,$symhacksfile." ".$files))
44144348Simp		{
44244348Simp		print STDERR "DEBUG: starting on $file:\n" if $debug;
44344348Simp		open(IN,"<$file") || die "unable to open $file:$!\n";
44444348Simp		my $line = "", my $def= "";
44544348Simp		my %tag = (
44644348Simp			(map { $_ => 0 } @known_platforms),
44744348Simp			(map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
44844348Simp			(map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
44944348Simp			NOPROTO		=> 0,
45044348Simp			PERL5		=> 0,
45144348Simp			_WINDLL		=> 0,
45244348Simp			CONST_STRICT	=> 0,
45344348Simp			TRUE		=> 1,
45444348Simp		);
45544348Simp		my $symhacking = $file eq $symhacksfile;
45644348Simp		my @current_platforms = ();
45744348Simp		my @current_algorithms = ();
45844348Simp
45944348Simp		# params: symbol, alias, platforms, kind
46044348Simp		# The reason to put this subroutine in a variable is that
46144348Simp		# it will otherwise create it's own, unshared, version of
46244348Simp		# %tag and %variant...
46344348Simp		my $make_variant = sub
46444348Simp		{
46544348Simp			my ($s, $a, $p, $k) = @_;
46644348Simp			my ($a1, $a2);
46744348Simp
46844348Simp			print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
46944348Simp			if (defined($p))
47044348Simp			{
47144348Simp				$a1 = join(",",$p,
47244348Simp					   grep(!/^$/,
47344348Simp						map { $tag{$_} == 1 ? $_ : "" }
47444348Simp						@known_platforms));
47544348Simp			}
47644348Simp			else
47744348Simp			{
47844348Simp				$a1 = join(",",
47944348Simp					   grep(!/^$/,
48044348Simp						map { $tag{$_} == 1 ? $_ : "" }
48144348Simp						@known_platforms));
48244348Simp			}
48344348Simp			$a2 = join(",",
48444348Simp				   grep(!/^$/,
48544348Simp					map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
48644348Simp					@known_ossl_platforms));
48744348Simp			print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
48844348Simp			if ($a1 eq "") { $a1 = $a2; }
48944348Simp			elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
490130561Sobrien			if ($a eq $s)
49144348Simp			{
492130561Sobrien				if (!defined($variant_cnt{$s}))
49344348Simp				{
49444348Simp					$variant_cnt{$s} = 0;
49544348Simp				}
49644348Simp				$variant_cnt{$s}++;
49744348Simp				$a .= "{$variant_cnt{$s}}";
49844348Simp			}
49944348Simp			my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
50044348Simp			my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
50144348Simp			if (!grep(/^$togrep$/,
50278828Sobrien				  split(/;/, defined($variant{$s})?$variant{$s}:""))) {
50378828Sobrien				if (defined($variant{$s})) { $variant{$s} .= ";"; }
50478828Sobrien				$variant{$s} .= $toadd;
50544348Simp			}
50678828Sobrien			print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
50778828Sobrien		};
50844348Simp
50944348Simp		print STDERR "DEBUG: parsing ----------\n" if $debug;
51044348Simp		while(<IN>) {
51144348Simp			if (/\/\* Error codes for the \w+ functions\. \*\//)
51244348Simp				{
51344348Simp				undef @tag;
51444348Simp				last;
51544348Simp				}
51644348Simp			if ($line ne '') {
51778828Sobrien				$_ = $line . $_;
51878828Sobrien				$line = '';
51978828Sobrien			}
52078828Sobrien
52144348Simp			if (/\\$/) {
52244348Simp				chomp; # remove eol
52344348Simp				chop; # remove ending backslash
52444348Simp				$line = $_;
52544348Simp				next;
52644348Simp			}
52744348Simp
52844348Simp			if(/\/\*/) {
52960484Sobrien				if (not /\*\//) {	# multiline comment...
53078828Sobrien					$line = $_;	# ... just accumulate
53160484Sobrien					next;
53244348Simp				} else {
53344348Simp					s/\/\*.*?\*\///gs;# wipe it
53444348Simp				}
53544348Simp			}
53644348Simp
53744348Simp			if ($cpp) {
53844348Simp				$cpp++ if /^#\s*if/;
53944348Simp				$cpp-- if /^#\s*endif/;
54044348Simp				next;
54144348Simp	    		}
54244348Simp			$cpp = 1 if /^#.*ifdef.*cplusplus/;
54344348Simp
54444348Simp			s/{[^{}]*}//gs;                      # ignore {} blocks
54544348Simp			print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
54644348Simp			print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
54744348Simp			if (/^\#\s*ifndef\s+(.*)/) {
54844348Simp				push(@tag,"-");
54944348Simp				push(@tag,$1);
55044348Simp				$tag{$1}=-1;
55144348Simp				print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
55244348Simp			} elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
55360484Sobrien				push(@tag,"-");
55460484Sobrien				if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) {
55560484Sobrien					my $tmp_1 = $1;
55644348Simp					my $tmp_;
55744348Simp					foreach $tmp_ (split '\&\&',$tmp_1) {
55844348Simp						$tmp_ =~ /!defined\(([^\)]+)\)/;
55944348Simp						print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
56044348Simp						push(@tag,$1);
56144348Simp						$tag{$1}=-1;
56244348Simp					}
56344348Simp				} else {
56444348Simp					print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O...
56544348Simp					print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
56644348Simp					push(@tag,$1);
56744348Simp					$tag{$1}=-1;
56844348Simp				}
56944348Simp			} elsif (/^\#\s*ifdef\s+(\S*)/) {
57044348Simp				push(@tag,"-");
57144348Simp				push(@tag,$1);
57244348Simp				$tag{$1}=1;
57344348Simp				print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
57444348Simp			} elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
57544348Simp				push(@tag,"-");
57644348Simp				if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) {
57744348Simp					my $tmp_1 = $1;
57844348Simp					my $tmp_;
57944348Simp					foreach $tmp_ (split '\|\|',$tmp_1) {
58044348Simp						$tmp_ =~ /defined\(([^\)]+)\)/;
58144348Simp						print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
58244348Simp						push(@tag,$1);
58344348Simp						$tag{$1}=1;
58444348Simp					}
58560484Sobrien				} else {
58660484Sobrien					print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O...
58760484Sobrien					print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
58860484Sobrien					push(@tag,$1);
58960484Sobrien					$tag{$1}=1;
59060484Sobrien				}
59160484Sobrien			} elsif (/^\#\s*error\s+(\w+) is disabled\./) {
59260484Sobrien				my $tag_i = $#tag;
59360484Sobrien				while($tag[$tag_i] ne "-") {
59460484Sobrien					if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
59560484Sobrien						$tag{$tag[$tag_i]}=2;
59660484Sobrien						print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug;
59760484Sobrien					}
59860484Sobrien					$tag_i--;
59960484Sobrien				}
60060484Sobrien			} elsif (/^\#\s*endif/) {
60160484Sobrien				my $tag_i = $#tag;
60260484Sobrien				while($tag_i > 0 && $tag[$tag_i] ne "-") {
60360484Sobrien					my $t=$tag[$tag_i];
60460484Sobrien					print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
60560484Sobrien					if ($tag{$t}==2) {
60660484Sobrien						$tag{$t}=-1;
60760484Sobrien					} else {
60860484Sobrien						$tag{$t}=0;
60960484Sobrien					}
61060484Sobrien					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
61160484Sobrien					pop(@tag);
61260484Sobrien					if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
61360484Sobrien						$t=$1;
61460484Sobrien					} else {
61560484Sobrien						$t="";
61660484Sobrien					}
61760484Sobrien					if ($t ne ""
61860484Sobrien					    && !grep(/^$t$/, @known_algorithms)) {
61960484Sobrien						$unknown_algorithms{$t} = 1;
62060484Sobrien						#print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
62160484Sobrien					}
62278828Sobrien					$tag_i--;
62360484Sobrien				}
62460484Sobrien				pop(@tag);
62578828Sobrien			} elsif (/^\#\s*else/) {
62660484Sobrien				my $tag_i = $#tag;
62760484Sobrien				while($tag[$tag_i] ne "-") {
62878828Sobrien					my $t=$tag[$tag_i];
62960484Sobrien					$tag{$t}= -$tag{$t};
63060484Sobrien					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
63178828Sobrien					$tag_i--;
63260484Sobrien				}
63360484Sobrien			} elsif (/^\#\s*if\s+1/) {
63478828Sobrien				push(@tag,"-");
63560484Sobrien				# Dummy tag
63660484Sobrien				push(@tag,"TRUE");
63760484Sobrien				$tag{"TRUE"}=1;
63860484Sobrien				print STDERR "DEBUG: $file: found 1\n" if $debug;
63960484Sobrien			} elsif (/^\#\s*if\s+0/) {
64060484Sobrien				push(@tag,"-");
64160484Sobrien				# Dummy tag
64260484Sobrien				push(@tag,"TRUE");
64360484Sobrien				$tag{"TRUE"}=-1;
64460484Sobrien				print STDERR "DEBUG: $file: found 0\n" if $debug;
64560484Sobrien			} elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
64660484Sobrien				 && $symhacking && $tag{'TRUE'} != -1) {
64760484Sobrien				# This is for aliasing.  When we find an alias,
64860484Sobrien				# we have to invert
64960484Sobrien				&$make_variant($1,$2);
65060484Sobrien				print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
65160484Sobrien			}
65260484Sobrien			if (/^\#/) {
65360484Sobrien				@current_platforms =
65460484Sobrien				    grep(!/^$/,
65560484Sobrien					 map { $tag{$_} == 1 ? $_ :
65660484Sobrien						   $tag{$_} == -1 ? "!".$_  : "" }
65760484Sobrien					 @known_platforms);
65860484Sobrien				push @current_platforms
65960484Sobrien				    , grep(!/^$/,
66060484Sobrien					   map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
66160484Sobrien						     $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
66260484Sobrien					   @known_ossl_platforms);
66344348Simp				@current_algorithms =
66444348Simp				    grep(!/^$/,
66544348Simp					 map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
66644348Simp					 @known_algorithms);
66744348Simp				$def .=
66844348Simp				    "#INFO:"
66944348Simp					.join(',',@current_platforms).":"
67044348Simp					    .join(',',@current_algorithms).";";
67144348Simp				next;
67244348Simp			}
67344348Simp			if ($tag{'TRUE'} != -1) {
67444348Simp				if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
67544348Simp					next;
67678828Sobrien				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
67760484Sobrien					$def .= "int d2i_$3(void);";
67878828Sobrien					$def .= "int i2d_$3(void);";
67978828Sobrien					# Variant for platforms that do not
68078828Sobrien					# have to access globale variables
68178828Sobrien					# in shared libraries through functions
68278828Sobrien					$def .=
68378828Sobrien					    "#INFO:"
68478828Sobrien						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
68578828Sobrien						    .join(',',@current_algorithms).";";
68660484Sobrien					$def .= "OPENSSL_EXTERN int $2_it;";
68778828Sobrien					$def .=
68878828Sobrien					    "#INFO:"
68960484Sobrien						.join(',',@current_platforms).":"
69078828Sobrien						    .join(',',@current_algorithms).";";
69178828Sobrien					# Variant for platforms that have to
69278828Sobrien					# access globale variables in shared
69360484Sobrien					# libraries through functions
69478828Sobrien					&$make_variant("$2_it","$2_it",
69578828Sobrien						      "EXPORT_VAR_AS_FUNCTION",
69660484Sobrien						      "FUNCTION");
69778828Sobrien					next;
69878828Sobrien				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
69960484Sobrien					$def .= "int d2i_$3(void);";
70078828Sobrien					$def .= "int i2d_$3(void);";
70178828Sobrien					$def .= "int $3_free(void);";
70260484Sobrien					$def .= "int $3_new(void);";
70378828Sobrien					# Variant for platforms that do not
70478828Sobrien					# have to access globale variables
70560484Sobrien					# in shared libraries through functions
70678828Sobrien					$def .=
70778828Sobrien					    "#INFO:"
70860484Sobrien						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
70978828Sobrien						    .join(',',@current_algorithms).";";
71078828Sobrien					$def .= "OPENSSL_EXTERN int $2_it;";
71160484Sobrien					$def .=
71278828Sobrien					    "#INFO:"
71378828Sobrien						.join(',',@current_platforms).":"
71460484Sobrien						    .join(',',@current_algorithms).";";
71544348Simp					# Variant for platforms that have to
71644348Simp					# access globale variables in shared
71744348Simp					# libraries through functions
71844348Simp					&$make_variant("$2_it","$2_it",
71960484Sobrien						      "EXPORT_VAR_AS_FUNCTION",
72060484Sobrien						      "FUNCTION");
72160484Sobrien					next;
72260484Sobrien				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
72344348Simp					 /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
72444348Simp					$def .= "int d2i_$1(void);";
72544348Simp					$def .= "int i2d_$1(void);";
726218822Sdim					$def .= "int $1_free(void);";
727218822Sdim					$def .= "int $1_new(void);";
728218822Sdim					# Variant for platforms that do not
729218822Sdim					# have to access globale variables
730218822Sdim					# in shared libraries through functions
731218822Sdim					$def .=
732218822Sdim					    "#INFO:"
73344348Simp						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
73444348Simp						    .join(',',@current_algorithms).";";
73544348Simp					$def .= "OPENSSL_EXTERN int $1_it;";
73644348Simp					$def .=
73744348Simp					    "#INFO:"
73844348Simp						.join(',',@current_platforms).":"
73944348Simp						    .join(',',@current_algorithms).";";
74044348Simp					# Variant for platforms that have to
74144348Simp					# access globale variables in shared
74244348Simp					# libraries through functions
74344348Simp					&$make_variant("$1_it","$1_it",
74444348Simp						      "EXPORT_VAR_AS_FUNCTION",
74544348Simp						      "FUNCTION");
74644348Simp					next;
74744348Simp				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
74844348Simp					$def .= "int d2i_$2(void);";
74944348Simp					$def .= "int i2d_$2(void);";
75044348Simp					# Variant for platforms that do not
75144348Simp					# have to access globale variables
75244348Simp					# in shared libraries through functions
75344348Simp					$def .=
75444348Simp					    "#INFO:"
75544348Simp						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
75644348Simp						    .join(',',@current_algorithms).";";
75744348Simp					$def .= "OPENSSL_EXTERN int $2_it;";
75844348Simp					$def .=
75944348Simp					    "#INFO:"
76044348Simp						.join(',',@current_platforms).":"
76144348Simp						    .join(',',@current_algorithms).";";
76244348Simp					# Variant for platforms that have to
76344348Simp					# access globale variables in shared
76444348Simp					# libraries through functions
76544348Simp					&$make_variant("$2_it","$2_it",
76644348Simp						      "EXPORT_VAR_AS_FUNCTION",
76744348Simp						      "FUNCTION");
76844348Simp					next;
76944348Simp				} elsif (/^\s*DECLARE_ASN1_ALLOC_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
77044348Simp					$def .= "int $1_free(void);";
77144348Simp					$def .= "int $1_new(void);";
77244348Simp					next;
77344348Simp				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
77444348Simp					$def .= "int d2i_$2(void);";
77544348Simp					$def .= "int i2d_$2(void);";
77644348Simp					$def .= "int $2_free(void);";
77744348Simp					$def .= "int $2_new(void);";
77844348Simp					# Variant for platforms that do not
77944348Simp					# have to access globale variables
78044348Simp					# in shared libraries through functions
78144348Simp					$def .=
78244348Simp					    "#INFO:"
78344348Simp						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
78444348Simp						    .join(',',@current_algorithms).";";
78544348Simp					$def .= "OPENSSL_EXTERN int $2_it;";
78644348Simp					$def .=
78744348Simp					    "#INFO:"
78844348Simp						.join(',',@current_platforms).":"
78944348Simp						    .join(',',@current_algorithms).";";
79044348Simp					# Variant for platforms that have to
79144348Simp					# access globale variables in shared
79244348Simp					# libraries through functions
79344348Simp					&$make_variant("$2_it","$2_it",
79444348Simp						      "EXPORT_VAR_AS_FUNCTION",
79544348Simp						      "FUNCTION");
79644348Simp					next;
79744348Simp				} elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
79844348Simp					# Variant for platforms that do not
79944348Simp					# have to access globale variables
80044348Simp					# in shared libraries through functions
80144348Simp					$def .=
80244348Simp					    "#INFO:"
80344348Simp						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
80444348Simp						    .join(',',@current_algorithms).";";
80544348Simp					$def .= "OPENSSL_EXTERN int $1_it;";
80644348Simp					$def .=
80744348Simp					    "#INFO:"
80844348Simp						.join(',',@current_platforms).":"
80944348Simp						    .join(',',@current_algorithms).";";
81044348Simp					# Variant for platforms that have to
81144348Simp					# access globale variables in shared
81244348Simp					# libraries through functions
81344348Simp					&$make_variant("$1_it","$1_it",
81478828Sobrien						      "EXPORT_VAR_AS_FUNCTION",
81578828Sobrien						      "FUNCTION");
81678828Sobrien					next;
81778828Sobrien				} elsif (/^\s*DECLARE_ASN1_NDEF_FUNCTION\s*\(\s*(\w*)\s*\)/) {
81878828Sobrien					$def .= "int i2d_$1_NDEF(void);";
81978828Sobrien				} elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
82044348Simp					next;
82144348Simp				} elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION\s*\(\s*(\w*)\s*\)/) {
82244348Simp					$def .= "int $1_print_ctx(void);";
82344348Simp					next;
82444348Simp				} elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
82544348Simp					$def .= "int $2_print_ctx(void);";
82644348Simp					next;
82744348Simp				} elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
82844348Simp					next;
82944348Simp				} elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
83044348Simp					 /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ||
83144348Simp					 /^DECLARE_PEM_rw_const\s*\(\s*(\w*)\s*,/ ) {
83244348Simp					# Things not in Win16
83344348Simp					$def .=
83444348Simp					    "#INFO:"
83544348Simp						.join(',',"!WIN16",@current_platforms).":"
83644348Simp						    .join(',',@current_algorithms).";";
83744348Simp					$def .= "int PEM_read_$1(void);";
83844348Simp					$def .= "int PEM_write_$1(void);";
83944348Simp					$def .=
84044348Simp					    "#INFO:"
84144348Simp						.join(',',@current_platforms).":"
84244348Simp						    .join(',',@current_algorithms).";";
84344348Simp					# Things that are everywhere
84444348Simp					$def .= "int PEM_read_bio_$1(void);";
84544348Simp					$def .= "int PEM_write_bio_$1(void);";
84644348Simp					next;
84744348Simp				} elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
84844348Simp					/^DECLARE_PEM_write_const\s*\(\s*(\w*)\s*,/ ||
84944348Simp					 /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
85044348Simp					# Things not in Win16
85144348Simp					$def .=
85244348Simp					    "#INFO:"
85344348Simp						.join(',',"!WIN16",@current_platforms).":"
85444348Simp						    .join(',',@current_algorithms).";";
85544348Simp					$def .= "int PEM_write_$1(void);";
85644348Simp					$def .=
85744348Simp					    "#INFO:"
85844348Simp						.join(',',@current_platforms).":"
85944348Simp						    .join(',',@current_algorithms).";";
86044348Simp					# Things that are everywhere
86144348Simp					$def .= "int PEM_write_bio_$1(void);";
86244348Simp					next;
863130561Sobrien				} elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
86444348Simp					 /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
865130561Sobrien					# Things not in Win16
86644348Simp					$def .=
86744348Simp					    "#INFO:"
86844348Simp						.join(',',"!WIN16",@current_platforms).":"
86944348Simp						    .join(',',@current_algorithms).";";
87044348Simp					$def .= "int PEM_read_$1(void);";
87144348Simp					$def .=
87244348Simp					    "#INFO:"
87344348Simp						.join(',',@current_platforms).":"
87444348Simp						    .join(',',@current_algorithms).";";
87544348Simp					# Things that are everywhere
87644348Simp					$def .= "int PEM_read_bio_$1(void);";
87744348Simp					next;
87844348Simp				} elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
87944348Simp					# Variant for platforms that do not
88044348Simp					# have to access globale variables
88144348Simp					# in shared libraries through functions
88260484Sobrien					$def .=
88360484Sobrien					    "#INFO:"
88460484Sobrien						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
88560484Sobrien						    .join(',',@current_algorithms).";";
88660484Sobrien					$def .= "OPENSSL_EXTERN int _shadow_$2;";
88760484Sobrien					$def .=
88860484Sobrien					    "#INFO:"
88960484Sobrien						.join(',',@current_platforms).":"
89060484Sobrien						    .join(',',@current_algorithms).";";
89160484Sobrien					# Variant for platforms that have to
89260484Sobrien					# access globale variables in shared
89360484Sobrien					# libraries through functions
89460484Sobrien					&$make_variant("_shadow_$2","_shadow_$2",
89560484Sobrien						      "EXPORT_VAR_AS_FUNCTION",
89660484Sobrien						      "FUNCTION");
89760484Sobrien				} elsif ($tag{'CONST_STRICT'} != 1) {
89860484Sobrien					if (/\{|\/\*|\([^\)]*$/) {
89960484Sobrien						$line = $_;
90060484Sobrien					} else {
90160484Sobrien						$def .= $_;
90260484Sobrien					}
90360484Sobrien				}
90444348Simp			}
90544348Simp		}
90644348Simp		close(IN);
90744348Simp
90844348Simp		my $algs;
90944348Simp		my $plays;
91044348Simp
91144348Simp		print STDERR "DEBUG: postprocessing ----------\n" if $debug;
91244348Simp		foreach (split /;/, $def) {
91344348Simp			my $s; my $k = "FUNCTION"; my $p; my $a;
91444348Simp			s/^[\n\s]*//g;
91544348Simp			s/[\n\s]*$//g;
91644348Simp			next if(/\#undef/);
91744348Simp			next if(/typedef\W/);
91844348Simp			next if(/\#define/);
91944348Simp
92044348Simp			# Reduce argument lists to empty ()
92144348Simp			# fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
92244348Simp			while(/\(.*\)/s) {
92344348Simp				s/\([^\(\)]+\)/\{\}/gs;
92444348Simp				s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;	#(*f{}) -> f
92544348Simp			}
92644348Simp			# pretend as we didn't use curly braces: {} -> ()
92744348Simp			s/\{\}/\(\)/gs;
92844348Simp
92944348Simp			s/STACK_OF\(\)/void/gs;
93044348Simp			s/LHASH_OF\(\)/void/gs;
93160484Sobrien
93260484Sobrien			print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug;
93360484Sobrien			if (/^\#INFO:([^:]*):(.*)$/) {
93460484Sobrien				$plats = $1;
93560484Sobrien				$algs = $2;
93660484Sobrien				print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug;
93760484Sobrien				next;
93860484Sobrien			} elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) {
93960484Sobrien				$s = $1;
94060484Sobrien				$k = "VARIABLE";
94160484Sobrien				print STDERR "DEBUG: found external variable $s\n" if $debug;
94260484Sobrien			} elsif (/TYPEDEF_\w+_OF/s) {
94360484Sobrien				next;
94460484Sobrien			} elsif (/(\w+)\s*\(\).*/s) {	# first token prior [first] () is
94560484Sobrien				$s = $1;		# a function name!
94660484Sobrien				print STDERR "DEBUG: found function $s\n" if $debug;
94760484Sobrien			} elsif (/\(/ and not (/=/)) {
94860484Sobrien				print STDERR "File $file: cannot parse: $_;\n";
94960484Sobrien				next;
95060484Sobrien			} else {
95160484Sobrien				next;
95260484Sobrien			}
95360484Sobrien
95460484Sobrien			$syms{$s} = 1;
95560484Sobrien			$kind{$s} = $k;
95660484Sobrien
95760484Sobrien			$p = $plats;
95860484Sobrien			$a = $algs;
95960484Sobrien			$a .= ",BF" if($s =~ /EVP_bf/);
96060484Sobrien			$a .= ",CAST" if($s =~ /EVP_cast/);
96160484Sobrien			$a .= ",DES" if($s =~ /EVP_des/);
96260484Sobrien			$a .= ",DSA" if($s =~ /EVP_dss/);
96360484Sobrien			$a .= ",IDEA" if($s =~ /EVP_idea/);
96460484Sobrien			$a .= ",MD2" if($s =~ /EVP_md2/);
96544348Simp			$a .= ",MD4" if($s =~ /EVP_md4/);
96644348Simp			$a .= ",MD5" if($s =~ /EVP_md5/);
967130561Sobrien			$a .= ",RC2" if($s =~ /EVP_rc2/);
96844348Simp			$a .= ",RC4" if($s =~ /EVP_rc4/);
969130561Sobrien			$a .= ",RC5" if($s =~ /EVP_rc5/);
97044348Simp			$a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
97160484Sobrien			$a .= ",SHA" if($s =~ /EVP_sha/);
97260484Sobrien			$a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
97360484Sobrien			$a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
97460484Sobrien			$a .= ",RSA" if($s =~ /RSAPrivateKey/);
97560484Sobrien			$a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
97678828Sobrien
97778828Sobrien			$platform{$s} =
97860484Sobrien			    &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
97960484Sobrien			$algorithm{$s} .= ','.$a;
98060484Sobrien
98160484Sobrien			if (defined($variant{$s})) {
98260484Sobrien				foreach $v (split /;/,$variant{$s}) {
98360484Sobrien					(my $r, my $p, my $k) = split(/:/,$v);
98460484Sobrien					my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
98560484Sobrien					$syms{$r} = 1;
98660484Sobrien					if (!defined($k)) { $k = $kind{$s}; }
98760484Sobrien					$kind{$r} = $k."(".$s.")";
98860484Sobrien					$algorithm{$r} = $algorithm{$s};
98960484Sobrien					$platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
99060484Sobrien					$platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
99160484Sobrien					print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
99260484Sobrien				}
99378828Sobrien			}
99478828Sobrien			print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
99578828Sobrien		}
99678828Sobrien	}
99778828Sobrien
99878828Sobrien	# Prune the returned symbols
99978828Sobrien
100060484Sobrien        delete $syms{"bn_dump1"};
100160484Sobrien	$platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
100260484Sobrien
100360484Sobrien	$platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
100460484Sobrien	$platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
100560484Sobrien	$platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
100660484Sobrien	$platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
100760484Sobrien	$platform{"EVP_sha384"} = "!VMSVAX";
100860484Sobrien	$platform{"EVP_sha512"} = "!VMSVAX";
100960484Sobrien	$platform{"SHA384_Init"} = "!VMSVAX";
1010218822Sdim	$platform{"SHA384_Transform"} = "!VMSVAX";
1011218822Sdim	$platform{"SHA384_Update"} = "!VMSVAX";
1012218822Sdim	$platform{"SHA384_Final"} = "!VMSVAX";
1013218822Sdim	$platform{"SHA384"} = "!VMSVAX";
1014218822Sdim	$platform{"SHA512_Init"} = "!VMSVAX";
1015218822Sdim	$platform{"SHA512_Transform"} = "!VMSVAX";
1016218822Sdim	$platform{"SHA512_Update"} = "!VMSVAX";
1017218822Sdim	$platform{"SHA512_Final"} = "!VMSVAX";
1018218822Sdim	$platform{"SHA512"} = "!VMSVAX";
1019218822Sdim	$platform{"WHIRLPOOL_Init"} = "!VMSVAX";
1020218822Sdim	$platform{"WHIRLPOOL"} = "!VMSVAX";
102144348Simp	$platform{"WHIRLPOOL_BitUpdate"} = "!VMSVAX";
1022	$platform{"EVP_whirlpool"} = "!VMSVAX";
1023	$platform{"WHIRLPOOL_Final"} = "!VMSVAX";
1024	$platform{"WHIRLPOOL_Update"} = "!VMSVAX";
1025
1026
1027	# Info we know about
1028
1029	push @ret, map { $_."\\".&info_string($_,"EXIST",
1030					      $platform{$_},
1031					      $kind{$_},
1032					      $algorithm{$_}) } keys %syms;
1033
1034	if (keys %unknown_algorithms) {
1035		print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
1036		print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
1037	}
1038	return(@ret);
1039}
1040
1041# Param: string of comma-separated platform-specs.
1042sub reduce_platforms
1043{
1044	my ($platforms) = @_;
1045	my $pl = defined($platforms) ? $platforms : "";
1046	my %p = map { $_ => 0 } split /,/, $pl;
1047	my $ret;
1048
1049	print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
1050	    if $debug;
1051	# We do this, because if there's code like the following, it really
1052	# means the function exists in all cases and should therefore be
1053	# everywhere.  By increasing and decreasing, we may attain 0:
1054	#
1055	# ifndef WIN16
1056	#    int foo();
1057	# else
1058	#    int _fat foo();
1059	# endif
1060	foreach $platform (split /,/, $pl) {
1061		if ($platform =~ /^!(.*)$/) {
1062			$p{$1}--;
1063		} else {
1064			$p{$platform}++;
1065		}
1066	}
1067	foreach $platform (keys %p) {
1068		if ($p{$platform} == 0) { delete $p{$platform}; }
1069	}
1070
1071	delete $p{""};
1072
1073	$ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
1074	print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
1075	    if $debug;
1076	return $ret;
1077}
1078
1079sub info_string {
1080	(my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
1081
1082	my %a = defined($algorithms) ?
1083	    map { $_ => 1 } split /,/, $algorithms : ();
1084	my $k = defined($kind) ? $kind : "FUNCTION";
1085	my $ret;
1086	my $p = &reduce_platforms($platforms);
1087
1088	delete $a{""};
1089
1090	$ret = $exist;
1091	$ret .= ":".$p;
1092	$ret .= ":".$k;
1093	$ret .= ":".join(',',sort keys %a);
1094	return $ret;
1095}
1096
1097sub maybe_add_info {
1098	(my $name, *nums, my @symbols) = @_;
1099	my $sym;
1100	my $new_info = 0;
1101	my %syms=();
1102
1103	print STDERR "Updating $name info\n";
1104	foreach $sym (@symbols) {
1105		(my $s, my $i) = split /\\/, $sym;
1106		if (defined($nums{$s})) {
1107			$i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
1108			(my $n, my $dummy) = split /\\/, $nums{$s};
1109			if (!defined($dummy) || $i ne $dummy) {
1110				$nums{$s} = $n."\\".$i;
1111				$new_info++;
1112				print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
1113			}
1114		}
1115		$syms{$s} = 1;
1116	}
1117
1118	my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
1119	foreach $sym (@s) {
1120		(my $n, my $i) = split /\\/, $nums{$sym};
1121		if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
1122			$new_info++;
1123			print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
1124		}
1125	}
1126	if ($new_info) {
1127		print STDERR "$new_info old symbols got an info update\n";
1128		if (!$do_rewrite) {
1129			print STDERR "You should do a rewrite to fix this.\n";
1130		}
1131	} else {
1132		print STDERR "No old symbols needed info update\n";
1133	}
1134}
1135
1136# Param: string of comma-separated keywords, each possibly prefixed with a "!"
1137sub is_valid
1138{
1139	my ($keywords_txt,$platforms) = @_;
1140	my (@keywords) = split /,/,$keywords_txt;
1141	my ($falsesum, $truesum) = (0, 1);
1142
1143	# Param: one keyword
1144	sub recognise
1145	{
1146		my ($keyword,$platforms) = @_;
1147
1148		if ($platforms) {
1149			# platforms
1150			if ($keyword eq "VMSVAX" && $VMSVAX) { return 1; }
1151			if ($keyword eq "VMSNonVAX" && $VMSNonVAX) { return 1; }
1152			if ($keyword eq "VMS" && $VMS) { return 1; }
1153			if ($keyword eq "WIN32" && $W32) { return 1; }
1154			if ($keyword eq "WIN16" && $W16) { return 1; }
1155			if ($keyword eq "WINNT" && $NT) { return 1; }
1156			if ($keyword eq "OS2" && $OS2) { return 1; }
1157			# Special platforms:
1158			# EXPORT_VAR_AS_FUNCTION means that global variables
1159			# will be represented as functions.  This currently
1160			# only happens on VMS-VAX.
1161			if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && ($VMSVAX || $W32 || $W16)) {
1162				return 1;
1163			}
1164			if ($keyword eq "OPENSSL_FIPS" && $fips) {
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 "RIPEMD" && $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 "ECDSA" && $no_ecdsa) { return 0; }
1190			if ($keyword eq "ECDH" && $no_ecdh) { return 0; }
1191			if ($keyword eq "HMAC" && $no_hmac) { return 0; }
1192			if ($keyword eq "AES" && $no_aes) { return 0; }
1193			if ($keyword eq "CAMELLIA" && $no_camellia) { return 0; }
1194			if ($keyword eq "SEED" && $no_seed) { 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 "KRB5" && $no_krb5) { return 0; }
1204			if ($keyword eq "ENGINE" && $no_engine) { return 0; }
1205			if ($keyword eq "HW" && $no_hw) { return 0; }
1206			if ($keyword eq "FP_API" && $no_fp_api) { return 0; }
1207			if ($keyword eq "STATIC_ENGINE" && $no_static_engine) { return 0; }
1208			if ($keyword eq "GMP" && $no_gmp) { return 0; }
1209			if ($keyword eq "RFC3779" && $no_rfc3779) { return 0; }
1210			if ($keyword eq "TLSEXT" && $no_tlsext) { return 0; }
1211			if ($keyword eq "PSK" && $no_psk) { return 0; }
1212			if ($keyword eq "CMS" && $no_cms) { return 0; }
1213			if ($keyword eq "EC2M" && $no_ec2m) { return 0; }
1214			if ($keyword eq "NEXTPROTONEG" && $no_nextprotoneg) { return 0; }
1215			if ($keyword eq "EC_NISTP_64_GCC_128" && $no_nistp_gcc)
1216					{ return 0; }
1217			if ($keyword eq "SSL2" && $no_ssl2) { return 0; }
1218			if ($keyword eq "SSL3_METHOD" && $no_ssl3_method) { return 0; }
1219			if ($keyword eq "SSL_TRACE" && $no_ssl_trace) { return 0; }
1220			if ($keyword eq "CAPIENG" && $no_capieng) { return 0; }
1221			if ($keyword eq "JPAKE" && $no_jpake) { return 0; }
1222			if ($keyword eq "SRP" && $no_srp) { return 0; }
1223			if ($keyword eq "SCTP" && $no_sctp) { return 0; }
1224			if ($keyword eq "SRTP" && $no_srtp) { return 0; }
1225			if ($keyword eq "UNIT_TEST" && $no_unit_test) { return 0; }
1226			if ($keyword eq "DEPRECATED" && $no_deprecated) { return 0; }
1227
1228			# Nothing recognise as true
1229			return 1;
1230		}
1231	}
1232
1233	foreach $k (@keywords) {
1234		if ($k =~ /^!(.*)$/) {
1235			$falsesum += &recognise($1,$platforms);
1236		} else {
1237			$truesum *= &recognise($k,$platforms);
1238		}
1239	}
1240	print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1241	return (!$falsesum) && $truesum;
1242}
1243
1244sub print_test_file
1245{
1246	(*OUT,my $name,*nums,my $testall,my @symbols)=@_;
1247	my $n = 1; my @e; my @r;
1248	my $sym; my $prev = ""; my $prefSSLeay;
1249
1250	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1251	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1252	@symbols=((sort @e),(sort @r));
1253
1254	foreach $sym (@symbols) {
1255		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1256		my $v = 0;
1257		$v = 1 if $i=~ /^.*?:.*?:VARIABLE/;
1258		my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1259		my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1260		if (!defined($nums{$s})) {
1261			print STDERR "Warning: $s does not have a number assigned\n"
1262			    if(!$do_update);
1263		} elsif (is_valid($p,1) && is_valid($a,0)) {
1264			my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1265			if ($prev eq $s2) {
1266				print OUT "\t/* The following has already appeared previously */\n";
1267				print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1268			}
1269			$prev = $s2;	# To warn about duplicates...
1270
1271			($nn,$ni)=($nums{$s2} =~ /^(.*?)\\(.*)$/);
1272			if ($v) {
1273				print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n";
1274			} else {
1275				print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n";
1276			}
1277		}
1278	}
1279}
1280
1281sub get_version {
1282   local *MF;
1283   my $v = '?';
1284   open MF, 'Makefile' or return $v;
1285   while (<MF>) {
1286     $v = $1, last if /^VERSION=(.*?)\s*$/;
1287   }
1288   close MF;
1289   return $v;
1290}
1291
1292sub print_def_file
1293{
1294	(*OUT,my $name,*nums,my @symbols)=@_;
1295	my $n = 1; my @e; my @r; my @v; my $prev="";
1296	my $liboptions="";
1297	my $libname = $name;
1298	my $http_vendor = 'www.openssl.org/';
1299	my $version = get_version();
1300	my $what = "OpenSSL: implementation of Secure Socket Layer";
1301	my $description = "$what $version, $name - http://$http_vendor";
1302
1303	if ($W32)
1304		{ $libname.="32"; }
1305	elsif ($W16)
1306		{ $libname.="16"; }
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;
1315INITINSTANCE
1316DATA MULTIPLE NONSHARED
1317EOO
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	print OUT <<"EOF";
1323;
1324; Definition file for the DLL version of the $name library from OpenSSL
1325;
1326
1327LIBRARY         $libname	$liboptions
1328
1329EOF
1330
1331	if ($W16) {
1332		print <<"EOF";
1333CODE            PRELOAD MOVEABLE
1334DATA            PRELOAD MOVEABLE SINGLE
1335
1336EXETYPE		WINDOWS
1337
1338HEAPSIZE	4096
1339STACKSIZE	8192
1340
1341EOF
1342	}
1343
1344	print "EXPORTS\n";
1345
1346	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1347	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1348	(@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols);
1349	@symbols=((sort @e),(sort @r), (sort @v));
1350
1351
1352	foreach $sym (@symbols) {
1353		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1354		my $v = 0;
1355		$v = 1 if $i =~ /^.*?:.*?:VARIABLE/;
1356		if (!defined($nums{$s})) {
1357			printf STDERR "Warning: $s does not have a number assigned\n"
1358			    if(!$do_update);
1359		} else {
1360			(my $n, my $dummy) = split /\\/, $nums{$s};
1361			my %pf = ();
1362			my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1363			my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1364			if (is_valid($p,1) && is_valid($a,0)) {
1365				my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1366				if ($prev eq $s2) {
1367					print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1368				}
1369				$prev = $s2;	# To warn about duplicates...
1370				if($v && !$OS2) {
1371					printf OUT "    %s%-39s @%-8d DATA\n",($W32)?"":"_",$s2,$n;
1372				} else {
1373					printf OUT "    %s%-39s @%d\n",($W32||$OS2)?"":"_",$s2,$n;
1374				}
1375			}
1376		}
1377	}
1378	printf OUT "\n";
1379}
1380
1381sub load_numbers
1382{
1383	my($name)=@_;
1384	my(@a,%ret);
1385
1386	$max_num = 0;
1387	$num_noinfo = 0;
1388	$prev = "";
1389	$prev_cnt = 0;
1390
1391	open(IN,"<$name") || die "unable to open $name:$!\n";
1392	while (<IN>) {
1393		chop;
1394		s/#.*$//;
1395		next if /^\s*$/;
1396		@a=split;
1397		if (defined $ret{$a[0]}) {
1398			# This is actually perfectly OK
1399			#print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
1400		}
1401		if ($max_num > $a[1]) {
1402			print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
1403		}
1404		elsif ($max_num == $a[1]) {
1405			# This is actually perfectly OK
1406			#print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1407			if ($a[0] eq $prev) {
1408				$prev_cnt++;
1409				$a[0] .= "{$prev_cnt}";
1410			}
1411		}
1412		else {
1413			$prev_cnt = 0;
1414		}
1415		if ($#a < 2) {
1416			# Existence will be proven later, in do_defs
1417			$ret{$a[0]}=$a[1];
1418			$num_noinfo++;
1419		} else {
1420			$ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
1421		}
1422		$max_num = $a[1] if $a[1] > $max_num;
1423		$prev=$a[0];
1424	}
1425	if ($num_noinfo) {
1426		print STDERR "Warning: $num_noinfo symbols were without info.";
1427		if ($do_rewrite) {
1428			printf STDERR "  The rewrite will fix this.\n";
1429		} else {
1430			printf STDERR "  You should do a rewrite to fix this.\n";
1431		}
1432	}
1433	close(IN);
1434	return(%ret);
1435}
1436
1437sub parse_number
1438{
1439	(my $str, my $what) = @_;
1440	(my $n, my $i) = split(/\\/,$str);
1441	if ($what eq "n") {
1442		return $n;
1443	} else {
1444		return $i;
1445	}
1446}
1447
1448sub rewrite_numbers
1449{
1450	(*OUT,$name,*nums,@symbols)=@_;
1451	my $thing;
1452
1453	print STDERR "Rewriting $name\n";
1454
1455	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1456	my $r; my %r; my %rsyms;
1457	foreach $r (@r) {
1458		(my $s, my $i) = split /\\/, $r;
1459		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1460		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1461		$r{$a} = $s."\\".$i;
1462		$rsyms{$s} = 1;
1463	}
1464
1465	my %syms = ();
1466	foreach $_ (@symbols) {
1467		(my $n, my $i) = split /\\/;
1468		$syms{$n} = 1;
1469	}
1470
1471	my @s=sort {
1472	    &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1473	    || $a cmp $b
1474	} keys %nums;
1475	foreach $sym (@s) {
1476		(my $n, my $i) = split /\\/, $nums{$sym};
1477		next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
1478		next if defined($rsyms{$sym});
1479		print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1480		$i="NOEXIST::FUNCTION:"
1481			if !defined($i) || $i eq "" || !defined($syms{$sym});
1482		my $s2 = $sym;
1483		$s2 =~ s/\{[0-9]+\}$//;
1484		printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
1485		if (exists $r{$sym}) {
1486			(my $s, $i) = split /\\/,$r{$sym};
1487			my $s2 = $s;
1488			$s2 =~ s/\{[0-9]+\}$//;
1489			printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
1490		}
1491	}
1492}
1493
1494sub update_numbers
1495{
1496	(*OUT,$name,*nums,my $start_num, my @symbols)=@_;
1497	my $new_syms = 0;
1498
1499	print STDERR "Updating $name numbers\n";
1500
1501	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1502	my $r; my %r; my %rsyms;
1503	foreach $r (@r) {
1504		(my $s, my $i) = split /\\/, $r;
1505		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1506		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1507		$r{$a} = $s."\\".$i;
1508		$rsyms{$s} = 1;
1509	}
1510
1511	foreach $sym (@symbols) {
1512		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1513		next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
1514		next if defined($rsyms{$sym});
1515		die "ERROR: Symbol $sym had no info attached to it."
1516		    if $i eq "";
1517		if (!exists $nums{$s}) {
1518			$new_syms++;
1519			my $s2 = $s;
1520			$s2 =~ s/\{[0-9]+\}$//;
1521			printf OUT "%s%-39s %d\t%s\n","",$s2, ++$start_num,$i;
1522			if (exists $r{$s}) {
1523				($s, $i) = split /\\/,$r{$s};
1524				$s =~ s/\{[0-9]+\}$//;
1525				printf OUT "%s%-39s %d\t%s\n","",$s, $start_num,$i;
1526			}
1527		}
1528	}
1529	if($new_syms) {
1530		print STDERR "$new_syms New symbols added\n";
1531	} else {
1532		print STDERR "No New symbols Added\n";
1533	}
1534}
1535
1536sub check_existing
1537{
1538	(*nums, my @symbols)=@_;
1539	my %existing; my @remaining;
1540	@remaining=();
1541	foreach $sym (@symbols) {
1542		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1543		$existing{$s}=1;
1544	}
1545	foreach $sym (keys %nums) {
1546		if (!exists $existing{$sym}) {
1547			push @remaining, $sym;
1548		}
1549	}
1550	if(@remaining) {
1551		print STDERR "The following symbols do not seem to exist:\n";
1552		foreach $sym (@remaining) {
1553			print STDERR "\t",$sym,"\n";
1554		}
1555	}
1556}
1557
1558