mkerr.pl 23 KB

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