Checksums.pm 2.8 KB

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