いいづか
[E-Mail]
[HomePage]
1999/03/13(土) 15:15:40
解決後の参考意見。
文字列をBase64にエンコードするロジックは、たとえば
Perlで書いたらこんなかんじですね。
$encoded = encode_mime_B( $moji )とすれば$mojiを
単にBase64エンコードした結果が $encodedに入ってきて、
$encoded = encode_mime_B( $moji,'header' )とすれば$mojiが
無条件にJUNET(JIS)とみなしてメールヘッダ形式にえんこーど
します。ただし、MIMEでは「メールヘッダの数字・アルファベットは
エンコードしてはいけない」というとりきめなので、本当は
ちょっと変。
@d64 = (
"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P", # 0
"Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f", # 1
"g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v", # 2
"w","x","y","z","0","1","2","3","4","5","6","7","8","9","+","/" # 3
);
sub encode_mime_B {
if ( $_[1] eq 'header' ) {
$b_encoded = "=?ISO-2022-JP?B?";
} else {
$b_encoded = "";
}
for ( $ix = 0; $ix < length($_[0]); $ix += 3 ){
$encode_token = substr($_[0],$ix,3 );
$len = length($encode_token);
$encode_code = ord( substr($encode_token,0,1) ) * 256 * 256 ;
$encode_code += ord( substr($encode_token,1,1) ) * 256 if ($len >= 2);
$encode_code += ord( substr($encode_token,2,1) ) if ($len >= 3);
$b_encoded .= $d64[$encode_code >> 18 & 0x3f];
$b_encoded .= $d64[$encode_code >> 12 & 0x3f];
$b_encoded .= $d64[$encode_code >> 6 & 0x3f] if ($len >= 2);
$b_encoded .= $d64[$encode_code & 0x3f] if ($len >= 3);
}
$b_encoded .= "==" if ( $len == 1 );
$b_encoded .= "=" if ( $len == 2 );
if ( $_[1] eq 'header' ) { $b_encoded .= "?="; }
return ( $b_encoded );
}