mkdef.pl revision 194206
155714Skris#!/usr/local/bin/perl -w 255714Skris# 355714Skris# generate a .def file 455714Skris# 555714Skris# It does this by parsing the header files and looking for the 655714Skris# prototyped functions: it then prunes the output. 755714Skris# 868651Skris# Intermediary files are created, call libeay.num and ssleay.num,... 968651Skris# Previously, they had the following format: 1068651Skris# 1168651Skris# routine-name nnnn 1268651Skris# 1368651Skris# But that isn't enough for a number of reasons, the first on being that 1468651Skris# this format is (needlessly) very Win32-centric, and even then... 1568651Skris# One of the biggest problems is that there's no information about what 1668651Skris# routines should actually be used, which varies with what crypto algorithms 1768651Skris# are disabled. Also, some operating systems (for example VMS with VAX C) 1868651Skris# need to keep track of the global variables as well as the functions. 1968651Skris# 2068651Skris# So, a remake of this script is done so as to include information on the 2168651Skris# kind of symbol it is (function or variable) and what algorithms they're 2268651Skris# part of. This will allow easy translating to .def files or the corresponding 2368651Skris# file in other operating systems (a .opt file for VMS, possibly with a .mar 2468651Skris# file). 2568651Skris# 2668651Skris# The format now becomes: 2768651Skris# 2868651Skris# routine-name nnnn info 2968651Skris# 3068651Skris# and the "info" part is actually a colon-separated string of fields with 3168651Skris# the following meaning: 3268651Skris# 3368651Skris# existence:platform:kind:algorithms 3468651Skris# 3568651Skris# - "existence" can be "EXIST" or "NOEXIST" depending on if the symbol is 3668651Skris# found somewhere in the source, 3768651Skris# - "platforms" is empty if it exists on all platforms, otherwise it contains 3868651Skris# comma-separated list of the platform, just as they are if the symbol exists 3968651Skris# for those platforms, or prepended with a "!" if not. This helps resolve 40109998Smarkm# symbol name variants for platforms where the names are too long for the 4168651Skris# compiler or linker, or if the systems is case insensitive and there is a 42109998Smarkm# clash, or the symbol is implemented differently (see 43109998Smarkm# EXPORT_VAR_AS_FUNCTION). This script assumes renaming of symbols is found 44109998Smarkm# in the file crypto/symhacks.h. 45109998Smarkm# The semantics for the platforms is that every item is checked against the 46109998Smarkm# environment. For the negative items ("!FOO"), if any of them is false 47109998Smarkm# (i.e. "FOO" is true) in the environment, the corresponding symbol can't be 48109998Smarkm# used. For the positive itms, if all of them are false in the environment, 49109998Smarkm# the corresponding symbol can't be used. Any combination of positive and 50109998Smarkm# negative items are possible, and of course leave room for some redundancy. 5168651Skris# - "kind" is "FUNCTION" or "VARIABLE". The meaning of that is obvious. 5268651Skris# - "algorithms" is a comma-separated list of algorithm names. This helps 5368651Skris# exclude symbols that are part of an algorithm that some user wants to 5468651Skris# exclude. 5568651Skris# 5655714Skris 57109998Smarkmmy $debug=0; 58109998Smarkm 5968651Skrismy $crypto_num= "util/libeay.num"; 6068651Skrismy $ssl_num= "util/ssleay.num"; 61109998Smarkmmy $libname; 6255714Skris 6355714Skrismy $do_update = 0; 64109998Smarkmmy $do_rewrite = 1; 6555714Skrismy $do_crypto = 0; 6655714Skrismy $do_ssl = 0; 6759191Skrismy $do_ctest = 0; 6868651Skrismy $do_ctestall = 0; 69109998Smarkmmy $do_checkexist = 0; 7055714Skris 71109998Smarkmmy $VMSVAX=0; 72109998Smarkmmy $VMSAlpha=0; 7368651Skrismy $VMS=0; 7468651Skrismy $W32=0; 7568651Skrismy $W16=0; 7659191Skrismy $NT=0; 77109998Smarkmmy $OS2=0; 7855714Skris# Set this to make typesafe STACK definitions appear in DEF 7968651Skrismy $safe_stack_def = 0; 8055714Skris 81109998Smarkmmy @known_platforms = ( "__FreeBSD__", "PERL5", "NeXT", 82194206Ssimon "EXPORT_VAR_AS_FUNCTION", "ZLIB", "OPENSSL_FIPS"); 83109998Smarkmmy @known_ossl_platforms = ( "VMS", "WIN16", "WIN32", "WINNT", "OS2" ); 8468651Skrismy @known_algorithms = ( "RC2", "RC4", "RC5", "IDEA", "DES", "BF", 85109998Smarkm "CAST", "MD2", "MD4", "MD5", "SHA", "SHA0", "SHA1", 86160814Ssimon "SHA256", "SHA512", "RIPEMD", 87194206Ssimon "MDC2", "RSA", "DSA", "DH", "EC", "ECDH", "ECDSA", "HMAC", "AES", "CAMELLIA", "SEED", 88109998Smarkm # Envelope "algorithms" 89109998Smarkm "EVP", "X509", "ASN1_TYPEDEFS", 90109998Smarkm # Helper "algorithms" 91109998Smarkm "BIO", "COMP", "BUFFER", "LHASH", "STACK", "ERR", 92109998Smarkm "LOCKING", 93109998Smarkm # External "algorithms" 94160814Ssimon "FP_API", "STDIO", "SOCK", "KRB5", "DGRAM", 95160814Ssimon # Engines 96160814Ssimon "STATIC_ENGINE", "ENGINE", "HW", "GMP", 97167612Ssimon # RFC3779 support 98167612Ssimon "RFC3779", 99194206Ssimon # TLS extension support 100194206Ssimon "TLSEXT", 101194206Ssimon # CMS 102194206Ssimon "CMS", 103194206Ssimon # CryptoAPI Engine 104194206Ssimon "CAPIENG", 105194206Ssimon # JPAKE 106194206Ssimon "JPAKE", 107160814Ssimon # Deprecated functions 108160814Ssimon "DEPRECATED" ); 10968651Skris 11059191Skrismy $options=""; 111142425Snectaropen(IN,"<Makefile") || die "unable to open Makefile!\n"; 11255714Skriswhile(<IN>) { 11355714Skris $options=$1 if (/^OPTIONS=(.*)$/); 11455714Skris} 11555714Skrisclose(IN); 11655714Skris 11759191Skris# The following ciphers may be excluded (by Configure). This means functions 11859191Skris# defined with ifndef(NO_XXX) are not included in the .def file, and everything 11959191Skris# in directory xxx is ignored. 12059191Skrismy $no_rc2; my $no_rc4; my $no_rc5; my $no_idea; my $no_des; my $no_bf; 12168651Skrismy $no_cast; 12268651Skrismy $no_md2; my $no_md4; my $no_md5; my $no_sha; my $no_ripemd; my $no_mdc2; 123109998Smarkmmy $no_rsa; my $no_dsa; my $no_dh; my $no_hmac=0; my $no_aes; my $no_krb5; 124162911Ssimonmy $no_ec; my $no_ecdsa; my $no_ecdh; my $no_engine; my $no_hw; my $no_camellia; 125194206Ssimonmy $no_seed; 126160814Ssimonmy $no_fp_api; my $no_static_engine; my $no_gmp; my $no_deprecated; 127194206Ssimonmy $no_rfc3779; my $no_tlsext; my $no_cms; my $no_capieng; my $no_jpake; 128194206Ssimonmy $fips; 12959191Skris 130160814Ssimon 13155714Skrisforeach (@ARGV, split(/ /, $options)) 13255714Skris { 133109998Smarkm $debug=1 if $_ eq "debug"; 13455714Skris $W32=1 if $_ eq "32"; 13568651Skris $W16=1 if $_ eq "16"; 13655714Skris if($_ eq "NT") { 13755714Skris $W32 = 1; 13855714Skris $NT = 1; 13955714Skris } 140109998Smarkm if ($_ eq "VMS-VAX") { 141109998Smarkm $VMS=1; 142109998Smarkm $VMSVAX=1; 143109998Smarkm } 144109998Smarkm if ($_ eq "VMS-Alpha") { 145109998Smarkm $VMS=1; 146109998Smarkm $VMSAlpha=1; 147109998Smarkm } 14868651Skris $VMS=1 if $_ eq "VMS"; 149109998Smarkm $OS2=1 if $_ eq "OS2"; 150194206Ssimon $fips=1 if /^fips/; 15168651Skris 152194206Ssimon if ($_ eq "zlib" || $_ eq "zlib-dynamic" 153194206Ssimon || $_ eq "enable-zlib-dynamic") { 154194206Ssimon $zlib = 1; 155194206Ssimon } 156194206Ssimon 15755714Skris $do_ssl=1 if $_ eq "ssleay"; 158109998Smarkm if ($_ eq "ssl") { 159109998Smarkm $do_ssl=1; 160109998Smarkm $libname=$_ 161109998Smarkm } 16255714Skris $do_crypto=1 if $_ eq "libeay"; 163109998Smarkm if ($_ eq "crypto") { 164109998Smarkm $do_crypto=1; 165109998Smarkm $libname=$_; 166109998Smarkm } 167160814Ssimon $no_static_engine=1 if $_ eq "no-static-engine"; 168160814Ssimon $no_static_engine=0 if $_ eq "enable-static-engine"; 16955714Skris $do_update=1 if $_ eq "update"; 17068651Skris $do_rewrite=1 if $_ eq "rewrite"; 17159191Skris $do_ctest=1 if $_ eq "ctest"; 17268651Skris $do_ctestall=1 if $_ eq "ctestall"; 173109998Smarkm $do_checkexist=1 if $_ eq "exist"; 17468651Skris #$safe_stack_def=1 if $_ eq "-DDEBUG_SAFESTACK"; 17555714Skris 17655714Skris if (/^no-rc2$/) { $no_rc2=1; } 17755714Skris elsif (/^no-rc4$/) { $no_rc4=1; } 17855714Skris elsif (/^no-rc5$/) { $no_rc5=1; } 17955714Skris elsif (/^no-idea$/) { $no_idea=1; } 18072613Skris elsif (/^no-des$/) { $no_des=1; $no_mdc2=1; } 18155714Skris elsif (/^no-bf$/) { $no_bf=1; } 18255714Skris elsif (/^no-cast$/) { $no_cast=1; } 18355714Skris elsif (/^no-md2$/) { $no_md2=1; } 18468651Skris elsif (/^no-md4$/) { $no_md4=1; } 18555714Skris elsif (/^no-md5$/) { $no_md5=1; } 18655714Skris elsif (/^no-sha$/) { $no_sha=1; } 18755714Skris elsif (/^no-ripemd$/) { $no_ripemd=1; } 18855714Skris elsif (/^no-mdc2$/) { $no_mdc2=1; } 18955714Skris elsif (/^no-rsa$/) { $no_rsa=1; } 19055714Skris elsif (/^no-dsa$/) { $no_dsa=1; } 19155714Skris elsif (/^no-dh$/) { $no_dh=1; } 192109998Smarkm elsif (/^no-ec$/) { $no_ec=1; } 193160814Ssimon elsif (/^no-ecdsa$/) { $no_ecdsa=1; } 194160814Ssimon elsif (/^no-ecdh$/) { $no_ecdh=1; } 19555714Skris elsif (/^no-hmac$/) { $no_hmac=1; } 196109998Smarkm elsif (/^no-aes$/) { $no_aes=1; } 197162911Ssimon elsif (/^no-camellia$/) { $no_camellia=1; } 198194206Ssimon elsif (/^no-seed$/) { $no_seed=1; } 199109998Smarkm elsif (/^no-evp$/) { $no_evp=1; } 200109998Smarkm elsif (/^no-lhash$/) { $no_lhash=1; } 201109998Smarkm elsif (/^no-stack$/) { $no_stack=1; } 202109998Smarkm elsif (/^no-err$/) { $no_err=1; } 203109998Smarkm elsif (/^no-buffer$/) { $no_buffer=1; } 204109998Smarkm elsif (/^no-bio$/) { $no_bio=1; } 205109998Smarkm #elsif (/^no-locking$/) { $no_locking=1; } 206109998Smarkm elsif (/^no-comp$/) { $no_comp=1; } 207109998Smarkm elsif (/^no-dso$/) { $no_dso=1; } 208109998Smarkm elsif (/^no-krb5$/) { $no_krb5=1; } 209111147Snectar elsif (/^no-engine$/) { $no_engine=1; } 210111147Snectar elsif (/^no-hw$/) { $no_hw=1; } 211160814Ssimon elsif (/^no-gmp$/) { $no_gmp=1; } 212167612Ssimon elsif (/^no-rfc3779$/) { $no_rfc3779=1; } 213194206Ssimon elsif (/^no-tlsext$/) { $no_tlsext=1; } 214194206Ssimon elsif (/^no-cms$/) { $no_cms=1; } 215194206Ssimon elsif (/^no-capieng$/) { $no_capieng=1; } 216194206Ssimon elsif (/^no-jpake$/) { $no_jpake=1; } 21755714Skris } 21855714Skris 21959191Skris 220109998Smarkmif (!$libname) { 221109998Smarkm if ($do_ssl) { 222109998Smarkm $libname="SSLEAY"; 223109998Smarkm } 224109998Smarkm if ($do_crypto) { 225109998Smarkm $libname="LIBEAY"; 226109998Smarkm } 227109998Smarkm} 228109998Smarkm 22968651Skris# If no platform is given, assume WIN32 230109998Smarkmif ($W32 + $W16 + $VMS + $OS2 == 0) { 23168651Skris $W32 = 1; 23268651Skris} 23368651Skris 23468651Skris# Add extra knowledge 23568651Skrisif ($W16) { 23668651Skris $no_fp_api=1; 23768651Skris} 23868651Skris 23955714Skrisif (!$do_ssl && !$do_crypto) 24055714Skris { 241109998Smarkm print STDERR "usage: $0 ( ssl | crypto ) [ 16 | 32 | NT | OS2 ]\n"; 24255714Skris exit(1); 24355714Skris } 24455714Skris 24555714Skris%ssl_list=&load_numbers($ssl_num); 24655714Skris$max_ssl = $max_num; 24755714Skris%crypto_list=&load_numbers($crypto_num); 24855714Skris$max_crypto = $max_num; 24955714Skris 25059191Skrismy $ssl="ssl/ssl.h"; 251109998Smarkm$ssl.=" ssl/kssl.h"; 252194206Ssimon$ssl.=" ssl/tls1.h"; 25355714Skris 25459191Skrismy $crypto ="crypto/crypto.h"; 255160814Ssimon$crypto.=" crypto/o_dir.h"; 256109998Smarkm$crypto.=" crypto/des/des.h crypto/des/des_old.h" ; # unless $no_des; 257109998Smarkm$crypto.=" crypto/idea/idea.h" ; # unless $no_idea; 258109998Smarkm$crypto.=" crypto/rc4/rc4.h" ; # unless $no_rc4; 259109998Smarkm$crypto.=" crypto/rc5/rc5.h" ; # unless $no_rc5; 260109998Smarkm$crypto.=" crypto/rc2/rc2.h" ; # unless $no_rc2; 261109998Smarkm$crypto.=" crypto/bf/blowfish.h" ; # unless $no_bf; 262109998Smarkm$crypto.=" crypto/cast/cast.h" ; # unless $no_cast; 263109998Smarkm$crypto.=" crypto/md2/md2.h" ; # unless $no_md2; 264109998Smarkm$crypto.=" crypto/md4/md4.h" ; # unless $no_md4; 265109998Smarkm$crypto.=" crypto/md5/md5.h" ; # unless $no_md5; 266109998Smarkm$crypto.=" crypto/mdc2/mdc2.h" ; # unless $no_mdc2; 267109998Smarkm$crypto.=" crypto/sha/sha.h" ; # unless $no_sha; 268109998Smarkm$crypto.=" crypto/ripemd/ripemd.h" ; # unless $no_ripemd; 269109998Smarkm$crypto.=" crypto/aes/aes.h" ; # unless $no_aes; 270162911Ssimon$crypto.=" crypto/camellia/camellia.h" ; # unless $no_camellia; 271194206Ssimon$crypto.=" crypto/seed/seed.h"; # unless $no_seed; 27255714Skris 27355714Skris$crypto.=" crypto/bn/bn.h"; 274109998Smarkm$crypto.=" crypto/rsa/rsa.h" ; # unless $no_rsa; 275109998Smarkm$crypto.=" crypto/dsa/dsa.h" ; # unless $no_dsa; 276109998Smarkm$crypto.=" crypto/dh/dh.h" ; # unless $no_dh; 277109998Smarkm$crypto.=" crypto/ec/ec.h" ; # unless $no_ec; 278160814Ssimon$crypto.=" crypto/ecdsa/ecdsa.h" ; # unless $no_ecdsa; 279160814Ssimon$crypto.=" crypto/ecdh/ecdh.h" ; # unless $no_ecdh; 280109998Smarkm$crypto.=" crypto/hmac/hmac.h" ; # unless $no_hmac; 28155714Skris 282111147Snectar$crypto.=" crypto/engine/engine.h"; # unless $no_engine; 283109998Smarkm$crypto.=" crypto/stack/stack.h" ; # unless $no_stack; 284109998Smarkm$crypto.=" crypto/buffer/buffer.h" ; # unless $no_buffer; 285109998Smarkm$crypto.=" crypto/bio/bio.h" ; # unless $no_bio; 286109998Smarkm$crypto.=" crypto/dso/dso.h" ; # unless $no_dso; 287109998Smarkm$crypto.=" crypto/lhash/lhash.h" ; # unless $no_lhash; 28855714Skris$crypto.=" crypto/conf/conf.h"; 28955714Skris$crypto.=" crypto/txt_db/txt_db.h"; 29055714Skris 291109998Smarkm$crypto.=" crypto/evp/evp.h" ; # unless $no_evp; 29255714Skris$crypto.=" crypto/objects/objects.h"; 29355714Skris$crypto.=" crypto/pem/pem.h"; 29455714Skris#$crypto.=" crypto/meth/meth.h"; 29555714Skris$crypto.=" crypto/asn1/asn1.h"; 296109998Smarkm$crypto.=" crypto/asn1/asn1t.h"; 29755714Skris$crypto.=" crypto/asn1/asn1_mac.h"; 298109998Smarkm$crypto.=" crypto/err/err.h" ; # unless $no_err; 29955714Skris$crypto.=" crypto/pkcs7/pkcs7.h"; 30055714Skris$crypto.=" crypto/pkcs12/pkcs12.h"; 30155714Skris$crypto.=" crypto/x509/x509.h"; 30255714Skris$crypto.=" crypto/x509/x509_vfy.h"; 30355714Skris$crypto.=" crypto/x509v3/x509v3.h"; 30455714Skris$crypto.=" crypto/rand/rand.h"; 305109998Smarkm$crypto.=" crypto/comp/comp.h" ; # unless $no_comp; 306109998Smarkm$crypto.=" crypto/ocsp/ocsp.h"; 307109998Smarkm$crypto.=" crypto/ui/ui.h crypto/ui/ui_compat.h"; 308109998Smarkm$crypto.=" crypto/krb5/krb5_asn.h"; 30955714Skris$crypto.=" crypto/tmdiff.h"; 310160814Ssimon$crypto.=" crypto/store/store.h"; 311160814Ssimon$crypto.=" crypto/pqueue/pqueue.h"; 312194206Ssimon$crypto.=" crypto/cms/cms.h"; 313194206Ssimon$crypto.=" crypto/jpake/jpake.h"; 314194206Ssimon$crypto.=" fips/fips.h fips/rand/fips_rand.h"; 31555714Skris 31668651Skrismy $symhacks="crypto/symhacks.h"; 31755714Skris 31868651Skrismy @ssl_symbols = &do_defs("SSLEAY", $ssl, $symhacks); 31968651Skrismy @crypto_symbols = &do_defs("LIBEAY", $crypto, $symhacks); 32055714Skris 32155714Skrisif ($do_update) { 32255714Skris 32355714Skrisif ($do_ssl == 1) { 32468651Skris 32568651Skris &maybe_add_info("SSLEAY",*ssl_list,@ssl_symbols); 32668651Skris if ($do_rewrite == 1) { 32768651Skris open(OUT, ">$ssl_num"); 32868651Skris &rewrite_numbers(*OUT,"SSLEAY",*ssl_list,@ssl_symbols); 32968651Skris } else { 33068651Skris open(OUT, ">>$ssl_num"); 33168651Skris } 33268651Skris &update_numbers(*OUT,"SSLEAY",*ssl_list,$max_ssl,@ssl_symbols); 33355714Skris close OUT; 33455714Skris} 33555714Skris 33655714Skrisif($do_crypto == 1) { 33768651Skris 33868651Skris &maybe_add_info("LIBEAY",*crypto_list,@crypto_symbols); 33968651Skris if ($do_rewrite == 1) { 34068651Skris open(OUT, ">$crypto_num"); 34168651Skris &rewrite_numbers(*OUT,"LIBEAY",*crypto_list,@crypto_symbols); 34268651Skris } else { 34368651Skris open(OUT, ">>$crypto_num"); 34468651Skris } 34568651Skris &update_numbers(*OUT,"LIBEAY",*crypto_list,$max_crypto,@crypto_symbols); 34655714Skris close OUT; 34759191Skris} 34855714Skris 349109998Smarkm} elsif ($do_checkexist) { 350109998Smarkm &check_existing(*ssl_list, @ssl_symbols) 351109998Smarkm if $do_ssl == 1; 352109998Smarkm &check_existing(*crypto_list, @crypto_symbols) 353109998Smarkm if $do_crypto == 1; 35468651Skris} elsif ($do_ctest || $do_ctestall) { 35559191Skris 35659191Skris print <<"EOF"; 35759191Skris 35859191Skris/* Test file to check all DEF file symbols are present by trying 35959191Skris * to link to all of them. This is *not* intended to be run! 36059191Skris */ 36159191Skris 36259191Skrisint main() 36359191Skris{ 36459191SkrisEOF 36568651Skris &print_test_file(*STDOUT,"SSLEAY",*ssl_list,$do_ctestall,@ssl_symbols) 36659191Skris if $do_ssl == 1; 36759191Skris 36868651Skris &print_test_file(*STDOUT,"LIBEAY",*crypto_list,$do_ctestall,@crypto_symbols) 36959191Skris if $do_crypto == 1; 37059191Skris 37159191Skris print "}\n"; 37259191Skris 37355714Skris} else { 37455714Skris 375109998Smarkm &print_def_file(*STDOUT,$libname,*ssl_list,@ssl_symbols) 37655714Skris if $do_ssl == 1; 37755714Skris 378109998Smarkm &print_def_file(*STDOUT,$libname,*crypto_list,@crypto_symbols) 37955714Skris if $do_crypto == 1; 38055714Skris 38155714Skris} 38255714Skris 38355714Skris 38455714Skrissub do_defs 38555714Skris{ 38668651Skris my($name,$files,$symhacksfile)=@_; 38759191Skris my $file; 38855714Skris my @ret; 38968651Skris my %syms; 39068651Skris my %platform; # For anything undefined, we assume "" 39168651Skris my %kind; # For anything undefined, we assume "FUNCTION" 39268651Skris my %algorithm; # For anything undefined, we assume "" 393109998Smarkm my %variant; 394109998Smarkm my %variant_cnt; # To be able to allocate "name{n}" if "name" 395109998Smarkm # is the same name as the original. 39659191Skris my $cpp; 397109998Smarkm my %unknown_algorithms = (); 39855714Skris 39968651Skris foreach $file (split(/\s+/,$symhacksfile." ".$files)) 40055714Skris { 401109998Smarkm print STDERR "DEBUG: starting on $file:\n" if $debug; 40255714Skris open(IN,"<$file") || die "unable to open $file:$!\n"; 40359191Skris my $line = "", my $def= ""; 40455714Skris my %tag = ( 40568651Skris (map { $_ => 0 } @known_platforms), 406109998Smarkm (map { "OPENSSL_SYS_".$_ => 0 } @known_ossl_platforms), 407109998Smarkm (map { "OPENSSL_NO_".$_ => 0 } @known_algorithms), 40855714Skris NOPROTO => 0, 40955714Skris PERL5 => 0, 41055714Skris _WINDLL => 0, 41155714Skris CONST_STRICT => 0, 41255714Skris TRUE => 1, 41355714Skris ); 41468651Skris my $symhacking = $file eq $symhacksfile; 415109998Smarkm my @current_platforms = (); 416109998Smarkm my @current_algorithms = (); 417109998Smarkm 418109998Smarkm # params: symbol, alias, platforms, kind 419109998Smarkm # The reason to put this subroutine in a variable is that 420109998Smarkm # it will otherwise create it's own, unshared, version of 421109998Smarkm # %tag and %variant... 422109998Smarkm my $make_variant = sub 423109998Smarkm { 424109998Smarkm my ($s, $a, $p, $k) = @_; 425109998Smarkm my ($a1, $a2); 426109998Smarkm 427109998Smarkm print STDERR "DEBUG: make_variant: Entered with ",$s,", ",$a,", ",(defined($p)?$p:""),", ",(defined($k)?$k:""),"\n" if $debug; 428109998Smarkm if (defined($p)) 429109998Smarkm { 430109998Smarkm $a1 = join(",",$p, 431109998Smarkm grep(!/^$/, 432109998Smarkm map { $tag{$_} == 1 ? $_ : "" } 433109998Smarkm @known_platforms)); 434109998Smarkm } 435109998Smarkm else 436109998Smarkm { 437109998Smarkm $a1 = join(",", 438109998Smarkm grep(!/^$/, 439109998Smarkm map { $tag{$_} == 1 ? $_ : "" } 440109998Smarkm @known_platforms)); 441109998Smarkm } 442109998Smarkm $a2 = join(",", 443109998Smarkm grep(!/^$/, 444109998Smarkm map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : "" } 445109998Smarkm @known_ossl_platforms)); 446109998Smarkm print STDERR "DEBUG: make_variant: a1 = $a1; a2 = $a2\n" if $debug; 447109998Smarkm if ($a1 eq "") { $a1 = $a2; } 448109998Smarkm elsif ($a1 ne "" && $a2 ne "") { $a1 .= ",".$a2; } 449109998Smarkm if ($a eq $s) 450109998Smarkm { 451109998Smarkm if (!defined($variant_cnt{$s})) 452109998Smarkm { 453109998Smarkm $variant_cnt{$s} = 0; 454109998Smarkm } 455109998Smarkm $variant_cnt{$s}++; 456109998Smarkm $a .= "{$variant_cnt{$s}}"; 457109998Smarkm } 458109998Smarkm my $toadd = $a.":".$a1.(defined($k)?":".$k:""); 459109998Smarkm my $togrep = $s.'(\{[0-9]+\})?:'.$a1.(defined($k)?":".$k:""); 460109998Smarkm if (!grep(/^$togrep$/, 461109998Smarkm split(/;/, defined($variant{$s})?$variant{$s}:""))) { 462109998Smarkm if (defined($variant{$s})) { $variant{$s} .= ";"; } 463109998Smarkm $variant{$s} .= $toadd; 464109998Smarkm } 465109998Smarkm print STDERR "DEBUG: make_variant: Exit with variant of ",$s," = ",$variant{$s},"\n" if $debug; 466109998Smarkm }; 467109998Smarkm 468109998Smarkm print STDERR "DEBUG: parsing ----------\n" if $debug; 46955714Skris while(<IN>) { 470160814Ssimon if (/\/\* Error codes for the \w+ functions\. \*\//) 471160814Ssimon { 472160814Ssimon undef @tag; 473160814Ssimon last; 474160814Ssimon } 47555714Skris if ($line ne '') { 47655714Skris $_ = $line . $_; 47755714Skris $line = ''; 47855714Skris } 47955714Skris 48055714Skris if (/\\$/) { 481109998Smarkm chomp; # remove eol 482109998Smarkm chop; # remove ending backslash 48355714Skris $line = $_; 48455714Skris next; 48555714Skris } 48655714Skris 487160814Ssimon if(/\/\*/) { 488160814Ssimon if (not /\*\//) { # multiline comment... 489160814Ssimon $line = $_; # ... just accumulate 490160814Ssimon next; 491160814Ssimon } else { 492160814Ssimon s/\/\*.*?\*\///gs;# wipe it 493160814Ssimon } 494160814Ssimon } 495160814Ssimon 49655714Skris if ($cpp) { 497160814Ssimon $cpp++ if /^#\s*if/; 498160814Ssimon $cpp-- if /^#\s*endif/; 49955714Skris next; 50055714Skris } 501160814Ssimon $cpp = 1 if /^#.*ifdef.*cplusplus/; 50255714Skris 50355714Skris s/{[^{}]*}//gs; # ignore {} blocks 504120631Snectar print STDERR "DEBUG: \$def=\"$def\"\n" if $debug && $def ne ""; 505109998Smarkm print STDERR "DEBUG: \$_=\"$_\"\n" if $debug; 506109998Smarkm if (/^\#\s*ifndef\s+(.*)/) { 507109998Smarkm push(@tag,"-"); 50855714Skris push(@tag,$1); 50955714Skris $tag{$1}=-1; 510109998Smarkm print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug; 511109998Smarkm } elsif (/^\#\s*if\s+!defined\(([^\)]+)\)/) { 512109998Smarkm push(@tag,"-"); 513109998Smarkm if (/^\#\s*if\s+(!defined\(([^\)]+)\)(\s+\&\&\s+!defined\(([^\)]+)\))*)$/) { 514109998Smarkm my $tmp_1 = $1; 515109998Smarkm my $tmp_; 516109998Smarkm foreach $tmp_ (split '\&\&',$tmp_1) { 517109998Smarkm $tmp_ =~ /!defined\(([^\)]+)\)/; 518109998Smarkm print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug; 519109998Smarkm push(@tag,$1); 520109998Smarkm $tag{$1}=-1; 521109998Smarkm } 522109998Smarkm } else { 523109998Smarkm print STDERR "Warning: $file: complicated expression: $_" if $debug; # because it is O... 524109998Smarkm print STDERR "DEBUG: $file: found tag $1 = -1\n" if $debug; 525109998Smarkm push(@tag,$1); 526109998Smarkm $tag{$1}=-1; 527109998Smarkm } 528160814Ssimon } elsif (/^\#\s*ifdef\s+(\S*)/) { 529109998Smarkm push(@tag,"-"); 53055714Skris push(@tag,$1); 53155714Skris $tag{$1}=1; 532109998Smarkm print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug; 533109998Smarkm } elsif (/^\#\s*if\s+defined\(([^\)]+)\)/) { 534109998Smarkm push(@tag,"-"); 535109998Smarkm if (/^\#\s*if\s+(defined\(([^\)]+)\)(\s+\|\|\s+defined\(([^\)]+)\))*)$/) { 536109998Smarkm my $tmp_1 = $1; 537109998Smarkm my $tmp_; 538109998Smarkm foreach $tmp_ (split '\|\|',$tmp_1) { 539109998Smarkm $tmp_ =~ /defined\(([^\)]+)\)/; 540109998Smarkm print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug; 541109998Smarkm push(@tag,$1); 542109998Smarkm $tag{$1}=1; 543109998Smarkm } 544109998Smarkm } else { 545109998Smarkm print STDERR "Warning: $file: complicated expression: $_\n" if $debug; # because it is O... 546109998Smarkm print STDERR "DEBUG: $file: found tag $1 = 1\n" if $debug; 547109998Smarkm push(@tag,$1); 548109998Smarkm $tag{$1}=1; 549109998Smarkm } 55068651Skris } elsif (/^\#\s*error\s+(\w+) is disabled\./) { 551109998Smarkm my $tag_i = $#tag; 552109998Smarkm while($tag[$tag_i] ne "-") { 553109998Smarkm if ($tag[$tag_i] eq "OPENSSL_NO_".$1) { 554109998Smarkm $tag{$tag[$tag_i]}=2; 555109998Smarkm print STDERR "DEBUG: $file: chaged tag $1 = 2\n" if $debug; 556109998Smarkm } 557109998Smarkm $tag_i--; 55868651Skris } 55955714Skris } elsif (/^\#\s*endif/) { 560109998Smarkm my $tag_i = $#tag; 561160814Ssimon while($tag_i > 0 && $tag[$tag_i] ne "-") { 562109998Smarkm my $t=$tag[$tag_i]; 563109998Smarkm print STDERR "DEBUG: \$t=\"$t\"\n" if $debug; 564109998Smarkm if ($tag{$t}==2) { 565109998Smarkm $tag{$t}=-1; 566109998Smarkm } else { 567109998Smarkm $tag{$t}=0; 568109998Smarkm } 569109998Smarkm print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug; 570109998Smarkm pop(@tag); 571109998Smarkm if ($t =~ /^OPENSSL_NO_([A-Z0-9_]+)$/) { 572109998Smarkm $t=$1; 573109998Smarkm } else { 574109998Smarkm $t=""; 575109998Smarkm } 576109998Smarkm if ($t ne "" 577109998Smarkm && !grep(/^$t$/, @known_algorithms)) { 578109998Smarkm $unknown_algorithms{$t} = 1; 579109998Smarkm #print STDERR "DEBUG: Added as unknown algorithm: $t\n" if $debug; 580109998Smarkm } 581109998Smarkm $tag_i--; 58268651Skris } 58355714Skris pop(@tag); 58455714Skris } elsif (/^\#\s*else/) { 585109998Smarkm my $tag_i = $#tag; 586109998Smarkm while($tag[$tag_i] ne "-") { 587109998Smarkm my $t=$tag[$tag_i]; 588109998Smarkm $tag{$t}= -$tag{$t}; 589109998Smarkm print STDERR "DEBUG: $file: changed tag ",$t," = ",$tag{$t},"\n" if $debug; 590109998Smarkm $tag_i--; 591109998Smarkm } 59255714Skris } elsif (/^\#\s*if\s+1/) { 593109998Smarkm push(@tag,"-"); 59455714Skris # Dummy tag 59555714Skris push(@tag,"TRUE"); 59655714Skris $tag{"TRUE"}=1; 597109998Smarkm print STDERR "DEBUG: $file: found 1\n" if $debug; 59859191Skris } elsif (/^\#\s*if\s+0/) { 599109998Smarkm push(@tag,"-"); 60059191Skris # Dummy tag 60159191Skris push(@tag,"TRUE"); 60259191Skris $tag{"TRUE"}=-1; 603109998Smarkm print STDERR "DEBUG: $file: found 0\n" if $debug; 60468651Skris } elsif (/^\#\s*define\s+(\w+)\s+(\w+)/ 605109998Smarkm && $symhacking && $tag{'TRUE'} != -1) { 606109998Smarkm # This is for aliasing. When we find an alias, 607109998Smarkm # we have to invert 608109998Smarkm &$make_variant($1,$2); 609109998Smarkm print STDERR "DEBUG: $file: defined $1 = $2\n" if $debug; 61068651Skris } 61168651Skris if (/^\#/) { 612109998Smarkm @current_platforms = 613109998Smarkm grep(!/^$/, 614109998Smarkm map { $tag{$_} == 1 ? $_ : 615109998Smarkm $tag{$_} == -1 ? "!".$_ : "" } 616109998Smarkm @known_platforms); 617109998Smarkm push @current_platforms 618109998Smarkm , grep(!/^$/, 619109998Smarkm map { $tag{"OPENSSL_SYS_".$_} == 1 ? $_ : 620109998Smarkm $tag{"OPENSSL_SYS_".$_} == -1 ? "!".$_ : "" } 621109998Smarkm @known_ossl_platforms); 622109998Smarkm @current_algorithms = 623109998Smarkm grep(!/^$/, 624109998Smarkm map { $tag{"OPENSSL_NO_".$_} == -1 ? $_ : "" } 625109998Smarkm @known_algorithms); 626109998Smarkm $def .= 627109998Smarkm "#INFO:" 628109998Smarkm .join(',',@current_platforms).":" 629109998Smarkm .join(',',@current_algorithms).";"; 63059191Skris next; 63168651Skris } 632109998Smarkm if ($tag{'TRUE'} != -1) { 633109998Smarkm if (/^\s*DECLARE_STACK_OF\s*\(\s*(\w*)\s*\)/) { 634109998Smarkm next; 635109998Smarkm } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) { 636109998Smarkm $def .= "int d2i_$3(void);"; 637109998Smarkm $def .= "int i2d_$3(void);"; 638109998Smarkm # Variant for platforms that do not 639109998Smarkm # have to access globale variables 640109998Smarkm # in shared libraries through functions 641109998Smarkm $def .= 642109998Smarkm "#INFO:" 643109998Smarkm .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":" 644109998Smarkm .join(',',@current_algorithms).";"; 645109998Smarkm $def .= "OPENSSL_EXTERN int $2_it;"; 646109998Smarkm $def .= 647109998Smarkm "#INFO:" 648109998Smarkm .join(',',@current_platforms).":" 649109998Smarkm .join(',',@current_algorithms).";"; 650109998Smarkm # Variant for platforms that have to 651109998Smarkm # access globale variables in shared 652109998Smarkm # libraries through functions 653109998Smarkm &$make_variant("$2_it","$2_it", 654109998Smarkm "EXPORT_VAR_AS_FUNCTION", 655109998Smarkm "FUNCTION"); 656109998Smarkm next; 657109998Smarkm } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_fname\s*\(\s*(\w*)\s*,\s*(\w*)\s*,\s*(\w*)\s*\)/) { 658109998Smarkm $def .= "int d2i_$3(void);"; 659109998Smarkm $def .= "int i2d_$3(void);"; 660109998Smarkm $def .= "int $3_free(void);"; 661109998Smarkm $def .= "int $3_new(void);"; 662109998Smarkm # Variant for platforms that do not 663109998Smarkm # have to access globale variables 664109998Smarkm # in shared libraries through functions 665109998Smarkm $def .= 666109998Smarkm "#INFO:" 667109998Smarkm .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":" 668109998Smarkm .join(',',@current_algorithms).";"; 669109998Smarkm $def .= "OPENSSL_EXTERN int $2_it;"; 670109998Smarkm $def .= 671109998Smarkm "#INFO:" 672109998Smarkm .join(',',@current_platforms).":" 673109998Smarkm .join(',',@current_algorithms).";"; 674109998Smarkm # Variant for platforms that have to 675109998Smarkm # access globale variables in shared 676109998Smarkm # libraries through functions 677109998Smarkm &$make_variant("$2_it","$2_it", 678109998Smarkm "EXPORT_VAR_AS_FUNCTION", 679109998Smarkm "FUNCTION"); 680109998Smarkm next; 681109998Smarkm } elsif (/^\s*DECLARE_ASN1_FUNCTIONS\s*\(\s*(\w*)\s*\)/ || 682109998Smarkm /^\s*DECLARE_ASN1_FUNCTIONS_const\s*\(\s*(\w*)\s*\)/) { 683109998Smarkm $def .= "int d2i_$1(void);"; 684109998Smarkm $def .= "int i2d_$1(void);"; 685109998Smarkm $def .= "int $1_free(void);"; 686109998Smarkm $def .= "int $1_new(void);"; 687109998Smarkm # Variant for platforms that do not 688109998Smarkm # have to access globale variables 689109998Smarkm # in shared libraries through functions 690109998Smarkm $def .= 691109998Smarkm "#INFO:" 692109998Smarkm .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":" 693109998Smarkm .join(',',@current_algorithms).";"; 694109998Smarkm $def .= "OPENSSL_EXTERN int $1_it;"; 695109998Smarkm $def .= 696109998Smarkm "#INFO:" 697109998Smarkm .join(',',@current_platforms).":" 698109998Smarkm .join(',',@current_algorithms).";"; 699109998Smarkm # Variant for platforms that have to 700109998Smarkm # access globale variables in shared 701109998Smarkm # libraries through functions 702109998Smarkm &$make_variant("$1_it","$1_it", 703109998Smarkm "EXPORT_VAR_AS_FUNCTION", 704109998Smarkm "FUNCTION"); 705109998Smarkm next; 706109998Smarkm } elsif (/^\s*DECLARE_ASN1_ENCODE_FUNCTIONS_const\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) { 707109998Smarkm $def .= "int d2i_$2(void);"; 708109998Smarkm $def .= "int i2d_$2(void);"; 709109998Smarkm # Variant for platforms that do not 710109998Smarkm # have to access globale variables 711109998Smarkm # in shared libraries through functions 712109998Smarkm $def .= 713109998Smarkm "#INFO:" 714109998Smarkm .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":" 715109998Smarkm .join(',',@current_algorithms).";"; 716109998Smarkm $def .= "OPENSSL_EXTERN int $2_it;"; 717109998Smarkm $def .= 718109998Smarkm "#INFO:" 719109998Smarkm .join(',',@current_platforms).":" 720109998Smarkm .join(',',@current_algorithms).";"; 721109998Smarkm # Variant for platforms that have to 722109998Smarkm # access globale variables in shared 723109998Smarkm # libraries through functions 724109998Smarkm &$make_variant("$2_it","$2_it", 725109998Smarkm "EXPORT_VAR_AS_FUNCTION", 726109998Smarkm "FUNCTION"); 727109998Smarkm next; 728160814Ssimon } elsif (/^\s*DECLARE_ASN1_ALLOC_FUNCTIONS\s*\(\s*(\w*)\s*\)/) { 729160814Ssimon $def .= "int $1_free(void);"; 730160814Ssimon $def .= "int $1_new(void);"; 731160814Ssimon next; 732109998Smarkm } elsif (/^\s*DECLARE_ASN1_FUNCTIONS_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) { 733109998Smarkm $def .= "int d2i_$2(void);"; 734109998Smarkm $def .= "int i2d_$2(void);"; 735109998Smarkm $def .= "int $2_free(void);"; 736109998Smarkm $def .= "int $2_new(void);"; 737109998Smarkm # Variant for platforms that do not 738109998Smarkm # have to access globale variables 739109998Smarkm # in shared libraries through functions 740109998Smarkm $def .= 741109998Smarkm "#INFO:" 742109998Smarkm .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":" 743109998Smarkm .join(',',@current_algorithms).";"; 744109998Smarkm $def .= "OPENSSL_EXTERN int $2_it;"; 745109998Smarkm $def .= 746109998Smarkm "#INFO:" 747109998Smarkm .join(',',@current_platforms).":" 748109998Smarkm .join(',',@current_algorithms).";"; 749109998Smarkm # Variant for platforms that have to 750109998Smarkm # access globale variables in shared 751109998Smarkm # libraries through functions 752109998Smarkm &$make_variant("$2_it","$2_it", 753109998Smarkm "EXPORT_VAR_AS_FUNCTION", 754109998Smarkm "FUNCTION"); 755109998Smarkm next; 756109998Smarkm } elsif (/^\s*DECLARE_ASN1_ITEM\s*\(\s*(\w*)\s*\)/) { 757109998Smarkm # Variant for platforms that do not 758109998Smarkm # have to access globale variables 759109998Smarkm # in shared libraries through functions 760109998Smarkm $def .= 761109998Smarkm "#INFO:" 762109998Smarkm .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":" 763109998Smarkm .join(',',@current_algorithms).";"; 764109998Smarkm $def .= "OPENSSL_EXTERN int $1_it;"; 765109998Smarkm $def .= 766109998Smarkm "#INFO:" 767109998Smarkm .join(',',@current_platforms).":" 768109998Smarkm .join(',',@current_algorithms).";"; 769109998Smarkm # Variant for platforms that have to 770109998Smarkm # access globale variables in shared 771109998Smarkm # libraries through functions 772109998Smarkm &$make_variant("$1_it","$1_it", 773109998Smarkm "EXPORT_VAR_AS_FUNCTION", 774109998Smarkm "FUNCTION"); 775109998Smarkm next; 776160814Ssimon } elsif (/^\s*DECLARE_ASN1_NDEF_FUNCTION\s*\(\s*(\w*)\s*\)/) { 777160814Ssimon $def .= "int i2d_$1_NDEF(void);"; 778109998Smarkm } elsif (/^\s*DECLARE_ASN1_SET_OF\s*\(\s*(\w*)\s*\)/) { 779109998Smarkm next; 780160814Ssimon } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION\s*\(\s*(\w*)\s*\)/) { 781160814Ssimon $def .= "int $1_print_ctx(void);"; 782160814Ssimon next; 783160814Ssimon } elsif (/^\s*DECLARE_ASN1_PRINT_FUNCTION_name\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) { 784160814Ssimon $def .= "int $2_print_ctx(void);"; 785160814Ssimon next; 786109998Smarkm } elsif (/^\s*DECLARE_PKCS12_STACK_OF\s*\(\s*(\w*)\s*\)/) { 787109998Smarkm next; 788109998Smarkm } elsif (/^DECLARE_PEM_rw\s*\(\s*(\w*)\s*,/ || 789160814Ssimon /^DECLARE_PEM_rw_cb\s*\(\s*(\w*)\s*,/ || 790160814Ssimon /^DECLARE_PEM_rw_const\s*\(\s*(\w*)\s*,/ ) { 791109998Smarkm # Things not in Win16 792109998Smarkm $def .= 793109998Smarkm "#INFO:" 794109998Smarkm .join(',',"!WIN16",@current_platforms).":" 795109998Smarkm .join(',',@current_algorithms).";"; 796109998Smarkm $def .= "int PEM_read_$1(void);"; 797109998Smarkm $def .= "int PEM_write_$1(void);"; 798109998Smarkm $def .= 799109998Smarkm "#INFO:" 800109998Smarkm .join(',',@current_platforms).":" 801109998Smarkm .join(',',@current_algorithms).";"; 802109998Smarkm # Things that are everywhere 803109998Smarkm $def .= "int PEM_read_bio_$1(void);"; 804109998Smarkm $def .= "int PEM_write_bio_$1(void);"; 805109998Smarkm next; 806109998Smarkm } elsif (/^DECLARE_PEM_write\s*\(\s*(\w*)\s*,/ || 807109998Smarkm /^DECLARE_PEM_write_cb\s*\(\s*(\w*)\s*,/ ) { 808109998Smarkm # Things not in Win16 809109998Smarkm $def .= 810109998Smarkm "#INFO:" 811109998Smarkm .join(',',"!WIN16",@current_platforms).":" 812109998Smarkm .join(',',@current_algorithms).";"; 813109998Smarkm $def .= "int PEM_write_$1(void);"; 814109998Smarkm $def .= 815109998Smarkm "#INFO:" 816109998Smarkm .join(',',@current_platforms).":" 817109998Smarkm .join(',',@current_algorithms).";"; 818109998Smarkm # Things that are everywhere 819109998Smarkm $def .= "int PEM_write_bio_$1(void);"; 820109998Smarkm next; 821109998Smarkm } elsif (/^DECLARE_PEM_read\s*\(\s*(\w*)\s*,/ || 822109998Smarkm /^DECLARE_PEM_read_cb\s*\(\s*(\w*)\s*,/ ) { 823109998Smarkm # Things not in Win16 824109998Smarkm $def .= 825109998Smarkm "#INFO:" 826109998Smarkm .join(',',"!WIN16",@current_platforms).":" 827109998Smarkm .join(',',@current_algorithms).";"; 828109998Smarkm $def .= "int PEM_read_$1(void);"; 829109998Smarkm $def .= 830109998Smarkm "#INFO:" 831109998Smarkm .join(',',@current_platforms).":" 832109998Smarkm .join(',',@current_algorithms).";"; 833109998Smarkm # Things that are everywhere 834109998Smarkm $def .= "int PEM_read_bio_$1(void);"; 835109998Smarkm next; 836109998Smarkm } elsif (/^OPENSSL_DECLARE_GLOBAL\s*\(\s*(\w*)\s*,\s*(\w*)\s*\)/) { 837109998Smarkm # Variant for platforms that do not 838109998Smarkm # have to access globale variables 839109998Smarkm # in shared libraries through functions 840109998Smarkm $def .= 841109998Smarkm "#INFO:" 842109998Smarkm .join(',',"!EXPORT_VAR_AS_FUNCTION",@current_platforms).":" 843109998Smarkm .join(',',@current_algorithms).";"; 844109998Smarkm $def .= "OPENSSL_EXTERN int _shadow_$2;"; 845109998Smarkm $def .= 846109998Smarkm "#INFO:" 847109998Smarkm .join(',',@current_platforms).":" 848109998Smarkm .join(',',@current_algorithms).";"; 849109998Smarkm # Variant for platforms that have to 850109998Smarkm # access globale variables in shared 851109998Smarkm # libraries through functions 852109998Smarkm &$make_variant("_shadow_$2","_shadow_$2", 853109998Smarkm "EXPORT_VAR_AS_FUNCTION", 854109998Smarkm "FUNCTION"); 855109998Smarkm } elsif ($tag{'CONST_STRICT'} != 1) { 85668651Skris if (/\{|\/\*|\([^\)]*$/) { 85755714Skris $line = $_; 85855714Skris } else { 85955714Skris $def .= $_; 86055714Skris } 86155714Skris } 86255714Skris } 863109998Smarkm } 86455714Skris close(IN); 86555714Skris 866160814Ssimon my $algs; 86768651Skris my $plays; 86868651Skris 869109998Smarkm print STDERR "DEBUG: postprocessing ----------\n" if $debug; 87055714Skris foreach (split /;/, $def) { 87168651Skris my $s; my $k = "FUNCTION"; my $p; my $a; 87255714Skris s/^[\n\s]*//g; 87355714Skris s/[\n\s]*$//g; 87468651Skris next if(/\#undef/); 87555714Skris next if(/typedef\W/); 87668651Skris next if(/\#define/); 87768651Skris 878160814Ssimon # Reduce argument lists to empty () 879160814Ssimon # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {} 880160814Ssimon while(/\(.*\)/s) { 881160814Ssimon s/\([^\(\)]+\)/\{\}/gs; 882160814Ssimon s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f 883160814Ssimon } 884160814Ssimon # pretend as we didn't use curly braces: {} -> () 885160814Ssimon s/\{\}/\(\)/gs; 886160814Ssimon 887160814Ssimon s/STACK_OF\(\)/void/gs; 888160814Ssimon 889109998Smarkm print STDERR "DEBUG: \$_ = \"$_\"\n" if $debug; 89068651Skris if (/^\#INFO:([^:]*):(.*)$/) { 89168651Skris $plats = $1; 89268651Skris $algs = $2; 893109998Smarkm print STDERR "DEBUG: found info on platforms ($plats) and algorithms ($algs)\n" if $debug; 89468651Skris next; 895109998Smarkm } elsif (/^\s*OPENSSL_EXTERN\s.*?(\w+(\{[0-9]+\})?)(\[[0-9]*\])*\s*$/) { 89668651Skris $s = $1; 89768651Skris $k = "VARIABLE"; 898109998Smarkm print STDERR "DEBUG: found external variable $s\n" if $debug; 899160814Ssimon } elsif (/TYPEDEF_\w+_OF/s) { 90055714Skris next; 901160814Ssimon } elsif (/(\w+)\s*\(\).*/s) { # first token prior [first] () is 902160814Ssimon $s = $1; # a function name! 903109998Smarkm print STDERR "DEBUG: found function $s\n" if $debug; 90455714Skris } elsif (/\(/ and not (/=/)) { 90555714Skris print STDERR "File $file: cannot parse: $_;\n"; 90668651Skris next; 90768651Skris } else { 90868651Skris next; 90955714Skris } 91068651Skris 91168651Skris $syms{$s} = 1; 91268651Skris $kind{$s} = $k; 91368651Skris 91468651Skris $p = $plats; 91568651Skris $a = $algs; 91668651Skris $a .= ",BF" if($s =~ /EVP_bf/); 91768651Skris $a .= ",CAST" if($s =~ /EVP_cast/); 91868651Skris $a .= ",DES" if($s =~ /EVP_des/); 91968651Skris $a .= ",DSA" if($s =~ /EVP_dss/); 92068651Skris $a .= ",IDEA" if($s =~ /EVP_idea/); 92168651Skris $a .= ",MD2" if($s =~ /EVP_md2/); 92268651Skris $a .= ",MD4" if($s =~ /EVP_md4/); 92368651Skris $a .= ",MD5" if($s =~ /EVP_md5/); 92468651Skris $a .= ",RC2" if($s =~ /EVP_rc2/); 92568651Skris $a .= ",RC4" if($s =~ /EVP_rc4/); 92668651Skris $a .= ",RC5" if($s =~ /EVP_rc5/); 92768651Skris $a .= ",RIPEMD" if($s =~ /EVP_ripemd/); 92868651Skris $a .= ",SHA" if($s =~ /EVP_sha/); 92968651Skris $a .= ",RSA" if($s =~ /EVP_(Open|Seal)(Final|Init)/); 93068651Skris $a .= ",RSA" if($s =~ /PEM_Seal(Final|Init|Update)/); 93168651Skris $a .= ",RSA" if($s =~ /RSAPrivateKey/); 93268651Skris $a .= ",RSA" if($s =~ /SSLv23?_((client|server)_)?method/); 93368651Skris 934109998Smarkm $platform{$s} = 935109998Smarkm &reduce_platforms((defined($platform{$s})?$platform{$s}.',':"").$p); 93668651Skris $algorithm{$s} .= ','.$a; 93768651Skris 938109998Smarkm if (defined($variant{$s})) { 939109998Smarkm foreach $v (split /;/,$variant{$s}) { 940109998Smarkm (my $r, my $p, my $k) = split(/:/,$v); 941109998Smarkm my $ip = join ',',map({ /^!(.*)$/ ? $1 : "!".$_ } split /,/, $p); 942109998Smarkm $syms{$r} = 1; 943109998Smarkm if (!defined($k)) { $k = $kind{$s}; } 944109998Smarkm $kind{$r} = $k."(".$s.")"; 945109998Smarkm $algorithm{$r} = $algorithm{$s}; 946109998Smarkm $platform{$r} = &reduce_platforms($platform{$s}.",".$p.",".$p); 947109998Smarkm $platform{$s} = &reduce_platforms($platform{$s}.','.$ip.','.$ip); 948109998Smarkm print STDERR "DEBUG: \$variant{\"$s\"} = ",$v,"; \$r = $r; \$p = ",$platform{$r},"; \$a = ",$algorithm{$r},"; \$kind = ",$kind{$r},"\n" if $debug; 949109998Smarkm } 95068651Skris } 951109998Smarkm print STDERR "DEBUG: \$s = $s; \$p = ",$platform{$s},"; \$a = ",$algorithm{$s},"; \$kind = ",$kind{$s},"\n" if $debug; 95255714Skris } 95355714Skris } 95455714Skris 95568651Skris # Prune the returned symbols 95655714Skris 95768651Skris delete $syms{"bn_dump1"}; 95868651Skris $platform{"BIO_s_log"} .= ",!WIN32,!WIN16,!macintosh"; 95968651Skris 960109998Smarkm $platform{"PEM_read_NS_CERT_SEQ"} = "VMS"; 961109998Smarkm $platform{"PEM_write_NS_CERT_SEQ"} = "VMS"; 962109998Smarkm $platform{"PEM_read_P8_PRIV_KEY_INFO"} = "VMS"; 963109998Smarkm $platform{"PEM_write_P8_PRIV_KEY_INFO"} = "VMS"; 96468651Skris 96568651Skris # Info we know about 96668651Skris 96768651Skris push @ret, map { $_."\\".&info_string($_,"EXIST", 96868651Skris $platform{$_}, 96968651Skris $kind{$_}, 97068651Skris $algorithm{$_}) } keys %syms; 97168651Skris 972109998Smarkm if (keys %unknown_algorithms) { 973109998Smarkm print STDERR "WARNING: mkdef.pl doesn't know the following algorithms:\n"; 974109998Smarkm print STDERR "\t",join("\n\t",keys %unknown_algorithms),"\n"; 975109998Smarkm } 97668651Skris return(@ret); 97768651Skris} 97868651Skris 979109998Smarkm# Param: string of comma-separated platform-specs. 980109998Smarkmsub reduce_platforms 981109998Smarkm{ 982109998Smarkm my ($platforms) = @_; 98368651Skris my $pl = defined($platforms) ? $platforms : ""; 98468651Skris my %p = map { $_ => 0 } split /,/, $pl; 98568651Skris my $ret; 98668651Skris 987109998Smarkm print STDERR "DEBUG: Entered reduce_platforms with \"$platforms\"\n" 988109998Smarkm if $debug; 98968651Skris # We do this, because if there's code like the following, it really 99068651Skris # means the function exists in all cases and should therefore be 99168651Skris # everywhere. By increasing and decreasing, we may attain 0: 99268651Skris # 99368651Skris # ifndef WIN16 99468651Skris # int foo(); 99568651Skris # else 99668651Skris # int _fat foo(); 99768651Skris # endif 99868651Skris foreach $platform (split /,/, $pl) { 99968651Skris if ($platform =~ /^!(.*)$/) { 100068651Skris $p{$1}--; 100168651Skris } else { 100268651Skris $p{$platform}++; 100355714Skris } 100455714Skris } 100568651Skris foreach $platform (keys %p) { 100668651Skris if ($p{$platform} == 0) { delete $p{$platform}; } 100755714Skris } 100855714Skris 100968651Skris delete $p{""}; 1010109998Smarkm 1011109998Smarkm $ret = join(',',sort(map { $p{$_} < 0 ? "!".$_ : $_ } keys %p)); 1012109998Smarkm print STDERR "DEBUG: Exiting reduce_platforms with \"$ret\"\n" 1013109998Smarkm if $debug; 1014109998Smarkm return $ret; 1015109998Smarkm} 1016109998Smarkm 1017109998Smarkmsub info_string { 1018109998Smarkm (my $symbol, my $exist, my $platforms, my $kind, my $algorithms) = @_; 1019109998Smarkm 1020109998Smarkm my %a = defined($algorithms) ? 1021109998Smarkm map { $_ => 1 } split /,/, $algorithms : (); 1022109998Smarkm my $k = defined($kind) ? $kind : "FUNCTION"; 1023109998Smarkm my $ret; 1024109998Smarkm my $p = &reduce_platforms($platforms); 1025109998Smarkm 102668651Skris delete $a{""}; 102755714Skris 102868651Skris $ret = $exist; 1029109998Smarkm $ret .= ":".$p; 103068651Skris $ret .= ":".$k; 1031109998Smarkm $ret .= ":".join(',',sort keys %a); 103268651Skris return $ret; 103355714Skris} 103455714Skris 103568651Skrissub maybe_add_info { 103668651Skris (my $name, *nums, my @symbols) = @_; 103768651Skris my $sym; 103868651Skris my $new_info = 0; 1039109998Smarkm my %syms=(); 104068651Skris 104168651Skris print STDERR "Updating $name info\n"; 104268651Skris foreach $sym (@symbols) { 104368651Skris (my $s, my $i) = split /\\/, $sym; 104468651Skris if (defined($nums{$s})) { 1045109998Smarkm $i =~ s/^(.*?:.*?:\w+)(\(\w+\))?/$1/; 104668651Skris (my $n, my $dummy) = split /\\/, $nums{$s}; 104768651Skris if (!defined($dummy) || $i ne $dummy) { 104868651Skris $nums{$s} = $n."\\".$i; 104968651Skris $new_info++; 1050109998Smarkm print STDERR "DEBUG: maybe_add_info for $s: \"$dummy\" => \"$i\"\n" if $debug; 105168651Skris } 105268651Skris } 1053109998Smarkm $syms{$s} = 1; 105468651Skris } 1055109998Smarkm 1056109998Smarkm my @s=sort { &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") } keys %nums; 1057109998Smarkm foreach $sym (@s) { 1058109998Smarkm (my $n, my $i) = split /\\/, $nums{$sym}; 1059109998Smarkm if (!defined($syms{$sym}) && $i !~ /^NOEXIST:/) { 1060109998Smarkm $new_info++; 1061109998Smarkm print STDERR "DEBUG: maybe_add_info for $sym: -> undefined\n" if $debug; 1062109998Smarkm } 1063109998Smarkm } 106468651Skris if ($new_info) { 106568651Skris print STDERR "$new_info old symbols got an info update\n"; 106668651Skris if (!$do_rewrite) { 106768651Skris print STDERR "You should do a rewrite to fix this.\n"; 106868651Skris } 106968651Skris } else { 107068651Skris print STDERR "No old symbols needed info update\n"; 107168651Skris } 107268651Skris} 107368651Skris 1074109998Smarkm# Param: string of comma-separated keywords, each possibly prefixed with a "!" 1075109998Smarkmsub is_valid 1076109998Smarkm{ 1077109998Smarkm my ($keywords_txt,$platforms) = @_; 1078109998Smarkm my (@keywords) = split /,/,$keywords_txt; 1079160814Ssimon my ($falsesum, $truesum) = (0, 1); 1080109998Smarkm 1081109998Smarkm # Param: one keyword 1082109998Smarkm sub recognise 1083109998Smarkm { 1084109998Smarkm my ($keyword,$platforms) = @_; 1085109998Smarkm 1086109998Smarkm if ($platforms) { 1087109998Smarkm # platforms 1088109998Smarkm if ($keyword eq "VMS" && $VMS) { return 1; } 1089109998Smarkm if ($keyword eq "WIN32" && $W32) { return 1; } 1090109998Smarkm if ($keyword eq "WIN16" && $W16) { return 1; } 1091109998Smarkm if ($keyword eq "WINNT" && $NT) { return 1; } 1092109998Smarkm if ($keyword eq "OS2" && $OS2) { return 1; } 1093109998Smarkm # Special platforms: 1094109998Smarkm # EXPORT_VAR_AS_FUNCTION means that global variables 1095109998Smarkm # will be represented as functions. This currently 1096109998Smarkm # only happens on VMS-VAX. 1097109998Smarkm if ($keyword eq "EXPORT_VAR_AS_FUNCTION" && ($VMSVAX || $W32 || $W16)) { 1098109998Smarkm return 1; 1099109998Smarkm } 1100194206Ssimon if ($keyword eq "OPENSSL_FIPS" && $fips) { 1101194206Ssimon return 1; 1102194206Ssimon } 1103194206Ssimon if ($keyword eq "ZLIB" && $zlib) { return 1; } 1104109998Smarkm return 0; 1105109998Smarkm } else { 1106109998Smarkm # algorithms 1107109998Smarkm if ($keyword eq "RC2" && $no_rc2) { return 0; } 1108109998Smarkm if ($keyword eq "RC4" && $no_rc4) { return 0; } 1109109998Smarkm if ($keyword eq "RC5" && $no_rc5) { return 0; } 1110109998Smarkm if ($keyword eq "IDEA" && $no_idea) { return 0; } 1111109998Smarkm if ($keyword eq "DES" && $no_des) { return 0; } 1112109998Smarkm if ($keyword eq "BF" && $no_bf) { return 0; } 1113109998Smarkm if ($keyword eq "CAST" && $no_cast) { return 0; } 1114109998Smarkm if ($keyword eq "MD2" && $no_md2) { return 0; } 1115109998Smarkm if ($keyword eq "MD4" && $no_md4) { return 0; } 1116109998Smarkm if ($keyword eq "MD5" && $no_md5) { return 0; } 1117109998Smarkm if ($keyword eq "SHA" && $no_sha) { return 0; } 1118109998Smarkm if ($keyword eq "RIPEMD" && $no_ripemd) { return 0; } 1119109998Smarkm if ($keyword eq "MDC2" && $no_mdc2) { return 0; } 1120109998Smarkm if ($keyword eq "RSA" && $no_rsa) { return 0; } 1121109998Smarkm if ($keyword eq "DSA" && $no_dsa) { return 0; } 1122109998Smarkm if ($keyword eq "DH" && $no_dh) { return 0; } 1123109998Smarkm if ($keyword eq "EC" && $no_ec) { return 0; } 1124160814Ssimon if ($keyword eq "ECDSA" && $no_ecdsa) { return 0; } 1125160814Ssimon if ($keyword eq "ECDH" && $no_ecdh) { return 0; } 1126109998Smarkm if ($keyword eq "HMAC" && $no_hmac) { return 0; } 1127109998Smarkm if ($keyword eq "AES" && $no_aes) { return 0; } 1128162911Ssimon if ($keyword eq "CAMELLIA" && $no_camellia) { return 0; } 1129194206Ssimon if ($keyword eq "SEED" && $no_seed) { return 0; } 1130109998Smarkm if ($keyword eq "EVP" && $no_evp) { return 0; } 1131109998Smarkm if ($keyword eq "LHASH" && $no_lhash) { return 0; } 1132109998Smarkm if ($keyword eq "STACK" && $no_stack) { return 0; } 1133109998Smarkm if ($keyword eq "ERR" && $no_err) { return 0; } 1134109998Smarkm if ($keyword eq "BUFFER" && $no_buffer) { return 0; } 1135109998Smarkm if ($keyword eq "BIO" && $no_bio) { return 0; } 1136109998Smarkm if ($keyword eq "COMP" && $no_comp) { return 0; } 1137109998Smarkm if ($keyword eq "DSO" && $no_dso) { return 0; } 1138109998Smarkm if ($keyword eq "KRB5" && $no_krb5) { return 0; } 1139111147Snectar if ($keyword eq "ENGINE" && $no_engine) { return 0; } 1140111147Snectar if ($keyword eq "HW" && $no_hw) { return 0; } 1141109998Smarkm if ($keyword eq "FP_API" && $no_fp_api) { return 0; } 1142160814Ssimon if ($keyword eq "STATIC_ENGINE" && $no_static_engine) { return 0; } 1143160814Ssimon if ($keyword eq "GMP" && $no_gmp) { return 0; } 1144167612Ssimon if ($keyword eq "RFC3779" && $no_rfc3779) { return 0; } 1145194206Ssimon if ($keyword eq "TLSEXT" && $no_tlsext) { return 0; } 1146194206Ssimon if ($keyword eq "CMS" && $no_cms) { return 0; } 1147194206Ssimon if ($keyword eq "CAPIENG" && $no_capieng) { return 0; } 1148194206Ssimon if ($keyword eq "JPAKE" && $no_jpake) { return 0; } 1149160814Ssimon if ($keyword eq "DEPRECATED" && $no_deprecated) { return 0; } 1150109998Smarkm 1151109998Smarkm # Nothing recognise as true 1152109998Smarkm return 1; 1153109998Smarkm } 1154109998Smarkm } 1155109998Smarkm 1156109998Smarkm foreach $k (@keywords) { 1157109998Smarkm if ($k =~ /^!(.*)$/) { 1158109998Smarkm $falsesum += &recognise($1,$platforms); 1159109998Smarkm } else { 1160160814Ssimon $truesum *= &recognise($k,$platforms); 1161109998Smarkm } 1162109998Smarkm } 1163109998Smarkm print STDERR "DEBUG: [",$#keywords,",",$#keywords < 0,"] is_valid($keywords_txt) => (\!$falsesum) && $truesum = ",(!$falsesum) && $truesum,"\n" if $debug; 1164109998Smarkm return (!$falsesum) && $truesum; 1165109998Smarkm} 1166109998Smarkm 116759191Skrissub print_test_file 116859191Skris{ 1169109998Smarkm (*OUT,my $name,*nums,my $testall,my @symbols)=@_; 117059191Skris my $n = 1; my @e; my @r; 117168651Skris my $sym; my $prev = ""; my $prefSSLeay; 117259191Skris 1173109998Smarkm (@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols); 1174109998Smarkm (@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:.*/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:.*/,@symbols); 117568651Skris @symbols=((sort @e),(sort @r)); 117659191Skris 117768651Skris foreach $sym (@symbols) { 117868651Skris (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/; 1179109998Smarkm my $v = 0; 1180109998Smarkm $v = 1 if $i=~ /^.*?:.*?:VARIABLE/; 1181109998Smarkm my $p = ($i =~ /^[^:]*:([^:]*):/,$1); 1182109998Smarkm my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1); 1183109998Smarkm if (!defined($nums{$s})) { 1184109998Smarkm print STDERR "Warning: $s does not have a number assigned\n" 1185109998Smarkm if(!$do_update); 1186109998Smarkm } elsif (is_valid($p,1) && is_valid($a,0)) { 1187109998Smarkm my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1); 1188109998Smarkm if ($prev eq $s2) { 1189109998Smarkm print OUT "\t/* The following has already appeared previously */\n"; 1190109998Smarkm print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n"; 1191109998Smarkm } 1192109998Smarkm $prev = $s2; # To warn about duplicates... 1193109998Smarkm 1194109998Smarkm ($nn,$ni)=($nums{$s2} =~ /^(.*?)\\(.*)$/); 1195109998Smarkm if ($v) { 1196109998Smarkm print OUT "\textern int $s2; /* type unknown */ /* $nn $ni */\n"; 119768651Skris } else { 1198109998Smarkm print OUT "\textern int $s2(); /* type unknown */ /* $nn $ni */\n"; 119968651Skris } 120059191Skris } 120159191Skris } 120259191Skris} 120359191Skris 1204127128Snectarsub get_version { 1205127128Snectar local *MF; 1206127128Snectar my $v = '?'; 1207142425Snectar open MF, 'Makefile' or return $v; 1208127128Snectar while (<MF>) { 1209127128Snectar $v = $1, last if /^VERSION=(.*?)\s*$/; 1210127128Snectar } 1211127128Snectar close MF; 1212127128Snectar return $v; 1213127128Snectar} 1214127128Snectar 121555714Skrissub print_def_file 121655714Skris{ 121768651Skris (*OUT,my $name,*nums,my @symbols)=@_; 1218109998Smarkm my $n = 1; my @e; my @r; my @v; my $prev=""; 1219109998Smarkm my $liboptions=""; 1220127128Snectar my $libname = $name; 1221127128Snectar my $http_vendor = 'www.openssl.org/'; 1222127128Snectar my $version = get_version(); 1223127128Snectar my $what = "OpenSSL: implementation of Secure Socket Layer"; 1224127128Snectar my $description = "$what $version, $name - http://$http_vendor"; 122555714Skris 122655714Skris if ($W32) 1227127128Snectar { $libname.="32"; } 1228109998Smarkm elsif ($W16) 1229127128Snectar { $libname.="16"; } 1230109998Smarkm elsif ($OS2) 1231127128Snectar { # DLL names should not clash on the whole system. 1232127128Snectar # However, they should not have any particular relationship 1233127128Snectar # to the name of the static library. Chose descriptive names 1234127128Snectar # (must be at most 8 chars). 1235127128Snectar my %translate = (ssl => 'open_ssl', crypto => 'cryptssl'); 1236127128Snectar $libname = $translate{$name} || $name; 1237127128Snectar $liboptions = <<EOO; 1238127128SnectarINITINSTANCE 1239127128SnectarDATA MULTIPLE NONSHARED 1240127128SnectarEOO 1241127128Snectar # Vendor field can't contain colon, drat; so we omit http:// 1242127128Snectar $description = "\@#$http_vendor:$version#\@$what; DLL for library $name. Build for EMX -Zmtd"; 1243127128Snectar } 124455714Skris 124555714Skris print OUT <<"EOF"; 124655714Skris; 124755714Skris; Definition file for the DLL version of the $name library from OpenSSL 124855714Skris; 124955714Skris 1250127128SnectarLIBRARY $libname $liboptions 125155714Skris 125255714SkrisEOF 125355714Skris 1254109998Smarkm if ($W16) { 125555714Skris print <<"EOF"; 125655714SkrisCODE PRELOAD MOVEABLE 125755714SkrisDATA PRELOAD MOVEABLE SINGLE 125855714Skris 125955714SkrisEXETYPE WINDOWS 126055714Skris 126155714SkrisHEAPSIZE 4096 126255714SkrisSTACKSIZE 8192 126355714Skris 126455714SkrisEOF 126555714Skris } 126655714Skris 126755714Skris print "EXPORTS\n"; 126855714Skris 1269109998Smarkm (@e)=grep(/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols); 1270109998Smarkm (@r)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:FUNCTION/ && !/^SSLeay(\{[0-9]+\})?\\.*?:.*?:FUNCTION/,@symbols); 1271109998Smarkm (@v)=grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:VARIABLE/,@symbols); 1272109998Smarkm @symbols=((sort @e),(sort @r), (sort @v)); 127355714Skris 127455714Skris 127568651Skris foreach $sym (@symbols) { 127668651Skris (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/; 1277109998Smarkm my $v = 0; 1278109998Smarkm $v = 1 if $i =~ /^.*?:.*?:VARIABLE/; 127968651Skris if (!defined($nums{$s})) { 128068651Skris printf STDERR "Warning: $s does not have a number assigned\n" 1281109998Smarkm if(!$do_update); 128255714Skris } else { 1283109998Smarkm (my $n, my $dummy) = split /\\/, $nums{$s}; 128468651Skris my %pf = (); 1285109998Smarkm my $p = ($i =~ /^[^:]*:([^:]*):/,$1); 1286109998Smarkm my $a = ($i =~ /^[^:]*:[^:]*:[^:]*:([^:]*)/,$1); 1287109998Smarkm if (is_valid($p,1) && is_valid($a,0)) { 1288109998Smarkm my $s2 = ($s =~ /^(.*?)(\{[0-9]+\})?$/, $1); 1289109998Smarkm if ($prev eq $s2) { 1290109998Smarkm print STDERR "Warning: Symbol '",$s2,"' redefined. old=",($nums{$prev} =~ /^(.*?)\\/,$1),", new=",($nums{$s2} =~ /^(.*?)\\/,$1),"\n"; 1291109998Smarkm } 1292109998Smarkm $prev = $s2; # To warn about duplicates... 1293109998Smarkm if($v && !$OS2) { 1294109998Smarkm printf OUT " %s%-39s @%-8d DATA\n",($W32)?"":"_",$s2,$n; 1295109998Smarkm } else { 1296109998Smarkm printf OUT " %s%-39s @%d\n",($W32||$OS2)?"":"_",$s2,$n; 1297109998Smarkm } 129868651Skris } 129955714Skris } 130055714Skris } 130155714Skris printf OUT "\n"; 130255714Skris} 130355714Skris 130455714Skrissub load_numbers 130555714Skris{ 130655714Skris my($name)=@_; 130755714Skris my(@a,%ret); 130855714Skris 130955714Skris $max_num = 0; 131068651Skris $num_noinfo = 0; 131168651Skris $prev = ""; 1312109998Smarkm $prev_cnt = 0; 131355714Skris 131455714Skris open(IN,"<$name") || die "unable to open $name:$!\n"; 131555714Skris while (<IN>) { 131655714Skris chop; 131755714Skris s/#.*$//; 131855714Skris next if /^\s*$/; 131955714Skris @a=split; 132068651Skris if (defined $ret{$a[0]}) { 1321109998Smarkm # This is actually perfectly OK 1322109998Smarkm #print STDERR "Warning: Symbol '",$a[0],"' redefined. old=",$ret{$a[0]},", new=",$a[1],"\n"; 132368651Skris } 132468651Skris if ($max_num > $a[1]) { 132568651Skris print STDERR "Warning: Number decreased from ",$max_num," to ",$a[1],"\n"; 132668651Skris } 1327109998Smarkm elsif ($max_num == $a[1]) { 132868651Skris # This is actually perfectly OK 132968651Skris #print STDERR "Warning: Symbol ",$a[0]," has same number as previous ",$prev,": ",$a[1],"\n"; 1330109998Smarkm if ($a[0] eq $prev) { 1331109998Smarkm $prev_cnt++; 1332109998Smarkm $a[0] .= "{$prev_cnt}"; 1333109998Smarkm } 133468651Skris } 1335109998Smarkm else { 1336109998Smarkm $prev_cnt = 0; 1337109998Smarkm } 133868651Skris if ($#a < 2) { 133968651Skris # Existence will be proven later, in do_defs 134068651Skris $ret{$a[0]}=$a[1]; 134168651Skris $num_noinfo++; 134268651Skris } else { 134368651Skris $ret{$a[0]}=$a[1]."\\".$a[2]; # \\ is a special marker 134468651Skris } 134555714Skris $max_num = $a[1] if $a[1] > $max_num; 134668651Skris $prev=$a[0]; 134755714Skris } 134868651Skris if ($num_noinfo) { 134968651Skris print STDERR "Warning: $num_noinfo symbols were without info."; 135068651Skris if ($do_rewrite) { 135168651Skris printf STDERR " The rewrite will fix this.\n"; 135268651Skris } else { 135368651Skris printf STDERR " You should do a rewrite to fix this.\n"; 135468651Skris } 135568651Skris } 135655714Skris close(IN); 135755714Skris return(%ret); 135855714Skris} 135955714Skris 136068651Skrissub parse_number 136168651Skris{ 136268651Skris (my $str, my $what) = @_; 136368651Skris (my $n, my $i) = split(/\\/,$str); 136468651Skris if ($what eq "n") { 136568651Skris return $n; 136668651Skris } else { 136768651Skris return $i; 136868651Skris } 136968651Skris} 137068651Skris 137168651Skrissub rewrite_numbers 137268651Skris{ 137368651Skris (*OUT,$name,*nums,@symbols)=@_; 137468651Skris my $thing; 137568651Skris 137668651Skris print STDERR "Rewriting $name\n"; 137768651Skris 1378109998Smarkm my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols); 137968651Skris my $r; my %r; my %rsyms; 138068651Skris foreach $r (@r) { 138168651Skris (my $s, my $i) = split /\\/, $r; 138268651Skris my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/; 138368651Skris $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/; 138468651Skris $r{$a} = $s."\\".$i; 138568651Skris $rsyms{$s} = 1; 138668651Skris } 138768651Skris 1388109998Smarkm my %syms = (); 1389109998Smarkm foreach $_ (@symbols) { 1390109998Smarkm (my $n, my $i) = split /\\/; 1391109998Smarkm $syms{$n} = 1; 1392109998Smarkm } 1393109998Smarkm 1394109998Smarkm my @s=sort { 1395109998Smarkm &parse_number($nums{$a},"n") <=> &parse_number($nums{$b},"n") 1396109998Smarkm || $a cmp $b 1397109998Smarkm } keys %nums; 139868651Skris foreach $sym (@s) { 139968651Skris (my $n, my $i) = split /\\/, $nums{$sym}; 140068651Skris next if defined($i) && $i =~ /^.*?:.*?:\w+\(\w+\)/; 140168651Skris next if defined($rsyms{$sym}); 1402109998Smarkm print STDERR "DEBUG: rewrite_numbers for sym = ",$sym,": i = ",$i,", n = ",$n,", rsym{sym} = ",$rsyms{$sym},"syms{sym} = ",$syms{$sym},"\n" if $debug; 1403109998Smarkm $i="NOEXIST::FUNCTION:" 1404109998Smarkm if !defined($i) || $i eq "" || !defined($syms{$sym}); 1405109998Smarkm my $s2 = $sym; 1406109998Smarkm $s2 =~ s/\{[0-9]+\}$//; 1407109998Smarkm printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i; 140868651Skris if (exists $r{$sym}) { 140968651Skris (my $s, $i) = split /\\/,$r{$sym}; 1410109998Smarkm my $s2 = $s; 1411109998Smarkm $s2 =~ s/\{[0-9]+\}$//; 1412109998Smarkm printf OUT "%s%-39s %d\t%s\n","",$s2,$n,$i; 141368651Skris } 141468651Skris } 141568651Skris} 141668651Skris 141755714Skrissub update_numbers 141855714Skris{ 141968651Skris (*OUT,$name,*nums,my $start_num, my @symbols)=@_; 142068651Skris my $new_syms = 0; 142168651Skris 142268651Skris print STDERR "Updating $name numbers\n"; 142368651Skris 1424109998Smarkm my @r = grep(/^\w+(\{[0-9]+\})?\\.*?:.*?:\w+\(\w+\)/,@symbols); 142568651Skris my $r; my %r; my %rsyms; 142668651Skris foreach $r (@r) { 142768651Skris (my $s, my $i) = split /\\/, $r; 142868651Skris my $a = $1 if $i =~ /^.*?:.*?:\w+\((\w+)\)/; 142968651Skris $i =~ s/^(.*?:.*?:\w+)\(\w+\)/$1/; 143068651Skris $r{$a} = $s."\\".$i; 143168651Skris $rsyms{$s} = 1; 143268651Skris } 143368651Skris 143468651Skris foreach $sym (@symbols) { 143568651Skris (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/; 143668651Skris next if $i =~ /^.*?:.*?:\w+\(\w+\)/; 143768651Skris next if defined($rsyms{$sym}); 143868651Skris die "ERROR: Symbol $sym had no info attached to it." 143968651Skris if $i eq ""; 144068651Skris if (!exists $nums{$s}) { 144168651Skris $new_syms++; 1442109998Smarkm my $s2 = $s; 1443109998Smarkm $s2 =~ s/\{[0-9]+\}$//; 1444109998Smarkm printf OUT "%s%-39s %d\t%s\n","",$s2, ++$start_num,$i; 144568651Skris if (exists $r{$s}) { 144668651Skris ($s, $i) = split /\\/,$r{$s}; 1447109998Smarkm $s =~ s/\{[0-9]+\}$//; 1448109998Smarkm printf OUT "%s%-39s %d\t%s\n","",$s, $start_num,$i; 144968651Skris } 145055714Skris } 145155714Skris } 145268651Skris if($new_syms) { 145368651Skris print STDERR "$new_syms New symbols added\n"; 145455714Skris } else { 145568651Skris print STDERR "No New symbols Added\n"; 145655714Skris } 145755714Skris} 145868651Skris 145968651Skrissub check_existing 146068651Skris{ 146168651Skris (*nums, my @symbols)=@_; 146268651Skris my %existing; my @remaining; 146368651Skris @remaining=(); 146468651Skris foreach $sym (@symbols) { 146568651Skris (my $s, my $i) = $sym =~ /^(.*?)\\(.*)$/; 146668651Skris $existing{$s}=1; 146768651Skris } 146868651Skris foreach $sym (keys %nums) { 146968651Skris if (!exists $existing{$sym}) { 147068651Skris push @remaining, $sym; 147168651Skris } 147268651Skris } 147368651Skris if(@remaining) { 147468651Skris print STDERR "The following symbols do not seem to exist:\n"; 147568651Skris foreach $sym (@remaining) { 147668651Skris print STDERR "\t",$sym,"\n"; 147768651Skris } 147868651Skris } 147968651Skris} 148068651Skris 1481