Checksums.pm 2.8 KB

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