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