629982a384bb3b84007d8e628ebea3f180477200
[openssl.git] / util / mkdef.pl
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
9 $crypto_num="util/libeay.num";
10 $ssl_num=   "util/ssleay.num";
11
12 my $do_update = 0;
13 my $do_crypto = 0;
14 my $do_ssl = 0;
15 $rsaref = 0;
16
17 $W32=1;
18 $NT=0;
19 # Set this to make typesafe STACK definitions appear in DEF
20 $safe_stack_def = 1;
21
22 $options="";
23 open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
24 while(<IN>) {
25     $options=$1 if (/^OPTIONS=(.*)$/);
26 }
27 close(IN);
28
29 foreach (@ARGV, split(/ /, $options))
30         {
31         $W32=1 if $_ eq "32";
32         $W32=0 if $_ eq "16";
33         if($_ eq "NT") {
34                 $W32 = 1;
35                 $NT = 1;
36         }
37         $do_ssl=1 if $_ eq "ssleay";
38         $do_ssl=1 if $_ eq "ssl";
39         $do_crypto=1 if $_ eq "libeay";
40         $do_crypto=1 if $_ eq "crypto";
41         $do_update=1 if $_ eq "update";
42         $rsaref=1 if $_ eq "rsaref";
43
44         if    (/^no-rc2$/)      { $no_rc2=1; }
45         elsif (/^no-rc4$/)      { $no_rc4=1; }
46         elsif (/^no-rc5$/)      { $no_rc5=1; }
47         elsif (/^no-idea$/)     { $no_idea=1; }
48         elsif (/^no-des$/)      { $no_des=1; }
49         elsif (/^no-bf$/)       { $no_bf=1; }
50         elsif (/^no-cast$/)     { $no_cast=1; }
51         elsif (/^no-md2$/)      { $no_md2=1; }
52         elsif (/^no-md5$/)      { $no_md5=1; }
53         elsif (/^no-sha$/)      { $no_sha=1; }
54         elsif (/^no-ripemd$/)   { $no_ripemd=1; }
55         elsif (/^no-mdc2$/)     { $no_mdc2=1; }
56         elsif (/^no-rsa$/)      { $no_rsa=1; }
57         elsif (/^no-dsa$/)      { $no_dsa=1; }
58         elsif (/^no-dh$/)       { $no_dh=1; }
59         elsif (/^no-hmac$/)     { $no_hmac=1; }
60         }
61
62 if (!$do_ssl && !$do_crypto)
63         {
64         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT ] [rsaref]\n";
65         exit(1);
66         }
67
68 %ssl_list=&load_numbers($ssl_num);
69 $max_ssl = $max_num;
70 %crypto_list=&load_numbers($crypto_num);
71 $max_crypto = $max_num;
72
73 $ssl="ssl/ssl.h";
74
75 $crypto ="crypto/crypto.h";
76 $crypto.=" crypto/des/des.h" unless $no_des;
77 $crypto.=" crypto/idea/idea.h" unless $no_idea;
78 $crypto.=" crypto/rc4/rc4.h" unless $no_rc4;
79 $crypto.=" crypto/rc5/rc5.h" unless $no_rc5;
80 $crypto.=" crypto/rc2/rc2.h" unless $no_rc2;
81 $crypto.=" crypto/bf/blowfish.h" unless $no_bf;
82 $crypto.=" crypto/cast/cast.h" unless $no_cast;
83 $crypto.=" crypto/md2/md2.h" unless $no_md2;
84 $crypto.=" crypto/md5/md5.h" unless $no_md5;
85 $crypto.=" crypto/mdc2/mdc2.h" unless $no_mdc2;
86 $crypto.=" crypto/sha/sha.h" unless $no_sha;
87 $crypto.=" crypto/ripemd/ripemd.h" unless $no_ripemd;
88
89 $crypto.=" crypto/bn/bn.h";
90 $crypto.=" crypto/rsa/rsa.h" unless $no_rsa;
91 $crypto.=" crypto/dsa/dsa.h" unless $no_dsa;
92 $crypto.=" crypto/dh/dh.h" unless $no_dh;
93 $crypto.=" crypto/hmac/hmac.h" unless $no_hmac;
94
95 $crypto.=" crypto/stack/stack.h";
96 $crypto.=" crypto/buffer/buffer.h";
97 $crypto.=" crypto/bio/bio.h";
98 $crypto.=" crypto/lhash/lhash.h";
99 $crypto.=" crypto/conf/conf.h";
100 $crypto.=" crypto/txt_db/txt_db.h";
101
102 $crypto.=" crypto/evp/evp.h";
103 $crypto.=" crypto/objects/objects.h";
104 $crypto.=" crypto/pem/pem.h";
105 #$crypto.=" crypto/meth/meth.h";
106 $crypto.=" crypto/asn1/asn1.h";
107 $crypto.=" crypto/asn1/asn1_mac.h";
108 $crypto.=" crypto/err/err.h";
109 $crypto.=" crypto/pkcs7/pkcs7.h";
110 $crypto.=" crypto/pkcs12/pkcs12.h";
111 $crypto.=" crypto/x509/x509.h";
112 $crypto.=" crypto/x509/x509_vfy.h";
113 $crypto.=" crypto/x509v3/x509v3.h";
114 $crypto.=" crypto/rand/rand.h";
115 $crypto.=" crypto/comp/comp.h";
116 $crypto.=" crypto/tmdiff.h";
117
118 @ssl_func = &do_defs("SSLEAY", $ssl);
119 @crypto_func = &do_defs("LIBEAY", $crypto);
120
121
122 if ($do_update) {
123
124 if ($do_ssl == 1) {
125         open(OUT, ">>$ssl_num");
126         &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl, @ssl_func);
127         close OUT;
128 }
129
130 if($do_crypto == 1) {
131         open(OUT, ">>$crypto_num");
132         &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto, @crypto_func);
133         close OUT;
134 }
135
136 } else {
137
138         &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_func)
139                 if $do_ssl == 1;
140
141         &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_func)
142                 if $do_crypto == 1;
143
144 }
145
146
147 sub do_defs
148 {
149         my($name,$files)=@_;
150         my @ret;
151         my %funcs;
152
153         foreach $file (split(/\s+/,$files))
154                 {
155                 open(IN,"<$file") || die "unable to open $file:$!\n";
156
157                 my $line = "", $def= "";
158                 my %tag = (
159                         FreeBSD         => 0,
160                         NOPROTO         => 0,
161                         WIN16           => 0,
162                         PERL5           => 0,
163                         _WINDLL         => 0,
164                         NO_FP_API       => 0,
165                         CONST_STRICT    => 0,
166                         TRUE            => 1,
167                 );
168                 while(<IN>) {
169                         last if (/BEGIN ERROR CODES/);
170                         if ($line ne '') {
171                                 $_ = $line . $_;
172                                 $line = '';
173                         }
174
175                         if (/\\$/) {
176                                 $line = $_;
177                                 next;
178                         }
179
180                         $cpp = 1 if /^#.*ifdef.*cplusplus/;
181                         if ($cpp) {
182                                 $cpp = 0 if /^#.*endif/;
183                                 next;
184                         }
185
186                         s/\/\*.*?\*\///gs;                   # ignore comments
187                         s/{[^{}]*}//gs;                      # ignore {} blocks
188                         if (/^\#\s*ifndef (.*)/) {
189                                 push(@tag,$1);
190                                 $tag{$1}=-1;
191                                 next;
192                         } elsif (/^\#\s*if !defined\(([^\)]+)\)/) {
193                                 push(@tag,$1);
194                                 $tag{$1}=-1;
195                                 next;
196                         } elsif (/^\#\s*ifdef (.*)/) {
197                                 push(@tag,$1);
198                                 $tag{$1}=1;
199                                 next;
200                         } elsif (/^\#\s*if defined(.*)/) {
201                                 push(@tag,$1);
202                                 $tag{$1}=1;
203                                 next;
204                         } elsif (/^\#\s*endif/) {
205                                 $tag{$tag[$#tag]}=0;
206                                 pop(@tag);
207                                 next;
208                         } elsif (/^\#\s*else/) {
209                                 my $t=$tag[$#tag];
210                                 $tag{$t}= -$tag{$t};
211                                 next;
212                         } elsif (/^\#\s*if\s+1/) {
213                                 # Dummy tag
214                                 push(@tag,"TRUE");
215                                 $tag{"TRUE"}=1;
216                                 next;
217                         } elsif (/^\#\s*if\s+0/) {
218                                 # Dummy tag
219                                 push(@tag,"TRUE");
220                                 $tag{"TRUE"}=-1;
221                                 next;
222                         } elsif (/^\#/) {
223                                 next;
224                         }
225                         if ($safe_stack_def &&
226                                 /^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
227                                 $funcs{"sk_${1}_new"} = 1;
228                                 $funcs{"sk_${1}_new_null"} = 1;
229                                 $funcs{"sk_${1}_free"} = 1;
230                                 $funcs{"sk_${1}_num"} = 1;
231                                 $funcs{"sk_${1}_value"} = 1;
232                                 $funcs{"sk_${1}_set"} = 1;
233                                 $funcs{"sk_${1}_zero"} = 1;
234                                 $funcs{"sk_${1}_push"} = 1;
235                                 $funcs{"sk_${1}_unshift"} = 1;
236                                 $funcs{"sk_${1}_find"} = 1;
237                                 $funcs{"sk_${1}_delete"} = 1;
238                                 $funcs{"sk_${1}_delete_ptr"} = 1;
239                                 $funcs{"sk_${1}_insert"} = 1;
240                                 $funcs{"sk_${1}_set_cmp_func"} = 1;
241                                 $funcs{"sk_${1}_dup"} = 1;
242                                 $funcs{"sk_${1}_pop_free"} = 1;
243                                 $funcs{"sk_${1}_shift"} = 1;
244                                 $funcs{"sk_${1}_pop"} = 1;
245                                 $funcs{"sk_${1}_sort"} = 1;
246                         } elsif ($safe_stack_def &&
247                                 /^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
248                                 $funcs{"d2i_ASN1_SET_OF_${1}"} = 1;
249                                 $funcs{"i2d_ASN1_SET_OF_${1}"} = 1;
250                         } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
251                                      /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
252                                 if($W32) {
253                                         $funcs{"PEM_read_${1}"} = 1;
254                                         $funcs{"PEM_write_${1}"} = 1;
255                                 }
256                                 $funcs{"PEM_read_bio_${1}"} = 1;
257                                 $funcs{"PEM_write_bio_${1}"} = 1;
258                         } elsif ( 
259                                 ($tag{'TRUE'} != -1) &&
260                                 ($tag{'FreeBSD'} != 1) &&
261                                 ($tag{'CONST_STRICT'} != 1) &&
262                                 (($W32 && ($tag{'WIN16'} != 1)) ||
263                                  (!$W32 && ($tag{'WIN16'} != -1))) &&
264                                 ($tag{'PERL5'} != 1) &&
265 #                               ($tag{'_WINDLL'} != -1) &&
266                                 ((!$W32 && $tag{'_WINDLL'} != -1) ||
267                                  ($W32 && $tag{'_WINDLL'} != 1)) &&
268                                 ((($tag{'NO_FP_API'} != 1) && $W32) ||
269                                  (($tag{'NO_FP_API'} != -1) && !$W32)))
270                                 {
271                                         if (/{|\/\*/) { # }
272                                                 $line = $_;
273                                         } else {
274                                                 $def .= $_;
275                                         }
276                                 }
277                         }
278                 close(IN);
279
280                 foreach (split /;/, $def) {
281                         s/^[\n\s]*//g;
282                         s/[\n\s]*$//g;
283                         next if(/typedef\W/);
284                         next if(/EVP_bf/ and $no_bf);
285                         next if(/EVP_cast/ and $no_cast);
286                         next if(/EVP_des/ and $no_des);
287                         next if(/EVP_dss/ and $no_dsa);
288                         next if(/EVP_idea/ and $no_idea);
289                         next if(/EVP_md2/ and $no_md2);
290                         next if(/EVP_md5/ and $no_md5);
291                         next if(/EVP_rc2/ and $no_rc2);
292                         next if(/EVP_rc4/ and $no_rc4);
293                         next if(/EVP_rc5/ and $no_rc5);
294                         next if(/EVP_ripemd/ and $no_ripemd);
295                         next if(/EVP_sha/ and $no_sha);
296                         if (/\(\*(\w*)\([^\)]+/) {
297                                 $funcs{$1} = 1;
298                         } elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
299                                 # K&R C
300                                 next;
301                         } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
302                                 while (not /\(\)$/s) {
303                                         s/[^\(\)]*\)$/\)/s;
304                                         s/\([^\(\)]*\)\)$/\)/s;
305                                 }
306                                 s/\(void\)//;
307                                 /(\w+)\W*\(\)/s;
308                                 $funcs{$1} = 1;
309                         } elsif (/\(/ and not (/=/)) {
310                                 print STDERR "File $file: cannot parse: $_;\n";
311                         }
312                 }
313         }
314
315         # Prune the returned functions
316
317         delete $funcs{"SSL_add_dir_cert_subjects_to_stack"};
318         delete $funcs{"des_crypt"};
319         delete $funcs{"RSA_PKCS1_RSAref"} unless $rsaref;
320
321         if($W32) {
322                 delete $funcs{"BIO_s_file_internal"};
323                 delete $funcs{"BIO_new_file_internal"};
324                 delete $funcs{"BIO_new_fp_internal"};
325         } else {
326                 if(exists $funcs{"ERR_load_CRYPTO_strings"}) {
327                         delete $funcs{"ERR_load_CRYPTO_strings"};
328                         $funcs{"ERR_load_CRYPTOlib_strings"} = 1;
329                 }
330                 delete $funcs{"BIO_s_file"};
331                 delete $funcs{"BIO_new_file"};
332                 delete $funcs{"BIO_new_fp"};
333         }
334         if (!$NT) {
335                 delete $funcs{"BIO_s_log"};
336         }
337
338         push @ret, keys %funcs;
339
340         return(@ret);
341 }
342
343 sub print_def_file
344 {
345         (*OUT,my $name,*nums,@functions)=@_;
346         my $n =1;
347
348         if ($W32)
349                 { $name.="32"; }
350         else
351                 { $name.="16"; }
352
353         print OUT <<"EOF";
354 ;
355 ; Definition file for the DLL version of the $name library from OpenSSL
356 ;
357
358 LIBRARY         $name
359
360 DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
361
362 EOF
363
364         if (!$W32) {
365                 print <<"EOF";
366 CODE            PRELOAD MOVEABLE
367 DATA            PRELOAD MOVEABLE SINGLE
368
369 EXETYPE         WINDOWS
370
371 HEAPSIZE        4096
372 STACKSIZE       8192
373
374 EOF
375         }
376
377         print "EXPORTS\n";
378
379
380         (@e)=grep(/^SSLeay/,@functions);
381         (@r)=grep(!/^SSLeay/,@functions);
382         @functions=((sort @e),(sort @r));
383
384         foreach $func (@functions) {
385                 if (!defined($nums{$func})) {
386                         printf STDERR "$func does not have a number assigned\n"
387                                         if(!$do_update);
388                 } else {
389                         $n=$nums{$func};
390                         printf OUT "    %s%-40s@%d\n",($W32)?"":"_",$func,$n;
391                 }
392         }
393         printf OUT "\n";
394 }
395
396 sub load_numbers
397 {
398         my($name)=@_;
399         my(@a,%ret);
400
401         $max_num = 0;
402
403         open(IN,"<$name") || die "unable to open $name:$!\n";
404         while (<IN>) {
405                 chop;
406                 s/#.*$//;
407                 next if /^\s*$/;
408                 @a=split;
409                 $ret{$a[0]}=$a[1];
410                 $max_num = $a[1] if $a[1] > $max_num;
411         }
412         close(IN);
413         return(%ret);
414 }
415
416 sub update_numbers
417 {
418         (*OUT,$name,*nums,my $start_num, my @functions)=@_;
419         my $new_funcs = 0;
420         print STDERR "Updating $name\n";
421         foreach $func (@functions) {
422                 if (!exists $nums{$func}) {
423                         $new_funcs++;
424                         printf OUT "%s%-40s%d\n","",$func, ++$start_num;
425                 }
426         }
427         if($new_funcs) {
428                 print STDERR "$new_funcs New Functions added\n";
429         } else {
430                 print STDERR "No New Functions Added\n";
431         }
432 }