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+)/) {
71 } elsif (/^F\s+(\S+)/) {
72 # Add extra function with $1
73 } elsif (/^R\s+(\S+)\s+(\S+)/) {
81 # Scan each header file in turn and make a list of error codes
84 while (($hdr, $lib) = each %libinc)
86 next if($hdr eq "NONE");
87 print STDERR "Scanning header file $hdr\n" if $debug;
88 my $line = "", $def= "", $linenr = 0, $gotfile = 0;
89 if (open(IN, "<$hdr")) {
93 print STDERR "line: $linenr\r" if $debug;
95 last if(/BEGIN\s+ERROR\s+CODES/);
107 if (not /\*\//) { # multiline comment...
108 $line = $_; # ... just accumulate
111 s/\/\*.*?\*\///gs; # wipe it
117 $cpp-- if /^#\s*endif/;
120 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
122 next if (/^\#/); # skip preprocessor directives
124 s/{[^{}]*}//gs; # ignore {} blocks
126 if (/\{|\/\*/) { # Add a } so editor works...
134 print STDERR " \r" if $debug;
136 foreach (split /;/, $def) {
138 print STDERR "def: $defnr\r" if $debug;
140 # The goal is to collect function names from function declarations.
145 # Skip over recognized non-function declarations
146 next if(/typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/);
148 # Reduce argument lists to empty ()
149 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
151 s/\([^\(\)]+\)/\{\}/gs;
152 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
154 # pretend as we didn't use curly braces: {} -> ()
157 if (/(\w+)\s*\(\).*/s) { # first token prior [first] () is
158 my $name = $1; # a function name!
159 $name =~ tr/[a-z]/[A-Z]/;
161 } elsif (/[\(\)]/ and not (/=/)) {
162 print STDERR "Header $hdr: cannot parse: $_;\n";
166 print STDERR " \r" if $debug;
170 # Scan function and reason codes and store them: keep a note of the
175 if(/^\#define\s+(\S+)\s+(\S+)/) {
178 next if $name =~ /^${lib}err/;
179 unless($name =~ /^${lib}_([RF])_(\w+)$/) {
180 print STDERR "Invalid error code $name\n";
184 $rcodes{$name} = $code;
185 if(!(exists $rextra{$name}) &&
186 ($code > $rmax{$lib}) ) {
190 if($code > $fmax{$lib}) {
193 $fcodes{$name} = $code;
200 if (defined($rmax{$lib})) {
201 print STDERR "Max reason code rmax" . "{" . "$lib" . "} = $rmax{$lib}\n";
203 if (defined($fmax{$lib})) {
204 print STDERR "Max function code fmax" . "{" . "$lib" . "} = $fmax{$lib}\n";
209 if ($rmax{$lib} >= 1000) {
210 print STDERR "!! ERROR: SSL error codes 1000+ are reserved for alerts.\n";
211 print STDERR "!! Any new alerts must be added to $config.\n";
218 # Scan each C source file and look for function and reason codes
219 # This is done by looking for strings that "look like" function or
220 # reason codes: basically anything consisting of all upper case and
221 # numerics which has _F_ or _R_ in it and which has the name of an
222 # error library at the start. This seems to work fine except for the
223 # oddly named structure BIO_F_CTX which needs to be ignored.
224 # If a code doesn't exist in list compiled from headers then mark it
225 # with the value "X" as a place holder to give it a value later.
226 # Store all function and reason codes found in %ufcodes and %urcodes
227 # so all those unreferenced can be printed out.
230 foreach $file (@source) {
231 # Don't parse the error source file.
232 next if exists $cskip{$file};
233 print STDERR "File loaded: ".$file."\r" if $debug;
234 open(IN, "<$file") || die "Can't open source file $file\n";
236 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
237 next unless exists $csrc{$2};
238 next if($1 eq "BIO_F_BUFFER_CTX");
240 if(!exists $fcodes{$1}) {
244 $notrans{$1} = 1 unless exists $ftrans{$3};
246 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
247 next unless exists $csrc{$2};
249 if(!exists $rcodes{$1}) {
257 print STDERR " \n" if $debug;
259 # Now process each library in turn.
261 foreach $lib (keys %csrc)
263 my $hfile = $hinc{$lib};
264 my $cfile = $csrc{$lib};
265 if(!$fnew{$lib} && !$rnew{$lib}) {
266 print STDERR "$lib:\t\tNo new error codes\n";
267 next unless $rebuild;
269 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
270 print STDERR " $rnew{$lib} New Reasons.\n";
271 next unless $dowrite;
274 # If we get here then we have some new error codes so we
275 # need to rebuild the header file and C file.
277 # Make a sorted list of error and reason codes for later use.
279 my @function = sort grep(/^${lib}_/,keys %fcodes);
280 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
282 # Rewrite the header file
284 if (open(IN, "<$hfile")) {
285 # Copy across the old file
288 last if (/BEGIN ERROR CODES/);
293 "/* ====================================================================\n",
294 " * Copyright (c) 2001-2005 The OpenSSL Project. All rights reserved.\n",
296 " * Redistribution and use in source and binary forms, with or without\n",
297 " * modification, are permitted provided that the following conditions\n",
300 " * 1. Redistributions of source code must retain the above copyright\n",
301 " * notice, this list of conditions and the following disclaimer. \n",
303 " * 2. Redistributions in binary form must reproduce the above copyright\n",
304 " * notice, this list of conditions and the following disclaimer in\n",
305 " * the documentation and/or other materials provided with the\n",
306 " * distribution.\n",
308 " * 3. All advertising materials mentioning features or use of this\n",
309 " * software must display the following acknowledgment:\n",
310 " * \"This product includes software developed by the OpenSSL Project\n",
311 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
313 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
314 " * endorse or promote products derived from this software without\n",
315 " * prior written permission. For written permission, please contact\n",
316 " * openssl-core\@openssl.org.\n",
318 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
319 " * nor may \"OpenSSL\" appear in their names without prior written\n",
320 " * permission of the OpenSSL Project.\n",
322 " * 6. Redistributions of any form whatsoever must retain the following\n",
323 " * acknowledgment:\n",
324 " * \"This product includes software developed by the OpenSSL Project\n",
325 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
327 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
328 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
329 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
330 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
331 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
332 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
333 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
334 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
335 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
336 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
337 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
338 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
339 " * ====================================================================\n",
341 " * This product includes cryptographic software written by Eric Young\n",
342 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
343 " * Hudson (tjh\@cryptsoft.com).\n",
347 "#ifndef HEADER_${lib}_ERR_H\n",
348 "#define HEADER_${lib}_ERR_H\n",
350 "/* BEGIN ERROR CODES */\n";
352 open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
357 /* The following lines are auto generated by the script mkerr.pl. Any changes
358 * made after this point may be overwritten when the script is next run.
363 ${staticloader}void ERR_load_${lib}_strings(void);
368 ${staticloader}void ERR_load_${lib}_strings(void);
369 ${staticloader}void ERR_unload_${lib}_strings(void);
370 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
371 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
376 /* Error codes for the $lib functions. */
378 /* Function codes. */
381 foreach $i (@function) {
382 $z=6-int(length($i)/8);
383 if($fcodes{$i} eq "X") {
384 $fcodes{$i} = ++$fmax{$lib};
385 print STDERR "New Function code $i\n" if $debug;
387 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
390 print OUT "\n/* Reason codes. */\n";
392 foreach $i (@reasons) {
393 $z=6-int(length($i)/8);
394 if($rcodes{$i} eq "X") {
395 $rcodes{$i} = ++$rmax{$lib};
396 print STDERR "New Reason code $i\n" if $debug;
398 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
409 # Rewrite the C source file containing the error details.
411 # First, read any existing reason string definitions:
412 my %err_reason_strings;
413 if (open(IN,"<$cfile")) {
415 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
416 $err_reason_strings{$1} = $2;
424 $hfile =~ /([^\/]+)$/;
425 $hincf = "<openssl/$1>";
427 $hincf = "\"$hfile\"";
430 # If static we know the error code at compile time so use it
431 # in error definitions.
435 $pack_errcode = "ERR_LIB_${lib}";
441 $load_errcode = "ERR_LIB_${lib}";
445 open (OUT,">$cfile") || die "Can't open $cfile for writing";
449 /* ====================================================================
450 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
452 * Redistribution and use in source and binary forms, with or without
453 * modification, are permitted provided that the following conditions
456 * 1. Redistributions of source code must retain the above copyright
457 * notice, this list of conditions and the following disclaimer.
459 * 2. Redistributions in binary form must reproduce the above copyright
460 * notice, this list of conditions and the following disclaimer in
461 * the documentation and/or other materials provided with the
464 * 3. All advertising materials mentioning features or use of this
465 * software must display the following acknowledgment:
466 * "This product includes software developed by the OpenSSL Project
467 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
469 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
470 * endorse or promote products derived from this software without
471 * prior written permission. For written permission, please contact
472 * openssl-core\@OpenSSL.org.
474 * 5. Products derived from this software may not be called "OpenSSL"
475 * nor may "OpenSSL" appear in their names without prior written
476 * permission of the OpenSSL Project.
478 * 6. Redistributions of any form whatsoever must retain the following
480 * "This product includes software developed by the OpenSSL Project
481 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
483 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
484 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
485 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
486 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
487 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
488 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
489 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
490 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
491 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
492 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
493 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
494 * OF THE POSSIBILITY OF SUCH DAMAGE.
495 * ====================================================================
497 * This product includes cryptographic software written by Eric Young
498 * (eay\@cryptsoft.com). This product includes software written by Tim
499 * Hudson (tjh\@cryptsoft.com).
503 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
504 * made to it will be overwritten when the script next updates this file,
505 * only reason strings will be preserved.
509 #include <openssl/err.h>
512 /* BEGIN ERROR CODES */
513 #ifndef OPENSSL_NO_ERR
515 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
516 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
518 static ERR_STRING_DATA ${lib}_str_functs[]=
521 # Add each function code: if a function name is found then use it.
522 foreach $i (@function) {
524 $i =~ /^${lib}_F_(\S+)$/;
526 if(exists $ftrans{$fn}) {
529 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
530 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
536 static ERR_STRING_DATA ${lib}_str_reasons[]=
539 # Add each reason code.
540 foreach $i (@reasons) {
542 my $rstr = "ERR_REASON($i)";
544 if (exists $err_reason_strings{$i}) {
545 $rn = $err_reason_strings{$i};
547 $i =~ /^${lib}_R_(\S+)$/;
549 $rn =~ tr/_[A-Z]/ [a-z]/;
551 $nspc = 40 - length($rstr) unless length($rstr) > 40;
553 print OUT "{${rstr}${nspc},\"$rn\"},\n";
562 ${staticloader}void ERR_load_${lib}_strings(void)
569 #ifndef OPENSSL_NO_ERR
570 ERR_load_strings($load_errcode,${lib}_str_functs);
571 ERR_load_strings($load_errcode,${lib}_str_reasons);
584 #ifdef ${lib}_LIB_NAME
585 static ERR_STRING_DATA ${lib}_lib_name[]=
587 {0 ,${lib}_LIB_NAME},
593 static int ${lib}_lib_error_code=0;
594 static int ${lib}_error_init=1;
596 ${staticloader}void ERR_load_${lib}_strings(void)
598 if (${lib}_lib_error_code == 0)
599 ${lib}_lib_error_code=ERR_get_next_error_library();
601 if (${lib}_error_init)
604 #ifndef OPENSSL_NO_ERR
605 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
606 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
609 #ifdef ${lib}_LIB_NAME
610 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
611 ERR_load_strings(0,${lib}_lib_name);
616 ${staticloader}void ERR_unload_${lib}_strings(void)
618 if (${lib}_error_init == 0)
620 #ifndef OPENSSL_NO_ERR
621 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
622 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
625 #ifdef ${lib}_LIB_NAME
626 ERR_unload_strings(0,${lib}_lib_name);
632 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
634 if (${lib}_lib_error_code == 0)
635 ${lib}_lib_error_code=ERR_get_next_error_library();
636 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
643 undef %err_reason_strings;
646 if($debug && defined(%notrans)) {
647 print STDERR "The following function codes were not translated:\n";
648 foreach(sort keys %notrans)
654 # Make a list of unreferenced function and reason codes
656 foreach (keys %fcodes) {
657 push (@funref, $_) unless exists $ufcodes{$_};
660 foreach (keys %rcodes) {
661 push (@runref, $_) unless exists $urcodes{$_};
664 if($debug && defined(@funref) ) {
665 print STDERR "The following function codes were not referenced:\n";
666 foreach(sort @funref)
672 if($debug && defined(@runref) ) {
673 print STDERR "The following reason codes were not referenced:\n";
674 foreach(sort @runref)