mkdef.pl 12.4 KB
Newer Older
1
#!/usr/local/bin/perl -w
2 3 4 5
#
# generate a .def file
#
# It does this by parsing the header files and looking for the
6
# prototyped functions: it then prunes the output.
7 8
#

9 10
my $crypto_num="util/libeay.num";
my $ssl_num=   "util/ssleay.num";
11

12 13 14
my $do_update = 0;
my $do_crypto = 0;
my $do_ssl = 0;
15 16
my $do_ctest = 0;
my $rsaref = 0;
17

18 19
my $W32=1;
my $NT=0;
20
# Set this to make typesafe STACK definitions appear in DEF
21
my $safe_stack_def = 0;
22

23
my $options="";
24 25 26 27
open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
while(<IN>) {
    $options=$1 if (/^OPTIONS=(.*)$/);
}
U
Ulf Möller 已提交
28
close(IN);
29

30 31 32 33
# The following ciphers may be excluded (by Configure). This means functions
# defined with ifndef(NO_XXX) are not included in the .def file, and everything
# in directory xxx is ignored.
my $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
34 35
my $no_cast;
my $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
36 37
my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0;

38
foreach (@ARGV, split(/ /, $options))
39
	{
40 41 42 43 44 45
	$W32=1 if $_ eq "32";
	$W32=0 if $_ eq "16";
	if($_ eq "NT") {
		$W32 = 1;
		$NT = 1;
	}
46
	$do_ssl=1 if $_ eq "ssleay";
47
	$do_ssl=1 if $_ eq "ssl";
48
	$do_crypto=1 if $_ eq "libeay";
49 50
	$do_crypto=1 if $_ eq "crypto";
	$do_update=1 if $_ eq "update";
D
Dr. Stephen Henson 已提交
51
	$do_ctest=1 if $_ eq "ctest";
52
	$rsaref=1 if $_ eq "rsaref";
53
	#$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
U
Ulf Möller 已提交
54 55 56 57 58 59 60 61 62

	if    (/^no-rc2$/)      { $no_rc2=1; }
	elsif (/^no-rc4$/)      { $no_rc4=1; }
	elsif (/^no-rc5$/)      { $no_rc5=1; }
	elsif (/^no-idea$/)     { $no_idea=1; }
	elsif (/^no-des$/)      { $no_des=1; }
	elsif (/^no-bf$/)       { $no_bf=1; }
	elsif (/^no-cast$/)     { $no_cast=1; }
	elsif (/^no-md2$/)      { $no_md2=1; }
63
	elsif (/^no-md4$/)      { $no_md4=1; }
U
Ulf Möller 已提交
64 65 66 67 68 69 70 71
	elsif (/^no-md5$/)      { $no_md5=1; }
	elsif (/^no-sha$/)      { $no_sha=1; }
	elsif (/^no-ripemd$/)   { $no_ripemd=1; }
	elsif (/^no-mdc2$/)     { $no_mdc2=1; }
	elsif (/^no-rsa$/)      { $no_rsa=1; }
	elsif (/^no-dsa$/)      { $no_dsa=1; }
	elsif (/^no-dh$/)       { $no_dh=1; }
	elsif (/^no-hmac$/)	{ $no_hmac=1; }
72 73
	}

D
Dr. Stephen Henson 已提交
74

75 76
if (!$do_ssl && !$do_crypto)
	{
77
	print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT ] [rsaref]\n";
78 79 80 81
	exit(1);
	}

%ssl_list=&load_numbers($ssl_num);
82
$max_ssl = $max_num;
83
%crypto_list=&load_numbers($crypto_num);
84
$max_crypto = $max_num;
85

86
my $ssl="ssl/ssl.h";
87

88
my $crypto ="crypto/crypto.h";
U
Ulf Möller 已提交
89 90 91 92 93 94 95 96
$crypto.=" crypto/des/des.h" unless $no_des;
$crypto.=" crypto/idea/idea.h" unless $no_idea;
$crypto.=" crypto/rc4/rc4.h" unless $no_rc4;
$crypto.=" crypto/rc5/rc5.h" unless $no_rc5;
$crypto.=" crypto/rc2/rc2.h" unless $no_rc2;
$crypto.=" crypto/bf/blowfish.h" unless $no_bf;
$crypto.=" crypto/cast/cast.h" unless $no_cast;
$crypto.=" crypto/md2/md2.h" unless $no_md2;
97
$crypto.=" crypto/md4/md4.h" unless $no_md4;
U
Ulf Möller 已提交
98 99 100 101
$crypto.=" crypto/md5/md5.h" unless $no_md5;
$crypto.=" crypto/mdc2/mdc2.h" unless $no_mdc2;
$crypto.=" crypto/sha/sha.h" unless $no_sha;
$crypto.=" crypto/ripemd/ripemd.h" unless $no_ripemd;
102 103

$crypto.=" crypto/bn/bn.h";
U
Ulf Möller 已提交
104 105 106 107
$crypto.=" crypto/rsa/rsa.h" unless $no_rsa;
$crypto.=" crypto/dsa/dsa.h" unless $no_dsa;
$crypto.=" crypto/dh/dh.h" unless $no_dh;
$crypto.=" crypto/hmac/hmac.h" unless $no_hmac;
108 109 110 111

$crypto.=" crypto/stack/stack.h";
$crypto.=" crypto/buffer/buffer.h";
$crypto.=" crypto/bio/bio.h";
112
$crypto.=" crypto/dso/dso.h";
113 114 115 116 117 118 119 120 121 122 123 124
$crypto.=" crypto/lhash/lhash.h";
$crypto.=" crypto/conf/conf.h";
$crypto.=" crypto/txt_db/txt_db.h";

$crypto.=" crypto/evp/evp.h";
$crypto.=" crypto/objects/objects.h";
$crypto.=" crypto/pem/pem.h";
#$crypto.=" crypto/meth/meth.h";
$crypto.=" crypto/asn1/asn1.h";
$crypto.=" crypto/asn1/asn1_mac.h";
$crypto.=" crypto/err/err.h";
$crypto.=" crypto/pkcs7/pkcs7.h";
125
$crypto.=" crypto/pkcs12/pkcs12.h";
126 127
$crypto.=" crypto/x509/x509.h";
$crypto.=" crypto/x509/x509_vfy.h";
128
$crypto.=" crypto/x509v3/x509v3.h";
129
$crypto.=" crypto/rand/rand.h";
130 131
$crypto.=" crypto/comp/comp.h";
$crypto.=" crypto/tmdiff.h";
132

133 134
my @ssl_func = &do_defs("SSLEAY", $ssl);
my @crypto_func = &do_defs("LIBEAY", $crypto);
135

136

137 138 139 140 141 142 143 144 145 146 147 148
if ($do_update) {

if ($do_ssl == 1) {
	open(OUT, ">>$ssl_num");
	&update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl, @ssl_func);
	close OUT;
}

if($do_crypto == 1) {
	open(OUT, ">>$crypto_num");
	&update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto, @crypto_func);
	close OUT;
D
Dr. Stephen Henson 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
} 

} elsif ($do_ctest) {

	print <<"EOF";

/* Test file to check all DEF file symbols are present by trying
 * to link to all of them. This is *not* intended to be run!
 */

int main()
{
EOF
	&print_test_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_func)
		if $do_ssl == 1;

	&print_test_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_func)
		if $do_crypto == 1;

	print "}\n";
169 170

} else {
U
Ulf Möller 已提交
171 172

	&print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_func)
173 174
		if $do_ssl == 1;

U
Ulf Möller 已提交
175
	&print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_func)
176
		if $do_crypto == 1;
U
Ulf Möller 已提交
177

178
}
179 180 181


sub do_defs
182 183
{
	my($name,$files)=@_;
184
	my $file;
185 186
	my @ret;
	my %funcs;
187
	my $cpp;
188 189 190 191

	foreach $file (split(/\s+/,$files))
		{
		open(IN,"<$file") || die "unable to open $file:$!\n";
192
		my $line = "", my $def= "";
193 194 195 196 197 198 199 200 201
		my %tag = (
			FreeBSD		=> 0,
			NOPROTO		=> 0,
			WIN16		=> 0,
			PERL5		=> 0,
			_WINDLL		=> 0,
			NO_FP_API	=> 0,
			CONST_STRICT	=> 0,
			TRUE		=> 1,
202 203 204 205 206 207 208 209
			NO_RC2		=> 0,
			NO_RC4		=> 0,
			NO_RC5		=> 0,
			NO_IDEA		=> 0,
			NO_DES		=> 0,
			NO_BF		=> 0,
			NO_CAST		=> 0,
			NO_MD2		=> 0,
210
			NO_MD4		=> 0,
211 212 213 214 215 216 217 218
			NO_MD5		=> 0,
			NO_SHA		=> 0,
			NO_RIPEMD	=> 0,
			NO_MDC2		=> 0,
			NO_RSA		=> 0,
			NO_DSA		=> 0,
			NO_DH		=> 0,
			NO_HMAC		=> 0,
219 220 221 222 223 224
		);
		while(<IN>) {
			last if (/BEGIN ERROR CODES/);
			if ($line ne '') {
				$_ = $line . $_;
				$line = '';
225
			}
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240

			if (/\\$/) {
				$line = $_;
				next;
			}

	    		$cpp = 1 if /^#.*ifdef.*cplusplus/;
			if ($cpp) {
				$cpp = 0 if /^#.*endif/;
				next;
	    		}

			s/\/\*.*?\*\///gs;                   # ignore comments
			s/{[^{}]*}//gs;                      # ignore {} blocks
			if (/^\#\s*ifndef (.*)/) {
241 242 243
				push(@tag,$1);
				$tag{$1}=-1;
				next;
244
			} elsif (/^\#\s*if !defined\(([^\)]+)\)/) {
245 246 247
				push(@tag,$1);
				$tag{$1}=-1;
				next;
248
			} elsif (/^\#\s*ifdef (.*)/) {
249 250 251
				push(@tag,$1);
				$tag{$1}=1;
				next;
252
			} elsif (/^\#\s*if defined(.*)/) {
253 254 255
				push(@tag,$1);
				$tag{$1}=1;
				next;
256
			} elsif (/^\#\s*endif/) {
257 258 259
				$tag{$tag[$#tag]}=0;
				pop(@tag);
				next;
260 261
			} elsif (/^\#\s*else/) {
				my $t=$tag[$#tag];
262 263
				$tag{$t}= -$tag{$t};
				next;
264 265 266 267 268
			} elsif (/^\#\s*if\s+1/) {
				# Dummy tag
				push(@tag,"TRUE");
				$tag{"TRUE"}=1;
				next;
B
Bodo Möller 已提交
269 270 271 272 273
			} elsif (/^\#\s*if\s+0/) {
				# Dummy tag
				push(@tag,"TRUE");
				$tag{"TRUE"}=-1;
				next;
274 275 276
			} elsif (/^\#/) {
				next;
			}
D
 
Dr. Stephen Henson 已提交
277
			if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
278
				next;
D
 
Dr. Stephen Henson 已提交
279
			} elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
280
				next;
281 282
			} elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
				     /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
283 284 285 286 287 288 289 290 291
				if (!($no_rsa && ($1 eq "RSAPrivateKey" ||
						  $1 eq "RSAPublicKey" ||
						  $1 eq "RSA_PUBKEY"))) {
					if($W32) {
						$funcs{"PEM_read_${1}"} = 1;
						$funcs{"PEM_write_${1}"} = 1;
					}
					$funcs{"PEM_read_bio_${1}"} = 1;
					$funcs{"PEM_write_bio_${1}"} = 1;
292
				}
293 294
			} elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
				     /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
295 296 297 298 299 300 301
				if (!($no_rsa && ($1 eq "RSAPrivateKey" ||
						  $1 eq "RSAPublicKey" ||
						  $1 eq "RSA_PUBKEY"))) {
					if($W32) {
						$funcs{"PEM_write_${1}"} = 1;
					}
					$funcs{"PEM_write_bio_${1}"} = 1;
302 303 304 305 306 307 308
				}
			} elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
				     /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
				if($W32) {
					$funcs{"PEM_read_${1}"} = 1;
				}
				$funcs{"PEM_read_bio_${1}"} = 1;
309
			} elsif (
B
Bodo Möller 已提交
310
				($tag{'TRUE'} != -1) &&
311
				($tag{'FreeBSD'} != 1) &&
312
				($tag{'CONST_STRICT'} != 1) &&
313 314
				(($W32 && ($tag{'WIN16'} != 1)) ||
				 (!$W32 && ($tag{'WIN16'} != -1))) &&
315 316
				($tag{'PERL5'} != 1) &&
#				($tag{'_WINDLL'} != -1) &&
317 318 319
				((!$W32 && $tag{'_WINDLL'} != -1) ||
				 ($W32 && $tag{'_WINDLL'} != 1)) &&
				((($tag{'NO_FP_API'} != 1) && $W32) ||
320 321 322 323 324 325 326 327 328
				 (($tag{'NO_FP_API'} != -1) && !$W32)) &&
				($tag{'NO_RC2'} == 0  || !$no_rc2) &&
				($tag{'NO_RC4'} == 0  || !$no_rc4) &&
				($tag{'NO_RC5'} == 0  || !$no_rc5) &&
				($tag{'NO_IDEA'} == 0 || !$no_idea) &&
				($tag{'NO_DES'} == 0  || !$no_des) &&
				($tag{'NO_BF'} == 0   || !$no_bf) &&
				($tag{'NO_CAST'} == 0 || !$no_cast) &&
				($tag{'NO_MD2'} == 0  || !$no_md2) &&
329
				($tag{'NO_MD4'} == 0  || !$no_md4) &&
330 331 332 333 334 335 336 337
				($tag{'NO_MD5'} == 0  || !$no_md5) &&
				($tag{'NO_SHA'} == 0  || !$no_sha) &&
				($tag{'NO_RIPEMD'} == 0 || !$no_ripemd) &&
				($tag{'NO_MDC2'} == 0 || !$no_mdc2) &&
				($tag{'NO_RSA'} == 0  || !$no_rsa) &&
				($tag{'NO_DSA'} == 0  || !$no_dsa) &&
				($tag{'NO_DH'} == 0   || !$no_dh) &&
				($tag{'NO_HMAC'} == 0 || !$no_hmac))
338
				{
339 340 341 342 343
					if (/{|\/\*/) { # }
						$line = $_;
					} else {
						$def .= $_;
					}
344 345 346
				}
			}
		close(IN);
347 348 349 350

		foreach (split /;/, $def) {
			s/^[\n\s]*//g;
			s/[\n\s]*$//g;
351
			next if(/#define/);
352
			next if(/typedef\W/);
U
Ulf Möller 已提交
353 354 355 356 357 358
			next if(/EVP_bf/ and $no_bf);
			next if(/EVP_cast/ and $no_cast);
			next if(/EVP_des/ and $no_des);
			next if(/EVP_dss/ and $no_dsa);
			next if(/EVP_idea/ and $no_idea);
			next if(/EVP_md2/ and $no_md2);
359
			next if(/EVP_md4/ and $no_md4);
U
Ulf Möller 已提交
360 361 362 363 364 365
			next if(/EVP_md5/ and $no_md5);
			next if(/EVP_rc2/ and $no_rc2);
			next if(/EVP_rc4/ and $no_rc4);
			next if(/EVP_rc5/ and $no_rc5);
			next if(/EVP_ripemd/ and $no_ripemd);
			next if(/EVP_sha/ and $no_sha);
366 367 368 369 370
			next if(/EVP_(Open|Seal)(Final|Init)/ and $no_rsa);
			next if(/PEM_Seal(Final|Init|Update)/ and $no_rsa);
			next if(/RSAPrivateKey/ and $no_rsa);
			next if(/SSLv23?_((client|server)_)?method/ and $no_rsa);

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
			if (/\(\*(\w*)\([^\)]+/) {
				$funcs{$1} = 1;
			} elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
				# K&R C
				next;
			} elsif (/\w+\W+\w+\W*\(.*\)$/s) {
				while (not /\(\)$/s) {
					s/[^\(\)]*\)$/\)/s;
					s/\([^\(\)]*\)\)$/\)/s;
				}
				s/\(void\)//;
				/(\w+)\W*\(\)/s;
				$funcs{$1} = 1;
			} elsif (/\(/ and not (/=/)) {
				print STDERR "File $file: cannot parse: $_;\n";
			}
387 388 389
		}
	}

390 391 392 393
	# Prune the returned functions

        delete $funcs{"SSL_add_dir_cert_subjects_to_stack"};
        delete $funcs{"RSA_PKCS1_RSAref"} unless $rsaref;
394
        delete $funcs{"bn_dump1"};
395 396 397 398 399 400 401 402 403

	if($W32) {
		delete $funcs{"BIO_s_file_internal"};
		delete $funcs{"BIO_new_file_internal"};
		delete $funcs{"BIO_new_fp_internal"};
	} else {
		if(exists $funcs{"ERR_load_CRYPTO_strings"}) {
			delete $funcs{"ERR_load_CRYPTO_strings"};
			$funcs{"ERR_load_CRYPTOlib_strings"} = 1;
404
		}
405 406 407 408 409 410
		delete $funcs{"BIO_s_file"};
		delete $funcs{"BIO_new_file"};
		delete $funcs{"BIO_new_fp"};
	}
	if (!$NT) {
		delete $funcs{"BIO_s_log"};
411 412
	}

413
	push @ret, keys %funcs;
414

415 416
	return(@ret);
}
417

D
Dr. Stephen Henson 已提交
418 419
sub print_test_file
{
420 421 422
	(*OUT,my $name,*nums,my @functions)=@_;
	my $n = 1; my @e; my @r;
	my $func;
D
Dr. Stephen Henson 已提交
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

	(@e)=grep(/^SSLeay/,@functions);
	(@r)=grep(!/^SSLeay/,@functions);
	@functions=((sort @e),(sort @r));

	foreach $func (@functions) {
		if (!defined($nums{$func})) {
			printf STDERR "$func does not have a number assigned\n"
					if(!$do_update);
		} else {
			$n=$nums{$func};
			print OUT "\t$func();\n";
		}
	}
}

439
sub print_def_file
440
{
441 442
	(*OUT,my $name,*nums,my @functions)=@_;
	my $n = 1; my @e; my @r;
443

444
	if ($W32)
445 446 447 448 449 450
		{ $name.="32"; }
	else
		{ $name.="16"; }

	print OUT <<"EOF";
;
451
; Definition file for the DLL version of the $name library from OpenSSL
452 453 454 455
;

LIBRARY         $name

456
DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
457 458 459

EOF

460
	if (!$W32) {
461 462 463 464 465 466 467 468 469 470
		print <<"EOF";
CODE            PRELOAD MOVEABLE
DATA            PRELOAD MOVEABLE SINGLE

EXETYPE		WINDOWS

HEAPSIZE	4096
STACKSIZE	8192

EOF
471
	}
472 473 474 475 476 477 478 479

	print "EXPORTS\n";


	(@e)=grep(/^SSLeay/,@functions);
	(@r)=grep(!/^SSLeay/,@functions);
	@functions=((sort @e),(sort @r));

480 481
	foreach $func (@functions) {
		if (!defined($nums{$func})) {
U
Ulf Möller 已提交
482 483
			printf STDERR "$func does not have a number assigned\n"
					if(!$do_update);
484
		} else {
485
			$n=$nums{$func};
486
			printf OUT "    %s%-40s@%d\n",($W32)?"":"_",$func,$n;
487 488
		}
	}
489 490
	printf OUT "\n";
}
491 492

sub load_numbers
493 494 495
{
	my($name)=@_;
	my(@a,%ret);
496

497 498
	$max_num = 0;

499
	open(IN,"<$name") || die "unable to open $name:$!\n";
500
	while (<IN>) {
501 502 503 504 505
		chop;
		s/#.*$//;
		next if /^\s*$/;
		@a=split;
		$ret{$a[0]}=$a[1];
506
		$max_num = $a[1] if $a[1] > $max_num;
507
	}
508 509
	close(IN);
	return(%ret);
510
}
511 512

sub update_numbers
513 514
{
	(*OUT,$name,*nums,my $start_num, my @functions)=@_;
515 516
	my $new_funcs = 0;
	print STDERR "Updating $name\n";
517 518
	foreach $func (@functions) {
		if (!exists $nums{$func}) {
519 520 521
			$new_funcs++;
			printf OUT "%s%-40s%d\n","",$func, ++$start_num;
		}
522
	}
523 524 525 526 527
	if($new_funcs) {
		print STDERR "$new_funcs New Functions added\n";
	} else {
		print STDERR "No New Functions Added\n";
	}
528
}