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
81238405Sjkimmy @known_platforms = ( "__FreeBSD__", "PERL5", "NeXT",
82238405Sjkim			"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",
87238405Sjkim			 "MDC2", "WHIRLPOOL", "RSA", "DSA", "DH", "EC", "ECDH", "ECDSA", "EC2M",
88238405Sjkim			 "HMAC", "AES", "CAMELLIA", "SEED", "GOST",
89238405Sjkim			 # EC_NISTP_64_GCC_128
90238405Sjkim			 "EC_NISTP_64_GCC_128",
91109998Smarkm			 # Envelope "algorithms"
92109998Smarkm			 "EVP", "X509", "ASN1_TYPEDEFS",
93109998Smarkm			 # Helper "algorithms"
94109998Smarkm			 "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
95109998Smarkm			 "LOCKING",
96109998Smarkm			 # External "algorithms"
97160814Ssimon			 "FP_API", "STDIO", "SOCK", "KRB5", "DGRAM",
98160814Ssimon			 # Engines
99160814Ssimon			 "STATIC_ENGINE", "ENGINE", "HW", "GMP",
100238405Sjkim			 # RFC3779
101167612Ssimon			 "RFC3779",
102238405Sjkim			 # TLS
103238405Sjkim			 "TLSEXT", "PSK", "SRP", "HEARTBEATS",
104194206Ssimon			 # CMS
105194206Ssimon			 "CMS",
106194206Ssimon			 # CryptoAPI Engine
107194206Ssimon			 "CAPIENG",
108238405Sjkim			 # SSL v2
109238405Sjkim			 "SSL2",
110298998Sjkim			 # SSL v2 method
111298998Sjkim			 "SSL2_METHOD",
112276861Sjkim			 # SSL v3 method
113276861Sjkim			 "SSL3_METHOD",
114194206Ssimon			 # JPAKE
115194206Ssimon			 "JPAKE",
116238405Sjkim			 # NEXTPROTONEG
117238405Sjkim			 "NEXTPROTONEG",
118160814Ssimon			 # Deprecated functions
119238405Sjkim			 "DEPRECATED",
120238405Sjkim			 # Hide SSL internals
121238405Sjkim			 "SSL_INTERN",
122238405Sjkim			 # SCTP
123269682Sjkim		 	 "SCTP",
124273144Sjkim			 # SRTP
125273144Sjkim			 "SRTP",
126290207Sjkim			 # SSL TRACE
127290207Sjkim		 	 "SSL_TRACE",
128269682Sjkim			 # Unit testing
129290207Sjkim			 "UNIT_TEST");
13068651Skris
13159191Skrismy $options="";
132142425Snectaropen(IN,"<Makefile") || die "unable to open Makefile!\n";
13355714Skriswhile(<IN>) {
13455714Skris    $options=$1 if (/^OPTIONS=(.*)$/);
13555714Skris}
13655714Skrisclose(IN);
13755714Skris
13859191Skris# The following ciphers may be excluded (by Configure). This means functions
13959191Skris# defined with ifndef(NO_XXX) are not included in the .def file, and everything
14059191Skris# in directory xxx is ignored.
14159191Skrismy $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
142238405Sjkimmy $no_cast; my $no_whirlpool; my $no_camellia; my $no_seed;
14368651Skrismy $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
144109998Smarkmmy $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_aes; my $no_krb5;
145238405Sjkimmy $no_ec; my $no_ecdsa; my $no_ecdh; my $no_engine; my $no_hw;
146238405Sjkimmy $no_fp_api; my $no_static_engine=1; my $no_gmp; my $no_deprecated;
147238405Sjkimmy $no_rfc3779; my $no_psk; my $no_tlsext; my $no_cms; my $no_capieng;
148238405Sjkimmy $no_jpake; my $no_srp; my $no_ssl2; my $no_ec2m; my $no_nistp_gcc;
149290207Sjkimmy $no_nextprotoneg; my $no_sctp; my $no_srtp; my $no_ssl_trace;
150298998Sjkimmy $no_unit_test; my $no_ssl3_method; my $no_ssl2_method;
151238405Sjkim
152194206Ssimonmy $fips;
15359191Skris
154238405Sjkimmy $zlib;
155160814Ssimon
156238405Sjkim
15755714Skrisforeach (@ARGV, split(/ /, $options))
15855714Skris	{
159109998Smarkm	$debug=1 if $_ eq "debug";
16055714Skris	$W32=1 if $_ eq "32";
16168651Skris	$W16=1 if $_ eq "16";
16255714Skris	if($_ eq "NT") {
16355714Skris		$W32 = 1;
16455714Skris		$NT = 1;
16555714Skris	}
166109998Smarkm	if ($_ eq "VMS-VAX") {
167109998Smarkm		$VMS=1;
168109998Smarkm		$VMSVAX=1;
169109998Smarkm	}
170205128Ssimon	if ($_ eq "VMS-NonVAX") {
171109998Smarkm		$VMS=1;
172205128Ssimon		$VMSNonVAX=1;
173109998Smarkm	}
17468651Skris	$VMS=1 if $_ eq "VMS";
175109998Smarkm	$OS2=1 if $_ eq "OS2";
176194206Ssimon	$fips=1 if /^fips/;
177238405Sjkim	if ($_ eq "zlib" || $_ eq "enable-zlib" || $_ eq "zlib-dynamic"
178238405Sjkim			 || $_ eq "enable-zlib-dynamic") {
179238405Sjkim		$zlib = 1;
180194206Ssimon	}
181194206Ssimon
18255714Skris	$do_ssl=1 if $_ eq "ssleay";
183109998Smarkm	if ($_ eq "ssl") {
184109998Smarkm		$do_ssl=1;
185109998Smarkm		$libname=$_
186109998Smarkm	}
18755714Skris	$do_crypto=1 if $_ eq "libeay";
188109998Smarkm	if ($_ eq "crypto") {
189109998Smarkm		$do_crypto=1;
190109998Smarkm		$libname=$_;
191109998Smarkm	}
192160814Ssimon	$no_static_engine=1 if $_ eq "no-static-engine";
193160814Ssimon	$no_static_engine=0 if $_ eq "enable-static-engine";
19455714Skris	$do_update=1 if $_ eq "update";
19568651Skris	$do_rewrite=1 if $_ eq "rewrite";
19659191Skris	$do_ctest=1 if $_ eq "ctest";
19768651Skris	$do_ctestall=1 if $_ eq "ctestall";
198109998Smarkm	$do_checkexist=1 if $_ eq "exist";
19968651Skris	#$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
20055714Skris
20155714Skris	if    (/^no-rc2$/)      { $no_rc2=1; }
20255714Skris	elsif (/^no-rc4$/)      { $no_rc4=1; }
20355714Skris	elsif (/^no-rc5$/)      { $no_rc5=1; }
20455714Skris	elsif (/^no-idea$/)     { $no_idea=1; }
20572613Skris	elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
20655714Skris	elsif (/^no-bf$/)       { $no_bf=1; }
20755714Skris	elsif (/^no-cast$/)     { $no_cast=1; }
208238405Sjkim	elsif (/^no-whirlpool$/)     { $no_whirlpool=1; }
20955714Skris	elsif (/^no-md2$/)      { $no_md2=1; }
21068651Skris	elsif (/^no-md4$/)      { $no_md4=1; }
21155714Skris	elsif (/^no-md5$/)      { $no_md5=1; }
21255714Skris	elsif (/^no-sha$/)      { $no_sha=1; }
21355714Skris	elsif (/^no-ripemd$/)   { $no_ripemd=1; }
21455714Skris	elsif (/^no-mdc2$/)     { $no_mdc2=1; }
21555714Skris	elsif (/^no-rsa$/)      { $no_rsa=1; }
21655714Skris	elsif (/^no-dsa$/)      { $no_dsa=1; }
21755714Skris	elsif (/^no-dh$/)       { $no_dh=1; }
218109998Smarkm	elsif (/^no-ec$/)       { $no_ec=1; }
219160814Ssimon	elsif (/^no-ecdsa$/)	{ $no_ecdsa=1; }
220160814Ssimon	elsif (/^no-ecdh$/) 	{ $no_ecdh=1; }
22155714Skris	elsif (/^no-hmac$/)	{ $no_hmac=1; }
222109998Smarkm	elsif (/^no-aes$/)	{ $no_aes=1; }
223162911Ssimon	elsif (/^no-camellia$/)	{ $no_camellia=1; }
224194206Ssimon	elsif (/^no-seed$/)     { $no_seed=1; }
225109998Smarkm	elsif (/^no-evp$/)	{ $no_evp=1; }
226109998Smarkm	elsif (/^no-lhash$/)	{ $no_lhash=1; }
227109998Smarkm	elsif (/^no-stack$/)	{ $no_stack=1; }
228109998Smarkm	elsif (/^no-err$/)	{ $no_err=1; }
229109998Smarkm	elsif (/^no-buffer$/)	{ $no_buffer=1; }
230109998Smarkm	elsif (/^no-bio$/)	{ $no_bio=1; }
231109998Smarkm	#elsif (/^no-locking$/)	{ $no_locking=1; }
232109998Smarkm	elsif (/^no-comp$/)	{ $no_comp=1; }
233109998Smarkm	elsif (/^no-dso$/)	{ $no_dso=1; }
234109998Smarkm	elsif (/^no-krb5$/)	{ $no_krb5=1; }
235111147Snectar	elsif (/^no-engine$/)	{ $no_engine=1; }
236111147Snectar	elsif (/^no-hw$/)	{ $no_hw=1; }
237160814Ssimon	elsif (/^no-gmp$/)	{ $no_gmp=1; }
238167612Ssimon	elsif (/^no-rfc3779$/)	{ $no_rfc3779=1; }
239194206Ssimon	elsif (/^no-tlsext$/)	{ $no_tlsext=1; }
240194206Ssimon	elsif (/^no-cms$/)	{ $no_cms=1; }
241238405Sjkim	elsif (/^no-ec2m$/)	{ $no_ec2m=1; }
242238405Sjkim	elsif (/^no-ec_nistp_64_gcc_128$/)	{ $no_nistp_gcc=1; }
243238405Sjkim	elsif (/^no-nextprotoneg$/)	{ $no_nextprotoneg=1; }
244238405Sjkim	elsif (/^no-ssl2$/)	{ $no_ssl2=1; }
245298998Sjkim	elsif (/^no-ssl2-method$/) { $no_ssl2_method=1; }
246276861Sjkim	elsif (/^no-ssl3-method$/) { $no_ssl3_method=1; }
247290207Sjkim	elsif (/^no-ssl-trace$/) { $no_ssl_trace=1; }
248194206Ssimon	elsif (/^no-capieng$/)	{ $no_capieng=1; }
249194206Ssimon	elsif (/^no-jpake$/)	{ $no_jpake=1; }
250238405Sjkim	elsif (/^no-srp$/)	{ $no_srp=1; }
251238405Sjkim	elsif (/^no-sctp$/)	{ $no_sctp=1; }
252273144Sjkim	elsif (/^no-srtp$/)	{ $no_srtp=1; }
253269682Sjkim	elsif (/^no-unit-test$/){ $no_unit_test=1; }
25455714Skris	}
25555714Skris
25659191Skris
257109998Smarkmif (!$libname) {
258109998Smarkm	if ($do_ssl) {
259109998Smarkm		$libname="SSLEAY";
260109998Smarkm	}
261109998Smarkm	if ($do_crypto) {
262109998Smarkm		$libname="LIBEAY";
263109998Smarkm	}
264109998Smarkm}
265109998Smarkm
26668651Skris# If no platform is given, assume WIN32
267109998Smarkmif ($W32 + $W16 + $VMS + $OS2 == 0) {
26868651Skris	$W32 = 1;
26968651Skris}
27068651Skris
27168651Skris# Add extra knowledge
27268651Skrisif ($W16) {
27368651Skris	$no_fp_api=1;
27468651Skris}
27568651Skris
27655714Skrisif (!$do_ssl && !$do_crypto)
27755714Skris	{
278109998Smarkm	print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 ]\n";
27955714Skris	exit(1);
28055714Skris	}
28155714Skris
28255714Skris%ssl_list=&load_numbers($ssl_num);
28355714Skris$max_ssl = $max_num;
28455714Skris%crypto_list=&load_numbers($crypto_num);
28555714Skris$max_crypto = $max_num;
28655714Skris
28759191Skrismy $ssl="ssl/ssl.h";
288109998Smarkm$ssl.=" ssl/kssl.h";
289194206Ssimon$ssl.=" ssl/tls1.h";
290238405Sjkim$ssl.=" ssl/srtp.h";
29155714Skris
29259191Skrismy $crypto ="crypto/crypto.h";
293238405Sjkim$crypto.=" crypto/cryptlib.h";
294160814Ssimon$crypto.=" crypto/o_dir.h";
295238405Sjkim$crypto.=" crypto/o_str.h";
296238405Sjkim$crypto.=" crypto/o_time.h";
297109998Smarkm$crypto.=" crypto/des/des.h crypto/des/des_old.h" ; # unless $no_des;
298109998Smarkm$crypto.=" crypto/idea/idea.h" ; # unless $no_idea;
299109998Smarkm$crypto.=" crypto/rc4/rc4.h" ; # unless $no_rc4;
300109998Smarkm$crypto.=" crypto/rc5/rc5.h" ; # unless $no_rc5;
301109998Smarkm$crypto.=" crypto/rc2/rc2.h" ; # unless $no_rc2;
302109998Smarkm$crypto.=" crypto/bf/blowfish.h" ; # unless $no_bf;
303109998Smarkm$crypto.=" crypto/cast/cast.h" ; # unless $no_cast;
304238405Sjkim$crypto.=" crypto/whrlpool/whrlpool.h" ;
305109998Smarkm$crypto.=" crypto/md2/md2.h" ; # unless $no_md2;
306109998Smarkm$crypto.=" crypto/md4/md4.h" ; # unless $no_md4;
307109998Smarkm$crypto.=" crypto/md5/md5.h" ; # unless $no_md5;
308109998Smarkm$crypto.=" crypto/mdc2/mdc2.h" ; # unless $no_mdc2;
309109998Smarkm$crypto.=" crypto/sha/sha.h" ; # unless $no_sha;
310109998Smarkm$crypto.=" crypto/ripemd/ripemd.h" ; # unless $no_ripemd;
311109998Smarkm$crypto.=" crypto/aes/aes.h" ; # unless $no_aes;
312162911Ssimon$crypto.=" crypto/camellia/camellia.h" ; # unless $no_camellia;
313194206Ssimon$crypto.=" crypto/seed/seed.h"; # unless $no_seed;
31455714Skris
31555714Skris$crypto.=" crypto/bn/bn.h";
316109998Smarkm$crypto.=" crypto/rsa/rsa.h" ; # unless $no_rsa;
317109998Smarkm$crypto.=" crypto/dsa/dsa.h" ; # unless $no_dsa;
318109998Smarkm$crypto.=" crypto/dh/dh.h" ; # unless $no_dh;
319109998Smarkm$crypto.=" crypto/ec/ec.h" ; # unless $no_ec;
320160814Ssimon$crypto.=" crypto/ecdsa/ecdsa.h" ; # unless $no_ecdsa;
321160814Ssimon$crypto.=" crypto/ecdh/ecdh.h" ; # unless $no_ecdh;
322109998Smarkm$crypto.=" crypto/hmac/hmac.h" ; # unless $no_hmac;
323238405Sjkim$crypto.=" crypto/cmac/cmac.h" ; # unless $no_hmac;
32455714Skris
325111147Snectar$crypto.=" crypto/engine/engine.h"; # unless $no_engine;
326109998Smarkm$crypto.=" crypto/stack/stack.h" ; # unless $no_stack;
327109998Smarkm$crypto.=" crypto/buffer/buffer.h" ; # unless $no_buffer;
328109998Smarkm$crypto.=" crypto/bio/bio.h" ; # unless $no_bio;
329109998Smarkm$crypto.=" crypto/dso/dso.h" ; # unless $no_dso;
330109998Smarkm$crypto.=" crypto/lhash/lhash.h" ; # unless $no_lhash;
33155714Skris$crypto.=" crypto/conf/conf.h";
33255714Skris$crypto.=" crypto/txt_db/txt_db.h";
33355714Skris
334109998Smarkm$crypto.=" crypto/evp/evp.h" ; # unless $no_evp;
33555714Skris$crypto.=" crypto/objects/objects.h";
33655714Skris$crypto.=" crypto/pem/pem.h";
33755714Skris#$crypto.=" crypto/meth/meth.h";
33855714Skris$crypto.=" crypto/asn1/asn1.h";
339109998Smarkm$crypto.=" crypto/asn1/asn1t.h";
34055714Skris$crypto.=" crypto/asn1/asn1_mac.h";
341109998Smarkm$crypto.=" crypto/err/err.h" ; # unless $no_err;
34255714Skris$crypto.=" crypto/pkcs7/pkcs7.h";
34355714Skris$crypto.=" crypto/pkcs12/pkcs12.h";
34455714Skris$crypto.=" crypto/x509/x509.h";
34555714Skris$crypto.=" crypto/x509/x509_vfy.h";
34655714Skris$crypto.=" crypto/x509v3/x509v3.h";
347238405Sjkim$crypto.=" crypto/ts/ts.h";
34855714Skris$crypto.=" crypto/rand/rand.h";
349109998Smarkm$crypto.=" crypto/comp/comp.h" ; # unless $no_comp;
350109998Smarkm$crypto.=" crypto/ocsp/ocsp.h";
351109998Smarkm$crypto.=" crypto/ui/ui.h crypto/ui/ui_compat.h";
352109998Smarkm$crypto.=" crypto/krb5/krb5_asn.h";
353238405Sjkim#$crypto.=" crypto/store/store.h";
354160814Ssimon$crypto.=" crypto/pqueue/pqueue.h";
355194206Ssimon$crypto.=" crypto/cms/cms.h";
356194206Ssimon$crypto.=" crypto/jpake/jpake.h";
357238405Sjkim$crypto.=" crypto/modes/modes.h";
358238405Sjkim$crypto.=" crypto/srp/srp.h";
35955714Skris
36068651Skrismy $symhacks="crypto/symhacks.h";
36155714Skris
36268651Skrismy @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
36368651Skrismy @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
36455714Skris
36555714Skrisif ($do_update) {
36655714Skris
36755714Skrisif ($do_ssl == 1) {
36868651Skris
36968651Skris	&maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
37068651Skris	if ($do_rewrite == 1) {
37168651Skris		open(OUT, ">$ssl_num");
37268651Skris		&rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
37368651Skris	} else {
37468651Skris		open(OUT, ">>$ssl_num");
37568651Skris	}
37668651Skris	&update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
37755714Skris	close OUT;
37855714Skris}
37955714Skris
38055714Skrisif($do_crypto == 1) {
38168651Skris
38268651Skris	&maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
38368651Skris	if ($do_rewrite == 1) {
38468651Skris		open(OUT, ">$crypto_num");
38568651Skris		&rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
38668651Skris	} else {
38768651Skris		open(OUT, ">>$crypto_num");
38868651Skris	}
38968651Skris	&update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
39055714Skris	close OUT;
39159191Skris}
39255714Skris
393109998Smarkm} elsif ($do_checkexist) {
394109998Smarkm	&check_existing(*ssl_list, @ssl_symbols)
395109998Smarkm		if $do_ssl == 1;
396109998Smarkm	&check_existing(*crypto_list, @crypto_symbols)
397109998Smarkm		if $do_crypto == 1;
39868651Skris} elsif ($do_ctest || $do_ctestall) {
39959191Skris
40059191Skris	print <<"EOF";
40159191Skris
40259191Skris/* Test file to check all DEF file symbols are present by trying
40359191Skris * to link to all of them. This is *not* intended to be run!
40459191Skris */
40559191Skris
40659191Skrisint main()
40759191Skris{
40859191SkrisEOF
40968651Skris	&print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
41059191Skris		if $do_ssl == 1;
41159191Skris
41268651Skris	&print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
41359191Skris		if $do_crypto == 1;
41459191Skris
41559191Skris	print "}\n";
41659191Skris
41755714Skris} else {
41855714Skris
419109998Smarkm	&print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols)
42055714Skris		if $do_ssl == 1;
42155714Skris
422109998Smarkm	&print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols)
42355714Skris		if $do_crypto == 1;
42455714Skris
42555714Skris}
42655714Skris
42755714Skris
42855714Skrissub do_defs
42955714Skris{
43068651Skris	my($name,$files,$symhacksfile)=@_;
43159191Skris	my $file;
43255714Skris	my @ret;
43368651Skris	my %syms;
43468651Skris	my %platform;		# For anything undefined, we assume ""
43568651Skris	my %kind;		# For anything undefined, we assume "FUNCTION"
43668651Skris	my %algorithm;		# For anything undefined, we assume ""
437109998Smarkm	my %variant;
438109998Smarkm	my %variant_cnt;	# To be able to allocate "name{n}" if "name"
439109998Smarkm				# is the same name as the original.
44059191Skris	my $cpp;
441109998Smarkm	my %unknown_algorithms = ();
44255714Skris
44368651Skris	foreach $file (split(/\s+/,$symhacksfile." ".$files))
44455714Skris		{
445109998Smarkm		print STDERR "DEBUG: starting on $file:\n" if $debug;
44655714Skris		open(IN,"<$file") || die "unable to open $file:$!\n";
44759191Skris		my $line = "", my $def= "";
44855714Skris		my %tag = (
44968651Skris			(map { $_ => 0 } @known_platforms),
450109998Smarkm			(map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
451109998Smarkm			(map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
45255714Skris			NOPROTO		=> 0,
45355714Skris			PERL5		=> 0,
45455714Skris			_WINDLL		=> 0,
45555714Skris			CONST_STRICT	=> 0,
45655714Skris			TRUE		=> 1,
45755714Skris		);
45868651Skris		my $symhacking = $file eq $symhacksfile;
459109998Smarkm		my @current_platforms = ();
460109998Smarkm		my @current_algorithms = ();
461109998Smarkm
462109998Smarkm		# params: symbol, alias, platforms, kind
463109998Smarkm		# The reason to put this subroutine in a variable is that
464109998Smarkm		# it will otherwise create it's own, unshared, version of
465109998Smarkm		# %tag and %variant...
466109998Smarkm		my $make_variant = sub
467109998Smarkm		{
468109998Smarkm			my ($s, $a, $p, $k) = @_;
469109998Smarkm			my ($a1, $a2);
470109998Smarkm
471109998Smarkm			print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
472109998Smarkm			if (defined($p))
473109998Smarkm			{
474109998Smarkm				$a1 = join(",",$p,
475109998Smarkm					   grep(!/^$/,
476109998Smarkm						map { $tag{$_} == 1 ? $_ : "" }
477109998Smarkm						@known_platforms));
478109998Smarkm			}
479109998Smarkm			else
480109998Smarkm			{
481109998Smarkm				$a1 = join(",",
482109998Smarkm					   grep(!/^$/,
483109998Smarkm						map { $tag{$_} == 1 ? $_ : "" }
484109998Smarkm						@known_platforms));
485109998Smarkm			}
486109998Smarkm			$a2 = join(",",
487109998Smarkm				   grep(!/^$/,
488109998Smarkm					map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
489109998Smarkm					@known_ossl_platforms));
490109998Smarkm			print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
491109998Smarkm			if ($a1 eq "") { $a1 = $a2; }
492109998Smarkm			elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
493109998Smarkm			if ($a eq $s)
494109998Smarkm			{
495109998Smarkm				if (!defined($variant_cnt{$s}))
496109998Smarkm				{
497109998Smarkm					$variant_cnt{$s} = 0;
498109998Smarkm				}
499109998Smarkm				$variant_cnt{$s}++;
500109998Smarkm				$a .= "{$variant_cnt{$s}}";
501109998Smarkm			}
502109998Smarkm			my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
503109998Smarkm			my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
504109998Smarkm			if (!grep(/^$togrep$/,
505109998Smarkm				  split(/;/, defined($variant{$s})?$variant{$s}:""))) {
506109998Smarkm				if (defined($variant{$s})) { $variant{$s} .= ";"; }
507109998Smarkm				$variant{$s} .= $toadd;
508109998Smarkm			}
509109998Smarkm			print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
510109998Smarkm		};
511109998Smarkm
512109998Smarkm		print STDERR "DEBUG: parsing ----------\n" if $debug;
51355714Skris		while(<IN>) {
514160814Ssimon			if (/\/\* Error codes for the \w+ functions\. \*\//)
515160814Ssimon				{
516160814Ssimon				undef @tag;
517160814Ssimon				last;
518160814Ssimon				}
51955714Skris			if ($line ne '') {
52055714Skris				$_ = $line . $_;
52155714Skris				$line = '';
52255714Skris			}
52355714Skris
52455714Skris			if (/\\$/) {
525109998Smarkm				chomp; # remove eol
526109998Smarkm				chop; # remove ending backslash
52755714Skris				$line = $_;
52855714Skris				next;
52955714Skris			}
53055714Skris
531160814Ssimon			if(/\/\*/) {
532160814Ssimon				if (not /\*\//) {	# multiline comment...
533160814Ssimon					$line = $_;	# ... just accumulate
534160814Ssimon					next;
535160814Ssimon				} else {
536160814Ssimon					s/\/\*.*?\*\///gs;# wipe it
537160814Ssimon				}
538160814Ssimon			}
539160814Ssimon
54055714Skris			if ($cpp) {
541160814Ssimon				$cpp++ if /^#\s*if/;
542160814Ssimon				$cpp-- if /^#\s*endif/;
54355714Skris				next;
54455714Skris	    		}
545160814Ssimon			$cpp = 1 if /^#.*ifdef.*cplusplus/;
54655714Skris
54755714Skris			s/{[^{}]*}//gs;                      # ignore {} blocks
548120631Snectar			print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
549109998Smarkm			print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
550109998Smarkm			if (/^\#\s*ifndef\s+(.*)/) {
551109998Smarkm				push(@tag,"-");
55255714Skris				push(@tag,$1);
55355714Skris				$tag{$1}=-1;
554109998Smarkm				print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
555109998Smarkm			} elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
556109998Smarkm				push(@tag,"-");
557109998Smarkm				if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) {
558109998Smarkm					my $tmp_1 = $1;
559109998Smarkm					my $tmp_;
560109998Smarkm					foreach $tmp_ (split '\&\&',$tmp_1) {
561109998Smarkm						$tmp_ =~ /!defined\(([^\)]+)\)/;
562109998Smarkm						print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
563109998Smarkm						push(@tag,$1);
564109998Smarkm						$tag{$1}=-1;
565109998Smarkm					}
566109998Smarkm				} else {
567109998Smarkm					print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O...
568109998Smarkm					print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
569109998Smarkm					push(@tag,$1);
570109998Smarkm					$tag{$1}=-1;
571109998Smarkm				}
572160814Ssimon			} elsif (/^\#\s*ifdef\s+(\S*)/) {
573109998Smarkm				push(@tag,"-");
57455714Skris				push(@tag,$1);
57555714Skris				$tag{$1}=1;
576109998Smarkm				print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
577109998Smarkm			} elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
578109998Smarkm				push(@tag,"-");
579109998Smarkm				if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) {
580109998Smarkm					my $tmp_1 = $1;
581109998Smarkm					my $tmp_;
582109998Smarkm					foreach $tmp_ (split '\|\|',$tmp_1) {
583109998Smarkm						$tmp_ =~ /defined\(([^\)]+)\)/;
584109998Smarkm						print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
585109998Smarkm						push(@tag,$1);
586109998Smarkm						$tag{$1}=1;
587109998Smarkm					}
588109998Smarkm				} else {
589109998Smarkm					print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O...
590109998Smarkm					print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
591109998Smarkm					push(@tag,$1);
592109998Smarkm					$tag{$1}=1;
593109998Smarkm				}
59468651Skris			} elsif (/^\#\s*error\s+(\w+) is disabled\./) {
595109998Smarkm				my $tag_i = $#tag;
596109998Smarkm				while($tag[$tag_i] ne "-") {
597109998Smarkm					if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
598109998Smarkm						$tag{$tag[$tag_i]}=2;
599109998Smarkm						print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug;
600109998Smarkm					}
601109998Smarkm					$tag_i--;
60268651Skris				}
60355714Skris			} elsif (/^\#\s*endif/) {
604109998Smarkm				my $tag_i = $#tag;
605160814Ssimon				while($tag_i > 0 && $tag[$tag_i] ne "-") {
606109998Smarkm					my $t=$tag[$tag_i];
607109998Smarkm					print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
608109998Smarkm					if ($tag{$t}==2) {
609109998Smarkm						$tag{$t}=-1;
610109998Smarkm					} else {
611109998Smarkm						$tag{$t}=0;
612109998Smarkm					}
613109998Smarkm					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
614109998Smarkm					pop(@tag);
615109998Smarkm					if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
616109998Smarkm						$t=$1;
617109998Smarkm					} else {
618109998Smarkm						$t="";
619109998Smarkm					}
620109998Smarkm					if ($t ne ""
621109998Smarkm					    && !grep(/^$t$/, @known_algorithms)) {
622109998Smarkm						$unknown_algorithms{$t} = 1;
623109998Smarkm						#print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
624109998Smarkm					}
625109998Smarkm					$tag_i--;
62668651Skris				}
62755714Skris				pop(@tag);
62855714Skris			} elsif (/^\#\s*else/) {
629109998Smarkm				my $tag_i = $#tag;
630109998Smarkm				while($tag[$tag_i] ne "-") {
631109998Smarkm					my $t=$tag[$tag_i];
632109998Smarkm					$tag{$t}= -$tag{$t};
633109998Smarkm					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
634109998Smarkm					$tag_i--;
635109998Smarkm				}
63655714Skris			} elsif (/^\#\s*if\s+1/) {
637109998Smarkm				push(@tag,"-");
63855714Skris				# Dummy tag
63955714Skris				push(@tag,"TRUE");
64055714Skris				$tag{"TRUE"}=1;
641109998Smarkm				print STDERR "DEBUG: $file: found 1\n" if $debug;
64259191Skris			} elsif (/^\#\s*if\s+0/) {
643109998Smarkm				push(@tag,"-");
64459191Skris				# Dummy tag
64559191Skris				push(@tag,"TRUE");
64659191Skris				$tag{"TRUE"}=-1;
647109998Smarkm				print STDERR "DEBUG: $file: found 0\n" if $debug;
64868651Skris			} elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
649109998Smarkm				 && $symhacking && $tag{'TRUE'} != -1) {
650109998Smarkm				# This is for aliasing.  When we find an alias,
651109998Smarkm				# we have to invert
652109998Smarkm				&$make_variant($1,$2);
653109998Smarkm				print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
65468651Skris			}
65568651Skris			if (/^\#/) {
656109998Smarkm				@current_platforms =
657109998Smarkm				    grep(!/^$/,
658109998Smarkm					 map { $tag{$_} == 1 ? $_ :
659109998Smarkm						   $tag{$_} == -1 ? "!".$_  : "" }
660109998Smarkm					 @known_platforms);
661109998Smarkm				push @current_platforms
662109998Smarkm				    , grep(!/^$/,
663109998Smarkm					   map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
664109998Smarkm						     $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
665109998Smarkm					   @known_ossl_platforms);
666109998Smarkm				@current_algorithms =
667109998Smarkm				    grep(!/^$/,
668109998Smarkm					 map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
669109998Smarkm					 @known_algorithms);
670109998Smarkm				$def .=
671109998Smarkm				    "#INFO:"
672109998Smarkm					.join(',',@current_platforms).":"
673109998Smarkm					    .join(',',@current_algorithms).";";
67459191Skris				next;
67568651Skris			}
676109998Smarkm			if ($tag{'TRUE'} != -1) {
677109998Smarkm				if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
678109998Smarkm					next;
679109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
680109998Smarkm					$def .= "int d2i_$3(void);";
681109998Smarkm					$def .= "int i2d_$3(void);";
682109998Smarkm					# Variant for platforms that do not
683109998Smarkm					# have to access globale variables
684109998Smarkm					# in shared libraries through functions
685109998Smarkm					$def .=
686109998Smarkm					    "#INFO:"
687109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
688109998Smarkm						    .join(',',@current_algorithms).";";
689109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
690109998Smarkm					$def .=
691109998Smarkm					    "#INFO:"
692109998Smarkm						.join(',',@current_platforms).":"
693109998Smarkm						    .join(',',@current_algorithms).";";
694109998Smarkm					# Variant for platforms that have to
695109998Smarkm					# access globale variables in shared
696109998Smarkm					# libraries through functions
697109998Smarkm					&$make_variant("$2_it","$2_it",
698109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
699109998Smarkm						      "FUNCTION");
700109998Smarkm					next;
701109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
702109998Smarkm					$def .= "int d2i_$3(void);";
703109998Smarkm					$def .= "int i2d_$3(void);";
704109998Smarkm					$def .= "int $3_free(void);";
705109998Smarkm					$def .= "int $3_new(void);";
706109998Smarkm					# Variant for platforms that do not
707109998Smarkm					# have to access globale variables
708109998Smarkm					# in shared libraries through functions
709109998Smarkm					$def .=
710109998Smarkm					    "#INFO:"
711109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
712109998Smarkm						    .join(',',@current_algorithms).";";
713109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
714109998Smarkm					$def .=
715109998Smarkm					    "#INFO:"
716109998Smarkm						.join(',',@current_platforms).":"
717109998Smarkm						    .join(',',@current_algorithms).";";
718109998Smarkm					# Variant for platforms that have to
719109998Smarkm					# access globale variables in shared
720109998Smarkm					# libraries through functions
721109998Smarkm					&$make_variant("$2_it","$2_it",
722109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
723109998Smarkm						      "FUNCTION");
724109998Smarkm					next;
725109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
726109998Smarkm					 /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
727109998Smarkm					$def .= "int d2i_$1(void);";
728109998Smarkm					$def .= "int i2d_$1(void);";
729109998Smarkm					$def .= "int $1_free(void);";
730109998Smarkm					$def .= "int $1_new(void);";
731109998Smarkm					# Variant for platforms that do not
732109998Smarkm					# have to access globale variables
733109998Smarkm					# in shared libraries through functions
734109998Smarkm					$def .=
735109998Smarkm					    "#INFO:"
736109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
737109998Smarkm						    .join(',',@current_algorithms).";";
738109998Smarkm					$def .= "OPENSSL_EXTERN int $1_it;";
739109998Smarkm					$def .=
740109998Smarkm					    "#INFO:"
741109998Smarkm						.join(',',@current_platforms).":"
742109998Smarkm						    .join(',',@current_algorithms).";";
743109998Smarkm					# Variant for platforms that have to
744109998Smarkm					# access globale variables in shared
745109998Smarkm					# libraries through functions
746109998Smarkm					&$make_variant("$1_it","$1_it",
747109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
748109998Smarkm						      "FUNCTION");
749109998Smarkm					next;
750109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
751109998Smarkm					$def .= "int d2i_$2(void);";
752109998Smarkm					$def .= "int i2d_$2(void);";
753109998Smarkm					# Variant for platforms that do not
754109998Smarkm					# have to access globale variables
755109998Smarkm					# in shared libraries through functions
756109998Smarkm					$def .=
757109998Smarkm					    "#INFO:"
758109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
759109998Smarkm						    .join(',',@current_algorithms).";";
760109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
761109998Smarkm					$def .=
762109998Smarkm					    "#INFO:"
763109998Smarkm						.join(',',@current_platforms).":"
764109998Smarkm						    .join(',',@current_algorithms).";";
765109998Smarkm					# Variant for platforms that have to
766109998Smarkm					# access globale variables in shared
767109998Smarkm					# libraries through functions
768109998Smarkm					&$make_variant("$2_it","$2_it",
769109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
770109998Smarkm						      "FUNCTION");
771109998Smarkm					next;
772160814Ssimon				} elsif (/^\s*DECLARE_ASN1_ALLOC_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
773160814Ssimon					$def .= "int $1_free(void);";
774160814Ssimon					$def .= "int $1_new(void);";
775160814Ssimon					next;
776109998Smarkm				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
777109998Smarkm					$def .= "int d2i_$2(void);";
778109998Smarkm					$def .= "int i2d_$2(void);";
779109998Smarkm					$def .= "int $2_free(void);";
780109998Smarkm					$def .= "int $2_new(void);";
781109998Smarkm					# Variant for platforms that do not
782109998Smarkm					# have to access globale variables
783109998Smarkm					# in shared libraries through functions
784109998Smarkm					$def .=
785109998Smarkm					    "#INFO:"
786109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
787109998Smarkm						    .join(',',@current_algorithms).";";
788109998Smarkm					$def .= "OPENSSL_EXTERN int $2_it;";
789109998Smarkm					$def .=
790109998Smarkm					    "#INFO:"
791109998Smarkm						.join(',',@current_platforms).":"
792109998Smarkm						    .join(',',@current_algorithms).";";
793109998Smarkm					# Variant for platforms that have to
794109998Smarkm					# access globale variables in shared
795109998Smarkm					# libraries through functions
796109998Smarkm					&$make_variant("$2_it","$2_it",
797109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
798109998Smarkm						      "FUNCTION");
799109998Smarkm					next;
800109998Smarkm				} elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
801109998Smarkm					# Variant for platforms that do not
802109998Smarkm					# have to access globale variables
803109998Smarkm					# in shared libraries through functions
804109998Smarkm					$def .=
805109998Smarkm					    "#INFO:"
806109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
807109998Smarkm						    .join(',',@current_algorithms).";";
808109998Smarkm					$def .= "OPENSSL_EXTERN int $1_it;";
809109998Smarkm					$def .=
810109998Smarkm					    "#INFO:"
811109998Smarkm						.join(',',@current_platforms).":"
812109998Smarkm						    .join(',',@current_algorithms).";";
813109998Smarkm					# Variant for platforms that have to
814109998Smarkm					# access globale variables in shared
815109998Smarkm					# libraries through functions
816109998Smarkm					&$make_variant("$1_it","$1_it",
817109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
818109998Smarkm						      "FUNCTION");
819109998Smarkm					next;
820160814Ssimon				} elsif (/^\s*DECLARE_ASN1_NDEF_FUNCTION\s*\(\s*(\w*)\s*\)/) {
821160814Ssimon					$def .= "int i2d_$1_NDEF(void);";
822109998Smarkm				} elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
823109998Smarkm					next;
824160814Ssimon				} elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION\s*\(\s*(\w*)\s*\)/) {
825160814Ssimon					$def .= "int $1_print_ctx(void);";
826160814Ssimon					next;
827160814Ssimon				} elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
828160814Ssimon					$def .= "int $2_print_ctx(void);";
829160814Ssimon					next;
830109998Smarkm				} elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
831109998Smarkm					next;
832109998Smarkm				} elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
833160814Ssimon					 /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ||
834160814Ssimon					 /^DECLARE_PEM_rw_const\s*\(\s*(\w*)\s*,/ ) {
835109998Smarkm					# Things not in Win16
836109998Smarkm					$def .=
837109998Smarkm					    "#INFO:"
838109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
839109998Smarkm						    .join(',',@current_algorithms).";";
840109998Smarkm					$def .= "int PEM_read_$1(void);";
841109998Smarkm					$def .= "int PEM_write_$1(void);";
842109998Smarkm					$def .=
843109998Smarkm					    "#INFO:"
844109998Smarkm						.join(',',@current_platforms).":"
845109998Smarkm						    .join(',',@current_algorithms).";";
846109998Smarkm					# Things that are everywhere
847109998Smarkm					$def .= "int PEM_read_bio_$1(void);";
848109998Smarkm					$def .= "int PEM_write_bio_$1(void);";
849109998Smarkm					next;
850109998Smarkm				} elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
851290207Sjkim					/^DECLARE_PEM_write_const\s*\(\s*(\w*)\s*,/ ||
852109998Smarkm					 /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
853109998Smarkm					# Things not in Win16
854109998Smarkm					$def .=
855109998Smarkm					    "#INFO:"
856109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
857109998Smarkm						    .join(',',@current_algorithms).";";
858109998Smarkm					$def .= "int PEM_write_$1(void);";
859109998Smarkm					$def .=
860109998Smarkm					    "#INFO:"
861109998Smarkm						.join(',',@current_platforms).":"
862109998Smarkm						    .join(',',@current_algorithms).";";
863109998Smarkm					# Things that are everywhere
864109998Smarkm					$def .= "int PEM_write_bio_$1(void);";
865109998Smarkm					next;
866109998Smarkm				} elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
867109998Smarkm					 /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
868109998Smarkm					# Things not in Win16
869109998Smarkm					$def .=
870109998Smarkm					    "#INFO:"
871109998Smarkm						.join(',',"!WIN16",@current_platforms).":"
872109998Smarkm						    .join(',',@current_algorithms).";";
873109998Smarkm					$def .= "int PEM_read_$1(void);";
874109998Smarkm					$def .=
875109998Smarkm					    "#INFO:"
876109998Smarkm						.join(',',@current_platforms).":"
877109998Smarkm						    .join(',',@current_algorithms).";";
878109998Smarkm					# Things that are everywhere
879109998Smarkm					$def .= "int PEM_read_bio_$1(void);";
880109998Smarkm					next;
881109998Smarkm				} elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
882109998Smarkm					# Variant for platforms that do not
883109998Smarkm					# have to access globale variables
884109998Smarkm					# in shared libraries through functions
885109998Smarkm					$def .=
886109998Smarkm					    "#INFO:"
887109998Smarkm						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
888109998Smarkm						    .join(',',@current_algorithms).";";
889109998Smarkm					$def .= "OPENSSL_EXTERN int _shadow_$2;";
890109998Smarkm					$def .=
891109998Smarkm					    "#INFO:"
892109998Smarkm						.join(',',@current_platforms).":"
893109998Smarkm						    .join(',',@current_algorithms).";";
894109998Smarkm					# Variant for platforms that have to
895109998Smarkm					# access globale variables in shared
896109998Smarkm					# libraries through functions
897109998Smarkm					&$make_variant("_shadow_$2","_shadow_$2",
898109998Smarkm						      "EXPORT_VAR_AS_FUNCTION",
899109998Smarkm						      "FUNCTION");
900109998Smarkm				} elsif ($tag{'CONST_STRICT'} != 1) {
90168651Skris					if (/\{|\/\*|\([^\)]*$/) {
90255714Skris						$line = $_;
90355714Skris					} else {
90455714Skris						$def .= $_;
90555714Skris					}
90655714Skris				}
90755714Skris			}
908109998Smarkm		}
90955714Skris		close(IN);
91055714Skris
911160814Ssimon		my $algs;
91268651Skris		my $plays;
91368651Skris
914109998Smarkm		print STDERR "DEBUG: postprocessing ----------\n" if $debug;
91555714Skris		foreach (split /;/, $def) {
91668651Skris			my $s; my $k = "FUNCTION"; my $p; my $a;
91755714Skris			s/^[\n\s]*//g;
91855714Skris			s/[\n\s]*$//g;
91968651Skris			next if(/\#undef/);
92055714Skris			next if(/typedef\W/);
92168651Skris			next if(/\#define/);
92268651Skris
923160814Ssimon			# Reduce argument lists to empty ()
924160814Ssimon			# fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
925160814Ssimon			while(/\(.*\)/s) {
926160814Ssimon				s/\([^\(\)]+\)/\{\}/gs;
927160814Ssimon				s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;	#(*f{}) -> f
928160814Ssimon			}
929160814Ssimon			# pretend as we didn't use curly braces: {} -> ()
930160814Ssimon			s/\{\}/\(\)/gs;
931160814Ssimon
932160814Ssimon			s/STACK_OF\(\)/void/gs;
933238405Sjkim			s/LHASH_OF\(\)/void/gs;
934160814Ssimon
935109998Smarkm			print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug;
93668651Skris			if (/^\#INFO:([^:]*):(.*)$/) {
93768651Skris				$plats = $1;
93868651Skris				$algs = $2;
939109998Smarkm				print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug;
94068651Skris				next;
941109998Smarkm			} elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) {
94268651Skris				$s = $1;
94368651Skris				$k = "VARIABLE";
944109998Smarkm				print STDERR "DEBUG: found external variable $s\n" if $debug;
945160814Ssimon			} elsif (/TYPEDEF_\w+_OF/s) {
94655714Skris				next;
947160814Ssimon			} elsif (/(\w+)\s*\(\).*/s) {	# first token prior [first] () is
948160814Ssimon				$s = $1;		# a function name!
949109998Smarkm				print STDERR "DEBUG: found function $s\n" if $debug;
95055714Skris			} elsif (/\(/ and not (/=/)) {
95155714Skris				print STDERR "File $file: cannot parse: $_;\n";
95268651Skris				next;
95368651Skris			} else {
95468651Skris				next;
95555714Skris			}
95668651Skris
95768651Skris			$syms{$s} = 1;
95868651Skris			$kind{$s} = $k;
95968651Skris
96068651Skris			$p = $plats;
96168651Skris			$a = $algs;
96268651Skris			$a .= ",BF" if($s =~ /EVP_bf/);
96368651Skris			$a .= ",CAST" if($s =~ /EVP_cast/);
96468651Skris			$a .= ",DES" if($s =~ /EVP_des/);
96568651Skris			$a .= ",DSA" if($s =~ /EVP_dss/);
96668651Skris			$a .= ",IDEA" if($s =~ /EVP_idea/);
96768651Skris			$a .= ",MD2" if($s =~ /EVP_md2/);
96868651Skris			$a .= ",MD4" if($s =~ /EVP_md4/);
96968651Skris			$a .= ",MD5" if($s =~ /EVP_md5/);
97068651Skris			$a .= ",RC2" if($s =~ /EVP_rc2/);
97168651Skris			$a .= ",RC4" if($s =~ /EVP_rc4/);
97268651Skris			$a .= ",RC5" if($s =~ /EVP_rc5/);
97368651Skris			$a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
97468651Skris			$a .= ",SHA" if($s =~ /EVP_sha/);
97568651Skris			$a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
97668651Skris			$a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
97768651Skris			$a .= ",RSA" if($s =~ /RSAPrivateKey/);
97868651Skris			$a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
97968651Skris
980109998Smarkm			$platform{$s} =
981109998Smarkm			    &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
98268651Skris			$algorithm{$s} .= ','.$a;
98368651Skris
984109998Smarkm			if (defined($variant{$s})) {
985109998Smarkm				foreach $v (split /;/,$variant{$s}) {
986109998Smarkm					(my $r, my $p, my $k) = split(/:/,$v);
987109998Smarkm					my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
988109998Smarkm					$syms{$r} = 1;
989109998Smarkm					if (!defined($k)) { $k = $kind{$s}; }
990109998Smarkm					$kind{$r} = $k."(".$s.")";
991109998Smarkm					$algorithm{$r} = $algorithm{$s};
992109998Smarkm					$platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
993109998Smarkm					$platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
994109998Smarkm					print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
995109998Smarkm				}
99668651Skris			}
997109998Smarkm			print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
99855714Skris		}
99955714Skris	}
100055714Skris
100168651Skris	# Prune the returned symbols
100255714Skris
100368651Skris        delete $syms{"bn_dump1"};
100468651Skris	$platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
100568651Skris
1006109998Smarkm	$platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
1007109998Smarkm	$platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
1008109998Smarkm	$platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
1009109998Smarkm	$platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
1010205128Ssimon	$platform{"EVP_sha384"} = "!VMSVAX";
1011205128Ssimon	$platform{"EVP_sha512"} = "!VMSVAX";
1012205128Ssimon	$platform{"SHA384_Init"} = "!VMSVAX";
1013205128Ssimon	$platform{"SHA384_Transform"} = "!VMSVAX";
1014205128Ssimon	$platform{"SHA384_Update"} = "!VMSVAX";
1015205128Ssimon	$platform{"SHA384_Final"} = "!VMSVAX";
1016205128Ssimon	$platform{"SHA384"} = "!VMSVAX";
1017205128Ssimon	$platform{"SHA512_Init"} = "!VMSVAX";
1018205128Ssimon	$platform{"SHA512_Transform"} = "!VMSVAX";
1019205128Ssimon	$platform{"SHA512_Update"} = "!VMSVAX";
1020205128Ssimon	$platform{"SHA512_Final"} = "!VMSVAX";
1021205128Ssimon	$platform{"SHA512"} = "!VMSVAX";
1022238405Sjkim	$platform{"WHIRLPOOL_Init"} = "!VMSVAX";
1023238405Sjkim	$platform{"WHIRLPOOL"} = "!VMSVAX";
1024238405Sjkim	$platform{"WHIRLPOOL_BitUpdate"} = "!VMSVAX";
1025238405Sjkim	$platform{"EVP_whirlpool"} = "!VMSVAX";
1026238405Sjkim	$platform{"WHIRLPOOL_Final"} = "!VMSVAX";
1027238405Sjkim	$platform{"WHIRLPOOL_Update"} = "!VMSVAX";
1028205128Ssimon
1029215697Ssimon
103068651Skris	# Info we know about
103168651Skris
103268651Skris	push @ret, map { $_."\\".&info_string($_,"EXIST",
103368651Skris					      $platform{$_},
103468651Skris					      $kind{$_},
103568651Skris					      $algorithm{$_}) } keys %syms;
103668651Skris
1037109998Smarkm	if (keys %unknown_algorithms) {
1038109998Smarkm		print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
1039109998Smarkm		print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
1040109998Smarkm	}
104168651Skris	return(@ret);
104268651Skris}
104368651Skris
1044109998Smarkm# Param: string of comma-separated platform-specs.
1045109998Smarkmsub reduce_platforms
1046109998Smarkm{
1047109998Smarkm	my ($platforms) = @_;
104868651Skris	my $pl = defined($platforms) ? $platforms : "";
104968651Skris	my %p = map { $_ => 0 } split /,/, $pl;
105068651Skris	my $ret;
105168651Skris
1052109998Smarkm	print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
1053109998Smarkm	    if $debug;
105468651Skris	# We do this, because if there's code like the following, it really
105568651Skris	# means the function exists in all cases and should therefore be
105668651Skris	# everywhere.  By increasing and decreasing, we may attain 0:
105768651Skris	#
105868651Skris	# ifndef WIN16
105968651Skris	#    int foo();
106068651Skris	# else
106168651Skris	#    int _fat foo();
106268651Skris	# endif
106368651Skris	foreach $platform (split /,/, $pl) {
106468651Skris		if ($platform =~ /^!(.*)$/) {
106568651Skris			$p{$1}--;
106668651Skris		} else {
106768651Skris			$p{$platform}++;
106855714Skris		}
106955714Skris	}
107068651Skris	foreach $platform (keys %p) {
107168651Skris		if ($p{$platform} == 0) { delete $p{$platform}; }
107255714Skris	}
107355714Skris
107468651Skris	delete $p{""};
1075109998Smarkm
1076109998Smarkm	$ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
1077109998Smarkm	print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
1078109998Smarkm	    if $debug;
1079109998Smarkm	return $ret;
1080109998Smarkm}
1081109998Smarkm
1082109998Smarkmsub info_string {
1083109998Smarkm	(my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
1084109998Smarkm
1085109998Smarkm	my %a = defined($algorithms) ?
1086109998Smarkm	    map { $_ => 1 } split /,/, $algorithms : ();
1087109998Smarkm	my $k = defined($kind) ? $kind : "FUNCTION";
1088109998Smarkm	my $ret;
1089109998Smarkm	my $p = &reduce_platforms($platforms);
1090109998Smarkm
109168651Skris	delete $a{""};
109255714Skris
109368651Skris	$ret = $exist;
1094109998Smarkm	$ret .= ":".$p;
109568651Skris	$ret .= ":".$k;
1096109998Smarkm	$ret .= ":".join(',',sort keys %a);
109768651Skris	return $ret;
109855714Skris}
109955714Skris
110068651Skrissub maybe_add_info {
110168651Skris	(my $name, *nums, my @symbols) = @_;
110268651Skris	my $sym;
110368651Skris	my $new_info = 0;
1104109998Smarkm	my %syms=();
110568651Skris
110668651Skris	print STDERR "Updating $name info\n";
110768651Skris	foreach $sym (@symbols) {
110868651Skris		(my $s, my $i) = split /\\/, $sym;
110968651Skris		if (defined($nums{$s})) {
1110109998Smarkm			$i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
111168651Skris			(my $n, my $dummy) = split /\\/, $nums{$s};
111268651Skris			if (!defined($dummy) || $i ne $dummy) {
111368651Skris				$nums{$s} = $n."\\".$i;
111468651Skris				$new_info++;
1115109998Smarkm				print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
111668651Skris			}
111768651Skris		}
1118109998Smarkm		$syms{$s} = 1;
111968651Skris	}
1120109998Smarkm
1121109998Smarkm	my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
1122109998Smarkm	foreach $sym (@s) {
1123109998Smarkm		(my $n, my $i) = split /\\/, $nums{$sym};
1124109998Smarkm		if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
1125109998Smarkm			$new_info++;
1126109998Smarkm			print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
1127109998Smarkm		}
1128109998Smarkm	}
112968651Skris	if ($new_info) {
113068651Skris		print STDERR "$new_info old symbols got an info update\n";
113168651Skris		if (!$do_rewrite) {
113268651Skris			print STDERR "You should do a rewrite to fix this.\n";
113368651Skris		}
113468651Skris	} else {
113568651Skris		print STDERR "No old symbols needed info update\n";
113668651Skris	}
113768651Skris}
113868651Skris
1139109998Smarkm# Param: string of comma-separated keywords, each possibly prefixed with a "!"
1140109998Smarkmsub is_valid
1141109998Smarkm{
1142109998Smarkm	my ($keywords_txt,$platforms) = @_;
1143109998Smarkm	my (@keywords) = split /,/,$keywords_txt;
1144160814Ssimon	my ($falsesum, $truesum) = (0, 1);
1145109998Smarkm
1146109998Smarkm	# Param: one keyword
1147109998Smarkm	sub recognise
1148109998Smarkm	{
1149109998Smarkm		my ($keyword,$platforms) = @_;
1150109998Smarkm
1151109998Smarkm		if ($platforms) {
1152109998Smarkm			# platforms
1153205128Ssimon			if ($keyword eq "VMSVAX" && $VMSVAX) { return 1; }
1154205128Ssimon			if ($keyword eq "VMSNonVAX" && $VMSNonVAX) { return 1; }
1155238405Sjkim			if ($keyword eq "VMS" && $VMS) { return 1; }
1156109998Smarkm			if ($keyword eq "WIN32" && $W32) { return 1; }
1157109998Smarkm			if ($keyword eq "WIN16" && $W16) { return 1; }
1158109998Smarkm			if ($keyword eq "WINNT" && $NT) { return 1; }
1159109998Smarkm			if ($keyword eq "OS2" && $OS2) { return 1; }
1160109998Smarkm			# Special platforms:
1161109998Smarkm			# EXPORT_VAR_AS_FUNCTION means that global variables
1162109998Smarkm			# will be represented as functions.  This currently
1163109998Smarkm			# only happens on VMS-VAX.
1164109998Smarkm			if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && ($VMSVAX || $W32 || $W16)) {
1165109998Smarkm				return 1;
1166109998Smarkm			}
1167194206Ssimon			if ($keyword eq "OPENSSL_FIPS" && $fips) {
1168194206Ssimon				return 1;
1169194206Ssimon			}
1170194206Ssimon			if ($keyword eq "ZLIB" && $zlib) { return 1; }
1171109998Smarkm			return 0;
1172109998Smarkm		} else {
1173109998Smarkm			# algorithms
1174109998Smarkm			if ($keyword eq "RC2" && $no_rc2) { return 0; }
1175109998Smarkm			if ($keyword eq "RC4" && $no_rc4) { return 0; }
1176109998Smarkm			if ($keyword eq "RC5" && $no_rc5) { return 0; }
1177109998Smarkm			if ($keyword eq "IDEA" && $no_idea) { return 0; }
1178109998Smarkm			if ($keyword eq "DES" && $no_des) { return 0; }
1179109998Smarkm			if ($keyword eq "BF" && $no_bf) { return 0; }
1180109998Smarkm			if ($keyword eq "CAST" && $no_cast) { return 0; }
1181109998Smarkm			if ($keyword eq "MD2" && $no_md2) { return 0; }
1182109998Smarkm			if ($keyword eq "MD4" && $no_md4) { return 0; }
1183109998Smarkm			if ($keyword eq "MD5" && $no_md5) { return 0; }
1184109998Smarkm			if ($keyword eq "SHA" && $no_sha) { return 0; }
1185109998Smarkm			if ($keyword eq "RIPEMD" && $no_ripemd) { return 0; }
1186109998Smarkm			if ($keyword eq "MDC2" && $no_mdc2) { return 0; }
1187238405Sjkim			if ($keyword eq "WHIRLPOOL" && $no_whirlpool) { return 0; }
1188109998Smarkm			if ($keyword eq "RSA" && $no_rsa) { return 0; }
1189109998Smarkm			if ($keyword eq "DSA" && $no_dsa) { return 0; }
1190109998Smarkm			if ($keyword eq "DH" && $no_dh) { return 0; }
1191109998Smarkm			if ($keyword eq "EC" && $no_ec) { return 0; }
1192160814Ssimon			if ($keyword eq "ECDSA" && $no_ecdsa) { return 0; }
1193160814Ssimon			if ($keyword eq "ECDH" && $no_ecdh) { return 0; }
1194109998Smarkm			if ($keyword eq "HMAC" && $no_hmac) { return 0; }
1195109998Smarkm			if ($keyword eq "AES" && $no_aes) { return 0; }
1196162911Ssimon			if ($keyword eq "CAMELLIA" && $no_camellia) { return 0; }
1197194206Ssimon			if ($keyword eq "SEED" && $no_seed) { return 0; }
1198109998Smarkm			if ($keyword eq "EVP" && $no_evp) { return 0; }
1199109998Smarkm			if ($keyword eq "LHASH" && $no_lhash) { return 0; }
1200109998Smarkm			if ($keyword eq "STACK" && $no_stack) { return 0; }
1201109998Smarkm			if ($keyword eq "ERR" && $no_err) { return 0; }
1202109998Smarkm			if ($keyword eq "BUFFER" && $no_buffer) { return 0; }
1203109998Smarkm			if ($keyword eq "BIO" && $no_bio) { return 0; }
1204109998Smarkm			if ($keyword eq "COMP" && $no_comp) { return 0; }
1205109998Smarkm			if ($keyword eq "DSO" && $no_dso) { return 0; }
1206109998Smarkm			if ($keyword eq "KRB5" && $no_krb5) { return 0; }
1207111147Snectar			if ($keyword eq "ENGINE" && $no_engine) { return 0; }
1208111147Snectar			if ($keyword eq "HW" && $no_hw) { return 0; }
1209109998Smarkm			if ($keyword eq "FP_API" && $no_fp_api) { return 0; }
1210160814Ssimon			if ($keyword eq "STATIC_ENGINE" && $no_static_engine) { return 0; }
1211160814Ssimon			if ($keyword eq "GMP" && $no_gmp) { return 0; }
1212167612Ssimon			if ($keyword eq "RFC3779" && $no_rfc3779) { return 0; }
1213194206Ssimon			if ($keyword eq "TLSEXT" && $no_tlsext) { return 0; }
1214238405Sjkim			if ($keyword eq "PSK" && $no_psk) { return 0; }
1215194206Ssimon			if ($keyword eq "CMS" && $no_cms) { return 0; }
1216238405Sjkim			if ($keyword eq "EC2M" && $no_ec2m) { return 0; }
1217238405Sjkim			if ($keyword eq "NEXTPROTONEG" && $no_nextprotoneg) { return 0; }
1218238405Sjkim			if ($keyword eq "EC_NISTP_64_GCC_128" && $no_nistp_gcc)
1219238405Sjkim					{ return 0; }
1220238405Sjkim			if ($keyword eq "SSL2" && $no_ssl2) { return 0; }
1221298998Sjkim			if ($keyword eq "SSL2_METHOD" && $no_ssl2_method) { return 0; }
1222276861Sjkim			if ($keyword eq "SSL3_METHOD" && $no_ssl3_method) { return 0; }
1223290207Sjkim			if ($keyword eq "SSL_TRACE" && $no_ssl_trace) { return 0; }
1224194206Ssimon			if ($keyword eq "CAPIENG" && $no_capieng) { return 0; }
1225194206Ssimon			if ($keyword eq "JPAKE" && $no_jpake) { return 0; }
1226238405Sjkim			if ($keyword eq "SRP" && $no_srp) { return 0; }
1227238405Sjkim			if ($keyword eq "SCTP" && $no_sctp) { return 0; }
1228273144Sjkim			if ($keyword eq "SRTP" && $no_srtp) { return 0; }
1229269682Sjkim			if ($keyword eq "UNIT_TEST" && $no_unit_test) { return 0; }
1230160814Ssimon			if ($keyword eq "DEPRECATED" && $no_deprecated) { return 0; }
1231109998Smarkm
1232109998Smarkm			# Nothing recognise as true
1233109998Smarkm			return 1;
1234109998Smarkm		}
1235109998Smarkm	}
1236109998Smarkm
1237109998Smarkm	foreach $k (@keywords) {
1238109998Smarkm		if ($k =~ /^!(.*)$/) {
1239109998Smarkm			$falsesum += &recognise($1,$platforms);
1240109998Smarkm		} else {
1241160814Ssimon			$truesum *= &recognise($k,$platforms);
1242109998Smarkm		}
1243109998Smarkm	}
1244109998Smarkm	print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1245109998Smarkm	return (!$falsesum) && $truesum;
1246109998Smarkm}
1247109998Smarkm
124859191Skrissub print_test_file
124959191Skris{
1250109998Smarkm	(*OUT,my $name,*nums,my $testall,my @symbols)=@_;
125159191Skris	my $n = 1; my @e; my @r;
125268651Skris	my $sym; my $prev = ""; my $prefSSLeay;
125359191Skris
1254109998Smarkm	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1255109998Smarkm	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
125668651Skris	@symbols=((sort @e),(sort @r));
125759191Skris
125868651Skris	foreach $sym (@symbols) {
125968651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1260109998Smarkm		my $v = 0;
1261109998Smarkm		$v = 1 if $i=~ /^.*?:.*?:VARIABLE/;
1262109998Smarkm		my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1263109998Smarkm		my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1264109998Smarkm		if (!defined($nums{$s})) {
1265109998Smarkm			print STDERR "Warning: $s does not have a number assigned\n"
1266109998Smarkm			    if(!$do_update);
1267109998Smarkm		} elsif (is_valid($p,1) && is_valid($a,0)) {
1268109998Smarkm			my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1269109998Smarkm			if ($prev eq $s2) {
1270109998Smarkm				print OUT "\t/* The following has already appeared previously */\n";
1271109998Smarkm				print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1272109998Smarkm			}
1273109998Smarkm			$prev = $s2;	# To warn about duplicates...
1274109998Smarkm
1275109998Smarkm			($nn,$ni)=($nums{$s2} =~ /^(.*?)\\(.*)$/);
1276109998Smarkm			if ($v) {
1277109998Smarkm				print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n";
127868651Skris			} else {
1279109998Smarkm				print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n";
128068651Skris			}
128159191Skris		}
128259191Skris	}
128359191Skris}
128459191Skris
1285127128Snectarsub get_version {
1286127128Snectar   local *MF;
1287127128Snectar   my $v = '?';
1288142425Snectar   open MF, 'Makefile' or return $v;
1289127128Snectar   while (<MF>) {
1290127128Snectar     $v = $1, last if /^VERSION=(.*?)\s*$/;
1291127128Snectar   }
1292127128Snectar   close MF;
1293127128Snectar   return $v;
1294127128Snectar}
1295127128Snectar
129655714Skrissub print_def_file
129755714Skris{
129868651Skris	(*OUT,my $name,*nums,my @symbols)=@_;
1299109998Smarkm	my $n = 1; my @e; my @r; my @v; my $prev="";
1300109998Smarkm	my $liboptions="";
1301127128Snectar	my $libname = $name;
1302127128Snectar	my $http_vendor = 'www.openssl.org/';
1303127128Snectar	my $version = get_version();
1304127128Snectar	my $what = "OpenSSL: implementation of Secure Socket Layer";
1305127128Snectar	my $description = "$what $version, $name - http://$http_vendor";
130655714Skris
130755714Skris	if ($W32)
1308127128Snectar		{ $libname.="32"; }
1309109998Smarkm	elsif ($W16)
1310127128Snectar		{ $libname.="16"; }
1311109998Smarkm	elsif ($OS2)
1312127128Snectar		{ # DLL names should not clash on the whole system.
1313127128Snectar		  # However, they should not have any particular relationship
1314127128Snectar		  # to the name of the static library.  Chose descriptive names
1315127128Snectar		  # (must be at most 8 chars).
1316127128Snectar		  my %translate = (ssl => 'open_ssl', crypto => 'cryptssl');
1317127128Snectar		  $libname = $translate{$name} || $name;
1318127128Snectar		  $liboptions = <<EOO;
1319127128SnectarINITINSTANCE
1320127128SnectarDATA MULTIPLE NONSHARED
1321127128SnectarEOO
1322127128Snectar		  # Vendor field can't contain colon, drat; so we omit http://
1323127128Snectar		  $description = "\@#$http_vendor:$version#\@$what; DLL for library $name.  Build for EMX -Zmtd";
1324127128Snectar		}
132555714Skris
132655714Skris	print OUT <<"EOF";
132755714Skris;
132855714Skris; Definition file for the DLL version of the $name library from OpenSSL
132955714Skris;
133055714Skris
1331127128SnectarLIBRARY         $libname	$liboptions
133255714Skris
133355714SkrisEOF
133455714Skris
1335109998Smarkm	if ($W16) {
133655714Skris		print <<"EOF";
133755714SkrisCODE            PRELOAD MOVEABLE
133855714SkrisDATA            PRELOAD MOVEABLE SINGLE
133955714Skris
134055714SkrisEXETYPE		WINDOWS
134155714Skris
134255714SkrisHEAPSIZE	4096
134355714SkrisSTACKSIZE	8192
134455714Skris
134555714SkrisEOF
134655714Skris	}
134755714Skris
134855714Skris	print "EXPORTS\n";
134955714Skris
1350109998Smarkm	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1351109998Smarkm	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1352109998Smarkm	(@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols);
1353109998Smarkm	@symbols=((sort @e),(sort @r), (sort @v));
135455714Skris
135555714Skris
135668651Skris	foreach $sym (@symbols) {
135768651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1358109998Smarkm		my $v = 0;
1359109998Smarkm		$v = 1 if $i =~ /^.*?:.*?:VARIABLE/;
136068651Skris		if (!defined($nums{$s})) {
136168651Skris			printf STDERR "Warning: $s does not have a number assigned\n"
1362109998Smarkm			    if(!$do_update);
136355714Skris		} else {
1364109998Smarkm			(my $n, my $dummy) = split /\\/, $nums{$s};
136568651Skris			my %pf = ();
1366109998Smarkm			my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1367109998Smarkm			my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1368109998Smarkm			if (is_valid($p,1) && is_valid($a,0)) {
1369109998Smarkm				my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1370109998Smarkm				if ($prev eq $s2) {
1371109998Smarkm					print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1372109998Smarkm				}
1373109998Smarkm				$prev = $s2;	# To warn about duplicates...
1374109998Smarkm				if($v && !$OS2) {
1375109998Smarkm					printf OUT "    %s%-39s @%-8d DATA\n",($W32)?"":"_",$s2,$n;
1376109998Smarkm				} else {
1377109998Smarkm					printf OUT "    %s%-39s @%d\n",($W32||$OS2)?"":"_",$s2,$n;
1378109998Smarkm				}
137968651Skris			}
138055714Skris		}
138155714Skris	}
138255714Skris	printf OUT "\n";
138355714Skris}
138455714Skris
138555714Skrissub load_numbers
138655714Skris{
138755714Skris	my($name)=@_;
138855714Skris	my(@a,%ret);
138955714Skris
139055714Skris	$max_num = 0;
139168651Skris	$num_noinfo = 0;
139268651Skris	$prev = "";
1393109998Smarkm	$prev_cnt = 0;
139455714Skris
139555714Skris	open(IN,"<$name") || die "unable to open $name:$!\n";
139655714Skris	while (<IN>) {
139755714Skris		chop;
139855714Skris		s/#.*$//;
139955714Skris		next if /^\s*$/;
140055714Skris		@a=split;
140168651Skris		if (defined $ret{$a[0]}) {
1402109998Smarkm			# This is actually perfectly OK
1403109998Smarkm			#print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
140468651Skris		}
140568651Skris		if ($max_num > $a[1]) {
140668651Skris			print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
140768651Skris		}
1408109998Smarkm		elsif ($max_num == $a[1]) {
140968651Skris			# This is actually perfectly OK
141068651Skris			#print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1411109998Smarkm			if ($a[0] eq $prev) {
1412109998Smarkm				$prev_cnt++;
1413109998Smarkm				$a[0] .= "{$prev_cnt}";
1414109998Smarkm			}
141568651Skris		}
1416109998Smarkm		else {
1417109998Smarkm			$prev_cnt = 0;
1418109998Smarkm		}
141968651Skris		if ($#a < 2) {
142068651Skris			# Existence will be proven later, in do_defs
142168651Skris			$ret{$a[0]}=$a[1];
142268651Skris			$num_noinfo++;
142368651Skris		} else {
142468651Skris			$ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
142568651Skris		}
142655714Skris		$max_num = $a[1] if $a[1] > $max_num;
142768651Skris		$prev=$a[0];
142855714Skris	}
142968651Skris	if ($num_noinfo) {
143068651Skris		print STDERR "Warning: $num_noinfo symbols were without info.";
143168651Skris		if ($do_rewrite) {
143268651Skris			printf STDERR "  The rewrite will fix this.\n";
143368651Skris		} else {
143468651Skris			printf STDERR "  You should do a rewrite to fix this.\n";
143568651Skris		}
143668651Skris	}
143755714Skris	close(IN);
143855714Skris	return(%ret);
143955714Skris}
144055714Skris
144168651Skrissub parse_number
144268651Skris{
144368651Skris	(my $str, my $what) = @_;
144468651Skris	(my $n, my $i) = split(/\\/,$str);
144568651Skris	if ($what eq "n") {
144668651Skris		return $n;
144768651Skris	} else {
144868651Skris		return $i;
144968651Skris	}
145068651Skris}
145168651Skris
145268651Skrissub rewrite_numbers
145368651Skris{
145468651Skris	(*OUT,$name,*nums,@symbols)=@_;
145568651Skris	my $thing;
145668651Skris
145768651Skris	print STDERR "Rewriting $name\n";
145868651Skris
1459109998Smarkm	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
146068651Skris	my $r; my %r; my %rsyms;
146168651Skris	foreach $r (@r) {
146268651Skris		(my $s, my $i) = split /\\/, $r;
146368651Skris		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
146468651Skris		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
146568651Skris		$r{$a} = $s."\\".$i;
146668651Skris		$rsyms{$s} = 1;
146768651Skris	}
146868651Skris
1469109998Smarkm	my %syms = ();
1470109998Smarkm	foreach $_ (@symbols) {
1471109998Smarkm		(my $n, my $i) = split /\\/;
1472109998Smarkm		$syms{$n} = 1;
1473109998Smarkm	}
1474109998Smarkm
1475109998Smarkm	my @s=sort {
1476109998Smarkm	    &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1477109998Smarkm	    || $a cmp $b
1478109998Smarkm	} keys %nums;
147968651Skris	foreach $sym (@s) {
148068651Skris		(my $n, my $i) = split /\\/, $nums{$sym};
148168651Skris		next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
148268651Skris		next if defined($rsyms{$sym});
1483109998Smarkm		print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1484109998Smarkm		$i="NOEXIST::FUNCTION:"
1485109998Smarkm			if !defined($i) || $i eq "" || !defined($syms{$sym});
1486109998Smarkm		my $s2 = $sym;
1487109998Smarkm		$s2 =~ s/\{[0-9]+\}$//;
1488109998Smarkm		printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
148968651Skris		if (exists $r{$sym}) {
149068651Skris			(my $s, $i) = split /\\/,$r{$sym};
1491109998Smarkm			my $s2 = $s;
1492109998Smarkm			$s2 =~ s/\{[0-9]+\}$//;
1493109998Smarkm			printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
149468651Skris		}
149568651Skris	}
149668651Skris}
149768651Skris
149855714Skrissub update_numbers
149955714Skris{
150068651Skris	(*OUT,$name,*nums,my $start_num, my @symbols)=@_;
150168651Skris	my $new_syms = 0;
150268651Skris
150368651Skris	print STDERR "Updating $name numbers\n";
150468651Skris
1505109998Smarkm	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
150668651Skris	my $r; my %r; my %rsyms;
150768651Skris	foreach $r (@r) {
150868651Skris		(my $s, my $i) = split /\\/, $r;
150968651Skris		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
151068651Skris		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
151168651Skris		$r{$a} = $s."\\".$i;
151268651Skris		$rsyms{$s} = 1;
151368651Skris	}
151468651Skris
151568651Skris	foreach $sym (@symbols) {
151668651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
151768651Skris		next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
151868651Skris		next if defined($rsyms{$sym});
151968651Skris		die "ERROR: Symbol $sym had no info attached to it."
152068651Skris		    if $i eq "";
152168651Skris		if (!exists $nums{$s}) {
152268651Skris			$new_syms++;
1523109998Smarkm			my $s2 = $s;
1524109998Smarkm			$s2 =~ s/\{[0-9]+\}$//;
1525109998Smarkm			printf OUT "%s%-39s %d\t%s\n","",$s2, ++$start_num,$i;
152668651Skris			if (exists $r{$s}) {
152768651Skris				($s, $i) = split /\\/,$r{$s};
1528109998Smarkm				$s =~ s/\{[0-9]+\}$//;
1529109998Smarkm				printf OUT "%s%-39s %d\t%s\n","",$s, $start_num,$i;
153068651Skris			}
153155714Skris		}
153255714Skris	}
153368651Skris	if($new_syms) {
153468651Skris		print STDERR "$new_syms New symbols added\n";
153555714Skris	} else {
153668651Skris		print STDERR "No New symbols Added\n";
153755714Skris	}
153855714Skris}
153968651Skris
154068651Skrissub check_existing
154168651Skris{
154268651Skris	(*nums, my @symbols)=@_;
154368651Skris	my %existing; my @remaining;
154468651Skris	@remaining=();
154568651Skris	foreach $sym (@symbols) {
154668651Skris		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
154768651Skris		$existing{$s}=1;
154868651Skris	}
154968651Skris	foreach $sym (keys %nums) {
155068651Skris		if (!exists $existing{$sym}) {
155168651Skris			push @remaining, $sym;
155268651Skris		}
155368651Skris	}
155468651Skris	if(@remaining) {
155568651Skris		print STDERR "The following symbols do not seem to exist:\n";
155668651Skris		foreach $sym (@remaining) {
155768651Skris			print STDERR "\t",$sym,"\n";
155868651Skris		}
155968651Skris	}
156068651Skris}
156168651Skris
1562