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