--- mimew.pl.old	Sat Feb 14 18:19:16 1998
+++ mimew.pl	Fri Apr 17 13:15:25 1998
@@ -1,7 +1,8 @@
 package MIME;
 # Copyright (C) 1993-94 Noboru Ikuta <ikuta@crc.co.jp>
+# HANKAKU-KANA support by alumina <alumina@sensyu.ne.jp>
 #
-# mimew.pl: MIME base64 encoder library Ver.2.00alpha ('94/08/27)
+# mimew.pl: MIME base64 encoder library Ver.2.00alpha-hkana ('98/04/11)
 #
 # ¥¤¥ó¥¹¥È¡¼¥ë : @INC ¤Î¥Ç¥£¥ì¥¯¥È¥ê¡ÊÄÌ¾ï¤Ï /usr/local/lib/perl¡Ë¤Ë¥³¥Ô¡¼
 #                ¤·¤Æ²¼¤µ¤¤¡£
@@ -42,6 +43,7 @@
 
 $jis_in  = "\x1b\$B"; # ESC-$-B ( or ESC-$-@ )
 $jis_out = "\x1b\(B"; # ESC-(-B ( or ESC-(-J )
+$jkana_in = "\x1b\(I";
 
 # ÇÛÉÛ¾ò·ï : Ãøºî¸¢¤ÏÊü´þ¤·¤Þ¤»¤ó¤¬¡¢ÇÛÉÛ¡¦²þÊÑ¤Ï¼«Í³¤È¤·¤Þ¤¹¡£²þÊÑ¤·¤Æ
 #            ÇÛÉÛ¤¹¤ë¾ì¹ç¤Ï¡¢¥ª¥ê¥¸¥Ê¥ë¤È°Û¤Ê¤ë¤³¤È¤òÌÀµ­¤·¡¢¥ª¥ê¥¸¥Ê¥ë
@@ -57,6 +59,22 @@
 #
 # »²¾È : RFC1521, RFC1522, RFC1468
 
+#   mime¥Ø¥Ã¥À¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤Ø¤Î¡¢È¾³Ñ¥«¥ÊÂÐ±þ (alumina)
+#
+#   Ê¸»úÎó¤ËÈ¾³Ñ¥«¥Ê¤¬´Þ¤Þ¤ì¤Æ¤¤¤ë¾ì¹ç¤Ë¤â¡¢¥¨¥ó¥³¡¼¥Ç¥£¥ó¥°¤·¤Þ¤¹¡£
+#   ´Á»ú¤ÈÈ¾³Ñ¤«¤Ê¤¬Ï¢Â³¤·¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢¤Þ¤È¤á¤ÆÃ±ÆÈ¤Î iso-2022-jp
+#   ¥¨¥ó¥³¡¼¥É¤Ë´Þ¤á¤Þ¤¹¡£
+#   ¹Ô¤¬ÅÓÃæ¤ÇÊ¬³ä¤µ¤ì¤ë¾ì¹ç¤Ç¤â´Á»ú¤Îµã¤­ÊÌ¤ì¤¬È¯À¸¤·¤Ê¤¤¤è¤¦¤Ë¤·¡¢
+#   iso-2022-jp¥¨¥ó¥³¡¼¥É¤Î½ªÎ»»þ¤Ë¤Ï¡¢É¬¤º ascii¥³¡¼¥É¤ËÌá¤·¤Æ¤¤¤Þ¤¹¡£
+#
+#   ÊÑ´¹·ë²Ì¤Ï nkf-1.62¤ÇÉü¸µ²ÄÇ½¤Ê»ö¤ò³ÎÇ§¤·¤Þ¤·¤¿¡£
+#
+#   ¤Ê¤ª¡¢ÊÑ´¹´Ø¿ô¤¬¸Æ¤Ð¤ì¤ë¤¿¤Ó¤Ë´Á»ú¥³¡¼¥É(sjis/euc)¤ÎÈ½Äê¤ò¹Ô¤Ê¤¦
+#   ¹½Â¤¤Î¤¿¤á¡¢¸íÈ½Äê¤¹¤ë¤³¤È¤¬Â¿¡¹¤¢¤ê¤Þ¤¹¡£
+#   ÆÃ¤Ë¡¢sjis¤ÇÈ¾³Ñ¥«¥¿¥«¥Ê¤À¤±¤Î¹Ô¤òÅÏ¤µ¤ì¤¿¾ì¹ç¤Ë¡¢euc¤È¸íÈ½Äê¤·¤Æ
+#   ¤·¤Þ¤¦¾ì¹ç¤¬Â¿¡¹¤¢¤ê¤Þ¤¹¤Î¤Ç¡¢´Á»ú¥³¡¼¥É¤¬¤ï¤«¤Ã¤Æ¤¤¤ë¾ì¹ç¤Ï¡¢¤¢¤é
+#   ¤«¤¸¤á jis¥³¡¼¥É¤ËÊÑ´¹¤·¤Æ¤ª¤¤¤¿Êý¤¬³Î¼Â¤Ç¤¹¡£
+
 ## MIME base64 ¥¢¥ë¥Õ¥¡¥Ù¥Ã¥È¥Æ¡¼¥Ö¥ë¡ÊRFC1521¤è¤ê¡Ë
 %mime = (
 "000000", "A",  "000001", "B",  "000010", "C",  "000011", "D",
@@ -95,12 +113,22 @@
 @zero = ( "", "00000", "0000", "000", "00", "0" );
 @pad  = ( "", "===",   "==",   "=" );
 
+## È¾³Ñ¥«¥Ê¤Î¥³¡¼¥É¤Ë¥Þ¥Ã¥Á¤¹¤ë¥Ñ¥¿¡¼¥ó
+$match_jkana = '\x1b\(I([\x21-\x5f]*)';
+$match_sjkana = '[\xa1-\xdf]+';
+$match_eukana = '(\x8e[\xa1-\xdf])+';
+
 ## ASCII, 7bit JIS, Shift-JIS µÚ¤Ó EUC ¤Î³Æ¡¹¤Ë¥Þ¥Ã¥Á¤¹¤ë¥Ñ¥¿¡¼¥ó
 $match_ascii = '\x1b\([BHJ]([\t\x20-\x7e]*)';
 $match_jis = '\x1b\$[@B](([\x21-\x7e]{2})*)';
 $match_sjis = '([\x81-\x9f\xe0-\xfc][\x40-\x7e\x80-\xfc])+';
 $match_euc  = '([\xa1-\xfe]{2})+';
 
+## ´Á»ú¤ÈÈ¾³Ñ¥«¥Ê, Î¾Êý¤Ë¥Þ¥Ã¥Á¤¹¤ë¥Ñ¥¿¡¼¥ó
+$match_alljis  = '(\x1b\$[@B]([\x21-\x7e]{2})*|\x1b\(I[\x21-\x5f]*)+';
+$match_allsjis = '([\x81-\x9f\xe0-\xfc][\x40-\x7e\x80-\xfc]|[\xa1-\xdf])+';
+$match_alleuc  = '([\xa1-\xfe]{2}|\x8e[\xa1-\xdf])+';
+
 ## MIME Part 2(charset=`ISO-2022-JP',encoding=`B') ¤Î head ¤È tail
 $mime_head = '=?ISO-2022-JP?B?';
 $mime_tail = '?=';
@@ -117,11 +145,11 @@
     s/$match_jis/$jis_in$1/go;
     s/$match_ascii/$jis_out$1/go;
     $kanji = &checkkanji;
-    s/$match_sjis/&s2j($&)/geo if ($kanji eq 'SJIS');
-    s/$match_euc/&e2j($&)/geo if ($kanji eq 'EUC');
-    s/(\x1b[\$\(][BHJ@])+/$1/g;
+    s/$match_allsjis/&s2j($&)/geo if ($kanji eq 'SJIS');
+    s/$match_alleuc/&e2j($&)/geo if ($kanji eq 'EUC');
+    s/(\x1b[\$\(][BHIJ@])+/$+/g;
     1 while s/(\x1b\$[B@][\x21-\x7e]+)\x1b\$[B@]/$1/;
-    1 while s/$match_jis/&mimeencode($&,$`,$')/eo;
+    1 while s/$match_alljis/&mimeencode($&,$`,$')/eo;
     s/$match_ascii/$1/go;
     $_;
 }
@@ -169,10 +197,28 @@
             $len = int(($limit-$blen-26)/4)*2+3;
         }
         if ($len >= 5){
-            $str = substr($_, 0, $len).$jis_out;
+            local($nextc_in) = $jis_in;
+            local($pos,$tmp);
+            $str = substr($_, 0, $len);
+            $pos = rindex($str, "\x1b");
+            if( $pos > ($len - 3) ) {
+                $nextc_in = "";
+                $len = $pos;
+                $str = substr($str, 0, $len);
+                $pos = rindex($str, "\x1b");
+            }
+            $tmp = substr( $str, $pos);
+            if ($tmp =~ /^$match_jis/o ) {
+                $len = $len - 1 unless $len & 1;
+                $str = substr($str, 0, $len);
+            } else {
+                $nextc_in = substr($tmp,0,3) if ($nextc_in ne "");
+            }
+
+            $str .= $jis_out;
             $str = &base64encode($str);
             $str = $mime_head.$str.$mime_tail;
-            $back.$str."\n ".$jis_in.substr($_, $len);
+            $back.$str."\n ".$nextc_in.substr($_, $len);
         }else{
             "\n ".$_;
         }
@@ -200,8 +246,8 @@
 ## Shift-JIS ¤È EUC ¤Î¤É¤Á¤é¤Î´Á»ú¥³¡¼¥É¤¬´Þ¤Þ¤ì¤ë¤«¤ò¥Á¥§¥Ã¥¯
 sub checkkanji {
     local($sjis,$euc);
-    $sjis += length($&) while(/$match_sjis/go);
-    $euc  += length($&) while(/$match_euc/go);
+    $sjis += length($&) while(/$match_allsjis/go);
+    $euc  += length($&) while(/$match_alleuc/go);
     return 'NONE' if ($sjis == 0 && $euc == 0);
     return 'SJIS' if ($sjis > $euc);
     return 'EUC'  if ($sjis < $euc);
@@ -211,25 +257,50 @@
 ## EUC ¤ò 7bit JIS ¤ËÊÑ´¹
 sub e2j {
     local($_) = @_;
-    tr/\xa1-\xfe/\x21-\x7e/;
-    $jis_in.$_.$jis_out;
+    local($string);
+    while( s/^($match_euc|$match_eukana)// ) {
+        local($_) = $&;
+        local($tmp);
+        if( /^$match_euc/ ) {
+            tr/\xa1-\xfe/\x21-\x7e/;
+            $tmp = $jis_in.$_;
+        } else {
+            s/\x8e//g;
+            tr/\xa1-\xfe/\x21-\x7e/;
+            $tmp = $jkana_in.$_;
+        }
+        $string .= $tmp;
+    }
+    $string.$jis_out;
 }
 
 ## Shift-JIS ¤ò 7bit JIS ¤ËÊÑ´¹
 sub s2j {
     local($string);
-    local(@ch) = split(//, $_[0]);
-    while(($j1,$j2)=unpack("CC",shift(@ch).shift(@ch))){
-        if ($j2 > 0x9e){
-            $j1 = (($j1>0x9f ? $j1-0xb1 : $j1-0x71)<<1)+2;
-            $j2 -= 0x7e;
-        }
-        else{
-            $j1 = (($j1>0x9f ? $j1-0xb1 : $j1-0x71)<<1)+1;
-            $j2 -= ($j2>0x7e ? 0x20 : 0x1f);
+    local($_) = @_;
+    while( s/^($match_sjis|$match_sjkana)// ) {
+        local($_) = $&;
+        local($tmp);
+        if( /^$match_sjis/ ) {
+            local(@ch) = split(//, $&);
+            $tmp = $jis_in;
+            while(($j1,$j2)=unpack("CC",shift(@ch).shift(@ch))){
+                if ($j2 > 0x9e){
+                    $j1 = (($j1>0x9f ? $j1-0xb1 : $j1-0x71)<<1)+2;
+                    $j2 -= 0x7e;
+                }
+                else{
+                    $j1 = (($j1>0x9f ? $j1-0xb1 : $j1-0x71)<<1)+1;
+                    $j2 -= ($j2>0x7e ? 0x20 : 0x1f);
+                }
+                $tmp .= pack("CC", $j1, $j2);
+            }
+        } else {
+            tr/\xa1-\xfe/\x21-\x7e/;
+            $tmp = $jkana_in.$_;
         }
-        $string .= pack("CC", $j1, $j2);
+        $string .= $tmp;
     }
-    $jis_in.$string.$jis_out;
+    $string.$jis_out;
 }
 1;
