2
0

mkerr.pl 21 KB

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