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/);
106 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
108 $cpp = 0 if /^#.*endif/;
112 next if (/^\#/); # skip preprocessor directives
114 s/\/\*.*?\*\///gs; # ignore comments
115 s/{[^{}]*}//gs; # ignore {} blocks
117 if (/\{|\/\*/) { # Add a } so editor works...
125 print STDERR " \r" if $debug;
127 foreach (split /;/, $def) {
129 print STDERR "def: $defnr\r" if $debug;
133 next if(/typedef\W/);
134 if (/\(\*(\w*)\([^\)]+/) {
136 $name =~ tr/[a-z]/[A-Z]/;
138 } elsif (/\w+\W+(\w+)\W*\(\s*\)(\s*__attribute__\(.*\)\s*)?$/s){
141 } elsif (/\w+\W+\w+\W*\(.*\)(\s*__attribute__\(.*\)\s*)?$/s) {
142 while (not /\(\)(\s*__attribute__\(.*\)\s*)?$/s) {
143 s/[^\(\)]*\)(\s*__attribute__\(.*\)\s*)?$/\)/s;
144 s/\([^\(\)]*\)\)(\s*__attribute__\(.*\)\s*)?$/\)/s;
147 /(\w+(\{[0-9]+\})?)\W*\(\)/s;
149 $name =~ tr/[a-z]/[A-Z]/;
151 } elsif (/\(/ and not (/=/ or /DECLARE_STACK/ or /TYPEDEF_D2I2D_OF/)) {
152 print STDERR "Header $hdr: cannot parse: $_;\n";
156 print STDERR " \r" if $debug;
160 # Scan function and reason codes and store them: keep a note of the
165 if(/^\#define\s+(\S+)\s+(\S+)/) {
168 next if $name =~ /^${lib}err/;
169 unless($name =~ /^${lib}_([RF])_(\w+)$/) {
170 print STDERR "Invalid error code $name\n";
174 $rcodes{$name} = $code;
175 if(!(exists $rextra{$name}) &&
176 ($code > $rmax{$lib}) ) {
180 if($code > $fmax{$lib}) {
183 $fcodes{$name} = $code;
191 # Scan each C source file and look for function and reason codes
192 # This is done by looking for strings that "look like" function or
193 # reason codes: basically anything consisting of all upper case and
194 # numerics which has _F_ or _R_ in it and which has the name of an
195 # error library at the start. This seems to work fine except for the
196 # oddly named structure BIO_F_CTX which needs to be ignored.
197 # If a code doesn't exist in list compiled from headers then mark it
198 # with the value "X" as a place holder to give it a value later.
199 # Store all function and reason codes found in %ufcodes and %urcodes
200 # so all those unreferenced can be printed out.
203 print STDERR "Files loaded: " if $debug;
204 foreach $file (@source) {
205 # Don't parse the error source file.
206 next if exists $cskip{$file};
207 print STDERR $file if $debug;
208 open(IN, "<$file") || die "Can't open source file $file\n";
210 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
211 next unless exists $csrc{$2};
212 next if($1 eq "BIO_F_BUFFER_CTX");
214 if(!exists $fcodes{$1}) {
218 $notrans{$1} = 1 unless exists $ftrans{$3};
220 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
221 next unless exists $csrc{$2};
223 if(!exists $rcodes{$1}) {
231 print STDERR "\n" if $debug;
233 # Now process each library in turn.
235 foreach $lib (keys %csrc)
237 my $hfile = $hinc{$lib};
238 my $cfile = $csrc{$lib};
239 if(!$fnew{$lib} && !$rnew{$lib}) {
240 print STDERR "$lib:\t\tNo new error codes\n";
241 next unless $rebuild;
243 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
244 print STDERR " $rnew{$lib} New Reasons.\n";
245 next unless $dowrite;
248 # If we get here then we have some new error codes so we
249 # need to rebuild the header file and C file.
251 # Make a sorted list of error and reason codes for later use.
253 my @function = sort grep(/^${lib}_/,keys %fcodes);
254 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
256 # Rewrite the header file
258 if (open(IN, "<$hfile")) {
259 # Copy across the old file
262 last if (/BEGIN ERROR CODES/);
267 "/* ====================================================================\n",
268 " * Copyright (c) 2001-2005 The OpenSSL Project. All rights reserved.\n",
270 " * Redistribution and use in source and binary forms, with or without\n",
271 " * modification, are permitted provided that the following conditions\n",
274 " * 1. Redistributions of source code must retain the above copyright\n",
275 " * notice, this list of conditions and the following disclaimer. \n",
277 " * 2. Redistributions in binary form must reproduce the above copyright\n",
278 " * notice, this list of conditions and the following disclaimer in\n",
279 " * the documentation and/or other materials provided with the\n",
280 " * distribution.\n",
282 " * 3. All advertising materials mentioning features or use of this\n",
283 " * software must display the following acknowledgment:\n",
284 " * \"This product includes software developed by the OpenSSL Project\n",
285 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
287 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
288 " * endorse or promote products derived from this software without\n",
289 " * prior written permission. For written permission, please contact\n",
290 " * openssl-core\@openssl.org.\n",
292 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
293 " * nor may \"OpenSSL\" appear in their names without prior written\n",
294 " * permission of the OpenSSL Project.\n",
296 " * 6. Redistributions of any form whatsoever must retain the following\n",
297 " * acknowledgment:\n",
298 " * \"This product includes software developed by the OpenSSL Project\n",
299 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
301 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
302 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
303 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
304 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
305 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
306 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
307 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
308 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
309 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
310 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
311 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
312 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
313 " * ====================================================================\n",
315 " * This product includes cryptographic software written by Eric Young\n",
316 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
317 " * Hudson (tjh\@cryptsoft.com).\n",
321 "#ifndef HEADER_${lib}_ERR_H\n",
322 "#define HEADER_${lib}_ERR_H\n",
324 "/* BEGIN ERROR CODES */\n";
326 open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
331 /* The following lines are auto generated by the script mkerr.pl. Any changes
332 * made after this point may be overwritten when the script is next run.
337 ${staticloader}void ERR_load_${lib}_strings(void);
342 ${staticloader}void ERR_load_${lib}_strings(void);
343 ${staticloader}void ERR_unload_${lib}_strings(void);
344 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
345 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
350 /* Error codes for the $lib functions. */
352 /* Function codes. */
355 foreach $i (@function) {
356 $z=6-int(length($i)/8);
357 if($fcodes{$i} eq "X") {
358 $fcodes{$i} = ++$fmax{$lib};
359 print STDERR "New Function code $i\n" if $debug;
361 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
364 print OUT "\n/* Reason codes. */\n";
366 foreach $i (@reasons) {
367 $z=6-int(length($i)/8);
368 if($rcodes{$i} eq "X") {
369 $rcodes{$i} = ++$rmax{$lib};
370 print STDERR "New Reason code $i\n" if $debug;
372 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
383 # Rewrite the C source file containing the error details.
385 # First, read any existing reason string definitions:
386 my %err_reason_strings;
387 if (open(IN,"<$cfile")) {
389 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
390 $err_reason_strings{$1} = $2;
398 $hfile =~ /([^\/]+)$/;
399 $hincf = "<openssl/$1>";
401 $hincf = "\"$hfile\"";
404 # If static we know the error code at compile time so use it
405 # in error definitions.
409 $pack_errcode = "ERR_LIB_${lib}";
415 $load_errcode = "ERR_LIB_${lib}";
419 open (OUT,">$cfile") || die "Can't open $cfile for writing";
423 /* ====================================================================
424 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
426 * Redistribution and use in source and binary forms, with or without
427 * modification, are permitted provided that the following conditions
430 * 1. Redistributions of source code must retain the above copyright
431 * notice, this list of conditions and the following disclaimer.
433 * 2. Redistributions in binary form must reproduce the above copyright
434 * notice, this list of conditions and the following disclaimer in
435 * the documentation and/or other materials provided with the
438 * 3. All advertising materials mentioning features or use of this
439 * software must display the following acknowledgment:
440 * "This product includes software developed by the OpenSSL Project
441 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
443 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
444 * endorse or promote products derived from this software without
445 * prior written permission. For written permission, please contact
446 * openssl-core\@OpenSSL.org.
448 * 5. Products derived from this software may not be called "OpenSSL"
449 * nor may "OpenSSL" appear in their names without prior written
450 * permission of the OpenSSL Project.
452 * 6. Redistributions of any form whatsoever must retain the following
454 * "This product includes software developed by the OpenSSL Project
455 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
457 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
458 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
459 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
460 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
461 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
462 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
463 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
464 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
465 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
466 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
467 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
468 * OF THE POSSIBILITY OF SUCH DAMAGE.
469 * ====================================================================
471 * This product includes cryptographic software written by Eric Young
472 * (eay\@cryptsoft.com). This product includes software written by Tim
473 * Hudson (tjh\@cryptsoft.com).
477 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
478 * made to it will be overwritten when the script next updates this file,
479 * only reason strings will be preserved.
483 #include <openssl/err.h>
486 /* BEGIN ERROR CODES */
487 #ifndef OPENSSL_NO_ERR
489 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
490 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
492 static ERR_STRING_DATA ${lib}_str_functs[]=
495 # Add each function code: if a function name is found then use it.
496 foreach $i (@function) {
498 $i =~ /^${lib}_F_(\S+)$/;
500 if(exists $ftrans{$fn}) {
503 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
504 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
510 static ERR_STRING_DATA ${lib}_str_reasons[]=
513 # Add each reason code.
514 foreach $i (@reasons) {
516 my $rstr = "ERR_REASON($i)";
518 if (exists $err_reason_strings{$i}) {
519 $rn = $err_reason_strings{$i};
521 $i =~ /^${lib}_R_(\S+)$/;
523 $rn =~ tr/_[A-Z]/ [a-z]/;
525 $nspc = 40 - length($rstr) unless length($rstr) > 40;
527 print OUT "{${rstr}${nspc},\"$rn\"},\n";
536 ${staticloader}void ERR_load_${lib}_strings(void)
543 #ifndef OPENSSL_NO_ERR
544 ERR_load_strings($load_errcode,${lib}_str_functs);
545 ERR_load_strings($load_errcode,${lib}_str_reasons);
558 #ifdef ${lib}_LIB_NAME
559 static ERR_STRING_DATA ${lib}_lib_name[]=
561 {0 ,${lib}_LIB_NAME},
567 static int ${lib}_lib_error_code=0;
568 static int ${lib}_error_init=1;
570 ${staticloader}void ERR_load_${lib}_strings(void)
572 if (${lib}_lib_error_code == 0)
573 ${lib}_lib_error_code=ERR_get_next_error_library();
575 if (${lib}_error_init)
578 #ifndef OPENSSL_NO_ERR
579 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
580 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
583 #ifdef ${lib}_LIB_NAME
584 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
585 ERR_load_strings(0,${lib}_lib_name);
590 ${staticloader}void ERR_unload_${lib}_strings(void)
592 if (${lib}_error_init == 0)
594 #ifndef OPENSSL_NO_ERR
595 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
596 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
599 #ifdef ${lib}_LIB_NAME
600 ERR_unload_strings(0,${lib}_lib_name);
606 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
608 if (${lib}_lib_error_code == 0)
609 ${lib}_lib_error_code=ERR_get_next_error_library();
610 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
617 undef %err_reason_strings;
620 if($debug && defined(%notrans)) {
621 print STDERR "The following function codes were not translated:\n";
622 foreach(sort keys %notrans)
628 # Make a list of unreferenced function and reason codes
630 foreach (keys %fcodes) {
631 push (@funref, $_) unless exists $ufcodes{$_};
634 foreach (keys %rcodes) {
635 push (@runref, $_) unless exists $urcodes{$_};
638 if($debug && defined(@funref) ) {
639 print STDERR "The following function codes were not referenced:\n";
640 foreach(sort @funref)
646 if($debug && defined(@runref) ) {
647 print STDERR "The following reason codes were not referenced:\n";
648 foreach(sort @runref)