1 #!/usr/local/bin/perl -w
3 my $config = "crypto/err/openssl.ec";
4 my $hprefix = "openssl/";
11 my $staticloader = "";
22 $config = shift @ARGV;
23 } elsif($arg eq "-hprefix") {
25 $hprefix = shift @ARGV;
26 } elsif($arg eq "-debug") {
29 } elsif($arg eq "-rebuild") {
32 } elsif($arg eq "-recurse") {
35 } elsif($arg eq "-reindex") {
38 } elsif($arg eq "-nostatic") {
41 } elsif($arg eq "-staticloader") {
42 $staticloader = "static ";
44 } elsif($arg eq "-write") {
53 @source = (<crypto/*.c>, <crypto/*/*.c>, <ssl/*.c>);
58 # Read in the config file
60 open(IN, "<$config") || die "Can't open config file $config";
66 if(/^L\s+(\S+)\s+(\S+)\s+(\S+)/) {
79 } elsif (/^F\s+(\S+)/) {
80 # Add extra function with $1
81 } elsif (/^R\s+(\S+)\s+(\S+)/) {
89 # Scan each header file in turn and make a list of error codes
92 while (($hdr, $lib) = each %libinc)
94 next if($hdr eq "NONE");
95 print STDERR "Scanning header file $hdr\n" if $debug;
96 my $line = "", $def= "", $linenr = 0, $gotfile = 0;
97 if (open(IN, "<$hdr")) {
101 print STDERR "line: $linenr\r" if $debug;
103 last if(/BEGIN\s+ERROR\s+CODES/);
115 if (not /\*\//) { # multiline comment...
116 $line = $_; # ... just accumulate
119 s/\/\*.*?\*\///gs; # wipe it
125 $cpp-- if /^#\s*endif/;
128 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
130 next if (/^\#/); # skip preprocessor directives
132 s/{[^{}]*}//gs; # ignore {} blocks
134 if (/\{|\/\*/) { # Add a } so editor works...
142 print STDERR " \r" if $debug;
144 # Delete any DECLARE_ macros
145 $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
146 foreach (split /;/, $def) {
148 print STDERR "def: $defnr\r" if $debug;
150 # The goal is to collect function names from function declarations.
155 # Skip over recognized non-function declarations
156 next if(/typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/);
158 # Remove STACK_OF(foo)
159 s/STACK_OF\(\w+\)/void/;
161 # Reduce argument lists to empty ()
162 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
164 s/\([^\(\)]+\)/\{\}/gs;
165 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
167 # pretend as we didn't use curly braces: {} -> ()
170 if (/(\w+)\s*\(\).*/s) { # first token prior [first] () is
171 my $name = $1; # a function name!
172 $name =~ tr/[a-z]/[A-Z]/;
174 } elsif (/[\(\)]/ and not (/=/)) {
175 print STDERR "Header $hdr: cannot parse: $_;\n";
179 print STDERR " \r" if $debug;
183 # Scan function and reason codes and store them: keep a note of the
188 if(/^\#define\s+(\S+)\s+(\S+)/) {
191 next if $name =~ /^${lib}err/;
192 unless($name =~ /^${lib}_([RF])_(\w+)$/) {
193 print STDERR "Invalid error code $name\n";
197 $rcodes{$name} = $code;
198 if ($rassigned{$lib} =~ /:$code:/) {
199 print STDERR "!! ERROR: $lib reason code $code assigned twice\n";
202 $rassigned{$lib} .= "$code:";
203 if(!(exists $rextra{$name}) &&
204 ($code > $rmax{$lib}) ) {
208 if ($fassigned{$lib} =~ /:$code:/) {
209 print STDERR "!! ERROR: $lib function code $code assigned twice\n";
212 $fassigned{$lib} .= "$code:";
213 if($code > $fmax{$lib}) {
216 $fcodes{$name} = $code;
223 if (defined($fmax{$lib})) {
224 print STDERR "Max function code fmax" . "{" . "$lib" . "} = $fmax{$lib}\n";
225 $fassigned{$lib} =~ m/^:(.*):$/;
226 @fassigned = sort {$a <=> $b} split(":", $1);
227 print STDERR " @fassigned\n";
229 if (defined($rmax{$lib})) {
230 print STDERR "Max reason code rmax" . "{" . "$lib" . "} = $rmax{$lib}\n";
231 $rassigned{$lib} =~ m/^:(.*):$/;
232 @rassigned = sort {$a <=> $b} split(":", $1);
233 print STDERR " @rassigned\n";
238 if ($rmax{$lib} >= 1000) {
239 print STDERR "!! ERROR: SSL error codes 1000+ are reserved for alerts.\n";
240 print STDERR "!! Any new alerts must be added to $config.\n";
248 # Scan each C source file and look for function and reason codes
249 # This is done by looking for strings that "look like" function or
250 # reason codes: basically anything consisting of all upper case and
251 # numerics which has _F_ or _R_ in it and which has the name of an
252 # error library at the start. This seems to work fine except for the
253 # oddly named structure BIO_F_CTX which needs to be ignored.
254 # If a code doesn't exist in list compiled from headers then mark it
255 # with the value "X" as a place holder to give it a value later.
256 # Store all function and reason codes found in %ufcodes and %urcodes
257 # so all those unreferenced can be printed out.
260 foreach $file (@source) {
261 # Don't parse the error source file.
262 next if exists $cskip{$file};
263 print STDERR "File loaded: ".$file."\r" if $debug;
264 open(IN, "<$file") || die "Can't open source file $file\n";
266 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
267 next unless exists $csrc{$2};
268 next if($1 eq "BIO_F_BUFFER_CTX");
270 if(!exists $fcodes{$1}) {
274 $notrans{$1} = 1 unless exists $ftrans{$3};
276 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
277 next unless exists $csrc{$2};
279 if(!exists $rcodes{$1}) {
287 print STDERR " \n" if $debug;
289 # Now process each library in turn.
291 foreach $lib (keys %csrc)
293 my $hfile = $hinc{$lib};
294 my $cfile = $csrc{$lib};
295 if(!$fnew{$lib} && !$rnew{$lib}) {
296 print STDERR "$lib:\t\tNo new error codes\n";
297 next unless $rebuild;
299 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
300 print STDERR " $rnew{$lib} New Reasons.\n";
301 next unless $dowrite;
304 # If we get here then we have some new error codes so we
305 # need to rebuild the header file and C file.
307 # Make a sorted list of error and reason codes for later use.
309 my @function = sort grep(/^${lib}_/,keys %fcodes);
310 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
312 # Rewrite the header file
314 if (open(IN, "<$hfile")) {
315 # Copy across the old file
318 last if (/BEGIN ERROR CODES/);
323 "/* ====================================================================\n",
324 " * Copyright (c) 2001-2008 The OpenSSL Project. All rights reserved.\n",
326 " * Redistribution and use in source and binary forms, with or without\n",
327 " * modification, are permitted provided that the following conditions\n",
330 " * 1. Redistributions of source code must retain the above copyright\n",
331 " * notice, this list of conditions and the following disclaimer. \n",
333 " * 2. Redistributions in binary form must reproduce the above copyright\n",
334 " * notice, this list of conditions and the following disclaimer in\n",
335 " * the documentation and/or other materials provided with the\n",
336 " * distribution.\n",
338 " * 3. All advertising materials mentioning features or use of this\n",
339 " * software must display the following acknowledgment:\n",
340 " * \"This product includes software developed by the OpenSSL Project\n",
341 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
343 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
344 " * endorse or promote products derived from this software without\n",
345 " * prior written permission. For written permission, please contact\n",
346 " * openssl-core\@openssl.org.\n",
348 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
349 " * nor may \"OpenSSL\" appear in their names without prior written\n",
350 " * permission of the OpenSSL Project.\n",
352 " * 6. Redistributions of any form whatsoever must retain the following\n",
353 " * acknowledgment:\n",
354 " * \"This product includes software developed by the OpenSSL Project\n",
355 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
357 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
358 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
359 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
360 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
361 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
362 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
363 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
364 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
365 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
366 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
367 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
368 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
369 " * ====================================================================\n",
371 " * This product includes cryptographic software written by Eric Young\n",
372 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
373 " * Hudson (tjh\@cryptsoft.com).\n",
377 "#ifndef HEADER_${lib}_ERR_H\n",
378 "#define HEADER_${lib}_ERR_H\n",
380 "#ifdef __cplusplus\n",
384 "/* BEGIN ERROR CODES */\n";
386 open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
391 /* The following lines are auto generated by the script mkerr.pl. Any changes
392 * made after this point may be overwritten when the script is next run.
397 ${staticloader}void ERR_load_${lib}_strings(void);
402 ${staticloader}void ERR_load_${lib}_strings(void);
403 ${staticloader}void ERR_unload_${lib}_strings(void);
404 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
405 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
410 /* Error codes for the $lib functions. */
412 /* Function codes. */
415 foreach $i (@function) {
416 $z=6-int(length($i)/8);
417 if($fcodes{$i} eq "X") {
418 $fassigned{$lib} =~ m/^:([^:]*):/;
420 if (!defined($findcode)) {
421 $findcode = $fmax{$lib};
423 while ($fassigned{$lib} =~ m/:$findcode:/) {
426 $fcodes{$i} = $findcode;
427 $fassigned{$lib} .= "$findcode:";
428 print STDERR "New Function code $i\n" if $debug;
430 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
433 print OUT "\n/* Reason codes. */\n";
435 foreach $i (@reasons) {
436 $z=6-int(length($i)/8);
437 if($rcodes{$i} eq "X") {
438 $rassigned{$lib} =~ m/^:([^:]*):/;
440 if (!defined($findcode)) {
441 $findcode = $rmax{$lib};
443 while ($rassigned{$lib} =~ m/:$findcode:/) {
446 $rcodes{$i} = $findcode;
447 $rassigned{$lib} .= "$findcode:";
448 print STDERR "New Reason code $i\n" if $debug;
450 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
461 # Rewrite the C source file containing the error details.
463 # First, read any existing reason string definitions:
464 my %err_reason_strings;
465 if (open(IN,"<$cfile")) {
467 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
468 $err_reason_strings{$1} = $2;
470 if (/\b${lib}_F_(\w*)\b.*\"(.*)\"/) {
471 if (!exists $ftrans{$1} && ($1 ne $2)) {
472 print STDERR "WARNING: Mismatched function string $2\n";
483 $hfile =~ /([^\/]+)$/;
484 $hincf = "<${hprefix}$1>";
486 $hincf = "\"$hfile\"";
489 # If static we know the error code at compile time so use it
490 # in error definitions.
494 $pack_errcode = "ERR_LIB_${lib}";
500 $load_errcode = "ERR_LIB_${lib}";
504 open (OUT,">$cfile") || die "Can't open $cfile for writing";
508 /* ====================================================================
509 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
511 * Redistribution and use in source and binary forms, with or without
512 * modification, are permitted provided that the following conditions
515 * 1. Redistributions of source code must retain the above copyright
516 * notice, this list of conditions and the following disclaimer.
518 * 2. Redistributions in binary form must reproduce the above copyright
519 * notice, this list of conditions and the following disclaimer in
520 * the documentation and/or other materials provided with the
523 * 3. All advertising materials mentioning features or use of this
524 * software must display the following acknowledgment:
525 * "This product includes software developed by the OpenSSL Project
526 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
528 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
529 * endorse or promote products derived from this software without
530 * prior written permission. For written permission, please contact
531 * openssl-core\@OpenSSL.org.
533 * 5. Products derived from this software may not be called "OpenSSL"
534 * nor may "OpenSSL" appear in their names without prior written
535 * permission of the OpenSSL Project.
537 * 6. Redistributions of any form whatsoever must retain the following
539 * "This product includes software developed by the OpenSSL Project
540 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
542 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
543 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
544 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
545 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
546 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
547 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
548 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
549 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
550 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
551 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
552 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
553 * OF THE POSSIBILITY OF SUCH DAMAGE.
554 * ====================================================================
556 * This product includes cryptographic software written by Eric Young
557 * (eay\@cryptsoft.com). This product includes software written by Tim
558 * Hudson (tjh\@cryptsoft.com).
562 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
563 * made to it will be overwritten when the script next updates this file,
564 * only reason strings will be preserved.
568 #include <openssl/err.h>
571 /* BEGIN ERROR CODES */
572 #ifndef OPENSSL_NO_ERR
574 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
575 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
577 static ERR_STRING_DATA ${lib}_str_functs[]=
580 # Add each function code: if a function name is found then use it.
581 foreach $i (@function) {
583 $i =~ /^${lib}_F_(\S+)$/;
585 if(exists $ftrans{$fn}) {
588 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
589 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
595 static ERR_STRING_DATA ${lib}_str_reasons[]=
598 # Add each reason code.
599 foreach $i (@reasons) {
601 my $rstr = "ERR_REASON($i)";
603 if (exists $err_reason_strings{$i}) {
604 $rn = $err_reason_strings{$i};
606 $i =~ /^${lib}_R_(\S+)$/;
608 $rn =~ tr/_[A-Z]/ [a-z]/;
610 $nspc = 40 - length($rstr) unless length($rstr) > 40;
612 print OUT "{${rstr}${nspc},\"$rn\"},\n";
621 ${staticloader}void ERR_load_${lib}_strings(void)
623 #ifndef OPENSSL_NO_ERR
625 if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL)
627 ERR_load_strings($load_errcode,${lib}_str_functs);
628 ERR_load_strings($load_errcode,${lib}_str_reasons);
640 #ifdef ${lib}_LIB_NAME
641 static ERR_STRING_DATA ${lib}_lib_name[]=
643 {0 ,${lib}_LIB_NAME},
649 static int ${lib}_lib_error_code=0;
650 static int ${lib}_error_init=1;
652 ${staticloader}void ERR_load_${lib}_strings(void)
654 if (${lib}_lib_error_code == 0)
655 ${lib}_lib_error_code=ERR_get_next_error_library();
657 if (${lib}_error_init)
660 #ifndef OPENSSL_NO_ERR
661 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
662 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
665 #ifdef ${lib}_LIB_NAME
666 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
667 ERR_load_strings(0,${lib}_lib_name);
672 ${staticloader}void ERR_unload_${lib}_strings(void)
674 if (${lib}_error_init == 0)
676 #ifndef OPENSSL_NO_ERR
677 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
678 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
681 #ifdef ${lib}_LIB_NAME
682 ERR_unload_strings(0,${lib}_lib_name);
688 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
690 if (${lib}_lib_error_code == 0)
691 ${lib}_lib_error_code=ERR_get_next_error_library();
692 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
699 undef %err_reason_strings;
702 if($debug && defined(%notrans)) {
703 print STDERR "The following function codes were not translated:\n";
704 foreach(sort keys %notrans)
710 # Make a list of unreferenced function and reason codes
712 foreach (keys %fcodes) {
713 push (@funref, $_) unless exists $ufcodes{$_};
716 foreach (keys %rcodes) {
717 push (@runref, $_) unless exists $urcodes{$_};
720 if($debug && defined(@funref) ) {
721 print STDERR "The following function codes were not referenced:\n";
722 foreach(sort @funref)
728 if($debug && defined(@runref) ) {
729 print STDERR "The following reason codes were not referenced:\n";
730 foreach(sort @runref)
737 print STDERR "There were errors, failing...\n\n";