Decoder/Base64.pm

filter: Statementstmt 100% 53 / 53 Branchbran 92.8% 13 / 14 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 98.7% 80 / 81 CC 9 SCAR 10.2
CC
9
Cov
99%
encode_it
4.0
decode_it
4.0
_decodable_stream_length
3.0
CRAP 2.8
« Body.pm ↑ Summary Field/ContType.pm »
1 package MIME::Decoder::Base64;
2 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5  
6 =head1 NAME
7  
8 MIME::Decoder::Base64 - encode/decode a "base64" stream
9  
10  
11 =head1 SYNOPSIS
12  
13 A generic decoder object; see L<MIME::Decoder> for usage.
14  
15  
16 =head1 DESCRIPTION
17  
18 A L<MIME::Decoder> subclass for the C<"base64"> encoding.
19 The name was chosen to jibe with the pre-existing MIME::Base64
20 utility package, which this class actually uses to translate each chunk.
21  
22 =over 4
23  
24 =item *
25  
26 When B<decoding>, the input is read one line at a time.
27 The input accumulates in an internal buffer, which is decoded in
28 multiple-of-4-sized chunks (plus a possible "leftover" input chunk,
29 of course).
30  
31 =item *
32  
33 When B<encoding>, the input is read 6840 (120 * 57) bytes at a time.
34 Each section of 57 bytes is encoded as a line containing 76 Base64
35 characters.
36  
37 =item *
38  
39 Some software produces Base64 parts that consist of concatentated
40 Base64-encoded streams. While this is not correct according to
41 RFC 2045, some clients will decode all of the streams and concatenate
42 the results. MIME::Decoder::Base64 copies this behavior when decoding.
43  
44 =back
45  
46 =head1 SEE ALSO
47  
48 L<MIME::Decoder>
49  
50 =head1 AUTHOR
51  
52 Eryq (F<eryq@zeegee.com>), ZeeGee Software Inc (F<http://www.zeegee.com>).
53  
54 All rights reserved. This program is free software; you can redistribute
55 it and/or modify it under the same terms as Perl itself.
56  
57 =cut
58  
59 13 ▶use vars qw(@ISA $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
60 13 ▶use MIME::Decoder;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 13 ▶use MIME::Base64 2.04;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 13 ▶use MIME::Tools qw(debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 13 @ISA = qw(MIME::Decoder);
65  
66 ### The package version, both in 1.23 style *and* usable by MakeMaker:
67 13 $VERSION = "5.522";
68  
69 ### How many bytes to encode at a time (must be a multiple of 3)
70 13 my $EncodeChunkLength = 120 * 57;
71  
72 ### How many bytes to decode at a time?
73 13 my $DecodeChunkLength = 32 * 1024;
74  
75 #------------------------------
76 #
77 # decode_it IN, OUT
78 #
79 55 ▶sub decode_it {
Subroutine decode_it: called CC=4 SCAR=13.9
Pod documented
80 55 my ($self, $in, $out) = @_;
81 55 my $len_4xN;
82  
83 ### Create a suitable buffer:
84 55 my $buffer = ' ' x (120 + $DecodeChunkLength); $buffer = '';
85 55 debug "in = $in; out = $out";
86  
87 ### Get chunks until done:
88 55 local($_) = ' ' x $DecodeChunkLength;
89 55 ▶ while ($in->read($_, $DecodeChunkLength)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in->read($_, $DecodeChunkLength)) 56 55 111
90 56 tr{A-Za-z0-9+/=}{}cd; ### get rid of non-base64 chars, but '='
91  
92 ### Concat any new input onto any leftover from the last round:
93 56 $buffer .= $_;
94  
95 ### Decode every stream that is all here:
96 56 ▶ while ($len_4xN = _decodable_stream_length(\$buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_4xN = _decodable_stream_length(\$buffer)) 149 56 205
97 149 $out->print(decode_base64(substr($buffer, 0, $len_4xN, '')));
98 }
99 }
100  
101 ### No more input remains. Dispose of anything left in buffer:
102 ### Pad to 4-byte multiple, and decode:
103 55 $buffer .= "==="; ### need no more than 3 pad chars
104  
105 ### Decode it!
106 55 ▶ while ($len_4xN = _decodable_stream_length(\$buffer)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len_4xN = _decodable_stream_length(\$buffer)) 3 55 58
107 3 $out->print(decode_base64(substr($buffer, 0, $len_4xN, '')));
108 }
109 55 1;
110 }
111  
112 #------------------------------
113 #
114 # _decodable_stream_length BUFREF
115 #
116 # How much of $$BUFREF can be decoded in one go: a whole base64 stream when
117 # it holds a padding, else the largest multiple of 4 bytes.
118 # 0 means not enough to work with... get more data!
119 #
120 # A part may carry several base64 streams in a row, each ended by its own
121 # '=' padding, and mainstream MUAs decode them in turn. decode_base64()
122 # never looks past a '=', so each padding must end one call and start the
123 # next.
124 #
125 263 ▶sub _decodable_stream_length {
Subroutine _decodable_stream_length: called CC=3 SCAR=11.0
126 263 my ($bufref) = @_; ### by reference: called once per stream
127 263 my ($pos_eq, $end_eq);
128  
129 ### No padding in sight: extract substring with highest multiple of 4
130 ### bytes, and leave the remainder for next time around:
131 263 $pos_eq = index($$bufref, '=');
132 263 ▶ return length($$bufref) & ~3 if $pos_eq < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos_eq < 0 76 187 263
133  
134 ### A padding ends the group it sits in, and that group ends the stream;
135 ### if that group is not all here yet, wait for the rest of it:
136 187 $end_eq = ($pos_eq | 3) + 1;
137 187 ▶ return $end_eq <= length($$bufref) ? $end_eq : 0;
Branch: 1 branch
BranchTrueFalseTotal
$end_eq <= length $$bufref ? : 131 56 187
138 }
139  
140 #------------------------------
141 #
142 # encode_it IN, OUT
143 #
144 10 ▶sub encode_it {
Subroutine encode_it: called CC=4 SCAR=13.9
Pod documented
145 10 my ($self, $in, $out) = @_;
146 10 my $encoded;
147  
148 my $nread;
149 10 my $buf = '';
150 10 ▶ my $nl = $MIME::Entity::BOUNDARY_DELIMITER || "\n";
Condition100%2/2
$MIME::Entity::BOUNDARY_DELIMITER || "\n"
l!l
28
MC/DC100%1/1
$MIME::Entity::BOUNDARY_DELIMITER || "\n"
$MIME::Entity::BOUNDARY_DELIMITER
Truth table100%2/2
A
A= $MIME::Entity::BOUNDARY_DELIMITER
Aresult
11
00
151 10 ▶ while ($nread = $in->read($buf, $EncodeChunkLength)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nread = $in->read($buf, $EncodeChunkLength)) 10 10 20
152 10 $encoded = encode_base64($buf, $nl);
153 10 ▶ $encoded .= $nl unless ($encoded =~ /$nl\Z/); ### ensure newline!
Branch: 1 branch
BranchTrueFalseTotal
unless $encoded =~ /$nl\Z/ 0 10 10
154 10 $out->print($encoded);
155 }
156 10 1;
157 }
158  
159 #------------------------------
160 13 1;
161  
« Body.pm ↑ Summary Field/ContType.pm »