mkerr.pl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  1. #! /usr/bin/env perl
  2. # Copyright 1999-2019 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. next if ! $fnew{$lib} && ! $rnew{$lib} && ! $rebuild;
  372. next if scalar keys %modules > 0 && !$modules{$lib};
  373. next if $nowrite;
  374. print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
  375. print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
  376. $newstate = 1;
  377. # If we get here then we have some new error codes so we
  378. # need to rebuild the header file and C file.
  379. # Make a sorted list of error and reason codes for later use.
  380. my @function = sort grep( /^${lib}_/, keys %fcodes );
  381. my @reasons = sort grep( /^${lib}_/, keys %rcodes );
  382. # indent level for innermost preprocessor lines
  383. my $indent = " ";
  384. # Rewrite the header file
  385. my $hfile = $hinc{$lib};
  386. $hfile =~ s/.h$/err.h/ if $internal;
  387. open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
  388. print OUT <<"EOF";
  389. /*
  390. * Generated by util/mkerr.pl DO NOT EDIT
  391. * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  392. *
  393. * Licensed under the OpenSSL license (the \"License\"). You may not use
  394. * this file except in compliance with the License. You can obtain a copy
  395. * in the file LICENSE in the source distribution or at
  396. * https://www.openssl.org/source/license.html
  397. */
  398. #ifndef HEADER_${lib}ERR_H
  399. # define HEADER_${lib}ERR_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) {
  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((f), (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. foreach my $i ( @function ) {
  439. my $z = 48 - length($i);
  440. $z = 0 if $z < 0;
  441. if ( $fcodes{$i} eq "X" ) {
  442. $fassigned{$lib} =~ m/^:([^:]*):/;
  443. my $findcode = $1;
  444. $findcode = $fmax{$lib} if !defined $findcode;
  445. while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
  446. $findcode++;
  447. }
  448. $fcodes{$i} = $findcode;
  449. $fassigned{$lib} .= "$findcode:";
  450. print STDERR "New Function code $i\n" if $debug;
  451. }
  452. printf OUT "#${indent}define $i%s $fcodes{$i}\n", " " x $z;
  453. }
  454. print OUT "\n/*\n * $lib reason codes.\n */\n";
  455. foreach my $i ( @reasons ) {
  456. my $z = 48 - length($i);
  457. $z = 0 if $z < 0;
  458. if ( $rcodes{$i} eq "X" ) {
  459. $rassigned{$lib} =~ m/^:([^:]*):/;
  460. my $findcode = $1;
  461. $findcode = $rmax{$lib} if !defined $findcode;
  462. while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
  463. $findcode++;
  464. }
  465. $rcodes{$i} = $findcode;
  466. $rassigned{$lib} .= "$findcode:";
  467. print STDERR "New Reason code $i\n" if $debug;
  468. }
  469. printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z;
  470. }
  471. print OUT "\n";
  472. while (length($indent) > 0) {
  473. $indent = substr $indent, 0, -1;
  474. print OUT "#${indent}endif\n";
  475. }
  476. # Rewrite the C source file containing the error details.
  477. # First, read any existing reason string definitions:
  478. my $cfile = $errorfile{$lib};
  479. my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
  480. my $hincf = $hfile;
  481. $hincf =~ s|.*include/||;
  482. if ( $hincf =~ m|^openssl/| ) {
  483. $hincf = "<${hincf}>";
  484. } else {
  485. $hincf = "\"${hincf}\"";
  486. }
  487. open( OUT, ">$cfile" )
  488. || die "Can't open $cfile for writing, $!, stopped";
  489. my $const = $internal ? 'const ' : '';
  490. print OUT <<"EOF";
  491. /*
  492. * Generated by util/mkerr.pl DO NOT EDIT
  493. * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  494. *
  495. * Licensed under the OpenSSL license (the "License"). You may not use
  496. * this file except in compliance with the License. You can obtain a copy
  497. * in the file LICENSE in the source distribution or at
  498. * https://www.openssl.org/source/license.html
  499. */
  500. #include <openssl/err.h>
  501. #include $hincf
  502. #ifndef OPENSSL_NO_ERR
  503. static ${const}ERR_STRING_DATA ${lib}_str_functs[] = {
  504. EOF
  505. # Add each function code: if a function name is found then use it.
  506. foreach my $i ( @function ) {
  507. my $fn;
  508. if ( exists $strings{$i} and $strings{$i} ne '' ) {
  509. $fn = $strings{$i};
  510. $fn = "" if $fn eq '*';
  511. } else {
  512. $i =~ /^${lib}_F_(\S+)$/;
  513. $fn = $1;
  514. $fn = $ftrans{$fn} if exists $ftrans{$fn};
  515. $strings{$i} = $fn;
  516. }
  517. my $short = " {ERR_PACK($pack_lib, $i, 0), \"$fn\"},";
  518. if ( length($short) <= 80 ) {
  519. print OUT "$short\n";
  520. } else {
  521. print OUT " {ERR_PACK($pack_lib, $i, 0),\n \"$fn\"},\n";
  522. }
  523. }
  524. print OUT <<"EOF";
  525. {0, NULL}
  526. };
  527. static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
  528. EOF
  529. # Add each reason code.
  530. foreach my $i ( @reasons ) {
  531. my $rn;
  532. if ( exists $strings{$i} ) {
  533. $rn = $strings{$i};
  534. $rn = "" if $rn eq '*';
  535. } else {
  536. $i =~ /^${lib}_R_(\S+)$/;
  537. $rn = $1;
  538. $rn =~ tr/_[A-Z]/ [a-z]/;
  539. $strings{$i} = $rn;
  540. }
  541. my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
  542. if ( length($short) <= 80 ) {
  543. print OUT "$short\n";
  544. } else {
  545. print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n";
  546. }
  547. }
  548. print OUT <<"EOF";
  549. {0, NULL}
  550. };
  551. #endif
  552. EOF
  553. if ( $internal ) {
  554. print OUT <<"EOF";
  555. int ERR_load_${lib}_strings(void)
  556. {
  557. #ifndef OPENSSL_NO_ERR
  558. if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) {
  559. ERR_load_strings_const(${lib}_str_functs);
  560. ERR_load_strings_const(${lib}_str_reasons);
  561. }
  562. #endif
  563. return 1;
  564. }
  565. EOF
  566. } else {
  567. my $st = $static ? "static " : "";
  568. print OUT <<"EOF";
  569. static int lib_code = 0;
  570. static int error_loaded = 0;
  571. ${st}int ERR_load_${lib}_strings(void)
  572. {
  573. if (lib_code == 0)
  574. lib_code = ERR_get_next_error_library();
  575. if (!error_loaded) {
  576. #ifndef OPENSSL_NO_ERR
  577. ERR_load_strings(lib_code, ${lib}_str_functs);
  578. ERR_load_strings(lib_code, ${lib}_str_reasons);
  579. #endif
  580. error_loaded = 1;
  581. }
  582. return 1;
  583. }
  584. ${st}void ERR_unload_${lib}_strings(void)
  585. {
  586. if (error_loaded) {
  587. #ifndef OPENSSL_NO_ERR
  588. ERR_unload_strings(lib_code, ${lib}_str_functs);
  589. ERR_unload_strings(lib_code, ${lib}_str_reasons);
  590. #endif
  591. error_loaded = 0;
  592. }
  593. }
  594. ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
  595. {
  596. if (lib_code == 0)
  597. lib_code = ERR_get_next_error_library();
  598. ERR_PUT_error(lib_code, function, reason, file, line);
  599. }
  600. EOF
  601. }
  602. close OUT;
  603. }
  604. &phase("Ending");
  605. # Make a list of unreferenced function and reason codes
  606. if ( $unref ) {
  607. my @funref;
  608. foreach ( keys %fcodes ) {
  609. push( @funref, $_ ) unless exists $usedfuncs{$_};
  610. }
  611. my @runref;
  612. foreach ( keys %rcodes ) {
  613. push( @runref, $_ ) unless exists $usedreasons{$_};
  614. }
  615. if ( @funref ) {
  616. print STDERR "The following function codes were not referenced:\n";
  617. foreach ( sort @funref ) {
  618. print STDERR " $_\n";
  619. }
  620. }
  621. if ( @runref ) {
  622. print STDERR "The following reason codes were not referenced:\n";
  623. foreach ( sort @runref ) {
  624. print STDERR " $_\n";
  625. }
  626. }
  627. }
  628. die "Found $errors errors, quitting" if $errors;
  629. # Update the state file
  630. if ( $newstate ) {
  631. open(OUT, ">$statefile.new")
  632. || die "Can't write $statefile.new, $!";
  633. print OUT <<"EOF";
  634. # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  635. #
  636. # Licensed under the OpenSSL license (the "License"). You may not use
  637. # this file except in compliance with the License. You can obtain a copy
  638. # in the file LICENSE in the source distribution or at
  639. # https://www.openssl.org/source/license.html
  640. EOF
  641. print OUT "\n# Function codes\n";
  642. foreach my $i ( sort keys %fcodes ) {
  643. my $short = "$i:$fcodes{$i}:";
  644. my $t = exists $strings{$i} ? $strings{$i} : "";
  645. $t = "\\\n\t" . $t if length($short) + length($t) > 80;
  646. print OUT "$short$t\n";
  647. }
  648. print OUT "\n#Reason codes\n";
  649. foreach my $i ( sort keys %rcodes ) {
  650. my $short = "$i:$rcodes{$i}:";
  651. my $t = exists $strings{$i} ? "$strings{$i}" : "";
  652. $t = "\\\n\t" . $t if length($short) + length($t) > 80;
  653. print OUT "$short$t\n" if !exists $rextra{$i};
  654. }
  655. close(OUT);
  656. if ( $skippedstate ) {
  657. print "Skipped state, leaving update in $statefile.new";
  658. } else {
  659. rename "$statefile", "$statefile.old"
  660. || die "Can't backup $statefile to $statefile.old, $!";
  661. rename "$statefile.new", "$statefile"
  662. || die "Can't rename $statefile to $statefile.new, $!";
  663. }
  664. }
  665. exit;