mkdef.pl 25 KB

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