| 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
Condition33%1/3
defined $from_fh and defined $to_fh
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
| |||||||||||||||||||||||||||||
| 51 | 4 | my( $n, $buff ); | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 4 | ▶ | while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 4 | ▶ | return( $self->pass_error ) if( !defined( $n ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $from_fh and defined $to_fh
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
| |||||||||||||||||||||||||||||
| 92 | 18 | ▶ | my $eol = exists( $opts->{eol} ) ? $opts->{eol} : "\015\012"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 18 | ▶ | my $has_eol = defined( $eol ) && length( $eol ); | ||||||||||||||||||||||||||
|
Condition33%1/3
defined $eol && length $eol
MC/DC0%0/2
defined $eol && length $eol
defined $eol
length $eol
Truth table33%1/3
A && B
A= defined $eolB= length $eol
| |||||||||||||||||||||||||||||
| 94 | 18 | my( $n, $buff ); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 18 | ▶ | while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 21 | my $rv = $writer->( $encoded ); | |||||||||||||||||||||||||||
| 108 | 21 | ▶ | return( $self->pass_error ) if( !defined( $rv ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 18 | ▶ | return( $self->pass_error ) if( !defined( $n ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||