8 use File
::Basename
'basename';
9 use File
::Glob
'bsd_glob';
13 open my $fh, "<", $f or die "FATAL: read_rawfile() cannot open file '$f': $!";
15 return do { local $/; <$fh> };
20 die "FATAL: write_file() no data" unless defined $data;
21 open my $fh, ">", $f or die "FATAL: write_file() cannot open file '$f': $!";
23 print $fh $data or die "FATAL: write_file() cannot write to '$f': $!";
24 close $fh or die "FATAL: write_file() cannot close '$f': $!";
29 my @all_files = (bsd_glob
("makefile*"), bsd_glob
("*.sh"), bsd_glob
("*.pl"));
30 find
({ wanted
=>sub { push @all_files, $_ if -f
$_ }, no_chdir
=>1 }, qw
/src tests demos/);
33 for my $file (sort @all_files) {
34 next unless $file =~ /\.(c|h|pl|py|sh)$/ || basename
($file) =~ /^makefile/i;
37 my $content = read_file
($file);
38 push @
{$troubles->{crlf_line_end
}}, '?' if $content =~ /\r/;
39 for my $l (split /\n/, $content) {
40 push @
{$troubles->{merge_conflict
}}, $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/;
41 push @
{$troubles->{trailing_space
}}, $lineno if $l =~ / $/;
42 push @
{$troubles->{tab
}}, $lineno if $l =~ /\t/ && basename
($file) !~ /^makefile/i;
43 push @
{$troubles->{non_ascii_char
}}, $lineno if $l =~ /[^[:ascii:]]/;
44 push @
{$troubles->{cpp_comment
}}, $lineno if $file =~ /\.(c|h)$/ && ($l =~ /\s\/\
// || $l =~ /\/\
/\s/);
45 # in ./src we prefer using XMEMCPY, XMALLOC, XFREE ...
46 push @
{$troubles->{unwanted_memcpy
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bmemcpy\s
*\
(/;
47 push @
{$troubles->{unwanted_malloc
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bmalloc\s
*\
(/;
48 push @
{$troubles->{unwanted_realloc
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\brealloc\s
*\
(/;
49 push @
{$troubles->{unwanted_calloc
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bcalloc\s
*\
(/;
50 push @
{$troubles->{unwanted_free
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /[^>.]\bfree\s
*\
(/;
51 push @
{$troubles->{unwanted_memset
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bmemset\s
*\
(/;
52 push @
{$troubles->{unwanted_memcpy
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bmemcpy\s
*\
(/;
53 push @
{$troubles->{unwanted_memmove
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bmemmove\s
*\
(/;
54 push @
{$troubles->{unwanted_memcmp
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bmemcmp\s
*\
(/;
55 push @
{$troubles->{unwanted_strcmp
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bstrcmp\s
*\
(/;
56 push @
{$troubles->{unwanted_strcpy
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bstrcpy\s
*\
(/;
57 push @
{$troubles->{unwanted_strlen
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bstrlen\s
*\
(/;
58 push @
{$troubles->{unwanted_strncpy
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bstrncpy\s
*\
(/;
59 push @
{$troubles->{unwanted_clock
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bclock\s
*\
(/;
60 push @
{$troubles->{unwanted_qsort
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bqsort\s
*\
(/;
61 push @
{$troubles->{sizeof_no_brackets
}}, $lineno if $file =~ /^src\/.*\
.c
$/ && $l =~ /\bsizeof\s
*[^\
(]/;
62 if ($file =~ m
|src
/.*\
.c
$| &&
63 $file !~ m
|src
/ciphers/.*\
.c
$| &&
64 $file !~ m
|src
/math/.+_desc
.c
$| &&
65 $file !~ m
|src
/pk/ec25519
/tweetnacl
.c
$| &&
66 $file !~ m
|src
/stream/sober
128/sober128_stream
.c
$| &&
67 $l =~ /^static(\s+[a-zA-Z0-9_]+)+\s++([^s][a-zA-Z0-9_]+)\s*\(/) {
68 push @
{$troubles->{staticfunc_name
}}, "$2";
70 if ($file =~ m
|src
/.*\.[ch]$| && $l =~ /^\s
*#\s*define\s+(_[A-Z_][a-zA-Z0-9_]*)\b/) {
72 push @
{$troubles->{invalid_macro_name
}}, "$lineno($n)"
73 unless ($file eq 'src/headers/tomcrypt_cfg.h' && $n eq '__has_builtin') ||
74 ($file eq 'src/headers/tomcrypt_cfg.h' && $n eq '_WIN32_WINNT') ||
75 ($file eq 'src/prngs/rng_get_bytes.c' && $n eq '_WIN32_WINNT');
79 for my $k (sort keys %$troubles) {
80 warn "[$k] $file line:" . join(",", @
{$troubles->{$k}}) . "\n";
85 warn( $fails > 0 ?
"check-source: FAIL $fails\n" : "check-source: PASS\n" );
91 my $cust_h = read_file
("src/headers/tomcrypt_custom.h");
92 my $cryp_c = read_file
("src/misc/crypt/crypt.c");
93 $cust_h =~ s
|/\*.*?\*/||sg
; # remove comments
94 $cryp_c =~ s
|/\*.*?\*/||sg
; # remove comments
95 my %def = map { $_ => 1 } map { my $x = $_; $x =~ s/^\s*#define\s+(LTC_\S+).*$/$1/; $x } grep { /^\s*#define\s+LTC_\S+/ } split /\n/, $cust_h;
96 for my $d (sort keys %def) {
97 next if $d =~ /^LTC_(DH\d+|ECC\d+|ECC_\S+|MPI|MUTEX_\S+\(x\)|NO_\S+)$/;
98 warn "$d missing in src/misc/crypt/crypt.c\n" and $fails++ if $cryp_c !~ /\Q
$d\E
/;
100 warn( $fails > 0 ?
"check-defines: FAIL $fails\n" : "check-defines: PASS\n" );
104 sub check_descriptor
{
109 find
({ wanted
=> sub { push @src, $_ if $_ =~ /\.c$/ }, no_chdir
=>1 }, "./src/${which}/");
111 my @n = map { my $x = $_; $x =~ s/^.*?ltc_${what}_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /ltc_${what}_descriptor/ } split /\n/, read_file
($f);
112 push @descriptors, @n if @n;
115 for my $d (@descriptors) {
116 for my $f ("./src/misc/crypt/crypt_register_all_${which}.c") {
117 my $txt = read_file
($f);
118 warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
121 for my $d (@descriptors) {
122 for my $f ("./tests/test.c") {
123 my $txt = read_file
($f);
124 warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
127 my $name = sprintf("%-17s", "check-${which}:");
128 warn( $fails > 0 ?
"${name}FAIL $fails\n" : "${name}PASS\n" );
132 sub check_descriptors
{
134 $fails = $fails + check_descriptor
("ciphers", "cipher");
135 $fails = $fails + check_descriptor
("hashes", "hash");
136 $fails = $fails + check_descriptor
("prngs", "prng");
142 my $first_comment = <<'MARKER';
143 /* LibTomCrypt, modular cryptographic library -- Tom St Denis */
144 /* SPDX-License-Identifier: Unlicense */
147 find
({ wanted
=> sub { push @all_files, $_ if $_ =~ /\.(c|h)$/ }, no_chdir
=>1 }, 'demos', 'src', 'tests');
148 for my $f (@all_files) {
149 my $txt = read_file
($f);
150 if ($txt !~ /^\Q$first_comment\E/s) {
151 warn "[first_comment] $f\n";
155 warn( $fails > 0 ?
"check-comments: FAIL $fails\n" : "check-comments: PASS\n" );
159 sub prepare_variable
{
160 my ($varname, @list) = @_;
161 my $output = "$varname=";
162 my $len = length($output);
163 foreach my $obj (sort @list) {
164 $len = $len + length $obj;
170 $output .= $obj . ' ';
176 sub prepare_msvc_files_xml
{
177 my ($all, $exclude_re, $targets) = @_;
181 # sort files in the same order as visual studio (ugly, I know)
183 for my $orig (@
$all) {
186 $p =~ s
|/~([^/]+)$|/$1|g
;
187 # now we have: 'src/pk/rsa/rsa_verify_hash.c' > 'src/~pk/~rsa/rsa_verify_hash.c'
188 my @l = map { sprintf "% -99s", $_ } split /\//, $p;
189 push @parts, [ $orig, join(':', @l) ];
191 my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } @parts;
193 my $files = "<Files>\r\n";
194 for my $full (@sorted) {
195 my @items = split /\//, $full; # split by '/'
196 $full =~ s|/|\\|g; # replace '/' bt '\'
197 shift @items; # drop first one (src)
198 pop @items; # drop last one (filename.ext)
199 my $current = \
@items;
200 if (join(':', @
$current) ne join(':', @
$last)) {
202 $common++ while ($last->[$common] && $current->[$common] && $last->[$common] eq $current->[$common]);
203 my $back = @
$last - $common;
205 $files .= ("\t" x
--$depth) . "</Filter>\r\n" for (1..$back);
207 my $fwd = [ @
$current ]; splice(@
$fwd, 0, $common);
208 for my $i (0..scalar(@
$fwd) - 1) {
209 $files .= ("\t" x
$depth) . "<Filter\r\n";
210 $files .= ("\t" x
$depth) . "\tName=\"$fwd->[$i]\"\r\n";
211 $files .= ("\t" x
$depth) . "\t>\r\n";
216 $files .= ("\t" x
$depth) . "<File\r\n";
217 $files .= ("\t" x
$depth) . "\tRelativePath=\"$full\"\r\n";
218 $files .= ("\t" x
$depth) . "\t>\r\n";
219 if ($full =~ $exclude_re) {
221 $files .= ("\t" x
$depth) . "\t<FileConfiguration\r\n";
222 $files .= ("\t" x
$depth) . "\t\tName=\"$_\"\r\n";
223 $files .= ("\t" x
$depth) . "\t\tExcludedFromBuild=\"true\"\r\n";
224 $files .= ("\t" x
$depth) . "\t\t>\r\n";
225 $files .= ("\t" x
$depth) . "\t\t<Tool\r\n";
226 $files .= ("\t" x
$depth) . "\t\t\tName=\"VCCLCompilerTool\"\r\n";
227 $files .= ("\t" x
$depth) . "\t\t\tAdditionalIncludeDirectories=\"\"\r\n";
228 $files .= ("\t" x
$depth) . "\t\t\tPreprocessorDefinitions=\"\"\r\n";
229 $files .= ("\t" x
$depth) . "\t\t/>\r\n";
230 $files .= ("\t" x
$depth) . "\t</FileConfiguration>\r\n";
233 ########### aes_enc "hack" disabled - discussion: https://github.com/libtom/libtomcrypt/pull/158
234 # if ($full eq 'src\ciphers\aes\aes.c') { #hack
236 # 'Debug|Win32' => [ 'Debug/aes.obj;Debug/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I "src\headers" /I "..\libtommath" /D "_DEBUG" /D "LTM_DESC" /D "WIN32" /D "_MBCS" /D "_LIB" /D "LTC_SOURCE" /D "USE_LTM" /Fp"Debug/libtomcrypt.pch" /YX /Fo"Debug/" /Fd"Debug/" /FD /GZ /c $(InputPath)
cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I "src\headers" /I "..\libtommath" /D "_DEBUG" /D "LTM_DESC" /D "WIN32" /D "_MBCS" /D "_LIB" /D "LTC_SOURCE" /D "USE_LTM" /Fp"Debug/libtomcrypt.pch" /YX /Fo"Debug/aes_enc.obj" /Fd"Debug/" /FD /GZ /c $(InputPath)
' ],
237 # 'Release|Win32' => [ 'Release/aes.obj;Release/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I "src\headers" /I "..\libtommath" /D "_DEBUG" /D "LTM_DESC" /D "WIN32" /D "_MBCS" /D "_LIB" /D "LTC_SOURCE" /D "USE_LTM" /Fp"Release/libtomcrypt.pch" /YX /Fo"Release/" /Fd"Release/" /FD /GZ /c $(InputPath)
cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I "src\headers" /I "..\libtommath" /D "_DEBUG" /D "LTM_DESC" /D "WIN32" /D "_MBCS" /D "_LIB" /D "LTC_SOURCE" /D "USE_LTM" /Fp"Release/libtomcrypt.pch" /YX /Fo"Release/aes_enc.obj" /Fd"Release/" /FD /GZ /c $(InputPath)
' ],
240 # next unless $cmd{$_};
241 # $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
242 # $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
243 # $files .= ("\t" x $depth) . "\t\t>\r\n";
244 # $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
245 # $files .= ("\t" x $depth) . "\t\t\tName=\"VCCustomBuildTool\"\r\n";
246 # $files .= ("\t" x $depth) . "\t\t\tCommandLine=\"$cmd{$_}[1]\"\r\n";
247 # $files .= ("\t" x $depth) . "\t\t\tOutputs=\"$cmd{$_}[0]\"\r\n";
248 # $files .= ("\t" x $depth) . "\t\t/>\r\n";
249 # $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
252 $files .= ("\t" x
$depth) . "</File>\r\n";
254 $files .= ("\t" x
--$depth) . "</Filter>\r\n" for (@
$last);
255 $files .= "\t</Files>";
260 my ($content, @variables) = @_;
261 for my $v (@variables) {
262 if ($v =~ /^([A-Z0-9_]+)\s*=.*$/si) {
264 $content =~ s/\n\Q$name\E\b.*?[^\\]\n/\n$v\n/s;
267 die "patch_file failed: " . substr($v, 0, 30) . "..";
273 sub version_from_tomcrypt_h
{
274 my $h = read_file
(shift);
275 if ($h =~ /\n#define\s*SCRYPT\s*"([0-9]+)\.([0-9]+)\.([0-9]+)(\S*)"/s) {
276 return "VERSION_PC=$1.$2.$3", "VERSION_LT=1:1", "VERSION=$1.$2.$3$4", "PROJECT_NUMBER=$1.$2.$3$4";
279 die "#define SCRYPT not found in tomcrypt.h";
283 sub make_sources_cmake
{
284 my ($list, $pub_headers) = @_;
285 my $output = "set(SOURCES\n";
287 foreach my $obj (sort @
$list) {
288 $output .= $obj . "\n";
292 if ($pub_headers eq "") {
296 $output .= "set(PUBLIC_HEADERS\n";
298 foreach my $obj (sort @
$pub_headers) {
299 $output .= $obj . "\n";
302 $output .= ")\n\nset(PRIVATE_HEADERS src/headers/tomcrypt_private.h)\n";
303 $output .= "set_property(GLOBAL PROPERTY PUBLIC_HEADERS \$\{PUBLIC_HEADERS\}\)\n\n";
308 sub process_makefiles
{
310 my $changed_count = 0;
312 find
({ no_chdir
=> 1, wanted
=> sub { push @c, $_ if -f
$_ && $_ =~ /\.c$/ && $_ !~ /tab.c$/ } }, 'src');
314 find
({ no_chdir
=> 1, wanted
=> sub { push @h, $_ if -f
$_ && $_ =~ /\.h$/ && $_ !~ /dh_static.h$/ && $_ !~ /tomcrypt_private.h$/ } }, 'src');
316 find
({ no_chdir
=> 1, wanted
=> sub { push @all, $_ if -f
$_ && $_ =~ /\.(c|h)$/ } }, 'src');
318 find
({ no_chdir
=> 1, wanted
=> sub { push @t, $_ if $_ =~ /(common|no_prng|_tests?|test).c$/ } }, 'tests');
320 my @o = sort ('src/ciphers/aes/aes_enc.o', 'src/ciphers/aes/aes_enc_desc.o', map { my $x = $_; $x =~ s
/\
.c
$/.o/; $x } @c);
321 my $var_o = prepare_variable
("OBJECTS", @o);
322 my $var_h = prepare_variable
("HEADERS_PUB", (sort @h));
323 (my $var_obj = $var_o) =~ s/\.o\b/.obj/sg;
325 my @t_srcs = sort (map { my $x = $_; $x =~ s/^tests\///; $x } @t);
326 my $var_to = prepare_variable
("TOBJECTS", sort map { my $x = $_; $x =~ s/\.c$/.o/; $x } @t);
327 (my $var_tobj = $var_to) =~ s/\.o\b/.obj/sg;
329 my @ver_version = version_from_tomcrypt_h
("src/headers/tomcrypt.h");
331 # update MSVC project files
332 my $msvc_files = prepare_msvc_files_xml
(\
@all, qr/tab\.c$/, ['Debug|Win32', 'Release|Win32', 'Debug|x64', 'Release|x64']);
333 for my $m (qw
/libtomcrypt_VS2008.vcproj/) {
334 my $old = read_file
($m);
336 $new =~ s
|<Files
>.*</Files
>|$msvc_files|s
;
338 write_file
($m, $new) if $write;
339 warn "changed: $m\n";
344 # update OBJECTS + HEADERS in makefile*
345 for my $m (qw
/ makefile makefile.shared makefile.unix makefile.mingw makefile.msvc makefile_include.mk doc\/Doxyfile sources
.cmake tests\
/sources.cmake /) {
346 my $old = read_file
($m);
347 my $new = $m eq 'makefile.msvc' ? patch_file
($old, $var_obj, $var_h, $var_tobj, @ver_version)
348 : $m eq 'sources.cmake' ? make_sources_cmake
(\
@all, \
@h)
349 : $m eq 'tests/sources.cmake' ? make_sources_cmake
(\
@t_srcs, "")
350 : patch_file
($old, $var_o, $var_h, $var_to, @ver_version);
353 write_file
($m, $new) if $write;
354 warn "changed: $m\n";
360 return 0; # no failures
363 warn( $changed_count > 0 ?
"check-makefiles: FAIL $changed_count\n" : "check-makefiles: PASS\n" );
364 return $changed_count;
370 usage: $0 -s OR $0 --check-source
371 $0 -c OR $0 --check-descriptors
372 $0 -d OR $0 --check-defines
373 $0 -o OR $0 --check-comments
374 $0 -m OR $0 --check-makefiles
375 $0 -a OR $0 --check-all
376 $0 -u OR $0 --update-makefiles
377 $0 --fixupind crypt.ind
381 GetOptions
( "s|check-source" => \
my $check_source,
382 "c|check-descriptors" => \
my $check_descriptors,
383 "d|check-defines" => \
my $check_defines,
384 "o|check-comments" => \
my $check_comments,
385 "m|check-makefiles" => \
my $check_makefiles,
386 "a|check-all" => \
my $check_all,
387 "u|update-makefiles" => \
my $update_makefiles,
388 "f|fixupind=s" => \
my $fixupind,
389 "h|help" => \
my $help
393 my $txt = read_file
($fixupind);
394 $txt =~ s/^([^\n]*\n)/$1\n\\addcontentsline{toc}{chapter}{Index}\n/s;
395 write_file
($fixupind, $txt);
400 $failure ||= check_source
() if $check_all || $check_source;
401 $failure ||= check_defines
() if $check_all || $check_defines;
402 $failure ||= check_descriptors
() if $check_all || $check_descriptors;
403 $failure ||= check_comments
() if $check_all || $check_comments;
404 $failure ||= process_makefiles
(0) if $check_all || $check_makefiles;
405 $failure ||= process_makefiles
(1) if $update_makefiles;
407 die_usage
unless defined $failure;
408 exit $failure ?
1 : 0;