]>
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 RS |
124 | # Data parsed out of the config and state files. |
125 | my %hinc; # lib -> header | |
126 | my %libinc; # header -> lib | |
127 | my %cskip; # error_file -> lib | |
128 | my %errorfile; # lib -> error file name | |
129 | my %fmax; # lib -> max assigned function code | |
130 | my %rmax; # lib -> max assigned reason code | |
131 | my %fassigned; # lib -> colon-separated list of assigned function codes | |
132 | my %rassigned; # lib -> colon-separated list of assigned reason codes | |
133 | my %fnew; # lib -> count of new function codes | |
134 | my %rnew; # lib -> count of new reason codes | |
135 | my %rextra; # "extra" reason code -> lib | |
136 | my %rcodes; # reason-name -> value | |
137 | my %ftrans; # old name -> #define-friendly name (all caps) | |
138 | my %fcodes; # function-name -> value | |
139 | my $statefile; # state file with assigned reason and function codes | |
5816586b | 140 | my %strings; # define -> text |
52df25cf RS |
141 | |
142 | # Read and parse the config file | |
143 | open(IN, "$config") || die "Can't open config file $config, $!,"; | |
144 | while ( <IN> ) { | |
145 | next if /^#/ || /^$/; | |
146 | if ( /^L\s+(\S+)\s+(\S+)\s+(\S+)/ ) { | |
147 | my $lib = $1; | |
148 | my $hdr = $2; | |
149 | my $err = $3; | |
150 | $hinc{$lib} = $hdr; | |
151 | $libinc{$hdr} = $lib; | |
152 | $cskip{$err} = $lib; | |
153 | next if $err eq 'NONE'; | |
154 | $errorfile{$lib} = $err; | |
155 | $fmax{$lib} = 100; | |
156 | $rmax{$lib} = 100; | |
157 | $fassigned{$lib} = ":"; | |
158 | $rassigned{$lib} = ":"; | |
159 | $fnew{$lib} = 0; | |
160 | $rnew{$lib} = 0; | |
161 | } elsif ( /^R\s+(\S+)\s+(\S+)/ ) { | |
162 | $rextra{$1} = $2; | |
163 | $rcodes{$1} = $2; | |
164 | } elsif ( /^S\s+(\S+)/ ) { | |
165 | $statefile = $1; | |
166 | } else { | |
167 | die "Illegal config line $_\n"; | |
168 | } | |
169 | } | |
170 | close IN; | |
6d311938 | 171 | |
52df25cf RS |
172 | if ( ! $statefile ) { |
173 | $statefile = $config; | |
174 | $statefile =~ s/.ec/.txt/; | |
6d311938 DSH |
175 | } |
176 | ||
52df25cf RS |
177 | # The statefile has all the previous assignments. |
178 | &phase("Reading state"); | |
179 | my $skippedstate = 0; | |
180 | if ( ! $reindex && $statefile ) { | |
181 | open(STATE, "<$statefile") || die "Can't open $statefile, $!"; | |
182 | ||
183 | # Scan function and reason codes and store them: keep a note of the | |
184 | # maximum code used. | |
185 | while ( <STATE> ) { | |
186 | next if /^#/ || /^$/; | |
5816586b RS |
187 | my $name; |
188 | my $code; | |
189 | if ( /^(.+):(\d+):\\$/ ) { | |
190 | $name = $1; | |
191 | $code = $2; | |
192 | my $next = <STATE>; | |
193 | $next =~ s/^\s*(.*)\s*$/$1/; | |
194 | die "Duplicate define $name" if exists $strings{$name}; | |
195 | $strings{$name} = $next; | |
196 | } elsif ( /^(\S+):(\d+):(.*)$/ ) { | |
197 | $name = $1; | |
198 | $code = $2; | |
199 | die "Duplicate define $name" if exists $strings{$name}; | |
200 | $strings{$name} = $3; | |
201 | } else { | |
202 | die "Bad line in $statefile:\n$_\n"; | |
203 | } | |
52df25cf | 204 | my $lib = $name; |
4b2799c1 | 205 | $lib =~ s/^((?:OSSL_|OPENSSL_)?[^_]{2,}).*$/$1/; |
52df25cf RS |
206 | $lib = "SSL" if $lib =~ /TLS/; |
207 | if ( !defined $errorfile{$lib} ) { | |
208 | print "Skipping $_"; | |
209 | $skippedstate++; | |
210 | next; | |
211 | } | |
4b2799c1 | 212 | if ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_R_/ ) { |
52df25cf RS |
213 | die "$lib reason code $code collision at $name\n" |
214 | if $rassigned{$lib} =~ /:$code:/; | |
215 | $rassigned{$lib} .= "$code:"; | |
216 | if ( !exists $rextra{$name} ) { | |
217 | $rmax{$lib} = $code if $code > $rmax{$lib}; | |
218 | } | |
219 | $rcodes{$name} = $code; | |
4b2799c1 | 220 | } elsif ( $name =~ /^(?:OSSL_|OPENSSL_)?[A-Z0-9]{2,}_F_/ ) { |
52df25cf RS |
221 | die "$lib function code $code collision at $name\n" |
222 | if $fassigned{$lib} =~ /:$code:/; | |
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 | } | |
6d311938 | 254 | |
52df25cf | 255 | # Scan each header file and make a list of error codes |
6d311938 | 256 | # and function names |
52df25cf RS |
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 | } | |
6d311938 | 288 | |
52df25cf RS |
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/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/; | |
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 | ||
5816586b RS |
333 | # Last token just before the first () is a function name. |
334 | if ( /(\w+)\s*\(\).*/s ) { | |
335 | my $name = $1; | |
52df25cf RS |
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; | |
6d311938 | 351 | } |
52df25cf | 352 | print STDERR "\n" if $debug; |
6d311938 DSH |
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 | |
52df25cf | 358 | # error library at the start. This seems to work fine except for the |
6d311938 DSH |
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. | |
52df25cf | 362 | # Store all function and reason codes found in %usedfuncs and %usedreasons |
6e781e8e | 363 | # so all those unreferenced can be printed out. |
52df25cf RS |
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 | ||
4b2799c1 | 385 | if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_F_([A-Z0-9_]+))/ ) { |
52df25cf RS |
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 | if ( uc($func) ne $3 ) { | |
f4c38857 | 396 | print STDERR "ERROR: mismatch $file:$linenr $func:$3\n"; |
52df25cf RS |
397 | $errors++; |
398 | } | |
399 | print STDERR " Function $1 = $fcodes{$1}\n" | |
400 | if $debug; | |
401 | } | |
4b2799c1 | 402 | if ( /(((?:OSSL_|OPENSSL_)?[A-Z0-9]{2,})_R_[A-Z0-9_]+)/ ) { |
52df25cf RS |
403 | next unless exists $errorfile{$2}; |
404 | $usedreasons{$1} = 1; | |
405 | if ( !exists $rcodes{$1} ) { | |
406 | print STDERR " New reason $1\n" if $debug; | |
407 | $rcodes{$1} = "X"; | |
408 | $rnew{$2}++; | |
f4c38857 | 409 | } |
52df25cf RS |
410 | print STDERR " Reason $1 = $rcodes{$1}\n" if $debug; |
411 | } | |
412 | } | |
413 | close IN; | |
6d311938 | 414 | } |
52df25cf | 415 | print STDERR "\n" if $debug; |
6d311938 DSH |
416 | |
417 | # Now process each library in turn. | |
52df25cf RS |
418 | &phase("Writing files"); |
419 | my $newstate = 0; | |
420 | foreach my $lib ( keys %errorfile ) { | |
b53c4fe3 | 421 | next if ! $fnew{$lib} && ! $rnew{$lib} && ! $rebuild; |
964ff301 | 422 | next if scalar keys %modules > 0 && !$modules{$lib}; |
52df25cf RS |
423 | next if $nowrite; |
424 | print STDERR "$lib: $fnew{$lib} new functions\n" if $fnew{$lib}; | |
425 | print STDERR "$lib: $rnew{$lib} new reasons\n" if $rnew{$lib}; | |
426 | $newstate = 1; | |
6d311938 | 427 | |
52df25cf RS |
428 | # If we get here then we have some new error codes so we |
429 | # need to rebuild the header file and C file. | |
430 | ||
431 | # Make a sorted list of error and reason codes for later use. | |
432 | my @function = sort grep( /^${lib}_/, keys %fcodes ); | |
433 | my @reasons = sort grep( /^${lib}_/, keys %rcodes ); | |
434 | ||
cb7b7275 RL |
435 | # indent level for innermost preprocessor lines |
436 | my $indent = " "; | |
437 | ||
52df25cf RS |
438 | # Rewrite the header file |
439 | ||
440 | my $hfile = $hinc{$lib}; | |
441 | $hfile =~ s/.h$/err.h/ if $internal; | |
442 | open( OUT, ">$hfile" ) || die "Can't write to $hfile, $!,"; | |
443 | print OUT <<"EOF"; | |
65aaab2f | 444 | /* |
52df25cf RS |
445 | * Generated by util/mkerr.pl DO NOT EDIT |
446 | * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved. | |
447 | * | |
9059ab42 | 448 | * Licensed under the Apache License 2.0 (the \"License\"). You may not use |
52df25cf RS |
449 | * this file except in compliance with the License. You can obtain a copy |
450 | * in the file LICENSE in the source distribution or at | |
451 | * https://www.openssl.org/source/license.html | |
6d311938 | 452 | */ |
97665e1c | 453 | |
52df25cf RS |
454 | #ifndef HEADER_${lib}ERR_H |
455 | # define HEADER_${lib}ERR_H | |
97665e1c | 456 | |
b53c4fe3 RS |
457 | # ifndef HEADER_SYMHACKS_H |
458 | # include <openssl/symhacks.h> | |
459 | # endif | |
460 | ||
6343e2fa | 461 | EOF |
52df25cf RS |
462 | if ( $internal ) { |
463 | # Declare the load function because the generate C file | |
464 | # includes "fooerr.h" not "foo.h" | |
cb7b7275 RL |
465 | if ($lib ne "SSL" && $lib ne "ASYNC" |
466 | && grep { $lib eq uc $_ } @disablables) { | |
467 | print OUT <<"EOF"; | |
468 | # include <openssl/opensslconf.h> | |
469 | ||
470 | # ifndef OPENSSL_NO_${lib} | |
471 | ||
472 | EOF | |
473 | $indent = " "; | |
474 | } | |
52df25cf | 475 | print OUT <<"EOF"; |
cb7b7275 | 476 | #${indent}ifdef __cplusplus |
a699b8e4 | 477 | extern \"C\" |
cb7b7275 | 478 | #${indent}endif |
52df25cf | 479 | int ERR_load_${lib}_strings(void); |
6343e2fa | 480 | EOF |
52df25cf RS |
481 | } else { |
482 | print OUT <<"EOF"; | |
483 | # define ${lib}err(f, r) ERR_${lib}_error((f), (r), OPENSSL_FILE, OPENSSL_LINE) | |
6d311938 | 484 | |
6d311938 | 485 | EOF |
52df25cf RS |
486 | if ( ! $static ) { |
487 | print OUT <<"EOF"; | |
488 | ||
489 | # ifdef __cplusplus | |
490 | extern \"C\" { | |
491 | # endif | |
492 | int ERR_load_${lib}_strings(void); | |
493 | void ERR_unload_${lib}_strings(void); | |
494 | void ERR_${lib}_error(int function, int reason, char *file, int line); | |
495 | # ifdef __cplusplus | |
496 | } | |
497 | # endif | |
498 | EOF | |
499 | } | |
500 | } | |
501 | ||
502 | print OUT "\n/*\n * $lib function codes.\n */\n"; | |
503 | foreach my $i ( @function ) { | |
504 | my $z = 48 - length($i); | |
0ffdaebf | 505 | $z = 0 if $z < 0; |
52df25cf RS |
506 | if ( $fcodes{$i} eq "X" ) { |
507 | $fassigned{$lib} =~ m/^:([^:]*):/; | |
508 | my $findcode = $1; | |
509 | $findcode = $fmax{$lib} if !defined $findcode; | |
510 | while ( $fassigned{$lib} =~ m/:$findcode:/ ) { | |
511 | $findcode++; | |
512 | } | |
513 | $fcodes{$i} = $findcode; | |
514 | $fassigned{$lib} .= "$findcode:"; | |
515 | print STDERR "New Function code $i\n" if $debug; | |
516 | } | |
cb7b7275 | 517 | printf OUT "#${indent}define $i%s $fcodes{$i}\n", " " x $z; |
52df25cf | 518 | } |
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_functs[] = { |
6d311938 | 579 | EOF |
52df25cf RS |
580 | |
581 | # Add each function code: if a function name is found then use it. | |
582 | foreach my $i ( @function ) { | |
583 | my $fn; | |
5816586b RS |
584 | if ( exists $strings{$i} and $strings{$i} ne '' ) { |
585 | $fn = $strings{$i}; | |
586 | $fn = "" if $fn eq '*'; | |
587 | } else { | |
588 | $i =~ /^${lib}_F_(\S+)$/; | |
589 | $fn = $1; | |
590 | $fn = $ftrans{$fn} if exists $ftrans{$fn}; | |
591 | $strings{$i} = $fn; | |
592 | } | |
52df25cf RS |
593 | my $short = " {ERR_PACK($pack_lib, $i, 0), \"$fn\"},"; |
594 | if ( length($short) <= 80 ) { | |
595 | print OUT "$short\n"; | |
596 | } else { | |
597 | print OUT " {ERR_PACK($pack_lib, $i, 0),\n \"$fn\"},\n"; | |
598 | } | |
599 | } | |
600 | print OUT <<"EOF"; | |
65aaab2f MC |
601 | {0, NULL} |
602 | }; | |
6d311938 | 603 | |
52df25cf | 604 | static ${const}ERR_STRING_DATA ${lib}_str_reasons[] = { |
6d311938 | 605 | EOF |
52df25cf RS |
606 | |
607 | # Add each reason code. | |
608 | foreach my $i ( @reasons ) { | |
609 | my $rn; | |
5816586b RS |
610 | if ( exists $strings{$i} ) { |
611 | $rn = $strings{$i}; | |
612 | $rn = "" if $rn eq '*'; | |
52df25cf RS |
613 | } else { |
614 | $i =~ /^${lib}_R_(\S+)$/; | |
615 | $rn = $1; | |
616 | $rn =~ tr/_[A-Z]/ [a-z]/; | |
5816586b | 617 | $strings{$i} = $rn; |
52df25cf RS |
618 | } |
619 | my $short = " {ERR_PACK($pack_lib, 0, $i), \"$rn\"},"; | |
620 | if ( length($short) <= 80 ) { | |
621 | print OUT "$short\n"; | |
622 | } else { | |
623 | print OUT " {ERR_PACK($pack_lib, 0, $i),\n \"$rn\"},\n"; | |
624 | } | |
625 | } | |
626 | print OUT <<"EOF"; | |
65aaab2f MC |
627 | {0, NULL} |
628 | }; | |
6d311938 DSH |
629 | |
630 | #endif | |
52df25cf RS |
631 | EOF |
632 | if ( $internal ) { | |
633 | print OUT <<"EOF"; | |
6d311938 | 634 | |
52df25cf | 635 | int ERR_load_${lib}_strings(void) |
65aaab2f | 636 | { |
47a9d527 | 637 | #ifndef OPENSSL_NO_ERR |
65aaab2f | 638 | if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) { |
52df25cf RS |
639 | ERR_load_strings_const(${lib}_str_functs); |
640 | ERR_load_strings_const(${lib}_str_reasons); | |
65aaab2f | 641 | } |
47a9d527 | 642 | #endif |
69588edb | 643 | return 1; |
65aaab2f | 644 | } |
6d311938 | 645 | EOF |
52df25cf RS |
646 | } else { |
647 | my $st = $static ? "static " : ""; | |
648 | print OUT <<"EOF"; | |
6d311938 | 649 | |
52df25cf RS |
650 | static int lib_code = 0; |
651 | static int error_loaded = 0; | |
6d311938 | 652 | |
52df25cf | 653 | ${st}int ERR_load_${lib}_strings(void) |
65aaab2f | 654 | { |
52df25cf RS |
655 | if (lib_code == 0) |
656 | lib_code = ERR_get_next_error_library(); | |
6d311938 | 657 | |
52df25cf | 658 | if (!error_loaded) { |
487550b6 | 659 | #ifndef OPENSSL_NO_ERR |
52df25cf RS |
660 | ERR_load_strings(lib_code, ${lib}_str_functs); |
661 | ERR_load_strings(lib_code, ${lib}_str_reasons); | |
525f51f6 | 662 | #endif |
52df25cf | 663 | error_loaded = 1; |
65aaab2f | 664 | } |
69588edb | 665 | return 1; |
65aaab2f | 666 | } |
6d311938 | 667 | |
52df25cf | 668 | ${st}void ERR_unload_${lib}_strings(void) |
65aaab2f | 669 | { |
52df25cf | 670 | if (error_loaded) { |
6343e2fa | 671 | #ifndef OPENSSL_NO_ERR |
52df25cf RS |
672 | ERR_unload_strings(lib_code, ${lib}_str_functs); |
673 | ERR_unload_strings(lib_code, ${lib}_str_reasons); | |
6343e2fa | 674 | #endif |
52df25cf | 675 | error_loaded = 0; |
65aaab2f MC |
676 | } |
677 | } | |
6343e2fa | 678 | |
52df25cf | 679 | ${st}void ERR_${lib}_error(int function, int reason, char *file, int line) |
65aaab2f | 680 | { |
52df25cf RS |
681 | if (lib_code == 0) |
682 | lib_code = ERR_get_next_error_library(); | |
683 | ERR_PUT_error(lib_code, function, reason, file, line); | |
65aaab2f | 684 | } |
6d311938 DSH |
685 | EOF |
686 | ||
52df25cf | 687 | } |
6d311938 | 688 | |
52df25cf | 689 | close OUT; |
6e781e8e DSH |
690 | } |
691 | ||
52df25cf | 692 | &phase("Ending"); |
6e781e8e | 693 | # Make a list of unreferenced function and reason codes |
52df25cf RS |
694 | if ( $unref ) { |
695 | my @funref; | |
696 | foreach ( keys %fcodes ) { | |
697 | push( @funref, $_ ) unless exists $usedfuncs{$_}; | |
698 | } | |
699 | my @runref; | |
700 | foreach ( keys %rcodes ) { | |
701 | push( @runref, $_ ) unless exists $usedreasons{$_}; | |
702 | } | |
703 | if ( @funref ) { | |
704 | print STDERR "The following function codes were not referenced:\n"; | |
705 | foreach ( sort @funref ) { | |
706 | print STDERR " $_\n"; | |
707 | } | |
708 | } | |
709 | if ( @runref ) { | |
710 | print STDERR "The following reason codes were not referenced:\n"; | |
711 | foreach ( sort @runref ) { | |
712 | print STDERR " $_\n"; | |
713 | } | |
714 | } | |
6e781e8e DSH |
715 | } |
716 | ||
52df25cf | 717 | die "Found $errors errors, quitting" if $errors; |
f3d2a9db | 718 | |
52df25cf RS |
719 | # Update the state file |
720 | if ( $newstate ) { | |
721 | open(OUT, ">$statefile.new") | |
722 | || die "Can't write $statefile.new, $!"; | |
979874a2 | 723 | print OUT <<"EOF"; |
f0bbf365 | 724 | # Copyright 1999-$YEAR The OpenSSL Project Authors. All Rights Reserved. |
979874a2 | 725 | # |
9059ab42 | 726 | # Licensed under the Apache License 2.0 (the "License"). You may not use |
979874a2 RL |
727 | # this file except in compliance with the License. You can obtain a copy |
728 | # in the file LICENSE in the source distribution or at | |
729 | # https://www.openssl.org/source/license.html | |
730 | EOF | |
731 | print OUT "\n# Function codes\n"; | |
52df25cf | 732 | foreach my $i ( sort keys %fcodes ) { |
5816586b RS |
733 | my $short = "$i:$fcodes{$i}:"; |
734 | my $t = exists $strings{$i} ? $strings{$i} : ""; | |
735 | $t = "\\\n\t" . $t if length($short) + length($t) > 80; | |
736 | print OUT "$short$t\n"; | |
52df25cf | 737 | } |
5816586b | 738 | print OUT "\n#Reason codes\n"; |
52df25cf | 739 | foreach my $i ( sort keys %rcodes ) { |
5816586b RS |
740 | my $short = "$i:$rcodes{$i}:"; |
741 | my $t = exists $strings{$i} ? "$strings{$i}" : ""; | |
742 | $t = "\\\n\t" . $t if length($short) + length($t) > 80; | |
743 | print OUT "$short$t\n" if !exists $rextra{$i}; | |
52df25cf RS |
744 | } |
745 | close(OUT); | |
746 | if ( $skippedstate ) { | |
747 | print "Skipped state, leaving update in $statefile.new"; | |
748 | } else { | |
749 | rename "$statefile", "$statefile.old" | |
750 | || die "Can't backup $statefile to $statefile.old, $!"; | |
751 | rename "$statefile.new", "$statefile" | |
752 | || die "Can't rename $statefile to $statefile.new, $!"; | |
753 | } | |
f3d2a9db | 754 | } |
5816586b | 755 | |
52df25cf | 756 | exit; |