lib/VOMS/Lite/Base64.pm untested

filter: Statementstmt 0.0% 0 / 46 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 90 CC 10 SCAR 36.0
CC
10
Cov
0%
Decode
56.0
Encode
20.0
BEGIN
2.0
CRAP 36.6
1 package VOMS::Lite::Base64;
2  
3 require Exporter;
4 use vars qw($VERSION $DEBUG @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
5 @ISA = qw(Exporter);
6 $VERSION = '0.20';
7  
8 my %Alphabets = ( VOMS => "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789[]",
9 RFC3548 => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
10 RFC3548URL => "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-="
11 );
12  
13 sub Encode{
14 my $data = shift;
15 my $str = shift; # Can supply custom Base64
16 my $pad="";
17 if ( defined $str ) {
18 $str = $Alphabets{$str} if ($Alphabets{$str});
19 if ( $str =~ /^(.{64})(.?)$/s ) { $str=$1; $pad="$2"; }
20 else { return undef; }
21 }
22 else { $str = $Alphabets{RFC3548}; }
23 $data=~s|(.)(.?)(.?)| substr($str,((ord($1)&252)>>2),1).
24 substr($str,((ord($1)&3)<<4)+((ord($2)&240)>>4),1).
25 ((length($2))?substr($str,((ord($2)&15)<<2)+((ord($3)&192)>>6),1):$pad).
26 ((length($3))?substr($str,(ord($3)&63),1):$pad)|gse;
27 return $data;
28 }
29  
30 sub Decode {
31 my $data = shift;
32 my $str = shift; # Can supply custom Base64
33 my $pad="=";
34  
35 my $type;
36 if ( defined $str && ! defined $Alphabets{$str} ) { $type = 'USER'; }
37 elsif ( defined $str && defined $Alphabets{$str} ) { $type = $str; }
38 #Try to guess
39 elsif ( $data =~ /[\[\]]/s && $data !~ /[+\/_-]/ ) { $type = 'VOMS'; }
40 elsif ( $data =~ /[_-]/s && $data !~ /[\[\]+\/]/) { $type = 'RFC3548URL'; }
41 else { $type = 'RFC3548'; } # Assume Standard Base64 if
42 if ( $type eq "USER" ) { $Alphabets{'USER'} = $str; }
43  
44 #strip non-base64 chars
45 my $estr;
46 if ( $Alphabets{$type} =~ /^(.{64})(.?)$/s ) { $str=$1; $estr=quotemeta($1); $pad=$2; } else { return undef; }
47 $data =~ s/[^$estr]//gs;
48  
49 # Force Padding
50 $data .= $pad x (3-(((length($data)+3) % 4)));
51 $data=~s|(.)(.)(.?)(.?)|
52 chr(((index($str,$1)<<2)&252)+((index($str,$2)>>4)&3)). #six bits from first with two bits from the second
53 (($3 ne $pad)?chr(((index($str,$2)<<4)&240)+((index($str,$3)>>2)&15)):""). #last 4 bits from second with four bits from third unless third is pad
54 (($4 ne $pad)?chr(((index($str,$3)<<6)&192)+((index($str,$4))&63)):"") #last 2 bits from third with six bits from the forth unless forth is pad
55 |ge;
56 return $data;
57 }
58  
59 1;
60  
61 __END__