Cdata.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # Copyright © 2007 Raphaël Hertzog <hertzog@debian.org>
  2. # This program is free software; you can redistribute it and/or modify
  3. # it under the terms of the GNU General Public License as published by
  4. # the Free Software Foundation; either version 2 of the License, or
  5. # (at your option) any later version.
  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. # You should have received a copy of the GNU General Public License along
  11. # with this program; if not, write to the Free Software Foundation, Inc.,
  12. # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  13. package Dpkg::Cdata;
  14. use strict;
  15. use warnings;
  16. use Dpkg::Gettext;
  17. use Dpkg::ErrorHandling;
  18. use Dpkg::Fields qw(capit);;
  19. use Exporter;
  20. our @ISA = qw(Exporter);
  21. our @EXPORT = qw(parsecdata);
  22. =head1 NAME
  23. Dpkg::Cdata - parse and manipulate a block of RFC822-like fields
  24. =head1 DESCRIPTION
  25. The Dpkg::Cdata module exports one function 'parsecdata' that reads a
  26. block of data (usually a block following the debian/control format)
  27. =head1 FUNCTIONS
  28. =over 4
  29. =item $obj = Dpkg::Cdata::parsecdata($input, $file, %options)
  30. $input is a filehandle, $file is the name of the file corresponding to
  31. $input. %options can contain two parameters: allow_pgp=>1 allows the parser
  32. to extrac the block of a data in a PGP-signed message (defaults to 0),
  33. and allow_duplicate=>1 ask the parser to not fail when it detects
  34. duplicate fields.
  35. The return value is a reference to a tied hash (Dpkg::Fields::Object) that
  36. can be used to access the various fields.
  37. =cut
  38. sub parsecdata {
  39. my ($input, $file, %options) = @_;
  40. $options{allow_pgp} = 0 unless exists $options{allow_pgp};
  41. $options{allow_duplicate} = 0 unless exists $options{allow_duplicate};
  42. my $paraborder = 1;
  43. my $fields = undef;
  44. my $cf = ''; # Current field
  45. my $expect_pgp_sig = 0;
  46. while (<$input>) {
  47. s/\s*\n$//;
  48. next if (m/^$/ and $paraborder);
  49. next if (m/^#/);
  50. $paraborder = 0;
  51. if (m/^(\S+?)\s*:\s*(.*)$/) {
  52. unless (defined $fields) {
  53. my %f;
  54. tie %f, "Dpkg::Fields::Object";
  55. $fields = \%f;
  56. }
  57. if (exists $fields->{$1}) {
  58. unless ($options{allow_duplicate}) {
  59. syntaxerr($file, sprintf(_g("duplicate field %s found"), capit($1)));
  60. }
  61. }
  62. $fields->{$1} = $2;
  63. $cf = $1;
  64. } elsif (m/^\s+\S/) {
  65. length($cf) || syntaxerr($file, _g("continued value line not in field"));
  66. $fields->{$cf} .= "\n$_";
  67. } elsif (m/^-----BEGIN PGP SIGNED MESSAGE/) {
  68. $expect_pgp_sig = 1;
  69. if ($options{allow_pgp}) {
  70. # Skip PGP headers
  71. while (<$input>) {
  72. last if m/^$/;
  73. }
  74. } else {
  75. syntaxerr($file, _g("PGP signature not allowed here"));
  76. }
  77. } elsif (m/^$/) {
  78. if ($expect_pgp_sig) {
  79. # Skip empty lines
  80. $_ = <$input> while defined($_) && $_ =~ /^\s*$/;
  81. length($_) ||
  82. syntaxerr($file, _g("expected PGP signature, found EOF after blank line"));
  83. s/\n$//;
  84. m/^-----BEGIN PGP SIGNATURE/ ||
  85. syntaxerr($file,
  86. sprintf(_g("expected PGP signature, found something else \`%s'"), $_));
  87. # Skip PGP signature
  88. while (<$input>) {
  89. last if m/^-----END PGP SIGNATURE/;
  90. }
  91. length($_) ||
  92. syntaxerr($file, _g("unfinished PGP signature"));
  93. }
  94. last; # Finished parsing one block
  95. } else {
  96. syntaxerr($file, _g("line with unknown format (not field-colon-value)"));
  97. }
  98. }
  99. return $fields;
  100. }
  101. =back
  102. =cut
  103. 1;