| 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 | { | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | warnings::register_categories( 'Mail::Make' ); | ||
| 19 | use Mail::Make::Stream; | ||
| 20 | use parent -norequire, qw( Mail::Make::Stream::Generic ); | ||
| 21 | use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $Base64Error ); | ||
| 22 | use Exporter qw( import ); | ||
| 23 | use Mail::Make::Exception; | ||
| 24 | use MIME::Base64 (); | ||
| 25 | use constant | ||
| 26 | { | ||
| 27 | # Input chunk must be a multiple of 3 so base64 output lines align cleanly | ||
| 28 | ENCODE_BUFFER_SIZE => 300, | ||
| 29 | DECODE_BUFFER_SIZE => ( 32 * 1024 ), | ||
| 30 | }; | ||
| 31 | our @EXPORT_OK = qw( decode_b64 encode_b64 ); | ||
| 32 | our $EXCEPTION_CLASS = 'Mail::Make::Exception'; | ||
| 33 | our $VERSION = 'v0.3.0'; | ||
| 34 | }; | ||
| 35 | |||
| 36 | use strict; | ||
| 37 | use warnings; | ||
| 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 | { | ||
| 44 | my $self = shift( @_ ); | ||
| 45 | my $from = shift( @_ ); | ||
| 46 | my $to = shift( @_ ); | ||
| 47 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 48 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | ||
| 49 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | ||
| 50 | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||
| 51 | my( $n, $buff ); | ||
| 52 | |||
| 53 | while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) ) | ||
| 54 | { | ||
| 55 | my $decoded = MIME::Base64::decode_base64( $buff ); | ||
| 56 | my $rv = $writer->( $decoded ); | ||
| 57 | return( $self->pass_error ) if( !defined( $rv ) ); | ||
| 58 | } | ||
| 59 | return( $self->pass_error ) if( !defined( $n ) ); | ||
| 60 | return( $self ); | ||
| 61 | } | ||
| 62 | |||
| 63 | sub decode_b64 | ||
| 64 | { | ||
| 65 | my $s = __PACKAGE__->new; | ||
| 66 | my $rv = $s->decode( @_ ); | ||
| 67 | if( !defined( $rv ) ) | ||
| 68 | { | ||
| 69 | $Base64Error = $s->error; | ||
| 70 | return( $s->pass_error ); | ||
| 71 | } | ||
| 72 | else | ||
| 73 | { | ||
| 74 | undef( $Base64Error ); | ||
| 75 | 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 | { | ||
| 85 | my $self = shift( @_ ); | ||
| 86 | my $from = shift( @_ ); | ||
| 87 | my $to = shift( @_ ); | ||
| 88 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 89 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | ||
| 90 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | ||
| 91 | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||
| 92 | my $eol = exists( $opts->{eol} ) ? $opts->{eol} : "\015\012"; | ||
| 93 | my $has_eol = defined( $eol ) && length( $eol ); | ||
| 94 | my( $n, $buff ); | ||
| 95 | |||
| 96 | while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) ) | ||
| 97 | { | ||
| 98 | # MIME::Base64::encode_base64 appends its own newline - strip it, then | ||
| 99 | # insert our configured eol every 76 characters. | ||
| 100 | my $encoded = MIME::Base64::encode_base64( $buff, '' ); | ||
| 101 | if( $has_eol ) | ||
| 102 | { | ||
| 103 | $encoded =~ s/(.{76})/$1$eol/g; | ||
| 104 | # Ensure a trailing eol if the final chunk did not land on a 76-char boundary | ||
| 105 | $encoded .= $eol unless( $encoded =~ /\015\012$/ ); | ||
| 106 | } | ||
| 107 | my $rv = $writer->( $encoded ); | ||
| 108 | return( $self->pass_error ) if( !defined( $rv ) ); | ||
| 109 | } | ||
| 110 | return( $self->pass_error ) if( !defined( $n ) ); | ||
| 111 | return( $self ); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub encode_b64 | ||
| 115 | { | ||
| 116 | my $s = __PACKAGE__->new; | ||
| 117 | my $rv = $s->encode( @_ ); | ||
| 118 | if( !defined( $rv ) ) | ||
| 119 | { | ||
| 120 | $Base64Error = $s->error; | ||
| 121 | return( $s->pass_error ); | ||
| 122 | } | ||
| 123 | else | ||
| 124 | { | ||
| 125 | undef( $Base64Error ); | ||
| 126 | return( $rv ); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | sub is_decoder_installed | ||
| 131 | { | ||
| 132 | local $@; | ||
| 133 | eval | ||
| 134 | { | ||
| 135 | local $SIG{__DIE__} = sub{}; | ||
| 136 | require MIME::Base64; | ||
| 137 | }; | ||
| 138 | return( $@ ? 0 : 1 ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub is_encoder_installed | ||
| 142 | { | ||
| 143 | local $@; | ||
| 144 | eval | ||
| 145 | { | ||
| 146 | local $SIG{__DIE__} = sub{}; | ||
| 147 | require MIME::Base64; | ||
| 148 | }; | ||
| 149 | return( $@ ? 0 : 1 ); | ||
| 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 | 1; | ||
| 161 | # NOTE: POD | ||
| 162 | __END__ |