mkerr.pl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727
  1. #! /usr/bin/env perl
  2. # Copyright 1999-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. use strict;
  9. use warnings;
  10. use lib ".";
  11. use configdata;
  12. my $config = "crypto/err/openssl.ec";
  13. my $debug = 0;
  14. my $internal = 0;
  15. my $nowrite = 0;
  16. my $rebuild = 0;
  17. my $reindex = 0;
  18. my $static = 0;
  19. my $unref = 0;
  20. my %modules = ();
  21. my $errors = 0;
  22. my @t = localtime();
  23. my $YEAR = $t[5] + 1900;
  24. sub phase
  25. {
  26. my $text = uc(shift);
  27. print STDERR "\n---\n$text\n" if $debug;
  28. }
  29. sub help
  30. {
  31. print STDERR <<"EOF";
  32. mkerr.pl [options] [files...]
  33. Options:
  34. -conf FILE Use the named config file FILE instead of the default.
  35. -debug Verbose output debugging on stderr.
  36. -internal Generate code that is to be built as part of OpenSSL itself.
  37. Also scans internal list of files.
  38. -module M Only useful with -internal!
  39. Only write files for library module M. Whether files are
  40. actually written or not depends on other options, such as
  41. -rebuild.
  42. Note: this option is cumulative. If not given at all, all
  43. internal modules will be considered.
  44. -nowrite Do not write the header/source files, even if changed.
  45. -rebuild Rebuild all header and C source files, even if there
  46. were no changes.
  47. -reindex Ignore previously assigned values (except for R records in
  48. the config file) and renumber everything starting at 100.
  49. -static Make the load/unload functions static.
  50. -unref List all unreferenced function and reason codes on stderr;
  51. implies -nowrite.
  52. -help Show this help text.
  53. ... Additional arguments are added to the file list to scan,
  54. if '-internal' was NOT specified on the command line.
  55. EOF
  56. }
  57. while ( @ARGV ) {
  58. my $arg = $ARGV[0];
  59. last unless $arg =~ /-.*/;
  60. $arg = $1 if $arg =~ /-(-.*)/;
  61. if ( $arg eq "-conf" ) {
  62. $config = $ARGV[1];
  63. shift @ARGV;
  64. } elsif ( $arg eq "-debug" ) {
  65. $debug = 1;
  66. $unref = 1;
  67. } elsif ( $arg eq "-internal" ) {
  68. $internal = 1;
  69. } elsif ( $arg eq "-nowrite" ) {
  70. $nowrite = 1;
  71. } elsif ( $arg eq "-rebuild" ) {
  72. $rebuild = 1;
  73. } elsif ( $arg eq "-reindex" ) {
  74. $reindex = 1;
  75. } elsif ( $arg eq "-static" ) {
  76. $static = 1;
  77. } elsif ( $arg eq "-unref" ) {
  78. $unref = 1;
  79. $nowrite = 1;
  80. } elsif ( $arg eq "-module" ) {
  81. shift @ARGV;
  82. $modules{uc $ARGV[0]} = 1;
  83. } elsif ( $arg =~ /-*h(elp)?/ ) {
  84. &help();
  85. exit;
  86. } elsif ( $arg =~ /-.*/ ) {
  87. die "Unknown option $arg; use -h for help.\n";
  88. }
  89. shift @ARGV;
  90. }
  91. my @source;
  92. if ( $internal ) {
  93. die "Cannot mix -internal and -static\n" if $static;
  94. die "Extra parameters given.\n" if @ARGV;
  95. @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
  96. glob('ssl/*.c'), glob('ssl/*/*.c'), glob('providers/*.c'),
  97. glob('providers/*/*.c'), glob('providers/*/*/*.c') );
  98. } else {
  99. die "-module isn't useful without -internal\n" if scalar keys %modules > 0;
  100. @source = @ARGV;
  101. }
  102. # Data parsed out of the config and state files.
  103. # We always map function-code values to zero, so items marked below with
  104. # an asterisk could eventually be removed. TODO(4.0)
  105. my %hinc; # lib -> header
  106. my %libinc; # header -> lib
  107. my %cskip; # error_file -> lib
  108. my %errorfile; # lib -> error file name
  109. my %fmax; # lib -> max assigned function code*
  110. my %rmax; # lib -> max assigned reason code
  111. my %fassigned; # lib -> colon-separated list of assigned function codes*
  112. my %rassigned; # lib -> colon-separated list of assigned reason codes
  113. my %fnew; # lib -> count of new function codes*
  114. my %rnew; # lib -> count of new reason codes
  115. my %rextra; # "extra" reason code -> lib
  116. my %rcodes; # reason-name -> value
  117. my %ftrans; # old name -> #define-friendly name (all caps)*
  118. my %fcodes; # function-name -> value*
  119. my $statefile; # state file with assigned reason and function codes
  120. my %strings; # define -> text
  121. # Read and parse the config file
  122. open(IN, "$config") || die "Can't open config file $config, $!,";
  123. while ( <IN> ) {
  124. next if /^#/ || /^$/;
  125. if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)/ ) {
  126. my $lib = $1;
  127. my $hdr = $2;
  128. my $err = $3;
  129. $hinc{$lib} = $hdr;
  130. $libinc{$hdr} = $lib;
  131. $cskip{$err} = $lib;
  132. next if $err eq 'NONE';
  133. $errorfile{$lib} = $err;
  134. $fmax{$lib} = 100;
  135. $rmax{$lib} = 100;
  136. $fassigned{$lib} = ":";
  137. $rassigned{$lib} = ":";
  138. $fnew{$lib} = 0;
  139. $rnew{$lib} = 0;
  140. } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
  141. $rextra{$1} = $2;
  142. $rcodes{$1} = $2;
  143. } elsif ( /^S\s+(\S+)/ ) {
  144. $statefile = $1;
  145. } else {
  146. die "Illegal config line $_\n";
  147. }
  148. }
  149. close IN;
  150. if ( ! $statefile ) {
  151. $statefile = $config;
  152. $statefile =~ s/.ec/.txt/;
  153. }
  154. # The statefile has all the previous assignments.
  155. &phase("Reading state");
  156. my $skippedstate = 0;
  157. if ( ! $reindex && $statefile ) {
  158. open(STATE, "<$statefile") || die "Can't open $statefile, $!";
  159. # Scan function and reason codes and store them: keep a note of the
  160. # maximum code used.
  161. while ( <STATE> ) {
  162. next if /^#/ || /^$/;
  163. my $name;
  164. my $code;
  165. if ( /^(.+):(\d+):\\$/ ) {
  166. $name = $1;
  167. $code = $2;
  168. my $next = <STATE>;
  169. $next =~ s/^\s*(.*)\s*$/$1/;
  170. die "Duplicate define $name" if exists $strings{$name};
  171. $strings{$name} = $next;
  172. } elsif ( /^(\S+):(\d+):(.*)$/ ) {
  173. $name = $1;
  174. $code = $2;
  175. die "Duplicate define $name" if exists $strings{$name};
  176. $strings{$name} = $3;
  177. } else {
  178. die "Bad line in $statefile:\n$_\n";
  179. }
  180. my $lib = $name;
  181. $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/;
  182. $lib = "SSL" if $lib =~ /TLS/;
  183. if ( !defined $errorfile{$lib} ) {
  184. print "Skipping $_";
  185. $skippedstate++;
  186. next;
  187. }
  188. if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) {
  189. die "$lib reason code $code collision at $name\n"
  190. if $rassigned{$lib} =~ /:$code:/;
  191. $rassigned{$lib} .= "$code:";
  192. if ( !exists $rextra{$name} ) {
  193. $rmax{$lib} = $code if $code > $rmax{$lib};
  194. }
  195. $rcodes{$name} = $code;
  196. } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) {
  197. $fassigned{$lib} .= "$code:";
  198. $fmax{$lib} = $code if $code > $fmax{$lib};
  199. $fcodes{$name} = $code;
  200. } else {
  201. die "Bad line in $statefile:\n$_\n";
  202. }
  203. }
  204. close(STATE);
  205. if ( $debug ) {
  206. foreach my $lib ( sort keys %rmax ) {
  207. print STDERR "Reason codes for ${lib}:\n";
  208. if ( $rassigned{$lib} =~ m/^:(.*):$/ ) {
  209. my @rassigned = sort { $a <=> $b } split( ":", $1 );
  210. print STDERR " ", join(' ', @rassigned), "\n";
  211. } else {
  212. print STDERR " --none--\n";
  213. }
  214. }
  215. print STDERR "\n";
  216. foreach my $lib ( sort keys %fmax ) {
  217. print STDERR "Function codes for ${lib}:\n";
  218. if ( $fassigned{$lib} =~ m/^:(.*):$/ ) {
  219. my @fassigned = sort { $a <=> $b } split( ":", $1 );
  220. print STDERR " ", join(' ', @fassigned), "\n";
  221. } else {
  222. print STDERR " --none--\n";
  223. }
  224. }
  225. }
  226. }
  227. # Scan each header file and make a list of error codes
  228. # and function names
  229. &phase("Scanning headers");
  230. while ( ( my $hdr, my $lib ) = each %libinc ) {
  231. next if $hdr eq "NONE";
  232. print STDERR " ." if $debug;
  233. my $line = "";
  234. my $def = "";
  235. my $linenr = 0;
  236. my $cpp = 0;
  237. open(IN, "<$hdr") || die "Can't open $hdr, $!,";
  238. while ( <IN> ) {
  239. $linenr++;
  240. if ( $line ne '' ) {
  241. $_ = $line . $_;
  242. $line = '';
  243. }
  244. if ( /\\$/ ) {
  245. $line = $_;
  246. next;
  247. }
  248. if ( /\/\*/ ) {
  249. if ( not /\*\// ) { # multiline comment...
  250. $line = $_; # ... just accumulate
  251. next;
  252. } else {
  253. s/\/\*.*?\*\///gs; # wipe it
  254. }
  255. }
  256. if ( $cpp ) {
  257. $cpp++ if /^#\s*if/;
  258. $cpp-- if /^#\s*endif/;
  259. next;
  260. }
  261. $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
  262. next if /^\#/; # skip preprocessor directives
  263. s/{[^{}]*}//gs; # ignore {} blocks
  264. if ( /\{|\/\*/ ) { # Add a so editor works...
  265. $line = $_;
  266. } else {
  267. $def .= $_;
  268. }
  269. }
  270. # Delete any DECLARE_ macros
  271. my $defnr = 0;
  272. $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
  273. foreach ( split /;/, $def ) {
  274. $defnr++;
  275. # The goal is to collect function names from function declarations.
  276. s/^[\n\s]*//g;
  277. s/[\n\s]*$//g;
  278. # Skip over recognized non-function declarations
  279. next if /typedef\W/;
  280. # Remove STACK_OF(foo)
  281. s/STACK_OF\(\w+\)/void/;
  282. # Reduce argument lists to empty ()
  283. # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
  284. while ( /\(.*\)/s ) {
  285. s/\([^\(\)]+\)/\{\}/gs;
  286. s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
  287. }
  288. # pretend as we didn't use curly braces: {} -> ()
  289. s/\{\}/\(\)/gs;
  290. # Last token just before the first () is a function name.
  291. if ( /(\w+)\s*\(\).*/s ) {
  292. my $name = $1;
  293. $name =~ tr/[a-z]/[A-Z]/;
  294. $ftrans{$name} = $1;
  295. } elsif ( /[\(\)]/ and not(/=/) ) {
  296. print STDERR "Header $hdr: cannot parse: $_;\n";
  297. }
  298. }
  299. next if $reindex;
  300. if ( $lib eq "SSL" && $rmax{$lib} >= 1000 ) {
  301. print STDERR "SSL error codes 1000+ are reserved for alerts.\n";
  302. print STDERR "Any new alerts must be added to $config.\n";
  303. $errors++;
  304. }
  305. close IN;
  306. }
  307. print STDERR "\n" if $debug;
  308. # Scan each C source file and look for function and reason codes
  309. # This is done by looking for strings that "look like" function or
  310. # reason codes: basically anything consisting of all upper case and
  311. # numerics which has _F_ or _R_ in it and which has the name of an
  312. # error library at the start. This seems to work fine except for the
  313. # oddly named structure BIO_F_CTX which needs to be ignored.
  314. # If a code doesn't exist in list compiled from headers then mark it
  315. # with the value "X" as a place holder to give it a value later.
  316. # Store all function and reason codes found in %usedfuncs and %usedreasons
  317. # so all those unreferenced can be printed out.
  318. &phase("Scanning source");
  319. my %usedfuncs;
  320. my %usedreasons;
  321. foreach my $file ( @source ) {
  322. # Don't parse the error source file.
  323. next if exists $cskip{$file};
  324. open( IN, "<$file" ) || die "Can't open $file, $!,";
  325. my $func;
  326. my $linenr = 0;
  327. print STDERR "$file:\n" if $debug;
  328. while ( <IN> ) {
  329. # skip obsoleted source files entirely!
  330. last if /^#error\s+obsolete/;
  331. $linenr++;
  332. if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
  333. /^([^()]*(\([^()]*\)[^()]*)*)\(/;
  334. $1 =~ /([A-Za-z_0-9]*)$/;
  335. $func = $1;
  336. }
  337. if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_F_([A-Z0-9_]+))/ ) {
  338. next unless exists $errorfile{$2};
  339. next if $1 eq "BIO_F_BUFFER_CTX";
  340. $usedfuncs{$1} = 1;
  341. if ( !exists $fcodes{$1} ) {
  342. print STDERR " New function $1\n" if $debug;
  343. $fcodes{$1} = "X";
  344. $fnew{$2}++;
  345. }
  346. $ftrans{$3} = $func unless exists $ftrans{$3};
  347. print STDERR " Function $1 = $fcodes{$1}\n"
  348. if $debug;
  349. }
  350. if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) {
  351. next unless exists $errorfile{$2};
  352. $usedreasons{$1} = 1;
  353. if ( !exists $rcodes{$1} ) {
  354. print STDERR " New reason $1\n" if $debug;
  355. $rcodes{$1} = "X";
  356. $rnew{$2}++;
  357. }
  358. print STDERR " Reason $1 = $rcodes{$1}\n" if $debug;
  359. }
  360. }
  361. close IN;
  362. }
  363. print STDERR "\n" if $debug;
  364. # Now process each library in turn.
  365. &phase("Writing files");
  366. my $newstate = 0;
  367. foreach my $lib ( keys %errorfile ) {
  368. next if ! $fnew{$lib} && ! $rnew{$lib} && ! $rebuild;
  369. next if scalar keys %modules > 0 && !$modules{$lib};
  370. next if $nowrite;
  371. next if $hinc{$lib} eq 'NONE';
  372. print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
  373. print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
  374. $newstate = 1;
  375. # If we get here then we have some new error codes so we
  376. # need to rebuild the header file and C file.
  377. # Make a sorted list of error and reason codes for later use.
  378. my @function = sort grep( /^${lib}_/, keys %fcodes );
  379. my @reasons = sort grep( /^${lib}_/, keys %rcodes );
  380. # indent level for innermost preprocessor lines
  381. my $indent = " ";
  382. # Rewrite the header file
  383. my $hfile = $hinc{$lib};
  384. $hfile =~ s/.h$/err.h/ if $internal;
  385. open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
  386. print OUT <<"EOF";
  387. /*
  388. * Generated by util/mkerr.pl DO NOT EDIT
  389. * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  390. *
  391. * Licensed under the Apache License 2.0 (the \"License\"). You may not use
  392. * this file except in compliance with the License. You can obtain a copy
  393. * in the file LICENSE in the source distribution or at
  394. * https://www.openssl.org/source/license.html
  395. */
  396. #ifndef OPENSSL_${lib}ERR_H
  397. # define OPENSSL_${lib}ERR_H
  398. # pragma once
  399. # include <openssl/opensslconf.h>
  400. # include <openssl/symhacks.h>
  401. EOF
  402. if ( $internal ) {
  403. # Declare the load function because the generate C file
  404. # includes "fooerr.h" not "foo.h"
  405. if ($lib ne "SSL" && $lib ne "ASYNC"
  406. && (grep { $lib eq uc $_ } @disablables, @disablables_int)) {
  407. print OUT <<"EOF";
  408. # include <openssl/opensslconf.h>
  409. # ifndef OPENSSL_NO_${lib}
  410. EOF
  411. $indent = " ";
  412. }
  413. print OUT <<"EOF";
  414. #${indent}ifdef __cplusplus
  415. extern \"C\"
  416. #${indent}endif
  417. int ERR_load_${lib}_strings(void);
  418. EOF
  419. } else {
  420. print OUT <<"EOF";
  421. # define ${lib}err(f, r) ERR_${lib}_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
  422. EOF
  423. if ( ! $static ) {
  424. print OUT <<"EOF";
  425. # ifdef __cplusplus
  426. extern \"C\" {
  427. # endif
  428. int ERR_load_${lib}_strings(void);
  429. void ERR_unload_${lib}_strings(void);
  430. void ERR_${lib}_error(int function, int reason, char *file, int line);
  431. # ifdef __cplusplus
  432. }
  433. # endif
  434. EOF
  435. }
  436. }
  437. print OUT "\n/*\n * $lib function codes.\n */\n";
  438. print OUT "# ifndef OPENSSL_NO_DEPRECATED_3_0\n";
  439. foreach my $i ( @function ) {
  440. my $z = 48 - length($i);
  441. $z = 0 if $z < 0;
  442. if ( $fcodes{$i} eq "X" ) {
  443. $fassigned{$lib} =~ m/^:([^:]*):/;
  444. my $findcode = $1;
  445. $findcode = $fmax{$lib} if !defined $findcode;
  446. while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
  447. $findcode++;
  448. }
  449. $fcodes{$i} = $findcode;
  450. $fassigned{$lib} .= "$findcode:";
  451. print STDERR "New Function code $i\n" if $debug;
  452. }
  453. printf OUT "#${indent} define $i%s 0\n", " " x $z;
  454. }
  455. print OUT "# endif\n";
  456. print OUT "\n/*\n * $lib reason codes.\n */\n";
  457. foreach my $i ( @reasons ) {
  458. my $z = 48 - length($i);
  459. $z = 0 if $z < 0;
  460. if ( $rcodes{$i} eq "X" ) {
  461. $rassigned{$lib} =~ m/^:([^:]*):/;
  462. my $findcode = $1;
  463. $findcode = $rmax{$lib} if !defined $findcode;
  464. while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
  465. $findcode++;
  466. }
  467. $rcodes{$i} = $findcode;
  468. $rassigned{$lib} .= "$findcode:";
  469. print STDERR "New Reason code $i\n" if $debug;
  470. }
  471. printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z;
  472. }
  473. print OUT "\n";
  474. while (length($indent) > 0) {
  475. $indent = substr $indent, 0, -1;
  476. print OUT "#${indent}endif\n";
  477. }
  478. # Rewrite the C source file containing the error details.
  479. # First, read any existing reason string definitions:
  480. my $cfile = $errorfile{$lib};
  481. my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
  482. my $hincf = $hfile;
  483. $hincf =~ s|.*include/||;
  484. if ( $hincf =~ m|^openssl/| ) {
  485. $hincf = "<${hincf}>";
  486. } else {
  487. $hincf = "\"${hincf}\"";
  488. }
  489. open( OUT, ">$cfile" )
  490. || die "Can't open $cfile for writing, $!, stopped";
  491. my $const = $internal ? 'const ' : '';
  492. print OUT <<"EOF";
  493. /*
  494. * Generated by util/mkerr.pl DO NOT EDIT
  495. * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  496. *
  497. * Licensed under the Apache License 2.0 (the "License"). You may not use
  498. * this file except in compliance with the License. You can obtain a copy
  499. * in the file LICENSE in the source distribution or at
  500. * https://www.openssl.org/source/license.html
  501. */
  502. #include <openssl/err.h>
  503. #include $hincf
  504. #ifndef OPENSSL_NO_ERR
  505. static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
  506. EOF
  507. # Add each reason code.
  508. foreach my $i ( @reasons ) {
  509. my $rn;
  510. if ( exists $strings{$i} ) {
  511. $rn = $strings{$i};
  512. $rn = "" if $rn eq '*';
  513. } else {
  514. $i =~ /^${lib}_R_(\S+)$/;
  515. $rn = $1;
  516. $rn =~ tr/_[A-Z]/ [a-z]/;
  517. $strings{$i} = $rn;
  518. }
  519. my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
  520. if ( length($short) <= 80 ) {
  521. print OUT "$short\n";
  522. } else {
  523. print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n";
  524. }
  525. }
  526. print OUT <<"EOF";
  527. {0, NULL}
  528. };
  529. #endif
  530. EOF
  531. if ( $internal ) {
  532. print OUT <<"EOF";
  533. int ERR_load_${lib}_strings(void)
  534. {
  535. #ifndef OPENSSL_NO_ERR
  536. if (ERR_reason_error_string(${lib}_str_reasons[0].error) == NULL)
  537. ERR_load_strings_const(${lib}_str_reasons);
  538. #endif
  539. return 1;
  540. }
  541. EOF
  542. } else {
  543. my $st = $static ? "static " : "";
  544. print OUT <<"EOF";
  545. static int lib_code = 0;
  546. static int error_loaded = 0;
  547. ${st}int ERR_load_${lib}_strings(void)
  548. {
  549. if (lib_code == 0)
  550. lib_code = ERR_get_next_error_library();
  551. if (!error_loaded) {
  552. #ifndef OPENSSL_NO_ERR
  553. ERR_load_strings(lib_code, ${lib}_str_reasons);
  554. #endif
  555. error_loaded = 1;
  556. }
  557. return 1;
  558. }
  559. ${st}void ERR_unload_${lib}_strings(void)
  560. {
  561. if (error_loaded) {
  562. #ifndef OPENSSL_NO_ERR
  563. ERR_unload_strings(lib_code, ${lib}_str_reasons);
  564. #endif
  565. error_loaded = 0;
  566. }
  567. }
  568. ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
  569. {
  570. if (lib_code == 0)
  571. lib_code = ERR_get_next_error_library();
  572. ERR_raise(lib_code, reason);
  573. ERR_set_debug(file, line, NULL);
  574. }
  575. EOF
  576. }
  577. close OUT;
  578. }
  579. &phase("Ending");
  580. # Make a list of unreferenced function and reason codes
  581. if ( $unref ) {
  582. my @funref;
  583. foreach ( keys %fcodes ) {
  584. push( @funref, $_ ) unless exists $usedfuncs{$_};
  585. }
  586. my @runref;
  587. foreach ( keys %rcodes ) {
  588. push( @runref, $_ ) unless exists $usedreasons{$_};
  589. }
  590. if ( @funref ) {
  591. print STDERR "The following function codes were not referenced:\n";
  592. foreach ( sort @funref ) {
  593. print STDERR " $_\n";
  594. }
  595. }
  596. if ( @runref ) {
  597. print STDERR "The following reason codes were not referenced:\n";
  598. foreach ( sort @runref ) {
  599. print STDERR " $_\n";
  600. }
  601. }
  602. }
  603. die "Found $errors errors, quitting" if $errors;
  604. # Update the state file
  605. if ( $newstate ) {
  606. open(OUT, ">$statefile.new")
  607. || die "Can't write $statefile.new, $!";
  608. print OUT <<"EOF";
  609. # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  610. #
  611. # Licensed under the Apache License 2.0 (the "License"). You may not use
  612. # this file except in compliance with the License. You can obtain a copy
  613. # in the file LICENSE in the source distribution or at
  614. # https://www.openssl.org/source/license.html
  615. EOF
  616. print OUT "\n# Function codes\n";
  617. foreach my $i ( sort keys %fcodes ) {
  618. my $short = "$i:$fcodes{$i}:";
  619. my $t = exists $strings{$i} ? $strings{$i} : "";
  620. $t = "\\\n\t" . $t if length($short) + length($t) > 80;
  621. print OUT "$short$t\n";
  622. }
  623. print OUT "\n#Reason codes\n";
  624. foreach my $i ( sort keys %rcodes ) {
  625. my $short = "$i:$rcodes{$i}:";
  626. my $t = exists $strings{$i} ? "$strings{$i}" : "";
  627. $t = "\\\n\t" . $t if length($short) + length($t) > 80;
  628. print OUT "$short$t\n" if !exists $rextra{$i};
  629. }
  630. close(OUT);
  631. if ( $skippedstate ) {
  632. print "Skipped state, leaving update in $statefile.new";
  633. } else {
  634. rename "$statefile", "$statefile.old"
  635. || die "Can't backup $statefile to $statefile.old, $!";
  636. rename "$statefile.new", "$statefile"
  637. || die "Can't rename $statefile to $statefile.new, $!";
  638. }
  639. }
  640. exit;