#!/Perl/bin/perl


#$test_data = "\x1B\x7C\xC9\xE9\xDE\xA2\xF3\xB3\x21\x3C\xEB\x21\x56\x3F\x65\x38\x4D\x32\x2B\x4C\x67\x21\x21\x42\x68\x1B\x7E\xB7\x49\x74\x21\x57\x1b\x24\x2A\x3B\x1B\x7D\xFA\xEA";
#printf STDOUT "%s\n", &eight_bit_char_decode($test_data);
#
#$test_data = "\x1b\x24\x3b\x7a\x5e\x7a\x56\x1b\x24\x39\x0e\x53\x4f\x4e\x47\x53\xfb\x0f\x1b\x24\x3b\x76\x47\x1b\x24\x39\x36\x36\x3f\x3f\x4d\x7c\x3b\x52\xfc";
#printf STDOUT "%s\n", &eight_bit_char_decode($test_data);
#
#exit;

#-------------------------------------------------------------------------------

package eight_bit_char_decode;

use Encode;
use Encode::JIS2K;

use strict;

sub eight_bit_char_decode (\$;\$) {

my %jis_kigou = (
0x21, "！",
0x22, "＂",
0x23, "＃",
0x24, "＄",
0x25, "％",
0x26, "＆",
0x27, "＇",
0x28, "（",
0x29, "）",
0x2A, "＊",
0x2B, "＋",
0x2C, "，",
0x2D, "－",
0x2E, "．",
0x2F, "／",

0x3A, "：",
0x3B, "；",
0x3C, "＜",
0x3D, "＝",
0x3E, "＞",
0x3F, "？",

0x40, "＠",

0x5B, "［",
0x5C, "＼",
0x5D, "］",
0x5E, "＾",
0x5F, "＿",

0x60, "｀",

0x7B, "｛",
0x7C, "｜",
0x7D, "｝",
0x7E, "～"
);

my %jis_katakana = (
0x77, "ヽ",
0x78, "ヾ",
0x79, "ー",
0x7A, "。",
0x7B, "「",
0x7C, "」",
0x7D, "、",
0x7E, "・"
);

my %jis_hiragana = (
0x77, "ゝ",
0x78, "ゞ",
0x79, "ー",
0x7A, "。",
0x7B, "「",
0x7C, "」",
0x7D, "、",
0x7E, "・"
);

my @arib_tuikakigou2_90 = (
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "[HV]", "[SD]", "[Ｐ]",
"[Ｗ]", "〓", "[手]", "[字]", "[双]", "[デ]", "[Ｓ]", "[二]", "[多]","[解]",
"[SS]", "[Ｂ]", "[Ｎ]", "〓", "〓", "[天]", "[交]", "[映]", "[無]", "[料]",
"[鍵]", "[前]", "[後]", "[再]", "[新]", "[初]", "[終]", "[生]", "[販]", "[声]", 
"[吹]", "[PPV]", "[秘]", "[他]" 
);

#my @arib_tuikakigou_91 = (
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓" 
#);

#my @arib_tuikakigou_92 = (
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓" 
#);

my @arib_tuikakigou2_93 = (
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
"〓", "〓", "〓", "〓", "〓", "〓", "〓", "!!", "!?", "〓",
"〓", "〓", "〓", "〓" 
);

#my @arib_tuikakigou_94 = (
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓" 
#);

#my @arib_tuikakanji_85 = (
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓"
#);
#
#my @arib_tuikakanji_86 = (
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓",
#"〓", "〓", "〓", "〓", "〓", "〓", "〓", "〓", "髙", "〓",
#"〓", "〓", "〓"
#);

my @arib_tuikakigou_85 = (
0xE080, 0xE081, 0x4EFD, 0x4EFF, 0x4F9A, 0x4FC9, 0x509C, 0x511E, 0x51BC, 0xE082,
0x5307, 0x5361, 0x536C, 0xE083, 0xE084, 0x544D, 0x5496, 0x549C, 0x54A9, 0x550E,
0x554A, 0x5672, 0x56E4, 0x5733, 0x5734, 0x585A, 0x5880, 0x59E4, 0x5A23, 0x5A55,
0x5BEC, 0xE085, 0xE086, 0x5EAC, 0x5F34, 0x5F45, 0x5FB7, 0x6017, 0xE087, 0x6130,
0x6624, 0x66C8, 0x66D9, 0x66FA, 0x66FB, 0xE088, 0xE089, 0x6911, 0x693B, 0x6A45,
0x6A91, 0x6ADB, 0xE08A, 0xE08B, 0xE08C, 0x6BF1, 0x6CE0, 0x6D2E, 0x6D77, 0x6DBF,
0x6DCA, 0x6DF8, 0x6E1A, 0x6F5E, 0x6FF9, 0x7064, 0xE08D, 0xE08E, 0x7147, 0x71C1,
0x7200, 0x739F, 0x73A8, 0x73C9, 0x73D6, 0x741B, 0x7421, 0x7422, 0x7426, 0x742A,
0x742C, 0x7439, 0x744B, 0xE08F, 0x7575, 0x7581, 0x7772, 0xE090, 0x78C8, 0x78E0,
0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EF, 0xE1F0, 0xE1F1, 0xE1F2, 0xE1F3, 0xE1F4, 0xE1F5, 0xE1F6,
0xE1F7, 0xE1F8, 0xE1F9, 0xE1FA, 0xE1FB, 0xE1FC, 0xE1FD, 0xE1FE, 0xE1FF, 0xE280,
0xE281, 0xE282, 0xE283, 0xE284
);

my @arib_tuikakigou_92 = (
0xE285, 0xE286, 0xE287, 0xE288, 0xE289, 0xE28A, 0xE28B, 0xE28C, 0xE28D, 0xE28E,
0x33A1, 0x33A5, 0x339D, 0x33A0, 0x33A4, 0xE28F, 0x2488, 0x2489, 0x248A, 0x248B,
0x248C, 0x248D, 0x248E, 0x248F, 0x2490, 0xE290, 0xE291, 0xE292, 0xE293, 0xE294,
0xE295, 0xE296, 0xE297, 0xE298, 0xE299, 0xE29A, 0xE29B, 0xE29C, 0xE29D, 0xE29E,
0xE29F, 0x3233, 0x3236, 0x3232, 0x3231, 0x3239, 0xE2A0, 0x25B6, 0x25C0, 0x3016,
0x3017, 0xE2A1, 0xE2A2, 0xE2A3, 0xE2A4, 0xE2A5, 0xE2A6, 0xE2A7, 0xE2A8, 0xE2A9,
0xE2AA, 0xE2AB, 0xE2AC, 0xE2AD, 0xE2AE, 0xE2AF, 0xE2B0, 0xE2B1, 0xE2B2, 0xE2B3,
0xE2B4, 0xE2B5, 0xE2B6, 0xE2B7, 0xE2B8, 0xE2B9, 0xE2BA, 0xE2BB, 0xE2BC, 0xE2BD,
0xE2BE, 0xE2BF, 0xE2C0, 0xE2C1, 0xE2C2, 0x00AE, 0x00A9, 0xE2C3, 0xE2C4, 0xE2C5,
0xE2C6, 0xE2C7, 0xE2C8, 0xE2C9
);

my @arib_tuikakigou_93 = (
0x322A, 0x322B, 0x322C, 0x322D, 0x322E, 0x322F, 0x3230, 0x3237, 0x337E, 0x337D,
0x337C, 0x337B, 0xE2CA, 0xE2CB, 0x3036, 0xE2CC, 0xE2CD, 0xE2CE, 0xE2CF, 0xE2D0,
0xE2D1, 0xE2D2, 0xE2D3, 0xE2D4, 0xE2D5, 0xE2D6, 0xE2D7, 0xE2D8, 0xE2D9, 0xE2DA,
0xE2DB, 0xE2DC, 0xE2DD, 0xE2DE, 0xE2DF, 0xE2E0, 0xE2E1, 0xE2E2, 0x2113, 0x338F,
0x3390, 0x33CA, 0x339E, 0x33A2, 0x3371, 0xE2E3, 0xE2E4, 0x00BD, 0xE2E5, 0x2153,
0x2154, 0x00BC, 0x00BE, 0x2155, 0x2156, 0x2157, 0x2158, 0x2159, 0x215A, 0xE2E6,
0x215B, 0xE2E7, 0xE2E8, 0x2600, 0x2601, 0x2602, 0xE2E9, 0xE2EA, 0xE2EB, 0xE2EC,
0xE2ED, 0x2666, 0x2665, 0x2663, 0x2660, 0xE2EE, 0xE2EF, 0x203C, 0xE2F0, 0xE2F1,
0xE2F2, 0xE2F3, 0xE2F4, 0xE2F5, 0xE2F6, 0xE2F7, 0xE2F8, 0xE2F9, 0xE2FA, 0x266C,
0xE2FB, 0xE2FC, 0xE2FD, 0xE2FE
);

my @arib_tuikakigou_94 = (
0x2160, 0x2161, 0x2162, 0x2163, 0x2164, 0x2165, 0x2166, 0x2167, 0x2168, 0x2169,
0x216A, 0x216B, 0x2470, 0x2471, 0x2472, 0x2473, 0x2474, 0x2475, 0x2476, 0x2477,
0x2478, 0x2479, 0x247A, 0x247B, 0x247C, 0x247D, 0x247E, 0x247F, 0xE2FF, 0xE380,
0xE381, 0xE382, 0xE383, 0xE384, 0xE385, 0xE386, 0xE387, 0xE388, 0xE389, 0xE38A,
0xE38B, 0xE38C, 0xE38D, 0xE38E, 0xE38F, 0xE390, 0xE391, 0xE392, 0xE393, 0xE394,
0xE395, 0xE396, 0xE397, 0xE398, 0xE399, 0xE39A, 0xE39B, 0xE39C, 0xE39D, 0xE39E,
0xE39F, 0xE3A0, 0xE3A1, 0xE3A2, 0x2460, 0x2461, 0x2462, 0x2463, 0x2464, 0x2465,
0x2466, 0x2467, 0x2468, 0x2469, 0x246A, 0x246B, 0x246C, 0x246D, 0x246E, 0x246F,
0x2776, 0x2777, 0x2778, 0x2779, 0x277A, 0x277B, 0x277C, 0x277D, 0x277E, 0x277F,
0xE3A3, 0xE3A4, 0xE3A5, 0xE3A6
);

	my $ON  = 1;
	my $OFF = 0;

	my $in_data = $_[0];

	# yobidasi_table
	my $G0_GL   = "\x0f";		# LS0
	my $G1_GL   = "\x0e";		# LS1
	my $G2_GL   = "\x1b\x6e";	# LS2
	my $G3_GL   = "\x1b\x6f";	# LS3

	my $G1_GR   = "\x1b\x7e";	# LS1R
	my $G2_GR   = "\x1b\x7d";	# LS2R
	my $G3_GR   = "\x1b\x7c";	# LS3R

	my $G2_GL_S = "\x19";		# SS2
	my $G3_GL_S = "\x1d";		# SS3
	
	# siji_table
	my $ONE_BYTE_GSET_G0 = "\x1b\x28"; 
	my $ONE_BYTE_GSET_G1 = "\x1b\x29";
	my $ONE_BYTE_GSET_G2 = "\x1b\x2a";
	my $ONE_BYTE_GSET_G3 = "\x1b\x2b";

	my $TWO_BYTE_GSET_G0 = "\x1b\x24";
	my $TWO_BYTE_GSET_G1 = "\x1b\x24\x29";
	my $TWO_BYTE_GSET_G2 = "\x1b\x24\x2a";
	my $TWO_BYTE_GSET_G3 = "\x1b\x24\x2b";

	my $ONE_BYTE_DRCS_G0 = "\x1b\x28\x20"; 
	my $ONE_BYTE_DRCS_G1 = "\x1b\x29\x20";
	my $ONE_BYTE_DRCS_G2 = "\x1b\x2a\x20";
	my $ONE_BYTE_DRCS_G3 = "\x1b\x2b\x20";

	my $TWO_BYTE_DRCS_G0 = "\x1b\x24\x28\x20";
	my $TWO_BYTE_DRCS_G1 = "\x1b\x24\x29\x20";
	my $TWO_BYTE_DRCS_G2 = "\x1b\x24\x2a\x20";
	my $TWO_BYTE_DRCS_G3 = "\x1b\x24\x2b\x20";

	# fugou_table
	my $CODE_CONTROL_CODE     = "\x00";

	my $CODE_KANJI            = "\x42";
	my $CODE_EISUU            = "\x4a";
	my $CODE_HIRAGANA         = "\x30";
	my $CODE_KATAKANA         = "\x31";
	my $CODE_MOSAIC_A         = "\x32";
	my $CODE_MOSAIC_B         = "\x33";
	my $CODE_MOSAIC_C         = "\x34";
	my $CODE_MOSAIC_D         = "\x35";
	my $CODE_P_EISUU          = "\x36";
	my $CODE_P_HIRAGANA       = "\x37";
	my $CODE_P_KATAKANA       = "\x38";
	my $CODE_X0201_KATAKANA   = "\x49";
	my $CODE_JIS_GOKAN_KANJI1 = "\x39";
	my $CODE_JIS_GOKAN_KANJI2 = "\x3a";
	my $CODE_TUIKAKIGOU       = "\x3b";

	my $CODE_NOT_IMPLEMENT    = "\xff";

	#init_table
	my $siji_g0 = $CODE_KANJI;
	my $siji_g1 = $CODE_EISUU;
	my $siji_g2 = $CODE_HIRAGANA;
	my $siji_g3 = $CODE_KATAKANA;

	my $yobidasi_gl = $G0_GL;
	my $yobidasi_gr = $G2_GR;

	my $yobidasi_g2_gl_single = $OFF;
	my $yobidasi_g3_gl_single = $OFF;


	my $code_count = 0;
	my $code_length = length($in_data);
	my $out_data = "";
	my $out_decode;
	if (@_ == 1) { $out_decode = 'CP932'; } else { $out_decode = $_[1];}
	while($code_count < $code_length) {

		my $conv_code;
		my $string;

		# YOBIDASI
		if      (substr($in_data, $code_count, 1) eq $G0_GL) {

			$code_count += 1;
			$yobidasi_gl = $G0_GL;
	      		$yobidasi_g2_gl_single = $OFF;
	      		$yobidasi_g3_gl_single = $OFF;
			next;

		} elsif (substr($in_data, $code_count, 1) eq $G1_GL) {

			$code_count += 1;
			$yobidasi_gl = $G1_GL;
	      		$yobidasi_g2_gl_single = $OFF;
	      		$yobidasi_g3_gl_single = $OFF;
			next;

		} elsif (substr($in_data, $code_count, 2) eq $G2_GL) {

			$code_count += 2;
			$yobidasi_gl = $G2_GL;
	      		$yobidasi_g2_gl_single = $OFF;
	      		$yobidasi_g3_gl_single = $OFF;
			next;

		} elsif (substr($in_data, $code_count, 2) eq $G3_GL) {

			$code_count += 2;
			$yobidasi_gl = $G3_GL;
	      		$yobidasi_g2_gl_single = $OFF;
	      		$yobidasi_g3_gl_single = $OFF;
			next;

		} elsif (substr($in_data, $code_count, 2) eq $G1_GR) {

			$code_count += 2;
			$yobidasi_gr = $G1_GR;
			next;

		} elsif (substr($in_data, $code_count, 2) eq $G2_GR) {

			$code_count += 2;
			$yobidasi_gr = $G2_GR;
			next;

		} elsif (substr($in_data, $code_count, 2) eq $G3_GR) {

			$code_count += 2;
			$yobidasi_gr = $G3_GR;
			next;

		} elsif (substr($in_data, $code_count, 1) eq $G2_GL_S) {

			$code_count += 1;
	      		$yobidasi_g2_gl_single = $ON;
			next;

		} elsif (substr($in_data, $code_count, 1) eq $G3_GL_S) {

			$code_count += 1;
	      		$yobidasi_g3_gl_single = $ON;
			next;

		#SIJI DRCS
		} elsif (substr($in_data, $code_count, 3) eq $ONE_BYTE_DRCS_G0) {

			$code_count += 4;
			$siji_g0 = $CODE_NOT_IMPLEMENT;
			next;

		} elsif (substr($in_data, $code_count, 3) eq $ONE_BYTE_DRCS_G1) {

			$code_count += 4;
			$siji_g1 = $CODE_NOT_IMPLEMENT;
			next;

		} elsif (substr($in_data, $code_count, 3) eq $ONE_BYTE_DRCS_G2) {

			$code_count += 4;
			$siji_g2 = $CODE_NOT_IMPLEMENT;
			next;

		} elsif (substr($in_data, $code_count, 3) eq $ONE_BYTE_DRCS_G3) {

			$code_count += 4;
			$siji_g3 = $CODE_NOT_IMPLEMENT;
			next;

		} elsif (substr($in_data, $code_count, 4) eq $TWO_BYTE_DRCS_G0) {

			$code_count += 5;
			$siji_g0 = $CODE_NOT_IMPLEMENT;
			next;

		} elsif (substr($in_data, $code_count, 4) eq $TWO_BYTE_DRCS_G1) {

			$code_count += 5;
			$siji_g1 = $CODE_NOT_IMPLEMENT;
			next;

		} elsif (substr($in_data, $code_count, 4) eq $TWO_BYTE_DRCS_G2) {

			$code_count += 5;
			$siji_g2 = $CODE_NOT_IMPLEMENT;
			next;

		} elsif (substr($in_data, $code_count, 4) eq $TWO_BYTE_DRCS_G3) {

			$code_count += 5;
			$siji_g3 = $CODE_NOT_IMPLEMENT;
			next;

		#SIJI GSET
		} elsif (substr($in_data, $code_count, 2) eq $ONE_BYTE_GSET_G0) {

			$siji_g0 = substr($in_data, $code_count + 2, 1);
			$code_count += 3;
			next;

		} elsif (substr($in_data, $code_count, 2) eq $ONE_BYTE_GSET_G1) {

			$siji_g1 = substr($in_data, $code_count + 2, 1);
			$code_count += 3;
			next;

		} elsif (substr($in_data, $code_count, 2) eq $ONE_BYTE_GSET_G2) {

			$siji_g2 = substr($in_data, $code_count + 2, 1);
			$code_count += 3;
			next;

		} elsif (substr($in_data, $code_count, 2) eq $ONE_BYTE_GSET_G3) {

			$siji_g3 = substr($in_data, $code_count + 2, 1);
			$code_count += 3;
			next;

#		} elsif (substr($in_data, $code_count, 2) eq $TWO_BYTE_GSET_G0) {
#			
#			$siji_g0 = substr($in_data, $code_count + 2, 1);
#			$code_count += 3;
#			next;
#
		} elsif (substr($in_data, $code_count, 3) eq $TWO_BYTE_GSET_G1) {

			$siji_g1 = substr($in_data, $code_count + 3, 1);
			$code_count += 4;
			next;

		} elsif (substr($in_data, $code_count, 3) eq $TWO_BYTE_GSET_G2) {

			$siji_g2 = substr($in_data, $code_count + 3, 1);
			$code_count += 4;
			next;

		} elsif (substr($in_data, $code_count, 3) eq $TWO_BYTE_GSET_G3) {

			$siji_g3 = substr($in_data, $code_count + 3, 1);
			$code_count += 4;
			next;

		} elsif (substr($in_data, $code_count, 2) eq $TWO_BYTE_GSET_G0) {

			$siji_g0 = substr($in_data, $code_count + 2, 1);
			$code_count += 3;
			next;
		}

		#
		# data convert start
		#

		# single shift
		my $save_yobidasi_gl;
		my $save_yobidasi_gr;
	      	if      ($yobidasi_g2_gl_single == $ON) {
			$save_yobidasi_gl = $yobidasi_gl;
			$yobidasi_gl = $G2_GL;

		} elsif ($yobidasi_g3_gl_single == $ON) {
			$save_yobidasi_gl = $yobidasi_gl;
			$yobidasi_gl = $G3_GL;
		}


		# GL or GR or control code?
		if      (hex(unpack("H*", substr($in_data, $code_count, 1))) >= 0x80) {

			# GR
			if    ($yobidasi_gr eq $G1_GR) { $conv_code = $siji_g1; }
			elsif ($yobidasi_gr eq $G2_GR) { $conv_code = $siji_g2; }
			elsif ($yobidasi_gr eq $G3_GR) { $conv_code = $siji_g3; }

		} elsif (hex(unpack("H*", substr($in_data, $code_count, 1))) >= 0x20) {

			# GL
			if    ($yobidasi_gl eq $G0_GL) { $conv_code = $siji_g0; }
			elsif ($yobidasi_gl eq $G1_GL) { $conv_code = $siji_g1; }
			elsif ($yobidasi_gl eq $G2_GL) { $conv_code = $siji_g2; }
			elsif ($yobidasi_gl eq $G3_GL) { $conv_code = $siji_g3; }
		} else {

			# control code 
			$conv_code = $CODE_CONTROL_CODE;
		}


		my $conv_char;
		if      ($conv_code eq $CODE_KANJI)            {

			$conv_char = (hex(unpack("H*", substr($in_data, $code_count, 2))) & 0x7F7F);
			$conv_char = pack("CC", $conv_char >> 8, $conv_char & 0x00ff);
			$string = Encode::decode('jis0208-raw', $conv_char);
			if      ($string eq chr(0x301c)) { #"～"
				$string = chr(0xff5e);
			} elsif ($string eq chr(0x2016)) { #"∥"
				$string = chr(0x2225);
			} elsif ($string eq chr(0x2212)) { #"－"
				$string = chr(0xff0d);
			}

			$code_count += 2;

		} elsif (($conv_code eq $CODE_EISUU) || ($conv_code eq $CODE_P_EISUU)) {

			$conv_char = hex(unpack("H*", substr($in_data, $code_count, 1))) & 0x7F;
			if (((0x30 <= $conv_char) && ($conv_char <= 0x39)) ||
		            ((0x41 <= $conv_char) && ($conv_char <= 0x5a)) ||
		            ((0x61 <= $conv_char) && ($conv_char <= 0x7a))) {

				$conv_char = 0x2300 + $conv_char;
				$conv_char = pack("CC", $conv_char >> 8, $conv_char & 0x00ff);
				$string = Encode::decode('jis0208-raw', $conv_char);
			} else {
				$string = Encode::decode('utf8', $jis_kigou{$conv_char});
			}
			$code_count += 1;

		} elsif (($conv_code eq $CODE_HIRAGANA) || ($conv_code eq $CODE_P_HIRAGANA)) {

			$conv_char = hex(unpack("H*", substr($in_data, $code_count, 1))) & 0x7F;
			if ((0x21 <= $conv_char) && ($conv_char <= 0x76)) {
				$conv_char = 0x2400 + $conv_char;
				$conv_char = pack("CC", $conv_char >> 8, $conv_char & 0x00ff);
				$string = Encode::decode('jis0208-raw', $conv_char);
			} else {
				$string = Encode::decode('utf8', $jis_hiragana{$conv_char});
			}
			$code_count += 1;

		} elsif (($conv_code eq $CODE_KATAKANA) || ($conv_code eq $CODE_P_KATAKANA))         {

			$conv_char = hex(unpack("H*", substr($in_data, $code_count, 1))) & 0x7F;
			if ((0x21 <= $conv_char) && ($conv_char <= 0x76)) {
				$conv_char = 0x2500 + $conv_char;
				$conv_char = pack("CC", $conv_char >> 8, $conv_char & 0x00ff);
				$string = Encode::decode('jis0208-raw', $conv_char);
			} else {
				$string = Encode::decode('utf8', $jis_katakana{$conv_char});
			}

			$code_count += 1;

		} elsif ($conv_code eq $CODE_MOSAIC_A)         {
			$code_count += 1;
		} elsif ($conv_code eq $CODE_MOSAIC_B)         {
			$code_count += 1;
		} elsif ($conv_code eq $CODE_MOSAIC_C)         {
			$code_count += 1;
		} elsif ($conv_code eq $CODE_MOSAIC_D)         {
			$code_count += 1;
#		} elsif ($conv_code eq $CODE_P_EISUU)          {
#			$code_count += 1;
#		} elsif ($conv_code eq $CODE_P_HIRAGANA)       {
#			$code_count += 1;
#		} elsif ($conv_code eq $CODE_P_KATAKANA)       {
#			$code_count += 1;
		} elsif ($conv_code eq $CODE_X0201_KATAKANA)   {

			$conv_char = hex(unpack("H*", substr($in_data, $code_count, 1))) & 0x7F;
			if      ((0x21 <= $conv_char) && ($conv_char <= 0x3f)) {
				$string = chr(0xff60 + $conv_char - 0x20);
			} elsif ((0x40 <= $conv_char) && ($conv_char <= 0x5f)) {
				$string = chr(0xff80 + $conv_char - 0x40);
			}
			$code_count += 1;

		} elsif ($conv_code eq $CODE_JIS_GOKAN_KANJI1) {
			$conv_char = (hex(unpack("H*", substr($in_data, $code_count, 2))) & 0x7F7F);
			$conv_char = pack("CC", $conv_char >> 8, $conv_char & 0x00ff);
			$string = Encode::JIS2K::decode('jis0213-1-raw', $conv_char);
			$code_count += 2;

		} elsif ($conv_code eq $CODE_JIS_GOKAN_KANJI2) {
			$conv_char = (hex(unpack("H*", substr($in_data, $code_count, 2))) & 0x7F7F);
			$conv_char = pack("CC", $conv_char >> 8, $conv_char & 0x00ff);
			$string = Encode::JIS2K::decode('jis0213-2-raw', $conv_char);
			$code_count += 2;

		} elsif ($conv_code eq $CODE_TUIKAKIGOU)       {

			$conv_char = hex(unpack("H*", substr($in_data, $code_count, 2))) & 0x7F7F;
			my $conv_char_hi  = ($conv_char >> 8)     - 0x20;
			my $conv_char_low = ($conv_char & 0x00ff) - 0x20;


			if      (($conv_char_hi == 90) && (48 <= $conv_char_low) && ($conv_char_low <= 84)) {

				$string = Encode::decode('utf8', $arib_tuikakigou2_90[$conv_char_low - 1]);

			} elsif (($conv_char_hi == 93) && (78 <= $conv_char_low) && ($conv_char_low <= 79)) {

				$string = Encode::decode('utf8', $arib_tuikakigou2_93[$conv_char_low - 1]);

			} elsif  ($conv_char_hi == 85) {

				$string = chr($arib_tuikakigou_85[$conv_char_low - 1]);

			} elsif  ($conv_char_hi == 86) {

				$string = chr($arib_tuikakigou_86[$conv_char_low - 1]);

			} elsif  ($conv_char_hi == 90) {

				$string = chr($arib_tuikakigou_90[$conv_char_low - 1]);

			} elsif  ($conv_char_hi == 91) {

				$string = chr($arib_tuikakigou_91[$conv_char_low - 1]);

			} elsif  ($conv_char_hi == 92) {

				$string = chr($arib_tuikakigou_92[$conv_char_low - 1]);

			} elsif  ($conv_char_hi == 93) {

				$string = chr($arib_tuikakigou_93[$conv_char_low - 1]);

			} elsif  ($conv_char_hi == 94) {

				$string = chr($arib_tuikakigou_94[$conv_char_low - 1]);
			}

			$code_count += 2;

		} elsif ($conv_code eq $CODE_CONTROL_CODE)    {

			$string = substr($in_data, $code_count, 1);
			$code_count += 1;

		} elsif ($conv_code eq $CODE_NOT_IMPLEMENT)    {

			printf STDOUT "CODE_NOT_IMPLEMENT\n";
			exit;
		}

		$out_data .= $string;

		# single shift
	      	if      ($yobidasi_g2_gl_single == $ON) {
			$yobidasi_gl = $save_yobidasi_gl;
	      		$yobidasi_g2_gl_single = $OFF;

		} elsif ($yobidasi_g3_gl_single == $ON) {
			$yobidasi_gl = $save_yobidasi_gl;
	      		$yobidasi_g3_gl_single = $OFF;
		}

	}

	return(Encode::encode($out_decode, $out_data));

}
return 1;
