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>,
48 <fips/*.c>, <fips/*/*.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+)/) {
74 } elsif (/^F\s+(\S+)/) {
75 # Add extra function with $1
76 } elsif (/^R\s+(\S+)\s+(\S+)/) {
84 # Scan each header file in turn and make a list of error codes
87 while (($hdr, $lib) = each %libinc)
89 next if($hdr eq "NONE");
90 print STDERR "Scanning header file $hdr\n" if $debug;
91 my $line = "", $def= "", $linenr = 0, $gotfile = 0;
92 if (open(IN, "<$hdr")) {
96 print STDERR "line: $linenr\r" if $debug;
98 last if(/BEGIN\s+ERROR\s+CODES/);
110 if (not /\*\//) { # multiline comment...
111 $line = $_; # ... just accumulate
114 s/\/\*.*?\*\///gs; # wipe it
120 $cpp-- if /^#\s*endif/;
123 $cpp = 1 if /^#.*ifdef.*cplusplus/; # skip "C" declaration
125 next if (/^\#/); # skip preprocessor directives
127 s/{[^{}]*}//gs; # ignore {} blocks
129 if (/\{|\/\*/) { # Add a } so editor works...
137 print STDERR " \r" if $debug;
139 # Delete any DECLARE_ macros
140 $def =~ s/DECLARE_\w+\([\w,\s]+\)//gs;
141 foreach (split /;/, $def) {
143 print STDERR "def: $defnr\r" if $debug;
145 # The goal is to collect function names from function declarations.
150 # Skip over recognized non-function declarations
151 next if(/typedef\W/ or /DECLARE_STACK_OF/ or /TYPEDEF_.*_OF/);
153 # Remove STACK_OF(foo)
154 s/STACK_OF\(\w+\)/void/;
156 # Reduce argument lists to empty ()
157 # fold round brackets recursively: (t(*v)(t),t) -> (t{}{},t) -> {}
159 s/\([^\(\)]+\)/\{\}/gs;
160 s/\(\s*\*\s*(\w+)\s*\{\}\s*\)/$1/gs; #(*f{}) -> f
162 # pretend as we didn't use curly braces: {} -> ()
165 if (/(\w+)\s*\(\).*/s) { # first token prior [first] () is
166 my $name = $1; # a function name!
167 $name =~ tr/[a-z]/[A-Z]/;
169 } elsif (/[\(\)]/ and not (/=/)) {
170 print STDERR "Header $hdr: cannot parse: $_;\n";
174 print STDERR " \r" if $debug;
178 # Scan function and reason codes and store them: keep a note of the
183 if(/^\#define\s+(\S+)\s+(\S+)/) {
186 next if $name =~ /^${lib}err/;
187 unless($name =~ /^${lib}_([RF])_(\w+)$/) {
188 print STDERR "Invalid error code $name\n";
192 $rcodes{$name} = $code;
193 if ($rassigned{$lib} =~ /:$code:/) {
194 print STDERR "!! ERROR: $lib reason code $code assigned twice\n";
196 $rassigned{$lib} .= "$code:";
197 if(!(exists $rextra{$name}) &&
198 ($code > $rmax{$lib}) ) {
202 if ($fassigned{$lib} =~ /:$code:/) {
203 print STDERR "!! ERROR: $lib function code $code assigned twice\n";
205 $fassigned{$lib} .= "$code:";
206 if($code > $fmax{$lib}) {
209 $fcodes{$name} = $code;
216 if (defined($fmax{$lib})) {
217 print STDERR "Max function code fmax" . "{" . "$lib" . "} = $fmax{$lib}\n";
218 $fassigned{$lib} =~ m/^:(.*):$/;
219 @fassigned = sort {$a <=> $b} split(":", $1);
220 print STDERR " @fassigned\n";
222 if (defined($rmax{$lib})) {
223 print STDERR "Max reason code rmax" . "{" . "$lib" . "} = $rmax{$lib}\n";
224 $rassigned{$lib} =~ m/^:(.*):$/;
225 @rassigned = sort {$a <=> $b} split(":", $1);
226 print STDERR " @rassigned\n";
231 if ($rmax{$lib} >= 1000) {
232 print STDERR "!! ERROR: SSL error codes 1000+ are reserved for alerts.\n";
233 print STDERR "!! Any new alerts must be added to $config.\n";
240 # Scan each C source file and look for function and reason codes
241 # This is done by looking for strings that "look like" function or
242 # reason codes: basically anything consisting of all upper case and
243 # numerics which has _F_ or _R_ in it and which has the name of an
244 # error library at the start. This seems to work fine except for the
245 # oddly named structure BIO_F_CTX which needs to be ignored.
246 # If a code doesn't exist in list compiled from headers then mark it
247 # with the value "X" as a place holder to give it a value later.
248 # Store all function and reason codes found in %ufcodes and %urcodes
249 # so all those unreferenced can be printed out.
252 foreach $file (@source) {
253 # Don't parse the error source file.
254 next if exists $cskip{$file};
255 print STDERR "File loaded: ".$file."\r" if $debug;
256 open(IN, "<$file") || die "Can't open source file $file\n";
258 if(/(([A-Z0-9]+)_F_([A-Z0-9_]+))/) {
259 next unless exists $csrc{$2};
260 next if($1 eq "BIO_F_BUFFER_CTX");
262 if(!exists $fcodes{$1}) {
266 $notrans{$1} = 1 unless exists $ftrans{$3};
268 if(/(([A-Z0-9]+)_R_[A-Z0-9_]+)/) {
269 next unless exists $csrc{$2};
271 if(!exists $rcodes{$1}) {
279 print STDERR " \n" if $debug;
281 # Now process each library in turn.
283 foreach $lib (keys %csrc)
285 my $hfile = $hinc{$lib};
286 my $cfile = $csrc{$lib};
287 if(!$fnew{$lib} && !$rnew{$lib}) {
288 print STDERR "$lib:\t\tNo new error codes\n";
289 next unless $rebuild;
291 print STDERR "$lib:\t\t$fnew{$lib} New Functions,";
292 print STDERR " $rnew{$lib} New Reasons.\n";
293 next unless $dowrite;
296 # If we get here then we have some new error codes so we
297 # need to rebuild the header file and C file.
299 # Make a sorted list of error and reason codes for later use.
301 my @function = sort grep(/^${lib}_/,keys %fcodes);
302 my @reasons = sort grep(/^${lib}_/,keys %rcodes);
304 # Rewrite the header file
306 if (open(IN, "<$hfile")) {
307 # Copy across the old file
310 last if (/BEGIN ERROR CODES/);
315 "/* ====================================================================\n",
316 " * Copyright (c) 2001-2011 The OpenSSL Project. All rights reserved.\n",
318 " * Redistribution and use in source and binary forms, with or without\n",
319 " * modification, are permitted provided that the following conditions\n",
322 " * 1. Redistributions of source code must retain the above copyright\n",
323 " * notice, this list of conditions and the following disclaimer. \n",
325 " * 2. Redistributions in binary form must reproduce the above copyright\n",
326 " * notice, this list of conditions and the following disclaimer in\n",
327 " * the documentation and/or other materials provided with the\n",
328 " * distribution.\n",
330 " * 3. All advertising materials mentioning features or use of this\n",
331 " * software must display the following acknowledgment:\n",
332 " * \"This product includes software developed by the OpenSSL Project\n",
333 " * for use in the OpenSSL Toolkit. (http://www.openssl.org/)\"\n",
335 " * 4. The names \"OpenSSL Toolkit\" and \"OpenSSL Project\" must not be used to\n",
336 " * endorse or promote products derived from this software without\n",
337 " * prior written permission. For written permission, please contact\n",
338 " * openssl-core\@openssl.org.\n",
340 " * 5. Products derived from this software may not be called \"OpenSSL\"\n",
341 " * nor may \"OpenSSL\" appear in their names without prior written\n",
342 " * permission of the OpenSSL Project.\n",
344 " * 6. Redistributions of any form whatsoever must retain the following\n",
345 " * acknowledgment:\n",
346 " * \"This product includes software developed by the OpenSSL Project\n",
347 " * for use in the OpenSSL Toolkit (http://www.openssl.org/)\"\n",
349 " * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY\n",
350 " * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\n",
351 " * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR\n",
352 " * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR\n",
353 " * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n",
354 " * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n",
355 " * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;\n",
356 " * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n",
357 " * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n",
358 " * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)\n",
359 " * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED\n",
360 " * OF THE POSSIBILITY OF SUCH DAMAGE.\n",
361 " * ====================================================================\n",
363 " * This product includes cryptographic software written by Eric Young\n",
364 " * (eay\@cryptsoft.com). This product includes software written by Tim\n",
365 " * Hudson (tjh\@cryptsoft.com).\n",
369 "#ifndef HEADER_${lib}_ERR_H\n",
370 "#define HEADER_${lib}_ERR_H\n",
372 "/* BEGIN ERROR CODES */\n";
374 open (OUT, ">$hfile") || die "Can't Open File $hfile for writing\n";
379 /* The following lines are auto generated by the script mkerr.pl. Any changes
380 * made after this point may be overwritten when the script is next run.
385 ${staticloader}void ERR_load_${lib}_strings(void);
390 ${staticloader}void ERR_load_${lib}_strings(void);
391 ${staticloader}void ERR_unload_${lib}_strings(void);
392 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
393 #define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
398 /* Error codes for the $lib functions. */
400 /* Function codes. */
403 foreach $i (@function) {
404 $z=6-int(length($i)/8);
405 if($fcodes{$i} eq "X") {
406 $fassigned{$lib} =~ m/^:([^:]*):/;
408 if (!defined($findcode)) {
409 $findcode = $fmax{$lib};
411 while ($fassigned{$lib} =~ m/:$findcode:/) {
414 $fcodes{$i} = $findcode;
415 $fassigned{$lib} .= "$findcode:";
416 print STDERR "New Function code $i\n" if $debug;
418 printf OUT "#define $i%s $fcodes{$i}\n","\t" x $z;
421 print OUT "\n/* Reason codes. */\n";
423 foreach $i (@reasons) {
424 $z=6-int(length($i)/8);
425 if($rcodes{$i} eq "X") {
426 $rassigned{$lib} =~ m/^:([^:]*):/;
428 if (!defined($findcode)) {
429 $findcode = $rmax{$lib};
431 while ($rassigned{$lib} =~ m/:$findcode:/) {
434 $rcodes{$i} = $findcode;
435 $rassigned{$lib} .= "$findcode:";
436 print STDERR "New Reason code $i\n" if $debug;
438 printf OUT "#define $i%s $rcodes{$i}\n","\t" x $z;
449 # Rewrite the C source file containing the error details.
451 # First, read any existing reason string definitions:
452 my %err_reason_strings;
453 if (open(IN,"<$cfile")) {
455 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
456 $err_reason_strings{$1} = $2;
464 $hfile =~ /([^\/]+)$/;
465 $hincf = "<openssl/$1>";
467 $hincf = "\"$hfile\"";
470 # If static we know the error code at compile time so use it
471 # in error definitions.
475 $pack_errcode = "ERR_LIB_${lib}";
481 $load_errcode = "ERR_LIB_${lib}";
485 open (OUT,">$cfile") || die "Can't open $cfile for writing";
489 /* ====================================================================
490 * Copyright (c) 1999-2011 The OpenSSL Project. All rights reserved.
492 * Redistribution and use in source and binary forms, with or without
493 * modification, are permitted provided that the following conditions
496 * 1. Redistributions of source code must retain the above copyright
497 * notice, this list of conditions and the following disclaimer.
499 * 2. Redistributions in binary form must reproduce the above copyright
500 * notice, this list of conditions and the following disclaimer in
501 * the documentation and/or other materials provided with the
504 * 3. All advertising materials mentioning features or use of this
505 * software must display the following acknowledgment:
506 * "This product includes software developed by the OpenSSL Project
507 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
509 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
510 * endorse or promote products derived from this software without
511 * prior written permission. For written permission, please contact
512 * openssl-core\@OpenSSL.org.
514 * 5. Products derived from this software may not be called "OpenSSL"
515 * nor may "OpenSSL" appear in their names without prior written
516 * permission of the OpenSSL Project.
518 * 6. Redistributions of any form whatsoever must retain the following
520 * "This product includes software developed by the OpenSSL Project
521 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
523 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
524 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
525 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
526 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
527 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
528 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
529 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
530 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
531 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
532 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
533 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
534 * OF THE POSSIBILITY OF SUCH DAMAGE.
535 * ====================================================================
537 * This product includes cryptographic software written by Eric Young
538 * (eay\@cryptsoft.com). This product includes software written by Tim
539 * Hudson (tjh\@cryptsoft.com).
543 /* NOTE: this file was auto generated by the mkerr.pl script: any changes
544 * made to it will be overwritten when the script next updates this file,
545 * only reason strings will be preserved.
549 #include <openssl/err.h>
552 /* BEGIN ERROR CODES */
553 #ifndef OPENSSL_NO_ERR
555 #define ERR_FUNC(func) ERR_PACK($pack_errcode,func,0)
556 #define ERR_REASON(reason) ERR_PACK($pack_errcode,0,reason)
558 static ERR_STRING_DATA ${lib}_str_functs[]=
561 # Add each function code: if a function name is found then use it.
562 foreach $i (@function) {
564 $i =~ /^${lib}_F_(\S+)$/;
566 if(exists $ftrans{$fn}) {
569 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
570 print OUT "{ERR_FUNC($i),\t\"$fn\"},\n";
576 static ERR_STRING_DATA ${lib}_str_reasons[]=
579 # Add each reason code.
580 foreach $i (@reasons) {
582 my $rstr = "ERR_REASON($i)";
584 if (exists $err_reason_strings{$i}) {
585 $rn = $err_reason_strings{$i};
587 $i =~ /^${lib}_R_(\S+)$/;
589 $rn =~ tr/_[A-Z]/ [a-z]/;
591 $nspc = 40 - length($rstr) unless length($rstr) > 40;
593 print OUT "{${rstr}${nspc},\"$rn\"},\n";
602 ${staticloader}void ERR_load_${lib}_strings(void)
604 #ifndef OPENSSL_NO_ERR
606 if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL)
608 ERR_load_strings($load_errcode,${lib}_str_functs);
609 ERR_load_strings($load_errcode,${lib}_str_reasons);
621 #ifdef ${lib}_LIB_NAME
622 static ERR_STRING_DATA ${lib}_lib_name[]=
624 {0 ,${lib}_LIB_NAME},
630 static int ${lib}_lib_error_code=0;
631 static int ${lib}_error_init=1;
633 ${staticloader}void ERR_load_${lib}_strings(void)
635 if (${lib}_lib_error_code == 0)
636 ${lib}_lib_error_code=ERR_get_next_error_library();
638 if (${lib}_error_init)
641 #ifndef OPENSSL_NO_ERR
642 ERR_load_strings(${lib}_lib_error_code,${lib}_str_functs);
643 ERR_load_strings(${lib}_lib_error_code,${lib}_str_reasons);
646 #ifdef ${lib}_LIB_NAME
647 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code,0,0);
648 ERR_load_strings(0,${lib}_lib_name);
653 ${staticloader}void ERR_unload_${lib}_strings(void)
655 if (${lib}_error_init == 0)
657 #ifndef OPENSSL_NO_ERR
658 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_functs);
659 ERR_unload_strings(${lib}_lib_error_code,${lib}_str_reasons);
662 #ifdef ${lib}_LIB_NAME
663 ERR_unload_strings(0,${lib}_lib_name);
669 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
671 if (${lib}_lib_error_code == 0)
672 ${lib}_lib_error_code=ERR_get_next_error_library();
673 ERR_PUT_error(${lib}_lib_error_code,function,reason,file,line);
680 undef %err_reason_strings;
683 if($debug && defined(%notrans)) {
684 print STDERR "The following function codes were not translated:\n";
685 foreach(sort keys %notrans)
691 # Make a list of unreferenced function and reason codes
693 foreach (keys %fcodes) {
694 push (@funref, $_) unless exists $ufcodes{$_};
697 foreach (keys %rcodes) {
698 push (@runref, $_) unless exists $urcodes{$_};
701 if($debug && defined(@funref) ) {
702 print STDERR "The following function codes were not referenced:\n";
703 foreach(sort @funref)
709 if($debug && defined(@runref) ) {
710 print STDERR "The following reason codes were not referenced:\n";
711 foreach(sort @runref)