Various updates to mkdef.pl to cope with new aes
[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 $do_checkexist = 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", "AES",
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_aes;
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
115         $do_ssl=1 if $_ eq "ssleay";
116         $do_ssl=1 if $_ eq "ssl";
117         $do_crypto=1 if $_ eq "libeay";
118         $do_crypto=1 if $_ eq "crypto";
119         $do_update=1 if $_ eq "update";
120         $do_rewrite=1 if $_ eq "rewrite";
121         $do_ctest=1 if $_ eq "ctest";
122         $do_ctestall=1 if $_ eq "ctestall";
123         $do_checkexist=1 if $_ eq "exist";
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-aes$/)      { $no_aes=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_aes;
195 $crypto.=" crypto/rijndael/rd_fst.h" unless $no_aes;
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_checkexist) {
265         &check_existing(*ssl_list, @ssl_symbols)
266                 if $do_ssl == 1;
267         &check_existing(*crypto_list, @crypto_symbols)
268                 if $do_crypto == 1;
269 } elsif ($do_ctest || $do_ctestall) {
270
271         print <<"EOF";
272
273 /* Test file to check all DEF file symbols are present by trying
274  * to link to all of them. This is *not* intended to be run!
275  */
276
277 int main()
278 {
279 EOF
280         &print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols)
281                 if $do_ssl == 1;
282
283         &print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols)
284                 if $do_crypto == 1;
285
286         print "}\n";
287
288 } else {
289
290         &print_def_file(*STDOUT,"SSLEAY",*ssl_list,@ssl_symbols)
291                 if $do_ssl == 1;
292
293         &print_def_file(*STDOUT,"LIBEAY",*crypto_list,@crypto_symbols)
294                 if $do_crypto == 1;
295
296 }
297
298
299 sub do_defs
300 {
301         my($name,$files,$symhacksfile)=@_;
302         my $file;
303         my @ret;
304         my %syms;
305         my %platform;           # For anything undefined, we assume ""
306         my %kind;               # For anything undefined, we assume "FUNCTION"
307         my %algorithm;          # For anything undefined, we assume ""
308         my %rename;
309         my $cpp;
310         my %unknown_algorithms = ();
311
312         foreach $file (split(/\s+/,$symhacksfile." ".$files))
313                 {
314                 open(IN,"<$file") || die "unable to open $file:$!\n";
315                 my $line = "", my $def= "";
316                 my %tag = (
317                         (map { $_ => 0 } @known_platforms),
318                         (map { "NO_".$_ => 0 } @known_algorithms),
319                         NOPROTO         => 0,
320                         PERL5           => 0,
321                         _WINDLL         => 0,
322                         CONST_STRICT    => 0,
323                         TRUE            => 1,
324                 );
325                 my $symhacking = $file eq $symhacksfile;
326                 while(<IN>) {
327                         last if (/BEGIN ERROR CODES/);
328                         if ($line ne '') {
329                                 $_ = $line . $_;
330                                 $line = '';
331                         }
332
333                         if (/\\$/) {
334                                 $line = $_;
335                                 next;
336                         }
337
338                         $cpp = 1 if /^\#.*ifdef.*cplusplus/;
339                         if ($cpp) {
340                                 $cpp = 0 if /^\#.*endif/;
341                                 next;
342                         }
343
344                         s/\/\*.*?\*\///gs;                   # ignore comments
345                         s/{[^{}]*}//gs;                      # ignore {} blocks
346                         #print STDERR "DEBUG: \$_=\"$_\"\n";
347                         if (/^\#\s*ifndef\s+(.*)/) {
348                                 push(@tag,$1);
349                                 $tag{$1}=-1;
350                         } elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) {
351                                 push(@tag,$1);
352                                 $tag{$1}=-1;
353                         } elsif (/^\#\s*ifdef\s+(.*)/) {
354                                 push(@tag,$1);
355                                 $tag{$1}=1;
356                         } elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) {
357                                 push(@tag,$1);
358                                 $tag{$1}=1;
359                         } elsif (/^\#\s*error\s+(\w+) is disabled\./) {
360                                 if ($tag[$#tag] eq "NO_".$1) {
361                                         $tag{$tag[$#tag]}=2;
362                                 }
363                         } elsif (/^\#\s*endif/) {
364                                 my $oldtag=$tag[$#tag];
365                                 #print STDERR "DEBUG: \$oldtag=\"$oldtag\"\n";
366                                 if ($tag{$tag[$#tag]}==2) {
367                                         $tag{$tag[$#tag]}=-1;
368                                 } else {
369                                         $tag{$tag[$#tag]}=0;
370                                 }
371                                 pop(@tag);
372                                 if ($oldtag =~ /^NO_([A-Z0-9_]+)$/) {
373                                         $oldtag=$1;
374                                 } else {
375                                         $oldtag="";
376                                 }
377                                 if ($oldtag ne ""
378                                     && !grep(/^$oldtag$/, @known_algorithms)) {
379                                         $unknown_algorithms{$oldtag} = 1;
380                                         #print STDERR "DEBUG: Added as unknown algorithm: $oldtag\n";
381                                 }
382                         } elsif (/^\#\s*else/) {
383                                 my $t=$tag[$#tag];
384                                 $tag{$t}= -$tag{$t};
385                         } elsif (/^\#\s*if\s+1/) {
386                                 # Dummy tag
387                                 push(@tag,"TRUE");
388                                 $tag{"TRUE"}=1;
389                         } elsif (/^\#\s*if\s+0/) {
390                                 # Dummy tag
391                                 push(@tag,"TRUE");
392                                 $tag{"TRUE"}=-1;
393                         } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/
394                                  && $symhacking) {
395                                 my $s = $1;
396                                 my $a =
397                                     $2.":".join(",", grep(!/^$/,
398                                                           map { $tag{$_} == 1 ?
399                                                                     $_ : "" }
400                                                           @known_platforms));
401                                 $rename{$s} = $a;
402                         }
403                         if (/^\#/) {
404                                 my @p = grep(!/^$/,
405                                              map { $tag{$_} == 1 ? $_ :
406                                                        $tag{$_} == -1 ? "!".$_  : "" }
407                                              @known_platforms);
408                                 my @a = grep(!/^$/,
409                                              map { $tag{"NO_".$_} == -1 ? $_ : "" }
410                                              @known_algorithms);
411                                 $def .= "#INFO:".join(',',@p).":".join(',',@a).";";
412                                 next;
413                         }
414                         if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) {
415                                 next;
416                         } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
417                                 $syms{"d2i_$3"} = 1;
418                                 $syms{"i2d_$3"} = 1;
419                                 $syms{"$2_it"} = 1;
420                                 $kind{"$2_it"} = "VARIABLE";
421                                 next;
422                         } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) {
423                                 $syms{"d2i_$3"} = 1;
424                                 $syms{"i2d_$3"} = 1;
425                                 $syms{"$3_new"} = 1;
426                                 $syms{"$3_free"} = 1;
427                                 $syms{"$2_it"} = 1;
428                                 $kind{"$2_it"} = "VARIABLE";
429                         } elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ ||
430                                 /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) {
431                                 $syms{"d2i_$1"} = 1;
432                                 $syms{"i2d_$1"} = 1;
433                                 $syms{"$1_new"} = 1;
434                                 $syms{"$1_free"} = 1;
435                                 $syms{"$1_it"} = 1;
436                                 $kind{"$1_it"} = "VARIABLE";
437                                 next;
438                         } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
439                                 $syms{"d2i_$2"} = 1;
440                                 $syms{"i2d_$2"} = 1;
441                                 $syms{"$2_it"} = 1;
442                                 $kind{"$2_it"} = "VARIABLE";
443                                 next;
444                         } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) {
445                                 $syms{"d2i_$2"} = 1;
446                                 $syms{"i2d_$2"} = 1;
447                                 $syms{"$2_new"} = 1;
448                                 $syms{"$2_free"} = 1;
449                                 $syms{"$2_it"} = 1;
450                                 $kind{"$2_it"} = "VARIABLE";
451                                 next;
452                         } elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*,(\w*)\s*\)/) {
453                                 $syms{"$1_it"} = 1;
454                                 $kind{"$1_it"} = "VARIABLE";
455                                 next;
456                         } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) {
457                                 next;
458                         } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) {
459                                 next;
460                         } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ ||
461                                  /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ ) {
462                                 # Things not in Win16
463                                 $syms{"PEM_read_${1}"} = 1;
464                                 $platform{"PEM_read_${1}"} = "!WIN16";
465                                 $syms{"PEM_write_${1}"} = 1;
466                                 $platform{"PEM_write_${1}"} = "!WIN16";
467                                 # Things that are everywhere
468                                 $syms{"PEM_read_bio_${1}"} = 1;
469                                 $syms{"PEM_write_bio_${1}"} = 1;
470                                 if ($1 eq "RSAPrivateKey" ||
471                                     $1 eq "RSAPublicKey" ||
472                                     $1 eq "RSA_PUBKEY") {
473                                         $algorithm{"PEM_read_${1}"} = "RSA";
474                                         $algorithm{"PEM_write_${1}"} = "RSA";
475                                         $algorithm{"PEM_read_bio_${1}"} = "RSA";
476                                         $algorithm{"PEM_write_bio_${1}"} = "RSA";
477                                 }
478                                 elsif ($1 eq "DSAPrivateKey" ||
479                                        $1 eq "DSAparams" ||
480                                        $1 eq "RSA_PUBKEY") {
481                                         $algorithm{"PEM_read_${1}"} = "DSA";
482                                         $algorithm{"PEM_write_${1}"} = "DSA";
483                                         $algorithm{"PEM_read_bio_${1}"} = "DSA";
484                                         $algorithm{"PEM_write_bio_${1}"} = "DSA";
485                                 }
486                                 elsif ($1 eq "DHparams") {
487                                         $algorithm{"PEM_read_${1}"} = "DH";
488                                         $algorithm{"PEM_write_${1}"} = "DH";
489                                         $algorithm{"PEM_read_bio_${1}"} = "DH";
490                                         $algorithm{"PEM_write_bio_${1}"} = "DH";
491                                 }
492                         } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ ||
493                                      /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) {
494                                 # Things not in Win16
495                                 $syms{"PEM_write_${1}"} = 1;
496                                 $platform{"PEM_write_${1}"} .= ",!WIN16";
497                                 # Things that are everywhere
498                                 $syms{"PEM_write_bio_${1}"} = 1;
499                                 if ($1 eq "RSAPrivateKey" ||
500                                     $1 eq "RSAPublicKey" ||
501                                     $1 eq "RSA_PUBKEY") {
502                                         $algorithm{"PEM_write_${1}"} = "RSA";
503                                         $algorithm{"PEM_write_bio_${1}"} = "RSA";
504                                 }
505                                 elsif ($1 eq "DSAPrivateKey" ||
506                                        $1 eq "DSAparams" ||
507                                        $1 eq "RSA_PUBKEY") {
508                                         $algorithm{"PEM_write_${1}"} = "DSA";
509                                         $algorithm{"PEM_write_bio_${1}"} = "DSA";
510                                 }
511                                 elsif ($1 eq "DHparams") {
512                                         $algorithm{"PEM_write_${1}"} = "DH";
513                                         $algorithm{"PEM_write_bio_${1}"} = "DH";
514                                 }
515                         } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ ||
516                                      /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) {
517                                 # Things not in Win16
518                                 $syms{"PEM_read_${1}"} = 1;
519                                 $platform{"PEM_read_${1}"} .= ",!WIN16";
520                                 # Things that are everywhere
521                                 $syms{"PEM_read_bio_${1}"} = 1;
522                         } elsif (
523                                 ($tag{'TRUE'} != -1)
524                                 && ($tag{'CONST_STRICT'} != 1)
525                                  )
526                                 {
527                                         if (/\{|\/\*|\([^\)]*$/) {
528                                                 $line = $_;
529                                         } else {
530                                                 $def .= $_;
531                                         }
532                                 }
533                         }
534                 close(IN);
535
536                 my $algs;
537                 my $plays;
538
539                 foreach (split /;/, $def) {
540                         my $s; my $k = "FUNCTION"; my $p; my $a;
541                         s/^[\n\s]*//g;
542                         s/[\n\s]*$//g;
543                         next if(/\#undef/);
544                         next if(/typedef\W/);
545                         next if(/\#define/);
546
547                         if (/^\#INFO:([^:]*):(.*)$/) {
548                                 $plats = $1;
549                                 $algs = $2;
550                                 next;
551                         } elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+)(\[[0-9]*\])*\s*$/) {
552                                 $s = $1;
553                                 $k = "VARIABLE";
554                         } elsif (/\(\*(\w*)\([^\)]+/) {
555                                 $s = $1;
556                         } elsif (/\w+\W+(\w+)\W*\(\s*\)$/s) {
557                                 # K&R C
558                                 next;
559                         } elsif (/\w+\W+\w+\W*\(.*\)$/s) {
560                                 while (not /\(\)$/s) {
561                                         s/[^\(\)]*\)$/\)/s;
562                                         s/\([^\(\)]*\)\)$/\)/s;
563                                 }
564                                 s/\(void\)//;
565                                 /(\w+)\W*\(\)/s;
566                                 $s = $1;
567                         } elsif (/\(/ and not (/=/)) {
568                                 print STDERR "File $file: cannot parse: $_;\n";
569                                 next;
570                         } else {
571                                 next;
572                         }
573
574                         $syms{$s} = 1;
575                         $kind{$s} = $k;
576
577                         $p = $plats;
578                         $a = $algs;
579                         $a .= ",BF" if($s =~ /EVP_bf/);
580                         $a .= ",CAST" if($s =~ /EVP_cast/);
581                         $a .= ",DES" if($s =~ /EVP_des/);
582                         $a .= ",DSA" if($s =~ /EVP_dss/);
583                         $a .= ",IDEA" if($s =~ /EVP_idea/);
584                         $a .= ",MD2" if($s =~ /EVP_md2/);
585                         $a .= ",MD4" if($s =~ /EVP_md4/);
586                         $a .= ",MD5" if($s =~ /EVP_md5/);
587                         $a .= ",RC2" if($s =~ /EVP_rc2/);
588                         $a .= ",RC4" if($s =~ /EVP_rc4/);
589                         $a .= ",RC5" if($s =~ /EVP_rc5/);
590                         $a .= ",RIPEMD" if($s =~ /EVP_ripemd/);
591                         $a .= ",SHA" if($s =~ /EVP_sha/);
592                         $a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/);
593                         $a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/);
594                         $a .= ",RSA" if($s =~ /RSAPrivateKey/);
595                         $a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/);
596
597                         $platform{$s} .= ','.$p;
598                         $algorithm{$s} .= ','.$a;
599
600                         if (defined($rename{$s})) {
601                                 (my $r, my $p) = split(/:/,$rename{$s});
602                                 my @ip = map { /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p;
603                                 $syms{$r} = 1;
604                                 $kind{$r} = $kind{$s}."(".$s.")";
605                                 $algorithm{$r} = $algorithm{$s};
606                                 $platform{$r} = $platform{$s}.",".$p;
607                                 $platform{$s} .= ','.join(',', @ip).','.join(',', @ip);
608                         }
609                 }
610         }
611
612         # Prune the returned symbols
613
614         $platform{"crypt"} .= ",!PERL5,!__FreeBSD__,!NeXT";
615
616         delete $syms{"SSL_add_dir_cert_subjects_to_stack"};
617         delete $syms{"bn_dump1"};
618
619         $platform{"BIO_s_file_internal"} .= ",WIN16";
620         $platform{"BIO_new_file_internal"} .= ",WIN16";
621         $platform{"BIO_new_fp_internal"} .= ",WIN16";
622
623         $platform{"BIO_s_file"} .= ",!WIN16";
624         $platform{"BIO_new_file"} .= ",!WIN16";
625         $platform{"BIO_new_fp"} .= ",!WIN16";
626
627         $platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh";
628
629         if(exists $syms{"ERR_load_CRYPTO_strings"}) {
630                 $platform{"ERR_load_CRYPTO_strings"} .= ",!VMS,!WIN16";
631                 $syms{"ERR_load_CRYPTOlib_strings"} = 1;
632                 $platform{"ERR_load_CRYPTOlib_strings"} .= ",VMS,WIN16";
633         }
634
635         # Info we know about
636
637         push @ret, map { $_."\\".&info_string($_,"EXIST",
638                                               $platform{$_},
639                                               $kind{$_},
640                                               $algorithm{$_}) } keys %syms;
641
642         if (keys %unknown_algorithms) {
643                 print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n";
644                 print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n";
645         }
646         return(@ret);
647 }
648
649 sub info_string {
650         (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_;
651
652         my %a = defined($algorithms) ?
653             map { $_ => 1 } split /,/, $algorithms : ();
654         my $pl = defined($platforms) ? $platforms : "";
655         my %p = map { $_ => 0 } split /,/, $pl;
656         my $k = defined($kind) ? $kind : "FUNCTION";
657         my $ret;
658
659         # We do this, because if there's code like the following, it really
660         # means the function exists in all cases and should therefore be
661         # everywhere.  By increasing and decreasing, we may attain 0:
662         #
663         # ifndef WIN16
664         #    int foo();
665         # else
666         #    int _fat foo();
667         # endif
668         foreach $platform (split /,/, $pl) {
669                 if ($platform =~ /^!(.*)$/) {
670                         $p{$1}--;
671                 } else {
672                         $p{$platform}++;
673                 }
674         }
675         foreach $platform (keys %p) {
676                 if ($p{$platform} == 0) { delete $p{$platform}; }
677         }
678
679         delete $p{""};
680         delete $a{""};
681
682         $ret = $exist;
683         $ret .= ":".join(',',map { $p{$_} < 0 ? "!".$_ : $_ } keys %p);
684         $ret .= ":".$k;
685         $ret .= ":".join(',',keys %a);
686         return $ret;
687 }
688
689 sub maybe_add_info {
690         (my $name, *nums, my @symbols) = @_;
691         my $sym;
692         my $new_info = 0;
693         my %syms=();
694
695         print STDERR "Updating $name info\n";
696         foreach $sym (@symbols) {
697                 (my $s, my $i) = split /\\/, $sym;
698                 $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/;
699                 if (defined($nums{$s})) {
700                         (my $n, my $dummy) = split /\\/, $nums{$s};
701                         if (!defined($dummy) || $i ne $dummy) {
702                                 $nums{$s} = $n."\\".$i;
703                                 $new_info++;
704                                 #print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n";
705                         }
706                 }
707                 $syms{sym} = 1;
708         }
709
710         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
711         foreach $sym (@s) {
712                 (my $n, my $i) = split /\\/, $nums{$sym};
713                 if (!defined($syms{sym})) {
714                         $new_info++;
715                         #print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n";
716                 }
717         }
718         if ($new_info) {
719                 print STDERR "$new_info old symbols got an info update\n";
720                 if (!$do_rewrite) {
721                         print STDERR "You should do a rewrite to fix this.\n";
722                 }
723         } else {
724                 print STDERR "No old symbols needed info update\n";
725         }
726 }
727
728 sub print_test_file
729 {
730         (*OUT,my $name,*nums,my @symbols)=@_;
731         my $n = 1; my @e; my @r;
732         my $sym; my $prev = ""; my $prefSSLeay;
733
734         (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
735         (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
736         @symbols=((sort @e),(sort @r));
737
738         foreach $sym (@symbols) {
739                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
740                 if ($s ne $prev) {
741                         if (!defined($nums{$sym})) {
742                                 printf STDERR "Warning: $sym does not have a number assigned\n"
743                                                 if(!$do_update);
744                         } else {
745                                 $n=$nums{$s};
746                                 print OUT "\t$s();\n";
747                         }
748                 }
749                 $prev = $s;     # To avoid duplicates...
750         }
751 }
752
753 sub print_def_file
754 {
755         (*OUT,my $name,*nums,my @symbols)=@_;
756         my $n = 1; my @e; my @r; my @v;
757
758         if ($W32)
759                 { $name.="32"; }
760         else
761                 { $name.="16"; }
762
763         print OUT <<"EOF";
764 ;
765 ; Definition file for the DLL version of the $name library from OpenSSL
766 ;
767
768 LIBRARY         $name
769
770 DESCRIPTION     'OpenSSL $name - http://www.openssl.org/'
771
772 EOF
773
774         if (!$W32) {
775                 print <<"EOF";
776 CODE            PRELOAD MOVEABLE
777 DATA            PRELOAD MOVEABLE SINGLE
778
779 EXETYPE         WINDOWS
780
781 HEAPSIZE        4096
782 STACKSIZE       8192
783
784 EOF
785         }
786
787         print "EXPORTS\n";
788
789         (@e)=grep(/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
790         (@r)=grep(/^\w+\\.*?:.*?:FUNCTION/ && !/^SSLeay\\.*?:.*?:FUNCTION/,@symbols);
791         (@v)=grep(/^\w+\\.*?:.*?:VARIABLE/,@symbols);
792         @symbols=((sort @e),(sort @r), (sort @v));
793
794
795         foreach $sym (@symbols) {
796                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
797                 my $v = 0;
798                 $v = 1 if $sym=~ /^\w+\\.*?:.*?:VARIABLE/;
799                 if (!defined($nums{$s})) {
800                         printf STDERR "Warning: $s does not have a number assigned\n"
801                                         if(!$do_update);
802                 } else {
803                         (my $n, my $i) = split /\\/, $nums{$s};
804                         my %pf = ();
805                         my @p = split(/,/, ($i =~ /^[^:]*:([^:]*):/,$1));
806                         my @a = split(/,/, ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1));
807                         # @p_purged must contain hardware platforms only
808                         my @p_purged = ();
809                         foreach $ptmp (@p) {
810                                 push @p_purged, $ptmp;
811                         }
812                         my $negatives = !!grep(/^!/,@p);
813                         # It is very important to check NT before W32
814                         if ((($NT && (!@p_purged
815                                       || (!$negatives && grep(/^WINNT$/,@p))
816                                       || ($negatives && !grep(/^!WINNT$/,@p))))
817                              || ($W32 && (!@p_purged
818                                           || (!$negatives && grep(/^WIN32$/,@p))
819                                           || ($negatives && !grep(/^!WIN32$/,@p))))
820                              || ($W16 && (!@p_purged
821                                           || (!$negatives && grep(/^WIN16$/,@p))
822                                           || ($negatives && !grep(/^!WIN16$/,@p)))))
823                             && (!@a || (!$no_rc2 || !grep(/^RC2$/,@a)))
824                             && (!@a || (!$no_rc4 || !grep(/^RC4$/,@a)))
825                             && (!@a || (!$no_rc5 || !grep(/^RC5$/,@a)))
826                             && (!@a || (!$no_idea || !grep(/^IDEA$/,@a)))
827                             && (!@a || (!$no_des || !grep(/^DES$/,@a)))
828                             && (!@a || (!$no_bf || !grep(/^BF$/,@a)))
829                             && (!@a || (!$no_cast || !grep(/^CAST$/,@a)))
830                             && (!@a || (!$no_md2 || !grep(/^MD2$/,@a)))
831                             && (!@a || (!$no_md4 || !grep(/^MD4$/,@a)))
832                             && (!@a || (!$no_md5 || !grep(/^MD5$/,@a)))
833                             && (!@a || (!$no_sha || !grep(/^SHA$/,@a)))
834                             && (!@a || (!$no_ripemd || !grep(/^RIPEMD$/,@a)))
835                             && (!@a || (!$no_mdc2 || !grep(/^MDC2$/,@a)))
836                             && (!@a || (!$no_rsa || !grep(/^RSA$/,@a)))
837                             && (!@a || (!$no_dsa || !grep(/^DSA$/,@a)))
838                             && (!@a || (!$no_dh || !grep(/^DH$/,@a)))
839                             && (!@a || (!$no_hmac || !grep(/^HMAC$/,@a)))
840                             && (!@a || (!$no_aes || !grep(/^AES$/,@a)))
841                             && (!@a || (!$no_fp_api || !grep(/^FP_API$/,@a)))
842                             ) {
843                                 if($v) {
844                                         printf OUT "    %s%-40s@%-8d DATA\n",($W32)?"":"_",$s,$n;
845                                 } else {
846                                         printf OUT "    %s%-40s@%d\n",($W32)?"":"_",$s,$n;
847                                 }
848 #                       } else {
849 #                               print STDERR "DEBUG: \"$sym\" (@p):",
850 #                               " rsaref:", !!(!@p
851 #                                              || (!$negatives
852 #                                                  && ($rsaref || !grep(/^RSAREF$/,@p)))
853 #                                              || ($negatives
854 #                                                  && (!$rsaref || !grep(/^!RSAREF$/,@p))))?1:0,
855 #                               " 16:", !!($W16 && (!@p_purged
856 #                                                   || (!$negatives && grep(/^WIN16$/,@p))
857 #                                                   || ($negatives && !grep(/^!WIN16$/,@p)))),
858 #                               " 32:", !!($W32 && (!@p_purged
859 #                                                   || (!$negatives && grep(/^WIN32$/,@p))
860 #                                                   || ($negatives && !grep(/^!WIN32$/,@p)))),
861 #                               " NT:", !!($NT && (!@p_purged
862 #                                                  || (!$negatives && grep(/^WINNT$/,@p))
863 #                                                  || ($negatives && !grep(/^!WINNT$/,@p)))),
864 #                               "\n";
865                         }
866                 }
867         }
868         printf OUT "\n";
869 }
870
871 sub load_numbers
872 {
873         my($name)=@_;
874         my(@a,%ret);
875
876         $max_num = 0;
877         $num_noinfo = 0;
878         $prev = "";
879
880         open(IN,"<$name") || die "unable to open $name:$!\n";
881         while (<IN>) {
882                 chop;
883                 s/#.*$//;
884                 next if /^\s*$/;
885                 @a=split;
886                 if (defined $ret{$a[0]}) {
887                         print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n";
888                 }
889                 if ($max_num > $a[1]) {
890                         print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n";
891                 }
892                 if ($max_num == $a[1]) {
893                         # This is actually perfectly OK
894                         #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n";
895                 }
896                 if ($#a < 2) {
897                         # Existence will be proven later, in do_defs
898                         $ret{$a[0]}=$a[1];
899                         $num_noinfo++;
900                 } else {
901                         $ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker
902                 }
903                 $max_num = $a[1] if $a[1] > $max_num;
904                 $prev=$a[0];
905         }
906         if ($num_noinfo) {
907                 print STDERR "Warning: $num_noinfo symbols were without info.";
908                 if ($do_rewrite) {
909                         printf STDERR "  The rewrite will fix this.\n";
910                 } else {
911                         printf STDERR "  You should do a rewrite to fix this.\n";
912                 }
913         }
914         close(IN);
915         return(%ret);
916 }
917
918 sub parse_number
919 {
920         (my $str, my $what) = @_;
921         (my $n, my $i) = split(/\\/,$str);
922         if ($what eq "n") {
923                 return $n;
924         } else {
925                 return $i;
926         }
927 }
928
929 sub rewrite_numbers
930 {
931         (*OUT,$name,*nums,@symbols)=@_;
932         my $thing;
933
934         print STDERR "Rewriting $name\n";
935
936         my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
937         my $r; my %r; my %rsyms;
938         foreach $r (@r) {
939                 (my $s, my $i) = split /\\/, $r;
940                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
941                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
942                 $r{$a} = $s."\\".$i;
943                 $rsyms{$s} = 1;
944         }
945
946         my %syms = ();
947         foreach $_ (@symbols) {
948                 (my $n, my $i) = split /\\/;
949                 $syms{$n} = 1;
950         }
951
952         my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums;
953         foreach $sym (@s) {
954                 (my $n, my $i) = split /\\/, $nums{$sym};
955                 next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/;
956                 next if defined($rsyms{$sym});
957                 $i="NOEXIST::FUNCTION:"
958                         if !defined($i) || $i eq "" || !defined($syms{$sym});
959                 printf OUT "%s%-40s%d\t%s\n","",$sym,$n,$i;
960                 if (exists $r{$sym}) {
961                         (my $s, $i) = split /\\/,$r{$sym};
962                         printf OUT "%s%-40s%d\t%s\n","",$s,$n,$i;
963                 }
964         }
965 }
966
967 sub update_numbers
968 {
969         (*OUT,$name,*nums,my $start_num, my @symbols)=@_;
970         my $new_syms = 0;
971
972         print STDERR "Updating $name numbers\n";
973
974         my @r = grep(/^\w+\\.*?:.*?:\w+\(\w+\)/,@symbols);
975         my $r; my %r; my %rsyms;
976         foreach $r (@r) {
977                 (my $s, my $i) = split /\\/, $r;
978                 my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/;
979                 $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/;
980                 $r{$a} = $s."\\".$i;
981                 $rsyms{$s} = 1;
982         }
983
984         foreach $sym (@symbols) {
985                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
986                 next if $i =~ /^.*?:.*?:\w+\(\w+\)/;
987                 next if defined($rsyms{$sym});
988                 die "ERROR: Symbol $sym had no info attached to it."
989                     if $i eq "";
990                 if (!exists $nums{$s}) {
991                         $new_syms++;
992                         printf OUT "%s%-40s%d\t%s\n","",$s, ++$start_num,$i;
993                         if (exists $r{$s}) {
994                                 ($s, $i) = split /\\/,$r{$s};
995                                 printf OUT "%s%-40s%d\t%s\n","",$s, $start_num,$i;
996                         }
997                 }
998         }
999         if($new_syms) {
1000                 print STDERR "$new_syms New symbols added\n";
1001         } else {
1002                 print STDERR "No New symbols Added\n";
1003         }
1004 }
1005
1006 sub check_existing
1007 {
1008         (*nums, my @symbols)=@_;
1009         my %existing; my @remaining;
1010         @remaining=();
1011         foreach $sym (@symbols) {
1012                 (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/;
1013                 $existing{$s}=1;
1014         }
1015         foreach $sym (keys %nums) {
1016                 if (!exists $existing{$sym}) {
1017                         push @remaining, $sym;
1018                 }
1019         }
1020         if(@remaining) {
1021                 print STDERR "The following symbols do not seem to exist:\n";
1022                 foreach $sym (@remaining) {
1023                         print STDERR "\t",$sym,"\n";
1024                 }
1025         }
1026 }
1027