mkdef.pl 5.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#!/usr/local/bin/perl
#
# generate a .def file
#
# It does this by parsing the header files and looking for the
# non-prototyped functions.
#

$crypto_num="util/libeay.num";
$ssl_num=   "util/ssleay.num";

$NT=1;
foreach (@ARGV)
	{
	$NT=1 if $_ eq "32";
	$NT=0 if $_ eq "16";
	$do_ssl=1 if $_ eq "ssleay";
	$do_crypto=1 if $_ eq "libeay";
	}

if (!$do_ssl && !$do_crypto)
	{
	print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 ]\n";
	exit(1);
	}

%ssl_list=&load_numbers($ssl_num);
%crypto_list=&load_numbers($crypto_num);

$ssl="ssl/ssl.h";

$crypto ="crypto/crypto.h";
$crypto.=" crypto/des/des.h";
$crypto.=" crypto/idea/idea.h";
$crypto.=" crypto/rc4/rc4.h";
36
$crypto.=" crypto/rc5/rc5.h";
37 38
$crypto.=" crypto/rc2/rc2.h";
$crypto.=" crypto/bf/blowfish.h";
39 40 41
$crypto.=" crypto/cast/cast.h";
$crypto.=" crypto/md2/md2.h";
$crypto.=" crypto/md5/md5.h";
42 43
$crypto.=" crypto/mdc2/mdc2.h";
$crypto.=" crypto/sha/sha.h";
44
$crypto.=" crypto/ripemd/ripemd.h";
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

$crypto.=" crypto/bn/bn.h";
$crypto.=" crypto/rsa/rsa.h";
$crypto.=" crypto/dsa/dsa.h";
$crypto.=" crypto/dh/dh.h";

$crypto.=" crypto/stack/stack.h";
$crypto.=" crypto/buffer/buffer.h";
$crypto.=" crypto/bio/bio.h";
$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";
$crypto.=" crypto/x509/x509.h";
$crypto.=" crypto/x509/x509_vfy.h";
$crypto.=" crypto/rand/rand.h";
69
$crypto.=" crypto/hmac/hmac.h";
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103

$match{'NOPROTO'}=1;
$match2{'PERL5'}=1;

&print_def_file(*STDOUT,"SSLEAY",*ssl_list,&do_defs("SSLEAY",$ssl))
	if $do_ssl == 1;

&print_def_file(*STDOUT,"LIBEAY",*crypto_list,&do_defs("LIBEAY",$crypto))
	if $do_crypto == 1;

sub do_defs
	{
	local($name,$files)=@_;
	local(@ret);

	$off=-1;
	foreach $file (split(/\s+/,$files))
		{
#		print STDERR "reading $file\n";
		open(IN,"<$file") || die "unable to open $file:$!\n";
		$depth=0;
		$pr=-1;
		@np="";
		$/=undef;
		$a=<IN>;
		while (($i=index($a,"/*")) >= 0)
			{
			$j=index($a,"*/");
			break unless ($j >= 0);
			$a=substr($a,0,$i).substr($a,$j+2);
		#	print "$i $j\n";
			}
		foreach (split("\n",$a))
			{
104
			if (/^\#\s*ifndef (.*)/)
105 106 107 108 109
				{
				push(@tag,$1);
				$tag{$1}=-1;
				next;
				}
110
			elsif (/^\#\s*if !defined\(([^\)]+)\)/)
111 112 113 114 115
				{
				push(@tag,$1);
				$tag{$1}=-1;
				next;
				}
116
			elsif (/^\#\s*ifdef (.*)/)
117 118 119 120 121
				{
				push(@tag,$1);
				$tag{$1}=1;
				next;
				}
122
			elsif (/^\#\s*if defined(.*)/)
123 124 125 126 127
				{
				push(@tag,$1);
				$tag{$1}=1;
				next;
				}
128
			elsif (/^\#\s*endif/)
129 130 131 132 133
				{
				$tag{$tag[$#tag]}=0;
				pop(@tag);
				next;
				}
134
			elsif (/^\#\s*else/)
135 136 137 138 139
				{
				$t=$tag[$#tag];
				$tag{$t}= -$tag{$t};
				next;
				}
140 141 142
#printf STDERR "$_\n%2d %2d %2d %2d %2d $NT\n",
#$tag{'NOPROTO'},$tag{'FreeBSD'},$tag{'WIN16'},$tag{'PERL5'},$tag{'NO_FP_API'};

143 144 145 146 147 148 149
			$t=undef;
			if (/^extern .*;$/)
				{ $t=&do_extern($name,$_); }
			elsif (	($tag{'NOPROTO'} == 1) &&
				($tag{'FreeBSD'} != 1) &&
				(($NT && ($tag{'WIN16'} != 1)) ||
				 (!$NT && ($tag{'WIN16'} != -1))) &&
150 151 152 153 154 155
				($tag{'PERL5'} != 1) &&
#				($tag{'_WINDLL'} != -1) &&
				((!$NT && $tag{'_WINDLL'} != -1) ||
				 ($NT && $tag{'_WINDLL'} != 1)) &&
				((($tag{'NO_FP_API'} != 1) && $NT) ||
				 (($tag{'NO_FP_API'} != -1) && !$NT)))
156
				{ $t=&do_line($name,$_); }
157 158
			else
				{ $t=undef; }
159 160 161 162
			if (($t ne undef) && (!$done{$name,$t}))
				{
				$done{$name,$t}++;
				push(@ret,$t);
163
#printf STDERR "one:$t\n" if $t =~ /BIO_/;
164 165 166 167 168 169 170 171 172 173 174 175 176 177
				}
			}
		close(IN);
		}
	return(@ret);
	}

sub do_line
	{
	local($file,$_)=@_;
	local($n);

	return(undef) if /^$/;
	return(undef) if /^\s/;
178
#printf STDERR "two:$_\n" if $_ =~ /BIO_/;
179 180 181 182 183 184 185 186
	if (/(CRYPTO_get_locking_callback)/)
		{ return($1); }
	elsif (/(CRYPTO_get_id_callback)/)
		{ return($1); }
	elsif (/(CRYPTO_get_add_lock_callback)/)
		{ return($1); }
	elsif (/(SSL_CTX_get_verify_callback)/)
		{ return($1); }
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
	elsif (/(SSL_get_info_callback)/)
		{ return($1); }
	elsif ((!$NT) && /(ERR_load_CRYPTO_strings)/)
		{ return("ERR_load_CRYPTOlib_strings"); }
	elsif (!$NT && /BIO_s_file/)
		{ return(undef); }
	elsif (!$NT && /BIO_new_file/)
		{ return(undef); }
	elsif (!$NT && /BIO_new_fp/)
		{ return(undef); }
	elsif ($NT && /BIO_s_file_internal/)
		{ return(undef); }
	elsif ($NT && /BIO_new_file_internal/)
		{ return(undef); }
	elsif ($NT && /BIO_new_fp_internal/)
		{ return(undef); }
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	else
		{
		/\s\**(\S+)\s*\(/;
		return($1);
		}
	}

sub do_extern
	{
	local($file,$_)=@_;
	local($n);

	/\s\**(\S+);$/;
	return($1);
	}

sub print_def_file
	{
	local(*OUT,$name,*nums,@functions)=@_;
	local($n)=1;

	if ($NT)
		{ $name.="32"; }
	else
		{ $name.="16"; }

	print OUT <<"EOF";
;
; Definition file for the DDL version of the $name library from SSLeay
;

LIBRARY         $name

DESCRIPTION     'SSLeay $name - eay\@cryptsoft.com'

EOF

	if (!$NT)
		{
		print <<"EOF";
CODE            PRELOAD MOVEABLE
DATA            PRELOAD MOVEABLE SINGLE

EXETYPE		WINDOWS

HEAPSIZE	4096
STACKSIZE	8192

EOF
		}

	print "EXPORTS\n";


	(@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";
			}
		else
			{
			$n=$nums{$func};
			printf OUT "    %s%-35s@%d\n",($NT)?"":"_",$func,$n;
			}
		}
	printf OUT "\n";
	}

sub load_numbers
	{
	local($name)=@_;
	local($j,@a,%ret);

	open(IN,"<$name") || die "unable to open $name:$!\n";
	while (<IN>)
		{
		chop;
		s/#.*$//;
		next if /^\s*$/;
		@a=split;
		$ret{$a[0]}=$a[1];
		}
	close(IN);
	return(%ret);
	}