blob: d365dc010311d184ddb68d1618fbec1a51863dbd [file] [log] [blame]
Paul Bakker9d781402011-05-09 16:17:09 +00001#!/usr/bin/perl
2#
3
4use strict;
5
6my $include_dir = shift or die "Missing include directory";
7my $data_dir = shift or die "Missing data directory";
8my $error_file = shift or die "Missing destination file";
9my $error_format_file = $data_dir.'/error.fmt';
10
11my @low_level_modules = ( "AES", "ASN1", "CAMELLIA", "BIGNUM", "BASE64", "XTEA",
12 "PADLOCK", "DES", "NET" );
13my @high_level_modules = ( "PEM", "X509", "DHM", "RSA", "MD", "CIPHER", "SSL" );
14
15my $line_separator = $/;
16undef $/;
17
18open(FORMAT_FILE, "$error_format_file") or die "Opening error format file '$error_format_file': $!";
19my $error_format = <FORMAT_FILE>;
20close(FORMAT_FILE);
21
22$/ = $line_separator;
23
24open(GREP, "/bin/grep \"define POLARSSL_ERR_\" $include_dir/* |") || die("Failure when calling grep: $!");
25
26my $ll_old_define = "";
27my $hl_old_define = "";
28
29my $ll_code_check = "";
30my $hl_code_check = "";
31
32my $headers = "";
33
34while (my $line = <GREP>)
35{
36 my ($error_name, $error_code) = $line =~ /(POLARSSL_ERR_\w+)\s+\-(0x\w+)/;
37 my ($description) = $line =~ /\/\*\*< (.*?)\.? \*\//;
38 $description =~ s/\\/\\\\/g;
39 $description = "DESCRIPTION MISSING" if ($description eq "");
40
41 my ($module_name) = $error_name =~ /^POLARSSL_ERR_([^_]+)/;
42
43 # Fix faulty ones
44 $module_name = "BIGNUM" if ($module_name eq "MPI");
45
46 my $define_name = $module_name;
47 $define_name = "X509_PARSE" if ($define_name eq "X509");
48 $define_name = "X509_PARSE" if ($define_name eq "ASN1");
49
50 my $include_name = $module_name;
51 $include_name =~ tr/A-Z/a-z/;
52 $include_name = "" if ($include_name eq "asn1");
53
54 my $found_ll = grep $_ eq $module_name, @low_level_modules;
55 my $found_hl = grep $_ eq $module_name, @high_level_modules;
56 if (!$found_ll && !$found_hl)
57 {
58 printf("Error: Do not know how to handle: $module_name\n");
59 exit 1;
60 }
61
62 my $code_check;
63 my $old_define;
64 my $white_space;
65
66 if ($found_ll)
67 {
68 $code_check = \$ll_code_check;
69 $old_define = \$ll_old_define;
70 $white_space = ' ';
71 }
72 else
73 {
74 $code_check = \$hl_code_check;
75 $old_define = \$hl_old_define;
76 $white_space = ' ';
77 }
78
79 if ($define_name ne ${$old_define})
80 {
81 if (${$old_define} ne "")
82 {
83 ${$code_check} .= "#endif /* POLARSSL_${$old_define}_C */\n\n";
84 }
85
86 ${$code_check} .= "#if defined(POLARSSL_${define_name}_C)\n";
87 $headers .= "#if defined(POLARSSL_${define_name}_C)\n".
88 "#include \"polarssl/${include_name}.h\"\n".
89 "#endif\n\n" if ($include_name ne "");
90 ${$old_define} = $define_name;
91 }
92
93 ${$code_check} .= "${white_space}if( use_ret == -($error_name) )\n".
94 "${white_space} snprintf( buf, buflen, \"$module_name - $description\" );\n"
95};
96
97if ($ll_old_define ne "")
98{
99 $ll_code_check .= "#endif /* POLARSSL_${ll_old_define}_C */\n\n";
100}
101if ($hl_old_define ne "")
102{
103 $hl_code_check .= "#endif /* POLARSSL_${hl_old_define}_C */\n\n";
104}
105
106$error_format =~ s/HEADER_INCLUDED\n/$headers/g;
107$error_format =~ s/LOW_LEVEL_CODE_CHECKS\n/$ll_code_check/g;
108$error_format =~ s/HIGH_LEVEL_CODE_CHECKS\n/$hl_code_check/g;
109
110open(ERROR_FILE, ">$error_file") or die "Opening destination file '$error_file': $!";
111print ERROR_FILE $error_format;
112close(ERROR_FILE);