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