1 #!/usr/local/bin/perl -w
3 my $config = "crypto/err/openssl.ec";
4 my $hprefix = "openssl/";
11 my $staticloader = "";
20 $config = shift @ARGV;
21 } elsif($arg eq "-hprefix") {
23 $hprefix = shift @ARGV;
24 } elsif($arg eq "-debug") {
27 } elsif($arg eq "-rebuild") {
30 } elsif($arg eq "-recurse") {
33 } elsif($arg eq "-reindex") {
36 } elsif($arg eq "-nostatic") {
39 } elsif($arg eq "-staticloader") {
40 $staticloader = "static ";
42 } elsif($arg eq "-write") {
51 @source = (<crypto/*.c>, <crypto/*/*.c>, <ssl/*.c>);
56 # Read in the config file
58 open(IN, "<$config") || die "Can't open config file $config";
64 if(/^L\s+(\S+)\s+(\S+)\s+(\S+)/) {
77 } elsif (/^F\s+(\S+)/) {
78 # Add extra function with $1
79 } elsif (/^R\s+(\S+)\s+(\S+)/) {
87 # Scan each header file in turn and make a list of error codes
90 while (($hdr, $lib) = each %libinc)
92 next if($hdr eq "NONE");
93 print STDERR "Scanning header file $hdr\n" if $debug;
94 my $line = "", $def= "", $linenr = 0, $gotfile = 0;
95 if (open(IN, "<$hdr")) {
99 print STDERR "line: $linenr\r" if $debug;
101 last if(/BEGIN\s+ERROR\s+CODES/);
113 if (not /\*\//) { # multiline comment...
114 $line = $_; # ... just accumulate
117 s/\/\*.*?\*\///gs; # wipe it
123 $cpp-- if /^#\s*endif/;
126 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
128 next if (/^\#/); # skip preprocessor directives
130 s/{[^{}]*}//gs; # ignore {} blocks
132 if (/\{|\/\*/) { # Add a } so editor works...
140 print STDERR " \r" if $debug;
142 # Delete any DECLARE_ macros
143 $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
144 foreach (split /;/, $def) {
146 print STDERR "def: $defnr\r" if $debug;
148 # The goal is to collect function names from function declarations.
153 # Skip over recognized non-function declarations
154 next if(/typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/);
156 # Remove STACK_OF(foo)
157 s/STACK_OF\(\w+\)/void/;
159 # Reduce argument lists to empty ()
160 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
162 s/\([^\(\)]+\)/\{\}/gs;
163 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
165 # pretend as we didn't use curly braces: {} -> ()
168 if (/(\w+)\s*\(\).*/s) { # first token prior [first] () is
169 my $name = $1; # a function name!
170 $name =~ tr/[a-z]/[A-Z]/;
172 } elsif (/[\(\)]/ and not (/=/)) {
173 print STDERR "Header $hdr: cannot parse: $_;\n";
177 print STDERR " \r" if $debug;
181 # Scan function and reason codes and store them: keep a note of the
186 if(/^\#define\s+(\S+)\s+(\S+)/) {
189 next if $name =~ /^${lib}err/;
190 unless($name =~ /^${lib}_([RF])_(\w+)$/) {
191 print STDERR "Invalid error code $name\n";
195 $rcodes{$name} = $code;
196 if ($rassigned{$lib} =~ /:$code:/) {
197 print STDERR "!! ERROR: $lib reason code $code assigned twice\n";
199 $rassigned{$lib} .= "$code:";
200 if(!(exists $rextra{$name}) &&
201 ($code > $rmax{$lib}) ) {
205 if ($fassigned{$lib} =~ /:$code:/) {
206 print STDERR "!! ERROR: $lib function code $code assigned twice\n";
208 $fassigned{$lib} .= "$code:";
209 if($code > $fmax{$lib}) {
212 $fcodes{$name} = $code;
219 if (defined($fmax{$lib})) {
220 print STDERR "Max function code fmax" . "{" . "$lib" . "} = $fmax{$lib}\n";
221 $fassigned{$lib} =~ m/^:(.*):$/;
222 @fassigned = sort {$a <=> $b} split(":", $1);
223 print STDERR " @fassigned\n";
225 if (defined($rmax{$lib})) {
226 print STDERR "Max reason code rmax" . "{" . "$lib" . "} = $rmax{$lib}\n";
227 $rassigned{$lib} =~ m/^:(.*):$/;
228 @rassigned = sort {$a <=> $b} split(":", $1);
229 print STDERR " @rassigned\n";
234 if ($rmax{$lib} >= 1000) {
235 print STDERR "!! ERROR: SSL error codes 1000+ are reserved for alerts.\n";
236 print STDERR "!! Any new alerts must be added to $config.\n";
243 # Scan each C source file and look for function and reason codes
244 # This is done by looking for strings that "look like" function or
245 # reason codes: basically anything consisting of all upper case and
246 # numerics which has _F_ or _R_ in it and which has the name of an
247 # error library at the start. This seems to work fine except for the
248 # oddly named structure BIO_F_CTX which needs to be ignored.
249 # If a code doesn't exist in list compiled from headers then mark it
250 # with the value "X" as a place holder to give it a value later.
251 # Store all function and reason codes found in %ufcodes and %urcodes
252 # so all those unreferenced can be printed out.
255 foreach $file (@source) {
256 # Don't parse the error source file.
257 next if exists $cskip{$file};
258 print STDERR "File loaded: ".$file."\r" if $debug;
259 open(IN, "<$file") || die "Can't open source file $file\n";
261 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
262 next unless exists $csrc{$2};
263 next if($1 eq "BIO_F_BUFFER_CTX");
265 if(!exists $fcodes{$1}) {
269 $notrans{$1} = 1 unless exists $ftrans{$3};
271 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
272 next unless exists $csrc{$2};
274 if(!exists $rcodes{$1}) {
282 print STDERR " \n" if $debug;
284 # Now process each library in turn.
286 foreach $lib (keys %csrc)
288 my $hfile = $hinc{$lib};
289 my $cfile = $csrc{$lib};
290 if(!$fnew{$lib} && !$rnew{$lib}) {
291 print STDERR "$lib:\t\tNo new error codes\n";
292 next unless $rebuild;
294 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
295 print STDERR " $rnew{$lib} New Reasons.\n";
296 next unless $dowrite;
299 # If we get here then we have some new error codes so we
300 # need to rebuild the header file and C file.
302 # Make a sorted list of error and reason codes for later use.
304 my @function = sort grep(/^${lib}_/,keys %fcodes);
305 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
307 # Rewrite the header file
309 if (open(IN, "<$hfile")) {
310 # Copy across the old file
313 last if (/BEGIN ERROR CODES/);
318 "/* ====================================================================\n",
319 " * Copyright (c) 2001-2007 The OpenSSL Project. All rights reserved.\n",
321 " * Redistribution and use in source and binary forms, with or without\n",
322 " * modification, are permitted provided that the following conditions\n",
325 " * 1. Redistributions of source code must retain the above copyright\n",
326 " * notice, this list of conditions and the following disclaimer. \n",
328 " * 2. Redistributions in binary form must reproduce the above copyright\n",
329 " * notice, this list of conditions and the following disclaimer in\n",
330 " * the documentation and/or other materials provided with the\n",
331 " * distribution.\n",
333 " * 3. All advertising materials mentioning features or use of this\n",
334 " * software must display the following acknowledgment:\n",
335 " * \"This product includes software developed by the OpenSSL Project\n",
336 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
338 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
339 " * endorse or promote products derived from this software without\n",
340 " * prior written permission. For written permission, please contact\n",
341 " * openssl-core\@openssl.org.\n",
343 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
344 " * nor may \"OpenSSL\" appear in their names without prior written\n",
345 " * permission of the OpenSSL Project.\n",
347 " * 6. Redistributions of any form whatsoever must retain the following\n",
348 " * acknowledgment:\n",
349 " * \"This product includes software developed by the OpenSSL Project\n",
350 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
352 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
353 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
354 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
355 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
356 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
357 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
358 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
359 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
360 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
361 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
362 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
363 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
364 " * ====================================================================\n",
366 " * This product includes cryptographic software written by Eric Young\n",
367 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
368 " * Hudson (tjh\@cryptsoft.com).\n",
372 "#ifndef HEADER_${lib}_ERR_H\n",
373 "#define HEADER_${lib}_ERR_H\n",
375 "/* BEGIN ERROR CODES */\n";
377 open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
382 /* The following lines are auto generated by the script mkerr.pl. Any changes
383 * made after this point may be overwritten when the script is next run.
388 ${staticloader}void ERR_load_${lib}_strings(void);
393 ${staticloader}void ERR_load_${lib}_strings(void);
394 ${staticloader}void ERR_unload_${lib}_strings(void);
395 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
396 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
401 /* Error codes for the $lib functions. */
403 /* Function codes. */
406 foreach $i (@function) {
407 $z=6-int(length($i)/8);
408 if($fcodes{$i} eq "X") {
409 $fassigned{$lib} =~ m/^:([^:]*):/;
411 if (!defined($findcode)) {
412 $findcode = $fmax{$lib};
414 while ($fassigned{$lib} =~ m/:$findcode:/) {
417 $fcodes{$i} = $findcode;
418 $fassigned{$lib} .= "$findcode:";
419 print STDERR "New Function code $i\n" if $debug;
421 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
424 print OUT "\n/* Reason codes. */\n";
426 foreach $i (@reasons) {
427 $z=6-int(length($i)/8);
428 if($rcodes{$i} eq "X") {
429 $rassigned{$lib} =~ m/^:([^:]*):/;
431 if (!defined($findcode)) {
432 $findcode = $rmax{$lib};
434 while ($rassigned{$lib} =~ m/:$findcode:/) {
437 $rcodes{$i} = $findcode;
438 $rassigned{$lib} .= "$findcode:";
439 print STDERR "New Reason code $i\n" if $debug;
441 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
452 # Rewrite the C source file containing the error details.
454 # First, read any existing reason string definitions:
455 my %err_reason_strings;
456 if (open(IN,"<$cfile")) {
458 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
459 $err_reason_strings{$1} = $2;
461 if (/\b${lib}_F_(\w*)\b.*\"(.*)\"/) {
462 if (!exists $ftrans{$1} && ($1 ne $2)) {
463 print STDERR "WARNING: Mismatched function string $2\n";
474 $hfile =~ /([^\/]+)$/;
475 $hincf = "<${hprefix}$1>";
477 $hincf = "\"$hfile\"";
480 # If static we know the error code at compile time so use it
481 # in error definitions.
485 $pack_errcode = "ERR_LIB_${lib}";
491 $load_errcode = "ERR_LIB_${lib}";
495 open (OUT,">$cfile") || die "Can't open $cfile for writing";
499 /* ====================================================================
500 * Copyright (c) 1999-2007 The OpenSSL Project. All rights reserved.
502 * Redistribution and use in source and binary forms, with or without
503 * modification, are permitted provided that the following conditions
506 * 1. Redistributions of source code must retain the above copyright
507 * notice, this list of conditions and the following disclaimer.
509 * 2. Redistributions in binary form must reproduce the above copyright
510 * notice, this list of conditions and the following disclaimer in
511 * the documentation and/or other materials provided with the
514 * 3. All advertising materials mentioning features or use of this
515 * software must display the following acknowledgment:
516 * "This product includes software developed by the OpenSSL Project
517 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
519 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
520 * endorse or promote products derived from this software without
521 * prior written permission. For written permission, please contact
522 * openssl-core\@OpenSSL.org.
524 * 5. Products derived from this software may not be called "OpenSSL"
525 * nor may "OpenSSL" appear in their names without prior written
526 * permission of the OpenSSL Project.
528 * 6. Redistributions of any form whatsoever must retain the following
530 * "This product includes software developed by the OpenSSL Project
531 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
533 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
534 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
535 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
536 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
537 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
538 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
539 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
540 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
541 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
542 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
543 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
544 * OF THE POSSIBILITY OF SUCH DAMAGE.
545 * ====================================================================
547 * This product includes cryptographic software written by Eric Young
548 * (eay\@cryptsoft.com). This product includes software written by Tim
549 * Hudson (tjh\@cryptsoft.com).
553 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
554 * made to it will be overwritten when the script next updates this file,
555 * only reason strings will be preserved.
559 #include <openssl/err.h>
562 /* BEGIN ERROR CODES */
563 #ifndef OPENSSL_NO_ERR
565 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
566 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
568 static ERR_STRING_DATA ${lib}_str_functs[]=
571 # Add each function code: if a function name is found then use it.
572 foreach $i (@function) {
574 $i =~ /^${lib}_F_(\S+)$/;
576 if(exists $ftrans{$fn}) {
579 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
580 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
586 static ERR_STRING_DATA ${lib}_str_reasons[]=
589 # Add each reason code.
590 foreach $i (@reasons) {
592 my $rstr = "ERR_REASON($i)";
594 if (exists $err_reason_strings{$i}) {
595 $rn = $err_reason_strings{$i};
597 $i =~ /^${lib}_R_(\S+)$/;
599 $rn =~ tr/_[A-Z]/ [a-z]/;
601 $nspc = 40 - length($rstr) unless length($rstr) > 40;
603 print OUT "{${rstr}${nspc},\"$rn\"},\n";
612 ${staticloader}void ERR_load_${lib}_strings(void)
614 #ifndef OPENSSL_NO_ERR
616 if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL)
618 ERR_load_strings($load_errcode,${lib}_str_functs);
619 ERR_load_strings($load_errcode,${lib}_str_reasons);
631 #ifdef ${lib}_LIB_NAME
632 static ERR_STRING_DATA ${lib}_lib_name[]=
634 {0 ,${lib}_LIB_NAME},
640 static int ${lib}_lib_error_code=0;
641 static int ${lib}_error_init=1;
643 ${staticloader}void ERR_load_${lib}_strings(void)
645 if (${lib}_lib_error_code == 0)
646 ${lib}_lib_error_code=ERR_get_next_error_library();
648 if (${lib}_error_init)
651 #ifndef OPENSSL_NO_ERR
652 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
653 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
656 #ifdef ${lib}_LIB_NAME
657 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
658 ERR_load_strings(0,${lib}_lib_name);
663 ${staticloader}void ERR_unload_${lib}_strings(void)
665 if (${lib}_error_init == 0)
667 #ifndef OPENSSL_NO_ERR
668 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
669 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
672 #ifdef ${lib}_LIB_NAME
673 ERR_unload_strings(0,${lib}_lib_name);
679 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
681 if (${lib}_lib_error_code == 0)
682 ${lib}_lib_error_code=ERR_get_next_error_library();
683 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
690 undef %err_reason_strings;
693 if($debug && defined(%notrans)) {
694 print STDERR "The following function codes were not translated:\n";
695 foreach(sort keys %notrans)
701 # Make a list of unreferenced function and reason codes
703 foreach (keys %fcodes) {
704 push (@funref, $_) unless exists $ufcodes{$_};
707 foreach (keys %rcodes) {
708 push (@runref, $_) unless exists $urcodes{$_};
711 if($debug && defined(@funref) ) {
712 print STDERR "The following function codes were not referenced:\n";
713 foreach(sort @funref)
719 if($debug && defined(@runref) ) {
720 print STDERR "The following reason codes were not referenced:\n";
721 foreach(sort @runref)