| 1 | # Copyright (c) 1998-2006 ActiveState Software Inc. | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # All rights reserved. | ||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||
| 4 | # This software (the Perl-DBGP package) is covered by the Artistic License | ||||||||||||||||||||||||||||||||||||||||||
| 5 | # (http://www.opensource.org/licenses/artistic-license.php). | ||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | package DB::MIME::Base64; | ||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | use strict; | ||||||||||||||||||||||||||||||||||||||||||
| 10 | use vars qw(@ISA $VERSION); | ||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | # Based on MIME::Base64::Perl | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # Exports nothing | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | $VERSION = '1.00'; | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | sub encode_base64 ($;$) | ||||||||||||||||||||||||||||||||||||||||||
| 18 | { | ||||||||||||||||||||||||||||||||||||||||||
| 19 | 0 | ▶ | if ($] >= 5.006) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
encode_base64: not called CC=8 SCAR=42.8
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 20 | 0 | require bytes; | |||||||||||||||||||||||||||||||||||||||||
| 21 | 0 | ▶ | if (bytes::length($_[0]) > length($_[0]) || | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
&bytes::length($_[0]) > length $_[0] or $] >= "5.008" and $_[0] =~ /[^\0-\xFF]/
$] >= "5.008" and $_[0] =~ /[^\0-\xFF]/
MC/DC0%0/3
&bytes::length($_[0]) > length $_[0] or $] >= "5.008" and $_[0] =~ /[^\0-\xFF]/
&bytes::length($_[0]) > length $_[0]
$] >= "5.008"
$_[0] =~ /[^\0-\xFF]/
Truth table0%0/4
A or B and C
A= &bytes::length($_[0]) > length $_[0]B= $] >= "5.008"C= $_[0] =~ /[^\0-\xFF]/
| |||||||||||||||||||||||||||||||||||||||||||
| 22 | ($] >= 5.008 && $_[0] =~ /[^\0-\xFF]/)) | ||||||||||||||||||||||||||||||||||||||||||
| 23 | { | ||||||||||||||||||||||||||||||||||||||||||
| 24 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | Carp::croak("The Base64 encoding is only defined for bytes"); | |||||||||||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | use integer; | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my $eol = $_[1]; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $eol = "\n" unless defined $eol; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | my $res = pack("u", $_[0]); | |||||||||||||||||||||||||||||||||||||||||
| 35 | # Remove first character of each line, remove newlines | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | $res =~ s/^.//mg; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $res =~ s/\n//g; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | 0 | $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs | |||||||||||||||||||||||||||||||||||||||||
| 40 | # fix padding at the end | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 0 | my $padding = (3 - length($_[0]) % 3) % 3; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $res =~ s/.{$padding}$/'=' x $padding/e if $padding; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | # break encoded string into lines of no more than 76 characters each | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | if (length $eol) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | $res =~ s/(.{1,76})/$1$eol/g; | |||||||||||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 0 | return $res; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||
| 51 | sub decode_base64 ($) | ||||||||||||||||||||||||||||||||||||||||||
| 52 | { | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | local($^W) = 0; # unpack("u",...) gives bogus warning in 5.00[123] | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
decode_base64: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | use integer; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $str = shift; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | $str =~ tr|A-Za-z0-9+=/||cd; # remove non-base64 chars | |||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if (length($str) % 4) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | require Carp; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | Carp::carp("Length of base64 data not a multiple of 4") | |||||||||||||||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | $str =~ s/=+$//; # remove padding | |||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | $str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format | |||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | ▶ | return "" unless length $str; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | ## I guess this could be written as | ||||||||||||||||||||||||||||||||||||||||||
| 67 | #return unpack("u", join('', map( chr(32 + length($_)*3/4) . $_, | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # $str =~ /(.{1,60})/gs) ) ); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | ## but I do not like that... | ||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | my $uustr = ''; | |||||||||||||||||||||||||||||||||||||||||
| 71 | 0 | my ($i, $l); | |||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | $l = length($str) - 60; | |||||||||||||||||||||||||||||||||||||||||
| 73 | 0 | ▶ | for ($i = 0; $i <= $l; $i += 60) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $uustr .= "M" . substr($str, $i, 60); | |||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | $str = substr($str, $i); | |||||||||||||||||||||||||||||||||||||||||
| 77 | # and any leftover chars | ||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | ▶ | if ($str ne "") { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | $uustr .= chr(32 + length($str)*3/4) . $str; | |||||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||||
| 81 | 0 | return unpack ("u", $uustr); | |||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 1; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | __END__ | ||||||||||||||||||||||||||||||||||||||||||