File Coverage

blib/lib/Crypt/OpenPGP/Buffer.pm
Criterion Covered Total %
statement 17 17 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod 2 2 100.0
total 23 23 100.0


line stmt bran cond sub pod time code
1             package Crypt::OpenPGP::Buffer;
2 11     11   79351 use base qw( Data::Buffer );
  11         15  
  11         6738  
3              
4 11     11   24410 use Crypt::OpenPGP::Util qw( bin2mp mp2bin bitsize );
  11         34  
  11         2655  
5              
6             sub get_big_int {
7 310     310 1 530 my $buf = shift;
8 310         1056 my $bits = $buf->get_int16;
9 310         5061 my $bytes = int(($bits + 7) / 8);
10 310         516 my $off = $buf->{offset};
11 310         463 $buf->{offset} += $bytes;
12 310         802 my $int = bin2mp($buf->bytes($off, $bytes));
13 310         906356 return "$int";
14             }
15              
16             sub put_big_int {
17 75     75 1 9011 my $buf = shift;
18 75         142 my($n) = @_;
19 75         377 $buf->put_int16(bitsize($n));
20 75         165379 $buf->put_bytes(mp2bin($n));
21             }
22              
23             *get_mp_int = \&get_big_int;
24             *put_mp_int = \&put_big_int;
25              
26             1;
27             __END__
28              
29             =head1 NAME
30              
31             Crypt::OpenPGP::Buffer - Binary in/out buffer
32              
33             =head1 SYNOPSIS
34              
35             use Crypt::OpenPGP::Buffer;
36              
37             my $n = Math::BigInt->new( 1 );
38            
39             my $buf = Crypt::OpenPGP::Buffer->new;
40             $buf->put_big_int($n);
41              
42             my $m = $buf->get_big_int;
43              
44             =head1 DESCRIPTION
45              
46             I<Crypt::OpenPGP::Buffer> subclasses the I<Data::Buffer> class to
47             provide binary in/out buffer capabilities for I<Crypt::OpenPGP>. In
48             addition to the standard I<Data::Buffer> methods, this class adds
49             methods to get and put multiple-precision integers (I<Math::BigInt>
50             objects).
51              
52             A PGP multiple precision integer is stored in two pieces: a two-octet
53             scalar representing the length of the integer in bits, followed by
54             a string of octets that is a serialized representation of the integer.
55              
56             =head1 USAGE
57              
58             As I<Crypt::OpenPGP::Buffer> subclasses I<Data::Buffer> there is no
59             need to reproduce the entire documentation of the latter module. Thus
60             this usage section will include only the methods added by
61             I<Crypt::OpenPGP::Buffer>.
62              
63             =head2 $buf->get_big_int
64              
65             Grabs a multiple-precision integer from the buffer I<$buf> (starting
66             after the current offset position in the buffer) and returns that
67             integer.
68              
69             I<get_mp_int()> is an alias for this method, for backwards
70             compatibility reasons.
71              
72             =head2 $buf->put_big_int($n)
73              
74             Serializes a multiple-precision integer into the buffer in the above
75             form (two-octet bitsize, string of octets).
76              
77             I<put_mp_int()> is an alias for this method, for backwards
78             compatibility reasons.
79              
80             =head1 AUTHOR & COPYRIGHTS
81              
82             Please see the Crypt::OpenPGP manpage for author, copyright, and
83             license information.
84              
85             =cut