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 foreach (split /;/, $def) {
144 print STDERR "def: $defnr\r" if $debug;
146 # The goal is to collect function names from function declarations.
151 # Skip over recognized non-function declarations
152 next if(/typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/);
154 # Reduce argument lists to empty ()
155 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
157 s/\([^\(\)]+\)/\{\}/gs;
158 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
160 # pretend as we didn't use curly braces: {} -> ()
163 if (/(\w+)\s*\(\).*/s) { # first token prior [first] () is
164 my $name = $1; # a function name!
165 $name =~ tr/[a-z]/[A-Z]/;
167 } elsif (/[\(\)]/ and not (/=/)) {
168 print STDERR "Header $hdr: cannot parse: $_;\n";
172 print STDERR " \r" if $debug;
176 # Scan function and reason codes and store them: keep a note of the
181 if(/^\#define\s+(\S+)\s+(\S+)/) {
184 next if $name =~ /^${lib}err/;
185 unless($name =~ /^${lib}_([RF])_(\w+)$/) {
186 print STDERR "Invalid error code $name\n";
190 $rcodes{$name} = $code;
191 if ($rassigned{$lib} =~ /:$code:/) {
192 print STDERR "!! ERROR: $lib reason code $code assigned twice\n";
194 $rassigned{$lib} .= "$code:";
195 if(!(exists $rextra{$name}) &&
196 ($code > $rmax{$lib}) ) {
200 if ($fassigned{$lib} =~ /:$code:/) {
201 print STDERR "!! ERROR: $lib function code $code assigned twice\n";
203 $fassigned{$lib} .= "$code:";
204 if($code > $fmax{$lib}) {
207 $fcodes{$name} = $code;
214 if (defined($fmax{$lib})) {
215 print STDERR "Max function code fmax" . "{" . "$lib" . "} = $fmax{$lib}\n";
216 $fassigned{$lib} =~ m/^:(.*):$/;
217 @fassigned = sort {$a <=> $b} split(":", $1);
218 print STDERR " @fassigned\n";
220 if (defined($rmax{$lib})) {
221 print STDERR "Max reason code rmax" . "{" . "$lib" . "} = $rmax{$lib}\n";
222 $rassigned{$lib} =~ m/^:(.*):$/;
223 @rassigned = sort {$a <=> $b} split(":", $1);
224 print STDERR " @rassigned\n";
229 if ($rmax{$lib} >= 1000) {
230 print STDERR "!! ERROR: SSL error codes 1000+ are reserved for alerts.\n";
231 print STDERR "!! Any new alerts must be added to $config.\n";
238 # Scan each C source file and look for function and reason codes
239 # This is done by looking for strings that "look like" function or
240 # reason codes: basically anything consisting of all upper case and
241 # numerics which has _F_ or _R_ in it and which has the name of an
242 # error library at the start. This seems to work fine except for the
243 # oddly named structure BIO_F_CTX which needs to be ignored.
244 # If a code doesn't exist in list compiled from headers then mark it
245 # with the value "X" as a place holder to give it a value later.
246 # Store all function and reason codes found in %ufcodes and %urcodes
247 # so all those unreferenced can be printed out.
250 foreach $file (@source) {
251 # Don't parse the error source file.
252 next if exists $cskip{$file};
253 print STDERR "File loaded: ".$file."\r" if $debug;
254 open(IN, "<$file") || die "Can't open source file $file\n";
256 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
257 next unless exists $csrc{$2};
258 next if($1 eq "BIO_F_BUFFER_CTX");
260 if(!exists $fcodes{$1}) {
264 $notrans{$1} = 1 unless exists $ftrans{$3};
266 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
267 next unless exists $csrc{$2};
269 if(!exists $rcodes{$1}) {
277 print STDERR " \n" if $debug;
279 # Now process each library in turn.
281 foreach $lib (keys %csrc)
283 my $hfile = $hinc{$lib};
284 my $cfile = $csrc{$lib};
285 if(!$fnew{$lib} && !$rnew{$lib}) {
286 print STDERR "$lib:\t\tNo new error codes\n";
287 next unless $rebuild;
289 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
290 print STDERR " $rnew{$lib} New Reasons.\n";
291 next unless $dowrite;
294 # If we get here then we have some new error codes so we
295 # need to rebuild the header file and C file.
297 # Make a sorted list of error and reason codes for later use.
299 my @function = sort grep(/^${lib}_/,keys %fcodes);
300 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
302 # Rewrite the header file
304 if (open(IN, "<$hfile")) {
305 # Copy across the old file
308 last if (/BEGIN ERROR CODES/);
313 "/* ====================================================================\n",
314 " * Copyright (c) 2001-2005 The OpenSSL Project. All rights reserved.\n",
316 " * Redistribution and use in source and binary forms, with or without\n",
317 " * modification, are permitted provided that the following conditions\n",
320 " * 1. Redistributions of source code must retain the above copyright\n",
321 " * notice, this list of conditions and the following disclaimer. \n",
323 " * 2. Redistributions in binary form must reproduce the above copyright\n",
324 " * notice, this list of conditions and the following disclaimer in\n",
325 " * the documentation and/or other materials provided with the\n",
326 " * distribution.\n",
328 " * 3. All advertising materials mentioning features or use of this\n",
329 " * software must display the following acknowledgment:\n",
330 " * \"This product includes software developed by the OpenSSL Project\n",
331 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
333 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
334 " * endorse or promote products derived from this software without\n",
335 " * prior written permission. For written permission, please contact\n",
336 " * openssl-core\@openssl.org.\n",
338 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
339 " * nor may \"OpenSSL\" appear in their names without prior written\n",
340 " * permission of the OpenSSL Project.\n",
342 " * 6. Redistributions of any form whatsoever must retain the following\n",
343 " * acknowledgment:\n",
344 " * \"This product includes software developed by the OpenSSL Project\n",
345 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
347 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
348 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
349 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
350 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
351 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
352 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
353 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
354 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
355 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
356 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
357 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
358 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
359 " * ====================================================================\n",
361 " * This product includes cryptographic software written by Eric Young\n",
362 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
363 " * Hudson (tjh\@cryptsoft.com).\n",
367 "#ifndef HEADER_${lib}_ERR_H\n",
368 "#define HEADER_${lib}_ERR_H\n",
370 "/* BEGIN ERROR CODES */\n";
372 open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
377 /* The following lines are auto generated by the script mkerr.pl. Any changes
378 * made after this point may be overwritten when the script is next run.
383 ${staticloader}void ERR_load_${lib}_strings(void);
388 ${staticloader}void ERR_load_${lib}_strings(void);
389 ${staticloader}void ERR_unload_${lib}_strings(void);
390 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
391 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
396 /* Error codes for the $lib functions. */
398 /* Function codes. */
401 foreach $i (@function) {
402 $z=6-int(length($i)/8);
403 if($fcodes{$i} eq "X") {
404 $fassigned{$lib} =~ m/^:([^:]*):/;
406 if (!defined($findcode)) {
407 $findcode = $fmax{$lib};
409 while ($fassigned{$lib} =~ m/:$findcode:/) {
412 $fcodes{$i} = $findcode;
413 $fassigned{$lib} .= "$findcode:";
414 print STDERR "New Function code $i\n" if $debug;
416 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
419 print OUT "\n/* Reason codes. */\n";
421 foreach $i (@reasons) {
422 $z=6-int(length($i)/8);
423 if($rcodes{$i} eq "X") {
424 $rassigned{$lib} =~ m/^:([^:]*):/;
426 if (!defined($findcode)) {
427 $findcode = $rmax{$lib};
429 while ($rassigned{$lib} =~ m/:$findcode:/) {
432 $rcodes{$i} = $findcode;
433 $rassigned{$lib} .= "$findcode:";
434 print STDERR "New Reason code $i\n" if $debug;
436 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
447 # Rewrite the C source file containing the error details.
449 # First, read any existing reason string definitions:
450 my %err_reason_strings;
451 if (open(IN,"<$cfile")) {
453 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
454 $err_reason_strings{$1} = $2;
462 $hfile =~ /([^\/]+)$/;
463 $hincf = "<${hprefix}$1>";
465 $hincf = "\"$hfile\"";
468 # If static we know the error code at compile time so use it
469 # in error definitions.
473 $pack_errcode = "ERR_LIB_${lib}";
479 $load_errcode = "ERR_LIB_${lib}";
483 open (OUT,">$cfile") || die "Can't open $cfile for writing";
487 /* ====================================================================
488 * Copyright (c) 1999-2005 The OpenSSL Project. All rights reserved.
490 * Redistribution and use in source and binary forms, with or without
491 * modification, are permitted provided that the following conditions
494 * 1. Redistributions of source code must retain the above copyright
495 * notice, this list of conditions and the following disclaimer.
497 * 2. Redistributions in binary form must reproduce the above copyright
498 * notice, this list of conditions and the following disclaimer in
499 * the documentation and/or other materials provided with the
502 * 3. All advertising materials mentioning features or use of this
503 * software must display the following acknowledgment:
504 * "This product includes software developed by the OpenSSL Project
505 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
507 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
508 * endorse or promote products derived from this software without
509 * prior written permission. For written permission, please contact
510 * openssl-core\@OpenSSL.org.
512 * 5. Products derived from this software may not be called "OpenSSL"
513 * nor may "OpenSSL" appear in their names without prior written
514 * permission of the OpenSSL Project.
516 * 6. Redistributions of any form whatsoever must retain the following
518 * "This product includes software developed by the OpenSSL Project
519 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
521 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
522 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
523 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
524 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
525 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
526 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
527 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
528 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
529 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
530 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
531 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
532 * OF THE POSSIBILITY OF SUCH DAMAGE.
533 * ====================================================================
535 * This product includes cryptographic software written by Eric Young
536 * (eay\@cryptsoft.com). This product includes software written by Tim
537 * Hudson (tjh\@cryptsoft.com).
541 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
542 * made to it will be overwritten when the script next updates this file,
543 * only reason strings will be preserved.
547 #include <openssl/err.h>
550 /* BEGIN ERROR CODES */
551 #ifndef OPENSSL_NO_ERR
553 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
554 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
556 static ERR_STRING_DATA ${lib}_str_functs[]=
559 # Add each function code: if a function name is found then use it.
560 foreach $i (@function) {
562 $i =~ /^${lib}_F_(\S+)$/;
564 if(exists $ftrans{$fn}) {
567 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
568 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
574 static ERR_STRING_DATA ${lib}_str_reasons[]=
577 # Add each reason code.
578 foreach $i (@reasons) {
580 my $rstr = "ERR_REASON($i)";
582 if (exists $err_reason_strings{$i}) {
583 $rn = $err_reason_strings{$i};
585 $i =~ /^${lib}_R_(\S+)$/;
587 $rn =~ tr/_[A-Z]/ [a-z]/;
589 $nspc = 40 - length($rstr) unless length($rstr) > 40;
591 print OUT "{${rstr}${nspc},\"$rn\"},\n";
600 ${staticloader}void ERR_load_${lib}_strings(void)
607 #ifndef OPENSSL_NO_ERR
608 ERR_load_strings($load_errcode,${lib}_str_functs);
609 ERR_load_strings($load_errcode,${lib}_str_reasons);
622 #ifdef ${lib}_LIB_NAME
623 static ERR_STRING_DATA ${lib}_lib_name[]=
625 {0 ,${lib}_LIB_NAME},
631 static int ${lib}_lib_error_code=0;
632 static int ${lib}_error_init=1;
634 ${staticloader}void ERR_load_${lib}_strings(void)
636 if (${lib}_lib_error_code == 0)
637 ${lib}_lib_error_code=ERR_get_next_error_library();
639 if (${lib}_error_init)
642 #ifndef OPENSSL_NO_ERR
643 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
644 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
647 #ifdef ${lib}_LIB_NAME
648 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
649 ERR_load_strings(0,${lib}_lib_name);
654 ${staticloader}void ERR_unload_${lib}_strings(void)
656 if (${lib}_error_init == 0)
658 #ifndef OPENSSL_NO_ERR
659 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
660 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
663 #ifdef ${lib}_LIB_NAME
664 ERR_unload_strings(0,${lib}_lib_name);
670 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
672 if (${lib}_lib_error_code == 0)
673 ${lib}_lib_error_code=ERR_get_next_error_library();
674 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
681 undef %err_reason_strings;
684 if($debug && defined(%notrans)) {
685 print STDERR "The following function codes were not translated:\n";
686 foreach(sort keys %notrans)
692 # Make a list of unreferenced function and reason codes
694 foreach (keys %fcodes) {
695 push (@funref, $_) unless exists $ufcodes{$_};
698 foreach (keys %rcodes) {
699 push (@runref, $_) unless exists $urcodes{$_};
702 if($debug && defined(@funref) ) {
703 print STDERR "The following function codes were not referenced:\n";
704 foreach(sort @funref)
710 if($debug && defined(@runref) ) {
711 print STDERR "The following reason codes were not referenced:\n";
712 foreach(sort @runref)