line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MIME::Base64::Perl; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: Perl.pm,v 1.2 2004/01/14 12:52:44 gisle Exp $ |
4
|
|
|
|
|
|
|
|
5
|
2
|
|
|
2
|
|
1213
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
70
|
|
6
|
2
|
|
|
2
|
|
11
|
use vars qw(@ISA @EXPORT $VERSION); |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
401
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
require Exporter; |
9
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
10
|
|
|
|
|
|
|
@EXPORT = qw(encode_base64 decode_base64); |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
$VERSION = '1.00'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub encode_base64 ($;$) |
15
|
|
|
|
|
|
|
{ |
16
|
271
|
50
|
|
271
|
1
|
2365
|
if ($] >= 5.006) { |
17
|
271
|
|
|
|
|
987
|
require bytes; |
18
|
271
|
100
|
33
|
|
|
572
|
if (bytes::length($_[0]) > length($_[0]) || |
|
|
|
66
|
|
|
|
|
19
|
|
|
|
|
|
|
($] >= 5.008 && $_[0] =~ /[^\0-\xFF]/)) |
20
|
|
|
|
|
|
|
{ |
21
|
1
|
|
|
|
|
993
|
require Carp; |
22
|
1
|
|
|
|
|
228
|
Carp::croak("The Base64 encoding is only defined for bytes"); |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
} |
25
|
|
|
|
|
|
|
|
26
|
2
|
|
|
2
|
|
1873
|
use integer; |
|
2
|
|
|
|
|
22
|
|
|
2
|
|
|
|
|
12
|
|
27
|
|
|
|
|
|
|
|
28
|
270
|
|
|
|
|
2832
|
my $eol = $_[1]; |
29
|
270
|
100
|
|
|
|
413
|
$eol = "\n" unless defined $eol; |
30
|
|
|
|
|
|
|
|
31
|
270
|
|
|
|
|
399
|
my $res = pack("u", $_[0]); |
32
|
|
|
|
|
|
|
# Remove first character of each line, remove newlines |
33
|
270
|
|
|
|
|
871
|
$res =~ s/^.//mg; |
34
|
270
|
|
|
|
|
494
|
$res =~ s/\n//g; |
35
|
|
|
|
|
|
|
|
36
|
270
|
|
|
|
|
319
|
$res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs |
37
|
|
|
|
|
|
|
# fix padding at the end |
38
|
270
|
|
|
|
|
348
|
my $padding = (3 - length($_[0]) % 3) % 3; |
39
|
270
|
100
|
|
|
|
1130
|
$res =~ s/.{$padding}$/'=' x $padding/e if $padding; |
|
263
|
|
|
|
|
481
|
|
40
|
|
|
|
|
|
|
# break encoded string into lines of no more than 76 characters each |
41
|
270
|
100
|
|
|
|
523
|
if (length $eol) { |
42
|
1
|
|
|
|
|
8
|
$res =~ s/(.{1,76})/$1$eol/g; |
43
|
|
|
|
|
|
|
} |
44
|
270
|
|
|
|
|
657
|
return $res; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub decode_base64 ($) |
49
|
|
|
|
|
|
|
{ |
50
|
283
|
|
|
283
|
1
|
1359
|
local($^W) = 0; # unpack("u",...) gives bogus warning in 5.00[123] |
51
|
2
|
|
|
2
|
|
419
|
use integer; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
7
|
|
52
|
|
|
|
|
|
|
|
53
|
283
|
|
|
|
|
273
|
my $str = shift; |
54
|
283
|
|
|
|
|
294
|
$str =~ tr|A-Za-z0-9+=/||cd; # remove non-base64 chars |
55
|
283
|
100
|
|
|
|
494
|
if (length($str) % 4) { |
56
|
4
|
|
|
|
|
17
|
require Carp; |
57
|
4
|
|
|
|
|
510
|
Carp::carp("Length of base64 data not a multiple of 4") |
58
|
|
|
|
|
|
|
} |
59
|
283
|
|
|
|
|
682
|
$str =~ s/=+$//; # remove padding |
60
|
283
|
|
|
|
|
294
|
$str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format |
61
|
283
|
100
|
|
|
|
455
|
return "" unless length $str; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
## I guess this could be written as |
64
|
|
|
|
|
|
|
#return unpack("u", join('', map( chr(32 + length($_)*3/4) . $_, |
65
|
|
|
|
|
|
|
# $str =~ /(.{1,60})/gs) ) ); |
66
|
|
|
|
|
|
|
## but I do not like that... |
67
|
280
|
|
|
|
|
262
|
my $uustr = ''; |
68
|
280
|
|
|
|
|
229
|
my ($i, $l); |
69
|
280
|
|
|
|
|
253
|
$l = length($str) - 60; |
70
|
280
|
|
|
|
|
598
|
for ($i = 0; $i <= $l; $i += 60) { |
71
|
5
|
|
|
|
|
16
|
$uustr .= "M" . substr($str, $i, 60); |
72
|
|
|
|
|
|
|
} |
73
|
280
|
|
|
|
|
328
|
$str = substr($str, $i); |
74
|
|
|
|
|
|
|
# and any leftover chars |
75
|
280
|
50
|
|
|
|
447
|
if ($str ne "") { |
76
|
280
|
|
|
|
|
434
|
$uustr .= chr(32 + length($str)*3/4) . $str; |
77
|
|
|
|
|
|
|
} |
78
|
280
|
|
|
|
|
809
|
return unpack ("u", $uustr); |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
1; |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
__END__ |