- A error_strerror function() has been added to translate between error codes and their description.
 - The error codes have been remapped and combining error codes is now done with a PLUS instead of an OR as error codes used are negative.
 - Descriptions to all error codes have been added.
 - Generation script for error.c has been created to automatically generate error.c from the available error definitions in the headers.


diff --git a/scripts/data_files/error.fmt b/scripts/data_files/error.fmt
new file mode 100644
index 0000000..e9dd741
--- /dev/null
+++ b/scripts/data_files/error.fmt
@@ -0,0 +1,85 @@
+/*
+ *  Error message information
+ *
+ *  Copyright (C) 2006-2010, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "polarssl/config.h"
+
+#if defined(POLARSSL_ERROR_C)
+
+HEADER_INCLUDED
+
+#include <string.h>
+
+void error_strerror( int ret, char *buf, size_t buflen )
+{
+    size_t len;
+    int use_ret;
+
+    memset( buf, 0x00, buflen );
+     
+    if( ret < 0 )
+        ret = -ret;
+
+    if( ret & 0xFF80 )
+    {
+        use_ret = ret & 0xFF80;
+
+        // High level error codes
+        //
+HIGH_LEVEL_CODE_CHECKS
+        if( strlen( buf ) == 0 )
+            snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
+    }
+
+    use_ret = ret & ~0xFF80;
+
+    if( use_ret == 0 )
+        return;
+
+    // If high level code is present, make a concatenation between both
+    // error strings.
+    //
+    len = strlen( buf );
+
+    if( len > 0 )
+    {
+        if( buflen - len < 5 )
+            return;
+
+        snprintf( buf + len, buflen - len, " : " );
+
+        buf += len + 3;
+        buflen -= len + 3;
+    }
+
+    // Low level error codes
+    //
+LOW_LEVEL_CODE_CHECKS
+    if( strlen( buf ) != 0 )
+        return;
+
+    snprintf( buf, buflen, "UNKNOWN ERROR CODE (%04X)", use_ret );
+}
+
+#endif /* POLARSSL_VERBOSE_ERROR */
diff --git a/scripts/generate_errors.pl b/scripts/generate_errors.pl
new file mode 100755
index 0000000..d365dc0
--- /dev/null
+++ b/scripts/generate_errors.pl
@@ -0,0 +1,112 @@
+#!/usr/bin/perl
+#
+
+use strict;
+
+my $include_dir = shift or die "Missing include directory";
+my $data_dir = shift or die "Missing data directory";
+my $error_file = shift or die "Missing destination file";
+my $error_format_file = $data_dir.'/error.fmt';
+
+my @low_level_modules = ( "AES", "ASN1", "CAMELLIA", "BIGNUM", "BASE64", "XTEA",
+                          "PADLOCK", "DES", "NET" );
+my @high_level_modules = ( "PEM", "X509", "DHM", "RSA", "MD", "CIPHER", "SSL" );
+
+my $line_separator = $/;
+undef $/;
+
+open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!";
+my $error_format = <FORMAT_FILE>;
+close(FORMAT_FILE);
+
+$/ = $line_separator;
+
+open(GREP, "/bin/grep \"define POLARSSL_ERR_\" $include_dir/* |") || die("Failure when calling grep: $!");
+
+my $ll_old_define = "";
+my $hl_old_define = "";
+
+my $ll_code_check = "";
+my $hl_code_check = "";
+
+my $headers = "";
+
+while (my $line = <GREP>)
+{
+    my ($error_name, $error_code) = $line =~ /(POLARSSL_ERR_\w+)\s+\-(0x\w+)/;
+    my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//;
+    $description =~ s/\\/\\\\/g;
+    $description = "DESCRIPTION MISSING" if ($description eq "");
+
+    my ($module_name) = $error_name =~ /^POLARSSL_ERR_([^_]+)/;
+
+    # Fix faulty ones
+    $module_name = "BIGNUM" if ($module_name eq "MPI");
+
+    my $define_name = $module_name;
+    $define_name = "X509_PARSE" if ($define_name eq "X509");
+    $define_name = "X509_PARSE" if ($define_name eq "ASN1");
+
+    my $include_name = $module_name;
+    $include_name =~ tr/A-Z/a-z/;
+    $include_name = "" if ($include_name eq "asn1");
+
+    my $found_ll = grep $_ eq $module_name, @low_level_modules;
+    my $found_hl = grep $_ eq $module_name, @high_level_modules;
+    if (!$found_ll && !$found_hl)
+    {
+        printf("Error: Do not know how to handle: $module_name\n");
+        exit 1;
+    }
+
+    my $code_check;
+    my $old_define;
+    my $white_space;
+
+    if ($found_ll)
+    {
+        $code_check = \$ll_code_check;
+        $old_define = \$ll_old_define;
+        $white_space = '    ';
+    }
+    else
+    {
+        $code_check = \$hl_code_check;
+        $old_define = \$hl_old_define;
+        $white_space = '        ';
+    }
+
+    if ($define_name ne ${$old_define})
+    {
+        if (${$old_define} ne "")
+        {
+            ${$code_check} .= "#endif /* POLARSSL_${$old_define}_C */\n\n";
+        }
+
+        ${$code_check} .= "#if defined(POLARSSL_${define_name}_C)\n";
+        $headers .= "#if defined(POLARSSL_${define_name}_C)\n".
+                    "#include \"polarssl/${include_name}.h\"\n".
+                    "#endif\n\n" if ($include_name ne "");
+        ${$old_define} = $define_name;
+    }
+
+    ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
+                      "${white_space}    snprintf( buf, buflen, \"$module_name - $description\" );\n"
+};
+
+if ($ll_old_define ne "")
+{
+    $ll_code_check .= "#endif /* POLARSSL_${ll_old_define}_C */\n\n";
+}
+if ($hl_old_define ne "")
+{
+    $hl_code_check .= "#endif /* POLARSSL_${hl_old_define}_C */\n\n";
+}
+
+$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
+$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
+$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
+
+open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
+print ERROR_FILE $error_format;
+close(ERROR_FILE);