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>, <fips-1.0/*.c>,
53 # Read in the config file
55 open(IN, "<$config") || die "Can't open config file $config";
61 if(/^L\s+(\S+)\s+(\S+)\s+(\S+)/) {
72 } elsif (/^F\s+(\S+)/) {
73 # Add extra function with $1
74 } elsif (/^R\s+(\S+)\s+(\S+)/) {
82 # Scan each header file in turn and make a list of error codes
85 while (($hdr, $lib) = each %libinc)
87 next if($hdr eq "NONE");
88 print STDERR "Scanning header file $hdr\n" if $debug;
89 my $line = "", $def= "", $linenr = 0, $gotfile = 0;
90 if (open(IN, "<$hdr")) {
94 print STDERR "line: $linenr\r" if $debug;
96 last if(/BEGIN\s+ERROR\s+CODES/);
107 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
109 $cpp = 0 if /^#.*endif/;
113 next if (/^\#/); # skip preprocessor directives
115 s/\/\*.*?\*\///gs; # ignore comments
116 s/{[^{}]*}//gs; # ignore {} blocks
118 if (/\{|\/\*/) { # Add a } so editor works...
126 print STDERR " \r" if $debug;
128 foreach (split /;/, $def) {
130 print STDERR "def: $defnr\r" if $debug;
134 next if(/typedef\W/);
135 if (/\(\*(\w*)\([^\)]+/) {
137 $name =~ tr/[a-z]/[A-Z]/;
139 } elsif (/\w+\W+(\w+)\W*\(\s*\)(\s*__attribute__\(.*\)\s*)?$/s){
142 } elsif (/\w+\W+\w+\W*\(.*\)(\s*__attribute__\(.*\)\s*)?$/s) {
143 while (not /\(\)(\s*__attribute__\(.*\)\s*)?$/s) {
144 s/[^\(\)]*\)(\s*__attribute__\(.*\)\s*)?$/\)/s;
145 s/\([^\(\)]*\)\)(\s*__attribute__\(.*\)\s*)?$/\)/s;
148 /(\w+(\{[0-9]+\})?)\W*\(\)/s;
150 $name =~ tr/[a-z]/[A-Z]/;
152 } elsif (/\(/ and not (/=/ or /DECLARE_STACK/)) {
153 print STDERR "Header $hdr: cannot parse: $_;\n";
157 print STDERR " \r" if $debug;
161 # Scan function and reason codes and store them: keep a note of the
166 if(/^\#define\s+(\S+)\s+(\S+)/) {
169 next if $name =~ /^${lib}err/;
170 unless($name =~ /^${lib}_([RF])_(\w+)$/) {
171 print STDERR "Invalid error code $name\n";
175 $rcodes{$name} = $code;
176 if(!(exists $rextra{$name}) &&
177 ($code > $rmax{$lib}) ) {
181 if($code > $fmax{$lib}) {
184 $fcodes{$name} = $code;
192 # Scan each C source file and look for function and reason codes
193 # This is done by looking for strings that "look like" function or
194 # reason codes: basically anything consisting of all upper case and
195 # numerics which has _F_ or _R_ in it and which has the name of an
196 # error library at the start. This seems to work fine except for the
197 # oddly named structure BIO_F_CTX which needs to be ignored.
198 # If a code doesn't exist in list compiled from headers then mark it
199 # with the value "X" as a place holder to give it a value later.
200 # Store all function and reason codes found in %ufcodes and %urcodes
201 # so all those unreferenced can be printed out.
204 print STDERR "Files loaded: " if $debug;
205 foreach $file (@source) {
206 # Don't parse the error source file.
207 next if exists $cskip{$file};
208 print STDERR $file if $debug;
209 open(IN, "<$file") || die "Can't open source file $file\n";
211 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
212 next unless exists $csrc{$2};
213 next if($1 eq "BIO_F_BUFFER_CTX");
215 if(!exists $fcodes{$1}) {
219 $notrans{$1} = 1 unless exists $ftrans{$3};
221 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
222 next unless exists $csrc{$2};
224 if(!exists $rcodes{$1}) {
232 print STDERR "\n" if $debug;
234 # Now process each library in turn.
236 foreach $lib (keys %csrc)
238 my $hfile = $hinc{$lib};
239 my $cfile = $csrc{$lib};
240 if(!$fnew{$lib} && !$rnew{$lib}) {
241 print STDERR "$lib:\t\tNo new error codes\n";
242 next unless $rebuild;
244 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
245 print STDERR " $rnew{$lib} New Reasons.\n";
246 next unless $dowrite;
249 # If we get here then we have some new error codes so we
250 # need to rebuild the header file and C file.
252 # Make a sorted list of error and reason codes for later use.
254 my @function = sort grep(/^${lib}_/,keys %fcodes);
255 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
257 # Rewrite the header file
259 if (open(IN, "<$hfile")) {
260 # Copy across the old file
263 last if (/BEGIN ERROR CODES/);
268 "/* ====================================================================\n",
269 " * Copyright (c) 2001-2007 The OpenSSL Project. All rights reserved.\n",
271 " * Redistribution and use in source and binary forms, with or without\n",
272 " * modification, are permitted provided that the following conditions\n",
275 " * 1. Redistributions of source code must retain the above copyright\n",
276 " * notice, this list of conditions and the following disclaimer. \n",
278 " * 2. Redistributions in binary form must reproduce the above copyright\n",
279 " * notice, this list of conditions and the following disclaimer in\n",
280 " * the documentation and/or other materials provided with the\n",
281 " * distribution.\n",
283 " * 3. All advertising materials mentioning features or use of this\n",
284 " * software must display the following acknowledgment:\n",
285 " * \"This product includes software developed by the OpenSSL Project\n",
286 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
288 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
289 " * endorse or promote products derived from this software without\n",
290 " * prior written permission. For written permission, please contact\n",
291 " * openssl-core\@openssl.org.\n",
293 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
294 " * nor may \"OpenSSL\" appear in their names without prior written\n",
295 " * permission of the OpenSSL Project.\n",
297 " * 6. Redistributions of any form whatsoever must retain the following\n",
298 " * acknowledgment:\n",
299 " * \"This product includes software developed by the OpenSSL Project\n",
300 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
302 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
303 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
304 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
305 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
306 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
307 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
308 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
309 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
310 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
311 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
312 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
313 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
314 " * ====================================================================\n",
316 " * This product includes cryptographic software written by Eric Young\n",
317 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
318 " * Hudson (tjh\@cryptsoft.com).\n",
322 "#ifndef HEADER_${lib}_ERR_H\n",
323 "#define HEADER_${lib}_ERR_H\n",
325 "/* BEGIN ERROR CODES */\n";
327 open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
332 /* The following lines are auto generated by the script mkerr.pl. Any changes
333 * made after this point may be overwritten when the script is next run.
338 ${staticloader}void ERR_load_${lib}_strings(void);
343 ${staticloader}void ERR_load_${lib}_strings(void);
344 ${staticloader}void ERR_unload_${lib}_strings(void);
345 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
346 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
351 /* Error codes for the $lib functions. */
353 /* Function codes. */
356 foreach $i (@function) {
357 $z=6-int(length($i)/8);
358 if($fcodes{$i} eq "X") {
359 $fcodes{$i} = ++$fmax{$lib};
360 print STDERR "New Function code $i\n" if $debug;
362 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
365 print OUT "\n/* Reason codes. */\n";
367 foreach $i (@reasons) {
368 $z=6-int(length($i)/8);
369 if($rcodes{$i} eq "X") {
370 $rcodes{$i} = ++$rmax{$lib};
371 print STDERR "New Reason code $i\n" if $debug;
373 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
384 # Rewrite the C source file containing the error details.
386 # First, read any existing reason string definitions:
387 my %err_reason_strings;
388 if (open(IN,"<$cfile")) {
390 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
391 $err_reason_strings{$1} = $2;
399 $hfile =~ /([^\/]+)$/;
400 $hincf = "<openssl/$1>";
402 $hincf = "\"$hfile\"";
405 # If static we know the error code at compile time so use it
406 # in error definitions.
410 $pack_errcode = "ERR_LIB_${lib}";
416 $load_errcode = "ERR_LIB_${lib}";
420 open (OUT,">$cfile") || die "Can't open $cfile for writing";
424 /* ====================================================================
425 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
427 * Redistribution and use in source and binary forms, with or without
428 * modification, are permitted provided that the following conditions
431 * 1. Redistributions of source code must retain the above copyright
432 * notice, this list of conditions and the following disclaimer.
434 * 2. Redistributions in binary form must reproduce the above copyright
435 * notice, this list of conditions and the following disclaimer in
436 * the documentation and/or other materials provided with the
439 * 3. All advertising materials mentioning features or use of this
440 * software must display the following acknowledgment:
441 * "This product includes software developed by the OpenSSL Project
442 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
444 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
445 * endorse or promote products derived from this software without
446 * prior written permission. For written permission, please contact
447 * openssl-core\@OpenSSL.org.
449 * 5. Products derived from this software may not be called "OpenSSL"
450 * nor may "OpenSSL" appear in their names without prior written
451 * permission of the OpenSSL Project.
453 * 6. Redistributions of any form whatsoever must retain the following
455 * "This product includes software developed by the OpenSSL Project
456 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
458 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
459 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
460 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
461 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
462 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
463 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
464 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
465 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
466 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
467 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
468 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
469 * OF THE POSSIBILITY OF SUCH DAMAGE.
470 * ====================================================================
472 * This product includes cryptographic software written by Eric Young
473 * (eay\@cryptsoft.com). This product includes software written by Tim
474 * Hudson (tjh\@cryptsoft.com).
478 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
479 * made to it will be overwritten when the script next updates this file,
480 * only reason strings will be preserved.
484 #include <openssl/err.h>
487 /* BEGIN ERROR CODES */
488 #ifndef OPENSSL_NO_ERR
490 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
491 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
493 static ERR_STRING_DATA ${lib}_str_functs[]=
496 # Add each function code: if a function name is found then use it.
497 foreach $i (@function) {
499 $i =~ /^${lib}_F_(\S+)$/;
501 if(exists $ftrans{$fn}) {
504 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
505 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
511 static ERR_STRING_DATA ${lib}_str_reasons[]=
514 # Add each reason code.
515 foreach $i (@reasons) {
517 my $rstr = "ERR_REASON($i)";
519 if (exists $err_reason_strings{$i}) {
520 $rn = $err_reason_strings{$i};
522 $i =~ /^${lib}_R_(\S+)$/;
524 $rn =~ tr/_[A-Z]/ [a-z]/;
526 $nspc = 40 - length($rstr) unless length($rstr) > 40;
528 print OUT "{${rstr}${nspc},\"$rn\"},\n";
537 ${staticloader}void ERR_load_${lib}_strings(void)
539 #ifndef OPENSSL_NO_ERR
541 if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL)
543 ERR_load_strings($load_errcode,${lib}_str_functs);
544 ERR_load_strings($load_errcode,${lib}_str_reasons);
556 #ifdef ${lib}_LIB_NAME
557 static ERR_STRING_DATA ${lib}_lib_name[]=
559 {0 ,${lib}_LIB_NAME},
565 static int ${lib}_lib_error_code=0;
566 static int ${lib}_error_init=1;
568 ${staticloader}void ERR_load_${lib}_strings(void)
570 if (${lib}_lib_error_code == 0)
571 ${lib}_lib_error_code=ERR_get_next_error_library();
573 if (${lib}_error_init)
576 #ifndef OPENSSL_NO_ERR
577 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
578 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
581 #ifdef ${lib}_LIB_NAME
582 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
583 ERR_load_strings(0,${lib}_lib_name);
588 ${staticloader}void ERR_unload_${lib}_strings(void)
590 if (${lib}_error_init == 0)
592 #ifndef OPENSSL_NO_ERR
593 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
594 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
597 #ifdef ${lib}_LIB_NAME
598 ERR_unload_strings(0,${lib}_lib_name);
604 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
606 if (${lib}_lib_error_code == 0)
607 ${lib}_lib_error_code=ERR_get_next_error_library();
608 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
615 undef %err_reason_strings;
618 if($debug && defined(%notrans)) {
619 print STDERR "The following function codes were not translated:\n";
620 foreach(sort keys %notrans)
626 # Make a list of unreferenced function and reason codes
628 foreach (keys %fcodes) {
629 push (@funref, $_) unless exists $ufcodes{$_};
632 foreach (keys %rcodes) {
633 push (@runref, $_) unless exists $urcodes{$_};
636 if($debug && defined(@funref) ) {
637 print STDERR "The following function codes were not referenced:\n";
638 foreach(sort @funref)
644 if($debug && defined(@runref) ) {
645 print STDERR "The following reason codes were not referenced:\n";
646 foreach(sort @runref)