mkerr.pl 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. #! /usr/bin/env perl
  2. # Copyright 1999-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the OpenSSL license (the "License"). You may not use
  5. # this file except in compliance with the License. You can obtain a copy
  6. # in the file LICENSE in the source distribution or at
  7. # https://www.openssl.org/source/license.html
  8. my $config = "crypto/err/openssl.ec";
  9. my $debug = 0;
  10. my $unref = 0;
  11. my $rebuild = 0;
  12. my $static = 1;
  13. my $recurse = 0;
  14. my $reindex = 0;
  15. my $dowrite = 0;
  16. my $staticloader = "";
  17. my @t = localtime();
  18. my $YEAR = @t[5] + 1900;
  19. my $pack_errcode;
  20. my $load_errcode;
  21. my $errcount;
  22. my $year = (localtime)[5] + 1900;
  23. while (@ARGV) {
  24. my $arg = $ARGV[0];
  25. if($arg eq "-conf") {
  26. shift @ARGV;
  27. $config = shift @ARGV;
  28. } elsif($arg eq "-hprefix") {
  29. shift @ARGV;
  30. $hprefix = shift @ARGV;
  31. } elsif($arg eq "-debug") {
  32. $debug = 1;
  33. $unref = 1;
  34. shift @ARGV;
  35. } elsif($arg eq "-rebuild") {
  36. $rebuild = 1;
  37. shift @ARGV;
  38. } elsif($arg eq "-recurse") {
  39. $recurse = 1;
  40. shift @ARGV;
  41. } elsif($arg eq "-reindex") {
  42. $reindex = 1;
  43. shift @ARGV;
  44. } elsif($arg eq "-nostatic") {
  45. $static = 0;
  46. shift @ARGV;
  47. } elsif($arg eq "-staticloader") {
  48. $staticloader = "static ";
  49. shift @ARGV;
  50. } elsif($arg eq "-unref") {
  51. $unref = 1;
  52. shift @ARGV;
  53. } elsif($arg eq "-write") {
  54. $dowrite = 1;
  55. shift @ARGV;
  56. } elsif($arg eq "-help" || $arg eq "-h" || $arg eq "-?" || $arg eq "--help") {
  57. print STDERR <<"EOF";
  58. mkerr.pl [options] ...
  59. Options:
  60. -conf F Use the config file F instead of the default one:
  61. crypto/err/openssl.ec
  62. -hprefix P Prepend the filenames in generated #include <header>
  63. statements with prefix P. Default: 'openssl/' (without
  64. the quotes, naturally)
  65. NOTE: not used any more because our include directory
  66. structure has changed.
  67. -debug Turn on debugging verbose output on stderr.
  68. -rebuild Rebuild all header and C source files, irrespective of the
  69. fact if any error or function codes have been added/removed.
  70. Default: only update files for libraries which saw change
  71. (of course, this requires '-write' as well, or no
  72. files will be touched!)
  73. -recurse scan a preconfigured set of directories / files for error and
  74. function codes:
  75. (<crypto/*.c>, <crypto/*/*.c>, <ssl/*.c>, <apps/*.c>)
  76. When this option is NOT specified, the filelist is taken from
  77. the commandline instead. Here, wildcards may be embedded. (Be
  78. sure to escape those to prevent the shell from expanding them
  79. for you when you wish mkerr.pl to do so instead.)
  80. Default: take file list to scan from the command line.
  81. -reindex Discard the numeric values previously assigned to the error
  82. and function codes as extracted from the scanned header files;
  83. instead renumber all of them starting from 100. (Note that
  84. the numbers assigned through 'R' records in the config file
  85. remain intact.)
  86. Default: keep previously assigned numbers. (You are warned
  87. when collisions are detected.)
  88. -nostatic Generates a different source code, where these additional
  89. functions are generated for each library specified in the
  90. config file:
  91. void ERR_load_<LIB>_strings(void);
  92. void ERR_unload_<LIB>_strings(void);
  93. void ERR_<LIB>_error(int f, int r, char *fn, int ln);
  94. #define <LIB>err(f,r) ERR_<LIB>_error(f,r,OPENSSL_FILE,OPENSSL_LINE)
  95. while the code facilitates the use of these in an environment
  96. where the error support routines are dynamically loaded at
  97. runtime.
  98. Default: 'static' code generation.
  99. -staticloader Prefix generated functions with the 'static' scope modifier.
  100. Default: don't write any scope modifier prefix.
  101. -unref Print out unreferenced function and reason codes.
  102. -write Actually (over)write the generated code to the header and C
  103. source files as assigned to each library through the config
  104. file.
  105. Default: don't write.
  106. -help / -h / -? / --help Show this help text.
  107. ... Additional arguments are added to the file list to scan,
  108. assuming '-recurse' was NOT specified on the command line.
  109. EOF
  110. exit 1;
  111. } else {
  112. last;
  113. }
  114. }
  115. if($recurse) {
  116. @source = ( <crypto/*.c>, <crypto/*/*.c>, <ssl/*.c>, <ssl/*/*.c> )
  117. } else {
  118. @source = @ARGV;
  119. }
  120. # Read in the config file
  121. open(IN, "<$config") || die "Can't open config file $config";
  122. # Parse config file
  123. while(<IN>)
  124. {
  125. if(/^L\s+(\S+)\s+(\S+)\s+(\S+)/) {
  126. $hinc{$1} = $2;
  127. $libinc{$2} = $1;
  128. $cskip{$3} = $1;
  129. if($3 ne "NONE") {
  130. $csrc{$1} = $3;
  131. $fmax{$1} = 100;
  132. $rmax{$1} = 100;
  133. $fassigned{$1} = ":";
  134. $rassigned{$1} = ":";
  135. $fnew{$1} = 0;
  136. $rnew{$1} = 0;
  137. }
  138. } elsif (/^F\s+(\S+)/) {
  139. # Add extra function with $1
  140. } elsif (/^R\s+(\S+)\s+(\S+)/) {
  141. $rextra{$1} = $2;
  142. $rcodes{$1} = $2;
  143. }
  144. }
  145. close IN;
  146. # Scan each header file in turn and make a list of error codes
  147. # and function names
  148. while (($hdr, $lib) = each %libinc)
  149. {
  150. next if($hdr eq "NONE");
  151. print STDERR "Scanning header file $hdr\n" if $debug;
  152. my $line = "", $def= "", $linenr = 0, $gotfile = 0, $cpp = 0;
  153. if (open(IN, "<$hdr")) {
  154. $gotfile = 1;
  155. while(<IN>) {
  156. $linenr++;
  157. print STDERR "line: $linenr\r" if $debug;
  158. last if(/BEGIN\s+ERROR\s+CODES/);
  159. if ($line ne '') {
  160. $_ = $line . $_;
  161. $line = '';
  162. }
  163. if (/\\$/) {
  164. $line = $_;
  165. next;
  166. }
  167. if(/\/\*/) {
  168. if (not /\*\//) { # multiline comment...
  169. $line = $_; # ... just accumulate
  170. next;
  171. } else {
  172. s/\/\*.*?\*\///gs; # wipe it
  173. }
  174. }
  175. if ($cpp) {
  176. $cpp++ if /^#\s*if/;
  177. $cpp-- if /^#\s*endif/;
  178. next;
  179. }
  180. $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
  181. next if (/^\#/); # skip preprocessor directives
  182. s/{[^{}]*}//gs; # ignore {} blocks
  183. if (/\{|\/\*/) { # Add a } so editor works...
  184. $line = $_;
  185. } else {
  186. $def .= $_;
  187. }
  188. }
  189. }
  190. print STDERR " \r" if $debug;
  191. $defnr = 0;
  192. # Delete any DECLARE_ macros
  193. $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
  194. foreach (split /;/, $def) {
  195. $defnr++;
  196. print STDERR "def: $defnr\r" if $debug;
  197. # The goal is to collect function names from function declarations.
  198. s/^[\n\s]*//g;
  199. s/[\n\s]*$//g;
  200. # Skip over recognized non-function declarations
  201. next if(/typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/);
  202. # Remove STACK_OF(foo)
  203. s/STACK_OF\(\w+\)/void/;
  204. # Reduce argument lists to empty ()
  205. # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
  206. while(/\(.*\)/s) {
  207. s/\([^\(\)]+\)/\{\}/gs;
  208. s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
  209. }
  210. # pretend as we didn't use curly braces: {} -> ()
  211. s/\{\}/\(\)/gs;
  212. if (/(\w+)\s*\(\).*/s) { # first token prior [first] () is
  213. my $name = $1; # a function name!
  214. $name =~ tr/[a-z]/[A-Z]/;
  215. $ftrans{$name} = $1;
  216. } elsif (/[\(\)]/ and not (/=/)) {
  217. print STDERR "Header $hdr: cannot parse: $_;\n";
  218. }
  219. }
  220. print STDERR " \r" if $debug;
  221. next if $reindex;
  222. # Scan function and reason codes and store them: keep a note of the
  223. # maximum code used.
  224. if ($gotfile) {
  225. while(<IN>) {
  226. if(/^\#\s*define\s+(\S+)\s+(\S+)/) {
  227. $name = $1;
  228. $code = $2;
  229. next if $name =~ /^${lib}err/;
  230. unless($name =~ /^${lib}_([RF])_(\w+)$/) {
  231. print STDERR "Invalid error code $name\n";
  232. next;
  233. }
  234. if($1 eq "R") {
  235. $rcodes{$name} = $code;
  236. if ($rassigned{$lib} =~ /:$code:/) {
  237. print STDERR "!! ERROR: $lib reason code $code assigned twice (collision at $name)\n";
  238. ++$errcount;
  239. }
  240. $rassigned{$lib} .= "$code:";
  241. if(!(exists $rextra{$name}) &&
  242. ($code > $rmax{$lib}) ) {
  243. $rmax{$lib} = $code;
  244. }
  245. } else {
  246. if ($fassigned{$lib} =~ /:$code:/) {
  247. print STDERR "!! ERROR: $lib function code $code assigned twice (collision at $name)\n";
  248. ++$errcount;
  249. }
  250. $fassigned{$lib} .= "$code:";
  251. if($code > $fmax{$lib}) {
  252. $fmax{$lib} = $code;
  253. }
  254. $fcodes{$name} = $code;
  255. }
  256. }
  257. }
  258. }
  259. if ($debug) {
  260. if (defined($fmax{$lib})) {
  261. print STDERR "Max function code fmax" . "{" . "$lib" . "} = $fmax{$lib}\n";
  262. $fassigned{$lib} =~ m/^:(.*):$/;
  263. @fassigned = sort {$a <=> $b} split(":", $1);
  264. print STDERR " @fassigned\n";
  265. }
  266. if (defined($rmax{$lib})) {
  267. print STDERR "Max reason code rmax" . "{" . "$lib" . "} = $rmax{$lib}\n";
  268. $rassigned{$lib} =~ m/^:(.*):$/;
  269. @rassigned = sort {$a <=> $b} split(":", $1);
  270. print STDERR " @rassigned\n";
  271. }
  272. }
  273. if ($lib eq "SSL") {
  274. if ($rmax{$lib} >= 1000) {
  275. print STDERR "!! ERROR: SSL error codes 1000+ are reserved for alerts.\n";
  276. print STDERR "!! Any new alerts must be added to $config.\n";
  277. ++$errcount;
  278. print STDERR "\n";
  279. }
  280. }
  281. close IN;
  282. }
  283. # Scan each C source file and look for function and reason codes
  284. # This is done by looking for strings that "look like" function or
  285. # reason codes: basically anything consisting of all upper case and
  286. # numerics which has _F_ or _R_ in it and which has the name of an
  287. # error library at the start. This seems to work fine except for the
  288. # oddly named structure BIO_F_CTX which needs to be ignored.
  289. # If a code doesn't exist in list compiled from headers then mark it
  290. # with the value "X" as a place holder to give it a value later.
  291. # Store all function and reason codes found in %ufcodes and %urcodes
  292. # so all those unreferenced can be printed out.
  293. foreach $file (@source) {
  294. # Don't parse the error source file.
  295. next if exists $cskip{$file};
  296. print STDERR "File loaded: ".$file."\r" if $debug;
  297. open(IN, "<$file") || die "Can't open source file $file\n";
  298. my $func;
  299. my $linenr = 0;
  300. while(<IN>) {
  301. # skip obsoleted source files entirely!
  302. last if(/^#error\s+obsolete/);
  303. $linenr++;
  304. if (!/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/)
  305. {
  306. /^([^()]*(\([^()]*\)[^()]*)*)\(/;
  307. $1 =~ /([A-Za-z_0-9]*)$/;
  308. $func = $1;
  309. }
  310. if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
  311. next unless exists $csrc{$2};
  312. next if($1 eq "BIO_F_BUFFER_CTX");
  313. $ufcodes{$1} = 1;
  314. if(!exists $fcodes{$1}) {
  315. $fcodes{$1} = "X";
  316. $fnew{$2}++;
  317. }
  318. $ftrans{$3} = $func unless exists $ftrans{$3};
  319. if (uc $func ne $3) {
  320. print STDERR "ERROR: mismatch $file:$linenr $func:$3\n";
  321. $errcount++;
  322. }
  323. print STDERR "Function: $1\t= $fcodes{$1} (lib: $2, name: $3)\n" if $debug;
  324. }
  325. if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
  326. next unless exists $csrc{$2};
  327. $urcodes{$1} = 1;
  328. if(!exists $rcodes{$1}) {
  329. $rcodes{$1} = "X";
  330. $rnew{$2}++;
  331. }
  332. print STDERR "Reason: $1\t= $rcodes{$1} (lib: $2)\n" if $debug;
  333. }
  334. }
  335. close IN;
  336. }
  337. print STDERR " \n" if $debug;
  338. # Now process each library in turn.
  339. foreach $lib (keys %csrc)
  340. {
  341. my $hfile = $hinc{$lib};
  342. my $cfile = $csrc{$lib};
  343. if(!$fnew{$lib} && !$rnew{$lib}) {
  344. next unless $rebuild;
  345. } else {
  346. print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
  347. print STDERR " $rnew{$lib} New Reasons.\n";
  348. next unless $dowrite;
  349. }
  350. # If we get here then we have some new error codes so we
  351. # need to rebuild the header file and C file.
  352. # Make a sorted list of error and reason codes for later use.
  353. my @function = sort grep(/^${lib}_/,keys %fcodes);
  354. my @reasons = sort grep(/^${lib}_/,keys %rcodes);
  355. # Rewrite the header file
  356. $cpp = 0;
  357. $cplusplus = 0;
  358. if (open(IN, "<$hfile")) {
  359. # Copy across the old file
  360. while(<IN>) {
  361. $cplusplus = $cpp if /^#.*ifdef.*cplusplus/;
  362. $cpp++ if /^#\s*if/;
  363. $cpp-- if /^#\s*endif/;
  364. push @out, $_;
  365. last if (/BEGIN ERROR CODES/);
  366. }
  367. close IN;
  368. } else {
  369. $cpp = 1;
  370. $cplusplus = 1;
  371. push @out,
  372. "/*\n",
  373. " * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.\n",
  374. " *\n",
  375. " * Licensed under the OpenSSL license (the \"License\"). You may not use\n",
  376. " * this file except in compliance with the License. You can obtain a copy\n",
  377. " * in the file LICENSE in the source distribution or at\n",
  378. " * https://www.openssl.org/source/license.html\n",
  379. " */\n",
  380. "\n",
  381. "#ifndef HEADER_${lib}_ERR_H\n",
  382. "# define HEADER_${lib}_ERR_H\n",
  383. "\n",
  384. "# ifdef __cplusplus\n",
  385. "extern \"C\" {\n",
  386. "# endif\n",
  387. "\n",
  388. "/* BEGIN ERROR CODES */\n";
  389. }
  390. open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
  391. print OUT @out;
  392. undef @out;
  393. print OUT <<"EOF";
  394. /*
  395. * The following lines are auto generated by the script mkerr.pl. Any changes
  396. * made after this point may be overwritten when the script is next run.
  397. */
  398. EOF
  399. if($static) {
  400. print OUT <<"EOF";
  401. ${staticloader}int ERR_load_${lib}_strings(void);
  402. EOF
  403. } else {
  404. print OUT <<"EOF";
  405. ${staticloader}int ERR_load_${lib}_strings(void);
  406. ${staticloader}void ERR_unload_${lib}_strings(void);
  407. ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
  408. # define ${lib}err(f,r) ERR_${lib}_error((f),(r),OPENSSL_FILE,OPENSSL_LINE)
  409. EOF
  410. }
  411. print OUT <<"EOF";
  412. /* Error codes for the $lib functions. */
  413. /* Function codes. */
  414. EOF
  415. foreach $i (@function) {
  416. $z=48 - length($i);
  417. if($fcodes{$i} eq "X") {
  418. $fassigned{$lib} =~ m/^:([^:]*):/;
  419. $findcode = $1;
  420. if (!defined($findcode)) {
  421. $findcode = $fmax{$lib};
  422. }
  423. while ($fassigned{$lib} =~ m/:$findcode:/) {
  424. $findcode++;
  425. }
  426. $fcodes{$i} = $findcode;
  427. $fassigned{$lib} .= "$findcode:";
  428. print STDERR "New Function code $i\n" if $debug;
  429. }
  430. printf OUT "# define $i%s $fcodes{$i}\n"," " x $z;
  431. }
  432. print OUT "\n/* Reason codes. */\n";
  433. foreach $i (@reasons) {
  434. $z=48 - length($i);
  435. if($rcodes{$i} eq "X") {
  436. $rassigned{$lib} =~ m/^:([^:]*):/;
  437. $findcode = $1;
  438. if (!defined($findcode)) {
  439. $findcode = $rmax{$lib};
  440. }
  441. while ($rassigned{$lib} =~ m/:$findcode:/) {
  442. $findcode++;
  443. }
  444. $rcodes{$i} = $findcode;
  445. $rassigned{$lib} .= "$findcode:";
  446. print STDERR "New Reason code $i\n" if $debug;
  447. }
  448. printf OUT "# define $i%s $rcodes{$i}\n"," " x $z;
  449. }
  450. print OUT <<"EOF";
  451. EOF
  452. do {
  453. if ($cplusplus == $cpp) {
  454. print OUT "#", " "x$cpp, "ifdef __cplusplus\n";
  455. print OUT "}\n";
  456. print OUT "#", " "x$cpp, "endif\n";
  457. }
  458. if ($cpp-- > 0) {
  459. print OUT "#", " "x$cpp, "endif\n";
  460. }
  461. } while ($cpp);
  462. close OUT;
  463. # Rewrite the C source file containing the error details.
  464. # First, read any existing reason string definitions:
  465. my %err_reason_strings;
  466. if (open(IN,"<$cfile")) {
  467. my $line = "";
  468. while (<IN>) {
  469. s|\R$||; # Better chomp
  470. $_ = $line . $_;
  471. $line = "";
  472. if (/{ERR_(FUNC|REASON)\(/) {
  473. if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
  474. $err_reason_strings{$1} = $2;
  475. } elsif (/\b${lib}_F_(\w*)\b.*\"(.*)\"/) {
  476. if (!exists $ftrans{$1} && ($1 ne $2)) {
  477. print STDERR "WARNING: Mismatched function string $2\n";
  478. $ftrans{$1} = $2;
  479. }
  480. } else {
  481. $line = $_;
  482. }
  483. }
  484. }
  485. close(IN);
  486. }
  487. my $hincf;
  488. if($static) {
  489. $hincf = $hfile;
  490. $hincf =~ s|.*include/||;
  491. if ($hincf =~ m|^openssl/|) {
  492. $hincf = "<${hincf}>";
  493. } else {
  494. $hincf = "\"${hincf}\"";
  495. }
  496. } else {
  497. $hincf = "\"$hfile\"";
  498. }
  499. # If static we know the error code at compile time so use it
  500. # in error definitions.
  501. if ($static)
  502. {
  503. $pack_errcode = "ERR_LIB_${lib}";
  504. $load_errcode = "0";
  505. }
  506. else
  507. {
  508. $pack_errcode = "0";
  509. $load_errcode = "ERR_LIB_${lib}";
  510. }
  511. open (OUT,">$cfile") || die "Can't open $cfile for writing";
  512. print OUT <<"EOF";
  513. /*
  514. * Generated by util/mkerr.pl DO NOT EDIT
  515. * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  516. *
  517. * Licensed under the OpenSSL license (the "License"). You may not use
  518. * this file except in compliance with the License. You can obtain a copy
  519. * in the file LICENSE in the source distribution or at
  520. * https://www.openssl.org/source/license.html
  521. */
  522. #include <stdio.h>
  523. #include <openssl/err.h>
  524. #include $hincf
  525. /* BEGIN ERROR CODES */
  526. #ifndef OPENSSL_NO_ERR
  527. # define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
  528. # define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
  529. static ERR_STRING_DATA ${lib}_str_functs[] = {
  530. EOF
  531. # Add each function code: if a function name is found then use it.
  532. foreach $i (@function) {
  533. my $fn;
  534. $i =~ /^${lib}_F_(\S+)$/;
  535. $fn = $1;
  536. if(exists $ftrans{$fn}) {
  537. $fn = $ftrans{$fn};
  538. }
  539. # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
  540. if(length($i) + length($fn) > 57) {
  541. print OUT " {ERR_FUNC($i),\n \"$fn\"},\n";
  542. } else {
  543. print OUT " {ERR_FUNC($i), \"$fn\"},\n";
  544. }
  545. }
  546. print OUT <<"EOF";
  547. {0, NULL}
  548. };
  549. static ERR_STRING_DATA ${lib}_str_reasons[] = {
  550. EOF
  551. # Add each reason code.
  552. foreach $i (@reasons) {
  553. my $rn;
  554. my $rstr = "ERR_REASON($i)";
  555. if (exists $err_reason_strings{$i}) {
  556. $rn = $err_reason_strings{$i};
  557. } else {
  558. $i =~ /^${lib}_R_(\S+)$/;
  559. $rn = $1;
  560. $rn =~ tr/_[A-Z]/ [a-z]/;
  561. }
  562. if(length($i) + length($rn) > 55) {
  563. print OUT " {${rstr},\n \"$rn\"},\n";
  564. } else {
  565. print OUT " {${rstr}, \"$rn\"},\n";
  566. }
  567. }
  568. if($static) {
  569. print OUT <<"EOF";
  570. {0, NULL}
  571. };
  572. #endif
  573. ${staticloader}int ERR_load_${lib}_strings(void)
  574. {
  575. #ifndef OPENSSL_NO_ERR
  576. if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) {
  577. ERR_load_strings($load_errcode, ${lib}_str_functs);
  578. ERR_load_strings($load_errcode, ${lib}_str_reasons);
  579. }
  580. #endif
  581. return 1;
  582. }
  583. EOF
  584. } else {
  585. print OUT <<"EOF";
  586. {0, NULL}
  587. };
  588. #endif
  589. #ifdef ${lib}_LIB_NAME
  590. static ERR_STRING_DATA ${lib}_lib_name[] = {
  591. {0, ${lib}_LIB_NAME},
  592. {0, NULL}
  593. };
  594. #endif
  595. static int ${lib}_lib_error_code = 0;
  596. static int ${lib}_error_init = 1;
  597. ${staticloader}int ERR_load_${lib}_strings(void)
  598. {
  599. if (${lib}_lib_error_code == 0)
  600. ${lib}_lib_error_code = ERR_get_next_error_library();
  601. if (${lib}_error_init) {
  602. ${lib}_error_init = 0;
  603. #ifndef OPENSSL_NO_ERR
  604. ERR_load_strings(${lib}_lib_error_code, ${lib}_str_functs);
  605. ERR_load_strings(${lib}_lib_error_code, ${lib}_str_reasons);
  606. #endif
  607. #ifdef ${lib}_LIB_NAME
  608. ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code, 0, 0);
  609. ERR_load_strings(0, ${lib}_lib_name);
  610. #endif
  611. }
  612. return 1;
  613. }
  614. ${staticloader}void ERR_unload_${lib}_strings(void)
  615. {
  616. if (${lib}_error_init == 0) {
  617. #ifndef OPENSSL_NO_ERR
  618. ERR_unload_strings(${lib}_lib_error_code, ${lib}_str_functs);
  619. ERR_unload_strings(${lib}_lib_error_code, ${lib}_str_reasons);
  620. #endif
  621. #ifdef ${lib}_LIB_NAME
  622. ERR_unload_strings(0, ${lib}_lib_name);
  623. #endif
  624. ${lib}_error_init = 1;
  625. }
  626. }
  627. ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
  628. {
  629. if (${lib}_lib_error_code == 0)
  630. ${lib}_lib_error_code = ERR_get_next_error_library();
  631. ERR_PUT_error(${lib}_lib_error_code, function, reason, file, line);
  632. }
  633. EOF
  634. }
  635. close OUT;
  636. undef %err_reason_strings;
  637. }
  638. if($debug && %notrans) {
  639. print STDERR "The following function codes were not translated:\n";
  640. foreach(sort keys %notrans)
  641. {
  642. print STDERR "$_\n";
  643. }
  644. }
  645. # Make a list of unreferenced function and reason codes
  646. foreach (keys %fcodes) {
  647. push (@funref, $_) unless exists $ufcodes{$_};
  648. }
  649. foreach (keys %rcodes) {
  650. push (@runref, $_) unless exists $urcodes{$_};
  651. }
  652. if($unref && @funref) {
  653. print STDERR "The following function codes were not referenced:\n";
  654. foreach(sort @funref)
  655. {
  656. print STDERR "$_\n";
  657. }
  658. }
  659. if($unref && @runref) {
  660. print STDERR "The following reason codes were not referenced:\n";
  661. foreach(sort @runref)
  662. {
  663. print STDERR "$_\n";
  664. }
  665. }
  666. if($errcount) {
  667. print STDERR "There were errors, failing...\n\n";
  668. exit $errcount;
  669. }