File Coverage

blib/lib/Crypt/OpenPGP/Message.pm
Criterion Covered Total %
statement 64 64 100.0
branch 12 16 75.0
condition 10 11 90.9
subroutine 11 11 100.0
pod 2 5 40.0
total 99 107 92.5


line stmt bran cond sub pod time code
1             package Crypt::OpenPGP::Message;
2 11     11   117 use strict;
  11         49  
  11         422  
3 11     11   51 use warnings;
  11         20  
  11         899  
4              
5             our $VERSION = '1.19'; # VERSION
6              
7 11     11   120 use Crypt::OpenPGP::Buffer;
  11         133  
  11         2433  
8 11     11   83 use Crypt::OpenPGP::PacketFactory;
  11         43  
  11         319  
9 11     11   52 use Crypt::OpenPGP::ErrorHandler;
  11         19  
  11         518  
10 11     11   74 use base qw( Crypt::OpenPGP::ErrorHandler );
  11         18  
  11         11114  
11              
12             sub new {
13 64     64 1 868 my $class = shift;
14 64         240 my $msg = bless { }, $class;
15 64         340 $msg->init(@_);
16             }
17              
18             sub init {
19 64     64 0 149 my $msg = shift;
20 64         280 my %param = @_;
21 64         336 $msg->{is_packet_stream} = delete $param{IsPacketStream};
22 64         199 $msg->{pieces} = [];
23 64   100     375 $msg->{_data} = $param{Data} || '';
24 64 100 66     299 if (!$msg->{_data} && (my $file = $param{Filename})) {
25 1         7 local *FH;
26 1 50       102 open FH, $file or
27             return (ref $msg)->error("Can't open message $file: $!");
28 1         5 binmode FH;
29 1         3 { local $/; $msg->{_data} = }
  1         5  
  1         77  
30 1         16 close FH;
31             }
32 64 50       404 $msg->read or return;
33 64         688 $msg;
34             }
35              
36             sub read {
37 64     64 0 167 my $msg = shift;
38             my $data = $msg->{_data} or
39 64 50       301 return $msg->error("Message contains no data");
40 64         140 my $pt;
41 64 100 100     472 if (!$msg->{is_packet_stream} &&
42             $data =~ /-----BEGIN PGP SIGNED MESSAGE/) {
43 2         517 require Crypt::OpenPGP::Armour;
44 2         13 require Crypt::OpenPGP::Util;
45 2         14 require Crypt::OpenPGP::Plaintext;
46 2         71 my($head, $text, $sig) = $data =~
47             m!-----BEGIN PGP SIGNED MESSAGE-----(.*?\r?\n\r?\n)?(.+?)(-----BEGIN PGP SIGNATURE.*?END PGP SIGNATURE-----)!s;
48             ## In clear-signed messages, the line ending before the signature
49             ## is not considered part of the signed text.
50 2         23 $text =~ s!\r?\n$!!;
51 2         25 $pt = Crypt::OpenPGP::Plaintext->new(
52             Data => Crypt::OpenPGP::Util::dash_unescape($text),
53             Mode => 't',
54             );
55 2         6 $data = $sig;
56             }
57              
58 64 100 100     475 if (!$msg->{is_packet_stream} && $data =~ /^-----BEGIN PGP/m) {
59 9         1757 require Crypt::OpenPGP::Armour;
60 9 50       84 my $rec = Crypt::OpenPGP::Armour->unarmour($data) or
61             return $msg->error("Unarmour failed: " .
62             Crypt::OpenPGP::Armour->errstr);
63 9         41 $data = $rec->{Data};
64             }
65 64         501 my $buf = Crypt::OpenPGP::Buffer->new;
66 64         979 $buf->append($data);
67 64         639 $msg->restore($buf);
68 64 100       809 push @{ $msg->{pieces} }, $pt if $pt;
  2         8  
69 64         382 1;
70             }
71              
72             sub restore {
73 64     64 0 156 my $msg = shift;
74 64         167 my($buf) = @_;
75 64         484 while (my $packet = Crypt::OpenPGP::PacketFactory->parse($buf)) {
76 100         212 push @{ $msg->{pieces} }, $packet;
  100         693  
77             }
78             }
79              
80 64     64 1 1925 sub pieces { @{ $_[0]->{pieces} } }
  64         273  
81              
82             1;
83             __END__