Perlito5X/Java/MIME/Base64.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 55 CC 2 SCAR 10.6
CC
2
Cov
0%
decode_base64
6.0
encode_base64
2.0
encode_base64url
2.0
CRAP 2.9
1 package MIME::Base64;
2 use strict;
3  
4 use Exporter qw(import);
5 our @EXPORT = qw(encode_base64 decode_base64);
6 our @EXPORT_OK = qw(encode_base64url decode_base64url encoded_base64_length decoded_base64_length);
7  
8 # Java 8 only
9 # package Java::Base64 { import => "java.util.Base64" }
10  
11 package Java::DatatypeConverter { import => "javax.xml.bind.DatatypeConverter" }
12 package String {};
13  
14 # encode_base64( $bytes )
15 # encode_base64( $bytes, $eol );
16 # Encode data by calling the encode_base64() function. The first argument is the byte string to encode.
17 # The second argument is the line-ending sequence to use. It is optional and defaults to "\n". The
18 # returned encoded string is broken into lines of no more than 76 characters each and it will end with
19 # $eol unless it is empty. Pass an empty string as second argument if you do not want the encoded string
20 # to be broken into lines.
21 #
22 # The function will croak with "Wide character in subroutine entry" if $bytes contains characters with
23 # code above 255. The base64 encoding is only defined for single-byte characters. Use the Encode module
24 # to select the byte encoding you want.
25 sub encode_base64 {
26 my $s = shift;
27 my $eol = shift // "\n";
28 my $result =
29 Java::DatatypeConverter->printBase64Binary( $s->toString()->getBytes() );
30 my @out;
31 while ($result) {
32 push @out, substr($result, 0, 76);
33 $result = substr($result, 76);
34 }
35 return join($eol, @out) . $eol;
36 }
37  
38 # decode_base64( $str )
39 # Decode a base64 string by calling the decode_base64() function. This function takes a single argument
40 # which is the string to decode and returns the decoded data.
41 #
42 # Any character not part of the 65-character base64 subset is silently ignored. Characters occurring
43 # after a '=' padding character are never decoded.
44 sub decode_base64 {
45 my $s = shift;
46 my $end = index($s, "=");
47 if ($end >= 0) {
48 $s = substr($s, 0, $end);
49 }
50 my $result =
51 String->new( Java::DatatypeConverter->parseBase64Binary( $s->toString() ) );
52 return $result;
53 }
54  
55 # plain-Perl code from MIME::Base64
56 *encode = \&encode_base64;
57 *decode = \&decode_base64;
58  
59 sub encode_base64url {
60 my $e = encode_base64(shift, "");
61 $e =~ s/=+\z//;
62 $e =~ tr[+/][-_];
63 return $e;
64 }
65  
66 sub decode_base64url {
67 my $s = shift;
68 $s =~ tr[-_][+/];
69 $s .= '=' while length($s) % 4;
70 return decode_base64($s);
71 }
72  
73  
74 sub encoded_base64_length { length(encode_base64(@_)) }
75 sub decoded_base64_length { length(decode_base64(@_)) }
76  
77 1;
78  
79 __END__