]>
Commit | Line | Data |
---|---|---|
e0a65194 | 1 | #! /usr/bin/env perl |
f0bbf365 | 2 | # Copyright 1999-2018 The OpenSSL Project Authors. All Rights Reserved. |
e0a65194 | 3 | # |
9059ab42 | 4 | # Licensed under the Apache License 2.0 (the "License"). You may not use |
e0a65194 RS |
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 |
9 | use strict; |
10 | use warnings; | |
11 | ||
cb7b7275 RL |
12 | use lib "."; |
13 | use configdata; | |
14 | ||
52df25cf RS |
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; | |
964ff301 | 23 | my %modules = (); |
52df25cf RS |
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...] | |
e5fa864f DSH |
39 | |
40 | Options: | |
41 | ||
52df25cf RS |
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 | ||
964ff301 RL |
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 | ||
52df25cf RS |
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. | |
e5fa864f DSH |
73 | |
74 | EOF | |
6d311938 DSH |
75 | } |
76 | ||
52df25cf RS |
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; | |
964ff301 RL |
100 | } elsif ( $arg eq "-module" ) { |
101 | shift @ARGV; | |
102 | $modules{uc $ARGV[0]} = 1; | |
52df25cf RS |
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; | |
6d311938 DSH |
110 | } |
111 | ||
52df25cf RS |
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'), | |
6caf7f3a MC |
117 | glob('ssl/*.c'), glob('ssl/*/*.c'), glob('providers/*.c'), |
118 | glob('providers/*/*.c'), glob('providers/*/*/*.c') ); | |
52df25cf | 119 | } else { |
964ff301 | 120 | die "-module isn't useful without -internal\n" if scalar keys %modules > 0; |
52df25cf RS |
121 | @source = @ARGV; |
122 | } | |
6d311938 | 123 | |
52df25cf | 124 | # Data parsed out of the config and state files. |
aac96e27 RS |
125 | # We always map function-code values to zero, so items marked below with |
126 | # an asterisk could eventually be removed. TODO(4.0) | |
52df25cf RS |
127 | my %hinc; # lib -> header |
128 | my %libinc; # header -> lib | |
129 | my %cskip; # error_file -> lib | |
130 | my %errorfile; # lib -> error file name | |
aac96e27 | 131 | my %fmax; # lib -> max assigned function code* |
52df25cf | 132 | my %rmax; # lib -> max assigned reason code |
aac96e27 | 133 | my %fassigned; # lib -> colon-separated list of assigned function codes* |
52df25cf | 134 | my %rassigned; # lib -> colon-separated list of assigned reason codes |
aac96e27 | 135 | my %fnew; # lib -> count of new function codes* |
52df25cf RS |
136 | my %rnew; # lib -> count of new reason codes |
137 | my %rextra; # "extra" reason code -> lib | |
138 | my %rcodes; # reason-name -> value | |
aac96e27 RS |
139 | my %ftrans; # old name -> #define-friendly name (all caps)* |
140 | my %fcodes; # function-name -> value* | |
52df25cf | 141 | my $statefile; # state file with assigned reason and function codes |
5816586b | 142 | my %strings; # define -> text |
52df25cf RS |
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; | |
6d311938 | 173 | |
52df25cf RS |
174 | if ( ! $statefile ) { |
175 | $statefile = $config; | |
176 | $statefile =~ s/.ec/.txt/; | |
6d311938 DSH |
177 | } |
178 | ||
52df25cf RS |
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 /^#/ || /^$/; | |
5816586b RS |
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 | } | |
52df25cf | 206 | my $lib = $name; |
4b2799c1 | 207 | $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/; |
52df25cf RS |
208 | $lib = "SSL" if $lib =~ /TLS/; |
209 | if ( !defined $errorfile{$lib} ) { | |
210 | print "Skipping $_"; | |
211 | $skippedstate++; | |
212 | next; | |
213 | } | |
4b2799c1 | 214 | if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) { |
52df25cf RS |
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; | |
4b2799c1 | 222 | } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) { |
52df25cf RS |
223 | die "$lib function code $code collision at $name\n" |
224 | if $fassigned{$lib} =~ /:$code:/; | |
225 | $fassigned{$lib} .= "$code:"; | |
226 | $fmax{$lib} = $code if $code > $fmax{$lib}; | |
227 | $fcodes{$name} = $code; | |
228 | } else { | |
229 | die "Bad line in $statefile:\n$_\n"; | |
230 | } | |
231 | } | |
232 | close(STATE); | |
233 | ||
234 | if ( $debug ) { | |
235 | foreach my $lib ( sort keys %rmax ) { | |
236 | print STDERR "Reason codes for ${lib}:\n"; | |
237 | if ( $rassigned{$lib} =~ m/^:(.*):$/ ) { | |
238 | my @rassigned = sort { $a <=> $b } split( ":", $1 ); | |
239 | print STDERR " ", join(' ', @rassigned), "\n"; | |
240 | } else { | |
241 | print STDERR " --none--\n"; | |
242 | } | |
243 | } | |
244 | print STDERR "\n"; | |
245 | foreach my $lib ( sort keys %fmax ) { | |
246 | print STDERR "Function codes for ${lib}:\n"; | |
247 | if ( $fassigned{$lib} =~ m/^:(.*):$/ ) { | |
248 | my @fassigned = sort { $a <=> $b } split( ":", $1 ); | |
249 | print STDERR " ", join(' ', @fassigned), "\n"; | |
250 | } else { | |
251 | print STDERR " --none--\n"; | |
252 | } | |
253 | } | |
254 | } | |
255 | } | |
6d311938 | 256 | |
52df25cf | 257 | # Scan each header file and make a list of error codes |
6d311938 | 258 | # and function names |
52df25cf RS |
259 | &phase("Scanning headers"); |
260 | while ( ( my $hdr, my $lib ) = each %libinc ) { | |
261 | next if $hdr eq "NONE"; | |
262 | print STDERR " ." if $debug; | |
263 | my $line = ""; | |
264 | my $def = ""; | |
265 | my $linenr = 0; | |
266 | my $cpp = 0; | |
267 | ||
268 | open(IN, "<$hdr") || die "Can't open $hdr, $!,"; | |
269 | while ( <IN> ) { | |
270 | $linenr++; | |
271 | ||
272 | if ( $line ne '' ) { | |
273 | $_ = $line . $_; | |
274 | $line = ''; | |
275 | } | |
276 | ||
277 | if ( /\\$/ ) { | |
278 | $line = $_; | |
279 | next; | |
280 | } | |
281 | ||
282 | if ( /\/\*/ ) { | |
283 | if ( not /\*\// ) { # multiline comment... | |
284 | $line = $_; # ... just accumulate | |
285 | next; | |
286 | } else { | |
287 | s/\/\*.*?\*\///gs; # wipe it | |
288 | } | |
289 | } | |
6d311938 | 290 | |
52df25cf RS |
291 | if ( $cpp ) { |
292 | $cpp++ if /^#\s*if/; | |
293 | $cpp-- if /^#\s*endif/; | |
294 | next; | |
295 | } | |
296 | $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration | |
297 | ||
298 | next if /^\#/; # skip preprocessor directives | |
299 | ||
300 | s/{[^{}]*}//gs; # ignore {} blocks | |
301 | ||
302 | if ( /\{|\/\*/ ) { # Add a so editor works... | |
303 | $line = $_; | |
304 | } else { | |
305 | $def .= $_; | |
306 | } | |
307 | } | |
308 | ||
309 | # Delete any DECLARE_ macros | |
310 | my $defnr = 0; | |
311 | $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs; | |
312 | foreach ( split /;/, $def ) { | |
313 | $defnr++; | |
314 | # The goal is to collect function names from function declarations. | |
315 | ||
316 | s/^[\n\s]*//g; | |
317 | s/[\n\s]*$//g; | |
318 | ||
319 | # Skip over recognized non-function declarations | |
320 | next if /typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/; | |
321 | ||
322 | # Remove STACK_OF(foo) | |
323 | s/STACK_OF\(\w+\)/void/; | |
324 | ||
325 | # Reduce argument lists to empty () | |
326 | # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {} | |
327 | while ( /\(.*\)/s ) { | |
328 | s/\([^\(\)]+\)/\{\}/gs; | |
329 | s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f | |
330 | } | |
331 | ||
332 | # pretend as we didn't use curly braces: {} -> () | |
333 | s/\{\}/\(\)/gs; | |
334 | ||
5816586b RS |
335 | # Last token just before the first () is a function name. |
336 | if ( /(\w+)\s*\(\).*/s ) { | |
337 | my $name = $1; | |
52df25cf RS |
338 | $name =~ tr/[a-z]/[A-Z]/; |
339 | $ftrans{$name} = $1; | |
340 | } elsif ( /[\(\)]/ and not(/=/) ) { | |
341 | print STDERR "Header $hdr: cannot parse: $_;\n"; | |
342 | } | |
343 | } | |
344 | ||
345 | next if $reindex; | |
346 | ||
347 | if ( $lib eq "SSL" && $rmax{$lib} >= 1000 ) { | |
348 | print STDERR "SSL error codes 1000+ are reserved for alerts.\n"; | |
349 | print STDERR "Any new alerts must be added to $config.\n"; | |
350 | $errors++; | |
351 | } | |
352 | close IN; | |
6d311938 | 353 | } |
52df25cf | 354 | print STDERR "\n" if $debug; |
6d311938 DSH |
355 | |
356 | # Scan each C source file and look for function and reason codes | |
357 | # This is done by looking for strings that "look like" function or | |
358 | # reason codes: basically anything consisting of all upper case and | |
359 | # numerics which has _F_ or _R_ in it and which has the name of an | |
52df25cf | 360 | # error library at the start. This seems to work fine except for the |
6d311938 DSH |
361 | # oddly named structure BIO_F_CTX which needs to be ignored. |
362 | # If a code doesn't exist in list compiled from headers then mark it | |
363 | # with the value "X" as a place holder to give it a value later. | |
52df25cf | 364 | # Store all function and reason codes found in %usedfuncs and %usedreasons |
6e781e8e | 365 | # so all those unreferenced can be printed out. |
52df25cf RS |
366 | &phase("Scanning source"); |
367 | my %usedfuncs; | |
368 | my %usedreasons; | |
369 | foreach my $file ( @source ) { | |
370 | # Don't parse the error source file. | |
371 | next if exists $cskip{$file}; | |
372 | open( IN, "<$file" ) || die "Can't open $file, $!,"; | |
373 | my $func; | |
374 | my $linenr = 0; | |
375 | print STDERR "$file:\n" if $debug; | |
376 | while ( <IN> ) { | |
377 | ||
378 | # skip obsoleted source files entirely! | |
379 | last if /^#error\s+obsolete/; | |
380 | $linenr++; | |
381 | if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) { | |
382 | /^([^()]*(\([^()]*\)[^()]*)*)\(/; | |
383 | $1 =~ /([A-Za-z_0-9]*)$/; | |
384 | $func = $1; | |
385 | } | |
386 | ||
4b2799c1 | 387 | if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_F_([A-Z0-9_]+))/ ) { |
52df25cf RS |
388 | next unless exists $errorfile{$2}; |
389 | next if $1 eq "BIO_F_BUFFER_CTX"; | |
390 | $usedfuncs{$1} = 1; | |
391 | if ( !exists $fcodes{$1} ) { | |
392 | print STDERR " New function $1\n" if $debug; | |
393 | $fcodes{$1} = "X"; | |
394 | $fnew{$2}++; | |
395 | } | |
396 | $ftrans{$3} = $func unless exists $ftrans{$3}; | |
52df25cf RS |
397 | print STDERR " Function $1 = $fcodes{$1}\n" |
398 | if $debug; | |
399 | } | |
4b2799c1 | 400 | if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) { |
52df25cf RS |
401 | next unless exists $errorfile{$2}; |
402 | $usedreasons{$1} = 1; | |
403 | if ( !exists $rcodes{$1} ) { | |
404 | print STDERR " New reason $1\n" if $debug; | |
405 | $rcodes{$1} = "X"; | |
406 | $rnew{$2}++; | |
f4c38857 | 407 | } |
52df25cf RS |
408 | print STDERR " Reason $1 = $rcodes{$1}\n" if $debug; |
409 | } | |
410 | } | |
411 | close IN; | |
6d311938 | 412 | } |
52df25cf | 413 | print STDERR "\n" if $debug; |
6d311938 DSH |
414 | |
415 | # Now process each library in turn. | |
52df25cf RS |
416 | &phase("Writing files"); |
417 | my $newstate = 0; | |
418 | foreach my $lib ( keys %errorfile ) { | |
b53c4fe3 | 419 | next if ! $fnew{$lib} && ! $rnew{$lib} && ! $rebuild; |
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 | ||
cb7b7275 RL |
433 | # indent level for innermost preprocessor lines |
434 | my $indent = " "; | |
435 | ||
52df25cf RS |
436 | # Rewrite the header file |
437 | ||
438 | my $hfile = $hinc{$lib}; | |
439 | $hfile =~ s/.h$/err.h/ if $internal; | |
440 | open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,"; | |
441 | print OUT <<"EOF"; | |
65aaab2f | 442 | /* |
52df25cf RS |
443 | * Generated by util/mkerr.pl DO NOT EDIT |
444 | * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved. | |
445 | * | |
9059ab42 | 446 | * Licensed under the Apache License 2.0 (the \"License\"). You may not use |
52df25cf RS |
447 | * this file except in compliance with the License. You can obtain a copy |
448 | * in the file LICENSE in the source distribution or at | |
449 | * https://www.openssl.org/source/license.html | |
6d311938 | 450 | */ |
97665e1c | 451 | |
52df25cf RS |
452 | #ifndef HEADER_${lib}ERR_H |
453 | # define HEADER_${lib}ERR_H | |
97665e1c | 454 | |
aac96e27 RS |
455 | # include <openssl/opensslconf.h> |
456 | # include <openssl/symhacks.h> | |
457 | ||
b53c4fe3 | 458 | |
6343e2fa | 459 | EOF |
52df25cf RS |
460 | if ( $internal ) { |
461 | # Declare the load function because the generate C file | |
462 | # includes "fooerr.h" not "foo.h" | |
cb7b7275 | 463 | if ($lib ne "SSL" && $lib ne "ASYNC" |
538f38db | 464 | && (grep { $lib eq uc $_ } @disablables, @disablables_int)) { |
cb7b7275 RL |
465 | print OUT <<"EOF"; |
466 | # include <openssl/opensslconf.h> | |
467 | ||
468 | # ifndef OPENSSL_NO_${lib} | |
469 | ||
470 | EOF | |
471 | $indent = " "; | |
472 | } | |
52df25cf | 473 | print OUT <<"EOF"; |
cb7b7275 | 474 | #${indent}ifdef __cplusplus |
a699b8e4 | 475 | extern \"C\" |
cb7b7275 | 476 | #${indent}endif |
52df25cf | 477 | int ERR_load_${lib}_strings(void); |
6343e2fa | 478 | EOF |
52df25cf RS |
479 | } else { |
480 | print OUT <<"EOF"; | |
aac96e27 | 481 | # define ${lib}err(f, r) ERR_${lib}_error(0, (r), OPENSSL_FILE, OPENSSL_LINE) |
6d311938 | 482 | |
6d311938 | 483 | EOF |
52df25cf RS |
484 | if ( ! $static ) { |
485 | print OUT <<"EOF"; | |
486 | ||
487 | # ifdef __cplusplus | |
488 | extern \"C\" { | |
489 | # endif | |
490 | int ERR_load_${lib}_strings(void); | |
491 | void ERR_unload_${lib}_strings(void); | |
492 | void ERR_${lib}_error(int function, int reason, char *file, int line); | |
493 | # ifdef __cplusplus | |
494 | } | |
495 | # endif | |
496 | EOF | |
497 | } | |
498 | } | |
499 | ||
500 | print OUT "\n/*\n * $lib function codes.\n */\n"; | |
aac96e27 | 501 | print OUT "# if !OPENSSL_API_3\n"; |
52df25cf RS |
502 | foreach my $i ( @function ) { |
503 | my $z = 48 - length($i); | |
0ffdaebf | 504 | $z = 0 if $z < 0; |
52df25cf RS |
505 | if ( $fcodes{$i} eq "X" ) { |
506 | $fassigned{$lib} =~ m/^:([^:]*):/; | |
507 | my $findcode = $1; | |
508 | $findcode = $fmax{$lib} if !defined $findcode; | |
509 | while ( $fassigned{$lib} =~ m/:$findcode:/ ) { | |
510 | $findcode++; | |
511 | } | |
512 | $fcodes{$i} = $findcode; | |
513 | $fassigned{$lib} .= "$findcode:"; | |
514 | print STDERR "New Function code $i\n" if $debug; | |
515 | } | |
aac96e27 | 516 | printf OUT "#${indent} define $i%s 0\n", " " x $z; |
52df25cf | 517 | } |
aac96e27 | 518 | print OUT "# endif\n"; |
6d311938 | 519 | |
52df25cf RS |
520 | print OUT "\n/*\n * $lib reason codes.\n */\n"; |
521 | foreach my $i ( @reasons ) { | |
522 | my $z = 48 - length($i); | |
0ffdaebf | 523 | $z = 0 if $z < 0; |
52df25cf RS |
524 | if ( $rcodes{$i} eq "X" ) { |
525 | $rassigned{$lib} =~ m/^:([^:]*):/; | |
526 | my $findcode = $1; | |
527 | $findcode = $rmax{$lib} if !defined $findcode; | |
528 | while ( $rassigned{$lib} =~ m/:$findcode:/ ) { | |
529 | $findcode++; | |
530 | } | |
531 | $rcodes{$i} = $findcode; | |
532 | $rassigned{$lib} .= "$findcode:"; | |
533 | print STDERR "New Reason code $i\n" if $debug; | |
534 | } | |
cb7b7275 | 535 | printf OUT "#${indent}define $i%s $rcodes{$i}\n", " " x $z; |
52df25cf RS |
536 | } |
537 | print OUT "\n"; | |
538 | ||
cb7b7275 RL |
539 | while (length($indent) > 0) { |
540 | $indent = substr $indent, 0, -1; | |
541 | print OUT "#${indent}endif\n"; | |
542 | } | |
52df25cf RS |
543 | |
544 | # Rewrite the C source file containing the error details. | |
545 | ||
546 | # First, read any existing reason string definitions: | |
52df25cf | 547 | my $cfile = $errorfile{$lib}; |
52df25cf RS |
548 | my $pack_lib = $internal ? "ERR_LIB_${lib}" : "0"; |
549 | my $hincf = $hfile; | |
550 | $hincf =~ s|.*include/||; | |
551 | if ( $hincf =~ m|^openssl/| ) { | |
552 | $hincf = "<${hincf}>"; | |
553 | } else { | |
554 | $hincf = "\"${hincf}\""; | |
555 | } | |
556 | ||
557 | open( OUT, ">$cfile" ) | |
558 | || die "Can't open $cfile for writing, $!, stopped"; | |
559 | ||
560 | my $const = $internal ? 'const ' : ''; | |
561 | ||
562 | print OUT <<"EOF"; | |
e0a65194 | 563 | /* |
e8b7c0c4 RS |
564 | * Generated by util/mkerr.pl DO NOT EDIT |
565 | * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved. | |
6d311938 | 566 | * |
9059ab42 | 567 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
e0a65194 RS |
568 | * this file except in compliance with the License. You can obtain a copy |
569 | * in the file LICENSE in the source distribution or at | |
570 | * https://www.openssl.org/source/license.html | |
6d311938 DSH |
571 | */ |
572 | ||
6d311938 | 573 | #include <openssl/err.h> |
170afce5 | 574 | #include $hincf |
6d311938 | 575 | |
487550b6 | 576 | #ifndef OPENSSL_NO_ERR |
bc3cae7e | 577 | |
52df25cf | 578 | static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = { |
6d311938 | 579 | EOF |
52df25cf RS |
580 | |
581 | # Add each reason code. | |
582 | foreach my $i ( @reasons ) { | |
583 | my $rn; | |
5816586b RS |
584 | if ( exists $strings{$i} ) { |
585 | $rn = $strings{$i}; | |
586 | $rn = "" if $rn eq '*'; | |
52df25cf RS |
587 | } else { |
588 | $i =~ /^${lib}_R_(\S+)$/; | |
589 | $rn = $1; | |
590 | $rn =~ tr/_[A-Z]/ [a-z]/; | |
5816586b | 591 | $strings{$i} = $rn; |
52df25cf RS |
592 | } |
593 | my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},"; | |
594 | if ( length($short) <= 80 ) { | |
595 | print OUT "$short\n"; | |
596 | } else { | |
597 | print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n"; | |
598 | } | |
599 | } | |
600 | print OUT <<"EOF"; | |
65aaab2f MC |
601 | {0, NULL} |
602 | }; | |
6d311938 DSH |
603 | |
604 | #endif | |
52df25cf RS |
605 | EOF |
606 | if ( $internal ) { | |
607 | print OUT <<"EOF"; | |
6d311938 | 608 | |
52df25cf | 609 | int ERR_load_${lib}_strings(void) |
65aaab2f | 610 | { |
47a9d527 | 611 | #ifndef OPENSSL_NO_ERR |
aac96e27 | 612 | if (ERR_func_error_string(${lib}_str_reasons[0].error) == NULL) |
52df25cf | 613 | ERR_load_strings_const(${lib}_str_reasons); |
47a9d527 | 614 | #endif |
69588edb | 615 | return 1; |
65aaab2f | 616 | } |
6d311938 | 617 | EOF |
52df25cf RS |
618 | } else { |
619 | my $st = $static ? "static " : ""; | |
620 | print OUT <<"EOF"; | |
6d311938 | 621 | |
52df25cf RS |
622 | static int lib_code = 0; |
623 | static int error_loaded = 0; | |
6d311938 | 624 | |
52df25cf | 625 | ${st}int ERR_load_${lib}_strings(void) |
65aaab2f | 626 | { |
52df25cf RS |
627 | if (lib_code == 0) |
628 | lib_code = ERR_get_next_error_library(); | |
6d311938 | 629 | |
52df25cf | 630 | if (!error_loaded) { |
487550b6 | 631 | #ifndef OPENSSL_NO_ERR |
52df25cf | 632 | ERR_load_strings(lib_code, ${lib}_str_reasons); |
525f51f6 | 633 | #endif |
52df25cf | 634 | error_loaded = 1; |
65aaab2f | 635 | } |
69588edb | 636 | return 1; |
65aaab2f | 637 | } |
6d311938 | 638 | |
52df25cf | 639 | ${st}void ERR_unload_${lib}_strings(void) |
65aaab2f | 640 | { |
52df25cf | 641 | if (error_loaded) { |
6343e2fa | 642 | #ifndef OPENSSL_NO_ERR |
52df25cf | 643 | ERR_unload_strings(lib_code, ${lib}_str_reasons); |
6343e2fa | 644 | #endif |
52df25cf | 645 | error_loaded = 0; |
65aaab2f MC |
646 | } |
647 | } | |
6343e2fa | 648 | |
52df25cf | 649 | ${st}void ERR_${lib}_error(int function, int reason, char *file, int line) |
65aaab2f | 650 | { |
52df25cf RS |
651 | if (lib_code == 0) |
652 | lib_code = ERR_get_next_error_library(); | |
653 | ERR_PUT_error(lib_code, function, reason, file, line); | |
65aaab2f | 654 | } |
6d311938 DSH |
655 | EOF |
656 | ||
52df25cf | 657 | } |
6d311938 | 658 | |
52df25cf | 659 | close OUT; |
6e781e8e DSH |
660 | } |
661 | ||
52df25cf | 662 | &phase("Ending"); |
6e781e8e | 663 | # Make a list of unreferenced function and reason codes |
52df25cf RS |
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 | } | |
6e781e8e DSH |
685 | } |
686 | ||
52df25cf | 687 | die "Found $errors errors, quitting" if $errors; |
f3d2a9db | 688 | |
52df25cf RS |
689 | # Update the state file |
690 | if ( $newstate ) { | |
691 | open(OUT, ">$statefile.new") | |
692 | || die "Can't write $statefile.new, $!"; | |
979874a2 | 693 | print OUT <<"EOF"; |
f0bbf365 | 694 | # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved. |
979874a2 | 695 | # |
9059ab42 | 696 | # Licensed under the Apache License 2.0 (the "License"). You may not use |
979874a2 RL |
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"; | |
52df25cf | 702 | foreach my $i ( sort keys %fcodes ) { |
5816586b RS |
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"; | |
52df25cf | 707 | } |
5816586b | 708 | print OUT "\n#Reason codes\n"; |
52df25cf | 709 | foreach my $i ( sort keys %rcodes ) { |
5816586b RS |
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}; | |
52df25cf RS |
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 | } | |
f3d2a9db | 724 | } |
5816586b | 725 | |
52df25cf | 726 | exit; |