Mail/Make/Stream/Base64.pm

filter: Statementstmt 85.2% 81 / 95 Branchbran 50.0% 15 / 30 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 80.0% 16 / 20 Podpod 100% 6 / 6 totaltotal 72.8% 121 / 166 CC 19 SCAR 16.8
CC
19
Cov
74%
encode
11.7
decode
6.3
is_decoder_installed
6.0
CRAP 5.3
« Mail/Make/Headers/ContentType.pm ↑ Summary Mail/Make/Headers/ContentTransferEncoding.pm »
1 ##----------------------------------------------------------------------------
2 ## MIME Email Builder - ~/lib/Mail/Make/Stream/Base64.pm
3 ## Version v0.3.0
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/03/02
7 ## Modified 2026/03/05
8 ## All rights reserved
9 ##
10 ## This program is free software; you can redistribute it and/or modify it
11 ## under the same terms as Perl itself.
12 ##----------------------------------------------------------------------------
13 package Mail::Make::Stream::Base64;
14 BEGIN
15 8 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
16 8 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 8 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 8 warnings::register_categories( 'Mail::Make' );
19 8 ▶ use Mail::Make::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 8 ▶ use parent -norequire, qw( Mail::Make::Stream::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 8 ▶ use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $Base64Error );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 ▶ use Exporter qw( import );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 8 ▶ use Mail::Make::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 8 ▶ use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 use constant
26 8 ▶ {
Subroutine BEGIN: called CC=1 SCAR=0.0
27 # Input chunk must be a multiple of 3 so base64 output lines align cleanly
28 8 ENCODE_BUFFER_SIZE => 300,
29 DECODE_BUFFER_SIZE => ( 32 * 1024 ),
30 8 };
31 8 our @EXPORT_OK = qw( decode_b64 encode_b64 );
32 8 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
33 8 our $VERSION = 'v0.3.0';
34 };
35  
36 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 # decode( $from, $to [, %opts] )
40 # Decodes base64 data from $from and writes raw bytes to $to.
41 # Each of $from / $to may be: a filehandle, a scalar reference, or a file path.
42 sub decode
43 4 ▶{
Subroutine decode: called CC=6 SCAR=18.4
Pod documented
44 4 my $self = shift( @_ );
45 4 my $from = shift( @_ );
46 4 my $to = shift( @_ );
47 4 my $opts = $self->_get_args_as_hash( @_ );
48 4 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
49 4 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
50 4 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 4 4
Condition33%1/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
004
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table33%1/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
51 4 my( $n, $buff );
52  
53 4 ▶ while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 32768)) 4 4 8
54 {
55 4 my $decoded = MIME::Base64::decode_base64( $buff );
56 4 my $rv = $writer->( $decoded );
57 4 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 4 4
58 }
59 4 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 4 4
60 4 return( $self );
61 }
62  
63 sub decode_b64
64 1 ▶{
Subroutine decode_b64: called CC=2 SCAR=7.6
Pod documented
65 1 my $s = __PACKAGE__->new;
66 1 my $rv = $s->decode( @_ );
67 1 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 1 1
68 {
69 0 $Base64Error = $s->error;
70 0 return( $s->pass_error );
71 }
72 else
73 {
74 1 undef( $Base64Error );
75 1 return( $rv );
76 }
77 }
78  
79 # encode( $from, $to [, %opts] )
80 # Encodes raw data from $from as RFC 2045 base64 and writes to $to.
81 # Options:
82 # eol => $str line ending after each 76-char line (default CRLF "\015\012")
83 sub encode
84 18 ▶{
Subroutine encode: called CC=10 SCAR=24.6
Pod documented
85 18 my $self = shift( @_ );
86 18 my $from = shift( @_ );
87 18 my $to = shift( @_ );
88 18 my $opts = $self->_get_args_as_hash( @_ );
89 18 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
90 18 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
91 18 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 18 18
Condition33%1/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table33%1/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
92 18 ▶ my $eol = exists( $opts->{eol} ) ? $opts->{eol} : "\015\012";
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'eol'} ? : 0 18 18
93 18 ▶ my $has_eol = defined( $eol ) && length( $eol );
Condition33%1/3
defined $eol && length $eol
!ll&&!rl&&r
0018
MC/DC0%0/2
defined $eol && length $eol
defined $eol length $eol
Truth table33%1/3
A && B
A= defined $eolB= length $eol
ABresult
0X0
100
111
94 18 my( $n, $buff );
95  
96 18 ▶ while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 300)) 21 18 39
97 {
98 # MIME::Base64::encode_base64 appends its own newline - strip it, then
99 # insert our configured eol every 76 characters.
100 21 my $encoded = MIME::Base64::encode_base64( $buff, '' );
101 21 ▶ if( $has_eol )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_eol) 21 0 21
102 {
103 21 $encoded =~ s/(.{76})/$1$eol/g;
104 # Ensure a trailing eol if the final chunk did not land on a 76-char boundary
105 21 ▶ $encoded .= $eol unless( $encoded =~ /\015\012$/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $encoded =~ /\015\012$/ 21 0 21
106 }
107 21 my $rv = $writer->( $encoded );
108 21 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 21 21
109 }
110 18 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 18 18
111 18 return( $self );
112 }
113  
114 sub encode_b64
115 1 ▶{
Subroutine encode_b64: called CC=2 SCAR=7.6
Pod documented
116 1 my $s = __PACKAGE__->new;
117 1 my $rv = $s->encode( @_ );
118 1 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 1 1
119 {
120 0 $Base64Error = $s->error;
121 0 return( $s->pass_error );
122 }
123 else
124 {
125 1 undef( $Base64Error );
126 1 return( $rv );
127 }
128 }
129  
130 sub is_decoder_installed
131 0 ▶{
Subroutine is_decoder_installed: not called CC=2 SCAR=17.9
Pod documented
132 0 local $@;
133 eval
134 0 {
135 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 0 require MIME::Base64;
137 };
138 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
139 }
140  
141 sub is_encoder_installed
142 0 ▶{
Subroutine is_encoder_installed: not called CC=2 SCAR=17.9
Pod documented
143 0 local $@;
144 eval
145 0 {
146 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 require MIME::Base64;
148 };
149 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
150 }
151  
152 # NOTE: sub FREEZE is inherited
153  
154 # NOTE: sub STORABLE_freeze is inherited
155  
156 # NOTE: sub STORABLE_thaw is inherited
157  
158 # NOTE: sub THAW is inherited
159  
160 8 1;
161 # NOTE: POD
162 __END__
« Mail/Make/Headers/ContentType.pm ↑ Summary Mail/Make/Headers/ContentTransferEncoding.pm »