Various fixes so Win32 compile may work. Convert GeneralNames to use safe stack.
[openssl.git] / util / mkdef.pl
1 #!/usr/local/bin/perl
2 #
3 # generate a .def file
4 #
5 # It does this by parsing the header files and looking for the
6 # non-prototyped functions.
7 #
8
9 $crypto_num="util/libeay.num";
10 $ssl_num=   "util/ssleay.num";
11
12 $W32=1;
13 $NT=0;
14 # Set this to make typesafe STACK definitions appear in DEF
15 $safe_stack_def = 1;
16 foreach (@ARGV)
17         {
18         $W32=1 if $_ eq "32";
19         $W32=0 if $_ eq "16";
20         if($_ eq "NT") {
21                 $W32 = 1;
22                 $NT = 1;
23         }
24         $do_ssl=1 if $_ eq "ssleay";
25         $do_ssl=1 if $_ eq "ssl";
26         $do_crypto=1 if $_ eq "libeay";
27         $do_crypto=1 if $_ eq "crypto";
28         $do_update=1 if $_ eq "update";
29         }
30
31 if (!$do_ssl && !$do_crypto)
32         {
33         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 ]\n";
34         exit(1);
35         }
36
37 %ssl_list=&load_numbers($ssl_num);
38 $max_ssl = $max_num;
39 %crypto_list=&load_numbers($crypto_num);
40 $max_crypto = $max_num;
41
42 $ssl="ssl/ssl.h";
43
44 $crypto ="crypto/crypto.h";
45 $crypto.=" crypto/des/des.h";
46 $crypto.=" crypto/idea/idea.h";
47 $crypto.=" crypto/rc4/rc4.h";
48 $crypto.=" crypto/rc5/rc5.h";
49 $crypto.=" crypto/rc2/rc2.h";
50 $crypto.=" crypto/bf/blowfish.h";
51 $crypto.=" crypto/cast/cast.h";
52 $crypto.=" crypto/md2/md2.h";
53 $crypto.=" crypto/md5/md5.h";
54 $crypto.=" crypto/mdc2/mdc2.h";
55 $crypto.=" crypto/sha/sha.h";
56 $crypto.=" crypto/ripemd/ripemd.h";
57
58 $crypto.=" crypto/bn/bn.h";
59 $crypto.=" crypto/rsa/rsa.h";
60 $crypto.=" crypto/dsa/dsa.h";
61 $crypto.=" crypto/dh/dh.h";
62
63 $crypto.=" crypto/stack/stack.h";
64 $crypto.=" crypto/buffer/buffer.h";
65 $crypto.=" crypto/bio/bio.h";
66 $crypto.=" crypto/lhash/lhash.h";
67 $crypto.=" crypto/conf/conf.h";
68 $crypto.=" crypto/txt_db/txt_db.h";
69
70 $crypto.=" crypto/evp/evp.h";
71 $crypto.=" crypto/objects/objects.h";
72 $crypto.=" crypto/pem/pem.h";
73 #$crypto.=" crypto/meth/meth.h";
74 $crypto.=" crypto/asn1/asn1.h";
75 $crypto.=" crypto/asn1/asn1_mac.h";
76 $crypto.=" crypto/err/err.h";
77 $crypto.=" crypto/pkcs7/pkcs7.h";
78 $crypto.=" crypto/pkcs12/pkcs12.h";
79 $crypto.=" crypto/x509/x509.h";
80 $crypto.=" crypto/x509/x509_vfy.h";
81 $crypto.=" crypto/x509v3/x509v3.h";
82 $crypto.=" crypto/rand/rand.h";
83 $crypto.=" crypto/hmac/hmac.h";
84 $crypto.=" crypto/comp/comp.h";
85 $crypto.=" crypto/tmdiff.h";
86
87 $match{'NOPROTO'}=1;
88 $match2{'PERL5'}=1;
89
90 @ssl_func = &do_defs("SSLEAY", $ssl);
91 @crypto_func = &do_defs("LIBEAY", $crypto);
92
93 if ($do_update) {
94
95 if ($do_ssl == 1) {
96         open(OUT, ">>$ssl_num");
97         &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl, @ssl_func);
98         close OUT;
99 }
100
101 if($do_crypto == 1) {
102         open(OUT, ">>$crypto_num");
103         &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto, @crypto_func);
104         close OUT;
105 }
106
107 } else {
108
109         &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_func)
110                 if $do_ssl == 1;
111
112         &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_func)
113                 if $do_crypto == 1;
114
115 }
116
117
118 sub do_defs
119         {
120         local($name,$files)=@_;
121         local(@ret);
122
123         $off=-1;
124         foreach $file (split(/\s+/,$files))
125                 {
126 #               print STDERR "reading $file\n";
127                 open(IN,"<$file") || die "unable to open $file:$!\n";
128                 $depth=0;
129                 $pr=-1;
130                 @np="";
131                 $/=undef;
132                 $a=<IN>;
133                 while (($i=index($a,"/*")) >= 0)
134                         {
135                         $j=index($a,"*/");
136                         last unless ($j >= 0);
137                         $a=substr($a,0,$i).substr($a,$j+2);
138                 #       print "$i $j\n";
139                         }
140                 foreach (split("\n",$a))
141                         {
142                         if (/^\#\s*ifndef (.*)/)
143                                 {
144                                 push(@tag,$1);
145                                 $tag{$1}=-1;
146                                 next;
147                                 }
148                         elsif (/^\#\s*if !defined\(([^\)]+)\)/)
149                                 {
150                                 push(@tag,$1);
151                                 $tag{$1}=-1;
152                                 next;
153                                 }
154                         elsif (/^\#\s*ifdef (.*)/)
155                                 {
156                                 push(@tag,$1);
157                                 $tag{$1}=1;
158                                 next;
159                                 }
160                         elsif (/^\#\s*if defined(.*)/)
161                                 {
162                                 push(@tag,$1);
163                                 $tag{$1}=1;
164                                 next;
165                                 }
166                         elsif (/^\#\s*endif/)
167                                 {
168                                 $tag{$tag[$#tag]}=0;
169                                 pop(@tag);
170                                 next;
171                                 }
172                         elsif (/^\#\s*else/)
173                                 {
174                                 $t=$tag[$#tag];
175                                 $tag{$t}= -$tag{$t};
176                                 next;
177                                 }
178 #printf STDERR "$_\n%2d %2d %2d %2d %2d $W32\n",
179 #$tag{'NOPROTO'},$tag{'FreeBSD'},$tag{'WIN16'},$tag{'PERL5'},$tag{'NO_FP_API'};
180
181                         $t=undef;
182                         if (/^extern .*;$/)
183                                 { $t=&do_extern($name,$_); }
184                         elsif ($safe_stack_def &&
185                                 /^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/)
186                                 {
187                                 push(@ret,"sk_${1}_new");
188                                 push(@ret,"sk_${1}_new_null");
189                                 push(@ret,"sk_${1}_free");
190                                 push(@ret,"sk_${1}_num");
191                                 push(@ret,"sk_${1}_value");
192                                 push(@ret,"sk_${1}_set");
193                                 push(@ret,"sk_${1}_zero");
194                                 push(@ret,"sk_${1}_push");
195                                 push(@ret,"sk_${1}_pop");
196                                 push(@ret,"sk_${1}_find");
197                                 push(@ret,"sk_${1}_delete");
198                                 push(@ret,"sk_${1}_delete_ptr");
199                                 push(@ret,"sk_${1}_set_cmp_func");
200                                 push(@ret,"sk_${1}_dup");
201                                 push(@ret,"sk_${1}_pop_free");
202                                 push(@ret,"sk_${1}_shift");
203                                 }
204                         elsif ($safe_stack_def &&
205                                 /^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/)
206                                 {
207                                 push(@ret,"d2i_ASN1_SET_OF_${1}");
208                                 push(@ret,"i2d_ASN1_SET_OF_${1}");
209                                 }
210                         elsif (($tag{'NOPROTO'} == 1) &&
211                                 ($tag{'FreeBSD'} != 1) &&
212                                 (($W32 && ($tag{'WIN16'} != 1)) ||
213                                  (!$W32 && ($tag{'WIN16'} != -1))) &&
214                                 ($tag{'PERL5'} != 1) &&
215 #                               ($tag{'_WINDLL'} != -1) &&
216                                 ((!$W32 && $tag{'_WINDLL'} != -1) ||
217                                  ($W32 && $tag{'_WINDLL'} != 1)) &&
218                                 ((($tag{'NO_FP_API'} != 1) && $W32) ||
219                                  (($tag{'NO_FP_API'} != -1) && !$W32)))
220                                 { $t=&do_line($name,$_); }
221                         else
222                                 { $t=undef; }
223                         if (($t ne undef) && (!$done{$name,$t}))
224                                 {
225                                 $done{$name,$t}++;
226                                 push(@ret,$t);
227 #printf STDERR "one:$t\n" if $t =~ /BIO_/;
228                                 }
229                         }
230                 close(IN);
231                 }
232         return(@ret);
233         }
234
235 sub do_line
236         {
237         local($file,$_)=@_;
238         local($n);
239
240         return(undef) if /^$/;
241         return(undef) if /^\s/;
242 #printf STDERR "two:$_\n" if $_ =~ /BIO_/;
243         if (/(CRYPTO_get_locking_callback)/)
244                 { return($1); }
245         elsif (/(CRYPTO_get_id_callback)/)
246                 { return($1); }
247         elsif (/(CRYPTO_get_add_lock_callback)/)
248                 { return($1); }
249         elsif (/(SSL_CTX_get_verify_callback)/)
250                 { return($1); }
251         elsif (/(SSL_get_info_callback)/)
252                 { return($1); }
253         elsif ((!$W32) && /(ERR_load_CRYPTO_strings)/)
254                 { return("ERR_load_CRYPTOlib_strings"); }
255         elsif (!$W32 && /BIO_s_file/)
256                 { return(undef); }
257         elsif (!$W32 && /BIO_new_file/)
258                 { return(undef); }
259         elsif (!$W32 && /BIO_new_fp/)
260                 { return(undef); }
261         elsif ($W32 && /BIO_s_file_internal/)
262                 { return(undef); }
263         elsif ($W32 && /BIO_new_file_internal/)
264                 { return(undef); }
265         elsif ($W32 && /BIO_new_fp_internal/)
266                 { return(undef); }
267         elsif (/SSL_add_dir_cert_subjects_to_stack/)
268                 { return(undef); }
269         elsif (!$NT && /BIO_s_log/)
270                 { return(undef); }
271         else
272                 {
273                 /\s\**(\S+)\s*\(/;
274                 $_ = $1;
275                 tr/()*//d;
276 #print STDERR "$1 : $_\n";
277                 return($_);
278                 }
279         }
280
281 sub do_extern
282         {
283         local($file,$_)=@_;
284         local($n);
285
286         /\s\**(\S+);$/;
287         return($1);
288         }
289
290 sub print_def_file
291         {
292         local(*OUT,$name,*nums,@functions)=@_;
293         local($n)=1;
294
295         if ($W32)
296                 { $name.="32"; }
297         else
298                 { $name.="16"; }
299
300         print OUT <<"EOF";
301 ;
302 ; Definition file for the DLL version of the $name library from OpenSSL
303 ;
304
305 LIBRARY         $name
306
307 DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
308
309 EOF
310
311         if (!$W32)
312                 {
313                 print <<"EOF";
314 CODE            PRELOAD MOVEABLE
315 DATA            PRELOAD MOVEABLE SINGLE
316
317 EXETYPE         WINDOWS
318
319 HEAPSIZE        4096
320 STACKSIZE       8192
321
322 EOF
323                 }
324
325         print "EXPORTS\n";
326
327
328         (@e)=grep(/^SSLeay/,@functions);
329         (@r)=grep(!/^SSLeay/,@functions);
330         @functions=((sort @e),(sort @r));
331
332         foreach $func (@functions)
333                 {
334                 if (!defined($nums{$func}))
335                         {
336                         printf STDERR "$func does not have a number assigned\n"
337                                         if(!$do_update);
338                         }
339                 else
340                         {
341                         $n=$nums{$func};
342                         printf OUT "    %s%-40s@%d\n",($W32)?"":"_",$func,$n;
343                         }
344                 }
345         printf OUT "\n";
346         }
347
348 sub load_numbers
349         {
350         local($name)=@_;
351         local($j,@a,%ret);
352
353         $max_num = 0;
354
355         open(IN,"<$name") || die "unable to open $name:$!\n";
356         while (<IN>)
357                 {
358                 chop;
359                 s/#.*$//;
360                 next if /^\s*$/;
361                 @a=split;
362                 $ret{$a[0]}=$a[1];
363                 $max_num = $a[1] if $a[1] > $max_num;
364                 }
365         close(IN);
366         return(%ret);
367         }
368
369 sub update_numbers
370         {
371         local(*OUT,$name,*nums,$start_num, @functions)=@_;
372         my $new_funcs = 0;
373         print STDERR "Updating $name\n";
374         foreach $func (@functions)
375                 {
376                 if (!defined($nums{$func}))
377                         {
378                         $new_funcs++;
379                         printf OUT "%s%-40s%d\n","",$func, ++$start_num;
380                         }
381                 }
382         if($new_funcs) {
383                 print STDERR "$new_funcs New Functions added\n";
384         } else {
385                 print STDERR "No New Functions Added\n";
386         }
387         }