1 #!/usr/local/bin/perl -w
3 my $config = "crypto/err/openssl.ec";
10 my $staticloader = "";
19 $config = shift @ARGV;
20 } elsif($arg eq "-debug") {
23 } elsif($arg eq "-rebuild") {
26 } elsif($arg eq "-recurse") {
29 } elsif($arg eq "-reindex") {
32 } elsif($arg eq "-nostatic") {
35 } elsif($arg eq "-staticloader") {
36 $staticloader = "static ";
38 } elsif($arg eq "-write") {
47 @source = (<crypto
/*.c>, <crypto/*/*.c>, <ssl/*.c
>);
52 # Read in the config file
54 open(IN
, "<$config") || die "Can't open config file $config";
60 if(/^L\s+(\S+)\s+(\S+)\s+(\S+)/) {
73 } elsif (/^F\s+(\S+)/) {
74 # Add extra function with $1
75 } elsif (/^R\s+(\S+)\s+(\S+)/) {
83 # Scan each header file in turn and make a list of error codes
86 while (($hdr, $lib) = each %libinc)
88 next if($hdr eq "NONE");
89 print STDERR
"Scanning header file $hdr\n" if $debug;
90 my $line = "", $def= "", $linenr = 0, $gotfile = 0;
91 if (open(IN
, "<$hdr")) {
95 print STDERR
"line: $linenr\r" if $debug;
97 last if(/BEGIN\s+ERROR\s+CODES/);
109 if (not /\*\//) { # multiline comment...
110 $line = $_; # ... just accumulate
113 s/\/\*.*?\*\///gs; # wipe it
119 $cpp-- if /^#\s*endif/;
122 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
124 next if (/^\#/); # skip preprocessor directives
126 s/{[^{}]*}//gs; # ignore {} blocks
128 if (/\{|\/\
*/) { # Add a } so editor works...
136 print STDERR
" \r" if $debug;
138 # Delete any DECLARE_ macros
139 $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
140 foreach (split /;/, $def) {
142 print STDERR
"def: $defnr\r" if $debug;
144 # The goal is to collect function names from function declarations.
149 # Skip over recognized non-function declarations
150 next if(/typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/);
152 # Remove STACK_OF(foo)
153 s/STACK_OF\(\w+\)/void/;
155 # Reduce argument lists to empty ()
156 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
158 s/\([^\(\)]+\)/\{\}/gs;
159 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
161 # pretend as we didn't use curly braces: {} -> ()
164 if (/(\w+)\s*\(\).*/s) { # first token prior [first] () is
165 my $name = $1; # a function name!
166 $name =~ tr/[a-z]/[A-Z]/;
168 } elsif (/[\(\)]/ and not (/=/)) {
169 print STDERR
"Header $hdr: cannot parse: $_;\n";
173 print STDERR
" \r" if $debug;
177 # Scan function and reason codes and store them: keep a note of the
182 if(/^\#define\s+(\S+)\s+(\S+)/) {
185 next if $name =~ /^${lib}err/;
186 unless($name =~ /^${lib}_([RF])_(\w+)$/) {
187 print STDERR
"Invalid error code $name\n";
191 $rcodes{$name} = $code;
192 if ($rassigned{$lib} =~ /:$code:/) {
193 print STDERR
"!! ERROR: $lib reason code $code assigned twice\n";
195 $rassigned{$lib} .= "$code:";
196 if(!(exists $rextra{$name}) &&
197 ($code > $rmax{$lib}) ) {
201 if ($fassigned{$lib} =~ /:$code:/) {
202 print STDERR
"!! ERROR: $lib function code $code assigned twice\n";
204 $fassigned{$lib} .= "$code:";
205 if($code > $fmax{$lib}) {
208 $fcodes{$name} = $code;
215 if (defined($fmax{$lib})) {
216 print STDERR
"Max function code fmax" . "{" . "$lib" . "} = $fmax{$lib}\n";
217 $fassigned{$lib} =~ m/^:(.*):$/;
218 @fassigned = sort {$a <=> $b} split(":", $1);
219 print STDERR
" @fassigned\n";
221 if (defined($rmax{$lib})) {
222 print STDERR
"Max reason code rmax" . "{" . "$lib" . "} = $rmax{$lib}\n";
223 $rassigned{$lib} =~ m/^:(.*):$/;
224 @rassigned = sort {$a <=> $b} split(":", $1);
225 print STDERR
" @rassigned\n";
230 if ($rmax{$lib} >= 1000) {
231 print STDERR
"!! ERROR: SSL error codes 1000+ are reserved for alerts.\n";
232 print STDERR
"!! Any new alerts must be added to $config.\n";
239 # Scan each C source file and look for function and reason codes
240 # This is done by looking for strings that "look like" function or
241 # reason codes: basically anything consisting of all upper case and
242 # numerics which has _F_ or _R_ in it and which has the name of an
243 # error library at the start. This seems to work fine except for the
244 # oddly named structure BIO_F_CTX which needs to be ignored.
245 # If a code doesn't exist in list compiled from headers then mark it
246 # with the value "X" as a place holder to give it a value later.
247 # Store all function and reason codes found in %ufcodes and %urcodes
248 # so all those unreferenced can be printed out.
251 foreach $file (@source) {
252 # Don't parse the error source file.
253 next if exists $cskip{$file};
254 print STDERR
"File loaded: ".$file."\r" if $debug;
255 open(IN
, "<$file") || die "Can't open source file $file\n";
257 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
258 next unless exists $csrc{$2};
259 next if($1 eq "BIO_F_BUFFER_CTX");
261 if(!exists $fcodes{$1}) {
265 $notrans{$1} = 1 unless exists $ftrans{$3};
267 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
268 next unless exists $csrc{$2};
270 if(!exists $rcodes{$1}) {
278 print STDERR
" \n" if $debug;
280 # Now process each library in turn.
282 foreach $lib (keys %csrc)
284 my $hfile = $hinc{$lib};
285 my $cfile = $csrc{$lib};
286 if(!$fnew{$lib} && !$rnew{$lib}) {
287 print STDERR
"$lib:\t\tNo new error codes\n";
288 next unless $rebuild;
290 print STDERR
"$lib:\t\t$fnew{$lib} New Functions,";
291 print STDERR
" $rnew{$lib} New Reasons.\n";
292 next unless $dowrite;
295 # If we get here then we have some new error codes so we
296 # need to rebuild the header file and C file.
298 # Make a sorted list of error and reason codes for later use.
300 my @function = sort grep(/^${lib}_/,keys %fcodes);
301 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
303 # Rewrite the header file
305 if (open(IN
, "<$hfile")) {
306 # Copy across the old file
309 last if (/BEGIN ERROR CODES/);
314 "/* ====================================================================\n",
315 " * Copyright (c) 2001-2005 The OpenSSL Project. All rights reserved.\n",
317 " * Redistribution and use in source and binary forms, with or without\n",
318 " * modification, are permitted provided that the following conditions\n",
321 " * 1. Redistributions of source code must retain the above copyright\n",
322 " * notice, this list of conditions and the following disclaimer. \n",
324 " * 2. Redistributions in binary form must reproduce the above copyright\n",
325 " * notice, this list of conditions and the following disclaimer in\n",
326 " * the documentation and/or other materials provided with the\n",
327 " * distribution.\n",
329 " * 3. All advertising materials mentioning features or use of this\n",
330 " * software must display the following acknowledgment:\n",
331 " * \"This product includes software developed by the OpenSSL Project\n",
332 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
334 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
335 " * endorse or promote products derived from this software without\n",
336 " * prior written permission. For written permission, please contact\n",
337 " * openssl-core\@openssl.org.\n",
339 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
340 " * nor may \"OpenSSL\" appear in their names without prior written\n",
341 " * permission of the OpenSSL Project.\n",
343 " * 6. Redistributions of any form whatsoever must retain the following\n",
344 " * acknowledgment:\n",
345 " * \"This product includes software developed by the OpenSSL Project\n",
346 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
348 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
349 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
350 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
351 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
352 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
353 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
354 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
355 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
356 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
357 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
358 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
359 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
360 " * ====================================================================\n",
362 " * This product includes cryptographic software written by Eric Young\n",
363 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
364 " * Hudson (tjh\@cryptsoft.com).\n",
368 "#ifndef HEADER_${lib}_ERR_H\n",
369 "#define HEADER_${lib}_ERR_H\n",
371 "/* BEGIN ERROR CODES */\n";
373 open (OUT
, ">$hfile") || die "Can't Open File $hfile for writing\n";
378 /* The following lines are auto generated by the script mkerr.pl. Any changes
379 * made after this point may be overwritten when the script is next run.
384 ${staticloader}void ERR_load_${lib}_strings(void);
389 ${staticloader}void ERR_load_${lib}_strings(void);
390 ${staticloader}void ERR_unload_${lib}_strings(void);
391 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
392 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
397 /* Error codes for the $lib functions. */
399 /* Function codes. */
402 foreach $i (@function) {
403 $z=6-int(length($i)/8);
404 if($fcodes{$i} eq "X") {
405 $fassigned{$lib} =~ m/^:([^:]*):/;
407 if (!defined($findcode)) {
408 $findcode = $fmax{$lib};
410 while ($fassigned{$lib} =~ m/:$findcode:/) {
413 $fcodes{$i} = $findcode;
414 $fassigned{$lib} .= "$findcode:";
415 print STDERR
"New Function code $i\n" if $debug;
417 printf OUT
"#define $i%s $fcodes{$i}\n","\t" x
$z;
420 print OUT
"\n/* Reason codes. */\n";
422 foreach $i (@reasons) {
423 $z=6-int(length($i)/8);
424 if($rcodes{$i} eq "X") {
425 $rassigned{$lib} =~ m/^:([^:]*):/;
427 if (!defined($findcode)) {
428 $findcode = $rmax{$lib};
430 while ($rassigned{$lib} =~ m/:$findcode:/) {
433 $rcodes{$i} = $findcode;
434 $rassigned{$lib} .= "$findcode:";
435 print STDERR
"New Reason code $i\n" if $debug;
437 printf OUT
"#define $i%s $rcodes{$i}\n","\t" x
$z;
448 # Rewrite the C source file containing the error details.
450 # First, read any existing reason string definitions:
451 my %err_reason_strings;
452 if (open(IN
,"<$cfile")) {
454 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
455 $err_reason_strings{$1} = $2;
463 $hfile =~ /([^\/]+)$/;
464 $hincf = "<openssl/$1>";
466 $hincf = "\"$hfile\"";
469 # If static we know the error code at compile time so use it
470 # in error definitions.
474 $pack_errcode = "ERR_LIB_${lib}";
480 $load_errcode = "ERR_LIB_${lib}";
484 open (OUT
,">$cfile") || die "Can't open $cfile for writing";
488 /* ====================================================================
489 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
491 * Redistribution and use in source and binary forms, with or without
492 * modification, are permitted provided that the following conditions
495 * 1. Redistributions of source code must retain the above copyright
496 * notice, this list of conditions and the following disclaimer.
498 * 2. Redistributions in binary form must reproduce the above copyright
499 * notice, this list of conditions and the following disclaimer in
500 * the documentation and/or other materials provided with the
503 * 3. All advertising materials mentioning features or use of this
504 * software must display the following acknowledgment:
505 * "This product includes software developed by the OpenSSL Project
506 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
508 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
509 * endorse or promote products derived from this software without
510 * prior written permission. For written permission, please contact
511 * openssl-core\@OpenSSL.org.
513 * 5. Products derived from this software may not be called "OpenSSL"
514 * nor may "OpenSSL" appear in their names without prior written
515 * permission of the OpenSSL Project.
517 * 6. Redistributions of any form whatsoever must retain the following
519 * "This product includes software developed by the OpenSSL Project
520 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
522 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
523 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
524 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
525 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
526 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
527 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
528 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
529 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
530 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
531 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
532 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
533 * OF THE POSSIBILITY OF SUCH DAMAGE.
534 * ====================================================================
536 * This product includes cryptographic software written by Eric Young
537 * (eay\@cryptsoft.com). This product includes software written by Tim
538 * Hudson (tjh\@cryptsoft.com).
542 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
543 * made to it will be overwritten when the script next updates this file,
544 * only reason strings will be preserved.
548 #include <openssl/err.h>
551 /* BEGIN ERROR CODES */
552 #ifndef OPENSSL_NO_ERR
554 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
555 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
557 static ERR_STRING_DATA ${lib}_str_functs[]=
560 # Add each function code: if a function name is found then use it.
561 foreach $i (@function) {
563 $i =~ /^${lib}_F_(\S+)$/;
565 if(exists $ftrans{$fn}) {
568 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
569 print OUT
"{ERR_FUNC($i),\t\"$fn\"},\n";
575 static ERR_STRING_DATA ${lib}_str_reasons[]=
578 # Add each reason code.
579 foreach $i (@reasons) {
581 my $rstr = "ERR_REASON($i)";
583 if (exists $err_reason_strings{$i}) {
584 $rn = $err_reason_strings{$i};
586 $i =~ /^${lib}_R_(\S+)$/;
588 $rn =~ tr/_[A-Z]/ [a-z]/;
590 $nspc = 40 - length($rstr) unless length($rstr) > 40;
592 print OUT
"{${rstr}${nspc},\"$rn\"},\n";
601 ${staticloader}void ERR_load_${lib}_strings(void)
603 #ifndef OPENSSL_NO_ERR
605 if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL)
607 ERR_load_strings($load_errcode,${lib}_str_functs);
608 ERR_load_strings($load_errcode,${lib}_str_reasons);
620 #ifdef ${lib}_LIB_NAME
621 static ERR_STRING_DATA ${lib}_lib_name[]=
623 {0 ,${lib}_LIB_NAME},
629 static int ${lib}_lib_error_code=0;
630 static int ${lib}_error_init=1;
632 ${staticloader}void ERR_load_${lib}_strings(void)
634 if (${lib}_lib_error_code == 0)
635 ${lib}_lib_error_code=ERR_get_next_error_library();
637 if (${lib}_error_init)
640 #ifndef OPENSSL_NO_ERR
641 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
642 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
645 #ifdef ${lib}_LIB_NAME
646 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
647 ERR_load_strings(0,${lib}_lib_name);
652 ${staticloader}void ERR_unload_${lib}_strings(void)
654 if (${lib}_error_init == 0)
656 #ifndef OPENSSL_NO_ERR
657 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
658 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
661 #ifdef ${lib}_LIB_NAME
662 ERR_unload_strings(0,${lib}_lib_name);
668 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
670 if (${lib}_lib_error_code == 0)
671 ${lib}_lib_error_code=ERR_get_next_error_library();
672 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
679 undef %err_reason_strings;
682 if($debug && defined(%notrans)) {
683 print STDERR
"The following function codes were not translated:\n";
684 foreach(sort keys %notrans)
690 # Make a list of unreferenced function and reason codes
692 foreach (keys %fcodes) {
693 push (@funref, $_) unless exists $ufcodes{$_};
696 foreach (keys %rcodes) {
697 push (@runref, $_) unless exists $urcodes{$_};
700 if($debug && defined(@funref) ) {
701 print STDERR
"The following function codes were not referenced:\n";
702 foreach(sort @funref)
708 if($debug && defined(@runref) ) {
709 print STDERR
"The following reason codes were not referenced:\n";
710 foreach(sort @runref)