mkdef.pl revision 205128
155714Skris#!/usr/local/bin/perl -w
255714Skris#
355714Skris# generate a .def file
455714Skris#
555714Skris# It does this by parsing the header files and looking for the
655714Skris# prototyped functions: it then prunes the output.
755714Skris#
868651Skris# Intermediary files are created, call libeay.num and ssleay.num,...
968651Skris# Previously, they had the following format:
1068651Skris#
1168651Skris#	routine-name	nnnn
1268651Skris#
1368651Skris# But that isn't enough for a number of reasons, the first on being that
1468651Skris# this format is (needlessly) very Win32-centric, and even then...
1568651Skris# One of the biggest problems is that there's no information about what
1668651Skris# routines should actually be used, which varies with what crypto algorithms
1768651Skris# are disabled.  Also, some operating systems (for example VMS with VAX C)
1868651Skris# need to keep track of the global variables as well as the functions.
1968651Skris#
2068651Skris# So, a remake of this script is done so as to include information on the
2168651Skris# kind of symbol it is (function or variable) and what algorithms they're
2268651Skris# part of.  This will allow easy translating to .def files or the corresponding
2368651Skris# file in other operating systems (a .opt file for VMS, possibly with a .mar
2468651Skris# file).
2568651Skris#
2668651Skris# The format now becomes:
2768651Skris#
2868651Skris#	routine-name	nnnn	info
2968651Skris#
3068651Skris# and the "info" part is actually a colon-separated string of fields with
3168651Skris# the following meaning:
3268651Skris#
3368651Skris#	existence:platform:kind:algorithms
3468651Skris#
3568651Skris# - "existence" can be "EXIST" or "NOEXIST" depending on if the symbol is
3668651Skris#   found somewhere in the source,
3768651Skris# - "platforms" is empty if it exists on all platforms, otherwise it contains
3868651Skris#   comma-separated list of the platform, just as they are if the symbol exists
3968651Skris#   for those platforms, or prepended with a "!" if not.  This helps resolve
40109998Smarkm#   symbol name variants for platforms where the names are too long for the
4168651Skris#   compiler or linker, or if the systems is case insensitive and there is a
42109998Smarkm#   clash, or the symbol is implemented differently (see
43109998Smarkm#   EXPORT_VAR_AS_FUNCTION).  This script assumes renaming of symbols is found
44109998Smarkm#   in the file crypto/symhacks.h.
45109998Smarkm#   The semantics for the platforms is that every item is checked against the
46109998Smarkm#   environment.  For the negative items ("!FOO"), if any of them is false
47109998Smarkm#   (i.e. "FOO" is true) in the environment, the corresponding symbol can't be
48109998Smarkm#   used.  For the positive itms, if all of them are false in the environment,
49109998Smarkm#   the corresponding symbol can't be used.  Any combination of positive and
50109998Smarkm#   negative items are possible, and of course leave room for some redundancy.
5168651Skris# - "kind" is "FUNCTION" or "VARIABLE".  The meaning of that is obvious.
5268651Skris# - "algorithms" is a comma-separated list of algorithm names.  This helps
5368651Skris#   exclude symbols that are part of an algorithm that some user wants to
5468651Skris#   exclude.
5568651Skris#
5655714Skris
57109998Smarkmmy $debug=0;
58109998Smarkm
5968651Skrismy $crypto_num= "util/libeay.num";
6068651Skrismy $ssl_num=    "util/ssleay.num";
61109998Smarkmmy $libname;
6255714Skris
6355714Skrismy $do_update = 0;
64109998Smarkmmy $do_rewrite = 1;
6555714Skrismy $do_crypto = 0;
6655714Skrismy $do_ssl = 0;
6759191Skrismy $do_ctest = 0;
6868651Skrismy $do_ctestall = 0;
69109998Smarkmmy $do_checkexist = 0;
7055714Skris
71109998Smarkmmy $VMSVAX=0;
72205128Ssimonmy $VMSNonVAX=0;
7368651Skrismy $VMS=0;
7468651Skrismy $W32=0;
7568651Skrismy $W16=0;
7659191Skrismy $NT=0;
77109998Smarkmmy $OS2=0;
7855714Skris# Set this to make typesafe STACK definitions appear in DEF
7968651Skrismy $safe_stack_def = 0;
8055714Skris
81205128Ssimonmy @known_platforms = ( "__FreeBSD__", "PERL5", "NeXT", "NETWARE",
82194206Ssimon			"EXPORT_VAR_AS_FUNCTION", "ZLIB", "OPENSSL_FIPS");
83109998Smarkmmy @known_ossl_platforms = ( "VMS", "WIN16", "WIN32", "WINNT", "OS2" );
8468651Skrismy @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
85109998Smarkm			 "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1",
86160814Ssimon			 "SHA256", "SHA512", "RIPEMD",
87194206Ssimon			 "MDC2", "RSA", "DSA", "DH", "EC", "ECDH", "ECDSA", "HMAC", "AES", "CAMELLIA", "SEED",
88109998Smarkm			 # Envelope "algorithms"
89109998Smarkm			 "EVP", "X509", "ASN1_TYPEDEFS",
90109998Smarkm			 # Helper "algorithms"
91109998Smarkm			 "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
92109998Smarkm			 "LOCKING",
93109998Smarkm			 # External "algorithms"
94160814Ssimon			 "FP_API", "STDIO", "SOCK", "KRB5", "DGRAM",
95160814Ssimon			 # Engines
96160814Ssimon			 "STATIC_ENGINE", "ENGINE", "HW", "GMP",
97167612Ssimon			 # RFC3779 support
98167612Ssimon			 "RFC3779",
99194206Ssimon			 # TLS extension support
100194206Ssimon			 "TLSEXT",
101194206Ssimon			 # CMS
102194206Ssimon			 "CMS",
103194206Ssimon			 # CryptoAPI Engine
104194206Ssimon			 "CAPIENG",
105194206Ssimon			 # JPAKE
106194206Ssimon			 "JPAKE",
107160814Ssimon			 # Deprecated functions
108160814Ssimon			 "DEPRECATED" );
10968651Skris
11059191Skrismy $options="";
111142425Snectaropen(IN,"<Makefile") || die "unable to open Makefile!\n";
11255714Skriswhile(<IN>) {
11355714Skris    $options=$1 if (/^OPTIONS=(.*)$/);
11455714Skris}
11555714Skrisclose(IN);
11655714Skris
11759191Skris# The following ciphers may be excluded (by Configure). This means functions
11859191Skris# defined with ifndef(NO_XXX) are not included in the .def file, and everything
11959191Skris# in directory xxx is ignored.
12059191Skrismy $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
12168651Skrismy $no_cast;
12268651Skrismy $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
123109998Smarkmmy $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_aes; my $no_krb5;
124162911Ssimonmy $no_ec; my $no_ecdsa; my $no_ecdh; my $no_engine; my $no_hw; my $no_camellia;
125194206Ssimonmy $no_seed;
126160814Ssimonmy $no_fp_api; my $no_static_engine; my $no_gmp; my $no_deprecated;
127194206Ssimonmy $no_rfc3779; my $no_tlsext; my $no_cms; my $no_capieng; my $no_jpake;
128194206Ssimonmy $fips;
12959191Skris
130160814Ssimon
13155714Skrisforeach (@ARGV, split(/ /, $options))
13255714Skris	{
133109998Smarkm	$debug=1 if $_ eq "debug";
13455714Skris	$W32=1 if $_ eq "32";
13568651Skris	$W16=1 if $_ eq "16";
13655714Skris	if($_ eq "NT") {
13755714Skris		$W32 = 1;
13855714Skris		$NT = 1;
13955714Skris	}
140109998Smarkm	if ($_ eq "VMS-VAX") {
141109998Smarkm		$VMS=1;
142109998Smarkm		$VMSVAX=1;
143109998Smarkm	}
144205128Ssimon	if ($_ eq "VMS-NonVAX") {
145109998Smarkm		$VMS=1;
146205128Ssimon		$VMSNonVAX=1;
147109998Smarkm	}
14868651Skris	$VMS=1 if $_ eq "VMS";
149109998Smarkm	$OS2=1 if $_ eq "OS2";
150194206Ssimon	$fips=1 if /^fips/;
15168651Skris
152194206Ssimon	if ($_ eq "zlib" || $_ eq "zlib-dynamic"
153194206Ssimon 			 || $_ eq "enable-zlib-dynamic") {
154194206Ssimon 		$zlib = 1;
155194206Ssimon	}
156194206Ssimon
15755714Skris	$do_ssl=1 if $_ eq "ssleay";
158109998Smarkm	if ($_ eq "ssl") {
159109998Smarkm		$do_ssl=1;
160109998Smarkm		$libname=$_
161109998Smarkm	}
16255714Skris	$do_crypto=1 if $_ eq "libeay";
163109998Smarkm	if ($_ eq "crypto") {
164109998Smarkm		$do_crypto=1;
165109998Smarkm		$libname=$_;
166109998Smarkm	}
167160814Ssimon	$no_static_engine=1 if $_ eq "no-static-engine";
168160814Ssimon	$no_static_engine=0 if $_ eq "enable-static-engine";
16955714Skris	$do_update=1 if $_ eq "update";
17068651Skris	$do_rewrite=1 if $_ eq "rewrite";
17159191Skris	$do_ctest=1 if $_ eq "ctest";
17268651Skris	$do_ctestall=1 if $_ eq "ctestall";
173109998Smarkm	$do_checkexist=1 if $_ eq "exist";
17468651Skris	#$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
17555714Skris
17655714Skris	if    (/^no-rc2$/)      { $no_rc2=1; }
17755714Skris	elsif (/^no-rc4$/)      { $no_rc4=1; }
17855714Skris	elsif (/^no-rc5$/)      { $no_rc5=1; }
17955714Skris	elsif (/^no-idea$/)     { $no_idea=1; }
18072613Skris	elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
18155714Skris	elsif (/^no-bf$/)       { $no_bf=1; }
18255714Skris	elsif (/^no-cast$/)     { $no_cast=1; }
18355714Skris	elsif (/^no-md2$/)      { $no_md2=1; }
18468651Skris	elsif (/^no-md4$/)      { $no_md4=1; }
18555714Skris	elsif (/^no-md5$/)      { $no_md5=1; }
18655714Skris	elsif (/^no-sha$/)      { $no_sha=1; }
18755714Skris	elsif (/^no-ripemd$/)   { $no_ripemd=1; }
18855714Skris	elsif (/^no-mdc2$/)     { $no_mdc2=1; }
18955714Skris	elsif (/^no-rsa$/)      { $no_rsa=1; }
19055714Skris	elsif (/^no-dsa$/)      { $no_dsa=1; }
19155714Skris	elsif (/^no-dh$/)       { $no_dh=1; }
192109998Smarkm	elsif (/^no-ec$/)       { $no_ec=1; }
193160814Ssimon	elsif (/^no-ecdsa$/)	{ $no_ecdsa=1; }
194160814Ssimon	elsif (/^no-ecdh$/) 	{ $no_ecdh=1; }
19555714Skris	elsif (/^no-hmac$/)	{ $no_hmac=1; }
196109998Smarkm	elsif (/^no-aes$/)	{ $no_aes=1; }
197162911Ssimon	elsif (/^no-camellia$/)	{ $no_camellia=1; }
198194206Ssimon	elsif (/^no-seed$/)     { $no_seed=1; }
199109998Smarkm	elsif (/^no-evp$/)	{ $no_evp=1; }
200109998Smarkm	elsif (/^no-lhash$/)	{ $no_lhash=1; }
201109998Smarkm	elsif (/^no-stack$/)	{ $no_stack=1; }
202109998Smarkm	elsif (/^no-err$/)	{ $no_err=1; }
203109998Smarkm	elsif (/^no-buffer$/)	{ $no_buffer=1; }
204109998Smarkm	elsif (/^no-bio$/)	{ $no_bio=1; }
205109998Smarkm	#elsif (/^no-locking$/)	{ $no_locking=1; }
206109998Smarkm	elsif (/^no-comp$/)	{ $no_comp=1; }
207109998Smarkm	elsif (/^no-dso$/)	{ $no_dso=1; }
208109998Smarkm	elsif (/^no-krb5$/)	{ $no_krb5=1; }
209111147Snectar	elsif (/^no-engine$/)	{ $no_engine=1; }
210111147Snectar	elsif (/^no-hw$/)	{ $no_hw=1; }
211160814Ssimon	elsif (/^no-gmp$/)	{ $no_gmp=1; }
212167612Ssimon	elsif (/^no-rfc3779$/)	{ $no_rfc3779=1; }
213194206Ssimon	elsif (/^no-tlsext$/)	{ $no_tlsext=1; }
214194206Ssimon	elsif (/^no-cms$/)	{ $no_cms=1; }
215194206Ssimon	elsif (/^no-capieng$/)	{ $no_capieng=1; }
216194206Ssimon	elsif (/^no-jpake$/)	{ $no_jpake=1; }
21755714Skris	}
21855714Skris
21959191Skris
220109998Smarkmif (!$libname) {
221109998Smarkm	if ($do_ssl) {
222109998Smarkm		$libname="SSLEAY";
223109998Smarkm	}
224109998Smarkm	if ($do_crypto) {
225109998Smarkm		$libname="LIBEAY";
226109998Smarkm	}
227109998Smarkm}
228109998Smarkm
22968651Skris# If no platform is given, assume WIN32
230109998Smarkmif ($W32 + $W16 + $VMS + $OS2 == 0) {
23168651Skris	$W32 = 1;
23268651Skris}
23368651Skris
23468651Skris# Add extra knowledge
23568651Skrisif ($W16) {
23668651Skris	$no_fp_api=1;
23768651Skris}
23868651Skris
23955714Skrisif (!$do_ssl && !$do_crypto)
24055714Skris	{
241109998Smarkm	print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 ]\n";
24255714Skris	exit(1);
24355714Skris	}
24455714Skris
24555714Skris%ssl_list=&load_numbers($ssl_num);
24655714Skris$max_ssl = $max_num;
24755714Skris%crypto_list=&load_numbers($crypto_num);
24855714Skris$max_crypto = $max_num;
24955714Skris
25059191Skrismy $ssl="ssl/ssl.h";
251109998Smarkm$ssl.=" ssl/kssl.h";
252194206Ssimon$ssl.=" ssl/tls1.h";
25355714Skris
25459191Skrismy $crypto ="crypto/crypto.h";
255160814Ssimon$crypto.=" crypto/o_dir.h";
256109998Smarkm$crypto.=" crypto/des/des.h crypto/des/des_old.h" ; # unless $no_des;
257109998Smarkm$crypto.=" crypto/idea/idea.h" ; # unless $no_idea;
258109998Smarkm$crypto.=" crypto/rc4/rc4.h" ; # unless $no_rc4;
259109998Smarkm$crypto.=" crypto/rc5/rc5.h" ; # unless $no_rc5;
260109998Smarkm$crypto.=" crypto/rc2/rc2.h" ; # unless $no_rc2;
261109998Smarkm$crypto.=" crypto/bf/blowfish.h" ; # unless $no_bf;
262109998Smarkm$crypto.=" crypto/cast/cast.h" ; # unless $no_cast;
263109998Smarkm$crypto.=" crypto/md2/md2.h" ; # unless $no_md2;
264109998Smarkm$crypto.=" crypto/md4/md4.h" ; # unless $no_md4;
265109998Smarkm$crypto.=" crypto/md5/md5.h" ; # unless $no_md5;
266109998Smarkm$crypto.=" crypto/mdc2/mdc2.h" ; # unless $no_mdc2;
267109998Smarkm$crypto.=" crypto/sha/sha.h" ; # unless $no_sha;
268109998Smarkm$crypto.=" crypto/ripemd/ripemd.h" ; # unless $no_ripemd;
269109998Smarkm$crypto.=" crypto/aes/aes.h" ; # unless $no_aes;
270162911Ssimon$crypto.=" crypto/camellia/camellia.h" ; # unless $no_camellia;
271194206Ssimon$crypto.=" crypto/seed/seed.h"; # unless $no_seed;
27255714Skris
27355714Skris$crypto.=" crypto/bn/bn.h";
274109998Smarkm$crypto.=" crypto/rsa/rsa.h" ; # unless $no_rsa;
275109998Smarkm$crypto.=" crypto/dsa/dsa.h" ; # unless $no_dsa;
276109998Smarkm$crypto.=" crypto/dh/dh.h" ; # unless $no_dh;
277109998Smarkm$crypto.=" crypto/ec/ec.h" ; # unless $no_ec;
278160814Ssimon$crypto.=" crypto/ecdsa/ecdsa.h" ; # unless $no_ecdsa;
279160814Ssimon$crypto.=" crypto/ecdh/ecdh.h" ; # unless $no_ecdh;
280109998Smarkm$crypto.=" crypto/hmac/hmac.h" ; # unless $no_hmac;
28155714Skris
282111147Snectar$crypto.=" crypto/engine/engine.h"; # unless $no_engine;
283109998Smarkm$crypto.=" crypto/stack/stack.h" ; # unless $no_stack;
284109998Smarkm$crypto.=" crypto/buffer/buffer.h" ; # unless $no_buffer;
285109998Smarkm$crypto.=" crypto/bio/bio.h" ; # unless $no_bio;
286109998Smarkm$crypto.=" crypto/dso/dso.h" ; # unless $no_dso;
287109998Smarkm$crypto.=" crypto/lhash/lhash.h" ; # unless $no_lhash;
28855714Skris$crypto.=" crypto/conf/conf.h";
28955714Skris$crypto.=" crypto/txt_db/txt_db.h";
29055714Skris
291109998Smarkm$crypto.=" crypto/evp/evp.h" ; # unless $no_evp;
29255714Skris$crypto.=" crypto/objects/objects.h";
29355714Skris$crypto.=" crypto/pem/pem.h";
29455714Skris#$crypto.=" crypto/meth/meth.h";
29555714Skris$crypto.=" crypto/asn1/asn1.h";
296109998Smarkm$crypto.=" crypto/asn1/asn1t.h";
29755714Skris$crypto.=" crypto/asn1/asn1_mac.h";
298109998Smarkm$crypto.=" crypto/err/err.h" ; # unless $no_err;
29955714Skris$crypto.=" crypto/pkcs7/pkcs7.h";
30055714Skris$crypto.=" crypto/pkcs12/pkcs12.h";
30155714Skris$crypto.=" crypto/x509/x509.h";
30255714Skris$crypto.=" crypto/x509/x509_vfy.h";
30355714Skris$crypto.=" crypto/x509v3/x509v3.h";
30455714Skris$crypto.=" crypto/rand/rand.h";
305109998Smarkm$crypto.=" crypto/comp/comp.h" ; # unless $no_comp;
306109998Smarkm$crypto.=" crypto/ocsp/ocsp.h";
307109998Smarkm$crypto.=" crypto/ui/ui.h crypto/ui/ui_compat.h";
308109998Smarkm$crypto.=" crypto/krb5/krb5_asn.h";
30955714Skris$crypto.=" crypto/tmdiff.h";
310160814Ssimon$crypto.=" crypto/store/store.h";
311160814Ssimon$crypto.=" crypto/pqueue/pqueue.h";
312194206Ssimon$crypto.=" crypto/cms/cms.h";
313194206Ssimon$crypto.=" crypto/jpake/jpake.h";
314194206Ssimon$crypto.=" fips/fips.h fips/rand/fips_rand.h";
31555714Skris
31668651Skrismy $symhacks="crypto/symhacks.h";
31755714Skris
31868651Skrismy @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
31968651Skrismy @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
32055714Skris
32155714Skrisif ($do_update) {
32255714Skris
32355714Skrisif ($do_ssl == 1) {
32468651Skris
32568651Skris	&maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
32668651Skris	if ($do_rewrite == 1) {
32768651Skris		open(OUT, ">$ssl_num");
32868651Skris		&rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
32968651Skris	} else {
33068651Skris		open(OUT, ">>$ssl_num");
33168651Skris	}
33268651Skris	&update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
33355714Skris	close OUT;
33455714Skris}
33555714Skris
33655714Skrisif($do_crypto == 1) {
33768651Skris
33868651Skris	&maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
33968651Skris	if ($do_rewrite == 1) {
34068651Skris		open(OUT, ">$crypto_num");
34168651Skris		&rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
34268651Skris	} else {
34368651Skris		open(OUT, ">>$crypto_num");
34468651Skris	}
34568651Skris	&update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
34655714Skris	close OUT;
34759191Skris}
34855714Skris
349109998Smarkm} elsif ($do_checkexist) {
350109998Smarkm	&check_existing(*ssl_list, @ssl_symbols)
351109998Smarkm		if $do_ssl == 1;
352109998Smarkm	&check_existing(*crypto_list, @crypto_symbols)
353109998Smarkm		if $do_crypto == 1;
35468651Skris} elsif ($do_ctest || $do_ctestall) {
35559191Skris
35659191Skris	print <<"EOF";
35759191Skris
35859191Skris/* Test file to check all DEF file symbols are present by trying
35959191Skris * to link to all of them. This is *not* intended to be run!
36059191Skris */
36159191Skris
36259191Skrisint main()
36359191Skris{
36459191SkrisEOF
36568651Skris	&print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
36659191Skris		if $do_ssl == 1;
36759191Skris
36868651Skris	&print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
36959191Skris		if $do_crypto == 1;
37059191Skris
37159191Skris	print "}\n";
37259191Skris
37355714Skris} else {
37455714Skris
375109998Smarkm	&print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols)
37655714Skris		if $do_ssl == 1;
37755714Skris
378109998Smarkm	&print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols)
37955714Skris		if $do_crypto == 1;
38055714Skris
38155714Skris}
38255714Skris
38355714Skris
38455714Skrissub do_defs
38555714Skris{
38668651Skris	my($name,$files,$symhacksfile)=@_;
38759191Skris	my $file;
38855714Skris	my @ret;
38968651Skris	my %syms;
39068651Skris	my %platform;		# For anything undefined, we assume ""
39168651Skris	my %kind;		# For anything undefined, we assume "FUNCTION"
39268651Skris	my %algorithm;		# For anything undefined, we assume ""
393109998Smarkm	my %variant;
394109998Smarkm	my %variant_cnt;	# To be able to allocate "name{n}" if "name"
395109998Smarkm				# is the same name as the original.
39659191Skris	my $cpp;
397109998Smarkm	my %unknown_algorithms = ();
39855714Skris
39968651Skris	foreach $file (split(/\s+/,$symhacksfile." ".$files))
40055714Skris		{
401109998Smarkm		print STDERR "DEBUG: starting on $file:\n" if $debug;
40255714Skris		open(IN,"<$file") || die "unable to open $file:$!\n";
40359191Skris		my $line = "", my $def= "";
40455714Skris		my %tag = (
40568651Skris			(map { $_ => 0 } @known_platforms),
406109998Smarkm			(map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
407109998Smarkm			(map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
40855714Skris			NOPROTO		=> 0,
40955714Skris			PERL5		=> 0,
41055714Skris			_WINDLL		=> 0,
41155714Skris			CONST_STRICT	=> 0,
41255714Skris			TRUE		=> 1,
41355714Skris		);
41468651Skris		my $symhacking = $file eq $symhacksfile;
415109998Smarkm		my @current_platforms = ();
416109998Smarkm		my @current_algorithms = ();
417109998Smarkm
418109998Smarkm		# params: symbol, alias, platforms, kind
419109998Smarkm		# The reason to put this subroutine in a variable is that
420109998Smarkm		# it will otherwise create it's own, unshared, version of
421109998Smarkm		# %tag and %variant...
422109998Smarkm		my $make_variant = sub
423109998Smarkm		{
424109998Smarkm			my ($s, $a, $p, $k) = @_;
425109998Smarkm			my ($a1, $a2);
426109998Smarkm
427109998Smarkm			print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
428109998Smarkm			if (defined($p))
429109998Smarkm			{
430109998Smarkm				$a1 = join(",",$p,
431109998Smarkm					   grep(!/^$/,
432109998Smarkm						map { $tag{$_} == 1 ? $_ : "" }
433109998Smarkm						@known_platforms));
434109998Smarkm			}
435109998Smarkm			else
436109998Smarkm			{
437109998Smarkm				$a1 = join(",",
438109998Smarkm					   grep(!/^$/,
439109998Smarkm						map { $tag{$_} == 1 ? $_ : "" }
440109998Smarkm						@known_platforms));
441109998Smarkm			}
442109998Smarkm			$a2 = join(",",
443109998Smarkm				   grep(!/^$/,
444109998Smarkm					map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
445109998Smarkm					@known_ossl_platforms));
446109998Smarkm			print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
447109998Smarkm			if ($a1 eq "") { $a1 = $a2; }
448109998Smarkm			elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
449109998Smarkm			if ($a eq $s)
450109998Smarkm			{
451109998Smarkm				if (!defined($variant_cnt{$s}))
452109998Smarkm				{
453109998Smarkm					$variant_cnt{$s} = 0;
454109998Smarkm				}
455109998Smarkm				$variant_cnt{$s}++;
456109998Smarkm				$a .= "{$variant_cnt{$s}}";
457109998Smarkm			}
458109998Smarkm			my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
459109998Smarkm			my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
460109998Smarkm			if (!grep(/^$togrep$/,
461109998Smarkm				  split(/;/, defined($variant{$s})?$variant{$s}:""))) {
462109998Smarkm				if (defined($variant{$s})) { $variant{$s} .= ";"; }
463109998Smarkm				$variant{$s} .= $toadd;
464109998Smarkm			}
465109998Smarkm			print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
466109998Smarkm		};
467109998Smarkm
468109998Smarkm		print STDERR "DEBUG: parsing ----------\n" if $debug;
46955714Skris		while(<IN>) {
470160814Ssimon			if (/\/\* Error codes for the \w+ functions\. \*\//)
471160814Ssimon				{
472160814Ssimon				undef @tag;
473160814Ssimon				last;
474160814Ssimon				}
47555714Skris			if ($line ne '') {
47655714Skris				$_ = $line . $_;
47755714Skris				$line = '';
47855714Skris			}
47955714Skris
48055714Skris			if (/\\$/) {
481109998Smarkm				chomp; # remove eol
482109998Smarkm				chop; # remove ending backslash
48355714Skris				$line = $_;
48455714Skris				next;
48555714Skris			}
48655714Skris
487160814Ssimon			if(/\/\*/) {
488160814Ssimon				if (not /\*\//) {	# multiline comment...
489160814Ssimon					$line = $_;	# ... just accumulate
490160814Ssimon					next;
491160814Ssimon				} else {
492160814Ssimon					s/\/\*.*?\*\///gs;# wipe it
493160814Ssimon				}
494160814Ssimon			}
495160814Ssimon
49655714Skris			if ($cpp) {
497160814Ssimon				$cpp++ if /^#\s*if/;
498160814Ssimon				$cpp-- if /^#\s*endif/;
49955714Skris				next;
50055714Skris	    		}
501160814Ssimon			$cpp = 1 if /^#.*ifdef.*cplusplus/;
50255714Skris
50355714Skris			s/{[^{}]*}//gs;                      # ignore {} blocks
504120631Snectar			print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
505109998Smarkm			print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
506109998Smarkm			if (/^\#\s*ifndef\s+(.*)/) {
507109998Smarkm				push(@tag,"-");
50855714Skris				push(@tag,$1);
50955714Skris				$tag{$1}=-1;
510109998Smarkm				print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
511109998Smarkm			} elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
512109998Smarkm				push(@tag,"-");
513109998Smarkm				if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) {
514109998Smarkm					my $tmp_1 = $1;
515109998Smarkm					my $tmp_;
516109998Smarkm					foreach $tmp_ (split '\&\&',$tmp_1) {
517109998Smarkm						$tmp_ =~ /!defined\(([^\)]+)\)/;
518109998Smarkm						print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
519109998Smarkm						push(@tag,$1);
520109998Smarkm						$tag{$1}=-1;
521109998Smarkm					}
522109998Smarkm				} else {
523109998Smarkm					print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O...
524109998Smarkm					print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
525109998Smarkm					push(@tag,$1);
526109998Smarkm					$tag{$1}=-1;
527109998Smarkm				}
528160814Ssimon			} elsif (/^\#\s*ifdef\s+(\S*)/) {
529109998Smarkm				push(@tag,"-");
53055714Skris				push(@tag,$1);
53155714Skris				$tag{$1}=1;
532109998Smarkm				print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
533109998Smarkm			} elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
534109998Smarkm				push(@tag,"-");
535109998Smarkm				if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) {
536109998Smarkm					my $tmp_1 = $1;
537109998Smarkm					my $tmp_;
538109998Smarkm					foreach $tmp_ (split '\|\|',$tmp_1) {
539109998Smarkm						$tmp_ =~ /defined\(([^\)]+)\)/;
540109998Smarkm						print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
541109998Smarkm						push(@tag,$1);
542109998Smarkm						$tag{$1}=1;
543109998Smarkm					}
544109998Smarkm				} else {
545109998Smarkm					print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O...
546109998Smarkm					print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
547109998Smarkm					push(@tag,$1);
548109998Smarkm					$tag{$1}=1;
549109998Smarkm				}
55068651Skris			} elsif (/^\#\s*error\s+(\w+) is disabled\./) {
551109998Smarkm				my $tag_i = $#tag;
552109998Smarkm				while($tag[$tag_i] ne "-") {
553109998Smarkm					if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
554109998Smarkm						$tag{$tag[$tag_i]}=2;
555109998Smarkm						print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug;
556109998Smarkm					}
557109998Smarkm					$tag_i--;
55868651Skris				}
55955714Skris			} elsif (/^\#\s*endif/) {
560109998Smarkm				my $tag_i = $#tag;
561160814Ssimon				while($tag_i > 0 && $tag[$tag_i] ne "-") {
562109998Smarkm					my $t=$tag[$tag_i];
563109998Smarkm					print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
564109998Smarkm					if ($tag{$t}==2) {
565109998Smarkm						$tag{$t}=-1;
566109998Smarkm					} else {
567109998Smarkm						$tag{$t}=0;
568109998Smarkm					}
569109998Smarkm					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
570109998Smarkm					pop(@tag);
571109998Smarkm					if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
572109998Smarkm						$t=$1;
573109998Smarkm					} else {
574109998Smarkm						$t="";
575109998Smarkm					}
576109998Smarkm					if ($t ne ""
577109998Smarkm					    && !grep(/^$t$/, @known_algorithms)) {
578109998Smarkm						$unknown_algorithms{$t} = 1;
579109998Smarkm						#print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
580109998Smarkm					}
581109998Smarkm					$tag_i--;
58268651Skris				}
58355714Skris				pop(@tag);
58455714Skris			} elsif (/^\#\s*else/) {
585109998Smarkm				my $tag_i = $#tag;
586109998Smarkm				while($tag[$tag_i] ne "-") {
587109998Smarkm					my $t=$tag[$tag_i];
588109998Smarkm					$tag{$t}= -$tag{$t};
589109998Smarkm					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
590109998Smarkm					$tag_i--;
591109998Smarkm				}
59255714Skris			} elsif (/^\#\s*if\s+1/) {
593109998Smarkm				push(@tag,"-");
59455714Skris				# Dummy tag
59555714Skris				push(@tag,"TRUE");
59655714Skris				$tag{"TRUE"}=1;
597109998Smarkm				print STDERR "DEBUG: $file: found 1\n" if $debug;
59859191Skris			} elsif (/^\#\s*if\s+0/) {
599109998Smarkm				push(@tag,"-");
60059191Skris				# Dummy tag
60159191Skris				push(@tag,"TRUE");
60259191Skris				$tag{"TRUE"}=-1;
603109998Smarkm				print STDERR "DEBUG: $file: found 0\n" if $debug;
60468651Skris			} elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
605109998Smarkm				 && $symhacking && $tag{'TRUE'} != -1) {
606109998Smarkm				# This is for aliasing.  When we find an alias,
607109998Smarkm				# we have to invert
608109998Smarkm				&$make_variant($1,$2);
609109998Smarkm				print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
61068651Skris			}
61168651Skris			if (/^\#/) {
612109998Smarkm				@current_platforms =
613109998Smarkm				    grep(!/^$/,
614109998Smarkm					 map { $tag{$_} == 1 ? $_ :
615109998Smarkm						   $tag{$_} == -1 ? "!".$_  : "" }
616109998Smarkm					 @known_platforms);
617109998Smarkm				push @current_platforms
618109998Smarkm				    , grep(!/^$/,
619109998Smarkm					   map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
620109998Smarkm						     $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
621109998Smarkm					   @known_ossl_platforms);
622109998Smarkm				@current_algorithms =
623109998Smarkm				    grep(!/^$/,
624109998Smarkm					 map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
625109998Smarkm					 @known_algorithms);
626109998Smarkm				$def .=
627109998Smarkm				    "#INFO:"
628109998Smarkm					.join(',',@current_platforms).":"
629109998Smarkm					    .join(',',@current_algorithms).";";
63059191Skris				next;
63168651Skris			}
632109998Smarkm			if ($tag{'TRUE'} != -1) {
633109998Smarkm				if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
634109998Smarkm					next;
635109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
636109998Smarkm					$def .= "int d2i_$3(void);";
637109998Smarkm					$def .= "int i2d_$3(void);";
638109998Smarkm					# Variant for platforms that do not
639109998Smarkm					# have to access globale variables
640109998Smarkm					# in shared libraries through functions
641109998Smarkm					$def .=
642109998Smarkm					    "#INFO:"
643109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
644109998Smarkm						    .join(',',@current_algorithms).";";
645109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
646109998Smarkm					$def .=
647109998Smarkm					    "#INFO:"
648109998Smarkm						.join(',',@current_platforms).":"
649109998Smarkm						    .join(',',@current_algorithms).";";
650109998Smarkm					# Variant for platforms that have to
651109998Smarkm					# access globale variables in shared
652109998Smarkm					# libraries through functions
653109998Smarkm					&$make_variant("$2_it","$2_it",
654109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
655109998Smarkm						      "FUNCTION");
656109998Smarkm					next;
657109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
658109998Smarkm					$def .= "int d2i_$3(void);";
659109998Smarkm					$def .= "int i2d_$3(void);";
660109998Smarkm					$def .= "int $3_free(void);";
661109998Smarkm					$def .= "int $3_new(void);";
662109998Smarkm					# Variant for platforms that do not
663109998Smarkm					# have to access globale variables
664109998Smarkm					# in shared libraries through functions
665109998Smarkm					$def .=
666109998Smarkm					    "#INFO:"
667109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
668109998Smarkm						    .join(',',@current_algorithms).";";
669109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
670109998Smarkm					$def .=
671109998Smarkm					    "#INFO:"
672109998Smarkm						.join(',',@current_platforms).":"
673109998Smarkm						    .join(',',@current_algorithms).";";
674109998Smarkm					# Variant for platforms that have to
675109998Smarkm					# access globale variables in shared
676109998Smarkm					# libraries through functions
677109998Smarkm					&$make_variant("$2_it","$2_it",
678109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
679109998Smarkm						      "FUNCTION");
680109998Smarkm					next;
681109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
682109998Smarkm					 /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
683109998Smarkm					$def .= "int d2i_$1(void);";
684109998Smarkm					$def .= "int i2d_$1(void);";
685109998Smarkm					$def .= "int $1_free(void);";
686109998Smarkm					$def .= "int $1_new(void);";
687109998Smarkm					# Variant for platforms that do not
688109998Smarkm					# have to access globale variables
689109998Smarkm					# in shared libraries through functions
690109998Smarkm					$def .=
691109998Smarkm					    "#INFO:"
692109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
693109998Smarkm						    .join(',',@current_algorithms).";";
694109998Smarkm					$def .= "OPENSSL_EXTERN int $1_it;";
695109998Smarkm					$def .=
696109998Smarkm					    "#INFO:"
697109998Smarkm						.join(',',@current_platforms).":"
698109998Smarkm						    .join(',',@current_algorithms).";";
699109998Smarkm					# Variant for platforms that have to
700109998Smarkm					# access globale variables in shared
701109998Smarkm					# libraries through functions
702109998Smarkm					&$make_variant("$1_it","$1_it",
703109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
704109998Smarkm						      "FUNCTION");
705109998Smarkm					next;
706109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
707109998Smarkm					$def .= "int d2i_$2(void);";
708109998Smarkm					$def .= "int i2d_$2(void);";
709109998Smarkm					# Variant for platforms that do not
710109998Smarkm					# have to access globale variables
711109998Smarkm					# in shared libraries through functions
712109998Smarkm					$def .=
713109998Smarkm					    "#INFO:"
714109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
715109998Smarkm						    .join(',',@current_algorithms).";";
716109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
717109998Smarkm					$def .=
718109998Smarkm					    "#INFO:"
719109998Smarkm						.join(',',@current_platforms).":"
720109998Smarkm						    .join(',',@current_algorithms).";";
721109998Smarkm					# Variant for platforms that have to
722109998Smarkm					# access globale variables in shared
723109998Smarkm					# libraries through functions
724109998Smarkm					&$make_variant("$2_it","$2_it",
725109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
726109998Smarkm						      "FUNCTION");
727109998Smarkm					next;
728160814Ssimon				} elsif (/^\s*DECLARE_ASN1_ALLOC_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
729160814Ssimon					$def .= "int $1_free(void);";
730160814Ssimon					$def .= "int $1_new(void);";
731160814Ssimon					next;
732109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
733109998Smarkm					$def .= "int d2i_$2(void);";
734109998Smarkm					$def .= "int i2d_$2(void);";
735109998Smarkm					$def .= "int $2_free(void);";
736109998Smarkm					$def .= "int $2_new(void);";
737109998Smarkm					# Variant for platforms that do not
738109998Smarkm					# have to access globale variables
739109998Smarkm					# in shared libraries through functions
740109998Smarkm					$def .=
741109998Smarkm					    "#INFO:"
742109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
743109998Smarkm						    .join(',',@current_algorithms).";";
744109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
745109998Smarkm					$def .=
746109998Smarkm					    "#INFO:"
747109998Smarkm						.join(',',@current_platforms).":"
748109998Smarkm						    .join(',',@current_algorithms).";";
749109998Smarkm					# Variant for platforms that have to
750109998Smarkm					# access globale variables in shared
751109998Smarkm					# libraries through functions
752109998Smarkm					&$make_variant("$2_it","$2_it",
753109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
754109998Smarkm						      "FUNCTION");
755109998Smarkm					next;
756109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
757109998Smarkm					# Variant for platforms that do not
758109998Smarkm					# have to access globale variables
759109998Smarkm					# in shared libraries through functions
760109998Smarkm					$def .=
761109998Smarkm					    "#INFO:"
762109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
763109998Smarkm						    .join(',',@current_algorithms).";";
764109998Smarkm					$def .= "OPENSSL_EXTERN int $1_it;";
765109998Smarkm					$def .=
766109998Smarkm					    "#INFO:"
767109998Smarkm						.join(',',@current_platforms).":"
768109998Smarkm						    .join(',',@current_algorithms).";";
769109998Smarkm					# Variant for platforms that have to
770109998Smarkm					# access globale variables in shared
771109998Smarkm					# libraries through functions
772109998Smarkm					&$make_variant("$1_it","$1_it",
773109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
774109998Smarkm						      "FUNCTION");
775109998Smarkm					next;
776160814Ssimon				} elsif (/^\s*DECLARE_ASN1_NDEF_FUNCTION\s*\(\s*(\w*)\s*\)/) {
777160814Ssimon					$def .= "int i2d_$1_NDEF(void);";
778109998Smarkm				} elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
779109998Smarkm					next;
780160814Ssimon				} elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION\s*\(\s*(\w*)\s*\)/) {
781160814Ssimon					$def .= "int $1_print_ctx(void);";
782160814Ssimon					next;
783160814Ssimon				} elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
784160814Ssimon					$def .= "int $2_print_ctx(void);";
785160814Ssimon					next;
786109998Smarkm				} elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
787109998Smarkm					next;
788109998Smarkm				} elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
789160814Ssimon					 /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ||
790160814Ssimon					 /^DECLARE_PEM_rw_const\s*\(\s*(\w*)\s*,/ ) {
791109998Smarkm					# Things not in Win16
792109998Smarkm					$def .=
793109998Smarkm					    "#INFO:"
794109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
795109998Smarkm						    .join(',',@current_algorithms).";";
796109998Smarkm					$def .= "int PEM_read_$1(void);";
797109998Smarkm					$def .= "int PEM_write_$1(void);";
798109998Smarkm					$def .=
799109998Smarkm					    "#INFO:"
800109998Smarkm						.join(',',@current_platforms).":"
801109998Smarkm						    .join(',',@current_algorithms).";";
802109998Smarkm					# Things that are everywhere
803109998Smarkm					$def .= "int PEM_read_bio_$1(void);";
804109998Smarkm					$def .= "int PEM_write_bio_$1(void);";
805109998Smarkm					next;
806109998Smarkm				} elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
807109998Smarkm					 /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
808109998Smarkm					# Things not in Win16
809109998Smarkm					$def .=
810109998Smarkm					    "#INFO:"
811109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
812109998Smarkm						    .join(',',@current_algorithms).";";
813109998Smarkm					$def .= "int PEM_write_$1(void);";
814109998Smarkm					$def .=
815109998Smarkm					    "#INFO:"
816109998Smarkm						.join(',',@current_platforms).":"
817109998Smarkm						    .join(',',@current_algorithms).";";
818109998Smarkm					# Things that are everywhere
819109998Smarkm					$def .= "int PEM_write_bio_$1(void);";
820109998Smarkm					next;
821109998Smarkm				} elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
822109998Smarkm					 /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
823109998Smarkm					# Things not in Win16
824109998Smarkm					$def .=
825109998Smarkm					    "#INFO:"
826109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
827109998Smarkm						    .join(',',@current_algorithms).";";
828109998Smarkm					$def .= "int PEM_read_$1(void);";
829109998Smarkm					$def .=
830109998Smarkm					    "#INFO:"
831109998Smarkm						.join(',',@current_platforms).":"
832109998Smarkm						    .join(',',@current_algorithms).";";
833109998Smarkm					# Things that are everywhere
834109998Smarkm					$def .= "int PEM_read_bio_$1(void);";
835109998Smarkm					next;
836109998Smarkm				} elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
837109998Smarkm					# Variant for platforms that do not
838109998Smarkm					# have to access globale variables
839109998Smarkm					# in shared libraries through functions
840109998Smarkm					$def .=
841109998Smarkm					    "#INFO:"
842109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
843109998Smarkm						    .join(',',@current_algorithms).";";
844109998Smarkm					$def .= "OPENSSL_EXTERN int _shadow_$2;";
845109998Smarkm					$def .=
846109998Smarkm					    "#INFO:"
847109998Smarkm						.join(',',@current_platforms).":"
848109998Smarkm						    .join(',',@current_algorithms).";";
849109998Smarkm					# Variant for platforms that have to
850109998Smarkm					# access globale variables in shared
851109998Smarkm					# libraries through functions
852109998Smarkm					&$make_variant("_shadow_$2","_shadow_$2",
853109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
854109998Smarkm						      "FUNCTION");
855109998Smarkm				} elsif ($tag{'CONST_STRICT'} != 1) {
85668651Skris					if (/\{|\/\*|\([^\)]*$/) {
85755714Skris						$line = $_;
85855714Skris					} else {
85955714Skris						$def .= $_;
86055714Skris					}
86155714Skris				}
86255714Skris			}
863109998Smarkm		}
86455714Skris		close(IN);
86555714Skris
866160814Ssimon		my $algs;
86768651Skris		my $plays;
86868651Skris
869109998Smarkm		print STDERR "DEBUG: postprocessing ----------\n" if $debug;
87055714Skris		foreach (split /;/, $def) {
87168651Skris			my $s; my $k = "FUNCTION"; my $p; my $a;
87255714Skris			s/^[\n\s]*//g;
87355714Skris			s/[\n\s]*$//g;
87468651Skris			next if(/\#undef/);
87555714Skris			next if(/typedef\W/);
87668651Skris			next if(/\#define/);
87768651Skris
878160814Ssimon			# Reduce argument lists to empty ()
879160814Ssimon			# fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
880160814Ssimon			while(/\(.*\)/s) {
881160814Ssimon				s/\([^\(\)]+\)/\{\}/gs;
882160814Ssimon				s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;	#(*f{}) -> f
883160814Ssimon			}
884160814Ssimon			# pretend as we didn't use curly braces: {} -> ()
885160814Ssimon			s/\{\}/\(\)/gs;
886160814Ssimon
887160814Ssimon			s/STACK_OF\(\)/void/gs;
888160814Ssimon
889109998Smarkm			print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug;
89068651Skris			if (/^\#INFO:([^:]*):(.*)$/) {
89168651Skris				$plats = $1;
89268651Skris				$algs = $2;
893109998Smarkm				print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug;
89468651Skris				next;
895109998Smarkm			} elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) {
89668651Skris				$s = $1;
89768651Skris				$k = "VARIABLE";
898109998Smarkm				print STDERR "DEBUG: found external variable $s\n" if $debug;
899160814Ssimon			} elsif (/TYPEDEF_\w+_OF/s) {
90055714Skris				next;
901160814Ssimon			} elsif (/(\w+)\s*\(\).*/s) {	# first token prior [first] () is
902160814Ssimon				$s = $1;		# a function name!
903109998Smarkm				print STDERR "DEBUG: found function $s\n" if $debug;
90455714Skris			} elsif (/\(/ and not (/=/)) {
90555714Skris				print STDERR "File $file: cannot parse: $_;\n";
90668651Skris				next;
90768651Skris			} else {
90868651Skris				next;
90955714Skris			}
91068651Skris
91168651Skris			$syms{$s} = 1;
91268651Skris			$kind{$s} = $k;
91368651Skris
91468651Skris			$p = $plats;
91568651Skris			$a = $algs;
91668651Skris			$a .= ",BF" if($s =~ /EVP_bf/);
91768651Skris			$a .= ",CAST" if($s =~ /EVP_cast/);
91868651Skris			$a .= ",DES" if($s =~ /EVP_des/);
91968651Skris			$a .= ",DSA" if($s =~ /EVP_dss/);
92068651Skris			$a .= ",IDEA" if($s =~ /EVP_idea/);
92168651Skris			$a .= ",MD2" if($s =~ /EVP_md2/);
92268651Skris			$a .= ",MD4" if($s =~ /EVP_md4/);
92368651Skris			$a .= ",MD5" if($s =~ /EVP_md5/);
92468651Skris			$a .= ",RC2" if($s =~ /EVP_rc2/);
92568651Skris			$a .= ",RC4" if($s =~ /EVP_rc4/);
92668651Skris			$a .= ",RC5" if($s =~ /EVP_rc5/);
92768651Skris			$a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
92868651Skris			$a .= ",SHA" if($s =~ /EVP_sha/);
92968651Skris			$a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
93068651Skris			$a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
93168651Skris			$a .= ",RSA" if($s =~ /RSAPrivateKey/);
93268651Skris			$a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
93368651Skris
934109998Smarkm			$platform{$s} =
935109998Smarkm			    &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
93668651Skris			$algorithm{$s} .= ','.$a;
93768651Skris
938109998Smarkm			if (defined($variant{$s})) {
939109998Smarkm				foreach $v (split /;/,$variant{$s}) {
940109998Smarkm					(my $r, my $p, my $k) = split(/:/,$v);
941109998Smarkm					my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
942109998Smarkm					$syms{$r} = 1;
943109998Smarkm					if (!defined($k)) { $k = $kind{$s}; }
944109998Smarkm					$kind{$r} = $k."(".$s.")";
945109998Smarkm					$algorithm{$r} = $algorithm{$s};
946109998Smarkm					$platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
947109998Smarkm					$platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
948109998Smarkm					print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
949109998Smarkm				}
95068651Skris			}
951109998Smarkm			print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
95255714Skris		}
95355714Skris	}
95455714Skris
95568651Skris	# Prune the returned symbols
95655714Skris
95768651Skris        delete $syms{"bn_dump1"};
95868651Skris	$platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
95968651Skris
960109998Smarkm	$platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
961109998Smarkm	$platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
962109998Smarkm	$platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
963109998Smarkm	$platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
96468651Skris
965205128Ssimon	$platform{"EVP_sha384"} = "!VMSVAX";
966205128Ssimon	$platform{"EVP_sha512"} = "!VMSVAX";
967205128Ssimon	$platform{"SHA384_Init"} = "!VMSVAX";
968205128Ssimon	$platform{"SHA384_Transform"} = "!VMSVAX";
969205128Ssimon	$platform{"SHA384_Update"} = "!VMSVAX";
970205128Ssimon	$platform{"SHA384_Final"} = "!VMSVAX";
971205128Ssimon	$platform{"SHA384"} = "!VMSVAX";
972205128Ssimon	$platform{"SHA512_Init"} = "!VMSVAX";
973205128Ssimon	$platform{"SHA512_Transform"} = "!VMSVAX";
974205128Ssimon	$platform{"SHA512_Update"} = "!VMSVAX";
975205128Ssimon	$platform{"SHA512_Final"} = "!VMSVAX";
976205128Ssimon	$platform{"SHA512"} = "!VMSVAX";
977205128Ssimon
97868651Skris	# Info we know about
97968651Skris
98068651Skris	push @ret, map { $_."\\".&info_string($_,"EXIST",
98168651Skris					      $platform{$_},
98268651Skris					      $kind{$_},
98368651Skris					      $algorithm{$_}) } keys %syms;
98468651Skris
985109998Smarkm	if (keys %unknown_algorithms) {
986109998Smarkm		print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
987109998Smarkm		print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
988109998Smarkm	}
98968651Skris	return(@ret);
99068651Skris}
99168651Skris
992109998Smarkm# Param: string of comma-separated platform-specs.
993109998Smarkmsub reduce_platforms
994109998Smarkm{
995109998Smarkm	my ($platforms) = @_;
99668651Skris	my $pl = defined($platforms) ? $platforms : "";
99768651Skris	my %p = map { $_ => 0 } split /,/, $pl;
99868651Skris	my $ret;
99968651Skris
1000109998Smarkm	print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
1001109998Smarkm	    if $debug;
100268651Skris	# We do this, because if there's code like the following, it really
100368651Skris	# means the function exists in all cases and should therefore be
100468651Skris	# everywhere.  By increasing and decreasing, we may attain 0:
100568651Skris	#
100668651Skris	# ifndef WIN16
100768651Skris	#    int foo();
100868651Skris	# else
100968651Skris	#    int _fat foo();
101068651Skris	# endif
101168651Skris	foreach $platform (split /,/, $pl) {
101268651Skris		if ($platform =~ /^!(.*)$/) {
101368651Skris			$p{$1}--;
101468651Skris		} else {
101568651Skris			$p{$platform}++;
101655714Skris		}
101755714Skris	}
101868651Skris	foreach $platform (keys %p) {
101968651Skris		if ($p{$platform} == 0) { delete $p{$platform}; }
102055714Skris	}
102155714Skris
102268651Skris	delete $p{""};
1023109998Smarkm
1024109998Smarkm	$ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
1025109998Smarkm	print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
1026109998Smarkm	    if $debug;
1027109998Smarkm	return $ret;
1028109998Smarkm}
1029109998Smarkm
1030109998Smarkmsub info_string {
1031109998Smarkm	(my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
1032109998Smarkm
1033109998Smarkm	my %a = defined($algorithms) ?
1034109998Smarkm	    map { $_ => 1 } split /,/, $algorithms : ();
1035109998Smarkm	my $k = defined($kind) ? $kind : "FUNCTION";
1036109998Smarkm	my $ret;
1037109998Smarkm	my $p = &reduce_platforms($platforms);
1038109998Smarkm
103968651Skris	delete $a{""};
104055714Skris
104168651Skris	$ret = $exist;
1042109998Smarkm	$ret .= ":".$p;
104368651Skris	$ret .= ":".$k;
1044109998Smarkm	$ret .= ":".join(',',sort keys %a);
104568651Skris	return $ret;
104655714Skris}
104755714Skris
104868651Skrissub maybe_add_info {
104968651Skris	(my $name, *nums, my @symbols) = @_;
105068651Skris	my $sym;
105168651Skris	my $new_info = 0;
1052109998Smarkm	my %syms=();
105368651Skris
105468651Skris	print STDERR "Updating $name info\n";
105568651Skris	foreach $sym (@symbols) {
105668651Skris		(my $s, my $i) = split /\\/, $sym;
105768651Skris		if (defined($nums{$s})) {
1058109998Smarkm			$i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
105968651Skris			(my $n, my $dummy) = split /\\/, $nums{$s};
106068651Skris			if (!defined($dummy) || $i ne $dummy) {
106168651Skris				$nums{$s} = $n."\\".$i;
106268651Skris				$new_info++;
1063109998Smarkm				print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
106468651Skris			}
106568651Skris		}
1066109998Smarkm		$syms{$s} = 1;
106768651Skris	}
1068109998Smarkm
1069109998Smarkm	my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
1070109998Smarkm	foreach $sym (@s) {
1071109998Smarkm		(my $n, my $i) = split /\\/, $nums{$sym};
1072109998Smarkm		if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
1073109998Smarkm			$new_info++;
1074109998Smarkm			print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
1075109998Smarkm		}
1076109998Smarkm	}
107768651Skris	if ($new_info) {
107868651Skris		print STDERR "$new_info old symbols got an info update\n";
107968651Skris		if (!$do_rewrite) {
108068651Skris			print STDERR "You should do a rewrite to fix this.\n";
108168651Skris		}
108268651Skris	} else {
108368651Skris		print STDERR "No old symbols needed info update\n";
108468651Skris	}
108568651Skris}
108668651Skris
1087109998Smarkm# Param: string of comma-separated keywords, each possibly prefixed with a "!"
1088109998Smarkmsub is_valid
1089109998Smarkm{
1090109998Smarkm	my ($keywords_txt,$platforms) = @_;
1091109998Smarkm	my (@keywords) = split /,/,$keywords_txt;
1092160814Ssimon	my ($falsesum, $truesum) = (0, 1);
1093109998Smarkm
1094109998Smarkm	# Param: one keyword
1095109998Smarkm	sub recognise
1096109998Smarkm	{
1097109998Smarkm		my ($keyword,$platforms) = @_;
1098109998Smarkm
1099109998Smarkm		if ($platforms) {
1100109998Smarkm			# platforms
1101109998Smarkm			if ($keyword eq "VMS" && $VMS) { return 1; }
1102205128Ssimon			if ($keyword eq "VMSVAX" && $VMSVAX) { return 1; }
1103205128Ssimon			if ($keyword eq "VMSNonVAX" && $VMSNonVAX) { return 1; }
1104109998Smarkm			if ($keyword eq "WIN32" && $W32) { return 1; }
1105109998Smarkm			if ($keyword eq "WIN16" && $W16) { return 1; }
1106109998Smarkm			if ($keyword eq "WINNT" && $NT) { return 1; }
1107109998Smarkm			if ($keyword eq "OS2" && $OS2) { return 1; }
1108109998Smarkm			# Special platforms:
1109109998Smarkm			# EXPORT_VAR_AS_FUNCTION means that global variables
1110109998Smarkm			# will be represented as functions.  This currently
1111109998Smarkm			# only happens on VMS-VAX.
1112109998Smarkm			if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && ($VMSVAX || $W32 || $W16)) {
1113109998Smarkm				return 1;
1114109998Smarkm			}
1115194206Ssimon			if ($keyword eq "OPENSSL_FIPS" && $fips) {
1116194206Ssimon				return 1;
1117194206Ssimon			}
1118194206Ssimon			if ($keyword eq "ZLIB" && $zlib) { return 1; }
1119109998Smarkm			return 0;
1120109998Smarkm		} else {
1121109998Smarkm			# algorithms
1122109998Smarkm			if ($keyword eq "RC2" && $no_rc2) { return 0; }
1123109998Smarkm			if ($keyword eq "RC4" && $no_rc4) { return 0; }
1124109998Smarkm			if ($keyword eq "RC5" && $no_rc5) { return 0; }
1125109998Smarkm			if ($keyword eq "IDEA" && $no_idea) { return 0; }
1126109998Smarkm			if ($keyword eq "DES" && $no_des) { return 0; }
1127109998Smarkm			if ($keyword eq "BF" && $no_bf) { return 0; }
1128109998Smarkm			if ($keyword eq "CAST" && $no_cast) { return 0; }
1129109998Smarkm			if ($keyword eq "MD2" && $no_md2) { return 0; }
1130109998Smarkm			if ($keyword eq "MD4" && $no_md4) { return 0; }
1131109998Smarkm			if ($keyword eq "MD5" && $no_md5) { return 0; }
1132109998Smarkm			if ($keyword eq "SHA" && $no_sha) { return 0; }
1133109998Smarkm			if ($keyword eq "RIPEMD" && $no_ripemd) { return 0; }
1134109998Smarkm			if ($keyword eq "MDC2" && $no_mdc2) { return 0; }
1135109998Smarkm			if ($keyword eq "RSA" && $no_rsa) { return 0; }
1136109998Smarkm			if ($keyword eq "DSA" && $no_dsa) { return 0; }
1137109998Smarkm			if ($keyword eq "DH" && $no_dh) { return 0; }
1138109998Smarkm			if ($keyword eq "EC" && $no_ec) { return 0; }
1139160814Ssimon			if ($keyword eq "ECDSA" && $no_ecdsa) { return 0; }
1140160814Ssimon			if ($keyword eq "ECDH" && $no_ecdh) { return 0; }
1141109998Smarkm			if ($keyword eq "HMAC" && $no_hmac) { return 0; }
1142109998Smarkm			if ($keyword eq "AES" && $no_aes) { return 0; }
1143162911Ssimon			if ($keyword eq "CAMELLIA" && $no_camellia) { return 0; }
1144194206Ssimon			if ($keyword eq "SEED" && $no_seed) { return 0; }
1145109998Smarkm			if ($keyword eq "EVP" && $no_evp) { return 0; }
1146109998Smarkm			if ($keyword eq "LHASH" && $no_lhash) { return 0; }
1147109998Smarkm			if ($keyword eq "STACK" && $no_stack) { return 0; }
1148109998Smarkm			if ($keyword eq "ERR" && $no_err) { return 0; }
1149109998Smarkm			if ($keyword eq "BUFFER" && $no_buffer) { return 0; }
1150109998Smarkm			if ($keyword eq "BIO" && $no_bio) { return 0; }
1151109998Smarkm			if ($keyword eq "COMP" && $no_comp) { return 0; }
1152109998Smarkm			if ($keyword eq "DSO" && $no_dso) { return 0; }
1153109998Smarkm			if ($keyword eq "KRB5" && $no_krb5) { return 0; }
1154111147Snectar			if ($keyword eq "ENGINE" && $no_engine) { return 0; }
1155111147Snectar			if ($keyword eq "HW" && $no_hw) { return 0; }
1156109998Smarkm			if ($keyword eq "FP_API" && $no_fp_api) { return 0; }
1157160814Ssimon			if ($keyword eq "STATIC_ENGINE" && $no_static_engine) { return 0; }
1158160814Ssimon			if ($keyword eq "GMP" && $no_gmp) { return 0; }
1159167612Ssimon			if ($keyword eq "RFC3779" && $no_rfc3779) { return 0; }
1160194206Ssimon			if ($keyword eq "TLSEXT" && $no_tlsext) { return 0; }
1161194206Ssimon			if ($keyword eq "CMS" && $no_cms) { return 0; }
1162194206Ssimon			if ($keyword eq "CAPIENG" && $no_capieng) { return 0; }
1163194206Ssimon			if ($keyword eq "JPAKE" && $no_jpake) { return 0; }
1164160814Ssimon			if ($keyword eq "DEPRECATED" && $no_deprecated) { return 0; }
1165109998Smarkm
1166109998Smarkm			# Nothing recognise as true
1167109998Smarkm			return 1;
1168109998Smarkm		}
1169109998Smarkm	}
1170109998Smarkm
1171109998Smarkm	foreach $k (@keywords) {
1172109998Smarkm		if ($k =~ /^!(.*)$/) {
1173109998Smarkm			$falsesum += &recognise($1,$platforms);
1174109998Smarkm		} else {
1175160814Ssimon			$truesum *= &recognise($k,$platforms);
1176109998Smarkm		}
1177109998Smarkm	}
1178109998Smarkm	print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1179109998Smarkm	return (!$falsesum) && $truesum;
1180109998Smarkm}
1181109998Smarkm
118259191Skrissub print_test_file
118359191Skris{
1184109998Smarkm	(*OUT,my $name,*nums,my $testall,my @symbols)=@_;
118559191Skris	my $n = 1; my @e; my @r;
118668651Skris	my $sym; my $prev = ""; my $prefSSLeay;
118759191Skris
1188109998Smarkm	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1189109998Smarkm	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
119068651Skris	@symbols=((sort @e),(sort @r));
119159191Skris
119268651Skris	foreach $sym (@symbols) {
119368651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1194109998Smarkm		my $v = 0;
1195109998Smarkm		$v = 1 if $i=~ /^.*?:.*?:VARIABLE/;
1196109998Smarkm		my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1197109998Smarkm		my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1198109998Smarkm		if (!defined($nums{$s})) {
1199109998Smarkm			print STDERR "Warning: $s does not have a number assigned\n"
1200109998Smarkm			    if(!$do_update);
1201109998Smarkm		} elsif (is_valid($p,1) && is_valid($a,0)) {
1202109998Smarkm			my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1203109998Smarkm			if ($prev eq $s2) {
1204109998Smarkm				print OUT "\t/* The following has already appeared previously */\n";
1205109998Smarkm				print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1206109998Smarkm			}
1207109998Smarkm			$prev = $s2;	# To warn about duplicates...
1208109998Smarkm
1209109998Smarkm			($nn,$ni)=($nums{$s2} =~ /^(.*?)\\(.*)$/);
1210109998Smarkm			if ($v) {
1211109998Smarkm				print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n";
121268651Skris			} else {
1213109998Smarkm				print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n";
121468651Skris			}
121559191Skris		}
121659191Skris	}
121759191Skris}
121859191Skris
1219127128Snectarsub get_version {
1220127128Snectar   local *MF;
1221127128Snectar   my $v = '?';
1222142425Snectar   open MF, 'Makefile' or return $v;
1223127128Snectar   while (<MF>) {
1224127128Snectar     $v = $1, last if /^VERSION=(.*?)\s*$/;
1225127128Snectar   }
1226127128Snectar   close MF;
1227127128Snectar   return $v;
1228127128Snectar}
1229127128Snectar
123055714Skrissub print_def_file
123155714Skris{
123268651Skris	(*OUT,my $name,*nums,my @symbols)=@_;
1233109998Smarkm	my $n = 1; my @e; my @r; my @v; my $prev="";
1234109998Smarkm	my $liboptions="";
1235127128Snectar	my $libname = $name;
1236127128Snectar	my $http_vendor = 'www.openssl.org/';
1237127128Snectar	my $version = get_version();
1238127128Snectar	my $what = "OpenSSL: implementation of Secure Socket Layer";
1239127128Snectar	my $description = "$what $version, $name - http://$http_vendor";
124055714Skris
124155714Skris	if ($W32)
1242127128Snectar		{ $libname.="32"; }
1243109998Smarkm	elsif ($W16)
1244127128Snectar		{ $libname.="16"; }
1245109998Smarkm	elsif ($OS2)
1246127128Snectar		{ # DLL names should not clash on the whole system.
1247127128Snectar		  # However, they should not have any particular relationship
1248127128Snectar		  # to the name of the static library.  Chose descriptive names
1249127128Snectar		  # (must be at most 8 chars).
1250127128Snectar		  my %translate = (ssl => 'open_ssl', crypto => 'cryptssl');
1251127128Snectar		  $libname = $translate{$name} || $name;
1252127128Snectar		  $liboptions = <<EOO;
1253127128SnectarINITINSTANCE
1254127128SnectarDATA MULTIPLE NONSHARED
1255127128SnectarEOO
1256127128Snectar		  # Vendor field can't contain colon, drat; so we omit http://
1257127128Snectar		  $description = "\@#$http_vendor:$version#\@$what; DLL for library $name.  Build for EMX -Zmtd";
1258127128Snectar		}
125955714Skris
126055714Skris	print OUT <<"EOF";
126155714Skris;
126255714Skris; Definition file for the DLL version of the $name library from OpenSSL
126355714Skris;
126455714Skris
1265127128SnectarLIBRARY         $libname	$liboptions
126655714Skris
126755714SkrisEOF
126855714Skris
1269109998Smarkm	if ($W16) {
127055714Skris		print <<"EOF";
127155714SkrisCODE            PRELOAD MOVEABLE
127255714SkrisDATA            PRELOAD MOVEABLE SINGLE
127355714Skris
127455714SkrisEXETYPE		WINDOWS
127555714Skris
127655714SkrisHEAPSIZE	4096
127755714SkrisSTACKSIZE	8192
127855714Skris
127955714SkrisEOF
128055714Skris	}
128155714Skris
128255714Skris	print "EXPORTS\n";
128355714Skris
1284109998Smarkm	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1285109998Smarkm	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1286109998Smarkm	(@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols);
1287109998Smarkm	@symbols=((sort @e),(sort @r), (sort @v));
128855714Skris
128955714Skris
129068651Skris	foreach $sym (@symbols) {
129168651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1292109998Smarkm		my $v = 0;
1293109998Smarkm		$v = 1 if $i =~ /^.*?:.*?:VARIABLE/;
129468651Skris		if (!defined($nums{$s})) {
129568651Skris			printf STDERR "Warning: $s does not have a number assigned\n"
1296109998Smarkm			    if(!$do_update);
129755714Skris		} else {
1298109998Smarkm			(my $n, my $dummy) = split /\\/, $nums{$s};
129968651Skris			my %pf = ();
1300109998Smarkm			my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1301109998Smarkm			my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1302109998Smarkm			if (is_valid($p,1) && is_valid($a,0)) {
1303109998Smarkm				my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1304109998Smarkm				if ($prev eq $s2) {
1305109998Smarkm					print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1306109998Smarkm				}
1307109998Smarkm				$prev = $s2;	# To warn about duplicates...
1308109998Smarkm				if($v && !$OS2) {
1309109998Smarkm					printf OUT "    %s%-39s @%-8d DATA\n",($W32)?"":"_",$s2,$n;
1310109998Smarkm				} else {
1311109998Smarkm					printf OUT "    %s%-39s @%d\n",($W32||$OS2)?"":"_",$s2,$n;
1312109998Smarkm				}
131368651Skris			}
131455714Skris		}
131555714Skris	}
131655714Skris	printf OUT "\n";
131755714Skris}
131855714Skris
131955714Skrissub load_numbers
132055714Skris{
132155714Skris	my($name)=@_;
132255714Skris	my(@a,%ret);
132355714Skris
132455714Skris	$max_num = 0;
132568651Skris	$num_noinfo = 0;
132668651Skris	$prev = "";
1327109998Smarkm	$prev_cnt = 0;
132855714Skris
132955714Skris	open(IN,"<$name") || die "unable to open $name:$!\n";
133055714Skris	while (<IN>) {
133155714Skris		chop;
133255714Skris		s/#.*$//;
133355714Skris		next if /^\s*$/;
133455714Skris		@a=split;
133568651Skris		if (defined $ret{$a[0]}) {
1336109998Smarkm			# This is actually perfectly OK
1337109998Smarkm			#print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
133868651Skris		}
133968651Skris		if ($max_num > $a[1]) {
134068651Skris			print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
134168651Skris		}
1342109998Smarkm		elsif ($max_num == $a[1]) {
134368651Skris			# This is actually perfectly OK
134468651Skris			#print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1345109998Smarkm			if ($a[0] eq $prev) {
1346109998Smarkm				$prev_cnt++;
1347109998Smarkm				$a[0] .= "{$prev_cnt}";
1348109998Smarkm			}
134968651Skris		}
1350109998Smarkm		else {
1351109998Smarkm			$prev_cnt = 0;
1352109998Smarkm		}
135368651Skris		if ($#a < 2) {
135468651Skris			# Existence will be proven later, in do_defs
135568651Skris			$ret{$a[0]}=$a[1];
135668651Skris			$num_noinfo++;
135768651Skris		} else {
135868651Skris			$ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
135968651Skris		}
136055714Skris		$max_num = $a[1] if $a[1] > $max_num;
136168651Skris		$prev=$a[0];
136255714Skris	}
136368651Skris	if ($num_noinfo) {
136468651Skris		print STDERR "Warning: $num_noinfo symbols were without info.";
136568651Skris		if ($do_rewrite) {
136668651Skris			printf STDERR "  The rewrite will fix this.\n";
136768651Skris		} else {
136868651Skris			printf STDERR "  You should do a rewrite to fix this.\n";
136968651Skris		}
137068651Skris	}
137155714Skris	close(IN);
137255714Skris	return(%ret);
137355714Skris}
137455714Skris
137568651Skrissub parse_number
137668651Skris{
137768651Skris	(my $str, my $what) = @_;
137868651Skris	(my $n, my $i) = split(/\\/,$str);
137968651Skris	if ($what eq "n") {
138068651Skris		return $n;
138168651Skris	} else {
138268651Skris		return $i;
138368651Skris	}
138468651Skris}
138568651Skris
138668651Skrissub rewrite_numbers
138768651Skris{
138868651Skris	(*OUT,$name,*nums,@symbols)=@_;
138968651Skris	my $thing;
139068651Skris
139168651Skris	print STDERR "Rewriting $name\n";
139268651Skris
1393109998Smarkm	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
139468651Skris	my $r; my %r; my %rsyms;
139568651Skris	foreach $r (@r) {
139668651Skris		(my $s, my $i) = split /\\/, $r;
139768651Skris		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
139868651Skris		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
139968651Skris		$r{$a} = $s."\\".$i;
140068651Skris		$rsyms{$s} = 1;
140168651Skris	}
140268651Skris
1403109998Smarkm	my %syms = ();
1404109998Smarkm	foreach $_ (@symbols) {
1405109998Smarkm		(my $n, my $i) = split /\\/;
1406109998Smarkm		$syms{$n} = 1;
1407109998Smarkm	}
1408109998Smarkm
1409109998Smarkm	my @s=sort {
1410109998Smarkm	    &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1411109998Smarkm	    || $a cmp $b
1412109998Smarkm	} keys %nums;
141368651Skris	foreach $sym (@s) {
141468651Skris		(my $n, my $i) = split /\\/, $nums{$sym};
141568651Skris		next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
141668651Skris		next if defined($rsyms{$sym});
1417109998Smarkm		print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1418109998Smarkm		$i="NOEXIST::FUNCTION:"
1419109998Smarkm			if !defined($i) || $i eq "" || !defined($syms{$sym});
1420109998Smarkm		my $s2 = $sym;
1421109998Smarkm		$s2 =~ s/\{[0-9]+\}$//;
1422109998Smarkm		printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
142368651Skris		if (exists $r{$sym}) {
142468651Skris			(my $s, $i) = split /\\/,$r{$sym};
1425109998Smarkm			my $s2 = $s;
1426109998Smarkm			$s2 =~ s/\{[0-9]+\}$//;
1427109998Smarkm			printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
142868651Skris		}
142968651Skris	}
143068651Skris}
143168651Skris
143255714Skrissub update_numbers
143355714Skris{
143468651Skris	(*OUT,$name,*nums,my $start_num, my @symbols)=@_;
143568651Skris	my $new_syms = 0;
143668651Skris
143768651Skris	print STDERR "Updating $name numbers\n";
143868651Skris
1439109998Smarkm	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
144068651Skris	my $r; my %r; my %rsyms;
144168651Skris	foreach $r (@r) {
144268651Skris		(my $s, my $i) = split /\\/, $r;
144368651Skris		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
144468651Skris		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
144568651Skris		$r{$a} = $s."\\".$i;
144668651Skris		$rsyms{$s} = 1;
144768651Skris	}
144868651Skris
144968651Skris	foreach $sym (@symbols) {
145068651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
145168651Skris		next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
145268651Skris		next if defined($rsyms{$sym});
145368651Skris		die "ERROR: Symbol $sym had no info attached to it."
145468651Skris		    if $i eq "";
145568651Skris		if (!exists $nums{$s}) {
145668651Skris			$new_syms++;
1457109998Smarkm			my $s2 = $s;
1458109998Smarkm			$s2 =~ s/\{[0-9]+\}$//;
1459109998Smarkm			printf OUT "%s%-39s %d\t%s\n","",$s2, ++$start_num,$i;
146068651Skris			if (exists $r{$s}) {
146168651Skris				($s, $i) = split /\\/,$r{$s};
1462109998Smarkm				$s =~ s/\{[0-9]+\}$//;
1463109998Smarkm				printf OUT "%s%-39s %d\t%s\n","",$s, $start_num,$i;
146468651Skris			}
146555714Skris		}
146655714Skris	}
146768651Skris	if($new_syms) {
146868651Skris		print STDERR "$new_syms New symbols added\n";
146955714Skris	} else {
147068651Skris		print STDERR "No New symbols Added\n";
147155714Skris	}
147255714Skris}
147368651Skris
147468651Skrissub check_existing
147568651Skris{
147668651Skris	(*nums, my @symbols)=@_;
147768651Skris	my %existing; my @remaining;
147868651Skris	@remaining=();
147968651Skris	foreach $sym (@symbols) {
148068651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
148168651Skris		$existing{$s}=1;
148268651Skris	}
148368651Skris	foreach $sym (keys %nums) {
148468651Skris		if (!exists $existing{$sym}) {
148568651Skris			push @remaining, $sym;
148668651Skris		}
148768651Skris	}
148868651Skris	if(@remaining) {
148968651Skris		print STDERR "The following symbols do not seem to exist:\n";
149068651Skris		foreach $sym (@remaining) {
149168651Skris			print STDERR "\t",$sym,"\n";
149268651Skris		}
149368651Skris	}
149468651Skris}
149568651Skris
1496