Checksums.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation; either version 2 of the License, or
  4. # (at your option) any later version.
  5. #
  6. # This program is distributed in the hope that it will be useful,
  7. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. # GNU General Public License for more details.
  10. #
  11. # You should have received a copy of the GNU General Public License
  12. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. package Dpkg::Checksums;
  14. use strict;
  15. use warnings;
  16. use Dpkg;
  17. use Dpkg::Gettext;
  18. use Dpkg::ErrorHandling;
  19. use base qw(Exporter);
  20. our @EXPORT = qw(@check_supported %check_supported %check_prog %check_regex
  21. readchecksums readallchecksums getchecksums);
  22. our @check_supported = qw(md5 sha1 sha256);
  23. our %check_supported = map { $_ => 1 } @check_supported;
  24. our %check_prog = ( md5 => 'md5sum', sha1 => 'sha1sum',
  25. sha256 => 'sha256sum' );
  26. our %check_regex = ( md5 => qr/[0-9a-f]{32}/,
  27. sha1 => qr/[0-9a-f]{40}/,
  28. sha256 => qr/[0-9a-f]{64}/ );
  29. sub extractchecksum {
  30. my ($alg, $checksum) = @_;
  31. ($checksum =~ /^($check_regex{$alg})(\s|$)/m)
  32. || error(_g("checksum program gave bogus output `%s'"), $checksum);
  33. return $1;
  34. }
  35. sub readchecksums {
  36. my ($alg, $fieldtext, $checksums, $sizes) = @_;
  37. my %checksums;
  38. $alg = lc($alg);
  39. unless ($check_supported{$alg}) {
  40. warning(_g("Unknown checksum algorithm \`%s', ignoring"), $alg);
  41. return;
  42. }
  43. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  44. for my $checksum (split /\n */, $fieldtext) {
  45. next if $checksum eq '';
  46. $checksum =~ m/^($check_regex{$alg})\s+(\d+)\s+($rx_fname)$/
  47. || do {
  48. warning(_g("Checksums-%s field contains bad line \`%s'"),
  49. ucfirst($alg), $checksum);
  50. next;
  51. };
  52. my ($sum, $size, $file) = ($1, $2, $3);
  53. if (exists($checksums->{$file}{$alg})
  54. and $checksums->{$file}{$alg} ne $sum) {
  55. error(_g("Conflicting checksums \`%s\' and \`%s' for file \`%s'"),
  56. $checksums->{$file}{$alg}, $sum, $file);
  57. }
  58. if (exists($sizes->{$file})
  59. and $sizes->{$file} != $size) {
  60. error(_g("Conflicting file sizes \`%u\' and \`%u' for file \`%s'"),
  61. $sizes->{$file}, $size, $file);
  62. }
  63. $checksums->{$file}{$alg} = $sum;
  64. $sizes->{$file} = $size;
  65. }
  66. return 1;
  67. }
  68. sub readallchecksums {
  69. my ($fields, $checksums, $sizes) = @_;
  70. foreach my $field (keys %$fields) {
  71. if ($field =~ /^Checksums-(\w+)$/
  72. && defined($fields->{$field})) {
  73. readchecksums($1, $fields->{$field}, $checksums, $sizes);
  74. }
  75. }
  76. }
  77. sub getchecksums {
  78. my ($file, $checksums, $size) = @_;
  79. (my @s = stat($file)) || syserr(_g("cannot fstat file %s"), $file);
  80. my $newsize = $s[7];
  81. if (defined($$size)
  82. and $newsize != $$size) {
  83. error(_g("File %s has size %u instead of expected %u"),
  84. $file, $newsize, $$size);
  85. }
  86. $$size = $newsize;
  87. foreach my $alg (@check_supported) {
  88. my $prog = $check_prog{$alg};
  89. my $newsum = `$prog $file`;
  90. $? && subprocerr("%s %s", $prog, $file);
  91. $newsum = extractchecksum($alg, $newsum);
  92. if (defined($checksums->{$alg})
  93. and $newsum ne $checksums->{$alg}) {
  94. error(_g("File %s has checksum %s instead of expected %s (algorithm %s)"),
  95. $file, $newsum, $checksums->{$alg}, $alg);
  96. }
  97. $checksums->{$alg} = $newsum;
  98. }
  99. }
  100. 1;