]> git.ipfire.org Git - thirdparty/openssl.git/blame - util/mkdef.pl
MD4 implemented. Assar Westerlund provided the digest code itself and the test utili...
[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
RE
7#
8
0f583f69
UM
9my $crypto_num="util/libeay.num";
10my $ssl_num= "util/ssleay.num";
d02b48c6 11
47339f61
DSH
12my $do_update = 0;
13my $do_crypto = 0;
14my $do_ssl = 0;
0f583f69
UM
15my $do_ctest = 0;
16my $rsaref = 0;
47339f61 17
0f583f69
UM
18my $W32=1;
19my $NT=0;
28a98809 20# Set this to make typesafe STACK definitions appear in DEF
e41c8d6a 21my $safe_stack_def = 0;
31ff97b2 22
0f583f69 23my $options="";
31ff97b2
UM
24open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
25while(<IN>) {
26 $options=$1 if (/^OPTIONS=(.*)$/);
27}
ce457a54 28close(IN);
31ff97b2 29
0f583f69
UM
30# The following ciphers may be excluded (by Configure). This means functions
31# defined with ifndef(NO_XXX) are not included in the .def file, and everything
32# in directory xxx is ignored.
33my $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
3009458e
RL
34my $no_cast;
35my $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
0f583f69
UM
36my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0;
37
31ff97b2 38foreach (@ARGV, split(/ /, $options))
d02b48c6 39 {
06c68491
DSH
40 $W32=1 if $_ eq "32";
41 $W32=0 if $_ eq "16";
42 if($_ eq "NT") {
43 $W32 = 1;
44 $NT = 1;
45 }
d02b48c6 46 $do_ssl=1 if $_ eq "ssleay";
55a9cc6e 47 $do_ssl=1 if $_ eq "ssl";
d02b48c6 48 $do_crypto=1 if $_ eq "libeay";
55a9cc6e
DSH
49 $do_crypto=1 if $_ eq "crypto";
50 $do_update=1 if $_ eq "update";
12aefe78 51 $do_ctest=1 if $_ eq "ctest";
47339f61 52 $rsaref=1 if $_ eq "rsaref";
a8b07aa4 53 #$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
8cf65228
UM
54
55 if (/^no-rc2$/) { $no_rc2=1; }
56 elsif (/^no-rc4$/) { $no_rc4=1; }
57 elsif (/^no-rc5$/) { $no_rc5=1; }
58 elsif (/^no-idea$/) { $no_idea=1; }
59 elsif (/^no-des$/) { $no_des=1; }
60 elsif (/^no-bf$/) { $no_bf=1; }
61 elsif (/^no-cast$/) { $no_cast=1; }
62 elsif (/^no-md2$/) { $no_md2=1; }
3009458e 63 elsif (/^no-md4$/) { $no_md4=1; }
8cf65228
UM
64 elsif (/^no-md5$/) { $no_md5=1; }
65 elsif (/^no-sha$/) { $no_sha=1; }
66 elsif (/^no-ripemd$/) { $no_ripemd=1; }
67 elsif (/^no-mdc2$/) { $no_mdc2=1; }
68 elsif (/^no-rsa$/) { $no_rsa=1; }
69 elsif (/^no-dsa$/) { $no_dsa=1; }
70 elsif (/^no-dh$/) { $no_dh=1; }
71 elsif (/^no-hmac$/) { $no_hmac=1; }
d02b48c6
RE
72 }
73
12aefe78 74
d02b48c6
RE
75if (!$do_ssl && !$do_crypto)
76 {
47339f61 77 print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT ] [rsaref]\n";
d02b48c6
RE
78 exit(1);
79 }
80
81%ssl_list=&load_numbers($ssl_num);
55a9cc6e 82$max_ssl = $max_num;
d02b48c6 83%crypto_list=&load_numbers($crypto_num);
55a9cc6e 84$max_crypto = $max_num;
d02b48c6 85
0f583f69 86my $ssl="ssl/ssl.h";
d02b48c6 87
0f583f69 88my $crypto ="crypto/crypto.h";
8cf65228
UM
89$crypto.=" crypto/des/des.h" unless $no_des;
90$crypto.=" crypto/idea/idea.h" unless $no_idea;
91$crypto.=" crypto/rc4/rc4.h" unless $no_rc4;
92$crypto.=" crypto/rc5/rc5.h" unless $no_rc5;
93$crypto.=" crypto/rc2/rc2.h" unless $no_rc2;
94$crypto.=" crypto/bf/blowfish.h" unless $no_bf;
95$crypto.=" crypto/cast/cast.h" unless $no_cast;
96$crypto.=" crypto/md2/md2.h" unless $no_md2;
3009458e 97$crypto.=" crypto/md4/md4.h" unless $no_md4;
8cf65228
UM
98$crypto.=" crypto/md5/md5.h" unless $no_md5;
99$crypto.=" crypto/mdc2/mdc2.h" unless $no_mdc2;
100$crypto.=" crypto/sha/sha.h" unless $no_sha;
101$crypto.=" crypto/ripemd/ripemd.h" unless $no_ripemd;
d02b48c6
RE
102
103$crypto.=" crypto/bn/bn.h";
8cf65228
UM
104$crypto.=" crypto/rsa/rsa.h" unless $no_rsa;
105$crypto.=" crypto/dsa/dsa.h" unless $no_dsa;
106$crypto.=" crypto/dh/dh.h" unless $no_dh;
107$crypto.=" crypto/hmac/hmac.h" unless $no_hmac;
d02b48c6
RE
108
109$crypto.=" crypto/stack/stack.h";
110$crypto.=" crypto/buffer/buffer.h";
111$crypto.=" crypto/bio/bio.h";
c22e4b19 112$crypto.=" crypto/dso/dso.h";
d02b48c6
RE
113$crypto.=" crypto/lhash/lhash.h";
114$crypto.=" crypto/conf/conf.h";
115$crypto.=" crypto/txt_db/txt_db.h";
116
117$crypto.=" crypto/evp/evp.h";
118$crypto.=" crypto/objects/objects.h";
119$crypto.=" crypto/pem/pem.h";
120#$crypto.=" crypto/meth/meth.h";
121$crypto.=" crypto/asn1/asn1.h";
122$crypto.=" crypto/asn1/asn1_mac.h";
123$crypto.=" crypto/err/err.h";
124$crypto.=" crypto/pkcs7/pkcs7.h";
ee0508d4 125$crypto.=" crypto/pkcs12/pkcs12.h";
d02b48c6
RE
126$crypto.=" crypto/x509/x509.h";
127$crypto.=" crypto/x509/x509_vfy.h";
679ab7c3 128$crypto.=" crypto/x509v3/x509v3.h";
d02b48c6 129$crypto.=" crypto/rand/rand.h";
dfeab068
RE
130$crypto.=" crypto/comp/comp.h";
131$crypto.=" crypto/tmdiff.h";
d02b48c6 132
0f583f69
UM
133my @ssl_func = &do_defs("SSLEAY", $ssl);
134my @crypto_func = &do_defs("LIBEAY", $crypto);
55a9cc6e 135
47339f61 136
55a9cc6e
DSH
137if ($do_update) {
138
139if ($do_ssl == 1) {
140 open(OUT, ">>$ssl_num");
141 &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl, @ssl_func);
142 close OUT;
143}
144
145if($do_crypto == 1) {
146 open(OUT, ">>$crypto_num");
147 &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto, @crypto_func);
148 close OUT;
12aefe78
DSH
149}
150
151} elsif ($do_ctest) {
152
153 print <<"EOF";
154
155/* Test file to check all DEF file symbols are present by trying
156 * to link to all of them. This is *not* intended to be run!
157 */
158
159int main()
160{
161EOF
162 &print_test_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_func)
163 if $do_ssl == 1;
164
165 &print_test_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_func)
166 if $do_crypto == 1;
167
168 print "}\n";
55a9cc6e
DSH
169
170} else {
8cf65228
UM
171
172 &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_func)
55a9cc6e
DSH
173 if $do_ssl == 1;
174
8cf65228 175 &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_func)
55a9cc6e 176 if $do_crypto == 1;
8cf65228 177
55a9cc6e 178}
d02b48c6 179
d02b48c6
RE
180
181sub do_defs
47339f61
DSH
182{
183 my($name,$files)=@_;
0f583f69 184 my $file;
47339f61
DSH
185 my @ret;
186 my %funcs;
0f583f69 187 my $cpp;
d02b48c6 188
d02b48c6
RE
189 foreach $file (split(/\s+/,$files))
190 {
d02b48c6 191 open(IN,"<$file") || die "unable to open $file:$!\n";
0f583f69 192 my $line = "", my $def= "";
47339f61
DSH
193 my %tag = (
194 FreeBSD => 0,
195 NOPROTO => 0,
196 WIN16 => 0,
197 PERL5 => 0,
198 _WINDLL => 0,
199 NO_FP_API => 0,
200 CONST_STRICT => 0,
201 TRUE => 1,
0f583f69
UM
202 NO_RC2 => 0,
203 NO_RC4 => 0,
204 NO_RC5 => 0,
205 NO_IDEA => 0,
206 NO_DES => 0,
207 NO_BF => 0,
208 NO_CAST => 0,
209 NO_MD2 => 0,
3009458e 210 NO_MD4 => 0,
0f583f69
UM
211 NO_MD5 => 0,
212 NO_SHA => 0,
213 NO_RIPEMD => 0,
214 NO_MDC2 => 0,
215 NO_RSA => 0,
216 NO_DSA => 0,
217 NO_DH => 0,
218 NO_HMAC => 0,
47339f61
DSH
219 );
220 while(<IN>) {
221 last if (/BEGIN ERROR CODES/);
222 if ($line ne '') {
223 $_ = $line . $_;
224 $line = '';
d02b48c6 225 }
47339f61
DSH
226
227 if (/\\$/) {
228 $line = $_;
229 next;
230 }
231
232 $cpp = 1 if /^#.*ifdef.*cplusplus/;
233 if ($cpp) {
234 $cpp = 0 if /^#.*endif/;
235 next;
236 }
237
238 s/\/\*.*?\*\///gs; # ignore comments
239 s/{[^{}]*}//gs; # ignore {} blocks
240 if (/^\#\s*ifndef (.*)/) {
d02b48c6
RE
241 push(@tag,$1);
242 $tag{$1}=-1;
243 next;
47339f61 244 } elsif (/^\#\s*if !defined\(([^\)]+)\)/) {
d02b48c6
RE
245 push(@tag,$1);
246 $tag{$1}=-1;
247 next;
47339f61 248 } elsif (/^\#\s*ifdef (.*)/) {
d02b48c6
RE
249 push(@tag,$1);
250 $tag{$1}=1;
251 next;
47339f61 252 } elsif (/^\#\s*if defined(.*)/) {
d02b48c6
RE
253 push(@tag,$1);
254 $tag{$1}=1;
255 next;
47339f61 256 } elsif (/^\#\s*endif/) {
d02b48c6
RE
257 $tag{$tag[$#tag]}=0;
258 pop(@tag);
259 next;
47339f61
DSH
260 } elsif (/^\#\s*else/) {
261 my $t=$tag[$#tag];
d02b48c6
RE
262 $tag{$t}= -$tag{$t};
263 next;
47339f61
DSH
264 } elsif (/^\#\s*if\s+1/) {
265 # Dummy tag
266 push(@tag,"TRUE");
267 $tag{"TRUE"}=1;
268 next;
1e414935
BM
269 } elsif (/^\#\s*if\s+0/) {
270 # Dummy tag
271 push(@tag,"TRUE");
272 $tag{"TRUE"}=-1;
273 next;
47339f61
DSH
274 } elsif (/^\#/) {
275 next;
276 }
13083215 277 if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
a8b07aa4 278 next;
13083215 279 } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
a8b07aa4 280 next;
dbd665c2
DSH
281 } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
282 /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
c22e4b19
UM
283 if (!($no_rsa && ($1 eq "RSAPrivateKey" ||
284 $1 eq "RSAPublicKey" ||
285 $1 eq "RSA_PUBKEY"))) {
286 if($W32) {
287 $funcs{"PEM_read_${1}"} = 1;
288 $funcs{"PEM_write_${1}"} = 1;
289 }
290 $funcs{"PEM_read_bio_${1}"} = 1;
291 $funcs{"PEM_write_bio_${1}"} = 1;
dbd665c2 292 }
8a208cba
DSH
293 } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
294 /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
c22e4b19
UM
295 if (!($no_rsa && ($1 eq "RSAPrivateKey" ||
296 $1 eq "RSAPublicKey" ||
297 $1 eq "RSA_PUBKEY"))) {
298 if($W32) {
299 $funcs{"PEM_write_${1}"} = 1;
300 }
301 $funcs{"PEM_write_bio_${1}"} = 1;
8a208cba 302 }
8a208cba
DSH
303 } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
304 /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
305 if($W32) {
306 $funcs{"PEM_read_${1}"} = 1;
307 }
308 $funcs{"PEM_read_bio_${1}"} = 1;
0f583f69 309 } elsif (
1e414935 310 ($tag{'TRUE'} != -1) &&
d02b48c6 311 ($tag{'FreeBSD'} != 1) &&
47339f61 312 ($tag{'CONST_STRICT'} != 1) &&
06c68491
DSH
313 (($W32 && ($tag{'WIN16'} != 1)) ||
314 (!$W32 && ($tag{'WIN16'} != -1))) &&
58964a49
RE
315 ($tag{'PERL5'} != 1) &&
316# ($tag{'_WINDLL'} != -1) &&
06c68491
DSH
317 ((!$W32 && $tag{'_WINDLL'} != -1) ||
318 ($W32 && $tag{'_WINDLL'} != 1)) &&
319 ((($tag{'NO_FP_API'} != 1) && $W32) ||
0f583f69
UM
320 (($tag{'NO_FP_API'} != -1) && !$W32)) &&
321 ($tag{'NO_RC2'} == 0 || !$no_rc2) &&
322 ($tag{'NO_RC4'} == 0 || !$no_rc4) &&
323 ($tag{'NO_RC5'} == 0 || !$no_rc5) &&
324 ($tag{'NO_IDEA'} == 0 || !$no_idea) &&
325 ($tag{'NO_DES'} == 0 || !$no_des) &&
326 ($tag{'NO_BF'} == 0 || !$no_bf) &&
327 ($tag{'NO_CAST'} == 0 || !$no_cast) &&
328 ($tag{'NO_MD2'} == 0 || !$no_md2) &&
3009458e 329 ($tag{'NO_MD4'} == 0 || !$no_md4) &&
0f583f69
UM
330 ($tag{'NO_MD5'} == 0 || !$no_md5) &&
331 ($tag{'NO_SHA'} == 0 || !$no_sha) &&
332 ($tag{'NO_RIPEMD'} == 0 || !$no_ripemd) &&
333 ($tag{'NO_MDC2'} == 0 || !$no_mdc2) &&
334 ($tag{'NO_RSA'} == 0 || !$no_rsa) &&
335 ($tag{'NO_DSA'} == 0 || !$no_dsa) &&
336 ($tag{'NO_DH'} == 0 || !$no_dh) &&
337 ($tag{'NO_HMAC'} == 0 || !$no_hmac))
d02b48c6 338 {
47339f61
DSH
339 if (/{|\/\*/) { # }
340 $line = $_;
341 } else {
342 $def .= $_;
343 }
d02b48c6
RE
344 }
345 }
346 close(IN);
47339f61
DSH
347
348 foreach (split /;/, $def) {
349 s/^[\n\s]*//g;
350 s/[\n\s]*$//g;
e41c8d6a 351 next if(/#define/);
47339f61 352 next if(/typedef\W/);
8cf65228
UM
353 next if(/EVP_bf/ and $no_bf);
354 next if(/EVP_cast/ and $no_cast);
355 next if(/EVP_des/ and $no_des);
356 next if(/EVP_dss/ and $no_dsa);
357 next if(/EVP_idea/ and $no_idea);
358 next if(/EVP_md2/ and $no_md2);
3009458e 359 next if(/EVP_md4/ and $no_md4);
8cf65228
UM
360 next if(/EVP_md5/ and $no_md5);
361 next if(/EVP_rc2/ and $no_rc2);
362 next if(/EVP_rc4/ and $no_rc4);
363 next if(/EVP_rc5/ and $no_rc5);
364 next if(/EVP_ripemd/ and $no_ripemd);
365 next if(/EVP_sha/ and $no_sha);
c22e4b19
UM
366 next if(/EVP_(Open|Seal)(Final|Init)/ and $no_rsa);
367 next if(/PEM_Seal(Final|Init|Update)/ and $no_rsa);
368 next if(/RSAPrivateKey/ and $no_rsa);
369 next if(/SSLv23?_((client|server)_)?method/ and $no_rsa);
370
47339f61
DSH
371 if (/\(\*(\w*)\([^\)]+/) {
372 $funcs{$1} = 1;
373 } elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
374 # K&R C
375 next;
376 } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
377 while (not /\(\)$/s) {
378 s/[^\(\)]*\)$/\)/s;
379 s/\([^\(\)]*\)\)$/\)/s;
380 }
381 s/\(void\)//;
382 /(\w+)\W*\(\)/s;
383 $funcs{$1} = 1;
384 } elsif (/\(/ and not (/=/)) {
385 print STDERR "File $file: cannot parse: $_;\n";
386 }
d02b48c6 387 }
d02b48c6
RE
388 }
389
47339f61
DSH
390 # Prune the returned functions
391
392 delete $funcs{"SSL_add_dir_cert_subjects_to_stack"};
47339f61 393 delete $funcs{"RSA_PKCS1_RSAref"} unless $rsaref;
9b95f1df 394 delete $funcs{"bn_dump1"};
47339f61
DSH
395
396 if($W32) {
397 delete $funcs{"BIO_s_file_internal"};
398 delete $funcs{"BIO_new_file_internal"};
399 delete $funcs{"BIO_new_fp_internal"};
400 } else {
401 if(exists $funcs{"ERR_load_CRYPTO_strings"}) {
402 delete $funcs{"ERR_load_CRYPTO_strings"};
403 $funcs{"ERR_load_CRYPTOlib_strings"} = 1;
d02b48c6 404 }
47339f61
DSH
405 delete $funcs{"BIO_s_file"};
406 delete $funcs{"BIO_new_file"};
407 delete $funcs{"BIO_new_fp"};
408 }
409 if (!$NT) {
410 delete $funcs{"BIO_s_log"};
d02b48c6
RE
411 }
412
47339f61 413 push @ret, keys %funcs;
d02b48c6 414
47339f61
DSH
415 return(@ret);
416}
d02b48c6 417
12aefe78
DSH
418sub print_test_file
419{
0f583f69
UM
420 (*OUT,my $name,*nums,my @functions)=@_;
421 my $n = 1; my @e; my @r;
422 my $func;
12aefe78
DSH
423
424 (@e)=grep(/^SSLeay/,@functions);
425 (@r)=grep(!/^SSLeay/,@functions);
426 @functions=((sort @e),(sort @r));
427
428 foreach $func (@functions) {
429 if (!defined($nums{$func})) {
430 printf STDERR "$func does not have a number assigned\n"
431 if(!$do_update);
432 } else {
433 $n=$nums{$func};
434 print OUT "\t$func();\n";
435 }
436 }
437}
438
d02b48c6 439sub print_def_file
47339f61 440{
0f583f69
UM
441 (*OUT,my $name,*nums,my @functions)=@_;
442 my $n = 1; my @e; my @r;
d02b48c6 443
06c68491 444 if ($W32)
d02b48c6
RE
445 { $name.="32"; }
446 else
447 { $name.="16"; }
448
449 print OUT <<"EOF";
450;
9b3086fe 451; Definition file for the DLL version of the $name library from OpenSSL
d02b48c6
RE
452;
453
454LIBRARY $name
455
9b3086fe 456DESCRIPTION 'OpenSSL $name - http://www.openssl.org/'
d02b48c6
RE
457
458EOF
459
47339f61 460 if (!$W32) {
d02b48c6
RE
461 print <<"EOF";
462CODE PRELOAD MOVEABLE
463DATA PRELOAD MOVEABLE SINGLE
464
465EXETYPE WINDOWS
466
467HEAPSIZE 4096
468STACKSIZE 8192
469
470EOF
47339f61 471 }
d02b48c6
RE
472
473 print "EXPORTS\n";
474
475
476 (@e)=grep(/^SSLeay/,@functions);
477 (@r)=grep(!/^SSLeay/,@functions);
478 @functions=((sort @e),(sort @r));
479
47339f61
DSH
480 foreach $func (@functions) {
481 if (!defined($nums{$func})) {
8cf65228
UM
482 printf STDERR "$func does not have a number assigned\n"
483 if(!$do_update);
47339f61 484 } else {
d02b48c6 485 $n=$nums{$func};
06c68491 486 printf OUT " %s%-40s@%d\n",($W32)?"":"_",$func,$n;
d02b48c6 487 }
d02b48c6 488 }
47339f61
DSH
489 printf OUT "\n";
490}
d02b48c6
RE
491
492sub load_numbers
47339f61
DSH
493{
494 my($name)=@_;
495 my(@a,%ret);
d02b48c6 496
55a9cc6e
DSH
497 $max_num = 0;
498
d02b48c6 499 open(IN,"<$name") || die "unable to open $name:$!\n";
47339f61 500 while (<IN>) {
d02b48c6
RE
501 chop;
502 s/#.*$//;
503 next if /^\s*$/;
504 @a=split;
505 $ret{$a[0]}=$a[1];
55a9cc6e 506 $max_num = $a[1] if $a[1] > $max_num;
47339f61 507 }
d02b48c6
RE
508 close(IN);
509 return(%ret);
47339f61 510}
55a9cc6e
DSH
511
512sub update_numbers
47339f61
DSH
513{
514 (*OUT,$name,*nums,my $start_num, my @functions)=@_;
55a9cc6e
DSH
515 my $new_funcs = 0;
516 print STDERR "Updating $name\n";
47339f61
DSH
517 foreach $func (@functions) {
518 if (!exists $nums{$func}) {
55a9cc6e
DSH
519 $new_funcs++;
520 printf OUT "%s%-40s%d\n","",$func, ++$start_num;
55a9cc6e 521 }
47339f61 522 }
55a9cc6e
DSH
523 if($new_funcs) {
524 print STDERR "$new_funcs New Functions added\n";
525 } else {
526 print STDERR "No New Functions Added\n";
527 }
47339f61 528}