| 1 | # $Id: base64.pl,v 2.5 2011/01/02 06:50:26 ehood Exp $ | ||
| 2 | # | ||
| 3 | # Library based on Perl 4 code from: | ||
| 4 | # base64.pl -- A perl package to handle MIME-style BASE64 encoding | ||
| 5 | # A. P. Barrett <barrett@ee.und.ac.za>, October 1993 | ||
| 6 | # Revision: 1.4 Date: 1994/08/11 16:08:51 | ||
| 7 | # | ||
| 8 | # Subsequent changes made by Earl Hood, earl@earlhood.com. | ||
| 9 | |||
| 10 | package base64; | ||
| 11 | |||
| 12 | my $_have_MIME_Base64; | ||
| 13 | |||
| 14 | BEGIN { | ||
| 15 | eval { require MIME::Base64; }; | ||
| 16 | $_have_MIME_Base64 = scalar($@) ? 0 : 1; | ||
| 17 | } | ||
| 18 | |||
| 19 | # Synopsis: | ||
| 20 | # require 'base64.pl'; | ||
| 21 | # | ||
| 22 | # $uuencode_string = &base64::b64touu($base64_string); | ||
| 23 | # $binary_string = &base64::b64decode($base64_string); | ||
| 24 | # $base64_string = &base64::uutob64($uuencode_string); | ||
| 25 | # $base64_string = &base64::b64encode($binary_string); | ||
| 26 | # $uuencode_string = &base64::uuencode($binary_string); | ||
| 27 | # $binary_string = &base64::uudecode($uuencode_string); | ||
| 28 | # | ||
| 29 | # uuencode and base64 input strings may contain multiple lines, | ||
| 30 | # but may not contain any headers or trailers. (For uuencode, | ||
| 31 | # remove the begin and end lines, and for base64, remove the MIME | ||
| 32 | # headers and boundaries.) | ||
| 33 | # | ||
| 34 | # uuencode and base64 output strings will be contain multiple | ||
| 35 | # lines if appropriate, but will not contain any headers or | ||
| 36 | # trailers. (For uuencode, add the "begin" line and the | ||
| 37 | # " \nend\n" afterwards, and for base64, add any MIME stuff | ||
| 38 | # afterwards.) | ||
| 39 | |||
| 40 | #################### | ||
| 41 | |||
| 42 | my $base64_alphabet = | ||
| 43 | 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | ||
| 44 | . 'abcdefghijklmnopqrstuvwxyz' | ||
| 45 | . '0123456789+/'; | ||
| 46 | my $base64_pad = '='; | ||
| 47 | |||
| 48 | my $uuencode_alphabet = | ||
| 49 | q|`!"#$%&'()*+,-./0123456789:;<=>?| . '@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_'; | ||
| 50 | my $uuencode_pad = '`'; | ||
| 51 | |||
| 52 | # Build some strings for use in tr/// commands. | ||
| 53 | # Some uuencodes use " " and some use "`", so we handle both. | ||
| 54 | # We also need to protect backslashes and other special characters. | ||
| 55 | my $tr_uuencode = " " . $uuencode_alphabet; | ||
| 56 | $tr_uuencode =~ s/(\W)/\\$1/g; | ||
| 57 | my $tr_base64 = "A" . $base64_alphabet; | ||
| 58 | $tr_base64 =~ s/(\W)/\\$1/g; | ||
| 59 | |||
| 60 | sub b64touu { | ||
| 61 | local ($_) = shift; | ||
| 62 | my ($result); | ||
| 63 | |||
| 64 | # zap bad characters and translate others to uuencode alphabet | ||
| 65 | eval qq{ | ||
| 66 | tr|$tr_base64||cd; | ||
| 67 | tr|$tr_base64|$tr_uuencode|; | ||
| 68 | }; | ||
| 69 | |||
| 70 | # break into lines of 60 encoded chars, prepending "M" for uuencode | ||
| 71 | while (s/^(.{60})//) { | ||
| 72 | $result .= 'M' . $1 . "\n"; | ||
| 73 | } | ||
| 74 | |||
| 75 | # any leftover chars go onto a shorter line | ||
| 76 | # with padding to the next multiple of 4 chars | ||
| 77 | if ($_ ne '') { | ||
| 78 | $result .= | ||
| 79 | substr($uuencode_alphabet, length($_) * 3 / 4, 1) | ||
| 80 | . $_ | ||
| 81 | . ($uuencode_pad x ((60 - length($_)) % 4)) . "\n"; | ||
| 82 | } | ||
| 83 | |||
| 84 | # return result | ||
| 85 | $result; | ||
| 86 | } | ||
| 87 | |||
| 88 | sub b64decode { | ||
| 89 | # call more efficient module if available (ehood, 2003-09-28) | ||
| 90 | if ($_have_MIME_Base64) { | ||
| 91 | return &MIME::Base64::decode_base64; | ||
| 92 | } | ||
| 93 | |||
| 94 | # substr() usage added by ehood, 1996/04/16 | ||
| 95 | |||
| 96 | local ($str) = shift; | ||
| 97 | my ($result, $tmp, $offset, $len); | ||
| 98 | |||
| 99 | # zap bad characters and translate others to uuencode alphabet | ||
| 100 | eval qq{ | ||
| 101 | \$str =~ tr|$tr_base64||cd; | ||
| 102 | \$str =~ tr|$tr_base64|$tr_uuencode|; | ||
| 103 | }; | ||
| 104 | |||
| 105 | # break into lines of 60 encoded chars, prepending "M" for uuencode, | ||
| 106 | # and then using perl's builtin uudecoder to convert to binary. | ||
| 107 | $result = ''; # init return string | ||
| 108 | $offset = 0; # init offset to 0 | ||
| 109 | $len = length($str); # store length | ||
| 110 | while ($offset + 60 <= $len) { # loop until < 60 chars left | ||
| 111 | $tmp = substr($str, $offset, 60); # grap 60 char block | ||
| 112 | $offset += 60; # increment offset | ||
| 113 | $result .= unpack('u', 'M' . $tmp); # decode block | ||
| 114 | } | ||
| 115 | # also decode any leftover chars | ||
| 116 | if ($offset < $len) { | ||
| 117 | $tmp = substr($str, $offset, $len - $offset); | ||
| 118 | $result .= | ||
| 119 | unpack('u', | ||
| 120 | substr($uuencode_alphabet, length($tmp) * 3 / 4, 1) . $tmp); | ||
| 121 | } | ||
| 122 | |||
| 123 | # return result | ||
| 124 | $result; | ||
| 125 | } | ||
| 126 | |||
| 127 | sub uutob64 { | ||
| 128 | # This is the most difficult, because some perverse uuencoder | ||
| 129 | # might have made lines that do not describe multiples of 3 bytes. | ||
| 130 | # I don't see any better method than uudecoding to binary and then | ||
| 131 | # b64encoding the binary. | ||
| 132 | |||
| 133 | &b64encode(&uudecode); # implicitly pass @_ to &uudecode | ||
| 134 | } | ||
| 135 | |||
| 136 | sub b64encode { | ||
| 137 | # call more efficient module if available (ehood, 2003-09-28) | ||
| 138 | if ($_have_MIME_Base64) { | ||
| 139 | return &MIME::Base64::encode_base64; | ||
| 140 | } | ||
| 141 | |||
| 142 | local ($_) = shift; | ||
| 143 | my ($chunk); | ||
| 144 | my ($result); | ||
| 145 | |||
| 146 | # break into chunks of 45 input chars, use perl's builtin | ||
| 147 | # uuencoder to convert each chunk to uuencode format, | ||
| 148 | # then kill the leading "M", translate to the base64 alphabet, | ||
| 149 | # and finally append a newline. | ||
| 150 | while (s/^([\s\S]{45})//) { | ||
| 151 | $chunk = substr(pack('u', $1), $[ + 1, 60); | ||
| 152 | eval qq{ | ||
| 153 | \$chunk =~ tr|$tr_uuencode|$tr_base64|; | ||
| 154 | }; | ||
| 155 | $result .= $chunk . "\n"; | ||
| 156 | } | ||
| 157 | |||
| 158 | # any leftover chars go onto a shorter line | ||
| 159 | # with uuencode padding converted to base64 padding | ||
| 160 | if ($_ ne '') { | ||
| 161 | $chunk = substr(pack('u', $_), | ||
| 162 | $[ + 1, int((length($_) + 2) / 3) * 4 - (45 - length($_)) % 3); | ||
| 163 | eval qq{ | ||
| 164 | \$chunk =~ tr|$tr_uuencode|$tr_base64|; | ||
| 165 | }; | ||
| 166 | $result .= | ||
| 167 | $chunk . ($base64_pad x ((60 - length($chunk)) % 4)) . "\n"; | ||
| 168 | } | ||
| 169 | |||
| 170 | # return result | ||
| 171 | $result; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub uuencode { | ||
| 175 | local ($_) = shift; | ||
| 176 | my ($result); | ||
| 177 | |||
| 178 | # break into chunks of 45 input chars, and use perl's builtin | ||
| 179 | # uuencoder to convert each chunk to uuencode format. | ||
| 180 | # (newline is added by builtin uuencoder.) | ||
| 181 | while (s/^([\s\S]{45})//) { | ||
| 182 | $result .= pack('u', $1); | ||
| 183 | } | ||
| 184 | |||
| 185 | # any leftover chars go onto a shorter line | ||
| 186 | # with padding to the next multiple of 4 chars | ||
| 187 | if ($_ ne '') { | ||
| 188 | $result .= pack('u', $_); | ||
| 189 | } | ||
| 190 | |||
| 191 | # return result | ||
| 192 | $result; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub uudecode { | ||
| 196 | local ($_) = shift; | ||
| 197 | my $result = ''; | ||
| 198 | |||
| 199 | # strip out begin/end lines (ehood, 1996/03/21) | ||
| 200 | s/^\s*begin[^\n]+\n//; | ||
| 201 | s/\nend\s*$//; | ||
| 202 | |||
| 203 | # use perl's builtin uudecoder to convert each line | ||
| 204 | while (s/^([^\n]+\n?)//) { | ||
| 205 | last if substr($1, 0, 1) eq '`'; | ||
| 206 | $result .= unpack('u', $1); | ||
| 207 | } | ||
| 208 | |||
| 209 | # return result | ||
| 210 | $result; | ||
| 211 | } | ||
| 212 | |||
| 213 | 1; |