xref: /freebsd/crypto/openssl/util/mkdef.pl (revision 15c433351f54e7cd5bec8d36c8e89e6a7fa55b26)
1#!/usr/local/bin/perl -w
2#
3# generate a .def file
4#
5# It does this by parsing the header files and looking for the
6# prototyped functions: it then prunes the output.
7#
8# Intermediary files are created, call libeay.num and ssleay.num,...
9# Previously, they had the following format:
10#
11#	routine-name	nnnn
12#
13# But that isn't enough for a number of reasons, the first on being that
14# this format is (needlessly) very Win32-centric, and even then...
15# One of the biggest problems is that there's no information about what
16# routines should actually be used, which varies with what crypto algorithms
17# are disabled.  Also, some operating systems (for example VMS with VAX C)
18# need to keep track of the global variables as well as the functions.
19#
20# So, a remake of this script is done so as to include information on the
21# kind of symbol it is (function or variable) and what algorithms they're
22# part of.  This will allow easy translating to .def files or the corresponding
23# file in other operating systems (a .opt file for VMS, possibly with a .mar
24# file).
25#
26# The format now becomes:
27#
28#	routine-name	nnnn	info
29#
30# and the "info" part is actually a colon-separated string of fields with
31# the following meaning:
32#
33#	existence:platform:kind:algorithms
34#
35# - "existence" can be "EXIST" or "NOEXIST" depending on if the symbol is
36#   found somewhere in the source,
37# - "platforms" is empty if it exists on all platforms, otherwise it contains
38#   comma-separated list of the platform, just as they are if the symbol exists
39#   for those platforms, or prepended with a "!" if not.  This helps resolve
40#   symbol name variants for platforms where the names are too long for the
41#   compiler or linker, or if the systems is case insensitive and there is a
42#   clash, or the symbol is implemented differently (see
43#   EXPORT_VAR_AS_FUNCTION).  This script assumes renaming of symbols is found
44#   in the file crypto/symhacks.h.
45#   The semantics for the platforms is that every item is checked against the
46#   environment.  For the negative items ("!FOO"), if any of them is false
47#   (i.e. "FOO" is true) in the environment, the corresponding symbol can't be
48#   used.  For the positive itms, if all of them are false in the environment,
49#   the corresponding symbol can't be used.  Any combination of positive and
50#   negative items are possible, and of course leave room for some redundancy.
51# - "kind" is "FUNCTION" or "VARIABLE".  The meaning of that is obvious.
52# - "algorithms" is a comma-separated list of algorithm names.  This helps
53#   exclude symbols that are part of an algorithm that some user wants to
54#   exclude.
55#
56
57my $debug=0;
58
59my $crypto_num= "util/libeay.num";
60my $ssl_num=    "util/ssleay.num";
61my $libname;
62
63my $do_update = 0;
64my $do_rewrite = 1;
65my $do_crypto = 0;
66my $do_ssl = 0;
67my $do_ctest = 0;
68my $do_ctestall = 0;
69my $do_checkexist = 0;
70
71my $VMSVAX=0;
72my $VMSNonVAX=0;
73my $VMS=0;
74my $W32=0;
75my $W16=0;
76my $NT=0;
77my $OS2=0;
78# Set this to make typesafe STACK definitions appear in DEF
79my $safe_stack_def = 0;
80
81my @known_platforms = ( "__FreeBSD__", "PERL5", "NeXT",
82			"EXPORT_VAR_AS_FUNCTION", "ZLIB", "OPENSSL_FIPS" );
83my @known_ossl_platforms = ( "VMS", "WIN16", "WIN32", "WINNT", "OS2" );
84my @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
85			 "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1",
86			 "SHA256", "SHA512", "RIPEMD",
87			 "MDC2", "WHIRLPOOL", "RSA", "DSA", "DH", "EC", "ECDH", "ECDSA", "EC2M",
88			 "HMAC", "AES", "CAMELLIA", "SEED", "GOST",
89			 # EC_NISTP_64_GCC_128
90			 "EC_NISTP_64_GCC_128",
91			 # Envelope "algorithms"
92			 "EVP", "X509", "ASN1_TYPEDEFS",
93			 # Helper "algorithms"
94			 "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
95			 "LOCKING",
96			 # External "algorithms"
97			 "FP_API", "STDIO", "SOCK", "KRB5", "DGRAM",
98			 # Engines
99			 "STATIC_ENGINE", "ENGINE", "HW", "GMP",
100			 # RFC3779
101			 "RFC3779",
102			 # TLS
103			 "TLSEXT", "PSK", "SRP", "HEARTBEATS",
104			 # CMS
105			 "CMS",
106			 # CryptoAPI Engine
107			 "CAPIENG",
108			 # SSL v2
109			 "SSL2",
110			 # SSL v3 method
111			 "SSL3_METHOD",
112			 # JPAKE
113			 "JPAKE",
114			 # NEXTPROTONEG
115			 "NEXTPROTONEG",
116			 # Deprecated functions
117			 "DEPRECATED",
118			 # Hide SSL internals
119			 "SSL_INTERN",
120			 # SCTP
121		 	 "SCTP",
122			 # SRTP
123			 "SRTP",
124			 # SSL TRACE
125		 	 "SSL_TRACE",
126			 # Unit testing
127			 "UNIT_TEST");
128
129my $options="";
130open(IN,"<Makefile") || die "unable to open Makefile!\n";
131while(<IN>) {
132    $options=$1 if (/^OPTIONS=(.*)$/);
133}
134close(IN);
135
136# The following ciphers may be excluded (by Configure). This means functions
137# defined with ifndef(NO_XXX) are not included in the .def file, and everything
138# in directory xxx is ignored.
139my $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
140my $no_cast; my $no_whirlpool; my $no_camellia; my $no_seed;
141my $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
142my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_aes; my $no_krb5;
143my $no_ec; my $no_ecdsa; my $no_ecdh; my $no_engine; my $no_hw;
144my $no_fp_api; my $no_static_engine=1; my $no_gmp; my $no_deprecated;
145my $no_rfc3779; my $no_psk; my $no_tlsext; my $no_cms; my $no_capieng;
146my $no_jpake; my $no_srp; my $no_ssl2; my $no_ec2m; my $no_nistp_gcc;
147my $no_nextprotoneg; my $no_sctp; my $no_srtp; my $no_ssl_trace;
148my $no_unit_test; my $no_ssl3_method;
149
150my $fips;
151
152my $zlib;
153
154
155foreach (@ARGV, split(/ /, $options))
156	{
157	$debug=1 if $_ eq "debug";
158	$W32=1 if $_ eq "32";
159	$W16=1 if $_ eq "16";
160	if($_ eq "NT") {
161		$W32 = 1;
162		$NT = 1;
163	}
164	if ($_ eq "VMS-VAX") {
165		$VMS=1;
166		$VMSVAX=1;
167	}
168	if ($_ eq "VMS-NonVAX") {
169		$VMS=1;
170		$VMSNonVAX=1;
171	}
172	$VMS=1 if $_ eq "VMS";
173	$OS2=1 if $_ eq "OS2";
174	$fips=1 if /^fips/;
175	if ($_ eq "zlib" || $_ eq "enable-zlib" || $_ eq "zlib-dynamic"
176			 || $_ eq "enable-zlib-dynamic") {
177		$zlib = 1;
178	}
179
180	$do_ssl=1 if $_ eq "ssleay";
181	if ($_ eq "ssl") {
182		$do_ssl=1;
183		$libname=$_
184	}
185	$do_crypto=1 if $_ eq "libeay";
186	if ($_ eq "crypto") {
187		$do_crypto=1;
188		$libname=$_;
189	}
190	$no_static_engine=1 if $_ eq "no-static-engine";
191	$no_static_engine=0 if $_ eq "enable-static-engine";
192	$do_update=1 if $_ eq "update";
193	$do_rewrite=1 if $_ eq "rewrite";
194	$do_ctest=1 if $_ eq "ctest";
195	$do_ctestall=1 if $_ eq "ctestall";
196	$do_checkexist=1 if $_ eq "exist";
197	#$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
198
199	if    (/^no-rc2$/)      { $no_rc2=1; }
200	elsif (/^no-rc4$/)      { $no_rc4=1; }
201	elsif (/^no-rc5$/)      { $no_rc5=1; }
202	elsif (/^no-idea$/)     { $no_idea=1; }
203	elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
204	elsif (/^no-bf$/)       { $no_bf=1; }
205	elsif (/^no-cast$/)     { $no_cast=1; }
206	elsif (/^no-whirlpool$/)     { $no_whirlpool=1; }
207	elsif (/^no-md2$/)      { $no_md2=1; }
208	elsif (/^no-md4$/)      { $no_md4=1; }
209	elsif (/^no-md5$/)      { $no_md5=1; }
210	elsif (/^no-sha$/)      { $no_sha=1; }
211	elsif (/^no-ripemd$/)   { $no_ripemd=1; }
212	elsif (/^no-mdc2$/)     { $no_mdc2=1; }
213	elsif (/^no-rsa$/)      { $no_rsa=1; }
214	elsif (/^no-dsa$/)      { $no_dsa=1; }
215	elsif (/^no-dh$/)       { $no_dh=1; }
216	elsif (/^no-ec$/)       { $no_ec=1; }
217	elsif (/^no-ecdsa$/)	{ $no_ecdsa=1; }
218	elsif (/^no-ecdh$/) 	{ $no_ecdh=1; }
219	elsif (/^no-hmac$/)	{ $no_hmac=1; }
220	elsif (/^no-aes$/)	{ $no_aes=1; }
221	elsif (/^no-camellia$/)	{ $no_camellia=1; }
222	elsif (/^no-seed$/)     { $no_seed=1; }
223	elsif (/^no-evp$/)	{ $no_evp=1; }
224	elsif (/^no-lhash$/)	{ $no_lhash=1; }
225	elsif (/^no-stack$/)	{ $no_stack=1; }
226	elsif (/^no-err$/)	{ $no_err=1; }
227	elsif (/^no-buffer$/)	{ $no_buffer=1; }
228	elsif (/^no-bio$/)	{ $no_bio=1; }
229	#elsif (/^no-locking$/)	{ $no_locking=1; }
230	elsif (/^no-comp$/)	{ $no_comp=1; }
231	elsif (/^no-dso$/)	{ $no_dso=1; }
232	elsif (/^no-krb5$/)	{ $no_krb5=1; }
233	elsif (/^no-engine$/)	{ $no_engine=1; }
234	elsif (/^no-hw$/)	{ $no_hw=1; }
235	elsif (/^no-gmp$/)	{ $no_gmp=1; }
236	elsif (/^no-rfc3779$/)	{ $no_rfc3779=1; }
237	elsif (/^no-tlsext$/)	{ $no_tlsext=1; }
238	elsif (/^no-cms$/)	{ $no_cms=1; }
239	elsif (/^no-ec2m$/)	{ $no_ec2m=1; }
240	elsif (/^no-ec_nistp_64_gcc_128$/)	{ $no_nistp_gcc=1; }
241	elsif (/^no-nextprotoneg$/)	{ $no_nextprotoneg=1; }
242	elsif (/^no-ssl2$/)	{ $no_ssl2=1; }
243	elsif (/^no-ssl3-method$/) { $no_ssl3_method=1; }
244	elsif (/^no-ssl-trace$/) { $no_ssl_trace=1; }
245	elsif (/^no-capieng$/)	{ $no_capieng=1; }
246	elsif (/^no-jpake$/)	{ $no_jpake=1; }
247	elsif (/^no-srp$/)	{ $no_srp=1; }
248	elsif (/^no-sctp$/)	{ $no_sctp=1; }
249	elsif (/^no-srtp$/)	{ $no_srtp=1; }
250	elsif (/^no-unit-test$/){ $no_unit_test=1; }
251	}
252
253
254if (!$libname) {
255	if ($do_ssl) {
256		$libname="SSLEAY";
257	}
258	if ($do_crypto) {
259		$libname="LIBEAY";
260	}
261}
262
263# If no platform is given, assume WIN32
264if ($W32 + $W16 + $VMS + $OS2 == 0) {
265	$W32 = 1;
266}
267
268# Add extra knowledge
269if ($W16) {
270	$no_fp_api=1;
271}
272
273if (!$do_ssl && !$do_crypto)
274	{
275	print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 ]\n";
276	exit(1);
277	}
278
279%ssl_list=&load_numbers($ssl_num);
280$max_ssl = $max_num;
281%crypto_list=&load_numbers($crypto_num);
282$max_crypto = $max_num;
283
284my $ssl="ssl/ssl.h";
285$ssl.=" ssl/kssl.h";
286$ssl.=" ssl/tls1.h";
287$ssl.=" ssl/srtp.h";
288
289my $crypto ="crypto/crypto.h";
290$crypto.=" crypto/cryptlib.h";
291$crypto.=" crypto/o_dir.h";
292$crypto.=" crypto/o_str.h";
293$crypto.=" crypto/o_time.h";
294$crypto.=" crypto/des/des.h crypto/des/des_old.h" ; # unless $no_des;
295$crypto.=" crypto/idea/idea.h" ; # unless $no_idea;
296$crypto.=" crypto/rc4/rc4.h" ; # unless $no_rc4;
297$crypto.=" crypto/rc5/rc5.h" ; # unless $no_rc5;
298$crypto.=" crypto/rc2/rc2.h" ; # unless $no_rc2;
299$crypto.=" crypto/bf/blowfish.h" ; # unless $no_bf;
300$crypto.=" crypto/cast/cast.h" ; # unless $no_cast;
301$crypto.=" crypto/whrlpool/whrlpool.h" ;
302$crypto.=" crypto/md2/md2.h" ; # unless $no_md2;
303$crypto.=" crypto/md4/md4.h" ; # unless $no_md4;
304$crypto.=" crypto/md5/md5.h" ; # unless $no_md5;
305$crypto.=" crypto/mdc2/mdc2.h" ; # unless $no_mdc2;
306$crypto.=" crypto/sha/sha.h" ; # unless $no_sha;
307$crypto.=" crypto/ripemd/ripemd.h" ; # unless $no_ripemd;
308$crypto.=" crypto/aes/aes.h" ; # unless $no_aes;
309$crypto.=" crypto/camellia/camellia.h" ; # unless $no_camellia;
310$crypto.=" crypto/seed/seed.h"; # unless $no_seed;
311
312$crypto.=" crypto/bn/bn.h";
313$crypto.=" crypto/rsa/rsa.h" ; # unless $no_rsa;
314$crypto.=" crypto/dsa/dsa.h" ; # unless $no_dsa;
315$crypto.=" crypto/dh/dh.h" ; # unless $no_dh;
316$crypto.=" crypto/ec/ec.h" ; # unless $no_ec;
317$crypto.=" crypto/ecdsa/ecdsa.h" ; # unless $no_ecdsa;
318$crypto.=" crypto/ecdh/ecdh.h" ; # unless $no_ecdh;
319$crypto.=" crypto/hmac/hmac.h" ; # unless $no_hmac;
320$crypto.=" crypto/cmac/cmac.h" ; # unless $no_hmac;
321
322$crypto.=" crypto/engine/engine.h"; # unless $no_engine;
323$crypto.=" crypto/stack/stack.h" ; # unless $no_stack;
324$crypto.=" crypto/buffer/buffer.h" ; # unless $no_buffer;
325$crypto.=" crypto/bio/bio.h" ; # unless $no_bio;
326$crypto.=" crypto/dso/dso.h" ; # unless $no_dso;
327$crypto.=" crypto/lhash/lhash.h" ; # unless $no_lhash;
328$crypto.=" crypto/conf/conf.h";
329$crypto.=" crypto/txt_db/txt_db.h";
330
331$crypto.=" crypto/evp/evp.h" ; # unless $no_evp;
332$crypto.=" crypto/objects/objects.h";
333$crypto.=" crypto/pem/pem.h";
334#$crypto.=" crypto/meth/meth.h";
335$crypto.=" crypto/asn1/asn1.h";
336$crypto.=" crypto/asn1/asn1t.h";
337$crypto.=" crypto/asn1/asn1_mac.h";
338$crypto.=" crypto/err/err.h" ; # unless $no_err;
339$crypto.=" crypto/pkcs7/pkcs7.h";
340$crypto.=" crypto/pkcs12/pkcs12.h";
341$crypto.=" crypto/x509/x509.h";
342$crypto.=" crypto/x509/x509_vfy.h";
343$crypto.=" crypto/x509v3/x509v3.h";
344$crypto.=" crypto/ts/ts.h";
345$crypto.=" crypto/rand/rand.h";
346$crypto.=" crypto/comp/comp.h" ; # unless $no_comp;
347$crypto.=" crypto/ocsp/ocsp.h";
348$crypto.=" crypto/ui/ui.h crypto/ui/ui_compat.h";
349$crypto.=" crypto/krb5/krb5_asn.h";
350#$crypto.=" crypto/store/store.h";
351$crypto.=" crypto/pqueue/pqueue.h";
352$crypto.=" crypto/cms/cms.h";
353$crypto.=" crypto/jpake/jpake.h";
354$crypto.=" crypto/modes/modes.h";
355$crypto.=" crypto/srp/srp.h";
356
357my $symhacks="crypto/symhacks.h";
358
359my @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
360my @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
361
362if ($do_update) {
363
364if ($do_ssl == 1) {
365
366	&maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
367	if ($do_rewrite == 1) {
368		open(OUT, ">$ssl_num");
369		&rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
370	} else {
371		open(OUT, ">>$ssl_num");
372	}
373	&update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
374	close OUT;
375}
376
377if($do_crypto == 1) {
378
379	&maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
380	if ($do_rewrite == 1) {
381		open(OUT, ">$crypto_num");
382		&rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
383	} else {
384		open(OUT, ">>$crypto_num");
385	}
386	&update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
387	close OUT;
388}
389
390} elsif ($do_checkexist) {
391	&check_existing(*ssl_list, @ssl_symbols)
392		if $do_ssl == 1;
393	&check_existing(*crypto_list, @crypto_symbols)
394		if $do_crypto == 1;
395} elsif ($do_ctest || $do_ctestall) {
396
397	print <<"EOF";
398
399/* Test file to check all DEF file symbols are present by trying
400 * to link to all of them. This is *not* intended to be run!
401 */
402
403int main()
404{
405EOF
406	&print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
407		if $do_ssl == 1;
408
409	&print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
410		if $do_crypto == 1;
411
412	print "}\n";
413
414} else {
415
416	&print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols)
417		if $do_ssl == 1;
418
419	&print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols)
420		if $do_crypto == 1;
421
422}
423
424
425sub do_defs
426{
427	my($name,$files,$symhacksfile)=@_;
428	my $file;
429	my @ret;
430	my %syms;
431	my %platform;		# For anything undefined, we assume ""
432	my %kind;		# For anything undefined, we assume "FUNCTION"
433	my %algorithm;		# For anything undefined, we assume ""
434	my %variant;
435	my %variant_cnt;	# To be able to allocate "name{n}" if "name"
436				# is the same name as the original.
437	my $cpp;
438	my %unknown_algorithms = ();
439
440	foreach $file (split(/\s+/,$symhacksfile." ".$files))
441		{
442		print STDERR "DEBUG: starting on $file:\n" if $debug;
443		open(IN,"<$file") || die "unable to open $file:$!\n";
444		my $line = "", my $def= "";
445		my %tag = (
446			(map { $_ => 0 } @known_platforms),
447			(map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms),
448			(map { "OPENSSL_NO_".$_ => 0 } @known_algorithms),
449			NOPROTO		=> 0,
450			PERL5		=> 0,
451			_WINDLL		=> 0,
452			CONST_STRICT	=> 0,
453			TRUE		=> 1,
454		);
455		my $symhacking = $file eq $symhacksfile;
456		my @current_platforms = ();
457		my @current_algorithms = ();
458
459		# params: symbol, alias, platforms, kind
460		# The reason to put this subroutine in a variable is that
461		# it will otherwise create it's own, unshared, version of
462		# %tag and %variant...
463		my $make_variant = sub
464		{
465			my ($s, $a, $p, $k) = @_;
466			my ($a1, $a2);
467
468			print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug;
469			if (defined($p))
470			{
471				$a1 = join(",",$p,
472					   grep(!/^$/,
473						map { $tag{$_} == 1 ? $_ : "" }
474						@known_platforms));
475			}
476			else
477			{
478				$a1 = join(",",
479					   grep(!/^$/,
480						map { $tag{$_} == 1 ? $_ : "" }
481						@known_platforms));
482			}
483			$a2 = join(",",
484				   grep(!/^$/,
485					map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" }
486					@known_ossl_platforms));
487			print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug;
488			if ($a1 eq "") { $a1 = $a2; }
489			elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; }
490			if ($a eq $s)
491			{
492				if (!defined($variant_cnt{$s}))
493				{
494					$variant_cnt{$s} = 0;
495				}
496				$variant_cnt{$s}++;
497				$a .= "{$variant_cnt{$s}}";
498			}
499			my $toadd = $a.":".$a1.(defined($k)?":".$k:"");
500			my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:"");
501			if (!grep(/^$togrep$/,
502				  split(/;/, defined($variant{$s})?$variant{$s}:""))) {
503				if (defined($variant{$s})) { $variant{$s} .= ";"; }
504				$variant{$s} .= $toadd;
505			}
506			print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug;
507		};
508
509		print STDERR "DEBUG: parsing ----------\n" if $debug;
510		while(<IN>) {
511			if (/\/\* Error codes for the \w+ functions\. \*\//)
512				{
513				undef @tag;
514				last;
515				}
516			if ($line ne '') {
517				$_ = $line . $_;
518				$line = '';
519			}
520
521			if (/\\$/) {
522				chomp; # remove eol
523				chop; # remove ending backslash
524				$line = $_;
525				next;
526			}
527
528			if(/\/\*/) {
529				if (not /\*\//) {	# multiline comment...
530					$line = $_;	# ... just accumulate
531					next;
532				} else {
533					s/\/\*.*?\*\///gs;# wipe it
534				}
535			}
536
537			if ($cpp) {
538				$cpp++ if /^#\s*if/;
539				$cpp-- if /^#\s*endif/;
540				next;
541	    		}
542			$cpp = 1 if /^#.*ifdef.*cplusplus/;
543
544			s/{[^{}]*}//gs;                      # ignore {} blocks
545			print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne "";
546			print STDERR "DEBUG: \$_=\"$_\"\n" if $debug;
547			if (/^\#\s*ifndef\s+(.*)/) {
548				push(@tag,"-");
549				push(@tag,$1);
550				$tag{$1}=-1;
551				print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
552			} elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
553				push(@tag,"-");
554				if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) {
555					my $tmp_1 = $1;
556					my $tmp_;
557					foreach $tmp_ (split '\&\&',$tmp_1) {
558						$tmp_ =~ /!defined\(([^\)]+)\)/;
559						print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
560						push(@tag,$1);
561						$tag{$1}=-1;
562					}
563				} else {
564					print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O...
565					print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug;
566					push(@tag,$1);
567					$tag{$1}=-1;
568				}
569			} elsif (/^\#\s*ifdef\s+(\S*)/) {
570				push(@tag,"-");
571				push(@tag,$1);
572				$tag{$1}=1;
573				print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
574			} elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
575				push(@tag,"-");
576				if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) {
577					my $tmp_1 = $1;
578					my $tmp_;
579					foreach $tmp_ (split '\|\|',$tmp_1) {
580						$tmp_ =~ /defined\(([^\)]+)\)/;
581						print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
582						push(@tag,$1);
583						$tag{$1}=1;
584					}
585				} else {
586					print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O...
587					print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug;
588					push(@tag,$1);
589					$tag{$1}=1;
590				}
591			} elsif (/^\#\s*error\s+(\w+) is disabled\./) {
592				my $tag_i = $#tag;
593				while($tag[$tag_i] ne "-") {
594					if ($tag[$tag_i] eq "OPENSSL_NO_".$1) {
595						$tag{$tag[$tag_i]}=2;
596						print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug;
597					}
598					$tag_i--;
599				}
600			} elsif (/^\#\s*endif/) {
601				my $tag_i = $#tag;
602				while($tag_i > 0 && $tag[$tag_i] ne "-") {
603					my $t=$tag[$tag_i];
604					print STDERR "DEBUG: \$t=\"$t\"\n" if $debug;
605					if ($tag{$t}==2) {
606						$tag{$t}=-1;
607					} else {
608						$tag{$t}=0;
609					}
610					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
611					pop(@tag);
612					if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) {
613						$t=$1;
614					} else {
615						$t="";
616					}
617					if ($t ne ""
618					    && !grep(/^$t$/, @known_algorithms)) {
619						$unknown_algorithms{$t} = 1;
620						#print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug;
621					}
622					$tag_i--;
623				}
624				pop(@tag);
625			} elsif (/^\#\s*else/) {
626				my $tag_i = $#tag;
627				while($tag[$tag_i] ne "-") {
628					my $t=$tag[$tag_i];
629					$tag{$t}= -$tag{$t};
630					print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug;
631					$tag_i--;
632				}
633			} elsif (/^\#\s*if\s+1/) {
634				push(@tag,"-");
635				# Dummy tag
636				push(@tag,"TRUE");
637				$tag{"TRUE"}=1;
638				print STDERR "DEBUG: $file: found 1\n" if $debug;
639			} elsif (/^\#\s*if\s+0/) {
640				push(@tag,"-");
641				# Dummy tag
642				push(@tag,"TRUE");
643				$tag{"TRUE"}=-1;
644				print STDERR "DEBUG: $file: found 0\n" if $debug;
645			} elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
646				 && $symhacking && $tag{'TRUE'} != -1) {
647				# This is for aliasing.  When we find an alias,
648				# we have to invert
649				&$make_variant($1,$2);
650				print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug;
651			}
652			if (/^\#/) {
653				@current_platforms =
654				    grep(!/^$/,
655					 map { $tag{$_} == 1 ? $_ :
656						   $tag{$_} == -1 ? "!".$_  : "" }
657					 @known_platforms);
658				push @current_platforms
659				    , grep(!/^$/,
660					   map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ :
661						     $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_  : "" }
662					   @known_ossl_platforms);
663				@current_algorithms =
664				    grep(!/^$/,
665					 map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" }
666					 @known_algorithms);
667				$def .=
668				    "#INFO:"
669					.join(',',@current_platforms).":"
670					    .join(',',@current_algorithms).";";
671				next;
672			}
673			if ($tag{'TRUE'} != -1) {
674				if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
675					next;
676				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
677					$def .= "int d2i_$3(void);";
678					$def .= "int i2d_$3(void);";
679					# Variant for platforms that do not
680					# have to access globale variables
681					# in shared libraries through functions
682					$def .=
683					    "#INFO:"
684						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
685						    .join(',',@current_algorithms).";";
686					$def .= "OPENSSL_EXTERN int $2_it;";
687					$def .=
688					    "#INFO:"
689						.join(',',@current_platforms).":"
690						    .join(',',@current_algorithms).";";
691					# Variant for platforms that have to
692					# access globale variables in shared
693					# libraries through functions
694					&$make_variant("$2_it","$2_it",
695						      "EXPORT_VAR_AS_FUNCTION",
696						      "FUNCTION");
697					next;
698				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
699					$def .= "int d2i_$3(void);";
700					$def .= "int i2d_$3(void);";
701					$def .= "int $3_free(void);";
702					$def .= "int $3_new(void);";
703					# Variant for platforms that do not
704					# have to access globale variables
705					# in shared libraries through functions
706					$def .=
707					    "#INFO:"
708						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
709						    .join(',',@current_algorithms).";";
710					$def .= "OPENSSL_EXTERN int $2_it;";
711					$def .=
712					    "#INFO:"
713						.join(',',@current_platforms).":"
714						    .join(',',@current_algorithms).";";
715					# Variant for platforms that have to
716					# access globale variables in shared
717					# libraries through functions
718					&$make_variant("$2_it","$2_it",
719						      "EXPORT_VAR_AS_FUNCTION",
720						      "FUNCTION");
721					next;
722				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
723					 /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
724					$def .= "int d2i_$1(void);";
725					$def .= "int i2d_$1(void);";
726					$def .= "int $1_free(void);";
727					$def .= "int $1_new(void);";
728					# Variant for platforms that do not
729					# have to access globale variables
730					# in shared libraries through functions
731					$def .=
732					    "#INFO:"
733						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
734						    .join(',',@current_algorithms).";";
735					$def .= "OPENSSL_EXTERN int $1_it;";
736					$def .=
737					    "#INFO:"
738						.join(',',@current_platforms).":"
739						    .join(',',@current_algorithms).";";
740					# Variant for platforms that have to
741					# access globale variables in shared
742					# libraries through functions
743					&$make_variant("$1_it","$1_it",
744						      "EXPORT_VAR_AS_FUNCTION",
745						      "FUNCTION");
746					next;
747				} elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
748					$def .= "int d2i_$2(void);";
749					$def .= "int i2d_$2(void);";
750					# Variant for platforms that do not
751					# have to access globale variables
752					# in shared libraries through functions
753					$def .=
754					    "#INFO:"
755						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
756						    .join(',',@current_algorithms).";";
757					$def .= "OPENSSL_EXTERN int $2_it;";
758					$def .=
759					    "#INFO:"
760						.join(',',@current_platforms).":"
761						    .join(',',@current_algorithms).";";
762					# Variant for platforms that have to
763					# access globale variables in shared
764					# libraries through functions
765					&$make_variant("$2_it","$2_it",
766						      "EXPORT_VAR_AS_FUNCTION",
767						      "FUNCTION");
768					next;
769				} elsif (/^\s*DECLARE_ASN1_ALLOC_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
770					$def .= "int $1_free(void);";
771					$def .= "int $1_new(void);";
772					next;
773				} elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
774					$def .= "int d2i_$2(void);";
775					$def .= "int i2d_$2(void);";
776					$def .= "int $2_free(void);";
777					$def .= "int $2_new(void);";
778					# Variant for platforms that do not
779					# have to access globale variables
780					# in shared libraries through functions
781					$def .=
782					    "#INFO:"
783						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
784						    .join(',',@current_algorithms).";";
785					$def .= "OPENSSL_EXTERN int $2_it;";
786					$def .=
787					    "#INFO:"
788						.join(',',@current_platforms).":"
789						    .join(',',@current_algorithms).";";
790					# Variant for platforms that have to
791					# access globale variables in shared
792					# libraries through functions
793					&$make_variant("$2_it","$2_it",
794						      "EXPORT_VAR_AS_FUNCTION",
795						      "FUNCTION");
796					next;
797				} elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) {
798					# Variant for platforms that do not
799					# have to access globale variables
800					# in shared libraries through functions
801					$def .=
802					    "#INFO:"
803						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
804						    .join(',',@current_algorithms).";";
805					$def .= "OPENSSL_EXTERN int $1_it;";
806					$def .=
807					    "#INFO:"
808						.join(',',@current_platforms).":"
809						    .join(',',@current_algorithms).";";
810					# Variant for platforms that have to
811					# access globale variables in shared
812					# libraries through functions
813					&$make_variant("$1_it","$1_it",
814						      "EXPORT_VAR_AS_FUNCTION",
815						      "FUNCTION");
816					next;
817				} elsif (/^\s*DECLARE_ASN1_NDEF_FUNCTION\s*\(\s*(\w*)\s*\)/) {
818					$def .= "int i2d_$1_NDEF(void);";
819				} elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
820					next;
821				} elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION\s*\(\s*(\w*)\s*\)/) {
822					$def .= "int $1_print_ctx(void);";
823					next;
824				} elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
825					$def .= "int $2_print_ctx(void);";
826					next;
827				} elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
828					next;
829				} elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
830					 /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ||
831					 /^DECLARE_PEM_rw_const\s*\(\s*(\w*)\s*,/ ) {
832					# Things not in Win16
833					$def .=
834					    "#INFO:"
835						.join(',',"!WIN16",@current_platforms).":"
836						    .join(',',@current_algorithms).";";
837					$def .= "int PEM_read_$1(void);";
838					$def .= "int PEM_write_$1(void);";
839					$def .=
840					    "#INFO:"
841						.join(',',@current_platforms).":"
842						    .join(',',@current_algorithms).";";
843					# Things that are everywhere
844					$def .= "int PEM_read_bio_$1(void);";
845					$def .= "int PEM_write_bio_$1(void);";
846					next;
847				} elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
848					/^DECLARE_PEM_write_const\s*\(\s*(\w*)\s*,/ ||
849					 /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
850					# Things not in Win16
851					$def .=
852					    "#INFO:"
853						.join(',',"!WIN16",@current_platforms).":"
854						    .join(',',@current_algorithms).";";
855					$def .= "int PEM_write_$1(void);";
856					$def .=
857					    "#INFO:"
858						.join(',',@current_platforms).":"
859						    .join(',',@current_algorithms).";";
860					# Things that are everywhere
861					$def .= "int PEM_write_bio_$1(void);";
862					next;
863				} elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
864					 /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
865					# Things not in Win16
866					$def .=
867					    "#INFO:"
868						.join(',',"!WIN16",@current_platforms).":"
869						    .join(',',@current_algorithms).";";
870					$def .= "int PEM_read_$1(void);";
871					$def .=
872					    "#INFO:"
873						.join(',',@current_platforms).":"
874						    .join(',',@current_algorithms).";";
875					# Things that are everywhere
876					$def .= "int PEM_read_bio_$1(void);";
877					next;
878				} elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
879					# Variant for platforms that do not
880					# have to access globale variables
881					# in shared libraries through functions
882					$def .=
883					    "#INFO:"
884						.join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":"
885						    .join(',',@current_algorithms).";";
886					$def .= "OPENSSL_EXTERN int _shadow_$2;";
887					$def .=
888					    "#INFO:"
889						.join(',',@current_platforms).":"
890						    .join(',',@current_algorithms).";";
891					# Variant for platforms that have to
892					# access globale variables in shared
893					# libraries through functions
894					&$make_variant("_shadow_$2","_shadow_$2",
895						      "EXPORT_VAR_AS_FUNCTION",
896						      "FUNCTION");
897				} elsif ($tag{'CONST_STRICT'} != 1) {
898					if (/\{|\/\*|\([^\)]*$/) {
899						$line = $_;
900					} else {
901						$def .= $_;
902					}
903				}
904			}
905		}
906		close(IN);
907
908		my $algs;
909		my $plays;
910
911		print STDERR "DEBUG: postprocessing ----------\n" if $debug;
912		foreach (split /;/, $def) {
913			my $s; my $k = "FUNCTION"; my $p; my $a;
914			s/^[\n\s]*//g;
915			s/[\n\s]*$//g;
916			next if(/\#undef/);
917			next if(/typedef\W/);
918			next if(/\#define/);
919
920			# Reduce argument lists to empty ()
921			# fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
922			while(/\(.*\)/s) {
923				s/\([^\(\)]+\)/\{\}/gs;
924				s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;	#(*f{}) -> f
925			}
926			# pretend as we didn't use curly braces: {} -> ()
927			s/\{\}/\(\)/gs;
928
929			s/STACK_OF\(\)/void/gs;
930			s/LHASH_OF\(\)/void/gs;
931
932			print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug;
933			if (/^\#INFO:([^:]*):(.*)$/) {
934				$plats = $1;
935				$algs = $2;
936				print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug;
937				next;
938			} elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) {
939				$s = $1;
940				$k = "VARIABLE";
941				print STDERR "DEBUG: found external variable $s\n" if $debug;
942			} elsif (/TYPEDEF_\w+_OF/s) {
943				next;
944			} elsif (/(\w+)\s*\(\).*/s) {	# first token prior [first] () is
945				$s = $1;		# a function name!
946				print STDERR "DEBUG: found function $s\n" if $debug;
947			} elsif (/\(/ and not (/=/)) {
948				print STDERR "File $file: cannot parse: $_;\n";
949				next;
950			} else {
951				next;
952			}
953
954			$syms{$s} = 1;
955			$kind{$s} = $k;
956
957			$p = $plats;
958			$a = $algs;
959			$a .= ",BF" if($s =~ /EVP_bf/);
960			$a .= ",CAST" if($s =~ /EVP_cast/);
961			$a .= ",DES" if($s =~ /EVP_des/);
962			$a .= ",DSA" if($s =~ /EVP_dss/);
963			$a .= ",IDEA" if($s =~ /EVP_idea/);
964			$a .= ",MD2" if($s =~ /EVP_md2/);
965			$a .= ",MD4" if($s =~ /EVP_md4/);
966			$a .= ",MD5" if($s =~ /EVP_md5/);
967			$a .= ",RC2" if($s =~ /EVP_rc2/);
968			$a .= ",RC4" if($s =~ /EVP_rc4/);
969			$a .= ",RC5" if($s =~ /EVP_rc5/);
970			$a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
971			$a .= ",SHA" if($s =~ /EVP_sha/);
972			$a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
973			$a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
974			$a .= ",RSA" if($s =~ /RSAPrivateKey/);
975			$a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
976
977			$platform{$s} =
978			    &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p);
979			$algorithm{$s} .= ','.$a;
980
981			if (defined($variant{$s})) {
982				foreach $v (split /;/,$variant{$s}) {
983					(my $r, my $p, my $k) = split(/:/,$v);
984					my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p);
985					$syms{$r} = 1;
986					if (!defined($k)) { $k = $kind{$s}; }
987					$kind{$r} = $k."(".$s.")";
988					$algorithm{$r} = $algorithm{$s};
989					$platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p);
990					$platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip);
991					print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug;
992				}
993			}
994			print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug;
995		}
996	}
997
998	# Prune the returned symbols
999
1000        delete $syms{"bn_dump1"};
1001	$platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
1002
1003	$platform{"PEM_read_NS_CERT_SEQ"} = "VMS";
1004	$platform{"PEM_write_NS_CERT_SEQ"} = "VMS";
1005	$platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS";
1006	$platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS";
1007	$platform{"EVP_sha384"} = "!VMSVAX";
1008	$platform{"EVP_sha512"} = "!VMSVAX";
1009	$platform{"SHA384_Init"} = "!VMSVAX";
1010	$platform{"SHA384_Transform"} = "!VMSVAX";
1011	$platform{"SHA384_Update"} = "!VMSVAX";
1012	$platform{"SHA384_Final"} = "!VMSVAX";
1013	$platform{"SHA384"} = "!VMSVAX";
1014	$platform{"SHA512_Init"} = "!VMSVAX";
1015	$platform{"SHA512_Transform"} = "!VMSVAX";
1016	$platform{"SHA512_Update"} = "!VMSVAX";
1017	$platform{"SHA512_Final"} = "!VMSVAX";
1018	$platform{"SHA512"} = "!VMSVAX";
1019	$platform{"WHIRLPOOL_Init"} = "!VMSVAX";
1020	$platform{"WHIRLPOOL"} = "!VMSVAX";
1021	$platform{"WHIRLPOOL_BitUpdate"} = "!VMSVAX";
1022	$platform{"EVP_whirlpool"} = "!VMSVAX";
1023	$platform{"WHIRLPOOL_Final"} = "!VMSVAX";
1024	$platform{"WHIRLPOOL_Update"} = "!VMSVAX";
1025
1026
1027	# Info we know about
1028
1029	push @ret, map { $_."\\".&info_string($_,"EXIST",
1030					      $platform{$_},
1031					      $kind{$_},
1032					      $algorithm{$_}) } keys %syms;
1033
1034	if (keys %unknown_algorithms) {
1035		print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
1036		print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
1037	}
1038	return(@ret);
1039}
1040
1041# Param: string of comma-separated platform-specs.
1042sub reduce_platforms
1043{
1044	my ($platforms) = @_;
1045	my $pl = defined($platforms) ? $platforms : "";
1046	my %p = map { $_ => 0 } split /,/, $pl;
1047	my $ret;
1048
1049	print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n"
1050	    if $debug;
1051	# We do this, because if there's code like the following, it really
1052	# means the function exists in all cases and should therefore be
1053	# everywhere.  By increasing and decreasing, we may attain 0:
1054	#
1055	# ifndef WIN16
1056	#    int foo();
1057	# else
1058	#    int _fat foo();
1059	# endif
1060	foreach $platform (split /,/, $pl) {
1061		if ($platform =~ /^!(.*)$/) {
1062			$p{$1}--;
1063		} else {
1064			$p{$platform}++;
1065		}
1066	}
1067	foreach $platform (keys %p) {
1068		if ($p{$platform} == 0) { delete $p{$platform}; }
1069	}
1070
1071	delete $p{""};
1072
1073	$ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p));
1074	print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n"
1075	    if $debug;
1076	return $ret;
1077}
1078
1079sub info_string {
1080	(my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
1081
1082	my %a = defined($algorithms) ?
1083	    map { $_ => 1 } split /,/, $algorithms : ();
1084	my $k = defined($kind) ? $kind : "FUNCTION";
1085	my $ret;
1086	my $p = &reduce_platforms($platforms);
1087
1088	delete $a{""};
1089
1090	$ret = $exist;
1091	$ret .= ":".$p;
1092	$ret .= ":".$k;
1093	$ret .= ":".join(',',sort keys %a);
1094	return $ret;
1095}
1096
1097sub maybe_add_info {
1098	(my $name, *nums, my @symbols) = @_;
1099	my $sym;
1100	my $new_info = 0;
1101	my %syms=();
1102
1103	print STDERR "Updating $name info\n";
1104	foreach $sym (@symbols) {
1105		(my $s, my $i) = split /\\/, $sym;
1106		if (defined($nums{$s})) {
1107			$i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
1108			(my $n, my $dummy) = split /\\/, $nums{$s};
1109			if (!defined($dummy) || $i ne $dummy) {
1110				$nums{$s} = $n."\\".$i;
1111				$new_info++;
1112				print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug;
1113			}
1114		}
1115		$syms{$s} = 1;
1116	}
1117
1118	my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
1119	foreach $sym (@s) {
1120		(my $n, my $i) = split /\\/, $nums{$sym};
1121		if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) {
1122			$new_info++;
1123			print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug;
1124		}
1125	}
1126	if ($new_info) {
1127		print STDERR "$new_info old symbols got an info update\n";
1128		if (!$do_rewrite) {
1129			print STDERR "You should do a rewrite to fix this.\n";
1130		}
1131	} else {
1132		print STDERR "No old symbols needed info update\n";
1133	}
1134}
1135
1136# Param: string of comma-separated keywords, each possibly prefixed with a "!"
1137sub is_valid
1138{
1139	my ($keywords_txt,$platforms) = @_;
1140	my (@keywords) = split /,/,$keywords_txt;
1141	my ($falsesum, $truesum) = (0, 1);
1142
1143	# Param: one keyword
1144	sub recognise
1145	{
1146		my ($keyword,$platforms) = @_;
1147
1148		if ($platforms) {
1149			# platforms
1150			if ($keyword eq "VMSVAX" && $VMSVAX) { return 1; }
1151			if ($keyword eq "VMSNonVAX" && $VMSNonVAX) { return 1; }
1152			if ($keyword eq "VMS" && $VMS) { return 1; }
1153			if ($keyword eq "WIN32" && $W32) { return 1; }
1154			if ($keyword eq "WIN16" && $W16) { return 1; }
1155			if ($keyword eq "WINNT" && $NT) { return 1; }
1156			if ($keyword eq "OS2" && $OS2) { return 1; }
1157			# Special platforms:
1158			# EXPORT_VAR_AS_FUNCTION means that global variables
1159			# will be represented as functions.  This currently
1160			# only happens on VMS-VAX.
1161			if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && ($VMSVAX || $W32 || $W16)) {
1162				return 1;
1163			}
1164			if ($keyword eq "OPENSSL_FIPS" && $fips) {
1165				return 1;
1166			}
1167			if ($keyword eq "ZLIB" && $zlib) { return 1; }
1168			return 0;
1169		} else {
1170			# algorithms
1171			if ($keyword eq "RC2" && $no_rc2) { return 0; }
1172			if ($keyword eq "RC4" && $no_rc4) { return 0; }
1173			if ($keyword eq "RC5" && $no_rc5) { return 0; }
1174			if ($keyword eq "IDEA" && $no_idea) { return 0; }
1175			if ($keyword eq "DES" && $no_des) { return 0; }
1176			if ($keyword eq "BF" && $no_bf) { return 0; }
1177			if ($keyword eq "CAST" && $no_cast) { return 0; }
1178			if ($keyword eq "MD2" && $no_md2) { return 0; }
1179			if ($keyword eq "MD4" && $no_md4) { return 0; }
1180			if ($keyword eq "MD5" && $no_md5) { return 0; }
1181			if ($keyword eq "SHA" && $no_sha) { return 0; }
1182			if ($keyword eq "RIPEMD" && $no_ripemd) { return 0; }
1183			if ($keyword eq "MDC2" && $no_mdc2) { return 0; }
1184			if ($keyword eq "WHIRLPOOL" && $no_whirlpool) { return 0; }
1185			if ($keyword eq "RSA" && $no_rsa) { return 0; }
1186			if ($keyword eq "DSA" && $no_dsa) { return 0; }
1187			if ($keyword eq "DH" && $no_dh) { return 0; }
1188			if ($keyword eq "EC" && $no_ec) { return 0; }
1189			if ($keyword eq "ECDSA" && $no_ecdsa) { return 0; }
1190			if ($keyword eq "ECDH" && $no_ecdh) { return 0; }
1191			if ($keyword eq "HMAC" && $no_hmac) { return 0; }
1192			if ($keyword eq "AES" && $no_aes) { return 0; }
1193			if ($keyword eq "CAMELLIA" && $no_camellia) { return 0; }
1194			if ($keyword eq "SEED" && $no_seed) { return 0; }
1195			if ($keyword eq "EVP" && $no_evp) { return 0; }
1196			if ($keyword eq "LHASH" && $no_lhash) { return 0; }
1197			if ($keyword eq "STACK" && $no_stack) { return 0; }
1198			if ($keyword eq "ERR" && $no_err) { return 0; }
1199			if ($keyword eq "BUFFER" && $no_buffer) { return 0; }
1200			if ($keyword eq "BIO" && $no_bio) { return 0; }
1201			if ($keyword eq "COMP" && $no_comp) { return 0; }
1202			if ($keyword eq "DSO" && $no_dso) { return 0; }
1203			if ($keyword eq "KRB5" && $no_krb5) { return 0; }
1204			if ($keyword eq "ENGINE" && $no_engine) { return 0; }
1205			if ($keyword eq "HW" && $no_hw) { return 0; }
1206			if ($keyword eq "FP_API" && $no_fp_api) { return 0; }
1207			if ($keyword eq "STATIC_ENGINE" && $no_static_engine) { return 0; }
1208			if ($keyword eq "GMP" && $no_gmp) { return 0; }
1209			if ($keyword eq "RFC3779" && $no_rfc3779) { return 0; }
1210			if ($keyword eq "TLSEXT" && $no_tlsext) { return 0; }
1211			if ($keyword eq "PSK" && $no_psk) { return 0; }
1212			if ($keyword eq "CMS" && $no_cms) { return 0; }
1213			if ($keyword eq "EC2M" && $no_ec2m) { return 0; }
1214			if ($keyword eq "NEXTPROTONEG" && $no_nextprotoneg) { return 0; }
1215			if ($keyword eq "EC_NISTP_64_GCC_128" && $no_nistp_gcc)
1216					{ return 0; }
1217			if ($keyword eq "SSL2" && $no_ssl2) { return 0; }
1218			if ($keyword eq "SSL3_METHOD" && $no_ssl3_method) { return 0; }
1219			if ($keyword eq "SSL_TRACE" && $no_ssl_trace) { return 0; }
1220			if ($keyword eq "CAPIENG" && $no_capieng) { return 0; }
1221			if ($keyword eq "JPAKE" && $no_jpake) { return 0; }
1222			if ($keyword eq "SRP" && $no_srp) { return 0; }
1223			if ($keyword eq "SCTP" && $no_sctp) { return 0; }
1224			if ($keyword eq "SRTP" && $no_srtp) { return 0; }
1225			if ($keyword eq "UNIT_TEST" && $no_unit_test) { return 0; }
1226			if ($keyword eq "DEPRECATED" && $no_deprecated) { return 0; }
1227
1228			# Nothing recognise as true
1229			return 1;
1230		}
1231	}
1232
1233	foreach $k (@keywords) {
1234		if ($k =~ /^!(.*)$/) {
1235			$falsesum += &recognise($1,$platforms);
1236		} else {
1237			$truesum *= &recognise($k,$platforms);
1238		}
1239	}
1240	print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug;
1241	return (!$falsesum) && $truesum;
1242}
1243
1244sub print_test_file
1245{
1246	(*OUT,my $name,*nums,my $testall,my @symbols)=@_;
1247	my $n = 1; my @e; my @r;
1248	my $sym; my $prev = ""; my $prefSSLeay;
1249
1250	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1251	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols);
1252	@symbols=((sort @e),(sort @r));
1253
1254	foreach $sym (@symbols) {
1255		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1256		my $v = 0;
1257		$v = 1 if $i=~ /^.*?:.*?:VARIABLE/;
1258		my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1259		my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1260		if (!defined($nums{$s})) {
1261			print STDERR "Warning: $s does not have a number assigned\n"
1262			    if(!$do_update);
1263		} elsif (is_valid($p,1) && is_valid($a,0)) {
1264			my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1265			if ($prev eq $s2) {
1266				print OUT "\t/* The following has already appeared previously */\n";
1267				print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1268			}
1269			$prev = $s2;	# To warn about duplicates...
1270
1271			($nn,$ni)=($nums{$s2} =~ /^(.*?)\\(.*)$/);
1272			if ($v) {
1273				print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n";
1274			} else {
1275				print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n";
1276			}
1277		}
1278	}
1279}
1280
1281sub get_version {
1282   local *MF;
1283   my $v = '?';
1284   open MF, 'Makefile' or return $v;
1285   while (<MF>) {
1286     $v = $1, last if /^VERSION=(.*?)\s*$/;
1287   }
1288   close MF;
1289   return $v;
1290}
1291
1292sub print_def_file
1293{
1294	(*OUT,my $name,*nums,my @symbols)=@_;
1295	my $n = 1; my @e; my @r; my @v; my $prev="";
1296	my $liboptions="";
1297	my $libname = $name;
1298	my $http_vendor = 'www.openssl.org/';
1299	my $version = get_version();
1300	my $what = "OpenSSL: implementation of Secure Socket Layer";
1301	my $description = "$what $version, $name - http://$http_vendor";
1302
1303	if ($W32)
1304		{ $libname.="32"; }
1305	elsif ($W16)
1306		{ $libname.="16"; }
1307	elsif ($OS2)
1308		{ # DLL names should not clash on the whole system.
1309		  # However, they should not have any particular relationship
1310		  # to the name of the static library.  Chose descriptive names
1311		  # (must be at most 8 chars).
1312		  my %translate = (ssl => 'open_ssl', crypto => 'cryptssl');
1313		  $libname = $translate{$name} || $name;
1314		  $liboptions = <<EOO;
1315INITINSTANCE
1316DATA MULTIPLE NONSHARED
1317EOO
1318		  # Vendor field can't contain colon, drat; so we omit http://
1319		  $description = "\@#$http_vendor:$version#\@$what; DLL for library $name.  Build for EMX -Zmtd";
1320		}
1321
1322	print OUT <<"EOF";
1323;
1324; Definition file for the DLL version of the $name library from OpenSSL
1325;
1326
1327LIBRARY         $libname	$liboptions
1328
1329EOF
1330
1331	if ($W16) {
1332		print <<"EOF";
1333CODE            PRELOAD MOVEABLE
1334DATA            PRELOAD MOVEABLE SINGLE
1335
1336EXETYPE		WINDOWS
1337
1338HEAPSIZE	4096
1339STACKSIZE	8192
1340
1341EOF
1342	}
1343
1344	print "EXPORTS\n";
1345
1346	(@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1347	(@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols);
1348	(@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols);
1349	@symbols=((sort @e),(sort @r), (sort @v));
1350
1351
1352	foreach $sym (@symbols) {
1353		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1354		my $v = 0;
1355		$v = 1 if $i =~ /^.*?:.*?:VARIABLE/;
1356		if (!defined($nums{$s})) {
1357			printf STDERR "Warning: $s does not have a number assigned\n"
1358			    if(!$do_update);
1359		} else {
1360			(my $n, my $dummy) = split /\\/, $nums{$s};
1361			my %pf = ();
1362			my $p = ($i =~ /^[^:]*:([^:]*):/,$1);
1363			my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1);
1364			if (is_valid($p,1) && is_valid($a,0)) {
1365				my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1);
1366				if ($prev eq $s2) {
1367					print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n";
1368				}
1369				$prev = $s2;	# To warn about duplicates...
1370				if($v && !$OS2) {
1371					printf OUT "    %s%-39s @%-8d DATA\n",($W32)?"":"_",$s2,$n;
1372				} else {
1373					printf OUT "    %s%-39s @%d\n",($W32||$OS2)?"":"_",$s2,$n;
1374				}
1375			}
1376		}
1377	}
1378	printf OUT "\n";
1379}
1380
1381sub load_numbers
1382{
1383	my($name)=@_;
1384	my(@a,%ret);
1385
1386	$max_num = 0;
1387	$num_noinfo = 0;
1388	$prev = "";
1389	$prev_cnt = 0;
1390
1391	open(IN,"<$name") || die "unable to open $name:$!\n";
1392	while (<IN>) {
1393		chop;
1394		s/#.*$//;
1395		next if /^\s*$/;
1396		@a=split;
1397		if (defined $ret{$a[0]}) {
1398			# This is actually perfectly OK
1399			#print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
1400		}
1401		if ($max_num > $a[1]) {
1402			print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
1403		}
1404		elsif ($max_num == $a[1]) {
1405			# This is actually perfectly OK
1406			#print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
1407			if ($a[0] eq $prev) {
1408				$prev_cnt++;
1409				$a[0] .= "{$prev_cnt}";
1410			}
1411		}
1412		else {
1413			$prev_cnt = 0;
1414		}
1415		if ($#a < 2) {
1416			# Existence will be proven later, in do_defs
1417			$ret{$a[0]}=$a[1];
1418			$num_noinfo++;
1419		} else {
1420			$ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
1421		}
1422		$max_num = $a[1] if $a[1] > $max_num;
1423		$prev=$a[0];
1424	}
1425	if ($num_noinfo) {
1426		print STDERR "Warning: $num_noinfo symbols were without info.";
1427		if ($do_rewrite) {
1428			printf STDERR "  The rewrite will fix this.\n";
1429		} else {
1430			printf STDERR "  You should do a rewrite to fix this.\n";
1431		}
1432	}
1433	close(IN);
1434	return(%ret);
1435}
1436
1437sub parse_number
1438{
1439	(my $str, my $what) = @_;
1440	(my $n, my $i) = split(/\\/,$str);
1441	if ($what eq "n") {
1442		return $n;
1443	} else {
1444		return $i;
1445	}
1446}
1447
1448sub rewrite_numbers
1449{
1450	(*OUT,$name,*nums,@symbols)=@_;
1451	my $thing;
1452
1453	print STDERR "Rewriting $name\n";
1454
1455	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1456	my $r; my %r; my %rsyms;
1457	foreach $r (@r) {
1458		(my $s, my $i) = split /\\/, $r;
1459		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1460		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1461		$r{$a} = $s."\\".$i;
1462		$rsyms{$s} = 1;
1463	}
1464
1465	my %syms = ();
1466	foreach $_ (@symbols) {
1467		(my $n, my $i) = split /\\/;
1468		$syms{$n} = 1;
1469	}
1470
1471	my @s=sort {
1472	    &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n")
1473	    || $a cmp $b
1474	} keys %nums;
1475	foreach $sym (@s) {
1476		(my $n, my $i) = split /\\/, $nums{$sym};
1477		next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
1478		next if defined($rsyms{$sym});
1479		print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug;
1480		$i="NOEXIST::FUNCTION:"
1481			if !defined($i) || $i eq "" || !defined($syms{$sym});
1482		my $s2 = $sym;
1483		$s2 =~ s/\{[0-9]+\}$//;
1484		printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
1485		if (exists $r{$sym}) {
1486			(my $s, $i) = split /\\/,$r{$sym};
1487			my $s2 = $s;
1488			$s2 =~ s/\{[0-9]+\}$//;
1489			printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i;
1490		}
1491	}
1492}
1493
1494sub update_numbers
1495{
1496	(*OUT,$name,*nums,my $start_num, my @symbols)=@_;
1497	my $new_syms = 0;
1498
1499	print STDERR "Updating $name numbers\n";
1500
1501	my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols);
1502	my $r; my %r; my %rsyms;
1503	foreach $r (@r) {
1504		(my $s, my $i) = split /\\/, $r;
1505		my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
1506		$i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
1507		$r{$a} = $s."\\".$i;
1508		$rsyms{$s} = 1;
1509	}
1510
1511	foreach $sym (@symbols) {
1512		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1513		next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
1514		next if defined($rsyms{$sym});
1515		die "ERROR: Symbol $sym had no info attached to it."
1516		    if $i eq "";
1517		if (!exists $nums{$s}) {
1518			$new_syms++;
1519			my $s2 = $s;
1520			$s2 =~ s/\{[0-9]+\}$//;
1521			printf OUT "%s%-39s %d\t%s\n","",$s2, ++$start_num,$i;
1522			if (exists $r{$s}) {
1523				($s, $i) = split /\\/,$r{$s};
1524				$s =~ s/\{[0-9]+\}$//;
1525				printf OUT "%s%-39s %d\t%s\n","",$s, $start_num,$i;
1526			}
1527		}
1528	}
1529	if($new_syms) {
1530		print STDERR "$new_syms New symbols added\n";
1531	} else {
1532		print STDERR "No New symbols Added\n";
1533	}
1534}
1535
1536sub check_existing
1537{
1538	(*nums, my @symbols)=@_;
1539	my %existing; my @remaining;
1540	@remaining=();
1541	foreach $sym (@symbols) {
1542		(my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1543		$existing{$s}=1;
1544	}
1545	foreach $sym (keys %nums) {
1546		if (!exists $existing{$sym}) {
1547			push @remaining, $sym;
1548		}
1549	}
1550	if(@remaining) {
1551		print STDERR "The following symbols do not seem to exist:\n";
1552		foreach $sym (@remaining) {
1553			print STDERR "\t",$sym,"\n";
1554		}
1555	}
1556}
1557
1558