summaryrefslogtreecommitdiff
path: root/lib/Mail/SendEasy/Base64.pm
diff options
context:
space:
mode:
authorAndreas Brachold <vdr07@deltab.de>2007-08-13 18:41:27 +0000
committerAndreas Brachold <vdr07@deltab.de>2007-08-13 18:41:27 +0000
commitbcbf441e09fb502cf64924ff2530fa144bdf52c5 (patch)
treef377707a2dac078db8cd0c7d7abfe69ac1006d71 /lib/Mail/SendEasy/Base64.pm
downloadxxv-bcbf441e09fb502cf64924ff2530fa144bdf52c5.tar.gz
xxv-bcbf441e09fb502cf64924ff2530fa144bdf52c5.tar.bz2
* Move files to trunk
Diffstat (limited to 'lib/Mail/SendEasy/Base64.pm')
-rw-r--r--lib/Mail/SendEasy/Base64.pm103
1 files changed, 103 insertions, 0 deletions
diff --git a/lib/Mail/SendEasy/Base64.pm b/lib/Mail/SendEasy/Base64.pm
new file mode 100644
index 0000000..995a7b1
--- /dev/null
+++ b/lib/Mail/SendEasy/Base64.pm
@@ -0,0 +1,103 @@
+#############################################################################
+## Name: Base64.pm
+## Purpose: Mail::SendEasy::Base64
+## Author: Graciliano M. P.
+## Modified by:
+## Created: 25/5/2003
+## RCS-ID:
+## Copyright: (c) 2003 Graciliano M. P.
+## Licence: This program is free software; you can redistribute it and/or
+## modify it under the same terms as Perl itself
+#############################################################################
+
+package Mail::SendEasy::Base64 ;
+
+use strict qw(vars) ;
+no warnings ;
+
+use vars qw($VERSION @ISA) ;
+our $VERSION = '1.0' ;
+
+require Exporter;
+@ISA = qw(Exporter);
+
+our @EXPORT = qw(encode_base64 decode_base64) ;
+our @EXPORT_OK = @EXPORT ;
+
+my ($BASE64_PM) ;
+eval("use MIME::Base64 ()") ;
+if ( defined &MIME::Base64::encode_base64 ) { $BASE64_PM = 1 ;}
+
+#################
+# ENCODE_BASE64 #
+#################
+
+sub encode_base64 {
+ if ( $BASE64_PM ) { return &MIME::Base64::encode_base64($_[0]) ;}
+ else { return &_encode_base64_pure_perl($_[0]) ;}
+}
+
+############################
+# _ENCODE_BASE64_PURE_PERL #
+############################
+
+sub _encode_base64_pure_perl {
+ my $res = "";
+ my $eol = $_[1];
+ $eol = "\n" unless defined $eol;
+ pos($_[0]) = 0; # ensure start at the beginning
+ while ($_[0] =~ /(.{1,45})/gs) {
+ $res .= substr(pack('u', $1), 1);
+ chop($res);
+ }
+ $res =~ tr|` -_|AA-Za-z0-9+/|; # `# help emacs
+ # fix padding at the end
+ my $padding = (3 - length($_[0]) % 3) % 3;
+ $res =~ s/.{$padding}$/'=' x $padding/e if $padding;
+ # break encoded string into lines of no more than 76 characters each
+ if (length $eol) {
+ $res =~ s/(.{1,76})/$1$eol/g;
+ }
+ $res;
+}
+
+#################
+# DECODE_BASE64 #
+#################
+
+sub decode_base64 {
+ if ( $BASE64_PM ) { return &MIME::Base64::decode_base64($_[0]) ;}
+ else { return &_decode_base64_pure_perl($_[0]) ;}
+}
+
+
+############################
+# _DECODE_BASE64_PURE_PERL #
+############################
+
+sub _decode_base64_pure_perl {
+ local($^W) = 0 ;
+ my $str = shift ;
+ my $res = "";
+
+ $str =~ tr|A-Za-z0-9+=/||cd; # remove non-base64 chars
+ if (length($str) % 4) {
+ #require Carp;
+ #Carp::carp("Length of base64 data not a multiple of 4")
+ }
+ $str =~ s/=+$//; # remove padding
+ $str =~ tr|A-Za-z0-9+/| -_|; # convert to uuencoded format
+ while ($str =~ /(.{1,60})/gs) {
+ my $len = chr(32 + length($1)*3/4); # compute length byte
+ $res .= unpack("u", $len . $1 ); # uudecode
+ }
+ $res;
+}
+
+#######
+# END #
+#######
+
+1;
+
+