mkerr.pl 22 KB

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