/*	
 *	clipsjis.c
 *
 *	僔僼僩JIS
 *
 *	CLiP - Common Library for P/ECE
 *	Copyright (C) 2015 Naoyuki Sawa
 *
 *	* Mon Dec 21 21:11:43 JST 2015 Naoyuki Sawa
 *	- 1st 儕儕乕僗丅
 */
#include "clip.h"
/****************************************************************************
 *	
 ****************************************************************************/
//仭嶲徠帒椏
//乽KGK婭廈媄尋岺嬈乿(https://www.kishugiken.co.jp/)偺丄
//亀傛傝徻偟偄暥帤僐乕僪偺榖亁(https://www.kishugiken.co.jp/cn/code04.html)偺儁乕僕傛傝堷梡:
//劆亀僔僼僩JIS僐乕僪亁
//劆僔僼僩JIS僐乕僪偼僷僜僐儞偺擔杮岅僐乕僪偲偟偰晛媦偟偰偄傑偡丅
//劆暥帤偳偍傝JIS僐乕僪(JISX208)傪侾僶僀僩僐乕僪(JISX201)偵廳側傜側偄傛偆偵僔僼僩偟偨傕偺偱偡丅
//劆傑偢丄JIS僐乕僪偺戞堦僶僀僩傪JISX201偱巊傢傟偰偄側偄椞堟偵僔僼僩偟傑偡丅(0x80乣0x9F丄0xE0乣0xFF)
//劆偮偓偵戞擇僶僀僩傕0x40乣0xFC偵僔僼僩偟傑偡丅(0x7F偼旔偗偰僔僼僩偟傑偡丅)
//劆僔僼僩偺寁嶼偼埲壓偺傛偆偵側偭偰偄傑偡丅
//劆仩戞堦僶僀僩
//劆ASCII椞堟偲JIS僐乕僪偺戞堦僶僀僩偑廳側傞偨傔丄ASCII丄敿妏僇僫偺偄偢傟偲傕廳側傜側偄0x80乣0x9F丄0xE0乣0xEF偵戞堦僶僀僩傪堏摦偝偣傑偡丅
//劆乽JIS椞堟1乿偺僐乕僪偼乽僔僼僩JIS椞堟1乿傊丄乽JIS椞堟2乿偺僐乕僪偼乽僔僼僩JIS椞堟2乿傊丄偦傟偧傟堏摦偟傑偡丅
//劆劇劅劅劅劍劅劅劅劅劅劅劍劅劅劅劅劅劅劍劅劅劅劅劅劅劅劅劉
//劆劆嬫暘  劆JIS僐乕僪(A)劆寁嶼幃      劆僔僼僩JIS僐乕僪 劆
//劆劌劅劅劅劑劅劅劅劅劅劅劑劅劅劅劅劅劅劑劅劅劅劅劅劅劅劅劎
//劆劆慡嬫  劆0x21乣0x5E  劆(A+1)/2+0x70劆0x81乣0x9F      劆
//劆劆      劆0x5F乣0x7E  劆(A+1)/2+0xB0劆0xE0乣0xEF      劆
//劆劋劅劅劅劏劅劅劅劅劅劅劏劅劅劅劅劅劅劏劅劅劅劅劅劅劅劅劊
//劆仩戞擇僶僀僩
//劆椬傝崌偆婏悢嬫偲嬼悢嬫偑摨偠戞堦僶僀僩傪嫟桳偡傞偨傔丄戞擇僶僀僩偼94亊2亖188偺椞堟傪巊梡偟傑偡丅
//劆惂屼僐乕僪偺椞堟傪旔偗傞偨傔丄0x40乣0x7E丄0x80乣0xFC偵僔僼僩偟傑偡丅(婏悢嬫偼0x40乣0x7E,0x80乣0x9E丄嬼悢嬫偼0x9F乣0xFC)
//劆ASCII偲廳暋偡傞僐乕僪傪娷傓偨傔丄張棟偵偼拲堄偡傞昁梫偑偁傝傑偡丅
//劆劇劅劅劅劍劅劅劅劅劅劅劍劅劅劅劅劅劅劍劅劅劅劅劅劅劅劅劉
//劆劆嬫暘  劆JIS僐乕僪(B)劆寁嶼幃      劆僔僼僩JIS僐乕僪 劆
//劆劌劅劅劅劑劅劅劅劅劅劅劑劅劅劅劅劅劅劑劅劅劅劅劅劅劅劅劎
//劆劆婏悢嬫劆0x21乣0x5F  劆B+0x1F      劆0x40乣0x7E      劆
//劆劆      劆0x60乣0x7E  劆B+0x20      劆0x80乣0x9E      劆
//劆劌劅劅劅劑劅劅劅劅劅劅劑劅劅劅劅劅劅劑劅劅劅劅劅劅劅劅劎
//劆劆嬼悢嬫劆0x21乣0x7E  劆B+0x7E      劆0x9F乣0xFC      劆 
//劆劋劅劅劅劏劅劅劅劅劅劅劏劅劅劅劅劅劅劏劅劅劅劅劅劅劅劅劊
//劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅
//忋婰偺婰帠偼丄僔僼僩JIS僐乕僪偺1僶僀僩栚傪0x81乣0x9F,媦傃,0xE0乣0xEF偲偟偰偄傑偡偑丄尩枾偵偼乣0xFC傑偱桳傝摼傑偡丅
//偨偩偟丄1僶僀僩栚偑0xEF傪挻夁偡傞暥帤僐乕僪偼奜帤偲IBM奼挘暥帤偩偗偱偁傝丄幚梡忋丄忋婰偺捠傝偲尒側偟偰傕廩暘偱偡丅
//1僶僀僩栚偑0xEF傪挻夁偡傞暥帤僐乕僪偵偮偄偰偼丄壓婰偺婰帠傪嶲徠偟偰壓偝偄丅
//劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅
//仭嶲徠帒椏
//乽偲偁傞僔僗僥儉僄儞僕僯傾偺擔婰乿(http://michyo.blog.fc2.com/)偝傫偺丄
//亀Shift_JIS暥帤僐乕僪斖埻亁(http://michyo.blog.fc2.com/blog-entry-99.html)偺婰帠傛傝堷梡:
//劆亀Shift_JIS暥帤僐乕僪斖埻亁
//劆Windows偱奐敪傪峴偭偰偄傞偲Shift_JIS僐乕僪傪庢傝埖偆帠偑懡偄偱偡偑丄偙偺Shift_JIS偺暥帤僐乕僪斖埻偼埲壓偺傛偆偵側偭偰偄傑偡丅
//劆仩1僶僀僩暥帤
//劆劇劅劅劅劅劅劅劅劅劅劍劅劅劅劅劅劅劅劉
//劆劆惂屼僐乕僪        劆&h00乣&h1F    劆
//劆劆                  劆&h7F          劆
//劆劆ASCII暥帤         劆&h20乣&h7E    劆
//劆劆敿妏僇僫          劆&hA1乣&hDF    劆
//劆劋劅劅劅劅劅劅劅劅劅劏劅劅劅劅劅劅劅劊
//劆仩2僶僀僩暥帤
//劆劇劅劅劅劅劅劅劅劅劅劍劅劅劅劅劅劅劅劉
//劆劆忋埵僶僀僩        劆&h81乣&h9F    劆
//劆劆                  劆&hE0乣&hEF    劆
//劆劆壓埵僶僀僩        劆&h40乣&h7E    劆
//劆劆                  劆&h80乣&hFC    劆
//劆劋劅劅劅劅劅劅劅劅劅劏劅劅劅劅劅劅劅劊
//劆峏偵2僶僀僩暥帤偺僐乕僪斖埻撪栿偼師偺傛偆偵側偭偰偄傑偡丅
//劆仩JIS娍帤(JISX0208)
//劆劇劅劅劅劅劅劅劅劅劅劍劅劅劅劅劅劅劅劉
//劆劆旕娍帤            劆&h8140乣&h84BE劆
//劆劆戞堦悈弨          劆&h889F乣&h9872劆
//劆劆戞擇悈弨          劆&h989F乣&h9FFC劆
//劆劆                  劆&hE040乣&hEAA4劆
//劆劆奜帤              劆&hF040乣&hF9FC劆
//劆劋劅劅劅劅劅劅劅劅劅劏劅劅劅劅劅劅劅劊
//劆仩Windows婡庬埶懚暥帤
//劆劇劅劅劅劅劅劅劅劅劅劍劅劅劅劅劅劅劅劉
//劆劆NEC摿庩暥帤       劆&h8740乣&h879C劆
//劆劆NEC慖掕IBM奼挘暥帤劆&hED40乣&hEEFC劆
//劆劆IBM奼挘暥帤       劆&hFA40乣&hFC4B劆
//劆劋劅劅劅劅劅劅劅劅劅劏劅劅劅劅劅劅劅劊
//劆仩Macintosh婡庬埶懚暥帤
//劆劇劅劅劅劅劅劅劅劅劅劍劅劅劅劅劅劅劅劉
//劆劆Apple奼挘暥帤     劆&h00FD乣&h00FF劆
//劆劆                  劆&h8540乣&h886D劆
//劆劆                  劆&hEB40乣&hED96劆
//劆劋劅劅劅劅劅劅劅劅劅劏劅劅劅劅劅劅劅劊
//劆搑拞偵枹掕媊椞堟傪娷傫偱偄偨傝偼偟傑偡偑乧
//劆嵟掅尷偙偺偔傜偄傪抦偭偰偍偔偲丄Shift_JIS僐乕僪傪埖傢側偗傟偽側傜側偄帪偵妝偱偟傚偆丅
/*--------------------------------------------------------------------------*/
//JIS 2僶僀僩暥帤僐乕僪傪丄僔僼僩JIS 2僶僀僩暥帤僐乕僪偵曄姺偟傑偡丅
//搉偝傟傞暥帤僐乕僪偼惓偟偄暥帤僐乕僪偱偁傞偲壖掕偟丄僄儔乕専嵏偼峴偄傑偣傫丅
int jistosjis(int jis) {
	int lo = (unsigned char) jis;
	int hi = (unsigned char)(jis >> 8);
	//嬫暘偑婏悢嬫側傜偽乧
	if(hi & 1) {
		//JIS僐乕僪(B)偑0x21乣0x5F側傜偽乧
		if(lo <= 0x5F) {
			lo += 0x1F;	//0x40乣0x7E
		//JIS僐乕僪(B)偑0x60乣0x7E側傜偽乧
		} else {
			lo += 0x20;	//0x80乣0x9E
		}
	//嬫暘偑嬼悢嬫側傜偽乧
	} else {
		lo += 0x7E;		//0x9F乣0xFC
	}
	//JIS僐乕僪(A)偑0x21乣0x5E側傜偽乧
	if(hi <= 0x5E) {
		hi++;			//0x22乣0x5F
		hi >>= 1;		//0x11乣0x2F
		hi += 0x70;		//0x81乣0x9F
	//JIS僐乕僪(A)偑0x5F乣0x7E側傜偽乧
	} else {
		hi++;			//0x60乣0x7F
		hi >>= 1;		//0x30乣0x3F
		hi += 0xB0;		//0xE0乣0xEF
	}
	return (hi << 8) | lo;
}
/*--------------------------------------------------------------------------*/
//僔僼僩JIS 2僶僀僩暥帤僐乕僪傪丄JIS 2僶僀僩暥帤僐乕僪偵曄姺偟傑偡丅
//搉偝傟傞暥帤僐乕僪偼惓偟偄暥帤僐乕僪偱偁傞偲壖掕偟丄僄儔乕専嵏偼峴偄傑偣傫丅
int sjistojis(int sjis) {
	int lo = (unsigned char) sjis;
	int hi = (unsigned char)(sjis >> 8);
	//戞堦僶僀僩偑0x81乣0x9F側傜偽乧
	if(hi <= 0x9F) {
		hi -= 0x70;		//0x11乣0x2F
		hi <<= 1;		//0x22乣0x5E
		//嬫暘偑婏悢嬫側傜偽乧
		if(lo <= 0x9E) {
			hi--;		//0x21乣0x5D
		}
	//戞堦僶僀僩偑0xE0乣0xEF側傜偽乧
	} else {
		hi -= 0xB0;		//0x30乣0x3F
		hi <<= 1;		//0x60乣0x7E
		//嬫暘偑婏悢嬫側傜偽乧
		if(lo <= 0x9E) {
			hi--;		//0x5F乣0x7D
		}
	}
	//嬫暘偑婏悢嬫側傜偽乧
	if(lo <= 0x9E) {
		//戞擇僶僀僩偑0x40乣0x7E側傜偽乧
		if(lo <= 0x7E) {
			lo -= 0x1F;	//0x21乣0x5F
		//戞擇僶僀僩偑0x80乣0x9E側傜偽乧
		} else {
			lo -= 0x20;	//0x60乣0x7E
		}
	//嬫暘偑嬼悢嬫側傜偽乧
	} else {
		lo -= 0x7E;		//0x21乣0x7E
	}
	return (hi << 8) | lo;
}
