File Coverage

blib/lib/PerlIO/via/CBC.pm
Criterion Covered Total %
statement 41 61 67.2
branch 14 26 53.8
condition 2 6 33.3
subroutine 9 11 81.8
pod 4 4 100.0
total 70 108 64.8


line stmt bran cond sub pod time code
1             package PerlIO::via::CBC;
2            
3 2     2   28275 use strict vars;
  2         5  
  2         81  
4 2     2   12 use warnings;
  2         4  
  2         65  
5            
6 2     2   2142 use Crypt::CBC ();
  2         11736  
  2         57  
7            
8 2     2   19 use vars '$VERSION';
  2         5  
  2         2006  
9             $VERSION = '0.08';
10            
11             my $Config = {};
12            
13             sub config {
14 1     1 1 20 my ($class, %args) = @_;
15 1 50       6 if(%args) {
16 1         4 $Config = {%args};
17             } else {
18 0         0 $Config = {};
19             }
20 1         9 return $Config;
21             }
22            
23             sub PUSHED {
24 2 50 66 2 1 1641 return -1 if $_[1] ne 'r' and $_[1] ne 'w';
25            
26 2         16 my $cbc = Crypt::CBC->new($Config);
27 2 50       3555 unless($cbc) {
28 0         0 require Carp;
29 0         0 Carp::croak("Couldn't create CBC object");
30             }
31            
32 2 100       9 if($_[1] eq 'r') { # open for reading: decrypt the data
33 1         700 $cbc->start('decrypting');
34             } else { # open for writing: encrypt the data
35 1         6 $cbc->start('encrypting');
36             }
37            
38 2         233 return (bless [$cbc, '', $_[1]], $_[0]);
39             }
40            
41             sub FILL {
42 3     3 1 304 my ($self, $fh) = @_;
43            
44             # Read the line from the handle
45 3         27 my $line = readline($fh);
46            
47 3         5 my $cbc = $self->[0];
48            
49             # If there is something to be crypted, crypt it
50 3 100       13 if(defined $line) {
    100          
51 1         5 return ($cbc->crypt($line));
52            
53             # elsif we still have an object (and end of data reached)
54             # Remove the object from PerlIO::via::Crypt object (so we'll really exit next)
55             # and finish crypting
56             } elsif($cbc) {
57 1         4 $self->[0] = '';
58 1         4 return ($cbc->finish());
59            
60             # else (end of data really reached)
61             # return signalling end of data reached
62             } else {
63 1         8 return (undef);
64             }
65             }
66            
67             sub BINMODE {
68 0     0   0 return (0);
69             }
70            
71             sub READ {
72 0     0   0 my ($self, $buffer, $len, $fh) = @_;
73            
74             # Read $len bytes from $fh into $buffer
75 0         0 my $ret = read $fh, $buffer, $len;
76            
77             # On Error return undef
78 0 0       0 return $ret unless defined $ret;
79            
80 0         0 my $cbc = $self->[0];
81            
82             # If there is something to be crypted, crypt it
83 0 0       0 if($ret) {
84 0         0 $buffer = $cbc->crypt($buffer);
85            
86             # elsif we still have an object (and end of data reached)
87             # Remove the object from PerlIO::via::Crypt object (so we'll really exit next)
88             # and finish crypting
89             } else {
90 0         0 $self->[0] = '';
91 0         0 $buffer = $cbc->finish();
92             }
93 0         0 $self->[1] = '';
94            
95             # calc length
96 0         0 $ret = length $buffer;
97            
98             # buffer is greater than required, shorten it but remember it
99 0 0 0     0 if($ret > $len and $self->[0])
100             {
101 0         0 $self->[1] = substr($buffer, $len);
102 0         0 $buffer = substr(0, $len);
103 0         0 $ret = $len;
104             }
105            
106             # return length of data (hopefully always less equal than $len)
107 0         0 return $ret;
108             }
109            
110             sub WRITE {
111 1     1   3 my ($self, $buffer, $fh) = @_;
112            
113 1         10 my $buf = $self->[0]->crypt($buffer);
114 1 50       1455 return ((print {$fh} $buf) ? length ($buf) : -1);
  1         22  
115             }
116            
117             sub FLUSH {
118 2     2 1 6 my ($self, $fh) = @_;
119            
120 2 100       36 return 0 if $self->[2] eq 'r';
121            
122 1         6 my $buf = $self->[0]->finish();
123 1 50       44 if($buf) {
124 1 50       2 return ((print {$fh} $buf) ? 0 : -1);
  1         112  
125             }
126            
127 0           return (0);
128             }
129            
130             1;
131             __END__