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