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 /struct\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) -> {}
150 while(/\(.*\)/s) { s/\([^\(\)]+\)/\{\}/gs; }
151 # pretend as we didn't use curly braces: {} -> ()
154 if (/(\w+)\s*\(\).*/s) { # first token prior [first] () is
155 my $name = $1; # a function name!
156 $name =~ tr/[a-z]/[A-Z]/;
158 } elsif (/[\(\)]/ and not (/=/)) {
159 print STDERR "Header $hdr: cannot parse: $_;\n";
163 print STDERR " \r" if $debug;
167 # Scan function and reason codes and store them: keep a note of the
172 if(/^\#define\s+(\S+)\s+(\S+)/) {
175 next if $name =~ /^${lib}err/;
176 unless($name =~ /^${lib}_([RF])_(\w+)$/) {
177 print STDERR "Invalid error code $name\n";
181 $rcodes{$name} = $code;
182 if(!(exists $rextra{$name}) &&
183 ($code > $rmax{$lib}) ) {
187 if($code > $fmax{$lib}) {
190 $fcodes{$name} = $code;
198 # Scan each C source file and look for function and reason codes
199 # This is done by looking for strings that "look like" function or
200 # reason codes: basically anything consisting of all upper case and
201 # numerics which has _F_ or _R_ in it and which has the name of an
202 # error library at the start. This seems to work fine except for the
203 # oddly named structure BIO_F_CTX which needs to be ignored.
204 # If a code doesn't exist in list compiled from headers then mark it
205 # with the value "X" as a place holder to give it a value later.
206 # Store all function and reason codes found in %ufcodes and %urcodes
207 # so all those unreferenced can be printed out.
210 foreach $file (@source) {
211 # Don't parse the error source file.
212 next if exists $cskip{$file};
213 print STDERR "File loaded: ".$file."\r" if $debug;
214 open(IN, "<$file") || die "Can't open source file $file\n";
216 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
217 next unless exists $csrc{$2};
218 next if($1 eq "BIO_F_BUFFER_CTX");
220 if(!exists $fcodes{$1}) {
224 $notrans{$1} = 1 unless exists $ftrans{$3};
226 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
227 next unless exists $csrc{$2};
229 if(!exists $rcodes{$1}) {
237 print STDERR "\n" if $debug;
239 # Now process each library in turn.
241 foreach $lib (keys %csrc)
243 my $hfile = $hinc{$lib};
244 my $cfile = $csrc{$lib};
245 if(!$fnew{$lib} && !$rnew{$lib}) {
246 print STDERR "$lib:\t\tNo new error codes\n";
247 next unless $rebuild;
249 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
250 print STDERR " $rnew{$lib} New Reasons.\n";
251 next unless $dowrite;
254 # If we get here then we have some new error codes so we
255 # need to rebuild the header file and C file.
257 # Make a sorted list of error and reason codes for later use.
259 my @function = sort grep(/^${lib}_/,keys %fcodes);
260 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
262 # Rewrite the header file
264 if (open(IN, "<$hfile")) {
265 # Copy across the old file
268 last if (/BEGIN ERROR CODES/);
273 "/* ====================================================================\n",
274 " * Copyright (c) 2001-2005 The OpenSSL Project. All rights reserved.\n",
276 " * Redistribution and use in source and binary forms, with or without\n",
277 " * modification, are permitted provided that the following conditions\n",
280 " * 1. Redistributions of source code must retain the above copyright\n",
281 " * notice, this list of conditions and the following disclaimer. \n",
283 " * 2. Redistributions in binary form must reproduce the above copyright\n",
284 " * notice, this list of conditions and the following disclaimer in\n",
285 " * the documentation and/or other materials provided with the\n",
286 " * distribution.\n",
288 " * 3. All advertising materials mentioning features or use of this\n",
289 " * software must display the following acknowledgment:\n",
290 " * \"This product includes software developed by the OpenSSL Project\n",
291 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
293 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
294 " * endorse or promote products derived from this software without\n",
295 " * prior written permission. For written permission, please contact\n",
296 " * openssl-core\@openssl.org.\n",
298 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
299 " * nor may \"OpenSSL\" appear in their names without prior written\n",
300 " * permission of the OpenSSL Project.\n",
302 " * 6. Redistributions of any form whatsoever must retain the following\n",
303 " * acknowledgment:\n",
304 " * \"This product includes software developed by the OpenSSL Project\n",
305 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
307 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
308 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
309 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
310 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
311 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
312 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
313 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
314 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
315 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
316 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
317 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
318 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
319 " * ====================================================================\n",
321 " * This product includes cryptographic software written by Eric Young\n",
322 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
323 " * Hudson (tjh\@cryptsoft.com).\n",
327 "#ifndef HEADER_${lib}_ERR_H\n",
328 "#define HEADER_${lib}_ERR_H\n",
330 "/* BEGIN ERROR CODES */\n";
332 open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
337 /* The following lines are auto generated by the script mkerr.pl. Any changes
338 * made after this point may be overwritten when the script is next run.
343 ${staticloader}void ERR_load_${lib}_strings(void);
348 ${staticloader}void ERR_load_${lib}_strings(void);
349 ${staticloader}void ERR_unload_${lib}_strings(void);
350 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
351 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
356 /* Error codes for the $lib functions. */
358 /* Function codes. */
361 foreach $i (@function) {
362 $z=6-int(length($i)/8);
363 if($fcodes{$i} eq "X") {
364 $fcodes{$i} = ++$fmax{$lib};
365 print STDERR "New Function code $i\n" if $debug;
367 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
370 print OUT "\n/* Reason codes. */\n";
372 foreach $i (@reasons) {
373 $z=6-int(length($i)/8);
374 if($rcodes{$i} eq "X") {
375 $rcodes{$i} = ++$rmax{$lib};
376 print STDERR "New Reason code $i\n" if $debug;
378 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
389 # Rewrite the C source file containing the error details.
391 # First, read any existing reason string definitions:
392 my %err_reason_strings;
393 if (open(IN,"<$cfile")) {
395 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
396 $err_reason_strings{$1} = $2;
404 $hfile =~ /([^\/]+)$/;
405 $hincf = "<openssl/$1>";
407 $hincf = "\"$hfile\"";
410 # If static we know the error code at compile time so use it
411 # in error definitions.
415 $pack_errcode = "ERR_LIB_${lib}";
421 $load_errcode = "ERR_LIB_${lib}";
425 open (OUT,">$cfile") || die "Can't open $cfile for writing";
429 /* ====================================================================
430 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
432 * Redistribution and use in source and binary forms, with or without
433 * modification, are permitted provided that the following conditions
436 * 1. Redistributions of source code must retain the above copyright
437 * notice, this list of conditions and the following disclaimer.
439 * 2. Redistributions in binary form must reproduce the above copyright
440 * notice, this list of conditions and the following disclaimer in
441 * the documentation and/or other materials provided with the
444 * 3. All advertising materials mentioning features or use of this
445 * software must display the following acknowledgment:
446 * "This product includes software developed by the OpenSSL Project
447 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
449 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
450 * endorse or promote products derived from this software without
451 * prior written permission. For written permission, please contact
452 * openssl-core\@OpenSSL.org.
454 * 5. Products derived from this software may not be called "OpenSSL"
455 * nor may "OpenSSL" appear in their names without prior written
456 * permission of the OpenSSL Project.
458 * 6. Redistributions of any form whatsoever must retain the following
460 * "This product includes software developed by the OpenSSL Project
461 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
463 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
464 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
465 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
466 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
467 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
468 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
469 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
470 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
471 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
472 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
473 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
474 * OF THE POSSIBILITY OF SUCH DAMAGE.
475 * ====================================================================
477 * This product includes cryptographic software written by Eric Young
478 * (eay\@cryptsoft.com). This product includes software written by Tim
479 * Hudson (tjh\@cryptsoft.com).
483 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
484 * made to it will be overwritten when the script next updates this file,
485 * only reason strings will be preserved.
489 #include <openssl/err.h>
492 /* BEGIN ERROR CODES */
493 #ifndef OPENSSL_NO_ERR
495 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
496 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
498 static ERR_STRING_DATA ${lib}_str_functs[]=
501 # Add each function code: if a function name is found then use it.
502 foreach $i (@function) {
504 $i =~ /^${lib}_F_(\S+)$/;
506 if(exists $ftrans{$fn}) {
509 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
510 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
516 static ERR_STRING_DATA ${lib}_str_reasons[]=
519 # Add each reason code.
520 foreach $i (@reasons) {
522 my $rstr = "ERR_REASON($i)";
524 if (exists $err_reason_strings{$i}) {
525 $rn = $err_reason_strings{$i};
527 $i =~ /^${lib}_R_(\S+)$/;
529 $rn =~ tr/_[A-Z]/ [a-z]/;
531 $nspc = 40 - length($rstr) unless length($rstr) > 40;
533 print OUT "{${rstr}${nspc},\"$rn\"},\n";
542 ${staticloader}void ERR_load_${lib}_strings(void)
549 #ifndef OPENSSL_NO_ERR
550 ERR_load_strings($load_errcode,${lib}_str_functs);
551 ERR_load_strings($load_errcode,${lib}_str_reasons);
564 #ifdef ${lib}_LIB_NAME
565 static ERR_STRING_DATA ${lib}_lib_name[]=
567 {0 ,${lib}_LIB_NAME},
573 static int ${lib}_lib_error_code=0;
574 static int ${lib}_error_init=1;
576 ${staticloader}void ERR_load_${lib}_strings(void)
578 if (${lib}_lib_error_code == 0)
579 ${lib}_lib_error_code=ERR_get_next_error_library();
581 if (${lib}_error_init)
584 #ifndef OPENSSL_NO_ERR
585 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
586 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
589 #ifdef ${lib}_LIB_NAME
590 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
591 ERR_load_strings(0,${lib}_lib_name);
596 ${staticloader}void ERR_unload_${lib}_strings(void)
598 if (${lib}_error_init == 0)
600 #ifndef OPENSSL_NO_ERR
601 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
602 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
605 #ifdef ${lib}_LIB_NAME
606 ERR_unload_strings(0,${lib}_lib_name);
612 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
614 if (${lib}_lib_error_code == 0)
615 ${lib}_lib_error_code=ERR_get_next_error_library();
616 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
623 undef %err_reason_strings;
626 if($debug && defined(%notrans)) {
627 print STDERR "The following function codes were not translated:\n";
628 foreach(sort keys %notrans)
634 # Make a list of unreferenced function and reason codes
636 foreach (keys %fcodes) {
637 push (@funref, $_) unless exists $ufcodes{$_};
640 foreach (keys %rcodes) {
641 push (@runref, $_) unless exists $urcodes{$_};
644 if($debug && defined(@funref) ) {
645 print STDERR "The following function codes were not referenced:\n";
646 foreach(sort @funref)
652 if($debug && defined(@runref) ) {
653 print STDERR "The following reason codes were not referenced:\n";
654 foreach(sort @runref)