Checksums.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. our $VERSION = "0.01";
  17. use Dpkg;
  18. use Dpkg::Gettext;
  19. use Dpkg::ErrorHandling;
  20. use base qw(Exporter);
  21. our @EXPORT = qw(@check_supported %check_supported %check_prog %check_regex
  22. readchecksums readallchecksums getchecksums);
  23. our @check_supported = qw(md5 sha1 sha256);
  24. our %check_supported = map { $_ => 1 } @check_supported;
  25. our %check_prog = ( md5 => 'md5sum', sha1 => 'sha1sum',
  26. sha256 => 'sha256sum' );
  27. our %check_regex = ( md5 => qr/[0-9a-f]{32}/,
  28. sha1 => qr/[0-9a-f]{40}/,
  29. sha256 => qr/[0-9a-f]{64}/ );
  30. sub extractchecksum {
  31. my ($alg, $checksum) = @_;
  32. ($checksum =~ /^($check_regex{$alg})(\s|$)/m)
  33. || error(_g("checksum program gave bogus output `%s'"), $checksum);
  34. return $1;
  35. }
  36. sub readchecksums {
  37. my ($alg, $fieldtext, $checksums, $sizes) = @_;
  38. my %checksums;
  39. $alg = lc($alg);
  40. unless ($check_supported{$alg}) {
  41. warning(_g("Unknown checksum algorithm \`%s', ignoring"), $alg);
  42. return;
  43. }
  44. my $rx_fname = qr/[0-9a-zA-Z][-+:.,=0-9a-zA-Z_~]+/;
  45. for my $checksum (split /\n */, $fieldtext) {
  46. next if $checksum eq '';
  47. $checksum =~ m/^($check_regex{$alg})\s+(\d+)\s+($rx_fname)$/
  48. || do {
  49. warning(_g("Checksums-%s field contains bad line \`%s'"),
  50. ucfirst($alg), $checksum);
  51. next;
  52. };
  53. my ($sum, $size, $file) = ($1, $2, $3);
  54. if (exists($checksums->{$file}{$alg})
  55. and $checksums->{$file}{$alg} ne $sum) {
  56. error(_g("Conflicting checksums \`%s\' and \`%s' for file \`%s'"),
  57. $checksums->{$file}{$alg}, $sum, $file);
  58. }
  59. if (exists($sizes->{$file})
  60. and $sizes->{$file} != $size) {
  61. error(_g("Conflicting file sizes \`%u\' and \`%u' for file \`%s'"),
  62. $sizes->{$file}, $size, $file);
  63. }
  64. $checksums->{$file}{$alg} = $sum;
  65. $sizes->{$file} = $size;
  66. }
  67. return 1;
  68. }
  69. sub readallchecksums {
  70. my ($fields, $checksums, $sizes) = @_;
  71. foreach my $field (keys %$fields) {
  72. if ($field =~ /^Checksums-(\w+)$/
  73. && defined($fields->{$field})) {
  74. readchecksums($1, $fields->{$field}, $checksums, $sizes);
  75. }
  76. }
  77. }
  78. sub getchecksums {
  79. my ($file, $checksums, $size) = @_;
  80. (my @s = stat($file)) || syserr(_g("cannot fstat file %s"), $file);
  81. my $newsize = $s[7];
  82. if (defined($$size)
  83. and $newsize != $$size) {
  84. error(_g("File %s has size %u instead of expected %u"),
  85. $file, $newsize, $$size);
  86. }
  87. $$size = $newsize;
  88. foreach my $alg (@check_supported) {
  89. my $prog = $check_prog{$alg};
  90. my $newsum = `$prog $file`;
  91. $? && subprocerr("%s %s", $prog, $file);
  92. $newsum = extractchecksum($alg, $newsum);
  93. if (defined($checksums->{$alg})
  94. and $newsum ne $checksums->{$alg}) {
  95. error(_g("File %s has checksum %s instead of expected %s (algorithm %s)"),
  96. $file, $newsum, $checksums->{$alg}, $alg);
  97. }
  98. $checksums->{$alg} = $newsum;
  99. }
  100. }
  101. 1;