File Coverage

blib/lib/Crypt/OpenPGP/Plaintext.pm
Criterion Covered Total %
statement 41 41 100.0
branch 2 2 100.0
condition 3 4 75.0
subroutine 10 10 100.0
pod 5 6 83.3
total 61 63 96.8


line stmt bran cond sub pod time code
1             package Crypt::OpenPGP::Plaintext;
2 8     8   24271 use strict;
  8         14  
  8         279  
3              
4 8     8   557 use Crypt::OpenPGP::Buffer;
  8         11  
  8         219  
5 8     8   442 use Crypt::OpenPGP::ErrorHandler;
  8         12  
  8         228  
6 8     8   36 use base qw( Crypt::OpenPGP::ErrorHandler );
  8         11  
  8         3459  
7              
8             sub new {
9 63     63 1 618 my $class = shift;
10 63         136 my $pt = bless { }, $class;
11 63         201 $pt->init(@_);
12             }
13              
14 35     35 1 273 sub data { $_[0]->{data} }
15 12     12 1 58 sub mode { $_[0]->{mode} }
16              
17             sub init {
18 63     63 0 84 my $pt = shift;
19 63         136 my %param = @_;
20 63 100       192 if (my $data = $param{Data}) {
21 31         101 $pt->{data} = $data;
22 31   100     190 $pt->{mode} = $param{Mode} || 'b';
23 31         91 $pt->{timestamp} = time;
24 31   50     173 $pt->{filename} = $param{Filename} || '';
25             }
26 63         164 $pt;
27             }
28              
29             sub parse {
30 32     32 1 69 my $class = shift;
31 32         46 my($buf) = @_;
32 32         99 my $pt = $class->new;
33 32         151 $pt->{mode} = $buf->get_char;
34 32         510 $pt->{filename} = $buf->get_bytes($buf->get_int8);
35 32         627 $pt->{timestamp} = $buf->get_int32;
36 32         469 $pt->{data} = $buf->get_bytes( $buf->length - $buf->offset );
37 32         402 $pt;
38             }
39              
40             sub save {
41 31     31 1 396 my $pt = shift;
42 31         150 my $buf = Crypt::OpenPGP::Buffer->new;
43 31         320 $buf->put_char($pt->{mode});
44 31         330 $buf->put_int8(length $pt->{filename});
45 31         278 $buf->put_bytes($pt->{filename});
46 31         298 $buf->put_int32($pt->{timestamp});
47 31         225 $buf->put_bytes($pt->{data});
48 31         227 $buf->bytes;
49             }
50              
51             1;
52             __END__
53              
54             =head1 NAME
55              
56             Crypt::OpenPGP::Plaintext - A plaintext, literal-data packet
57              
58             =head1 SYNOPSIS
59              
60             use Crypt::OpenPGP::Plaintext;
61              
62             my $data = 'foo bar';
63             my $file = 'foo.txt';
64              
65             my $pt = Crypt::OpenPGP::Plaintext->new(
66             Data => $data,
67             Filename => $file,
68             );
69             my $serialized = $pt->save;
70              
71             =head1 DESCRIPTION
72              
73             I<Crypt::OpenPGP::Plaintext> implements plaintext literal-data packets,
74             and is essentially just a container for a string of octets, along
75             with some meta-data about the plaintext.
76              
77             =head1 USAGE
78              
79             =head2 Crypt::OpenPGP::Plaintext->new( %arg )
80              
81             Creates a new plaintext data packet object and returns that object.
82             If there are no arguments in I<%arg>, the object is created with an
83             empty data container; this is used, for example, in I<parse> (below),
84             to create an empty packet which is then filled from the data in the
85             buffer.
86              
87             If you wish to initialize a non-empty object, I<%arg> can contain:
88              
89             =over 4
90              
91             =item * Data
92              
93             A block of octets that make up the plaintext data.
94              
95             This argument is required (for a non-empty object).
96              
97             =item * Filename
98              
99             The name of the file that this data came from, or the name of a file
100             where it should be saved upon extraction from the packet (after
101             decryption, for example, if this packet is going to be encrypted).
102              
103             =item * Mode
104              
105             The mode in which the data is formatted. Valid values are C<t> and
106             C<b>, meaning "text" and "binary", respectively.
107              
108             This argument is optional; I<Mode> defaults to C<b>.
109              
110             =back
111              
112             =head2 $pt->save
113              
114             Returns the serialized form of the plaintext object, which is the
115             plaintext data, preceded by some meta-data describing the data.
116              
117             =head2 Crypt::OpenPGP::Plaintext->parse($buffer)
118              
119             Given I<$buffer>, a I<Crypt::OpenPGP::Buffer> object holding (or
120             with offset pointing to) a plaintext data packet, returns a new
121             I<Crypt::OpenPGP::Ciphertext> object, initialized with the data
122             in the buffer.
123              
124             =head2 $pt->data
125              
126             Returns the plaintext data.
127              
128             =head2 $pt->mode
129              
130             Returns the mode of the packet (either C<t> or C<b>).
131              
132             =head1 AUTHOR & COPYRIGHTS
133              
134             Please see the Crypt::OpenPGP manpage for author, copyright, and
135             license information.
136              
137             =cut