mkerr.pl 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. #! /usr/bin/env perl
  2. # Copyright 1999-2018 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. my %hinc; # lib -> header
  104. my %libinc; # header -> lib
  105. my %cskip; # error_file -> lib
  106. my %errorfile; # lib -> error file name
  107. my %fmax; # lib -> max assigned function code
  108. my %rmax; # lib -> max assigned reason code
  109. my %fassigned; # lib -> colon-separated list of assigned function codes
  110. my %rassigned; # lib -> colon-separated list of assigned reason codes
  111. my %fnew; # lib -> count of new function codes
  112. my %rnew; # lib -> count of new reason codes
  113. my %rextra; # "extra" reason code -> lib
  114. my %rcodes; # reason-name -> value
  115. my %ftrans; # old name -> #define-friendly name (all caps)
  116. my %fcodes; # function-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+)/ ) {
  124. my $lib = $1;
  125. my $hdr = $2;
  126. my $err = $3;
  127. $hinc{$lib} = $hdr;
  128. $libinc{$hdr} = $lib;
  129. $cskip{$err} = $lib;
  130. next if $err eq 'NONE';
  131. $errorfile{$lib} = $err;
  132. $fmax{$lib} = 100;
  133. $rmax{$lib} = 100;
  134. $fassigned{$lib} = ":";
  135. $rassigned{$lib} = ":";
  136. $fnew{$lib} = 0;
  137. $rnew{$lib} = 0;
  138. } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
  139. $rextra{$1} = $2;
  140. $rcodes{$1} = $2;
  141. } elsif ( /^S\s+(\S+)/ ) {
  142. $statefile = $1;
  143. } else {
  144. die "Illegal config line $_\n";
  145. }
  146. }
  147. close IN;
  148. if ( ! $statefile ) {
  149. $statefile = $config;
  150. $statefile =~ s/.ec/.txt/;
  151. }
  152. # The statefile has all the previous assignments.
  153. &phase("Reading state");
  154. my $skippedstate = 0;
  155. if ( ! $reindex && $statefile ) {
  156. open(STATE, "<$statefile") || die "Can't open $statefile, $!";
  157. # Scan function and reason codes and store them: keep a note of the
  158. # maximum code used.
  159. while ( <STATE> ) {
  160. next if /^#/ || /^$/;
  161. my $name;
  162. my $code;
  163. if ( /^(.+):(\d+):\\$/ ) {
  164. $name = $1;
  165. $code = $2;
  166. my $next = <STATE>;
  167. $next =~ s/^\s*(.*)\s*$/$1/;
  168. die "Duplicate define $name" if exists $strings{$name};
  169. $strings{$name} = $next;
  170. } elsif ( /^(\S+):(\d+):(.*)$/ ) {
  171. $name = $1;
  172. $code = $2;
  173. die "Duplicate define $name" if exists $strings{$name};
  174. $strings{$name} = $3;
  175. } else {
  176. die "Bad line in $statefile:\n$_\n";
  177. }
  178. my $lib = $name;
  179. $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/;
  180. $lib = "SSL" if $lib =~ /TLS/;
  181. if ( !defined $errorfile{$lib} ) {
  182. print "Skipping $_";
  183. $skippedstate++;
  184. next;
  185. }
  186. if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) {
  187. die "$lib reason code $code collision at $name\n"
  188. if $rassigned{$lib} =~ /:$code:/;
  189. $rassigned{$lib} .= "$code:";
  190. if ( !exists $rextra{$name} ) {
  191. $rmax{$lib} = $code if $code > $rmax{$lib};
  192. }
  193. $rcodes{$name} = $code;
  194. } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) {
  195. die "$lib function code $code collision at $name\n"
  196. if $fassigned{$lib} =~ /:$code:/;
  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/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/;
  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. if ( uc($func) ne $3 ) {
  348. print STDERR "ERROR: mismatch $file:$linenr $func:$3\n";
  349. $errors++;
  350. }
  351. print STDERR " Function $1 = $fcodes{$1}\n"
  352. if $debug;
  353. }
  354. if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) {
  355. next unless exists $errorfile{$2};
  356. $usedreasons{$1} = 1;
  357. if ( !exists $rcodes{$1} ) {
  358. print STDERR " New reason $1\n" if $debug;
  359. $rcodes{$1} = "X";
  360. $rnew{$2}++;
  361. }
  362. print STDERR " Reason $1 = $rcodes{$1}\n" if $debug;
  363. }
  364. }
  365. close IN;
  366. }
  367. print STDERR "\n" if $debug;
  368. # Now process each library in turn.
  369. &phase("Writing files");
  370. my $newstate = 0;
  371. foreach my $lib ( keys %errorfile ) {
  372. next if ! $fnew{$lib} && ! $rnew{$lib} && ! $rebuild;
  373. next if scalar keys %modules > 0 && !$modules{$lib};
  374. next if $nowrite;
  375. print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
  376. print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
  377. $newstate = 1;
  378. # If we get here then we have some new error codes so we
  379. # need to rebuild the header file and C file.
  380. # Make a sorted list of error and reason codes for later use.
  381. my @function = sort grep( /^${lib}_/, keys %fcodes );
  382. my @reasons = sort grep( /^${lib}_/, keys %rcodes );
  383. # indent level for innermost preprocessor lines
  384. my $indent = " ";
  385. # Rewrite the header file
  386. my $hfile = $hinc{$lib};
  387. $hfile =~ s/.h$/err.h/ if $internal;
  388. open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
  389. print OUT <<"EOF";
  390. /*
  391. * Generated by util/mkerr.pl DO NOT EDIT
  392. * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  393. *
  394. * Licensed under the Apache License 2.0 (the \"License\"). You may not use
  395. * this file except in compliance with the License. You can obtain a copy
  396. * in the file LICENSE in the source distribution or at
  397. * https://www.openssl.org/source/license.html
  398. */
  399. #ifndef HEADER_${lib}ERR_H
  400. # define HEADER_${lib}ERR_H
  401. # ifndef HEADER_SYMHACKS_H
  402. # include <openssl/symhacks.h>
  403. # endif
  404. EOF
  405. if ( $internal ) {
  406. # Declare the load function because the generate C file
  407. # includes "fooerr.h" not "foo.h"
  408. if ($lib ne "SSL" && $lib ne "ASYNC"
  409. && (grep { $lib eq uc $_ } @disablables, @disablables_int)) {
  410. print OUT <<"EOF";
  411. # include <openssl/opensslconf.h>
  412. # ifndef OPENSSL_NO_${lib}
  413. EOF
  414. $indent = " ";
  415. }
  416. print OUT <<"EOF";
  417. #${indent}ifdef __cplusplus
  418. extern \"C\"
  419. #${indent}endif
  420. int ERR_load_${lib}_strings(void);
  421. EOF
  422. } else {
  423. print OUT <<"EOF";
  424. # define ${lib}err(f, r) ERR_${lib}_error((f), (r), OPENSSL_FILE, OPENSSL_LINE)
  425. EOF
  426. if ( ! $static ) {
  427. print OUT <<"EOF";
  428. # ifdef __cplusplus
  429. extern \"C\" {
  430. # endif
  431. int ERR_load_${lib}_strings(void);
  432. void ERR_unload_${lib}_strings(void);
  433. void ERR_${lib}_error(int function, int reason, char *file, int line);
  434. # ifdef __cplusplus
  435. }
  436. # endif
  437. EOF
  438. }
  439. }
  440. print OUT "\n/*\n * $lib function codes.\n */\n";
  441. foreach my $i ( @function ) {
  442. my $z = 48 - length($i);
  443. $z = 0 if $z < 0;
  444. if ( $fcodes{$i} eq "X" ) {
  445. $fassigned{$lib} =~ m/^:([^:]*):/;
  446. my $findcode = $1;
  447. $findcode = $fmax{$lib} if !defined $findcode;
  448. while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
  449. $findcode++;
  450. }
  451. $fcodes{$i} = $findcode;
  452. $fassigned{$lib} .= "$findcode:";
  453. print STDERR "New Function code $i\n" if $debug;
  454. }
  455. printf OUT "#${indent}define $i%s $fcodes{$i}\n", " " x $z;
  456. }
  457. print OUT "\n/*\n * $lib reason codes.\n */\n";
  458. foreach my $i ( @reasons ) {
  459. my $z = 48 - length($i);
  460. $z = 0 if $z < 0;
  461. if ( $rcodes{$i} eq "X" ) {
  462. $rassigned{$lib} =~ m/^:([^:]*):/;
  463. my $findcode = $1;
  464. $findcode = $rmax{$lib} if !defined $findcode;
  465. while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
  466. $findcode++;
  467. }
  468. $rcodes{$i} = $findcode;
  469. $rassigned{$lib} .= "$findcode:";
  470. print STDERR "New Reason code $i\n" if $debug;
  471. }
  472. printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z;
  473. }
  474. print OUT "\n";
  475. while (length($indent) > 0) {
  476. $indent = substr $indent, 0, -1;
  477. print OUT "#${indent}endif\n";
  478. }
  479. # Rewrite the C source file containing the error details.
  480. # First, read any existing reason string definitions:
  481. my $cfile = $errorfile{$lib};
  482. my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
  483. my $hincf = $hfile;
  484. $hincf =~ s|.*include/||;
  485. if ( $hincf =~ m|^openssl/| ) {
  486. $hincf = "<${hincf}>";
  487. } else {
  488. $hincf = "\"${hincf}\"";
  489. }
  490. open( OUT, ">$cfile" )
  491. || die "Can't open $cfile for writing, $!, stopped";
  492. my $const = $internal ? 'const ' : '';
  493. print OUT <<"EOF";
  494. /*
  495. * Generated by util/mkerr.pl DO NOT EDIT
  496. * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  497. *
  498. * Licensed under the Apache License 2.0 (the "License"). You may not use
  499. * this file except in compliance with the License. You can obtain a copy
  500. * in the file LICENSE in the source distribution or at
  501. * https://www.openssl.org/source/license.html
  502. */
  503. #include <openssl/err.h>
  504. #include $hincf
  505. #ifndef OPENSSL_NO_ERR
  506. static ${const}ERR_STRING_DATA ${lib}_str_functs[] = {
  507. EOF
  508. # Add each function code: if a function name is found then use it.
  509. foreach my $i ( @function ) {
  510. my $fn;
  511. if ( exists $strings{$i} and $strings{$i} ne '' ) {
  512. $fn = $strings{$i};
  513. $fn = "" if $fn eq '*';
  514. } else {
  515. $i =~ /^${lib}_F_(\S+)$/;
  516. $fn = $1;
  517. $fn = $ftrans{$fn} if exists $ftrans{$fn};
  518. $strings{$i} = $fn;
  519. }
  520. my $short = " {ERR_PACK($pack_lib, $i, 0), \"$fn\"},";
  521. if ( length($short) <= 80 ) {
  522. print OUT "$short\n";
  523. } else {
  524. print OUT " {ERR_PACK($pack_lib, $i, 0),\n \"$fn\"},\n";
  525. }
  526. }
  527. print OUT <<"EOF";
  528. {0, NULL}
  529. };
  530. static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
  531. EOF
  532. # Add each reason code.
  533. foreach my $i ( @reasons ) {
  534. my $rn;
  535. if ( exists $strings{$i} ) {
  536. $rn = $strings{$i};
  537. $rn = "" if $rn eq '*';
  538. } else {
  539. $i =~ /^${lib}_R_(\S+)$/;
  540. $rn = $1;
  541. $rn =~ tr/_[A-Z]/ [a-z]/;
  542. $strings{$i} = $rn;
  543. }
  544. my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
  545. if ( length($short) <= 80 ) {
  546. print OUT "$short\n";
  547. } else {
  548. print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n";
  549. }
  550. }
  551. print OUT <<"EOF";
  552. {0, NULL}
  553. };
  554. #endif
  555. EOF
  556. if ( $internal ) {
  557. print OUT <<"EOF";
  558. int ERR_load_${lib}_strings(void)
  559. {
  560. #ifndef OPENSSL_NO_ERR
  561. if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) {
  562. ERR_load_strings_const(${lib}_str_functs);
  563. ERR_load_strings_const(${lib}_str_reasons);
  564. }
  565. #endif
  566. return 1;
  567. }
  568. EOF
  569. } else {
  570. my $st = $static ? "static " : "";
  571. print OUT <<"EOF";
  572. static int lib_code = 0;
  573. static int error_loaded = 0;
  574. ${st}int ERR_load_${lib}_strings(void)
  575. {
  576. if (lib_code == 0)
  577. lib_code = ERR_get_next_error_library();
  578. if (!error_loaded) {
  579. #ifndef OPENSSL_NO_ERR
  580. ERR_load_strings(lib_code, ${lib}_str_functs);
  581. ERR_load_strings(lib_code, ${lib}_str_reasons);
  582. #endif
  583. error_loaded = 1;
  584. }
  585. return 1;
  586. }
  587. ${st}void ERR_unload_${lib}_strings(void)
  588. {
  589. if (error_loaded) {
  590. #ifndef OPENSSL_NO_ERR
  591. ERR_unload_strings(lib_code, ${lib}_str_functs);
  592. ERR_unload_strings(lib_code, ${lib}_str_reasons);
  593. #endif
  594. error_loaded = 0;
  595. }
  596. }
  597. ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
  598. {
  599. if (lib_code == 0)
  600. lib_code = ERR_get_next_error_library();
  601. ERR_PUT_error(lib_code, function, reason, file, line);
  602. }
  603. EOF
  604. }
  605. close OUT;
  606. }
  607. &phase("Ending");
  608. # Make a list of unreferenced function and reason codes
  609. if ( $unref ) {
  610. my @funref;
  611. foreach ( keys %fcodes ) {
  612. push( @funref, $_ ) unless exists $usedfuncs{$_};
  613. }
  614. my @runref;
  615. foreach ( keys %rcodes ) {
  616. push( @runref, $_ ) unless exists $usedreasons{$_};
  617. }
  618. if ( @funref ) {
  619. print STDERR "The following function codes were not referenced:\n";
  620. foreach ( sort @funref ) {
  621. print STDERR " $_\n";
  622. }
  623. }
  624. if ( @runref ) {
  625. print STDERR "The following reason codes were not referenced:\n";
  626. foreach ( sort @runref ) {
  627. print STDERR " $_\n";
  628. }
  629. }
  630. }
  631. die "Found $errors errors, quitting" if $errors;
  632. # Update the state file
  633. if ( $newstate ) {
  634. open(OUT, ">$statefile.new")
  635. || die "Can't write $statefile.new, $!";
  636. print OUT <<"EOF";
  637. # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  638. #
  639. # Licensed under the Apache License 2.0 (the "License"). You may not use
  640. # this file except in compliance with the License. You can obtain a copy
  641. # in the file LICENSE in the source distribution or at
  642. # https://www.openssl.org/source/license.html
  643. EOF
  644. print OUT "\n# Function codes\n";
  645. foreach my $i ( sort keys %fcodes ) {
  646. my $short = "$i:$fcodes{$i}:";
  647. my $t = exists $strings{$i} ? $strings{$i} : "";
  648. $t = "\\\n\t" . $t if length($short) + length($t) > 80;
  649. print OUT "$short$t\n";
  650. }
  651. print OUT "\n#Reason codes\n";
  652. foreach my $i ( sort keys %rcodes ) {
  653. my $short = "$i:$rcodes{$i}:";
  654. my $t = exists $strings{$i} ? "$strings{$i}" : "";
  655. $t = "\\\n\t" . $t if length($short) + length($t) > 80;
  656. print OUT "$short$t\n" if !exists $rextra{$i};
  657. }
  658. close(OUT);
  659. if ( $skippedstate ) {
  660. print "Skipped state, leaving update in $statefile.new";
  661. } else {
  662. rename "$statefile", "$statefile.old"
  663. || die "Can't backup $statefile to $statefile.old, $!";
  664. rename "$statefile.new", "$statefile"
  665. || die "Can't rename $statefile to $statefile.new, $!";
  666. }
  667. }
  668. exit;