]> git.ipfire.org Git - thirdparty/openssl.git/blame - util/mkdef.pl
fix editing error
[thirdparty/openssl.git] / util / mkdef.pl
CommitLineData
ce9449cf 1#!/usr/local/bin/perl -w
d02b48c6
RE
2#
3# generate a .def file
4#
5# It does this by parsing the header files and looking for the
47339f61 6# prototyped functions: it then prunes the output.
d02b48c6 7#
948d0125
RL
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 replacements 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. This script assumes those redefinitions are place in the file
d63b8db8
RL
43# crypto/symhacks.h.
44# The semantics for the platforms list is a bit complicated. The rule of
45# thumb is that the list is exclusive, but it seems to mean different things.
46# So, if the list is all negatives (like "!VMS,!WIN16"), the symbol exists
47# on all platforms except those listed. If the list is all positives (like
48# "VMS,WIN16"), the symbol exists only on those platforms and nowhere else.
49# The combination of positives and negatives will act as if the positives
50# weren't there.
948d0125
RL
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#
d02b48c6 56
948d0125
RL
57my $crypto_num= "util/libeay.num";
58my $ssl_num= "util/ssleay.num";
d02b48c6 59
47339f61 60my $do_update = 0;
948d0125 61my $do_rewrite = 0;
47339f61
DSH
62my $do_crypto = 0;
63my $do_ssl = 0;
0f583f69 64my $do_ctest = 0;
967f4ca8 65my $do_ctestall = 0;
47339f61 66
948d0125
RL
67my $VMS=0;
68my $W32=0;
69my $W16=0;
0f583f69 70my $NT=0;
28a98809 71# Set this to make typesafe STACK definitions appear in DEF
e41c8d6a 72my $safe_stack_def = 0;
31ff97b2 73
948d0125
RL
74my @known_platforms = ( "__FreeBSD__", "VMS", "WIN16", "WIN32",
75 "WINNT", "PERL5", "NeXT" );
76my @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
3f07fe09
RL
77 "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1",
78 "RIPEMD",
79 "MDC2", "RSA", "DSA", "DH", "HMAC", "RIJNDAEL",
80 # Envelope "algorithms"
81 "EVP", "X509", "ASN1_TYPEDEFS",
82 # Helper "algorithms"
83 "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
84 "LOCKING",
85 # External "algorithms"
86 "FP_API", "STDIO", "SOCK", "KRB5" );
948d0125 87
0f583f69 88my $options="";
31ff97b2
UM
89open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
90while(<IN>) {
91 $options=$1 if (/^OPTIONS=(.*)$/);
92}
ce457a54 93close(IN);
31ff97b2 94
0f583f69
UM
95# The following ciphers may be excluded (by Configure). This means functions
96# defined with ifndef(NO_XXX) are not included in the .def file, and everything
97# in directory xxx is ignored.
98my $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
3009458e
RL
99my $no_cast;
100my $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
993ea851 101my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_rijndael;
948d0125 102my $no_fp_api;
0f583f69 103
31ff97b2 104foreach (@ARGV, split(/ /, $options))
d02b48c6 105 {
06c68491 106 $W32=1 if $_ eq "32";
948d0125 107 $W16=1 if $_ eq "16";
06c68491
DSH
108 if($_ eq "NT") {
109 $W32 = 1;
110 $NT = 1;
111 }
948d0125 112 $VMS=1 if $_ eq "VMS";
d63b8db8 113
d02b48c6 114 $do_ssl=1 if $_ eq "ssleay";
55a9cc6e 115 $do_ssl=1 if $_ eq "ssl";
d02b48c6 116 $do_crypto=1 if $_ eq "libeay";
55a9cc6e
DSH
117 $do_crypto=1 if $_ eq "crypto";
118 $do_update=1 if $_ eq "update";
948d0125 119 $do_rewrite=1 if $_ eq "rewrite";
12aefe78 120 $do_ctest=1 if $_ eq "ctest";
967f4ca8 121 $do_ctestall=1 if $_ eq "ctestall";
a8b07aa4 122 #$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
8cf65228
UM
123
124 if (/^no-rc2$/) { $no_rc2=1; }
125 elsif (/^no-rc4$/) { $no_rc4=1; }
126 elsif (/^no-rc5$/) { $no_rc5=1; }
127 elsif (/^no-idea$/) { $no_idea=1; }
be054868 128 elsif (/^no-des$/) { $no_des=1; $no_mdc2=1; }
8cf65228
UM
129 elsif (/^no-bf$/) { $no_bf=1; }
130 elsif (/^no-cast$/) { $no_cast=1; }
131 elsif (/^no-md2$/) { $no_md2=1; }
3009458e 132 elsif (/^no-md4$/) { $no_md4=1; }
8cf65228
UM
133 elsif (/^no-md5$/) { $no_md5=1; }
134 elsif (/^no-sha$/) { $no_sha=1; }
135 elsif (/^no-ripemd$/) { $no_ripemd=1; }
136 elsif (/^no-mdc2$/) { $no_mdc2=1; }
137 elsif (/^no-rsa$/) { $no_rsa=1; }
138 elsif (/^no-dsa$/) { $no_dsa=1; }
139 elsif (/^no-dh$/) { $no_dh=1; }
140 elsif (/^no-hmac$/) { $no_hmac=1; }
993ea851 141 elsif (/^no-rijndael$/) { $no_rijndael=1; }
3f07fe09
RL
142 elsif (/^no-evp$/) { $no_evp=1; }
143 elsif (/^no-lhash$/) { $no_lhash=1; }
144 elsif (/^no-stack$/) { $no_stack=1; }
145 elsif (/^no-err$/) { $no_err=1; }
146 elsif (/^no-buffer$/) { $no_buffer=1; }
147 elsif (/^no-bio$/) { $no_bio=1; }
148 #elsif (/^no-locking$/) { $no_locking=1; }
149 elsif (/^no-comp$/) { $no_comp=1; }
150 elsif (/^no-dso$/) { $no_dso=1; }
d02b48c6
RE
151 }
152
12aefe78 153
948d0125
RL
154# If no platform is given, assume WIN32
155if ($W32 + $W16 + $VMS == 0) {
156 $W32 = 1;
157}
158
159# Add extra knowledge
160if ($W16) {
161 $no_fp_api=1;
162}
163
d02b48c6
RE
164if (!$do_ssl && !$do_crypto)
165 {
ccb9643f 166 print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT ]\n";
d02b48c6
RE
167 exit(1);
168 }
169
170%ssl_list=&load_numbers($ssl_num);
55a9cc6e 171$max_ssl = $max_num;
d02b48c6 172%crypto_list=&load_numbers($crypto_num);
55a9cc6e 173$max_crypto = $max_num;
d02b48c6 174
0f583f69 175my $ssl="ssl/ssl.h";
3f07fe09 176$ssl.=" ssl/kssl.h";
d02b48c6 177
0f583f69 178my $crypto ="crypto/crypto.h";
8cf65228
UM
179$crypto.=" crypto/des/des.h" unless $no_des;
180$crypto.=" crypto/idea/idea.h" unless $no_idea;
181$crypto.=" crypto/rc4/rc4.h" unless $no_rc4;
182$crypto.=" crypto/rc5/rc5.h" unless $no_rc5;
183$crypto.=" crypto/rc2/rc2.h" unless $no_rc2;
184$crypto.=" crypto/bf/blowfish.h" unless $no_bf;
185$crypto.=" crypto/cast/cast.h" unless $no_cast;
186$crypto.=" crypto/md2/md2.h" unless $no_md2;
3009458e 187$crypto.=" crypto/md4/md4.h" unless $no_md4;
8cf65228
UM
188$crypto.=" crypto/md5/md5.h" unless $no_md5;
189$crypto.=" crypto/mdc2/mdc2.h" unless $no_mdc2;
190$crypto.=" crypto/sha/sha.h" unless $no_sha;
191$crypto.=" crypto/ripemd/ripemd.h" unless $no_ripemd;
993ea851 192$crypto.=" crypto/rijndael/rijndael.h" unless $no_rijndael;
5f524acc 193$crypto.=" crypto/rijndael/rd_fst.h" unless $no_rijndael;
d02b48c6
RE
194
195$crypto.=" crypto/bn/bn.h";
8cf65228
UM
196$crypto.=" crypto/rsa/rsa.h" unless $no_rsa;
197$crypto.=" crypto/dsa/dsa.h" unless $no_dsa;
198$crypto.=" crypto/dh/dh.h" unless $no_dh;
199$crypto.=" crypto/hmac/hmac.h" unless $no_hmac;
d02b48c6 200
5270e702 201$crypto.=" crypto/engine/engine.h";
3f07fe09
RL
202$crypto.=" crypto/stack/stack.h" unless $no_stack;
203$crypto.=" crypto/buffer/buffer.h" unless $no_buffer;
204$crypto.=" crypto/bio/bio.h" unless $no_bio;
205$crypto.=" crypto/dso/dso.h" unless $no_dso;
206$crypto.=" crypto/lhash/lhash.h" unless $no_lhash;
d02b48c6
RE
207$crypto.=" crypto/conf/conf.h";
208$crypto.=" crypto/txt_db/txt_db.h";
209
3f07fe09 210$crypto.=" crypto/evp/evp.h" unless $no_evp;
d02b48c6
RE
211$crypto.=" crypto/objects/objects.h";
212$crypto.=" crypto/pem/pem.h";
213#$crypto.=" crypto/meth/meth.h";
214$crypto.=" crypto/asn1/asn1.h";
9c67ab2f 215$crypto.=" crypto/asn1/asn1t.h";
d02b48c6 216$crypto.=" crypto/asn1/asn1_mac.h";
3f07fe09 217$crypto.=" crypto/err/err.h" unless $no_err;
d02b48c6 218$crypto.=" crypto/pkcs7/pkcs7.h";
ee0508d4 219$crypto.=" crypto/pkcs12/pkcs12.h";
d02b48c6
RE
220$crypto.=" crypto/x509/x509.h";
221$crypto.=" crypto/x509/x509_vfy.h";
679ab7c3 222$crypto.=" crypto/x509v3/x509v3.h";
d02b48c6 223$crypto.=" crypto/rand/rand.h";
3f07fe09 224$crypto.=" crypto/comp/comp.h" unless $no_comp;
eb64730b 225$crypto.=" crypto/ocsp/ocsp.h";
dfeab068 226$crypto.=" crypto/tmdiff.h";
d02b48c6 227
948d0125 228my $symhacks="crypto/symhacks.h";
55a9cc6e 229
948d0125
RL
230my @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
231my @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
47339f61 232
55a9cc6e
DSH
233if ($do_update) {
234
235if ($do_ssl == 1) {
948d0125
RL
236
237 &maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
238 if ($do_rewrite == 1) {
239 open(OUT, ">$ssl_num");
240 &rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
241 close OUT;
242 } else {
243 open(OUT, ">>$ssl_num");
244 }
245 &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
55a9cc6e
DSH
246 close OUT;
247}
248
249if($do_crypto == 1) {
948d0125
RL
250
251 &maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
252 if ($do_rewrite == 1) {
253 open(OUT, ">$crypto_num");
254 &rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
255 } else {
256 open(OUT, ">>$crypto_num");
257 }
258 &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
55a9cc6e 259 close OUT;
12aefe78
DSH
260}
261
967f4ca8 262} elsif ($do_ctest || $do_ctestall) {
12aefe78
DSH
263
264 print <<"EOF";
265
266/* Test file to check all DEF file symbols are present by trying
267 * to link to all of them. This is *not* intended to be run!
268 */
269
270int main()
271{
272EOF
948d0125 273 &print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
12aefe78
DSH
274 if $do_ssl == 1;
275
948d0125 276 &print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
12aefe78
DSH
277 if $do_crypto == 1;
278
279 print "}\n";
55a9cc6e
DSH
280
281} else {
8cf65228 282
948d0125 283 &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_symbols)
55a9cc6e
DSH
284 if $do_ssl == 1;
285
948d0125 286 &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_symbols)
55a9cc6e 287 if $do_crypto == 1;
8cf65228 288
55a9cc6e 289}
d02b48c6 290
d02b48c6
RE
291
292sub do_defs
47339f61 293{
948d0125 294 my($name,$files,$symhacksfile)=@_;
0f583f69 295 my $file;
47339f61 296 my @ret;
948d0125
RL
297 my %syms;
298 my %platform; # For anything undefined, we assume ""
299 my %kind; # For anything undefined, we assume "FUNCTION"
300 my %algorithm; # For anything undefined, we assume ""
301 my %rename;
0f583f69 302 my $cpp;
3f07fe09 303 my %unknown_algorithms = ();
d02b48c6 304
948d0125 305 foreach $file (split(/\s+/,$symhacksfile." ".$files))
d02b48c6 306 {
d02b48c6 307 open(IN,"<$file") || die "unable to open $file:$!\n";
0f583f69 308 my $line = "", my $def= "";
47339f61 309 my %tag = (
948d0125
RL
310 (map { $_ => 0 } @known_platforms),
311 (map { "NO_".$_ => 0 } @known_algorithms),
47339f61 312 NOPROTO => 0,
47339f61
DSH
313 PERL5 => 0,
314 _WINDLL => 0,
47339f61
DSH
315 CONST_STRICT => 0,
316 TRUE => 1,
317 );
948d0125 318 my $symhacking = $file eq $symhacksfile;
47339f61
DSH
319 while(<IN>) {
320 last if (/BEGIN ERROR CODES/);
321 if ($line ne '') {
322 $_ = $line . $_;
323 $line = '';
d02b48c6 324 }
47339f61
DSH
325
326 if (/\\$/) {
327 $line = $_;
328 next;
329 }
330
948d0125 331 $cpp = 1 if /^\#.*ifdef.*cplusplus/;
47339f61 332 if ($cpp) {
948d0125 333 $cpp = 0 if /^\#.*endif/;
47339f61
DSH
334 next;
335 }
336
337 s/\/\*.*?\*\///gs; # ignore comments
338 s/{[^{}]*}//gs; # ignore {} blocks
3f07fe09
RL
339 #print STDERR "DEBUG: \$_=\"$_\"\n";
340 if (/^\#\s*ifndef\s+(.*)/) {
d02b48c6
RE
341 push(@tag,$1);
342 $tag{$1}=-1;
3f07fe09 343 } elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
d02b48c6
RE
344 push(@tag,$1);
345 $tag{$1}=-1;
3f07fe09 346 } elsif (/^\#\s*ifdef\s+(.*)/) {
d02b48c6
RE
347 push(@tag,$1);
348 $tag{$1}=1;
3f07fe09 349 } elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
d02b48c6
RE
350 push(@tag,$1);
351 $tag{$1}=1;
948d0125
RL
352 } elsif (/^\#\s*error\s+(\w+) is disabled\./) {
353 if ($tag[$#tag] eq "NO_".$1) {
354 $tag{$tag[$#tag]}=2;
355 }
47339f61 356 } elsif (/^\#\s*endif/) {
3f07fe09
RL
357 my $oldtag=$tag[$#tag];
358 #print STDERR "DEBUG: \$oldtag=\"$oldtag\"\n";
948d0125
RL
359 if ($tag{$tag[$#tag]}==2) {
360 $tag{$tag[$#tag]}=-1;
361 } else {
362 $tag{$tag[$#tag]}=0;
363 }
d02b48c6 364 pop(@tag);
3f07fe09
RL
365 if ($oldtag =~ /^NO_([A-Z0-9_]+)$/) {
366 $oldtag=$1;
367 } else {
368 $oldtag="";
369 }
370 if ($oldtag ne ""
371 && !grep(/^$oldtag$/, @known_algorithms)) {
372 $unknown_algorithms{$oldtag} = 1;
373 #print STDERR "DEBUG: Added as unknown algorithm: $oldtag\n";
374 }
47339f61
DSH
375 } elsif (/^\#\s*else/) {
376 my $t=$tag[$#tag];
d02b48c6 377 $tag{$t}= -$tag{$t};
47339f61
DSH
378 } elsif (/^\#\s*if\s+1/) {
379 # Dummy tag
380 push(@tag,"TRUE");
381 $tag{"TRUE"}=1;
1e414935
BM
382 } elsif (/^\#\s*if\s+0/) {
383 # Dummy tag
384 push(@tag,"TRUE");
385 $tag{"TRUE"}=-1;
948d0125
RL
386 } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
387 && $symhacking) {
388 my $s = $1;
389 my $a =
390 $2.":".join(",", grep(!/^$/,
391 map { $tag{$_} == 1 ?
392 $_ : "" }
393 @known_platforms));
394 $rename{$s} = $a;
395 }
396 if (/^\#/) {
397 my @p = grep(!/^$/,
398 map { $tag{$_} == 1 ? $_ :
399 $tag{$_} == -1 ? "!".$_ : "" }
400 @known_platforms);
401 my @a = grep(!/^$/,
402 map { $tag{"NO_".$_} == -1 ? $_ : "" }
403 @known_algorithms);
404 $def .= "#INFO:".join(',',@p).":".join(',',@a).";";
47339f61
DSH
405 next;
406 }
13083215 407 if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
a8b07aa4 408 next;
3c07b4c2
DSH
409 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
410 $syms{"d2i_$3"} = 1;
411 $syms{"i2d_$3"} = 1;
412 $syms{"$3_new"} = 1;
413 $syms{"$3_free"} = 1;
414 $syms{"$2_it"} = 1;
415 $kind{"$2_it"} = "VARIABLE";
9c67ab2f
DSH
416 } elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
417 $syms{"d2i_$1"} = 1;
418 $syms{"i2d_$1"} = 1;
419 $syms{"$1_new"} = 1;
420 $syms{"$1_free"} = 1;
421 $syms{"$1_it"} = 1;
422 $kind{"$1_it"} = "VARIABLE";
423 next;
948d0125
RL
424 } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
425 next;
13083215 426 } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
a8b07aa4 427 next;
dbd665c2 428 } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
948d0125
RL
429 /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
430 # Things not in Win16
431 $syms{"PEM_read_${1}"} = 1;
432 $platform{"PEM_read_${1}"} = "!WIN16";
433 $syms{"PEM_write_${1}"} = 1;
434 $platform{"PEM_write_${1}"} = "!WIN16";
435 # Things that are everywhere
436 $syms{"PEM_read_bio_${1}"} = 1;
437 $syms{"PEM_write_bio_${1}"} = 1;
438 if ($1 eq "RSAPrivateKey" ||
439 $1 eq "RSAPublicKey" ||
440 $1 eq "RSA_PUBKEY") {
441 $algorithm{"PEM_read_${1}"} = "RSA";
442 $algorithm{"PEM_write_${1}"} = "RSA";
443 $algorithm{"PEM_read_bio_${1}"} = "RSA";
444 $algorithm{"PEM_write_bio_${1}"} = "RSA";
445 }
446 elsif ($1 eq "DSAPrivateKey" ||
447 $1 eq "DSAparams" ||
448 $1 eq "RSA_PUBKEY") {
449 $algorithm{"PEM_read_${1}"} = "DSA";
450 $algorithm{"PEM_write_${1}"} = "DSA";
451 $algorithm{"PEM_read_bio_${1}"} = "DSA";
452 $algorithm{"PEM_write_bio_${1}"} = "DSA";
453 }
454 elsif ($1 eq "DHparams") {
455 $algorithm{"PEM_read_${1}"} = "DH";
456 $algorithm{"PEM_write_${1}"} = "DH";
457 $algorithm{"PEM_read_bio_${1}"} = "DH";
458 $algorithm{"PEM_write_bio_${1}"} = "DH";
dbd665c2 459 }
8a208cba
DSH
460 } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
461 /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
948d0125
RL
462 # Things not in Win16
463 $syms{"PEM_write_${1}"} = 1;
464 $platform{"PEM_write_${1}"} .= ",!WIN16";
465 # Things that are everywhere
466 $syms{"PEM_write_bio_${1}"} = 1;
467 if ($1 eq "RSAPrivateKey" ||
468 $1 eq "RSAPublicKey" ||
469 $1 eq "RSA_PUBKEY") {
470 $algorithm{"PEM_write_${1}"} = "RSA";
471 $algorithm{"PEM_write_bio_${1}"} = "RSA";
472 }
473 elsif ($1 eq "DSAPrivateKey" ||
474 $1 eq "DSAparams" ||
475 $1 eq "RSA_PUBKEY") {
476 $algorithm{"PEM_write_${1}"} = "DSA";
477 $algorithm{"PEM_write_bio_${1}"} = "DSA";
478 }
479 elsif ($1 eq "DHparams") {
480 $algorithm{"PEM_write_${1}"} = "DH";
481 $algorithm{"PEM_write_bio_${1}"} = "DH";
8a208cba 482 }
8a208cba
DSH
483 } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
484 /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
948d0125
RL
485 # Things not in Win16
486 $syms{"PEM_read_${1}"} = 1;
487 $platform{"PEM_read_${1}"} .= ",!WIN16";
488 # Things that are everywhere
489 $syms{"PEM_read_bio_${1}"} = 1;
0f583f69 490 } elsif (
948d0125
RL
491 ($tag{'TRUE'} != -1)
492 && ($tag{'CONST_STRICT'} != 1)
493 )
d02b48c6 494 {
948d0125 495 if (/\{|\/\*|\([^\)]*$/) {
47339f61
DSH
496 $line = $_;
497 } else {
498 $def .= $_;
499 }
d02b48c6
RE
500 }
501 }
502 close(IN);
47339f61 503
948d0125
RL
504 my $algs;
505 my $plays;
506
47339f61 507 foreach (split /;/, $def) {
948d0125 508 my $s; my $k = "FUNCTION"; my $p; my $a;
47339f61
DSH
509 s/^[\n\s]*//g;
510 s/[\n\s]*$//g;
948d0125 511 next if(/\#undef/);
47339f61 512 next if(/typedef\W/);
948d0125
RL
513 next if(/\#define/);
514
515 if (/^\#INFO:([^:]*):(.*)$/) {
516 $plats = $1;
517 $algs = $2;
518 next;
519 } elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+)(\[[0-9]*\])*\s*$/) {
520 $s = $1;
521 $k = "VARIABLE";
522 } elsif (/\(\*(\w*)\([^\)]+/) {
523 $s = $1;
47339f61
DSH
524 } elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
525 # K&R C
526 next;
527 } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
528 while (not /\(\)$/s) {
529 s/[^\(\)]*\)$/\)/s;
530 s/\([^\(\)]*\)\)$/\)/s;
531 }
532 s/\(void\)//;
533 /(\w+)\W*\(\)/s;
948d0125 534 $s = $1;
47339f61
DSH
535 } elsif (/\(/ and not (/=/)) {
536 print STDERR "File $file: cannot parse: $_;\n";
948d0125
RL
537 next;
538 } else {
539 next;
540 }
541
542 $syms{$s} = 1;
543 $kind{$s} = $k;
544
545 $p = $plats;
546 $a = $algs;
547 $a .= ",BF" if($s =~ /EVP_bf/);
548 $a .= ",CAST" if($s =~ /EVP_cast/);
549 $a .= ",DES" if($s =~ /EVP_des/);
550 $a .= ",DSA" if($s =~ /EVP_dss/);
551 $a .= ",IDEA" if($s =~ /EVP_idea/);
552 $a .= ",MD2" if($s =~ /EVP_md2/);
553 $a .= ",MD4" if($s =~ /EVP_md4/);
554 $a .= ",MD5" if($s =~ /EVP_md5/);
555 $a .= ",RC2" if($s =~ /EVP_rc2/);
556 $a .= ",RC4" if($s =~ /EVP_rc4/);
557 $a .= ",RC5" if($s =~ /EVP_rc5/);
558 $a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
559 $a .= ",SHA" if($s =~ /EVP_sha/);
560 $a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
561 $a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
562 $a .= ",RSA" if($s =~ /RSAPrivateKey/);
563 $a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
564
565 $platform{$s} .= ','.$p;
566 $algorithm{$s} .= ','.$a;
567
568 if (defined($rename{$s})) {
569 (my $r, my $p) = split(/:/,$rename{$s});
570 my @ip = map { /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p;
571 $syms{$r} = 1;
572 $kind{$r} = $kind{$s}."(".$s.")";
573 $algorithm{$r} = $algorithm{$s};
574 $platform{$r} = $platform{$s}.",".$p;
575 $platform{$s} .= ','.join(',', @ip).','.join(',', @ip);
47339f61 576 }
d02b48c6 577 }
d02b48c6
RE
578 }
579
948d0125 580 # Prune the returned symbols
47339f61 581
948d0125 582 $platform{"crypt"} .= ",!PERL5,!__FreeBSD__,!NeXT";
47339f61 583
948d0125
RL
584 delete $syms{"SSL_add_dir_cert_subjects_to_stack"};
585 delete $syms{"bn_dump1"};
586
587 $platform{"BIO_s_file_internal"} .= ",WIN16";
588 $platform{"BIO_new_file_internal"} .= ",WIN16";
589 $platform{"BIO_new_fp_internal"} .= ",WIN16";
590
591 $platform{"BIO_s_file"} .= ",!WIN16";
592 $platform{"BIO_new_file"} .= ",!WIN16";
593 $platform{"BIO_new_fp"} .= ",!WIN16";
594
595 $platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
596
597 if(exists $syms{"ERR_load_CRYPTO_strings"}) {
598 $platform{"ERR_load_CRYPTO_strings"} .= ",!VMS,!WIN16";
599 $syms{"ERR_load_CRYPTOlib_strings"} = 1;
600 $platform{"ERR_load_CRYPTOlib_strings"} .= ",VMS,WIN16";
601 }
602
603 # Info we know about
604
948d0125
RL
605 push @ret, map { $_."\\".&info_string($_,"EXIST",
606 $platform{$_},
607 $kind{$_},
608 $algorithm{$_}) } keys %syms;
609
3f07fe09
RL
610 if (keys %unknown_algorithms) {
611 print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
612 print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
613 }
948d0125
RL
614 return(@ret);
615}
616
617sub info_string {
618 (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
619
620 my %a = defined($algorithms) ?
621 map { $_ => 1 } split /,/, $algorithms : ();
622 my $pl = defined($platforms) ? $platforms : "";
623 my %p = map { $_ => 0 } split /,/, $pl;
624 my $k = defined($kind) ? $kind : "FUNCTION";
625 my $ret;
626
627 # We do this, because if there's code like the following, it really
628 # means the function exists in all cases and should therefore be
629 # everywhere. By increasing and decreasing, we may attain 0:
630 #
631 # ifndef WIN16
632 # int foo();
633 # else
634 # int _fat foo();
635 # endif
636 foreach $platform (split /,/, $pl) {
637 if ($platform =~ /^!(.*)$/) {
638 $p{$1}--;
639 } else {
640 $p{$platform}++;
d02b48c6 641 }
47339f61 642 }
948d0125
RL
643 foreach $platform (keys %p) {
644 if ($p{$platform} == 0) { delete $p{$platform}; }
d02b48c6
RE
645 }
646
948d0125
RL
647 delete $p{""};
648 delete $a{""};
d02b48c6 649
948d0125
RL
650 $ret = $exist;
651 $ret .= ":".join(',',map { $p{$_} < 0 ? "!".$_ : $_ } keys %p);
652 $ret .= ":".$k;
653 $ret .= ":".join(',',keys %a);
654 return $ret;
655}
656
657sub maybe_add_info {
658 (my $name, *nums, my @symbols) = @_;
659 my $sym;
660 my $new_info = 0;
451e60e9 661 my %syms=();
948d0125
RL
662
663 print STDERR "Updating $name info\n";
664 foreach $sym (@symbols) {
665 (my $s, my $i) = split /\\/, $sym;
666 $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
667 if (defined($nums{$s})) {
668 (my $n, my $dummy) = split /\\/, $nums{$s};
669 if (!defined($dummy) || $i ne $dummy) {
670 $nums{$s} = $n."\\".$i;
671 $new_info++;
672 #print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n";
673 }
674 }
451e60e9
RL
675 $syms{sym} = 1;
676 }
677
678 my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
679 foreach $sym (@s) {
680 (my $n, my $i) = split /\\/, $nums{$sym};
681 if (!defined($syms{sym})) {
682 $new_info++;
683 #print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n";
684 }
948d0125
RL
685 }
686 if ($new_info) {
687 print STDERR "$new_info old symbols got an info update\n";
6d50071e
RL
688 if (!$do_rewrite) {
689 print STDERR "You should do a rewrite to fix this.\n";
690 }
948d0125
RL
691 } else {
692 print STDERR "No old symbols needed info update\n";
693 }
47339f61 694}
d02b48c6 695
12aefe78
DSH
696sub print_test_file
697{
948d0125 698 (*OUT,my $name,*nums,my @symbols)=@_;
0f583f69 699 my $n = 1; my @e; my @r;
948d0125
RL
700 my $sym; my $prev = ""; my $prefSSLeay;
701
702 (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
703 (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
704 @symbols=((sort @e),(sort @r));
705
706 foreach $sym (@symbols) {
707 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
708 if ($s ne $prev) {
709 if (!defined($nums{$sym})) {
710 printf STDERR "Warning: $sym does not have a number assigned\n"
711 if(!$do_update);
967f4ca8 712 } else {
948d0125
RL
713 $n=$nums{$s};
714 print OUT "\t$s();\n";
967f4ca8 715 }
12aefe78 716 }
948d0125 717 $prev = $s; # To avoid duplicates...
12aefe78
DSH
718 }
719}
720
d02b48c6 721sub print_def_file
47339f61 722{
948d0125 723 (*OUT,my $name,*nums,my @symbols)=@_;
9c67ab2f 724 my $n = 1; my @e; my @r; my @v;
d02b48c6 725
06c68491 726 if ($W32)
d02b48c6
RE
727 { $name.="32"; }
728 else
729 { $name.="16"; }
730
731 print OUT <<"EOF";
732;
9b3086fe 733; Definition file for the DLL version of the $name library from OpenSSL
d02b48c6
RE
734;
735
736LIBRARY $name
737
9b3086fe 738DESCRIPTION 'OpenSSL $name - http://www.openssl.org/'
d02b48c6
RE
739
740EOF
741
47339f61 742 if (!$W32) {
d02b48c6
RE
743 print <<"EOF";
744CODE PRELOAD MOVEABLE
745DATA PRELOAD MOVEABLE SINGLE
746
747EXETYPE WINDOWS
748
749HEAPSIZE 4096
750STACKSIZE 8192
751
752EOF
47339f61 753 }
d02b48c6
RE
754
755 print "EXPORTS\n";
756
948d0125
RL
757 (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
758 (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
9c67ab2f
DSH
759 (@v)=grep(/^\w+\\.*?:.*?:VARIABLE/,@symbols);
760 @symbols=((sort @e),(sort @r), (sort @v));
d02b48c6 761
d02b48c6 762
948d0125
RL
763 foreach $sym (@symbols) {
764 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
9c67ab2f
DSH
765 my $v = 0;
766 $v = 1 if $sym=~ /^\w+\\.*?:.*?:VARIABLE/;
948d0125
RL
767 if (!defined($nums{$s})) {
768 printf STDERR "Warning: $s does not have a number assigned\n"
8cf65228 769 if(!$do_update);
47339f61 770 } else {
948d0125 771 (my $n, my $i) = split /\\/, $nums{$s};
d63b8db8 772 my %pf = ();
be054868
RL
773 my @p = split(/,/, ($i =~ /^[^:]*:([^:]*):/,$1));
774 my @a = split(/,/, ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1));
d63b8db8
RL
775 # @p_purged must contain hardware platforms only
776 my @p_purged = ();
777 foreach $ptmp (@p) {
d63b8db8
RL
778 push @p_purged, $ptmp;
779 }
780 my $negatives = !!grep(/^!/,@p);
965c1775 781 # It is very important to check NT before W32
d63b8db8
RL
782 if ((($NT && (!@p_purged
783 || (!$negatives && grep(/^WINNT$/,@p))
784 || ($negatives && !grep(/^!WINNT$/,@p))))
785 || ($W32 && (!@p_purged
786 || (!$negatives && grep(/^WIN32$/,@p))
787 || ($negatives && !grep(/^!WIN32$/,@p))))
788 || ($W16 && (!@p_purged
789 || (!$negatives && grep(/^WIN16$/,@p))
790 || ($negatives && !grep(/^!WIN16$/,@p)))))
be054868
RL
791 && (!@a || (!$no_rc2 || !grep(/^RC2$/,@a)))
792 && (!@a || (!$no_rc4 || !grep(/^RC4$/,@a)))
793 && (!@a || (!$no_rc5 || !grep(/^RC5$/,@a)))
794 && (!@a || (!$no_idea || !grep(/^IDEA$/,@a)))
795 && (!@a || (!$no_des || !grep(/^DES$/,@a)))
796 && (!@a || (!$no_bf || !grep(/^BF$/,@a)))
797 && (!@a || (!$no_cast || !grep(/^CAST$/,@a)))
798 && (!@a || (!$no_md2 || !grep(/^MD2$/,@a)))
799 && (!@a || (!$no_md4 || !grep(/^MD4$/,@a)))
800 && (!@a || (!$no_md5 || !grep(/^MD5$/,@a)))
801 && (!@a || (!$no_sha || !grep(/^SHA$/,@a)))
802 && (!@a || (!$no_ripemd || !grep(/^RIPEMD$/,@a)))
803 && (!@a || (!$no_mdc2 || !grep(/^MDC2$/,@a)))
804 && (!@a || (!$no_rsa || !grep(/^RSA$/,@a)))
805 && (!@a || (!$no_dsa || !grep(/^DSA$/,@a)))
806 && (!@a || (!$no_dh || !grep(/^DH$/,@a)))
807 && (!@a || (!$no_hmac || !grep(/^HMAC$/,@a)))
993ea851 808 && (!@a || (!$no_rijndael || !grep(/^RIJNDAEL$/,@a)))
be054868
RL
809 && (!@a || (!$no_fp_api || !grep(/^FP_API$/,@a)))
810 ) {
9c67ab2f
DSH
811 if($v) {
812 printf OUT " %s%-40s@%-8d DATA\n",($W32)?"":"_",$s,$n;
813 } else {
814 printf OUT " %s%-40s@%d\n",($W32)?"":"_",$s,$n;
815 }
d63b8db8
RL
816# } else {
817# print STDERR "DEBUG: \"$sym\" (@p):",
818# " rsaref:", !!(!@p
819# || (!$negatives
820# && ($rsaref || !grep(/^RSAREF$/,@p)))
821# || ($negatives
822# && (!$rsaref || !grep(/^!RSAREF$/,@p))))?1:0,
823# " 16:", !!($W16 && (!@p_purged
824# || (!$negatives && grep(/^WIN16$/,@p))
825# || ($negatives && !grep(/^!WIN16$/,@p)))),
826# " 32:", !!($W32 && (!@p_purged
827# || (!$negatives && grep(/^WIN32$/,@p))
828# || ($negatives && !grep(/^!WIN32$/,@p)))),
829# " NT:", !!($NT && (!@p_purged
830# || (!$negatives && grep(/^WINNT$/,@p))
831# || ($negatives && !grep(/^!WINNT$/,@p)))),
832# "\n";
965c1775 833 }
d02b48c6 834 }
d02b48c6 835 }
47339f61
DSH
836 printf OUT "\n";
837}
d02b48c6
RE
838
839sub load_numbers
47339f61
DSH
840{
841 my($name)=@_;
842 my(@a,%ret);
d02b48c6 843
55a9cc6e 844 $max_num = 0;
948d0125
RL
845 $num_noinfo = 0;
846 $prev = "";
55a9cc6e 847
d02b48c6 848 open(IN,"<$name") || die "unable to open $name:$!\n";
47339f61 849 while (<IN>) {
d02b48c6
RE
850 chop;
851 s/#.*$//;
852 next if /^\s*$/;
853 @a=split;
948d0125
RL
854 if (defined $ret{$a[0]}) {
855 print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
856 }
857 if ($max_num > $a[1]) {
858 print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
859 }
860 if ($max_num == $a[1]) {
861 # This is actually perfectly OK
862 #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
863 }
864 if ($#a < 2) {
865 # Existence will be proven later, in do_defs
866 $ret{$a[0]}=$a[1];
867 $num_noinfo++;
868 } else {
869 $ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
870 }
55a9cc6e 871 $max_num = $a[1] if $a[1] > $max_num;
948d0125
RL
872 $prev=$a[0];
873 }
874 if ($num_noinfo) {
875 print STDERR "Warning: $num_noinfo symbols were without info.";
876 if ($do_rewrite) {
877 printf STDERR " The rewrite will fix this.\n";
878 } else {
879 printf STDERR " You should do a rewrite to fix this.\n";
880 }
47339f61 881 }
d02b48c6
RE
882 close(IN);
883 return(%ret);
47339f61 884}
55a9cc6e 885
948d0125
RL
886sub parse_number
887{
888 (my $str, my $what) = @_;
889 (my $n, my $i) = split(/\\/,$str);
890 if ($what eq "n") {
891 return $n;
892 } else {
893 return $i;
894 }
895}
896
897sub rewrite_numbers
898{
899 (*OUT,$name,*nums,@symbols)=@_;
900 my $thing;
901
902 print STDERR "Rewriting $name\n";
903
904 my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
905 my $r; my %r; my %rsyms;
906 foreach $r (@r) {
907 (my $s, my $i) = split /\\/, $r;
908 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
909 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
910 $r{$a} = $s."\\".$i;
911 $rsyms{$s} = 1;
912 }
913
451e60e9
RL
914 my %syms = ();
915 foreach $_ (@symbols) {
916 (my $n, my $i) = split /\\/;
917 $syms{$n} = 1;
918 }
919
948d0125
RL
920 my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
921 foreach $sym (@s) {
922 (my $n, my $i) = split /\\/, $nums{$sym};
923 next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
924 next if defined($rsyms{$sym});
451e60e9
RL
925 $i="NOEXIST::FUNCTION:"
926 if !defined($i) || $i eq "" || !defined($syms{$sym});
948d0125
RL
927 printf OUT "%s%-40s%d\t%s\n","",$sym,$n,$i;
928 if (exists $r{$sym}) {
929 (my $s, $i) = split /\\/,$r{$sym};
930 printf OUT "%s%-40s%d\t%s\n","",$s,$n,$i;
931 }
932 }
933}
934
55a9cc6e 935sub update_numbers
47339f61 936{
948d0125
RL
937 (*OUT,$name,*nums,my $start_num, my @symbols)=@_;
938 my $new_syms = 0;
939
940 print STDERR "Updating $name numbers\n";
941
942 my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
943 my $r; my %r; my %rsyms;
944 foreach $r (@r) {
945 (my $s, my $i) = split /\\/, $r;
946 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
947 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
948 $r{$a} = $s."\\".$i;
949 $rsyms{$s} = 1;
950 }
951
952 foreach $sym (@symbols) {
953 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
954 next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
955 next if defined($rsyms{$sym});
956 die "ERROR: Symbol $sym had no info attached to it."
957 if $i eq "";
958 if (!exists $nums{$s}) {
959 $new_syms++;
960 printf OUT "%s%-40s%d\t%s\n","",$s, ++$start_num,$i;
961 if (exists $r{$s}) {
33b1a4c2 962 ($s, $i) = split /\\/,$r{$s};
948d0125
RL
963 printf OUT "%s%-40s%d\t%s\n","",$s, $start_num,$i;
964 }
55a9cc6e 965 }
47339f61 966 }
948d0125
RL
967 if($new_syms) {
968 print STDERR "$new_syms New symbols added\n";
55a9cc6e 969 } else {
948d0125
RL
970 print STDERR "No New symbols Added\n";
971 }
972}
973
974sub check_existing
975{
976 (*nums, my @symbols)=@_;
977 my %existing; my @remaining;
978 @remaining=();
979 foreach $sym (@symbols) {
980 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
981 $existing{$s}=1;
982 }
983 foreach $sym (keys %nums) {
984 if (!exists $existing{$sym}) {
985 push @remaining, $sym;
986 }
987 }
988 if(@remaining) {
989 print STDERR "The following symbols do not seem to exist:\n";
990 foreach $sym (@remaining) {
991 print STDERR "\t",$sym,"\n";
992 }
55a9cc6e 993 }
47339f61 994}
948d0125 995