]>
git.ipfire.org Git - thirdparty/openssl.git/blob - util/mkerr.pl
2 # Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved.
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
13 use File
::Spec
::Functions
qw(abs2rel rel2abs);
18 my $config = "crypto/err/openssl.ec";
30 my $YEAR = $t[5] + 1900;
35 print STDERR
"\n---\n$text\n" if $debug;
41 mkerr.pl [options] [files...]
45 -conf FILE Use the named config file FILE instead of the default.
47 -debug Verbose output debugging on stderr.
49 -internal Generate code that is to be built as part of OpenSSL itself.
50 Also scans internal list of files.
52 -module M Only useful with -internal!
53 Only write files for library module M. Whether files are
54 actually written or not depends on other options, such as
56 Note: this option is cumulative. If not given at all, all
57 internal modules will be considered.
59 -nowrite Do not write the header/source files, even if changed.
61 -rebuild Rebuild all header and C source files, even if there
64 -reindex Ignore previously assigned values (except for R records in
65 the config file) and renumber everything starting at 100.
67 -static Make the load/unload functions static.
69 -unref List all unreferenced function and reason codes on stderr;
72 -help Show this help text.
74 ... Additional arguments are added to the file list to scan,
75 if '-internal' was NOT specified on the command line.
82 last unless $arg =~ /-.*/;
83 $arg = $1 if $arg =~ /-(-.*)/;
84 if ( $arg eq "-conf" ) {
87 } elsif ( $arg eq "-debug" ) {
90 } elsif ( $arg eq "-internal" ) {
92 } elsif ( $arg eq "-nowrite" ) {
94 } elsif ( $arg eq "-rebuild" ) {
96 } elsif ( $arg eq "-reindex" ) {
98 } elsif ( $arg eq "-static" ) {
100 } elsif ( $arg eq "-unref" ) {
103 } elsif ( $arg eq "-module" ) {
105 $modules{uc $ARGV[0]} = 1;
106 } elsif ( $arg =~ /-*h(elp)?/ ) {
109 } elsif ( $arg =~ /-.*/ ) {
110 die "Unknown option $arg; use -h for help.\n";
117 die "Cannot mix -internal and -static\n" if $static;
118 die "Extra parameters given.\n" if @ARGV;
119 @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
120 glob('ssl/*.c'), glob('ssl/*/*.c'), glob('providers/*.c'),
121 glob('providers/*/*.c'), glob('providers/*/*/*.c') );
123 die "-module isn't useful without -internal\n" if scalar keys %modules > 0;
127 # Data parsed out of the config and state files.
128 my %hpubinc; # lib -> public header
129 my %libpubinc; # public header -> lib
130 my %hprivinc; # lib -> private header
131 my %libprivinc; # private header -> lib
132 my %cskip; # error_file -> lib
133 my %errorfile; # lib -> error file name
134 my %rmax; # lib -> max assigned reason code
135 my %rassigned; # lib -> colon-separated list of assigned reason codes
136 my %rnew; # lib -> count of new reason codes
137 my %rextra; # "extra" reason code -> lib
138 my %rcodes; # reason-name -> value
139 my $statefile; # state file with assigned reason and function codes
140 my %strings; # define -> text
142 # Read and parse the config file
143 open(IN
, "$config") || die "Can't open config file $config, $!,";
145 next if /^#/ || /^$/;
146 if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)(?:\s+(\S+))?\s+$/ ) {
150 my $privhdr = $4 // 'NONE';
151 $hpubinc{$lib} = $pubhdr;
152 $libpubinc{$pubhdr} = $lib;
153 $hprivinc{$lib} = $privhdr;
154 $libprivinc{$privhdr} = $lib;
156 $errorfile{$lib} = $err;
157 next if $err eq 'NONE';
159 $rassigned{$lib} = ":";
161 die "Public header file must be in include/openssl ($pubhdr is not)\n"
164 && $pubhdr !~ m
|^include
/openssl/|);
165 die "Private header file may only be specified with -internal ($privhdr given)\n"
166 unless ($privhdr eq 'NONE' || $internal);
167 } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
170 } elsif ( /^S\s+(\S+)/ ) {
173 die "Illegal config line $_\n";
178 if ( ! $statefile ) {
179 $statefile = $config;
180 $statefile =~ s/.ec/.txt/;
183 # The statefile has all the previous assignments.
184 &phase
("Reading state");
185 my $skippedstate = 0;
186 if ( ! $reindex && $statefile ) {
187 open(STATE
, "<$statefile") || die "Can't open $statefile, $!";
189 # Scan function and reason codes and store them: keep a note of the
192 next if /^#/ || /^$/;
195 if ( /^(.+):(\d+):\\$/ ) {
199 $next =~ s/^\s*(.*)\s*$/$1/;
200 die "Duplicate define $name" if exists $strings{$name};
201 $strings{$name} = $next;
202 } elsif ( /^(\S+):(\d+):(.*)$/ ) {
205 die "Duplicate define $name" if exists $strings{$name};
206 $strings{$name} = $3;
208 die "Bad line in $statefile:\n$_\n";
211 $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/;
212 $lib = "SSL" if $lib =~ /TLS/;
213 if ( !defined $errorfile{$lib} ) {
218 next if $errorfile{$lib} eq 'NONE';
219 if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) {
220 die "$lib reason code $code collision at $name\n"
221 if $rassigned{$lib} =~ /:$code:/;
222 $rassigned{$lib} .= "$code:";
223 if ( !exists $rextra{$name} ) {
224 $rmax{$lib} = $code if $code > $rmax{$lib};
226 $rcodes{$name} = $code;
227 } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) {
228 # We do nothing with the function codes, just let them go away
230 die "Bad line in $statefile:\n$_\n";
236 foreach my $lib ( sort keys %rmax ) {
237 print STDERR
"Reason codes for ${lib}:\n";
238 if ( $rassigned{$lib} =~ m/^:(.*):$/ ) {
239 my @rassigned = sort { $a <=> $b } split( ":", $1 );
240 print STDERR
" ", join(' ', @rassigned), "\n";
242 print STDERR
" --none--\n";
248 # Scan each C source file and look for reason codes. This is done by
249 # looking for strings that "look like" reason codes: basically anything
250 # consisting of all upper case and numerics which _R_ in it and which has
251 # the name of an error library at the start. Should there be anything else,
252 # such as a type name, we add exceptions here.
253 # If a code doesn't exist in list compiled from headers then mark it
254 # with the value "X" as a place holder to give it a value later.
255 # Store all reason codes found in and %usedreasons so all those unreferenced
256 # can be printed out.
257 &phase
("Scanning source");
259 foreach my $file ( @source ) {
260 # Don't parse the error source file.
261 next if exists $cskip{$file};
262 open( IN
, "<$file" ) || die "Can't open $file, $!,";
265 print STDERR
"$file:\n" if $debug;
268 # skip obsoleted source files entirely!
269 last if /^#error\s+obsolete/;
272 if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) {
273 next unless exists $errorfile{$2};
274 next if $errorfile{$2} eq 'NONE';
275 $usedreasons{$1} = 1;
276 if ( !exists $rcodes{$1} ) {
277 print STDERR
" New reason $1\n" if $debug;
281 print STDERR
" Reason $1 = $rcodes{$1}\n" if $debug;
286 print STDERR
"\n" if $debug;
288 # Now process each library in turn.
289 &phase
("Writing files");
291 foreach my $lib ( keys %errorfile ) {
292 next if ! $rnew{$lib} && ! $rebuild;
293 next if scalar keys %modules > 0 && !$modules{$lib};
295 print STDERR
"$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
298 # If we get here then we have some new error codes so we
299 # need to rebuild the header file and C file.
301 # Make a sorted list of error and reason codes for later use.
302 my @reasons = sort grep( /^${lib}_/, keys %rcodes );
304 # indent level for innermost preprocessor lines
307 # Flag if the sub-library is disablable
308 # There are a few exceptions, where disabling the sub-library
309 # doesn't actually remove the whole sub-library, but rather implements
310 # it with a NULL backend.
312 ($lib ne "SSL" && $lib ne "ASYNC" && $lib ne "DSO"
313 && (grep { $lib eq uc $_ } @disablables, @disablables_int));
315 # Rewrite the internal header file if there is one ($internal only!)
317 if ($hprivinc{$lib} ne 'NONE') {
318 my $hfile = $hprivinc{$lib};
321 if ($guard =~ m
|^include
/|) {
324 $guard = basename($guard);
326 $guard = "OSSL_" . join('_
', split(m|[./]|, uc $guard));
328 open( OUT, ">$hfile" ) || die "Can't
write to
$hfile, $!,";
331 * Generated by util/mkerr.pl DO NOT EDIT
332 * Copyright 2020-$YEAR The OpenSSL Project Authors. All Rights Reserved.
334 * Licensed under the Apache License 2.0 (the \"License\"). You may not use
335 * this file except in compliance with the License. You can obtain a copy
336 * in the file LICENSE in the source distribution or at
337 * https://www.openssl.org/source/license.html
344 # include <openssl/opensslconf.h>
345 # include <openssl/symhacks.h>
355 # ifndef OPENSSL_NO_${lib}
361 int ossl_err_load_${lib}_strings(void);
364 # If this library doesn't have a public header file, we write all
365 # definitions that would end up there here instead
366 if ($hpubinc{$lib} eq 'NONE') {
367 print OUT
"\n/*\n * $lib reason codes.\n */\n";
368 foreach my $i ( @reasons ) {
369 my $z = 48 - length($i);
371 if ( $rcodes{$i} eq "X" ) {
372 $rassigned{$lib} =~ m/^:([^:]*):/;
374 $findcode = $rmax{$lib} if !defined $findcode;
375 while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
378 $rcodes{$i} = $findcode;
379 $rassigned{$lib} .= "$findcode:";
380 print STDERR
"New Reason code $i\n" if $debug;
382 printf OUT
"#${indent}define $i%s $rcodes{$i}\n", " " x
$z;
387 # This doesn't go all the way down to zero, to allow for the ending
388 # brace for 'extern "C" {'.
389 while (length($indent) > 1) {
390 $indent = substr $indent, 0, -1;
391 print OUT
"#${indent}endif\n";
404 # Rewrite the public header file
406 if ($hpubinc{$lib} ne 'NONE') {
410 ?
"# include <openssl/cryptoerr_legacy.h>\n"
411 : "# include <openssl/sslerr_legacy.h>\n")
413 my $hfile = $hpubinc{$lib};
415 $guard =~ s
|^include
/||;
416 $guard = join('_', split(m
|[./]|, uc $guard));
417 $guard = "OSSL_" . $guard unless $internal;
419 open( OUT
, ">$hfile" ) || die "Can't write to $hfile, $!,";
422 * Generated by util/mkerr.pl DO NOT EDIT
423 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
425 * Licensed under the Apache License 2.0 (the \"License\"). You may not use
426 * this file except in compliance with the License. You can obtain a copy
427 * in the file LICENSE in the source distribution or at
428 * https://www.openssl.org/source/license.html
435 # include <openssl/opensslconf.h>
436 # include <openssl/symhacks.h>
444 # ifndef OPENSSL_NO_${lib}
451 # define ${lib}err(f, r) ERR_${lib}_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
460 int ERR_load_${lib}_strings(void);
461 void ERR_unload_${lib}_strings(void);
462 void ERR_${lib}_error(int function, int reason, char *file, int line);
470 print OUT
"\n/*\n * $lib reason codes.\n */\n";
471 foreach my $i ( @reasons ) {
472 my $z = 48 - length($i);
474 if ( $rcodes{$i} eq "X" ) {
475 $rassigned{$lib} =~ m/^:([^:]*):/;
477 $findcode = $rmax{$lib} if !defined $findcode;
478 while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
481 $rcodes{$i} = $findcode;
482 $rassigned{$lib} .= "$findcode:";
483 print STDERR
"New Reason code $i\n" if $debug;
485 printf OUT
"#${indent}define $i%s $rcodes{$i}\n", " " x
$z;
489 while (length($indent) > 0) {
490 $indent = substr $indent, 0, -1;
491 print OUT
"#${indent}endif\n";
496 # Rewrite the C source file containing the error details.
498 if ($errorfile{$lib} ne 'NONE') {
499 # First, read any existing reason string definitions:
500 my $cfile = $errorfile{$lib};
501 my $pack_lib = $internal ?
"ERR_LIB_${lib}" : "0";
502 my $hpubincf = $hpubinc{$lib};
503 my $hprivincf = $hprivinc{$lib};
506 if ($hpubincf ne 'NONE') {
507 $hpubincf =~ s
|^include
/||;
508 $includes .= "#include <${hpubincf}>\n";
510 if ($hprivincf =~ m
|^include
/|) {
513 $hprivincf = abs2rel(rel2abs($hprivincf),
514 rel2abs(dirname($cfile)));
516 $includes .= "#include \"${hprivincf}\"\n";
518 $includes .= "#include \"${hpubincf}\"\n";
521 open( OUT, ">$cfile" )
522 || die "Can't
open $cfile for writing
, $!, stopped
";
524 my $const = $internal ? 'const ' : '';
528 * Generated by util/mkerr.pl DO NOT EDIT
529 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
531 * Licensed under the Apache License 2.0 (the "License
"). You may not use
532 * this file except in compliance with the License. You can obtain a copy
533 * in the file LICENSE in the source distribution or at
534 * https://www.openssl.org/source/license.html
537 #include <openssl/err.h>
544 #ifndef OPENSSL_NO_${lib}
551 #${indent}ifndef OPENSSL_NO_ERR
553 static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
556 # Add each reason code.
557 foreach my $i ( @reasons ) {
559 if ( exists $strings{$i} ) {
561 $rn = "" if $rn eq '*';
563 $i =~ /^${lib}_R_(\S+)$/;
565 $rn =~ tr/_[A-Z]/ [a-z]/;
568 my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
569 if ( length($short) <= 80 ) {
570 print OUT
"$short\n";
572 print OUT
" {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n";
584 int ossl_err_load_${lib}_strings(void)
586 #${indent}ifndef OPENSSL_NO_ERR
587 if (ERR_reason_error_string(${lib}_str_reasons[0].error) == NULL)
588 ERR_load_strings_const(${lib}_str_reasons);
594 my $st = $static ?
"static " : "";
597 static int lib_code = 0;
598 static int error_loaded = 0;
600 ${st}int ERR_load_${lib}_strings(void)
603 lib_code = ERR_get_next_error_library();
606 #ifndef OPENSSL_NO_ERR
607 ERR_load_strings(lib_code, ${lib}_str_reasons);
614 ${st}void ERR_unload_${lib}_strings(void)
617 #ifndef OPENSSL_NO_ERR
618 ERR_unload_strings(lib_code, ${lib}_str_reasons);
624 ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
627 lib_code = ERR_get_next_error_library();
628 ERR_raise(lib_code, reason);
629 ERR_set_debug(file, line, NULL);
635 while (length($indent) > 1) {
636 $indent = substr $indent, 0, -1;
637 print OUT
"#${indent}endif\n";
639 if ($internal && $disablable) {
642 NON_EMPTY_TRANSLATION_UNIT
651 # Make a list of unreferenced reason codes
654 foreach ( keys %rcodes ) {
655 push( @runref, $_ ) unless exists $usedreasons{$_};
658 print STDERR
"The following reason codes were not referenced:\n";
659 foreach ( sort @runref ) {
660 print STDERR
" $_\n";
665 die "Found $errors errors, quitting" if $errors;
667 # Update the state file
669 open(OUT
, ">$statefile.new")
670 || die "Can't write $statefile.new, $!";
672 # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved.
674 # Licensed under the Apache License 2.0 (the "License"). You may not use
675 # this file except in compliance with the License. You can obtain a copy
676 # in the file LICENSE in the source distribution or at
677 # https://www.openssl.org/source/license.html
679 print OUT
"\n#Reason codes\n";
680 foreach my $i ( sort keys %rcodes ) {
681 my $short = "$i:$rcodes{$i}:";
682 my $t = exists $strings{$i} ?
"$strings{$i}" : "";
683 $t = "\\\n\t" . $t if length($short) + length($t) > 80;
684 print OUT
"$short$t\n" if !exists $rextra{$i};
687 if ( $skippedstate ) {
688 print "Skipped state, leaving update in $statefile.new";
690 rename "$statefile", "$statefile.old"
691 || die "Can't backup $statefile to $statefile.old, $!";
692 rename "$statefile.new", "$statefile"
693 || die "Can't rename $statefile to $statefile.new, $!";