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(/^\#\s*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";
380 * The following lines are auto generated by the script mkerr.pl. Any changes
381 * made after this point may be overwritten when the script is next run.
386 ${staticloader}void ERR_load_${lib}_strings(void);
391 ${staticloader}void ERR_load_${lib}_strings(void);
392 ${staticloader}void ERR_unload_${lib}_strings(void);
393 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line);
394 # define ${lib}err(f,r) ERR_${lib}_error((f),(r),__FILE__,__LINE__)
399 /* Error codes for the $lib functions. */
401 /* Function codes. */
404 foreach $i (@function) {
406 if($fcodes{$i} eq "X") {
407 $fassigned{$lib} =~ m/^:([^:]*):/;
409 if (!defined($findcode)) {
410 $findcode = $fmax{$lib};
412 while ($fassigned{$lib} =~ m/:$findcode:/) {
415 $fcodes{$i} = $findcode;
416 $fassigned{$lib} .= "$findcode:";
417 print STDERR "New Function code $i\n" if $debug;
419 printf OUT "# define $i%s $fcodes{$i}\n"," " x $z;
422 print OUT "\n/* Reason codes. */\n";
424 foreach $i (@reasons) {
426 if($rcodes{$i} eq "X") {
427 $rassigned{$lib} =~ m/^:([^:]*):/;
429 if (!defined($findcode)) {
430 $findcode = $rmax{$lib};
432 while ($rassigned{$lib} =~ m/:$findcode:/) {
435 $rcodes{$i} = $findcode;
436 $rassigned{$lib} .= "$findcode:";
437 print STDERR "New Reason code $i\n" if $debug;
439 printf OUT "# define $i%s $rcodes{$i}\n"," " x $z;
450 # Rewrite the C source file containing the error details.
452 # First, read any existing reason string definitions:
453 my %err_reason_strings;
454 if (open(IN,"<$cfile")) {
460 if (/{ERR_REASON\(/) {
461 if (/\b(${lib}_R_\w*)\b.*\"(.*)\"/) {
462 $err_reason_strings{$1} = $2;
473 $hfile =~ /([^\/]+)$/;
474 $hincf = "<openssl/$1>";
476 $hincf = "\"$hfile\"";
479 # If static we know the error code at compile time so use it
480 # in error definitions.
484 $pack_errcode = "ERR_LIB_${lib}";
490 $load_errcode = "ERR_LIB_${lib}";
494 open (OUT,">$cfile") || die "Can't open $cfile for writing";
498 /* ====================================================================
499 * Copyright (c) 1999-2011 The OpenSSL Project. All rights reserved.
501 * Redistribution and use in source and binary forms, with or without
502 * modification, are permitted provided that the following conditions
505 * 1. Redistributions of source code must retain the above copyright
506 * notice, this list of conditions and the following disclaimer.
508 * 2. Redistributions in binary form must reproduce the above copyright
509 * notice, this list of conditions and the following disclaimer in
510 * the documentation and/or other materials provided with the
513 * 3. All advertising materials mentioning features or use of this
514 * software must display the following acknowledgment:
515 * "This product includes software developed by the OpenSSL Project
516 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
518 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
519 * endorse or promote products derived from this software without
520 * prior written permission. For written permission, please contact
521 * openssl-core\@OpenSSL.org.
523 * 5. Products derived from this software may not be called "OpenSSL"
524 * nor may "OpenSSL" appear in their names without prior written
525 * permission of the OpenSSL Project.
527 * 6. Redistributions of any form whatsoever must retain the following
529 * "This product includes software developed by the OpenSSL Project
530 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
532 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
533 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
534 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
535 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
536 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
537 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
538 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
539 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
540 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
541 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
542 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
543 * OF THE POSSIBILITY OF SUCH DAMAGE.
544 * ====================================================================
546 * This product includes cryptographic software written by Eric Young
547 * (eay\@cryptsoft.com). This product includes software written by Tim
548 * 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[] = {
570 # Add each function code: if a function name is found then use it.
571 foreach $i (@function) {
573 $i =~ /^${lib}_F_(\S+)$/;
575 if(exists $ftrans{$fn}) {
578 # print OUT "{ERR_PACK($pack_errcode,$i,0),\t\"$fn\"},\n";
579 if(length($i) + length($fn) > 58) {
580 print OUT " {ERR_FUNC($i),\n \"$fn\"},\n";
582 print OUT " {ERR_FUNC($i), \"$fn\"},\n";
589 static ERR_STRING_DATA ${lib}_str_reasons[] = {
591 # Add each reason code.
592 foreach $i (@reasons) {
594 my $rstr = "ERR_REASON($i)";
595 if (exists $err_reason_strings{$i}) {
596 $rn = $err_reason_strings{$i};
598 $i =~ /^${lib}_R_(\S+)$/;
600 $rn =~ tr/_[A-Z]/ [a-z]/;
602 if(length($i) + length($rn) > 56) {
603 print OUT " {${rstr},\n \"$rn\"},\n";
605 print OUT " {${rstr}, \"$rn\"},\n";
615 ${staticloader}void ERR_load_${lib}_strings(void)
617 #ifndef OPENSSL_NO_ERR
619 if (ERR_func_error_string(${lib}_str_functs[0].error) == NULL) {
620 ERR_load_strings($load_errcode, ${lib}_str_functs);
621 ERR_load_strings($load_errcode, ${lib}_str_reasons);
633 #ifdef ${lib}_LIB_NAME
634 static ERR_STRING_DATA ${lib}_lib_name[] = {
635 {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) {
649 ${lib}_error_init = 0;
650 #ifndef OPENSSL_NO_ERR
651 ERR_load_strings(${lib}_lib_error_code, ${lib}_str_functs);
652 ERR_load_strings(${lib}_lib_error_code, ${lib}_str_reasons);
655 #ifdef ${lib}_LIB_NAME
656 ${lib}_lib_name->error = ERR_PACK(${lib}_lib_error_code, 0, 0);
657 ERR_load_strings(0, ${lib}_lib_name);
662 ${staticloader}void ERR_unload_${lib}_strings(void)
664 if (${lib}_error_init == 0) {
665 #ifndef OPENSSL_NO_ERR
666 ERR_unload_strings(${lib}_lib_error_code, ${lib}_str_functs);
667 ERR_unload_strings(${lib}_lib_error_code, ${lib}_str_reasons);
670 #ifdef ${lib}_LIB_NAME
671 ERR_unload_strings(0, ${lib}_lib_name);
673 ${lib}_error_init = 1;
677 ${staticloader}void ERR_${lib}_error(int function, int reason, char *file, int line)
679 if (${lib}_lib_error_code == 0)
680 ${lib}_lib_error_code = ERR_get_next_error_library();
681 ERR_PUT_error(${lib}_lib_error_code, function, reason, file, line);
688 undef %err_reason_strings;
691 if($debug && %notrans) {
692 print STDERR "The following function codes were not translated:\n";
693 foreach(sort keys %notrans)
699 # Make a list of unreferenced function and reason codes
701 foreach (keys %fcodes) {
702 push (@funref, $_) unless exists $ufcodes{$_};
705 foreach (keys %rcodes) {
706 push (@runref, $_) unless exists $urcodes{$_};
709 if($debug && @funref) {
710 print STDERR "The following function codes were not referenced:\n";
711 foreach(sort @funref)
717 if($debug && @runref) {
718 print STDERR "The following reason codes were not referenced:\n";
719 foreach(sort @runref)