Cdata.pm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 base qw(Exporter);
  20. our @EXPORT = qw(parsecdata);
  21. =head1 NAME
  22. Dpkg::Cdata - parse and manipulate a block of RFC822-like fields
  23. =head1 DESCRIPTION
  24. The Dpkg::Cdata module exports one function 'parsecdata' that reads a
  25. block of data (usually a block following the debian/control format)
  26. =head1 FUNCTIONS
  27. =over 4
  28. =item $obj = Dpkg::Cdata::parsecdata($input, $file, %options)
  29. $input is a filehandle, $file is the name of the file corresponding to
  30. $input. %options can contain two parameters: allow_pgp=>1 allows the parser
  31. to extrac the block of a data in a PGP-signed message (defaults to 0),
  32. and allow_duplicate=>1 ask the parser to not fail when it detects
  33. duplicate fields.
  34. The return value is a reference to a tied hash (Dpkg::Fields::Object) that
  35. can be used to access the various fields.
  36. =cut
  37. sub parsecdata {
  38. my ($input, $file, %options) = @_;
  39. $options{allow_pgp} = 0 unless exists $options{allow_pgp};
  40. $options{allow_duplicate} = 0 unless exists $options{allow_duplicate};
  41. my $paraborder = 1;
  42. my $fields = undef;
  43. my $cf = ''; # Current field
  44. my $expect_pgp_sig = 0;
  45. while (<$input>) {
  46. s/\s*\n$//;
  47. next if (m/^$/ and $paraborder);
  48. next if (m/^#/);
  49. $paraborder = 0;
  50. if (m/^(\S+?)\s*:\s*(.*)$/) {
  51. unless (defined $fields) {
  52. my %f;
  53. tie %f, "Dpkg::Fields::Object";
  54. $fields = \%f;
  55. }
  56. if (exists $fields->{$1}) {
  57. unless ($options{allow_duplicate}) {
  58. syntaxerr($file, sprintf(_g("duplicate field %s found"), capit($1)));
  59. }
  60. }
  61. $fields->{$1} = $2;
  62. $cf = $1;
  63. } elsif (m/^\s+\S/) {
  64. length($cf) || syntaxerr($file, _g("continued value line not in field"));
  65. $fields->{$cf} .= "\n$_";
  66. } elsif (m/^-----BEGIN PGP SIGNED MESSAGE/) {
  67. $expect_pgp_sig = 1;
  68. if ($options{allow_pgp}) {
  69. # Skip PGP headers
  70. while (<$input>) {
  71. last if m/^$/;
  72. }
  73. } else {
  74. syntaxerr($file, _g("PGP signature not allowed here"));
  75. }
  76. } elsif (m/^$/) {
  77. if ($expect_pgp_sig) {
  78. # Skip empty lines
  79. $_ = <$input> while defined($_) && $_ =~ /^\s*$/;
  80. length($_) ||
  81. syntaxerr($file, _g("expected PGP signature, found EOF after blank line"));
  82. s/\n$//;
  83. m/^-----BEGIN PGP SIGNATURE/ ||
  84. syntaxerr($file,
  85. sprintf(_g("expected PGP signature, found something else \`%s'"), $_));
  86. # Skip PGP signature
  87. while (<$input>) {
  88. last if m/^-----END PGP SIGNATURE/;
  89. }
  90. length($_) ||
  91. syntaxerr($file, _g("unfinished PGP signature"));
  92. }
  93. last; # Finished parsing one block
  94. } else {
  95. syntaxerr($file, _g("line with unknown format (not field-colon-value)"));
  96. }
  97. }
  98. return $fields;
  99. }
  100. =back
  101. =cut
  102. 1;