961dff5b8fc9761634f4da5a7b10a4faa3204584
[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 # Intermediary files are created, call libeay.num and ssleay.num,...
9 # Previously, they had the following format:
10 #
11 #       routine-name    nnnn
12 #
13 # But that isn't enough for a number of reasons, the first on being that
14 # this format is (needlessly) very Win32-centric, and even then...
15 # One of the biggest problems is that there's no information about what
16 # routines should actually be used, which varies with what crypto algorithms
17 # are disabled.  Also, some operating systems (for example VMS with VAX C)
18 # need to keep track of the global variables as well as the functions.
19 #
20 # So, a remake of this script is done so as to include information on the
21 # kind of symbol it is (function or variable) and what algorithms they're
22 # part of.  This will allow easy translating to .def files or the corresponding
23 # file in other operating systems (a .opt file for VMS, possibly with a .mar
24 # file).
25 #
26 # The format now becomes:
27 #
28 #       routine-name    nnnn    info
29 #
30 # and the "info" part is actually a colon-separated string of fields with
31 # the following meaning:
32 #
33 #       existence:platform:kind:algorithms
34 #
35 # - "existence" can be "EXIST" or "NOEXIST" depending on if the symbol is
36 #   found somewhere in the source, 
37 # - "platforms" is empty if it exists on all platforms, otherwise it contains
38 #   comma-separated list of the platform, just as they are if the symbol exists
39 #   for those platforms, or prepended with a "!" if not.  This helps resolve
40 #   symbol name replacements for platforms where the names are too long for the
41 #   compiler or linker, or if the systems is case insensitive and there is a
42 #   clash.  This script assumes those redefinitions are place in the file
43 #   crypto/symhacks.h.
44 #   The semantics for the platforms list is a bit complicated.  The rule of
45 #   thumb is that the list is exclusive, but it seems to mean different things.
46 #   So, if the list is all negatives (like "!VMS,!WIN16"), the symbol exists
47 #   on all platforms except those listed.  If the list is all positives (like
48 #   "VMS,WIN16"), the symbol exists only on those platforms and nowhere else.
49 #   The combination of positives and negatives will act as if the positives
50 #   weren't there.
51 # - "kind" is "FUNCTION" or "VARIABLE".  The meaning of that is obvious.
52 # - "algorithms" is a comma-separated list of algorithm names.  This helps
53 #   exclude symbols that are part of an algorithm that some user wants to
54 #   exclude.
55 #
56
57 my $crypto_num= "util/libeay.num";
58 my $ssl_num=    "util/ssleay.num";
59
60 my $do_update = 0;
61 my $do_rewrite = 0;
62 my $do_crypto = 0;
63 my $do_ssl = 0;
64 my $do_ctest = 0;
65 my $do_ctestall = 0;
66 my $rsaref = 0;
67
68 my $VMS=0;
69 my $W32=0;
70 my $W16=0;
71 my $NT=0;
72 # Set this to make typesafe STACK definitions appear in DEF
73 my $safe_stack_def = 0;
74
75 my @known_platforms = ( "__FreeBSD__", "VMS", "WIN16", "WIN32",
76                         "WINNT", "PERL5", "NeXT" );
77 my @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF",
78                          "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1",
79                          "RIPEMD",
80                          "MDC2", "RSA", "DSA", "DH", "HMAC", "RIJNDAEL",
81                          # Envelope "algorithms"
82                          "EVP", "X509", "ASN1_TYPEDEFS",
83                          # Helper "algorithms"
84                          "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR",
85                          "LOCKING",
86                          # External "algorithms"
87                          "FP_API", "STDIO", "SOCK", "KRB5" );
88
89 my $options="";
90 open(IN,"<Makefile.ssl") || die "unable to open Makefile.ssl!\n";
91 while(<IN>) {
92     $options=$1 if (/^OPTIONS=(.*)$/);
93 }
94 close(IN);
95
96 # The following ciphers may be excluded (by Configure). This means functions
97 # defined with ifndef(NO_XXX) are not included in the .def file, and everything
98 # in directory xxx is ignored.
99 my $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf;
100 my $no_cast;
101 my $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2;
102 my $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_rijndael;
103 my $no_fp_api;
104
105 foreach (@ARGV, split(/ /, $options))
106         {
107         $W32=1 if $_ eq "32";
108         $W16=1 if $_ eq "16";
109         if($_ eq "NT") {
110                 $W32 = 1;
111                 $NT = 1;
112         }
113         $VMS=1 if $_ eq "VMS";
114         #$rsaref=1 if $_ eq "rsaref";
115
116         $do_ssl=1 if $_ eq "ssleay";
117         $do_ssl=1 if $_ eq "ssl";
118         $do_crypto=1 if $_ eq "libeay";
119         $do_crypto=1 if $_ eq "crypto";
120         $do_update=1 if $_ eq "update";
121         $do_rewrite=1 if $_ eq "rewrite";
122         $do_ctest=1 if $_ eq "ctest";
123         $do_ctestall=1 if $_ eq "ctestall";
124         #$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK";
125
126         if    (/^no-rc2$/)      { $no_rc2=1; }
127         elsif (/^no-rc4$/)      { $no_rc4=1; }
128         elsif (/^no-rc5$/)      { $no_rc5=1; }
129         elsif (/^no-idea$/)     { $no_idea=1; }
130         elsif (/^no-des$/)      { $no_des=1; $no_mdc2=1; }
131         elsif (/^no-bf$/)       { $no_bf=1; }
132         elsif (/^no-cast$/)     { $no_cast=1; }
133         elsif (/^no-md2$/)      { $no_md2=1; }
134         elsif (/^no-md4$/)      { $no_md4=1; }
135         elsif (/^no-md5$/)      { $no_md5=1; }
136         elsif (/^no-sha$/)      { $no_sha=1; }
137         elsif (/^no-ripemd$/)   { $no_ripemd=1; }
138         elsif (/^no-mdc2$/)     { $no_mdc2=1; }
139         elsif (/^no-rsa$/)      { $no_rsa=1; }
140         elsif (/^no-dsa$/)      { $no_dsa=1; }
141         elsif (/^no-dh$/)       { $no_dh=1; }
142         elsif (/^no-hmac$/)     { $no_hmac=1; }
143         elsif (/^no-rijndael$/) { $no_rijndael=1; }
144         elsif (/^no-evp$/)      { $no_evp=1; }
145         elsif (/^no-lhash$/)    { $no_lhash=1; }
146         elsif (/^no-stack$/)    { $no_stack=1; }
147         elsif (/^no-err$/)      { $no_err=1; }
148         elsif (/^no-buffer$/)   { $no_buffer=1; }
149         elsif (/^no-bio$/)      { $no_bio=1; }
150         #elsif (/^no-locking$/) { $no_locking=1; }
151         elsif (/^no-comp$/)     { $no_comp=1; }
152         elsif (/^no-dso$/)      { $no_dso=1; }
153         }
154
155
156 # If no platform is given, assume WIN32
157 if ($W32 + $W16 + $VMS == 0) {
158         $W32 = 1;
159 }
160
161 # Add extra knowledge
162 if ($W16) {
163         $no_fp_api=1;
164 }
165
166 if (!$do_ssl && !$do_crypto)
167         {
168         print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT ]\n";
169         exit(1);
170         }
171
172 %ssl_list=&load_numbers($ssl_num);
173 $max_ssl = $max_num;
174 %crypto_list=&load_numbers($crypto_num);
175 $max_crypto = $max_num;
176
177 my $ssl="ssl/ssl.h";
178 $ssl.=" ssl/kssl.h";
179
180 my $crypto ="crypto/crypto.h";
181 $crypto.=" crypto/des/des.h" unless $no_des;
182 $crypto.=" crypto/idea/idea.h" unless $no_idea;
183 $crypto.=" crypto/rc4/rc4.h" unless $no_rc4;
184 $crypto.=" crypto/rc5/rc5.h" unless $no_rc5;
185 $crypto.=" crypto/rc2/rc2.h" unless $no_rc2;
186 $crypto.=" crypto/bf/blowfish.h" unless $no_bf;
187 $crypto.=" crypto/cast/cast.h" unless $no_cast;
188 $crypto.=" crypto/md2/md2.h" unless $no_md2;
189 $crypto.=" crypto/md4/md4.h" unless $no_md4;
190 $crypto.=" crypto/md5/md5.h" unless $no_md5;
191 $crypto.=" crypto/mdc2/mdc2.h" unless $no_mdc2;
192 $crypto.=" crypto/sha/sha.h" unless $no_sha;
193 $crypto.=" crypto/ripemd/ripemd.h" unless $no_ripemd;
194 $crypto.=" crypto/rijndael/rijndael.h" unless $no_rijndael;
195 $crypto.=" crypto/rijndael/rd_fst.h" unless $no_rijndael;
196
197 $crypto.=" crypto/bn/bn.h";
198 $crypto.=" crypto/rsa/rsa.h" unless $no_rsa;
199 $crypto.=" crypto/dsa/dsa.h" unless $no_dsa;
200 $crypto.=" crypto/dh/dh.h" unless $no_dh;
201 $crypto.=" crypto/hmac/hmac.h" unless $no_hmac;
202
203 $crypto.=" crypto/engine/engine.h";
204 $crypto.=" crypto/stack/stack.h" unless $no_stack;
205 $crypto.=" crypto/buffer/buffer.h" unless $no_buffer;
206 $crypto.=" crypto/bio/bio.h" unless $no_bio;
207 $crypto.=" crypto/dso/dso.h" unless $no_dso;
208 $crypto.=" crypto/lhash/lhash.h" unless $no_lhash;
209 $crypto.=" crypto/conf/conf.h";
210 $crypto.=" crypto/txt_db/txt_db.h";
211
212 $crypto.=" crypto/evp/evp.h" unless $no_evp;
213 $crypto.=" crypto/objects/objects.h";
214 $crypto.=" crypto/pem/pem.h";
215 #$crypto.=" crypto/meth/meth.h";
216 $crypto.=" crypto/asn1/asn1.h";
217 $crypto.=" crypto/asn1/asn1t.h";
218 $crypto.=" crypto/asn1/asn1_mac.h";
219 $crypto.=" crypto/err/err.h" unless $no_err;
220 $crypto.=" crypto/pkcs7/pkcs7.h";
221 $crypto.=" crypto/pkcs12/pkcs12.h";
222 $crypto.=" crypto/x509/x509.h";
223 $crypto.=" crypto/x509/x509_vfy.h";
224 $crypto.=" crypto/x509v3/x509v3.h";
225 $crypto.=" crypto/rand/rand.h";
226 $crypto.=" crypto/comp/comp.h" unless $no_comp;
227 $crypto.=" crypto/ocsp/ocsp.h";
228 $crypto.=" crypto/tmdiff.h";
229
230 my $symhacks="crypto/symhacks.h";
231
232 my @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks);
233 my @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks);
234
235 if ($do_update) {
236
237 if ($do_ssl == 1) {
238
239         &maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols);
240         if ($do_rewrite == 1) {
241                 open(OUT, ">$ssl_num");
242                 &rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols);
243                 close OUT;
244         } else {
245                 open(OUT, ">>$ssl_num");
246         }
247         &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols);
248         close OUT;
249 }
250
251 if($do_crypto == 1) {
252
253         &maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols);
254         if ($do_rewrite == 1) {
255                 open(OUT, ">$crypto_num");
256                 &rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols);
257         } else {
258                 open(OUT, ">>$crypto_num");
259         }
260         &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols);
261         close OUT;
262
263
264 } elsif ($do_ctest || $do_ctestall) {
265
266         print <<"EOF";
267
268 /* Test file to check all DEF file symbols are present by trying
269  * to link to all of them. This is *not* intended to be run!
270  */
271
272 int main()
273 {
274 EOF
275         &print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
276                 if $do_ssl == 1;
277
278         &print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
279                 if $do_crypto == 1;
280
281         print "}\n";
282
283 } else {
284
285         &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_symbols)
286                 if $do_ssl == 1;
287
288         &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_symbols)
289                 if $do_crypto == 1;
290
291 }
292
293
294 sub do_defs
295 {
296         my($name,$files,$symhacksfile)=@_;
297         my $file;
298         my @ret;
299         my %syms;
300         my %platform;           # For anything undefined, we assume ""
301         my %kind;               # For anything undefined, we assume "FUNCTION"
302         my %algorithm;          # For anything undefined, we assume ""
303         my %rename;
304         my $cpp;
305         my %unknown_algorithms = ();
306
307         foreach $file (split(/\s+/,$symhacksfile." ".$files))
308                 {
309                 open(IN,"<$file") || die "unable to open $file:$!\n";
310                 my $line = "", my $def= "";
311                 my %tag = (
312                         (map { $_ => 0 } @known_platforms),
313                         (map { "NO_".$_ => 0 } @known_algorithms),
314                         NOPROTO         => 0,
315                         PERL5           => 0,
316                         _WINDLL         => 0,
317                         CONST_STRICT    => 0,
318                         TRUE            => 1,
319                 );
320                 my $symhacking = $file eq $symhacksfile;
321                 while(<IN>) {
322                         last if (/BEGIN ERROR CODES/);
323                         if ($line ne '') {
324                                 $_ = $line . $_;
325                                 $line = '';
326                         }
327
328                         if (/\\$/) {
329                                 $line = $_;
330                                 next;
331                         }
332
333                         $cpp = 1 if /^\#.*ifdef.*cplusplus/;
334                         if ($cpp) {
335                                 $cpp = 0 if /^\#.*endif/;
336                                 next;
337                         }
338
339                         s/\/\*.*?\*\///gs;                   # ignore comments
340                         s/{[^{}]*}//gs;                      # ignore {} blocks
341                         #print STDERR "DEBUG: \$_=\"$_\"\n";
342                         if (/^\#\s*ifndef\s+(.*)/) {
343                                 push(@tag,$1);
344                                 $tag{$1}=-1;
345                         } elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
346                                 push(@tag,$1);
347                                 $tag{$1}=-1;
348                         } elsif (/^\#\s*ifdef\s+(.*)/) {
349                                 push(@tag,$1);
350                                 $tag{$1}=1;
351                         } elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
352                                 push(@tag,$1);
353                                 $tag{$1}=1;
354                         } elsif (/^\#\s*error\s+(\w+) is disabled\./) {
355                                 if ($tag[$#tag] eq "NO_".$1) {
356                                         $tag{$tag[$#tag]}=2;
357                                 }
358                         } elsif (/^\#\s*endif/) {
359                                 my $oldtag=$tag[$#tag];
360                                 #print STDERR "DEBUG: \$oldtag=\"$oldtag\"\n";
361                                 if ($tag{$tag[$#tag]}==2) {
362                                         $tag{$tag[$#tag]}=-1;
363                                 } else {
364                                         $tag{$tag[$#tag]}=0;
365                                 }
366                                 pop(@tag);
367                                 if ($oldtag =~ /^NO_([A-Z0-9_]+)$/) {
368                                         $oldtag=$1;
369                                 } else {
370                                         $oldtag="";
371                                 }
372                                 if ($oldtag ne ""
373                                     && !grep(/^$oldtag$/, @known_algorithms)) {
374                                         $unknown_algorithms{$oldtag} = 1;
375                                         #print STDERR "DEBUG: Added as unknown algorithm: $oldtag\n";
376                                 }
377                         } elsif (/^\#\s*else/) {
378                                 my $t=$tag[$#tag];
379                                 $tag{$t}= -$tag{$t};
380                         } elsif (/^\#\s*if\s+1/) {
381                                 # Dummy tag
382                                 push(@tag,"TRUE");
383                                 $tag{"TRUE"}=1;
384                         } elsif (/^\#\s*if\s+0/) {
385                                 # Dummy tag
386                                 push(@tag,"TRUE");
387                                 $tag{"TRUE"}=-1;
388                         } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
389                                  && $symhacking) {
390                                 my $s = $1;
391                                 my $a =
392                                     $2.":".join(",", grep(!/^$/,
393                                                           map { $tag{$_} == 1 ?
394                                                                     $_ : "" }
395                                                           @known_platforms));
396                                 $rename{$s} = $a;
397                         }
398                         if (/^\#/) {
399                                 my @p = grep(!/^$/,
400                                              map { $tag{$_} == 1 ? $_ :
401                                                        $tag{$_} == -1 ? "!".$_  : "" }
402                                              @known_platforms);
403                                 my @a = grep(!/^$/,
404                                              map { $tag{"NO_".$_} == -1 ? $_ : "" }
405                                              @known_algorithms);
406                                 $def .= "#INFO:".join(',',@p).":".join(',',@a).";";
407                                 next;
408                         }
409                         if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
410                                 next;
411                         } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
412                                 $syms{"d2i_$3"} = 1;
413                                 $syms{"i2d_$3"} = 1;
414                                 $syms{"$3_new"} = 1;
415                                 $syms{"$3_free"} = 1;
416                                 $syms{"$2_it"} = 1;
417                                 $kind{"$2_it"} = "VARIABLE";
418                         } elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/) {
419                                 $syms{"d2i_$1"} = 1;
420                                 $syms{"i2d_$1"} = 1;
421                                 $syms{"$1_new"} = 1;
422                                 $syms{"$1_free"} = 1;
423                                 $syms{"$1_it"} = 1;
424                                 $kind{"$1_it"} = "VARIABLE";
425                                 next;
426                         } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
427                                 next;
428                         } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
429                                 next;
430                         } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
431                                  /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
432                                 # Things not in Win16
433                                 $syms{"PEM_read_${1}"} = 1;
434                                 $platform{"PEM_read_${1}"} = "!WIN16";
435                                 $syms{"PEM_write_${1}"} = 1;
436                                 $platform{"PEM_write_${1}"} = "!WIN16";
437                                 # Things that are everywhere
438                                 $syms{"PEM_read_bio_${1}"} = 1;
439                                 $syms{"PEM_write_bio_${1}"} = 1;
440                                 if ($1 eq "RSAPrivateKey" ||
441                                     $1 eq "RSAPublicKey" ||
442                                     $1 eq "RSA_PUBKEY") {
443                                         $algorithm{"PEM_read_${1}"} = "RSA";
444                                         $algorithm{"PEM_write_${1}"} = "RSA";
445                                         $algorithm{"PEM_read_bio_${1}"} = "RSA";
446                                         $algorithm{"PEM_write_bio_${1}"} = "RSA";
447                                 }
448                                 elsif ($1 eq "DSAPrivateKey" ||
449                                        $1 eq "DSAparams" ||
450                                        $1 eq "RSA_PUBKEY") {
451                                         $algorithm{"PEM_read_${1}"} = "DSA";
452                                         $algorithm{"PEM_write_${1}"} = "DSA";
453                                         $algorithm{"PEM_read_bio_${1}"} = "DSA";
454                                         $algorithm{"PEM_write_bio_${1}"} = "DSA";
455                                 }
456                                 elsif ($1 eq "DHparams") {
457                                         $algorithm{"PEM_read_${1}"} = "DH";
458                                         $algorithm{"PEM_write_${1}"} = "DH";
459                                         $algorithm{"PEM_read_bio_${1}"} = "DH";
460                                         $algorithm{"PEM_write_bio_${1}"} = "DH";
461                                 }
462                         } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
463                                      /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
464                                 # Things not in Win16
465                                 $syms{"PEM_write_${1}"} = 1;
466                                 $platform{"PEM_write_${1}"} .= ",!WIN16";
467                                 # Things that are everywhere
468                                 $syms{"PEM_write_bio_${1}"} = 1;
469                                 if ($1 eq "RSAPrivateKey" ||
470                                     $1 eq "RSAPublicKey" ||
471                                     $1 eq "RSA_PUBKEY") {
472                                         $algorithm{"PEM_write_${1}"} = "RSA";
473                                         $algorithm{"PEM_write_bio_${1}"} = "RSA";
474                                 }
475                                 elsif ($1 eq "DSAPrivateKey" ||
476                                        $1 eq "DSAparams" ||
477                                        $1 eq "RSA_PUBKEY") {
478                                         $algorithm{"PEM_write_${1}"} = "DSA";
479                                         $algorithm{"PEM_write_bio_${1}"} = "DSA";
480                                 }
481                                 elsif ($1 eq "DHparams") {
482                                         $algorithm{"PEM_write_${1}"} = "DH";
483                                         $algorithm{"PEM_write_bio_${1}"} = "DH";
484                                 }
485                         } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
486                                      /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
487                                 # Things not in Win16
488                                 $syms{"PEM_read_${1}"} = 1;
489                                 $platform{"PEM_read_${1}"} .= ",!WIN16";
490                                 # Things that are everywhere
491                                 $syms{"PEM_read_bio_${1}"} = 1;
492                         } elsif (
493                                 ($tag{'TRUE'} != -1)
494                                 && ($tag{'CONST_STRICT'} != 1)
495                                  )
496                                 {
497                                         if (/\{|\/\*|\([^\)]*$/) {
498                                                 $line = $_;
499                                         } else {
500                                                 $def .= $_;
501                                         }
502                                 }
503                         }
504                 close(IN);
505
506                 my $algs;
507                 my $plays;
508
509                 foreach (split /;/, $def) {
510                         my $s; my $k = "FUNCTION"; my $p; my $a;
511                         s/^[\n\s]*//g;
512                         s/[\n\s]*$//g;
513                         next if(/\#undef/);
514                         next if(/typedef\W/);
515                         next if(/\#define/);
516
517                         if (/^\#INFO:([^:]*):(.*)$/) {
518                                 $plats = $1;
519                                 $algs = $2;
520                                 next;
521                         } elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+)(\[[0-9]*\])*\s*$/) {
522                                 $s = $1;
523                                 $k = "VARIABLE";
524                         } elsif (/\(\*(\w*)\([^\)]+/) {
525                                 $s = $1;
526                         } elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
527                                 # K&R C
528                                 next;
529                         } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
530                                 while (not /\(\)$/s) {
531                                         s/[^\(\)]*\)$/\)/s;
532                                         s/\([^\(\)]*\)\)$/\)/s;
533                                 }
534                                 s/\(void\)//;
535                                 /(\w+)\W*\(\)/s;
536                                 $s = $1;
537                         } elsif (/\(/ and not (/=/)) {
538                                 print STDERR "File $file: cannot parse: $_;\n";
539                                 next;
540                         } else {
541                                 next;
542                         }
543
544                         $syms{$s} = 1;
545                         $kind{$s} = $k;
546
547                         $p = $plats;
548                         $a = $algs;
549                         $a .= ",BF" if($s =~ /EVP_bf/);
550                         $a .= ",CAST" if($s =~ /EVP_cast/);
551                         $a .= ",DES" if($s =~ /EVP_des/);
552                         $a .= ",DSA" if($s =~ /EVP_dss/);
553                         $a .= ",IDEA" if($s =~ /EVP_idea/);
554                         $a .= ",MD2" if($s =~ /EVP_md2/);
555                         $a .= ",MD4" if($s =~ /EVP_md4/);
556                         $a .= ",MD5" if($s =~ /EVP_md5/);
557                         $a .= ",RC2" if($s =~ /EVP_rc2/);
558                         $a .= ",RC4" if($s =~ /EVP_rc4/);
559                         $a .= ",RC5" if($s =~ /EVP_rc5/);
560                         $a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
561                         $a .= ",SHA" if($s =~ /EVP_sha/);
562                         $a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
563                         $a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
564                         $a .= ",RSA" if($s =~ /RSAPrivateKey/);
565                         $a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
566
567                         $platform{$s} .= ','.$p;
568                         $algorithm{$s} .= ','.$a;
569
570                         if (defined($rename{$s})) {
571                                 (my $r, my $p) = split(/:/,$rename{$s});
572                                 my @ip = map { /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p;
573                                 $syms{$r} = 1;
574                                 $kind{$r} = $kind{$s}."(".$s.")";
575                                 $algorithm{$r} = $algorithm{$s};
576                                 $platform{$r} = $platform{$s}.",".$p;
577                                 $platform{$s} .= ','.join(',', @ip).','.join(',', @ip);
578                         }
579                 }
580         }
581
582         # Prune the returned symbols
583
584         $platform{"crypt"} .= ",!PERL5,!__FreeBSD__,!NeXT";
585
586         delete $syms{"SSL_add_dir_cert_subjects_to_stack"};
587         delete $syms{"bn_dump1"};
588
589         $platform{"BIO_s_file_internal"} .= ",WIN16";
590         $platform{"BIO_new_file_internal"} .= ",WIN16";
591         $platform{"BIO_new_fp_internal"} .= ",WIN16";
592
593         $platform{"BIO_s_file"} .= ",!WIN16";
594         $platform{"BIO_new_file"} .= ",!WIN16";
595         $platform{"BIO_new_fp"} .= ",!WIN16";
596
597         $platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
598
599         if(exists $syms{"ERR_load_CRYPTO_strings"}) {
600                 $platform{"ERR_load_CRYPTO_strings"} .= ",!VMS,!WIN16";
601                 $syms{"ERR_load_CRYPTOlib_strings"} = 1;
602                 $platform{"ERR_load_CRYPTOlib_strings"} .= ",VMS,WIN16";
603         }
604
605         # Info we know about
606
607         $platform{"RSA_PKCS1_RSAref"} = "RSAREF";
608         $algorithm{"RSA_PKCS1_RSAref"} = "RSA";
609
610         push @ret, map { $_."\\".&info_string($_,"EXIST",
611                                               $platform{$_},
612                                               $kind{$_},
613                                               $algorithm{$_}) } keys %syms;
614
615         if (keys %unknown_algorithms) {
616                 print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
617                 print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
618         }
619         return(@ret);
620 }
621
622 sub info_string {
623         (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
624
625         my %a = defined($algorithms) ?
626             map { $_ => 1 } split /,/, $algorithms : ();
627         my $pl = defined($platforms) ? $platforms : "";
628         my %p = map { $_ => 0 } split /,/, $pl;
629         my $k = defined($kind) ? $kind : "FUNCTION";
630         my $ret;
631
632         # We do this, because if there's code like the following, it really
633         # means the function exists in all cases and should therefore be
634         # everywhere.  By increasing and decreasing, we may attain 0:
635         #
636         # ifndef WIN16
637         #    int foo();
638         # else
639         #    int _fat foo();
640         # endif
641         foreach $platform (split /,/, $pl) {
642                 if ($platform =~ /^!(.*)$/) {
643                         $p{$1}--;
644                 } else {
645                         $p{$platform}++;
646                 }
647         }
648         foreach $platform (keys %p) {
649                 if ($p{$platform} == 0) { delete $p{$platform}; }
650         }
651
652         delete $p{""};
653         delete $a{""};
654
655         $ret = $exist;
656         $ret .= ":".join(',',map { $p{$_} < 0 ? "!".$_ : $_ } keys %p);
657         $ret .= ":".$k;
658         $ret .= ":".join(',',keys %a);
659         return $ret;
660 }
661
662 sub maybe_add_info {
663         (my $name, *nums, my @symbols) = @_;
664         my $sym;
665         my $new_info = 0;
666         my %syms=();
667
668         print STDERR "Updating $name info\n";
669         foreach $sym (@symbols) {
670                 (my $s, my $i) = split /\\/, $sym;
671                 $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
672                 if (defined($nums{$s})) {
673                         (my $n, my $dummy) = split /\\/, $nums{$s};
674                         if (!defined($dummy) || $i ne $dummy) {
675                                 $nums{$s} = $n."\\".$i;
676                                 $new_info++;
677                                 #print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n";
678                         }
679                 }
680                 $syms{sym} = 1;
681         }
682
683         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
684         foreach $sym (@s) {
685                 (my $n, my $i) = split /\\/, $nums{$sym};
686                 if (!defined($syms{sym})) {
687                         $new_info++;
688                         #print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n";
689                 }
690         }
691         if ($new_info) {
692                 print STDERR "$new_info old symbols got an info update\n";
693                 if (!$do_rewrite) {
694                         print STDERR "You should do a rewrite to fix this.\n";
695                 }
696         } else {
697                 print STDERR "No old symbols needed info update\n";
698         }
699 }
700
701 sub print_test_file
702 {
703         (*OUT,my $name,*nums,my @symbols)=@_;
704         my $n = 1; my @e; my @r;
705         my $sym; my $prev = ""; my $prefSSLeay;
706
707         (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
708         (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
709         @symbols=((sort @e),(sort @r));
710
711         foreach $sym (@symbols) {
712                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
713                 if ($s ne $prev) {
714                         if (!defined($nums{$sym})) {
715                                 printf STDERR "Warning: $sym does not have a number assigned\n"
716                                                 if(!$do_update);
717                         } else {
718                                 $n=$nums{$s};
719                                 print OUT "\t$s();\n";
720                         }
721                 }
722                 $prev = $s;     # To avoid duplicates...
723         }
724 }
725
726 sub print_def_file
727 {
728         (*OUT,my $name,*nums,my @symbols)=@_;
729         my $n = 1; my @e; my @r; my @v;
730
731         if ($W32)
732                 { $name.="32"; }
733         else
734                 { $name.="16"; }
735
736         print OUT <<"EOF";
737 ;
738 ; Definition file for the DLL version of the $name library from OpenSSL
739 ;
740
741 LIBRARY         $name
742
743 DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
744
745 EOF
746
747         if (!$W32) {
748                 print <<"EOF";
749 CODE            PRELOAD MOVEABLE
750 DATA            PRELOAD MOVEABLE SINGLE
751
752 EXETYPE         WINDOWS
753
754 HEAPSIZE        4096
755 STACKSIZE       8192
756
757 EOF
758         }
759
760         print "EXPORTS\n";
761
762         (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
763         (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
764         (@v)=grep(/^\w+\\.*?:.*?:VARIABLE/,@symbols);
765         @symbols=((sort @e),(sort @r), (sort @v));
766
767
768         foreach $sym (@symbols) {
769                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
770                 my $v = 0;
771                 $v = 1 if $sym=~ /^\w+\\.*?:.*?:VARIABLE/;
772                 if (!defined($nums{$s})) {
773                         printf STDERR "Warning: $s does not have a number assigned\n"
774                                         if(!$do_update);
775                 } else {
776                         (my $n, my $i) = split /\\/, $nums{$s};
777                         my %pf = ();
778                         my @p = split(/,/, ($i =~ /^[^:]*:([^:]*):/,$1));
779                         my @a = split(/,/, ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1));
780                         # @p_purged must contain hardware platforms only
781                         my @p_purged = ();
782                         foreach $ptmp (@p) {
783                                 next if $ptmp =~ /^!?RSAREF$/;
784                                 push @p_purged, $ptmp;
785                         }
786                         my $negatives = !!grep(/^!/,@p);
787                         # It is very important to check NT before W32
788                         if ((($NT && (!@p_purged
789                                       || (!$negatives && grep(/^WINNT$/,@p))
790                                       || ($negatives && !grep(/^!WINNT$/,@p))))
791                              || ($W32 && (!@p_purged
792                                           || (!$negatives && grep(/^WIN32$/,@p))
793                                           || ($negatives && !grep(/^!WIN32$/,@p))))
794                              || ($W16 && (!@p_purged
795                                           || (!$negatives && grep(/^WIN16$/,@p))
796                                           || ($negatives && !grep(/^!WIN16$/,@p)))))
797                             && (!@p
798                                 || (!$negatives
799                                     && ($rsaref || !grep(/^RSAREF$/,@p)))
800                                 || ($negatives
801                                     && (!$rsaref || !grep(/^!RSAREF$/,@p))))
802                             && (!@a || (!$no_rc2 || !grep(/^RC2$/,@a)))
803                             && (!@a || (!$no_rc4 || !grep(/^RC4$/,@a)))
804                             && (!@a || (!$no_rc5 || !grep(/^RC5$/,@a)))
805                             && (!@a || (!$no_idea || !grep(/^IDEA$/,@a)))
806                             && (!@a || (!$no_des || !grep(/^DES$/,@a)))
807                             && (!@a || (!$no_bf || !grep(/^BF$/,@a)))
808                             && (!@a || (!$no_cast || !grep(/^CAST$/,@a)))
809                             && (!@a || (!$no_md2 || !grep(/^MD2$/,@a)))
810                             && (!@a || (!$no_md4 || !grep(/^MD4$/,@a)))
811                             && (!@a || (!$no_md5 || !grep(/^MD5$/,@a)))
812                             && (!@a || (!$no_sha || !grep(/^SHA$/,@a)))
813                             && (!@a || (!$no_ripemd || !grep(/^RIPEMD$/,@a)))
814                             && (!@a || (!$no_mdc2 || !grep(/^MDC2$/,@a)))
815                             && (!@a || (!$no_rsa || !grep(/^RSA$/,@a)))
816                             && (!@a || (!$no_dsa || !grep(/^DSA$/,@a)))
817                             && (!@a || (!$no_dh || !grep(/^DH$/,@a)))
818                             && (!@a || (!$no_hmac || !grep(/^HMAC$/,@a)))
819                             && (!@a || (!$no_rijndael || !grep(/^RIJNDAEL$/,@a)))
820                             && (!@a || (!$no_fp_api || !grep(/^FP_API$/,@a)))
821                             ) {
822                                 if($v) {
823                                         printf OUT "    %s%-40s@%-8d DATA\n",($W32)?"":"_",$s,$n;
824                                 } else {
825                                         printf OUT "    %s%-40s@%d\n",($W32)?"":"_",$s,$n;
826                                 }
827 #                       } else {
828 #                               print STDERR "DEBUG: \"$sym\" (@p):",
829 #                               " rsaref:", !!(!@p
830 #                                              || (!$negatives
831 #                                                  && ($rsaref || !grep(/^RSAREF$/,@p)))
832 #                                              || ($negatives
833 #                                                  && (!$rsaref || !grep(/^!RSAREF$/,@p))))?1:0,
834 #                               " 16:", !!($W16 && (!@p_purged
835 #                                                   || (!$negatives && grep(/^WIN16$/,@p))
836 #                                                   || ($negatives && !grep(/^!WIN16$/,@p)))),
837 #                               " 32:", !!($W32 && (!@p_purged
838 #                                                   || (!$negatives && grep(/^WIN32$/,@p))
839 #                                                   || ($negatives && !grep(/^!WIN32$/,@p)))),
840 #                               " NT:", !!($NT && (!@p_purged
841 #                                                  || (!$negatives && grep(/^WINNT$/,@p))
842 #                                                  || ($negatives && !grep(/^!WINNT$/,@p)))),
843 #                               "\n";
844                         }
845                 }
846         }
847         printf OUT "\n";
848 }
849
850 sub load_numbers
851 {
852         my($name)=@_;
853         my(@a,%ret);
854
855         $max_num = 0;
856         $num_noinfo = 0;
857         $prev = "";
858
859         open(IN,"<$name") || die "unable to open $name:$!\n";
860         while (<IN>) {
861                 chop;
862                 s/#.*$//;
863                 next if /^\s*$/;
864                 @a=split;
865                 if (defined $ret{$a[0]}) {
866                         print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
867                 }
868                 if ($max_num > $a[1]) {
869                         print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
870                 }
871                 if ($max_num == $a[1]) {
872                         # This is actually perfectly OK
873                         #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
874                 }
875                 if ($#a < 2) {
876                         # Existence will be proven later, in do_defs
877                         $ret{$a[0]}=$a[1];
878                         $num_noinfo++;
879                 } else {
880                         $ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
881                 }
882                 $max_num = $a[1] if $a[1] > $max_num;
883                 $prev=$a[0];
884         }
885         if ($num_noinfo) {
886                 print STDERR "Warning: $num_noinfo symbols were without info.";
887                 if ($do_rewrite) {
888                         printf STDERR "  The rewrite will fix this.\n";
889                 } else {
890                         printf STDERR "  You should do a rewrite to fix this.\n";
891                 }
892         }
893         close(IN);
894         return(%ret);
895 }
896
897 sub parse_number
898 {
899         (my $str, my $what) = @_;
900         (my $n, my $i) = split(/\\/,$str);
901         if ($what eq "n") {
902                 return $n;
903         } else {
904                 return $i;
905         }
906 }
907
908 sub rewrite_numbers
909 {
910         (*OUT,$name,*nums,@symbols)=@_;
911         my $thing;
912
913         print STDERR "Rewriting $name\n";
914
915         my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
916         my $r; my %r; my %rsyms;
917         foreach $r (@r) {
918                 (my $s, my $i) = split /\\/, $r;
919                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
920                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
921                 $r{$a} = $s."\\".$i;
922                 $rsyms{$s} = 1;
923         }
924
925         my %syms = ();
926         foreach $_ (@symbols) {
927                 (my $n, my $i) = split /\\/;
928                 $syms{$n} = 1;
929         }
930
931         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
932         foreach $sym (@s) {
933                 (my $n, my $i) = split /\\/, $nums{$sym};
934                 next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
935                 next if defined($rsyms{$sym});
936                 $i="NOEXIST::FUNCTION:"
937                         if !defined($i) || $i eq "" || !defined($syms{$sym});
938                 printf OUT "%s%-40s%d\t%s\n","",$sym,$n,$i;
939                 if (exists $r{$sym}) {
940                         (my $s, $i) = split /\\/,$r{$sym};
941                         printf OUT "%s%-40s%d\t%s\n","",$s,$n,$i;
942                 }
943         }
944 }
945
946 sub update_numbers
947 {
948         (*OUT,$name,*nums,my $start_num, my @symbols)=@_;
949         my $new_syms = 0;
950
951         print STDERR "Updating $name numbers\n";
952
953         my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
954         my $r; my %r; my %rsyms;
955         foreach $r (@r) {
956                 (my $s, my $i) = split /\\/, $r;
957                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
958                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
959                 $r{$a} = $s."\\".$i;
960                 $rsyms{$s} = 1;
961         }
962
963         foreach $sym (@symbols) {
964                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
965                 next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
966                 next if defined($rsyms{$sym});
967                 die "ERROR: Symbol $sym had no info attached to it."
968                     if $i eq "";
969                 if (!exists $nums{$s}) {
970                         $new_syms++;
971                         printf OUT "%s%-40s%d\t%s\n","",$s, ++$start_num,$i;
972                         if (exists $r{$s}) {
973                                 ($s, $i) = split /\\/,$r{$s};
974                                 printf OUT "%s%-40s%d\t%s\n","",$s, $start_num,$i;
975                         }
976                 }
977         }
978         if($new_syms) {
979                 print STDERR "$new_syms New symbols added\n";
980         } else {
981                 print STDERR "No New symbols Added\n";
982         }
983 }
984
985 sub check_existing
986 {
987         (*nums, my @symbols)=@_;
988         my %existing; my @remaining;
989         @remaining=();
990         foreach $sym (@symbols) {
991                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
992                 $existing{$s}=1;
993         }
994         foreach $sym (keys %nums) {
995                 if (!exists $existing{$sym}) {
996                         push @remaining, $sym;
997                 }
998         }
999         if(@remaining) {
1000                 print STDERR "The following symbols do not seem to exist:\n";
1001                 foreach $sym (@remaining) {
1002                         print STDERR "\t",$sym,"\n";
1003                 }
1004         }
1005 }
1006