Switch version string to SSLeay/OpenSSL
[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 $NT=1;
13 foreach (@ARGV)
14         {
15         $NT=1 if $_ eq "32";
16         $NT=0 if $_ eq "16";
17         $do_ssl=1 if $_ eq "ssleay";
18         $do_crypto=1 if $_ eq "libeay";
19         }
20
21 if (!$do_ssl && !$do_crypto)
22         {
23         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 ]\n";
24         exit(1);
25         }
26
27 %ssl_list=&load_numbers($ssl_num);
28 %crypto_list=&load_numbers($crypto_num);
29
30 $ssl="ssl/ssl.h";
31
32 $crypto ="crypto/crypto.h";
33 $crypto.=" crypto/des/des.h";
34 $crypto.=" crypto/idea/idea.h";
35 $crypto.=" crypto/rc4/rc4.h";
36 $crypto.=" crypto/rc5/rc5.h";
37 $crypto.=" crypto/rc2/rc2.h";
38 $crypto.=" crypto/bf/blowfish.h";
39 $crypto.=" crypto/cast/cast.h";
40 $crypto.=" crypto/md2/md2.h";
41 $crypto.=" crypto/md5/md5.h";
42 $crypto.=" crypto/mdc2/mdc2.h";
43 $crypto.=" crypto/sha/sha.h";
44 $crypto.=" crypto/ripemd/ripemd.h";
45
46 $crypto.=" crypto/bn/bn.h";
47 $crypto.=" crypto/rsa/rsa.h";
48 $crypto.=" crypto/dsa/dsa.h";
49 $crypto.=" crypto/dh/dh.h";
50
51 $crypto.=" crypto/stack/stack.h";
52 $crypto.=" crypto/buffer/buffer.h";
53 $crypto.=" crypto/bio/bio.h";
54 $crypto.=" crypto/lhash/lhash.h";
55 $crypto.=" crypto/conf/conf.h";
56 $crypto.=" crypto/txt_db/txt_db.h";
57
58 $crypto.=" crypto/evp/evp.h";
59 $crypto.=" crypto/objects/objects.h";
60 $crypto.=" crypto/pem/pem.h";
61 #$crypto.=" crypto/meth/meth.h";
62 $crypto.=" crypto/asn1/asn1.h";
63 $crypto.=" crypto/asn1/asn1_mac.h";
64 $crypto.=" crypto/err/err.h";
65 $crypto.=" crypto/pkcs7/pkcs7.h";
66 $crypto.=" crypto/x509/x509.h";
67 $crypto.=" crypto/x509/x509_vfy.h";
68 $crypto.=" crypto/rand/rand.h";
69 $crypto.=" crypto/hmac/hmac.h";
70 $crypto.=" crypto/comp/comp.h";
71 $crypto.=" crypto/tmdiff.h";
72
73 $match{'NOPROTO'}=1;
74 $match2{'PERL5'}=1;
75
76 &print_def_file(*STDOUT,"SSLEAY",*ssl_list,&do_defs("SSLEAY",$ssl))
77         if $do_ssl == 1;
78
79 &print_def_file(*STDOUT,"LIBEAY",*crypto_list,&do_defs("LIBEAY",$crypto))
80         if $do_crypto == 1;
81
82 sub do_defs
83         {
84         local($name,$files)=@_;
85         local(@ret);
86
87         $off=-1;
88         foreach $file (split(/\s+/,$files))
89                 {
90 #               print STDERR "reading $file\n";
91                 open(IN,"<$file") || die "unable to open $file:$!\n";
92                 $depth=0;
93                 $pr=-1;
94                 @np="";
95                 $/=undef;
96                 $a=<IN>;
97                 while (($i=index($a,"/*")) >= 0)
98                         {
99                         $j=index($a,"*/");
100                         break unless ($j >= 0);
101                         $a=substr($a,0,$i).substr($a,$j+2);
102                 #       print "$i $j\n";
103                         }
104                 foreach (split("\n",$a))
105                         {
106                         if (/^\#\s*ifndef (.*)/)
107                                 {
108                                 push(@tag,$1);
109                                 $tag{$1}=-1;
110                                 next;
111                                 }
112                         elsif (/^\#\s*if !defined\(([^\)]+)\)/)
113                                 {
114                                 push(@tag,$1);
115                                 $tag{$1}=-1;
116                                 next;
117                                 }
118                         elsif (/^\#\s*ifdef (.*)/)
119                                 {
120                                 push(@tag,$1);
121                                 $tag{$1}=1;
122                                 next;
123                                 }
124                         elsif (/^\#\s*if defined(.*)/)
125                                 {
126                                 push(@tag,$1);
127                                 $tag{$1}=1;
128                                 next;
129                                 }
130                         elsif (/^\#\s*endif/)
131                                 {
132                                 $tag{$tag[$#tag]}=0;
133                                 pop(@tag);
134                                 next;
135                                 }
136                         elsif (/^\#\s*else/)
137                                 {
138                                 $t=$tag[$#tag];
139                                 $tag{$t}= -$tag{$t};
140                                 next;
141                                 }
142 #printf STDERR "$_\n%2d %2d %2d %2d %2d $NT\n",
143 #$tag{'NOPROTO'},$tag{'FreeBSD'},$tag{'WIN16'},$tag{'PERL5'},$tag{'NO_FP_API'};
144
145                         $t=undef;
146                         if (/^extern .*;$/)
147                                 { $t=&do_extern($name,$_); }
148                         elsif ( ($tag{'NOPROTO'} == 1) &&
149                                 ($tag{'FreeBSD'} != 1) &&
150                                 (($NT && ($tag{'WIN16'} != 1)) ||
151                                  (!$NT && ($tag{'WIN16'} != -1))) &&
152                                 ($tag{'PERL5'} != 1) &&
153 #                               ($tag{'_WINDLL'} != -1) &&
154                                 ((!$NT && $tag{'_WINDLL'} != -1) ||
155                                  ($NT && $tag{'_WINDLL'} != 1)) &&
156                                 ((($tag{'NO_FP_API'} != 1) && $NT) ||
157                                  (($tag{'NO_FP_API'} != -1) && !$NT)))
158                                 { $t=&do_line($name,$_); }
159                         else
160                                 { $t=undef; }
161                         if (($t ne undef) && (!$done{$name,$t}))
162                                 {
163                                 $done{$name,$t}++;
164                                 push(@ret,$t);
165 #printf STDERR "one:$t\n" if $t =~ /BIO_/;
166                                 }
167                         }
168                 close(IN);
169                 }
170         return(@ret);
171         }
172
173 sub do_line
174         {
175         local($file,$_)=@_;
176         local($n);
177
178         return(undef) if /^$/;
179         return(undef) if /^\s/;
180 #printf STDERR "two:$_\n" if $_ =~ /BIO_/;
181         if (/(CRYPTO_get_locking_callback)/)
182                 { return($1); }
183         elsif (/(CRYPTO_get_id_callback)/)
184                 { return($1); }
185         elsif (/(CRYPTO_get_add_lock_callback)/)
186                 { return($1); }
187         elsif (/(SSL_CTX_get_verify_callback)/)
188                 { return($1); }
189         elsif (/(SSL_get_info_callback)/)
190                 { return($1); }
191         elsif ((!$NT) && /(ERR_load_CRYPTO_strings)/)
192                 { return("ERR_load_CRYPTOlib_strings"); }
193         elsif (!$NT && /BIO_s_file/)
194                 { return(undef); }
195         elsif (!$NT && /BIO_new_file/)
196                 { return(undef); }
197         elsif (!$NT && /BIO_new_fp/)
198                 { return(undef); }
199         elsif ($NT && /BIO_s_file_internal/)
200                 { return(undef); }
201         elsif ($NT && /BIO_new_file_internal/)
202                 { return(undef); }
203         elsif ($NT && /BIO_new_fp_internal/)
204                 { return(undef); }
205         else
206                 {
207                 /\s\**(\S+)\s*\(/;
208                 return($1);
209                 }
210         }
211
212 sub do_extern
213         {
214         local($file,$_)=@_;
215         local($n);
216
217         /\s\**(\S+);$/;
218         return($1);
219         }
220
221 sub print_def_file
222         {
223         local(*OUT,$name,*nums,@functions)=@_;
224         local($n)=1;
225
226         if ($NT)
227                 { $name.="32"; }
228         else
229                 { $name.="16"; }
230
231         print OUT <<"EOF";
232 ;
233 ; Definition file for the DDL version of the $name library from SSLeay
234 ;
235
236 LIBRARY         $name
237
238 DESCRIPTION     'SSLeay $name - eay\@cryptsoft.com'
239
240 EOF
241
242         if (!$NT)
243                 {
244                 print <<"EOF";
245 CODE            PRELOAD MOVEABLE
246 DATA            PRELOAD MOVEABLE SINGLE
247
248 EXETYPE         WINDOWS
249
250 HEAPSIZE        4096
251 STACKSIZE       8192
252
253 EOF
254                 }
255
256         print "EXPORTS\n";
257
258
259         (@e)=grep(/^SSLeay/,@functions);
260         (@r)=grep(!/^SSLeay/,@functions);
261         @functions=((sort @e),(sort @r));
262
263         foreach $func (@functions)
264                 {
265                 if (!defined($nums{$func}))
266                         {
267                         printf STDERR "$func does not have a number assigned\n";
268                         }
269                 else
270                         {
271                         $n=$nums{$func};
272                         printf OUT "    %s%-35s@%d\n",($NT)?"":"_",$func,$n;
273                         }
274                 }
275         printf OUT "\n";
276         }
277
278 sub load_numbers
279         {
280         local($name)=@_;
281         local($j,@a,%ret);
282
283         open(IN,"<$name") || die "unable to open $name:$!\n";
284         while (<IN>)
285                 {
286                 chop;
287                 s/#.*$//;
288                 next if /^\s*$/;
289                 @a=split;
290                 $ret{$a[0]}=$a[1];
291                 }
292         close(IN);
293         return(%ret);
294         }