Break circular dependency between pem and err.
[openssl.git] / crypto / err / err_code.pl
index 5b893001c62aad4aa0417a6aa6deeb9d1660c35d..8b510c96fcf88135aad20a222d1e754ab0e881c3 100644 (file)
@@ -1,79 +1,66 @@
-#!/usr/local/bin/perl
+#!/usr/local/bin/perl -w
 
-%errfile=(
-       "ERR",  "NONE",
-       "BN",   "bn/bn.err",
-       "RSA",  "rsa/rsa.err",
-       "DSA",  "dsa/dsa.err",
-       "DH",   "dh/dh.err",
-       "EVP",  "evp/evp.err",
-       "BUF",  "buffer/buffer.err",
-       "BIO",  "bio/bio.err",
-       "OBJ",  "objects/objects.err",
-       "PEM",  "pem/pem.err",
-       "X509", "x509/x509.err",
-       "METH", "meth/meth.err",
-       "ASN1", "asn1/asn1.err",
-       "CONF", "conf/conf.err",
-       "PROXY","proxy/proxy.err",
-       "PKCS7","pkcs7/pkcs7.err",
-       "RSAREF","../rsaref/rsaref.err",
-       "SSL",  "../ssl/ssl.err",
-       "SSL2", "../ssl/ssl2.err",
-       "SSL3", "../ssl/ssl3.err",
-       "SSL23","../ssl/ssl23.err",
-       );
+# Modified by Steve Henson. It should now read in the .err
+# file and only add new error codes, retaining the old
+# numbers. 
 
-$function{'RSAREF_F_RSA_BN2BIN'}=1;
-$function{'RSAREF_F_RSA_PRIVATE_DECRYPT'}=1;
-$function{'RSAREF_F_RSA_PRIVATE_ENCRYPT'}=1;
-$function{'RSAREF_F_RSA_PUBLIC_DECRYPT'}=1;
-$function{'RSAREF_F_RSA_PUBLIC_ENCRYPT'}=1;
-$function{'SSL_F_CLIENT_CERTIFICATE'}=1;
+# Before it re-sorted new codes and re-ordered the whole thing. 
+# This is the motivation for the change: the re numbering caused large
+# patch files even if only one error or reason code was added.
+# To force regeneration of all error codes (the old behaviour) use the
+# -regen flag.
 
-$r_value{'SSL_R_SSLV3_ALERT_UNEXPECTED_MESSAGE'}=      1010;
-$r_value{'SSL_R_SSLV3_ALERT_BAD_RECORD_MAC'}=  1020;
-$r_value{'SSL_R_SSLV3_ALERT_DECOMPRESSION_FAILURE'}=1030;
-$r_value{'SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE'}=       1040;
-$r_value{'SSL_R_SSLV3_ALERT_NO_CERTIFICATE'}=  1041;
-$r_value{'SSL_R_SSLV3_ALERT_BAD_CERTIFICATE'}= 1042;
-$r_value{'SSL_R_SSLV3_ALERT_UNSUPPORTED_CERTIFICATE'}=1043;
-$r_value{'SSL_R_SSLV3_ALERT_CERTIFICATE_REVOKED'}=     1044;
-$r_value{'SSL_R_SSLV3_ALERT_CERTIFICATE_EXPIRED'}=     1045;
-$r_value{'SSL_R_SSLV3_ALERT_CERTIFICATE_UNKNOWN'}=     1046;
-$r_value{'SSL_R_SSLV3_ALERT_ILLEGAL_PARAMETER'}=       1047;
+$regen = 0;
 
-$r_value{'RSAREF_R_CONTENT_ENCODING'}= 0x0400;
-$r_value{'RSAREF_R_DATA'}=             0x0401;
-$r_value{'RSAREF_R_DIGEST_ALGORITHM'}= 0x0402;
-$r_value{'RSAREF_R_ENCODING'}=         0x0403;
-$r_value{'RSAREF_R_KEY'}=              0x0404;
-$r_value{'RSAREF_R_KEY_ENCODING'}=     0x0405;
-$r_value{'RSAREF_R_LEN'}=              0x0406;
-$r_value{'RSAREF_R_MODULUS_LEN'}=      0x0407;
-$r_value{'RSAREF_R_NEED_RANDOM'}=      0x0408;
-$r_value{'RSAREF_R_PRIVATE_KEY'}=      0x0409;
-$r_value{'RSAREF_R_PUBLIC_KEY'}=       0x040a;
-$r_value{'RSAREF_R_SIGNATURE'}=                0x040b;
-$r_value{'RSAREF_R_SIGNATURE_ENCODING'}=0x040c;
-$r_value{'RSAREF_R_ENCRYPTION_ALGORITHM'}=0x040d;
-
-$last="";
-while (<>)
+while (@ARGV)
        {
-       if (/err\(([A-Z0-9]+_F_[0-9A-Z_]+)\s*,\s*([0-9A-Z]+_R_[0-9A-Z_]+)\s*\)/)
+       $in=shift(@ARGV);
+       if ($in =~ /^-conf$/)
                {
-               if ($1 != $last)
+               $in=shift(@ARGV);
+               open(IN,"<$in") || die "unable to open '$in'\n";
+               while (<IN>)
                        {
-                       if ($function{$1} == 0)
-                               {
-                               printf STDERR "$. $1 is bad\n";
-                               }
+                       s/#.*$//;
+                       s/\s+$//;
+                       next if (/^$/);
+                       if (/^L\s+(\S+)\s+(\S+)$/)
+                               { $errfile{$1}=$2; }
+                       elsif (/^F\s+(\S+)$/)
+                               { $function{$1}=1; }
+                       elsif (/^R\s+(\S+)\s+(\S+)$/)
+                               { $r_value{$1}=$2; }
+                       else { die "bad input line: $in:$.\n"; }
+                       }
+               close(IN);
+               next;
+               }
+       elsif ($in =~ /^-regen/)
+               {
+               $regen = 1;
+               next;
+       }
+
+       open(IN,"<$in") || die "unable to open '$in'\n";
+       $last="";
+       while (<IN>)
+               {
+               if (/err\(([A-Z0-9]+_F_[0-9A-Z_]+)\s*,\s*([0-9A-Z]+_R_[0-9A-Z_]+)\s*\)/)
+                       {
+# Not sure what this was supposed to be for: it's broken anyway [steve]
+#                      if ($1 != $last)
+#                              {
+#                              if ($function{$1} == 0)
+#                                      {
+#                                      printf STDERR "$. $1 is bad\n";
+#                                      }
+#                              }
+                       $function{$1}++;
+                       $last=$1;
+                       $reason{$2}++;
                        }
-               $function{$1}++;
-               $last=$1;
-               $reason{$2}++;
                }
+       close(IN);
        }
 
 foreach (keys %function,keys %reason)
@@ -86,41 +73,108 @@ foreach (keys %function,keys %reason)
 @R=sort keys %reason;
 foreach $j (sort keys %prefix)
        {
+       next if !defined $errfile{$j};
        next if $errfile{$j} eq "NONE";
        printf STDERR "doing %-6s - ",$j;
-       open(OUT,">$errfile{$j}") || die "unable to open '$errfile{$j}':$!\n";
        @f=grep(/^${j}_/,@F);
        @r=grep(/^${j}_/,@R);
-       $num=100;
+       if (defined($errfile{$j}))
+               {
+               read_errcodes($errfile{$j});
+               # Check to see if any new codes: if not ignore.
+               $new_codes = 0;
+               foreach (@f) {
+                       if(!exists $func_codes{$_}) {
+                               $new_codes = 1;
+                               last;
+                       }
+               }
+               if(!$new_codes) {
+                       foreach (@r) {
+                               if(!exists $reason_codes{$_}) {
+                                       $new_codes = 1;
+                                       last;
+                               }
+                       }
+               }
+               if(!$new_codes) {
+                       print STDERR "No New Codes\n";
+                       next;
+               }
+               open(OUT,">$errfile{$j}") ||
+                       die "unable to open '$errfile{$j}':$!\n";
+               $close_file=1;
+               }
+       else
+               {
+               $min_func = 100;
+               $min_reason = 100;
+               *OUT=*STDOUT;
+               $close_file=0;
+               }
+       $num=$min_func;
        print OUT "/* Error codes for the $j functions. */\n\n";
        print OUT "/* Function codes. */\n";
        $f_count=0;
        foreach $i (@f)
                {
                $z=6-int(length($i)/8);
-               printf OUT "#define $i%s $num\n","\t" x $z;
-               $num++;
+               if(exists $func_codes{$i}) {
+                       printf OUT "#define $i%s $func_codes{$i}\n","\t" x $z;
+               } else {
+                       printf OUT "#define $i%s $num\n","\t" x $z;
+                       $num++;
+               }
                $f_count++;
                }
-       $num=100;
+       $num=$min_reason;
        print OUT "\n/* Reason codes. */\n";
        $r_count=0;
        foreach $i (@r)
                {
                $z=6-int(length($i)/8);
-               if (defined($r_value{$i}))
-                       {
+               if (exists $reason_codes{$i}) {
+                       printf OUT "#define $i%s $reason_codes{$i}\n","\t" x $z;
+               } elsif (exists $r_value{$i}) {
                        printf OUT "#define $i%s $r_value{$i}\n","\t" x $z;
-                       }
-               else
-                       {
+               } else {
                        printf OUT "#define $i%s $num\n","\t" x $z;
                        $num++;
-                       }
+               }
                $r_count++;
                }
-       close(OUT);
+       close(OUT) if $close_file;
 
        printf STDERR "%3d functions, %3d reasons\n",$f_count,$r_count;
        }
 
+# Read in the error codes and populate %function and %reason with the
+# old codes. Also define $min_func and $min_reason with the smallest
+# unused function and reason codes. Care is needed because the
+# config file can define larger reason codes and these should be
+# ignored.
+
+sub read_errcodes {
+$file = $_[0];
+$min_func = 100;
+$min_reason = 100;
+undef %func_codes;
+undef %reason_codes;
+return if ($regen);
+if (open IN, $file) {
+       while(<IN>) {
+               if(/^#define\s*(\S*)\s*(\S*)/) {
+                       if (exists $function{$1} ) {
+                               if($2 >= $min_func) {$min_func = $2 + 1;}
+                               $func_codes{$1} = $2;
+                       } elsif ((defined %reason) && exists $reason{$1}) {
+                               $reason_codes{$1} = $2;
+                               if( !(exists $r_value{$1})  &&
+                                                ($2 >= $min_reason))
+                                                        {$min_reason = $2 + 1;}
+                       }
+               }
+       }
+       close IN;
+}
+}