Re-enable testing of ciphersuites
[openssl.git] / util / mkerr.pl
1 #! /usr/bin/env perl
2 # Copyright 1999-2020 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
9 use strict;
10 use warnings;
11
12 use File::Basename;
13 use File::Spec::Functions qw(abs2rel rel2abs);
14
15 use lib ".";
16 use configdata;
17
18 my $config       = "crypto/err/openssl.ec";
19 my $debug        = 0;
20 my $internal     = 0;
21 my $nowrite      = 0;
22 my $rebuild      = 0;
23 my $reindex      = 0;
24 my $static       = 0;
25 my $unref        = 0;
26 my %modules         = ();
27
28 my $errors       = 0;
29 my @t            = localtime();
30 my $YEAR         = $t[5] + 1900;
31
32 sub phase
33 {
34     my $text = uc(shift);
35     print STDERR "\n---\n$text\n" if $debug;
36 }
37
38 sub help
39 {
40     print STDERR <<"EOF";
41 mkerr.pl [options] [files...]
42
43 Options:
44
45     -conf FILE  Use the named config file FILE instead of the default.
46
47     -debug      Verbose output debugging on stderr.
48
49     -internal   Generate code that is to be built as part of OpenSSL itself.
50                 Also scans internal list of files.
51
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
55                 -rebuild.
56                 Note: this option is cumulative.  If not given at all, all
57                 internal modules will be considered.
58
59     -nowrite    Do not write the header/source files, even if changed.
60
61     -rebuild    Rebuild all header and C source files, even if there
62                 were no changes.
63
64     -reindex    Ignore previously assigned values (except for R records in
65                 the config file) and renumber everything starting at 100.
66
67     -static     Make the load/unload functions static.
68
69     -unref      List all unreferenced function and reason codes on stderr;
70                 implies -nowrite.
71
72     -help       Show this help text.
73
74     ...         Additional arguments are added to the file list to scan,
75                 if '-internal' was NOT specified on the command line.
76
77 EOF
78 }
79
80 while ( @ARGV ) {
81     my $arg = $ARGV[0];
82     last unless $arg =~ /-.*/;
83     $arg = $1 if $arg =~ /-(-.*)/;
84     if ( $arg eq "-conf" ) {
85         $config = $ARGV[1];
86         shift @ARGV;
87     } elsif ( $arg eq "-debug" ) {
88         $debug = 1;
89         $unref = 1;
90     } elsif ( $arg eq "-internal" ) {
91         $internal = 1;
92     } elsif ( $arg eq "-nowrite" ) {
93         $nowrite = 1;
94     } elsif ( $arg eq "-rebuild" ) {
95         $rebuild = 1;
96     } elsif ( $arg eq "-reindex" ) {
97         $reindex = 1;
98     } elsif ( $arg eq "-static" ) {
99         $static = 1;
100     } elsif ( $arg eq "-unref" ) {
101         $unref = 1;
102         $nowrite = 1;
103     } elsif ( $arg eq "-module" ) {
104         shift @ARGV;
105         $modules{uc $ARGV[0]} = 1;
106     } elsif ( $arg =~ /-*h(elp)?/ ) {
107         &help();
108         exit;
109     } elsif ( $arg =~ /-.*/ ) {
110         die "Unknown option $arg; use -h for help.\n";
111     }
112     shift @ARGV;
113 }
114
115 my @source;
116 if ( $internal ) {
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') );
122 } else {
123     die "-module isn't useful without -internal\n" if scalar keys %modules > 0;
124     @source = @ARGV;
125 }
126
127 # Data parsed out of the config and state files.
128 # We always map function-code values to zero, so items marked below with
129 # an asterisk could eventually be removed.  TODO(4.0)
130 my %hpubinc;    # lib -> public header
131 my %libpubinc;  # public header -> lib
132 my %hprivinc;   # lib -> private header
133 my %libprivinc; # private header -> lib
134 my %cskip;      # error_file -> lib
135 my %errorfile;  # lib -> error file name
136 my %fmax;       # lib -> max assigned function code*
137 my %rmax;       # lib -> max assigned reason code
138 my %fassigned;  # lib -> colon-separated list of assigned function codes*
139 my %rassigned;  # lib -> colon-separated list of assigned reason codes
140 my %fnew;       # lib -> count of new function codes*
141 my %rnew;       # lib -> count of new reason codes
142 my %rextra;     # "extra" reason code -> lib
143 my %rcodes;     # reason-name -> value
144 my %ftrans;     # old name -> #define-friendly name (all caps)*
145 my %fcodes;     # function-name -> value*
146 my $statefile;  # state file with assigned reason and function codes
147 my %strings;    # define -> text
148
149 # Read and parse the config file
150 open(IN, "$config") || die "Can't open config file $config, $!,";
151 while ( <IN> ) {
152     next if /^#/ || /^$/;
153     if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)(?:\s+(\S+))?\s+$/ ) {
154         my $lib = $1;
155         my $pubhdr = $2;
156         my $err = $3;
157         my $privhdr = $4 // 'NONE';
158         $hpubinc{$lib}   = $pubhdr;
159         $libpubinc{$pubhdr} = $lib;
160         $hprivinc{$lib}   = $privhdr;
161         $libprivinc{$privhdr} = $lib;
162         $cskip{$err}  = $lib;
163         $errorfile{$lib} = $err;
164         next if $err eq 'NONE';
165         $fmax{$lib}      = 100;
166         $rmax{$lib}      = 100;
167         $fassigned{$lib} = ":";
168         $rassigned{$lib} = ":";
169         $fnew{$lib}      = 0;
170         $rnew{$lib}      = 0;
171         die "Public header file must be in include/openssl ($pubhdr is not)\n"
172             if ($internal
173                 && $pubhdr ne 'NONE'
174                 && $pubhdr !~ m|^include/openssl/|);
175         die "Private header file may only be specified with -internal ($privhdr given)\n"
176             unless ($privhdr eq 'NONE' || $internal);
177     } elsif ( /^R\s+(\S+)\s+(\S+)/ ) {
178         $rextra{$1} = $2;
179         $rcodes{$1} = $2;
180     } elsif ( /^S\s+(\S+)/ ) {
181         $statefile = $1;
182     } else {
183         die "Illegal config line $_\n";
184     }
185 }
186 close IN;
187
188 if ( ! $statefile ) {
189     $statefile = $config;
190     $statefile =~ s/.ec/.txt/;
191 }
192
193 # The statefile has all the previous assignments.
194 &phase("Reading state");
195 my $skippedstate = 0;
196 if ( ! $reindex && $statefile ) {
197     open(STATE, "<$statefile") || die "Can't open $statefile, $!";
198
199     # Scan function and reason codes and store them: keep a note of the
200     # maximum code used.
201     while ( <STATE> ) {
202         next if /^#/ || /^$/;
203         my $name;
204         my $code;
205         if ( /^(.+):(\d+):\\$/ ) {
206             $name = $1;
207             $code = $2;
208             my $next = <STATE>;
209             $next =~ s/^\s*(.*)\s*$/$1/;
210             die "Duplicate define $name" if exists $strings{$name};
211             $strings{$name} = $next;
212         } elsif ( /^(\S+):(\d+):(.*)$/ ) {
213             $name = $1;
214             $code = $2;
215             die "Duplicate define $name" if exists $strings{$name};
216             $strings{$name} = $3;
217         } else {
218             die "Bad line in $statefile:\n$_\n";
219         }
220         my $lib = $name;
221         $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/;
222         $lib = "SSL" if $lib =~ /TLS/;
223         if ( !defined $errorfile{$lib} ) {
224             print "Skipping $_";
225             $skippedstate++;
226             next;
227         }
228         next if $errorfile{$lib} eq 'NONE';
229         if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) {
230             die "$lib reason code $code collision at $name\n"
231                 if $rassigned{$lib} =~ /:$code:/;
232             $rassigned{$lib} .= "$code:";
233             if ( !exists $rextra{$name} ) {
234                 $rmax{$lib} = $code if $code > $rmax{$lib};
235             }
236             $rcodes{$name} = $code;
237         } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) {
238             $fassigned{$lib} .= "$code:";
239             $fmax{$lib} = $code if $code > $fmax{$lib};
240             $fcodes{$name} = $code;
241         } else {
242             die "Bad line in $statefile:\n$_\n";
243         }
244     }
245     close(STATE);
246
247     if ( $debug ) {
248         foreach my $lib ( sort keys %rmax ) {
249             print STDERR "Reason codes for ${lib}:\n";
250             if ( $rassigned{$lib} =~ m/^:(.*):$/ ) {
251                 my @rassigned = sort { $a <=> $b } split( ":", $1 );
252                 print STDERR "  ", join(' ', @rassigned), "\n";
253             } else {
254                 print STDERR "  --none--\n";
255             }
256         }
257         print STDERR "\n";
258         foreach my $lib ( sort keys %fmax ) {
259             print STDERR "Function codes for ${lib}:\n";
260             if ( $fassigned{$lib} =~ m/^:(.*):$/ ) {
261                 my @fassigned = sort { $a <=> $b } split( ":", $1 );
262                 print STDERR "  ", join(' ', @fassigned), "\n";
263             } else {
264                 print STDERR "  --none--\n";
265             }
266         }
267     }
268 }
269
270 # Scan each public header file and make a list of function codes and names
271 &phase("Scanning headers");
272 while ( ( my $hdr, my $lib ) = each %libpubinc ) {
273     next if $hdr eq "NONE";
274     print STDERR " ." if $debug;
275     my $line = "";
276     my $def = "";
277     my $linenr = 0;
278     my $cpp = 0;
279
280     open(IN, "<$hdr")
281         || open(IN, "<$hdr.in")
282         || die "Can't open $hdr or $hdr.in, $!,";
283     while ( <IN> ) {
284         $linenr++;
285
286         if ( $line ne '' ) {
287             $_    = $line . $_;
288             $line = '';
289         }
290
291         if ( /\\$/ ) {
292             $line = $_;
293             next;
294         }
295
296         if ( /\/\*/ ) {
297             if ( not /\*\// ) {    # multiline comment...
298                 $line = $_;        # ... just accumulate
299                 next;
300             } else {
301                 s/\/\*.*?\*\///gs;    # wipe it
302             }
303         }
304
305         if ( $cpp ) {
306             $cpp++ if /^#\s*if/;
307             $cpp-- if /^#\s*endif/;
308             next;
309         }
310         $cpp = 1 if /^#.*ifdef.*cplusplus/;    # skip "C" declaration
311
312         next if /^\#/;    # skip preprocessor directives
313
314         s/{[^{}]*}//gs;     # ignore {} blocks
315
316         if ( /\{|\/\*/ ) {    # Add a so editor works...
317             $line = $_;
318         } else {
319             $def .= $_;
320         }
321     }
322
323     # Delete any DECLARE_ macros
324     my $defnr = 0;
325     $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
326     foreach ( split /;/, $def ) {
327         $defnr++;
328         # The goal is to collect function names from function declarations.
329
330         s/^[\n\s]*//g;
331         s/[\n\s]*$//g;
332
333         # Skip over recognized non-function declarations
334         next if /typedef\W/;
335
336         # Remove STACK_OF(foo)
337         s/STACK_OF\(\w+\)/void/;
338
339         # Reduce argument lists to empty ()
340         # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
341         while ( /\(.*\)/s ) {
342             s/\([^\(\)]+\)/\{\}/gs;
343             s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs;    #(*f{}) -> f
344         }
345
346         # pretend as we didn't use curly braces: {} -> ()
347         s/\{\}/\(\)/gs;
348
349         # Last token just before the first () is a function name.
350         if ( /(\w+)\s*\(\).*/s ) {
351             my $name = $1;
352             $name =~ tr/[a-z]/[A-Z]/;
353             $ftrans{$name} = $1;
354         } elsif ( /[\(\)]/ and not(/=/) ) {
355             print STDERR "Header $hdr: cannot parse: $_;\n";
356         }
357     }
358
359     next if $reindex;
360
361     if ( $lib eq "SSL" && $rmax{$lib} >= 1000 ) {
362         print STDERR "SSL error codes 1000+ are reserved for alerts.\n";
363         print STDERR "Any new alerts must be added to $config.\n";
364         $errors++;
365     }
366     close IN;
367 }
368 print STDERR "\n" if $debug;
369
370 # Scan each C source file and look for function and reason codes
371 # This is done by looking for strings that "look like" function or
372 # reason codes: basically anything consisting of all upper case and
373 # numerics which has _F_ or _R_ in it and which has the name of an
374 # error library at the start.  This seems to work fine except for the
375 # oddly named structure BIO_F_CTX which needs to be ignored.
376 # If a code doesn't exist in list compiled from headers then mark it
377 # with the value "X" as a place holder to give it a value later.
378 # Store all function and reason codes found in %usedfuncs and %usedreasons
379 # so all those unreferenced can be printed out.
380 &phase("Scanning source");
381 my %usedfuncs;
382 my %usedreasons;
383 foreach my $file ( @source ) {
384     # Don't parse the error source file.
385     next if exists $cskip{$file};
386     open( IN, "<$file" ) || die "Can't open $file, $!,";
387     my $func;
388     my $linenr = 0;
389     print STDERR "$file:\n" if $debug;
390     while ( <IN> ) {
391
392         # skip obsoleted source files entirely!
393         last if /^#error\s+obsolete/;
394         $linenr++;
395         if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
396             /^([^()]*(\([^()]*\)[^()]*)*)\(/;
397             $1 =~ /([A-Za-z_0-9]*)$/;
398             $func = $1;
399         }
400
401         if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_F_([A-Z0-9_]+))/ ) {
402             next unless exists $errorfile{$2};
403             next if $errorfile{$2} eq 'NONE';
404             next if $1 eq "BIO_F_BUFFER_CTX";
405             $usedfuncs{$1} = 1;
406             if ( !exists $fcodes{$1} ) {
407                 print STDERR "  New function $1\n" if $debug;
408                 $fcodes{$1} = "X";
409                 $fnew{$2}++;
410             }
411             $ftrans{$3} = $func unless exists $ftrans{$3};
412             print STDERR "  Function $1 = $fcodes{$1}\n"
413               if $debug;
414         }
415         if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) {
416             next unless exists $errorfile{$2};
417             next if $errorfile{$2} eq 'NONE';
418             $usedreasons{$1} = 1;
419             if ( !exists $rcodes{$1} ) {
420                 print STDERR "  New reason $1\n" if $debug;
421                 $rcodes{$1} = "X";
422                 $rnew{$2}++;
423             }
424             print STDERR "  Reason $1 = $rcodes{$1}\n" if $debug;
425         }
426     }
427     close IN;
428 }
429 print STDERR "\n" if $debug;
430
431 # Now process each library in turn.
432 &phase("Writing files");
433 my $newstate = 0;
434 foreach my $lib ( keys %errorfile ) {
435     next if ! $fnew{$lib} && ! $rnew{$lib} && ! $rebuild;
436     next if scalar keys %modules > 0 && !$modules{$lib};
437     next if $nowrite;
438     print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib};
439     print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib};
440     $newstate = 1;
441
442     # If we get here then we have some new error codes so we
443     # need to rebuild the header file and C file.
444
445     # Make a sorted list of error and reason codes for later use.
446     my @function = sort grep( /^${lib}_/, keys %fcodes );
447     my @reasons  = sort grep( /^${lib}_/, keys %rcodes );
448
449     # indent level for innermost preprocessor lines
450     my $indent = " ";
451
452     # Flag if the sub-library is disablable
453     # There are a few exceptions, where disabling the sub-library
454     # doesn't actually remove the whole sub-library, but rather implements
455     # it with a NULL backend.
456     my $disablable =
457         ($lib ne "SSL" && $lib ne "ASYNC" && $lib ne "DSO"
458          && (grep { $lib eq uc $_ } @disablables, @disablables_int));
459
460     # Rewrite the internal header file if there is one ($internal only!)
461
462     if ($hprivinc{$lib} ne 'NONE') {
463         my $hfile = $hprivinc{$lib};
464         my $guard = $hfile;
465
466         if ($guard =~ m|^include/|) {
467             $guard = $';
468         } else {
469             $guard = basename($guard);
470         }
471         $guard = "OSSL_" . join('_', split(m|[./]|, uc $guard));
472
473         open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
474         print OUT <<"EOF";
475 /*
476  * Generated by util/mkerr.pl DO NOT EDIT
477  * Copyright 2020-$YEAR The OpenSSL Project Authors. All Rights Reserved.
478  *
479  * Licensed under the Apache License 2.0 (the \"License\").  You may not use
480  * this file except in compliance with the License.  You can obtain a copy
481  * in the file LICENSE in the source distribution or at
482  * https://www.openssl.org/source/license.html
483  */
484
485 #ifndef $guard
486 # define $guard
487 # pragma once
488
489 # include <openssl/opensslconf.h>
490 # include <openssl/symhacks.h>
491
492 # ifdef  __cplusplus
493 extern \"C\" {
494 # endif
495
496 EOF
497         $indent = ' ';
498         if ($disablable) {
499             print OUT <<"EOF";
500 # ifndef OPENSSL_NO_${lib}
501
502 EOF
503             $indent = "  ";
504         }
505         print OUT <<"EOF";
506 int err_load_${lib}_strings_int(void);
507 EOF
508
509         # If this library doesn't have a public header file, we write all
510         # definitions that would end up there here instead
511         if ($hpubinc{$lib} eq 'NONE') {
512             print OUT "\n/*\n * $lib function codes.\n */\n";
513             print OUT "#${indent}ifndef OPENSSL_NO_DEPRECATED_3_0\n";
514             foreach my $i ( @function ) {
515                 my $z = 48 - length($i);
516                 $z = 0 if $z < 0;
517                 if ( $fcodes{$i} eq "X" ) {
518                     $fassigned{$lib} =~ m/^:([^:]*):/;
519                     my $findcode = $1;
520                     $findcode = $fmax{$lib} if !defined $findcode;
521                     while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
522                         $findcode++;
523                     }
524                     $fcodes{$i} = $findcode;
525                     $fassigned{$lib} .= "$findcode:";
526                     print STDERR "New Function code $i\n" if $debug;
527                 }
528                 printf OUT "#${indent} define $i%s 0\n", " " x $z;
529             }
530             print OUT "#${indent}endif\n";
531
532             print OUT "\n/*\n * $lib reason codes.\n */\n";
533             foreach my $i ( @reasons ) {
534                 my $z = 48 - length($i);
535                 $z = 0 if $z < 0;
536                 if ( $rcodes{$i} eq "X" ) {
537                     $rassigned{$lib} =~ m/^:([^:]*):/;
538                     my $findcode = $1;
539                     $findcode = $rmax{$lib} if !defined $findcode;
540                     while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
541                         $findcode++;
542                     }
543                     $rcodes{$i} = $findcode;
544                     $rassigned{$lib} .= "$findcode:";
545                     print STDERR "New Reason code $i\n" if $debug;
546                 }
547                 printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z;
548             }
549             print OUT "\n";
550         }
551
552         # This doesn't go all the way down to zero, to allow for the ending
553         # brace for 'extern "C" {'.
554         while (length($indent) > 1) {
555             $indent = substr $indent, 0, -1;
556             print OUT "#${indent}endif\n";
557         }
558
559         print OUT <<"EOF";
560
561 # ifdef  __cplusplus
562 }
563 # endif
564 #endif
565 EOF
566         close OUT;
567     }
568
569     # Rewrite the public header file
570
571     if ($hpubinc{$lib} ne 'NONE') {
572         my $extra_include =
573             $internal
574             ? ($lib ne 'SSL'
575                ? "# include <openssl/cryptoerr_legacy.h>\n"
576                : "# include <openssl/sslerr_legacy.h>\n")
577             : '';
578         my $hfile = $hpubinc{$lib};
579         my $guard = $hfile;
580         $guard =~ s|^include/||;
581         $guard = join('_', split(m|[./]|, uc $guard));
582         $guard = "OSSL_" . $guard unless $internal;
583
584         open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,";
585         print OUT <<"EOF";
586 /*
587  * Generated by util/mkerr.pl DO NOT EDIT
588  * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
589  *
590  * Licensed under the Apache License 2.0 (the \"License\").  You may not use
591  * this file except in compliance with the License.  You can obtain a copy
592  * in the file LICENSE in the source distribution or at
593  * https://www.openssl.org/source/license.html
594  */
595
596 #ifndef $guard
597 # define $guard
598 # pragma once
599
600 # include <openssl/opensslconf.h>
601 # include <openssl/symhacks.h>
602 $extra_include
603
604 EOF
605         $indent = ' ';
606         if ( $internal ) {
607             if ($disablable) {
608                 print OUT <<"EOF";
609 # ifndef OPENSSL_NO_${lib}
610
611 EOF
612                 $indent .= ' ';
613             }
614         } else {
615             print OUT <<"EOF";
616 # define ${lib}err(f, r) ERR_${lib}_error(0, (r), OPENSSL_FILE, OPENSSL_LINE)
617
618 EOF
619             if ( ! $static ) {
620                 print OUT <<"EOF";
621
622 # ifdef  __cplusplus
623 extern \"C\" {
624 # endif
625 int ERR_load_${lib}_strings(void);
626 void ERR_unload_${lib}_strings(void);
627 void ERR_${lib}_error(int function, int reason, char *file, int line);
628 # ifdef  __cplusplus
629 }
630 # endif
631 EOF
632             }
633         }
634
635         print OUT "\n/*\n * $lib function codes.\n */\n";
636         print OUT "#${indent}ifndef OPENSSL_NO_DEPRECATED_3_0\n";
637         foreach my $i ( @function ) {
638             my $z = 48 - length($i);
639             $z = 0 if $z < 0;
640             if ( $fcodes{$i} eq "X" ) {
641                 $fassigned{$lib} =~ m/^:([^:]*):/;
642                 my $findcode = $1;
643                 $findcode = $fmax{$lib} if !defined $findcode;
644                 while ( $fassigned{$lib} =~ m/:$findcode:/ ) {
645                     $findcode++;
646                 }
647                 $fcodes{$i} = $findcode;
648                 $fassigned{$lib} .= "$findcode:";
649                 print STDERR "New Function code $i\n" if $debug;
650             }
651             printf OUT "#${indent} define $i%s 0\n", " " x $z;
652         }
653         print OUT "#${indent}endif\n";
654
655         print OUT "\n/*\n * $lib reason codes.\n */\n";
656         foreach my $i ( @reasons ) {
657             my $z = 48 - length($i);
658             $z = 0 if $z < 0;
659             if ( $rcodes{$i} eq "X" ) {
660                 $rassigned{$lib} =~ m/^:([^:]*):/;
661                 my $findcode = $1;
662                 $findcode = $rmax{$lib} if !defined $findcode;
663                 while ( $rassigned{$lib} =~ m/:$findcode:/ ) {
664                     $findcode++;
665                 }
666                 $rcodes{$i} = $findcode;
667                 $rassigned{$lib} .= "$findcode:";
668                 print STDERR "New Reason code $i\n" if $debug;
669             }
670             printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z;
671         }
672         print OUT "\n";
673
674         while (length($indent) > 0) {
675             $indent = substr $indent, 0, -1;
676             print OUT "#${indent}endif\n";
677         }
678         close OUT;
679     }
680
681     # Rewrite the C source file containing the error details.
682
683     if ($errorfile{$lib} ne 'NONE') {
684         # First, read any existing reason string definitions:
685         my $cfile = $errorfile{$lib};
686         my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0";
687         my $hpubincf = $hpubinc{$lib};
688         my $hprivincf = $hprivinc{$lib};
689         my $includes = '';
690         if ($internal) {
691             if ($hpubincf ne 'NONE') {
692                 $hpubincf =~ s|^include/||;
693                 $includes .= "#include <${hpubincf}>\n";
694             }
695             if ($hprivincf =~ m|^include/|) {
696                 $hprivincf = $';
697             } else {
698                 $hprivincf = abs2rel(rel2abs($hprivincf),
699                                      rel2abs(dirname($cfile)));
700             }
701             $includes .= "#include \"${hprivincf}\"\n";
702         } else {
703             $includes .= "#include \"${hpubincf}\"\n";
704         }
705
706         open( OUT, ">$cfile" )
707             || die "Can't open $cfile for writing, $!, stopped";
708
709         my $const = $internal ? 'const ' : '';
710
711         print OUT <<"EOF";
712 /*
713  * Generated by util/mkerr.pl DO NOT EDIT
714  * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
715  *
716  * Licensed under the Apache License 2.0 (the "License").  You may not use
717  * this file except in compliance with the License.  You can obtain a copy
718  * in the file LICENSE in the source distribution or at
719  * https://www.openssl.org/source/license.html
720  */
721
722 #include <openssl/err.h>
723 $includes
724 EOF
725         $indent = '';
726         if ( $internal ) {
727             if ($disablable) {
728                 print OUT <<"EOF";
729 #ifndef OPENSSL_NO_${lib}
730
731 EOF
732                 $indent .= ' ';
733             }
734         }
735         print OUT <<"EOF";
736 #${indent}ifndef OPENSSL_NO_ERR
737
738 static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = {
739 EOF
740
741         # Add each reason code.
742         foreach my $i ( @reasons ) {
743             my $rn;
744             if ( exists $strings{$i} ) {
745                 $rn = $strings{$i};
746                 $rn = "" if $rn eq '*';
747             } else {
748                 $i =~ /^${lib}_R_(\S+)$/;
749                 $rn = $1;
750                 $rn =~ tr/_[A-Z]/ [a-z]/;
751                 $strings{$i} = $rn;
752             }
753             my $short = "    {ERR_PACK($pack_lib, 0, $i), \"$rn\"},";
754             if ( length($short) <= 80 ) {
755                 print OUT "$short\n";
756             } else {
757                 print OUT "    {ERR_PACK($pack_lib, 0, $i),\n    \"$rn\"},\n";
758             }
759         }
760         print OUT <<"EOF";
761     {0, NULL}
762 };
763
764 #${indent}endif
765 EOF
766         if ( $internal ) {
767             print OUT <<"EOF";
768
769 int err_load_${lib}_strings_int(void)
770 {
771 #${indent}ifndef OPENSSL_NO_ERR
772     if (ERR_reason_error_string(${lib}_str_reasons[0].error) == NULL)
773         ERR_load_strings_const(${lib}_str_reasons);
774 #${indent}endif
775     return 1;
776 }
777 EOF
778         } else {
779             my $st = $static ? "static " : "";
780             print OUT <<"EOF";
781
782 static int lib_code = 0;
783 static int error_loaded = 0;
784
785 ${st}int ERR_load_${lib}_strings(void)
786 {
787     if (lib_code == 0)
788         lib_code = ERR_get_next_error_library();
789
790     if (!error_loaded) {
791 #ifndef OPENSSL_NO_ERR
792         ERR_load_strings(lib_code, ${lib}_str_reasons);
793 #endif
794         error_loaded = 1;
795     }
796     return 1;
797 }
798
799 ${st}void ERR_unload_${lib}_strings(void)
800 {
801     if (error_loaded) {
802 #ifndef OPENSSL_NO_ERR
803         ERR_unload_strings(lib_code, ${lib}_str_reasons);
804 #endif
805         error_loaded = 0;
806     }
807 }
808
809 ${st}void ERR_${lib}_error(int function, int reason, char *file, int line)
810 {
811     if (lib_code == 0)
812         lib_code = ERR_get_next_error_library();
813     ERR_raise(lib_code, reason);
814     ERR_set_debug(file, line, NULL);
815 }
816 EOF
817
818         }
819
820         while (length($indent) > 1) {
821             $indent = substr $indent, 0, -1;
822             print OUT "#${indent}endif\n";
823         }
824         if ($internal && $disablable) {
825             print OUT <<"EOF";
826 #else
827 NON_EMPTY_TRANSLATION_UNIT
828 #endif
829 EOF
830         }
831         close OUT;
832     }
833 }
834
835 &phase("Ending");
836 # Make a list of unreferenced function and reason codes
837 if ( $unref ) {
838     my @funref;
839     foreach ( keys %fcodes ) {
840         push( @funref, $_ ) unless exists $usedfuncs{$_};
841     }
842     my @runref;
843     foreach ( keys %rcodes ) {
844         push( @runref, $_ ) unless exists $usedreasons{$_};
845     }
846     if ( @funref ) {
847         print STDERR "The following function codes were not referenced:\n";
848         foreach ( sort @funref ) {
849             print STDERR "  $_\n";
850         }
851     }
852     if ( @runref ) {
853         print STDERR "The following reason codes were not referenced:\n";
854         foreach ( sort @runref ) {
855             print STDERR "  $_\n";
856         }
857     }
858 }
859
860 die "Found $errors errors, quitting" if $errors;
861
862 # Update the state file
863 if ( $newstate )  {
864     open(OUT, ">$statefile.new")
865         || die "Can't write $statefile.new, $!";
866     print OUT <<"EOF";
867 # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved.
868 #
869 # Licensed under the Apache License 2.0 (the "License").  You may not use
870 # this file except in compliance with the License.  You can obtain a copy
871 # in the file LICENSE in the source distribution or at
872 # https://www.openssl.org/source/license.html
873 EOF
874     print OUT "\n# Function codes\n";
875     foreach my $i ( sort keys %fcodes ) {
876         my $short = "$i:$fcodes{$i}:";
877         my $t = exists $strings{$i} ? $strings{$i} : "";
878         $t = "\\\n\t" . $t if length($short) + length($t) > 80;
879         print OUT "$short$t\n";
880     }
881     print OUT "\n#Reason codes\n";
882     foreach my $i ( sort keys %rcodes ) {
883         my $short = "$i:$rcodes{$i}:";
884         my $t = exists $strings{$i} ? "$strings{$i}" : "";
885         $t = "\\\n\t" . $t if length($short) + length($t) > 80;
886         print OUT "$short$t\n" if !exists $rextra{$i};
887     }
888     close(OUT);
889     if ( $skippedstate ) {
890         print "Skipped state, leaving update in $statefile.new";
891     } else {
892         rename "$statefile", "$statefile.old"
893             || die "Can't backup $statefile to $statefile.old, $!";
894         rename "$statefile.new", "$statefile"
895             || die "Can't rename $statefile to $statefile.new, $!";
896     }
897 }
898
899 exit;