generate_errors.pl 6.1 KB
Newer Older
1
#!/usr/bin/perl
2 3

# Generate error.c
4
#
5 6
# Usage: ./generate_errors.pl or scripts/generate_errors.pl without arguments,
# or generate_errors.pl include_dir data_dir error_file
7 8 9

use strict;

10 11 12 13 14 15 16 17 18
my ($include_dir, $data_dir, $error_file);

if( @ARGV ) {
    die "Invalid number of arguments" if scalar @ARGV != 3;
    ($include_dir, $data_dir, $error_file) = @ARGV;

    -d $include_dir or die "No such directory: $include_dir\n";
    -d $data_dir or die "No such directory: $data_dir\n";
} else {
19
    $include_dir = 'include/mbedtls';
20 21 22 23 24 25 26 27 28 29
    $data_dir = 'scripts/data_files';
    $error_file = 'library/error.c';

    unless( -d $include_dir && -d $data_dir ) {
        chdir '..' or die;
        -d $include_dir && -d $data_dir
            or die "Without arguments, must be run from root or scripts\n"
    }
}

30 31
my $error_format_file = $data_dir.'/error.fmt';

32
my @low_level_modules = ( "AES", "ASN1", "BLOWFISH", "CAMELLIA", "BIGNUM",
33
                          "BASE64", "XTEA", "PBKDF2", "OID",
34
                          "PADLOCK", "DES", "NET", "CTR_DRBG", "ENTROPY",
35
                          "HMAC_DRBG", "MD2", "MD4", "MD5", "RIPEMD160",
36
                          "SHA1", "SHA256", "SHA512", "GCM", "THREADING", "CCM" );
37
my @high_level_modules = ( "PEM", "X509", "DHM", "RSA", "ECP", "MD", "CIPHER", "SSL",
38
                           "PK", "PKCS12", "PKCS5" );
39 40 41 42 43 44 45 46 47 48

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;

49
open(GREP, "grep \"define MBEDTLS_ERR_\" $include_dir/* |") || die("Failure when calling grep: $!");
50 51 52 53 54 55 56 57 58

my $ll_old_define = "";
my $hl_old_define = "";

my $ll_code_check = "";
my $hl_code_check = "";

my $headers = "";

59 60
my %error_codes_seen;

61 62
while (my $line = <GREP>)
{
63
    next if ($line =~ /compat-1.2.h/);
64
    my ($error_name, $error_code) = $line =~ /(MBEDTLS_ERR_\w+)\s+\-(0x\w+)/;
65
    my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//;
66 67 68 69

    die "Duplicated error code: $error_code ($error_name)\n"
        if( $error_codes_seen{$error_code}++ );

70
    $description =~ s/\\/\\\\/g;
71 72 73 74
    if ($description eq "") {
        $description = "DESCRIPTION MISSING";
        warn "Missing description for $error_name\n";
    }
75

76
    my ($module_name) = $error_name =~ /^MBEDTLS_ERR_([^_]+)/;
77 78 79

    # Fix faulty ones
    $module_name = "BIGNUM" if ($module_name eq "MPI");
80
    $module_name = "CTR_DRBG" if ($module_name eq "CTR");
81
    $module_name = "HMAC_DRBG" if ($module_name eq "HMAC");
82 83

    my $define_name = $module_name;
84
    $define_name = "X509_USE,X509_CREATE" if ($define_name eq "X509");
85
    $define_name = "ASN1_PARSE" if ($define_name eq "ASN1");
86
    $define_name = "SSL_TLS" if ($define_name eq "SSL");
87
    $define_name = "PEM_PARSE,PEM_WRITE" if ($define_name eq "PEM");
88 89 90 91 92

    my $include_name = $module_name;
    $include_name =~ tr/A-Z/a-z/;
    $include_name = "" if ($include_name eq "asn1");

93 94 95
    # Fix faulty ones
    $include_name = "net_sockets" if ($module_name eq "NET");

96 97 98 99
    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)
    {
100
        printf("Error: Do not know how to handle: $module_name\n");
101 102 103 104 105 106
        exit 1;
    }

    my $code_check;
    my $old_define;
    my $white_space;
107
    my $first;
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125

    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 "")
        {
126 127 128 129 130
            ${$code_check} .= "#endif /* ";
            $first = 0;
            foreach my $dep (split(/,/, ${$old_define}))
            {
                ${$code_check} .= " || " if ($first++);
131
                ${$code_check} .= "MBEDTLS_${dep}_C";
132 133
            }
            ${$code_check} .= " */\n\n";
134 135
        }

136 137 138 139 140 141 142 143
        ${$code_check} .= "#if ";
        $headers .= "#if " if ($include_name ne "");
        $first = 0;
        foreach my $dep (split(/,/, ${define_name}))
        {
            ${$code_check} .= " || " if ($first);
            $headers       .= " || " if ($first++);

144 145
            ${$code_check} .= "defined(MBEDTLS_${dep}_C)";
            $headers       .= "defined(MBEDTLS_${dep}_C)" if
146 147 148
                                                    ($include_name ne "");
        }
        ${$code_check} .= "\n";
149
        $headers .= "\n#include \"mbedtls/${include_name}.h\"\n".
150 151 152 153
                    "#endif\n\n" if ($include_name ne "");
        ${$old_define} = $define_name;
    }

154
    if ($error_name eq "MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE")
155 156 157
    {
        ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
                          "${white_space}\{\n".
158
                          "${white_space}    mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n".
159 160 161 162 163 164
                          "${white_space}    return;\n".
                          "${white_space}}\n"
    }
    else
    {
        ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
165
                          "${white_space}    mbedtls_snprintf( buf, buflen, \"$module_name - $description\" );\n"
166
    }
167 168 169 170
};

if ($ll_old_define ne "")
{
171 172 173 174 175 176 177 178
    $ll_code_check .= "#endif /* ";
    my $first = 0;
    foreach my $dep (split(/,/, $ll_old_define))
    {
        $ll_code_check .= " || " if ($first++);
        $ll_code_check .= "MBEDTLS_${dep}_C";
    }
    $ll_code_check .= " */\n";
179 180 181
}
if ($hl_old_define ne "")
{
182 183 184 185 186 187 188 189
    $hl_code_check .= "#endif /* ";
    my $first = 0;
    foreach my $dep (split(/,/, $hl_old_define))
    {
        $hl_code_check .= " || " if ($first++);
        $hl_code_check .= "MBEDTLS_${dep}_C";
    }
    $hl_code_check .= " */\n";
190 191 192 193 194 195 196 197 198
}

$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);