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