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