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