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";
211 # Scan each C source file and look for function and reason codes
212 # This is done by looking for strings that "look like" function or
213 # reason codes: basically anything consisting of all upper case and
214 # numerics which has _F_ or _R_ in it and which has the name of an
215 # error library at the start. This seems to work fine except for the
216 # oddly named structure BIO_F_CTX which needs to be ignored.
217 # If a code doesn't exist in list compiled from headers then mark it
218 # with the value "X" as a place holder to give it a value later.
219 # Store all function and reason codes found in %ufcodes and %urcodes
220 # so all those unreferenced can be printed out.
223 foreach $file (@source) {
224 # Don't parse the error source file.
225 next if exists $cskip{$file};
226 print STDERR "File loaded: ".$file."\r" if $debug;
227 open(IN, "<$file") || die "Can't open source file $file\n";
229 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
230 next unless exists $csrc{$2};
231 next if($1 eq "BIO_F_BUFFER_CTX");
233 if(!exists $fcodes{$1}) {
237 $notrans{$1} = 1 unless exists $ftrans{$3};
239 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
240 next unless exists $csrc{$2};
242 if(!exists $rcodes{$1}) {
250 print STDERR " \n" if $debug;
252 # Now process each library in turn.
254 foreach $lib (keys %csrc)
256 my $hfile = $hinc{$lib};
257 my $cfile = $csrc{$lib};
258 if(!$fnew{$lib} && !$rnew{$lib}) {
259 print STDERR "$lib:\t\tNo new error codes\n";
260 next unless $rebuild;
262 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
263 print STDERR " $rnew{$lib} New Reasons.\n";
264 next unless $dowrite;
267 # If we get here then we have some new error codes so we
268 # need to rebuild the header file and C file.
270 # Make a sorted list of error and reason codes for later use.
272 my @function = sort grep(/^${lib}_/,keys %fcodes);
273 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
275 # Rewrite the header file
277 if (open(IN, "<$hfile")) {
278 # Copy across the old file
281 last if (/BEGIN ERROR CODES/);
286 "/* ====================================================================\n",
287 " * Copyright (c) 2001-2005 The OpenSSL Project. All rights reserved.\n",
289 " * Redistribution and use in source and binary forms, with or without\n",
290 " * modification, are permitted provided that the following conditions\n",
293 " * 1. Redistributions of source code must retain the above copyright\n",
294 " * notice, this list of conditions and the following disclaimer. \n",
296 " * 2. Redistributions in binary form must reproduce the above copyright\n",
297 " * notice, this list of conditions and the following disclaimer in\n",
298 " * the documentation and/or other materials provided with the\n",
299 " * distribution.\n",
301 " * 3. All advertising materials mentioning features or use of this\n",
302 " * software must display the following acknowledgment:\n",
303 " * \"This product includes software developed by the OpenSSL Project\n",
304 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
306 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
307 " * endorse or promote products derived from this software without\n",
308 " * prior written permission. For written permission, please contact\n",
309 " * openssl-core\@openssl.org.\n",
311 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
312 " * nor may \"OpenSSL\" appear in their names without prior written\n",
313 " * permission of the OpenSSL Project.\n",
315 " * 6. Redistributions of any form whatsoever must retain the following\n",
316 " * acknowledgment:\n",
317 " * \"This product includes software developed by the OpenSSL Project\n",
318 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
320 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
321 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
322 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
323 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
324 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
325 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
326 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
327 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
328 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
329 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
330 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
331 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
332 " * ====================================================================\n",
334 " * This product includes cryptographic software written by Eric Young\n",
335 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
336 " * Hudson (tjh\@cryptsoft.com).\n",
340 "#ifndef HEADER_${lib}_ERR_H\n",
341 "#define HEADER_${lib}_ERR_H\n",
343 "/* BEGIN ERROR CODES */\n";
345 open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
350 /* The following lines are auto generated by the script mkerr.pl. Any changes
351 * made after this point may be overwritten when the script is next run.
356 ${staticloader}void ERR_load_${lib}_strings(void);
361 ${staticloader}void ERR_load_${lib}_strings(void);
362 ${staticloader}void ERR_unload_${lib}_strings(void);
363 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
364 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
369 /* Error codes for the $lib functions. */
371 /* Function codes. */
374 foreach $i (@function) {
375 $z=6-int(length($i)/8);
376 if($fcodes{$i} eq "X") {
377 $fcodes{$i} = ++$fmax{$lib};
378 print STDERR "New Function code $i\n" if $debug;
380 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
383 print OUT "\n/* Reason codes. */\n";
385 foreach $i (@reasons) {
386 $z=6-int(length($i)/8);
387 if($rcodes{$i} eq "X") {
388 $rcodes{$i} = ++$rmax{$lib};
389 print STDERR "New Reason code $i\n" if $debug;
391 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
402 # Rewrite the C source file containing the error details.
404 # First, read any existing reason string definitions:
405 my %err_reason_strings;
406 if (open(IN,"<$cfile")) {
408 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
409 $err_reason_strings{$1} = $2;
417 $hfile =~ /([^\/]+)$/;
418 $hincf = "<openssl/$1>";
420 $hincf = "\"$hfile\"";
423 # If static we know the error code at compile time so use it
424 # in error definitions.
428 $pack_errcode = "ERR_LIB_${lib}";
434 $load_errcode = "ERR_LIB_${lib}";
438 open (OUT,">$cfile") || die "Can't open $cfile for writing";
442 /* ====================================================================
443 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
445 * Redistribution and use in source and binary forms, with or without
446 * modification, are permitted provided that the following conditions
449 * 1. Redistributions of source code must retain the above copyright
450 * notice, this list of conditions and the following disclaimer.
452 * 2. Redistributions in binary form must reproduce the above copyright
453 * notice, this list of conditions and the following disclaimer in
454 * the documentation and/or other materials provided with the
457 * 3. All advertising materials mentioning features or use of this
458 * software must display the following acknowledgment:
459 * "This product includes software developed by the OpenSSL Project
460 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
462 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
463 * endorse or promote products derived from this software without
464 * prior written permission. For written permission, please contact
465 * openssl-core\@OpenSSL.org.
467 * 5. Products derived from this software may not be called "OpenSSL"
468 * nor may "OpenSSL" appear in their names without prior written
469 * permission of the OpenSSL Project.
471 * 6. Redistributions of any form whatsoever must retain the following
473 * "This product includes software developed by the OpenSSL Project
474 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
476 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
477 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
478 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
479 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
480 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
481 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
482 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
483 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
484 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
485 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
486 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
487 * OF THE POSSIBILITY OF SUCH DAMAGE.
488 * ====================================================================
490 * This product includes cryptographic software written by Eric Young
491 * (eay\@cryptsoft.com). This product includes software written by Tim
492 * Hudson (tjh\@cryptsoft.com).
496 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
497 * made to it will be overwritten when the script next updates this file,
498 * only reason strings will be preserved.
502 #include <openssl/err.h>
505 /* BEGIN ERROR CODES */
506 #ifndef OPENSSL_NO_ERR
508 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
509 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
511 static ERR_STRING_DATA ${lib}_str_functs[]=
514 # Add each function code: if a function name is found then use it.
515 foreach $i (@function) {
517 $i =~ /^${lib}_F_(\S+)$/;
519 if(exists $ftrans{$fn}) {
522 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
523 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
529 static ERR_STRING_DATA ${lib}_str_reasons[]=
532 # Add each reason code.
533 foreach $i (@reasons) {
535 my $rstr = "ERR_REASON($i)";
537 if (exists $err_reason_strings{$i}) {
538 $rn = $err_reason_strings{$i};
540 $i =~ /^${lib}_R_(\S+)$/;
542 $rn =~ tr/_[A-Z]/ [a-z]/;
544 $nspc = 40 - length($rstr) unless length($rstr) > 40;
546 print OUT "{${rstr}${nspc},\"$rn\"},\n";
555 ${staticloader}void ERR_load_${lib}_strings(void)
562 #ifndef OPENSSL_NO_ERR
563 ERR_load_strings($load_errcode,${lib}_str_functs);
564 ERR_load_strings($load_errcode,${lib}_str_reasons);
577 #ifdef ${lib}_LIB_NAME
578 static ERR_STRING_DATA ${lib}_lib_name[]=
580 {0 ,${lib}_LIB_NAME},
586 static int ${lib}_lib_error_code=0;
587 static int ${lib}_error_init=1;
589 ${staticloader}void ERR_load_${lib}_strings(void)
591 if (${lib}_lib_error_code == 0)
592 ${lib}_lib_error_code=ERR_get_next_error_library();
594 if (${lib}_error_init)
597 #ifndef OPENSSL_NO_ERR
598 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
599 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
602 #ifdef ${lib}_LIB_NAME
603 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
604 ERR_load_strings(0,${lib}_lib_name);
609 ${staticloader}void ERR_unload_${lib}_strings(void)
611 if (${lib}_error_init == 0)
613 #ifndef OPENSSL_NO_ERR
614 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
615 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
618 #ifdef ${lib}_LIB_NAME
619 ERR_unload_strings(0,${lib}_lib_name);
625 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
627 if (${lib}_lib_error_code == 0)
628 ${lib}_lib_error_code=ERR_get_next_error_library();
629 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
636 undef %err_reason_strings;
639 if($debug && defined(%notrans)) {
640 print STDERR "The following function codes were not translated:\n";
641 foreach(sort keys %notrans)
647 # Make a list of unreferenced function and reason codes
649 foreach (keys %fcodes) {
650 push (@funref, $_) unless exists $ufcodes{$_};
653 foreach (keys %rcodes) {
654 push (@runref, $_) unless exists $urcodes{$_};
657 if($debug && defined(@funref) ) {
658 print STDERR "The following function codes were not referenced:\n";
659 foreach(sort @funref)
665 if($debug && defined(@runref) ) {
666 print STDERR "The following reason codes were not referenced:\n";
667 foreach(sort @runref)