I2C toy code
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

384 rindas
16 KiB

  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. use Getopt::Long;
  5. use File::Find 'find';
  6. use File::Basename 'basename';
  7. use File::Glob 'bsd_glob';
  8. sub read_file {
  9. my $f = shift;
  10. open my $fh, "<", $f or die "FATAL: read_rawfile() cannot open file '$f': $!";
  11. binmode $fh;
  12. return do { local $/; <$fh> };
  13. }
  14. sub write_file {
  15. my ($f, $data) = @_;
  16. die "FATAL: write_file() no data" unless defined $data;
  17. open my $fh, ">", $f or die "FATAL: write_file() cannot open file '$f': $!";
  18. binmode $fh;
  19. print $fh $data or die "FATAL: write_file() cannot write to '$f': $!";
  20. close $fh or die "FATAL: write_file() cannot close '$f': $!";
  21. return;
  22. }
  23. sub check_source {
  24. my @all_files = (bsd_glob("makefile*"), bsd_glob("*.sh"), bsd_glob("*.pl"));
  25. find({ wanted=>sub { push @all_files, $_ if -f $_ }, no_chdir=>1 }, qw/src tests demos/);
  26. my $fails = 0;
  27. for my $file (sort @all_files) {
  28. next unless $file =~ /\.(c|h|pl|py|sh)$/ || basename($file) =~ /^makefile/i;
  29. my $troubles = {};
  30. my $lineno = 1;
  31. my $content = read_file($file);
  32. push @{$troubles->{crlf_line_end}}, '?' if $content =~ /\r/;
  33. for my $l (split /\n/, $content) {
  34. push @{$troubles->{merge_conflict}}, $lineno if $l =~ /^(<<<<<<<|=======|>>>>>>>)([^<=>]|$)/;
  35. push @{$troubles->{trailing_space}}, $lineno if $l =~ / $/;
  36. push @{$troubles->{tab}}, $lineno if $l =~ /\t/ && basename($file) !~ /^makefile/i;
  37. push @{$troubles->{non_ascii_char}}, $lineno if $l =~ /[^[:ascii:]]/;
  38. push @{$troubles->{cpp_comment}}, $lineno if $file =~ /\.(c|h)$/ && ($l =~ /\s\/\// || $l =~ /\/\/\s/);
  39. # in ./src we prefer using XMEMCPY, XMALLOC, XFREE ...
  40. push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
  41. push @{$troubles->{unwanted_malloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmalloc\s*\(/;
  42. push @{$troubles->{unwanted_realloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\brealloc\s*\(/;
  43. push @{$troubles->{unwanted_calloc}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bcalloc\s*\(/;
  44. push @{$troubles->{unwanted_free}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bfree\s*\(/;
  45. push @{$troubles->{unwanted_memset}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemset\s*\(/;
  46. push @{$troubles->{unwanted_memcpy}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcpy\s*\(/;
  47. push @{$troubles->{unwanted_memmove}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemmove\s*\(/;
  48. push @{$troubles->{unwanted_memcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bmemcmp\s*\(/;
  49. push @{$troubles->{unwanted_strcmp}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bstrcmp\s*\(/;
  50. push @{$troubles->{unwanted_clock}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bclock\s*\(/;
  51. push @{$troubles->{unwanted_qsort}}, $lineno if $file =~ /^src\/.*\.c$/ && $l =~ /\bqsort\s*\(/;
  52. if ($file =~ m|src/.*\.c$| &&
  53. $file !~ m|src/ciphers/.*\.c$| &&
  54. $file !~ m|src/hashes/.*\.c$| &&
  55. $file !~ m|src/math/.+_desc.c$| &&
  56. $file !~ m|src/stream/sober128/sober128.c$| &&
  57. $l =~ /^static\s+\S+\s+([^_][a-zA-Z0-9_]+)\s*\(/) {
  58. push @{$troubles->{staticfunc_name}}, "$lineno($1)";
  59. }
  60. $lineno++;
  61. }
  62. for my $k (sort keys %$troubles) {
  63. warn "[$k] $file line:" . join(",", @{$troubles->{$k}}) . "\n";
  64. $fails++;
  65. }
  66. }
  67. warn( $fails > 0 ? "check-source: FAIL $fails\n" : "check-source: PASS\n" );
  68. return $fails;
  69. }
  70. sub check_defines {
  71. my $fails = 0;
  72. my $cust_h = read_file("src/headers/tomcrypt_custom.h");
  73. my $cryp_c = read_file("src/misc/crypt/crypt.c");
  74. $cust_h =~ s|/\*.*?\*/||sg; # remove comments
  75. $cryp_c =~ s|/\*.*?\*/||sg; # remove comments
  76. 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;
  77. for my $d (sort keys %def) {
  78. next if $d =~ /^LTC_(DH\d+|ECC\d+|ECC_\S+|MPI|MUTEX_\S+\(x\)|NO_\S+)$/;
  79. warn "$d missing in src/misc/crypt/crypt.c\n" and $fails++ if $cryp_c !~ /\Q$d\E/;
  80. }
  81. warn( $fails > 0 ? "check-defines: FAIL $fails\n" : "check-defines: PASS\n" );
  82. return $fails;
  83. }
  84. sub check_descriptor {
  85. my $which = shift;
  86. my $what = shift;
  87. my @src;
  88. my @descriptors;
  89. find({ wanted => sub { push @src, $_ if $_ =~ /\.c$/ }, no_chdir=>1 }, "./src/${which}/");
  90. for my $f (@src) {
  91. my @n = map { my $x = $_; $x =~ s/^.*?ltc_${what}_descriptor\s+(\S+).*$/$1/; $x } grep { $_ =~ /ltc_${what}_descriptor/ } split /\n/, read_file($f);
  92. push @descriptors, @n if @n;
  93. }
  94. my $fails = 0;
  95. for my $d (@descriptors) {
  96. for my $f ("./src/misc/crypt/crypt_register_all_${which}.c") {
  97. my $txt = read_file($f);
  98. warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
  99. }
  100. }
  101. for my $d (@descriptors) {
  102. for my $f ("./tests/test.c") {
  103. my $txt = read_file($f);
  104. warn "$d missing in $f\n" and $fails++ if $txt !~ /\Q$d\E/;
  105. }
  106. }
  107. my $name = sprintf("%-17s", "check-${which}:");
  108. warn( $fails > 0 ? "${name}FAIL $fails\n" : "${name}PASS\n" );
  109. return $fails;
  110. }
  111. sub check_descriptors {
  112. my $fails = 0;
  113. $fails = $fails + check_descriptor("ciphers", "cipher");
  114. $fails = $fails + check_descriptor("hashes", "hash");
  115. $fails = $fails + check_descriptor("prngs", "prng");
  116. return $fails;
  117. }
  118. sub check_comments {
  119. my $fails = 0;
  120. my $first_comment = <<'MARKER';
  121. /* LibTomCrypt, modular cryptographic library -- Tom St Denis
  122. *
  123. * LibTomCrypt is a library that provides various cryptographic
  124. * algorithms in a highly modular and flexible manner.
  125. *
  126. * The library is free for all purposes without any express
  127. * guarantee it works.
  128. */
  129. MARKER
  130. my $last_comment = <<'MARKER';
  131. /* ref: $Format:%D$ */
  132. /* git commit: $Format:%H$ */
  133. /* commit time: $Format:%ai$ */
  134. MARKER
  135. my @all_files;
  136. find({ wanted=> sub { push @all_files, $_ if $_ =~ /\.(c|h)$/ }, no_chdir=>1 }, 'demos', 'src', 'tests');
  137. for my $f (@all_files) {
  138. my $txt = read_file($f);
  139. if ($txt !~ /^\Q$first_comment\E/s) {
  140. warn "[first_comment] $f\n";
  141. $fails++;
  142. }
  143. if ($txt !~ /\Q$last_comment\E\s*$/s) {
  144. warn "[last_comment] $f\n";
  145. $fails++;
  146. }
  147. }
  148. warn( $fails > 0 ? "check-comments: FAIL $fails\n" : "check-comments: PASS\n" );
  149. return $fails;
  150. }
  151. sub prepare_variable {
  152. my ($varname, @list) = @_;
  153. my $output = "$varname=";
  154. my $len = length($output);
  155. foreach my $obj (sort @list) {
  156. $len = $len + length $obj;
  157. $obj =~ s/\*/\$/;
  158. if ($len > 100) {
  159. $output .= "\\\n";
  160. $len = length $obj;
  161. }
  162. $output .= $obj . ' ';
  163. }
  164. $output =~ s/ $//;
  165. return $output;
  166. }
  167. sub prepare_msvc_files_xml {
  168. my ($all, $exclude_re, $targets) = @_;
  169. my $last = [];
  170. my $depth = 2;
  171. # sort files in the same order as visual studio (ugly, I know)
  172. my @parts = ();
  173. for my $orig (@$all) {
  174. my $p = $orig;
  175. $p =~ s|/|/~|g;
  176. $p =~ s|/~([^/]+)$|/$1|g;
  177. # now we have: 'src/pk/rsa/rsa_verify_hash.c' > 'src/~pk/~rsa/rsa_verify_hash.c'
  178. my @l = map { sprintf "% -99s", $_ } split /\//, $p;
  179. push @parts, [ $orig, join(':', @l) ];
  180. }
  181. my @sorted = map { $_->[0] } sort { $a->[1] cmp $b->[1] } @parts;
  182. my $files = "<Files>\r\n";
  183. for my $full (@sorted) {
  184. my @items = split /\//, $full; # split by '/'
  185. $full =~ s|/|\\|g; # replace '/' bt '\'
  186. shift @items; # drop first one (src)
  187. pop @items; # drop last one (filename.ext)
  188. my $current = \@items;
  189. if (join(':', @$current) ne join(':', @$last)) {
  190. my $common = 0;
  191. $common++ while ($last->[$common] && $current->[$common] && $last->[$common] eq $current->[$common]);
  192. my $back = @$last - $common;
  193. if ($back > 0) {
  194. $files .= ("\t" x --$depth) . "</Filter>\r\n" for (1..$back);
  195. }
  196. my $fwd = [ @$current ]; splice(@$fwd, 0, $common);
  197. for my $i (0..scalar(@$fwd) - 1) {
  198. $files .= ("\t" x $depth) . "<Filter\r\n";
  199. $files .= ("\t" x $depth) . "\tName=\"$fwd->[$i]\"\r\n";
  200. $files .= ("\t" x $depth) . "\t>\r\n";
  201. $depth++;
  202. }
  203. $last = $current;
  204. }
  205. $files .= ("\t" x $depth) . "<File\r\n";
  206. $files .= ("\t" x $depth) . "\tRelativePath=\"$full\"\r\n";
  207. $files .= ("\t" x $depth) . "\t>\r\n";
  208. if ($full =~ $exclude_re) {
  209. for (@$targets) {
  210. $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
  211. $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
  212. $files .= ("\t" x $depth) . "\t\tExcludedFromBuild=\"true\"\r\n";
  213. $files .= ("\t" x $depth) . "\t\t>\r\n";
  214. $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
  215. $files .= ("\t" x $depth) . "\t\t\tName=\"VCCLCompilerTool\"\r\n";
  216. $files .= ("\t" x $depth) . "\t\t\tAdditionalIncludeDirectories=\"\"\r\n";
  217. $files .= ("\t" x $depth) . "\t\t\tPreprocessorDefinitions=\"\"\r\n";
  218. $files .= ("\t" x $depth) . "\t\t/>\r\n";
  219. $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
  220. }
  221. }
  222. ########### aes_enc "hack" disabled - discussion: https://github.com/libtom/libtomcrypt/pull/158
  223. # if ($full eq 'src\ciphers\aes\aes.c') { #hack
  224. # my %cmd = (
  225. # 'Debug|Win32' => [ 'Debug/aes.obj;Debug/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Debug/libtomcrypt.pch&quot; /YX /Fo&quot;Debug/&quot; /Fd&quot;Debug/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Debug/libtomcrypt.pch&quot; /YX /Fo&quot;Debug/aes_enc.obj&quot; /Fd&quot;Debug/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;' ],
  226. # 'Release|Win32' => [ 'Release/aes.obj;Release/aes_enc.obj', 'cl /nologo /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Release/libtomcrypt.pch&quot; /YX /Fo&quot;Release/&quot; /Fd&quot;Release/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;cl /nologo /DENCRYPT_ONLY /MLd /W3 /Gm /GX /ZI /Od /I &quot;src\headers&quot; /I &quot;..\libtommath&quot; /D &quot;_DEBUG&quot; /D &quot;LTM_DESC&quot; /D &quot;WIN32&quot; /D &quot;_MBCS&quot; /D &quot;_LIB&quot; /D &quot;LTC_SOURCE&quot; /D &quot;USE_LTM&quot; /Fp&quot;Release/libtomcrypt.pch&quot; /YX /Fo&quot;Release/aes_enc.obj&quot; /Fd&quot;Release/&quot; /FD /GZ /c $(InputPath)&#x0D;&#x0A;' ],
  227. # );
  228. # for (@$targets) {
  229. # next unless $cmd{$_};
  230. # $files .= ("\t" x $depth) . "\t<FileConfiguration\r\n";
  231. # $files .= ("\t" x $depth) . "\t\tName=\"$_\"\r\n";
  232. # $files .= ("\t" x $depth) . "\t\t>\r\n";
  233. # $files .= ("\t" x $depth) . "\t\t<Tool\r\n";
  234. # $files .= ("\t" x $depth) . "\t\t\tName=\"VCCustomBuildTool\"\r\n";
  235. # $files .= ("\t" x $depth) . "\t\t\tCommandLine=\"$cmd{$_}[1]\"\r\n";
  236. # $files .= ("\t" x $depth) . "\t\t\tOutputs=\"$cmd{$_}[0]\"\r\n";
  237. # $files .= ("\t" x $depth) . "\t\t/>\r\n";
  238. # $files .= ("\t" x $depth) . "\t</FileConfiguration>\r\n";
  239. # }
  240. # }
  241. $files .= ("\t" x $depth) . "</File>\r\n";
  242. }
  243. $files .= ("\t" x --$depth) . "</Filter>\r\n" for (@$last);
  244. $files .= "\t</Files>";
  245. return $files;
  246. }
  247. sub patch_makefile {
  248. my ($content, @variables) = @_;
  249. for my $v (@variables) {
  250. if ($v =~ /^([A-Z0-9_]+)\s*=.*$/si) {
  251. my $name = $1;
  252. $content =~ s/\n\Q$name\E\b.*?[^\\]\n/\n$v\n/s;
  253. }
  254. else {
  255. die "patch_makefile failed: " . substr($v, 0, 30) . "..";
  256. }
  257. }
  258. return $content;
  259. }
  260. sub version_from_tomcrypt_h {
  261. my $h = read_file(shift);
  262. if ($h =~ /\n#define\s*SCRYPT\s*"([0-9]+)\.([0-9]+)\.([0-9]+)(.*)"/s) {
  263. return "VERSION_PC=$1.$2.$3", "VERSION_LT=0:$1$2", "VERSION=$1.$2.$3$4";
  264. }
  265. else {
  266. die "#define SCRYPT not found in tomcrypt.h";
  267. }
  268. }
  269. sub process_makefiles {
  270. my $write = shift;
  271. my $changed_count = 0;
  272. my @c = ();
  273. find({ no_chdir => 1, wanted => sub { push @c, $_ if -f $_ && $_ =~ /\.c$/ && $_ !~ /tab.c$/ } }, 'src');
  274. my @h = ();
  275. find({ no_chdir => 1, wanted => sub { push @h, $_ if -f $_ && $_ =~ /\.h$/ && $_ !~ /dh_static.h$/ } }, 'src');
  276. my @all = ();
  277. find({ no_chdir => 1, wanted => sub { push @all, $_ if -f $_ && $_ =~ /\.(c|h)$/ } }, 'src');
  278. my @t = qw();
  279. find({ no_chdir => 1, wanted => sub { push @t, $_ if $_ =~ /(common|no_prng|_tests?|test).c$/ } }, 'tests');
  280. my @o = sort ('src/ciphers/aes/aes_enc.o', map { my $x = $_; $x =~ s/\.c$/.o/; $x } @c);
  281. my $var_o = prepare_variable("OBJECTS", @o);
  282. my $var_h = prepare_variable("HEADERS", (sort @h));
  283. (my $var_obj = $var_o) =~ s/\.o\b/.obj/sg;
  284. my $var_to = prepare_variable("TOBJECTS", sort map { my $x = $_; $x =~ s/\.c$/.o/; $x } @t);
  285. (my $var_tobj = $var_to) =~ s/\.o\b/.obj/sg;
  286. my @ver_version = version_from_tomcrypt_h("src/headers/tomcrypt.h");
  287. # update MSVC project files
  288. my $msvc_files = prepare_msvc_files_xml(\@all, qr/tab\.c$/, ['Debug|Win32', 'Release|Win32', 'Debug|x64', 'Release|x64']);
  289. for my $m (qw/libtomcrypt_VS2008.vcproj/) {
  290. my $old = read_file($m);
  291. my $new = $old;
  292. $new =~ s|<Files>.*</Files>|$msvc_files|s;
  293. if ($old ne $new) {
  294. write_file($m, $new) if $write;
  295. warn "changed: $m\n";
  296. $changed_count++;
  297. }
  298. }
  299. # update OBJECTS + HEADERS in makefile*
  300. for my $m (qw/ makefile makefile.shared makefile.unix makefile.mingw makefile.msvc makefile_include.mk /) {
  301. my $old = read_file($m);
  302. my $new = $m eq 'makefile.msvc' ? patch_makefile($old, $var_obj, $var_h, $var_tobj, @ver_version)
  303. : patch_makefile($old, $var_o, $var_h, $var_to, @ver_version);
  304. if ($old ne $new) {
  305. write_file($m, $new) if $write;
  306. warn "changed: $m\n";
  307. $changed_count++;
  308. }
  309. }
  310. if ($write) {
  311. return 0; # no failures
  312. }
  313. else {
  314. warn( $changed_count > 0 ? "check-makefiles: FAIL $changed_count\n" : "check-makefiles: PASS\n" );
  315. return $changed_count;
  316. }
  317. }
  318. sub die_usage {
  319. die <<"MARKER";
  320. usage: $0 -s OR $0 --check-source
  321. $0 -c OR $0 --check-descriptors
  322. $0 -d OR $0 --check-defines
  323. $0 -o OR $0 --check-comments
  324. $0 -m OR $0 --check-makefiles
  325. $0 -a OR $0 --check-all
  326. $0 -u OR $0 --update-makefiles
  327. $0 --fixupind crypt.ind
  328. MARKER
  329. }
  330. GetOptions( "s|check-source" => \my $check_source,
  331. "c|check-descriptors" => \my $check_descriptors,
  332. "d|check-defines" => \my $check_defines,
  333. "o|check-comments" => \my $check_comments,
  334. "m|check-makefiles" => \my $check_makefiles,
  335. "a|check-all" => \my $check_all,
  336. "u|update-makefiles" => \my $update_makefiles,
  337. "f|fixupind=s" => \my $fixupind,
  338. "h|help" => \my $help
  339. ) or die_usage;
  340. if ($fixupind) {
  341. my $txt = read_file($fixupind);
  342. $txt =~ s/^([^\n]*\n)/$1\n\\addcontentsline{toc}{chapter}{Index}\n/s;
  343. write_file($fixupind, $txt);
  344. exit 0;
  345. }
  346. my $failure;
  347. $failure ||= check_source() if $check_all || $check_source;
  348. $failure ||= check_defines() if $check_all || $check_defines;
  349. $failure ||= check_descriptors() if $check_all || $check_descriptors;
  350. $failure ||= check_comments() if $check_all || $check_comments;
  351. $failure ||= process_makefiles(0) if $check_all || $check_makefiles;
  352. $failure ||= process_makefiles(1) if $update_makefiles;
  353. die_usage unless defined $failure;
  354. exit $failure ? 1 : 0;