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