#include "jisx0213.h"
#define ESC 0x1b
#define STATE_ASCII 0
#define STATE_JISX0201ROMAN 1
#define STATE_JISX0201KATAKANA 2
#define STATE_JISX0208 3
#define STATE_JISX02131 4
#define STATE_JISX02132 5
static int
iso2022_jp3_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
{
ucs4_t last_wc = conv->istate >> 3;
if (last_wc) {
conv->istate &= 7;
*pwc = last_wc;
return 0;
} else {
state_t state = conv->istate;
int count = 0;
unsigned char c;
for (;;) {
c = *s;
if (c == ESC) {
if (n < count+3)
goto none;
if (s[1] == '(') {
if (s[2] == 'B') {
state = STATE_ASCII;
s += 3; count += 3;
if (n < count+1)
goto none;
continue;
}
if (s[2] == 'J') {
state = STATE_JISX0201ROMAN;
s += 3; count += 3;
if (n < count+1)
goto none;
continue;
}
if (s[2] == 'I') {
state = STATE_JISX0201KATAKANA;
s += 3; count += 3;
if (n < count+1)
goto none;
continue;
}
return RET_ILSEQ;
}
if (s[1] == '$') {
if (s[2] == '@' || s[2] == 'B') {
state = STATE_JISX0208;
s += 3; count += 3;
if (n < count+1)
goto none;
continue;
}
if (s[2] == '(') {
if (n < count+4)
goto none;
if (s[3] == 'O') {
state = STATE_JISX02131;
s += 4; count += 4;
if (n < count+1)
goto none;
continue;
}
if (s[3] == 'P') {
state = STATE_JISX02132;
s += 4; count += 4;
if (n < count+1)
goto none;
continue;
}
}
return RET_ILSEQ;
}
return RET_ILSEQ;
}
break;
}
switch (state) {
case STATE_ASCII:
if (c < 0x80) {
int ret = ascii_mbtowc(conv,pwc,s,1);
if (ret == RET_ILSEQ)
return RET_ILSEQ;
if (ret != 1) abort();
conv->istate = state;
return count+1;
} else
return RET_ILSEQ;
case STATE_JISX0201ROMAN:
if (c < 0x80) {
int ret = jisx0201_mbtowc(conv,pwc,s,1);
if (ret == RET_ILSEQ)
return RET_ILSEQ;
if (ret != 1) abort();
conv->istate = state;
return count+1;
} else
return RET_ILSEQ;
case STATE_JISX0201KATAKANA:
if (c < 0x80) {
unsigned char buf = c+0x80;
int ret = jisx0201_mbtowc(conv,pwc,&buf,1);
if (ret == RET_ILSEQ)
return RET_ILSEQ;
if (ret != 1) abort();
conv->istate = state;
return count+1;
} else
return RET_ILSEQ;
case STATE_JISX0208:
if (n < count+2)
goto none;
if (s[0] < 0x80 && s[1] < 0x80) {
int ret = jisx0208_mbtowc(conv,pwc,s,2);
if (ret == RET_ILSEQ)
return RET_ILSEQ;
if (ret != 2) abort();
conv->istate = state;
return count+2;
} else
return RET_ILSEQ;
case STATE_JISX02131:
case STATE_JISX02132:
if (n < count+2)
goto none;
if (s[0] < 0x80 && s[1] < 0x80) {
ucs4_t wc = jisx0213_to_ucs4(((state-STATE_JISX02131+1)<<8)+s[0],s[1]);
if (wc) {
if (wc < 0x80) {
ucs4_t wc1 = jisx0213_to_ucs_combining[wc - 1][0];
ucs4_t wc2 = jisx0213_to_ucs_combining[wc - 1][1];
*pwc = wc1;
conv->istate = (wc2 << 3) | state;
} else {
*pwc = wc;
conv->istate = state;
}
return count+2;
}
}
return RET_ILSEQ;
default: abort();
}
none:
conv->istate = state;
return RET_TOOFEW(count);
}
}
static int
iso2022_jp3_flushwc (conv_t conv, ucs4_t *pwc)
{
ucs4_t last_wc = conv->istate >> 3;
if (last_wc) {
conv->istate &= 7;
*pwc = last_wc;
return 1;
} else
return 0;
}
static const struct { unsigned short base; unsigned short composed; } iso2022_jp3_comp_table_data[] = {
#define iso2022_jp3_comp_table02e5_idx 0
#define iso2022_jp3_comp_table02e5_len 1
{ 0x2b64, 0x2b65 },
#define iso2022_jp3_comp_table02e9_idx (iso2022_jp3_comp_table02e5_idx+iso2022_jp3_comp_table02e5_len)
#define iso2022_jp3_comp_table02e9_len 1
{ 0x2b60, 0x2b66 },
#define iso2022_jp3_comp_table0300_idx (iso2022_jp3_comp_table02e9_idx+iso2022_jp3_comp_table02e9_len)
#define iso2022_jp3_comp_table0300_len 5
{ 0x295c, 0x2b44 },
{ 0x2b38, 0x2b48 },
{ 0x2b37, 0x2b4a },
{ 0x2b30, 0x2b4c },
{ 0x2b43, 0x2b4e },
#define iso2022_jp3_comp_table0301_idx (iso2022_jp3_comp_table0300_idx+iso2022_jp3_comp_table0300_len)
#define iso2022_jp3_comp_table0301_len 4
{ 0x2b38, 0x2b49 },
{ 0x2b37, 0x2b4b },
{ 0x2b30, 0x2b4d },
{ 0x2b43, 0x2b4f },
#define iso2022_jp3_comp_table309a_idx (iso2022_jp3_comp_table0301_idx+iso2022_jp3_comp_table0301_len)
#define iso2022_jp3_comp_table309a_len 14
{ 0x242b, 0x2477 },
{ 0x242d, 0x2478 },
{ 0x242f, 0x2479 },
{ 0x2431, 0x247a },
{ 0x2433, 0x247b },
{ 0x252b, 0x2577 },
{ 0x252d, 0x2578 },
{ 0x252f, 0x2579 },
{ 0x2531, 0x257a },
{ 0x2533, 0x257b },
{ 0x253b, 0x257c },
{ 0x2544, 0x257d },
{ 0x2548, 0x257e },
{ 0x2675, 0x2678 },
};
#define SPLIT_STATE \
unsigned short lasttwo = state >> 3; state_t prevstate = state >> 19; state &= 7
#define COMBINE_STATE \
state |= (prevstate << 19) | (lasttwo << 3)
#define COMBINE_STATE_NO_LASTTWO \
static int
iso2022_jp3_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
{
int count = 0;
unsigned char buf[2];
unsigned short jch;
int ret;
state_t state = conv->ostate;
SPLIT_STATE;
if (lasttwo) {
unsigned int idx;
unsigned int len;
if (wc == 0x02e5)
idx = iso2022_jp3_comp_table02e5_idx,
len = iso2022_jp3_comp_table02e5_len;
else if (wc == 0x02e9)
idx = iso2022_jp3_comp_table02e9_idx,
len = iso2022_jp3_comp_table02e9_len;
else if (wc == 0x0300)
idx = iso2022_jp3_comp_table0300_idx,
len = iso2022_jp3_comp_table0300_len;
else if (wc == 0x0301)
idx = iso2022_jp3_comp_table0301_idx,
len = iso2022_jp3_comp_table0301_len;
else if (wc == 0x309a)
idx = iso2022_jp3_comp_table309a_idx,
len = iso2022_jp3_comp_table309a_len;
else
goto not_combining;
do
if (iso2022_jp3_comp_table_data[idx].base == lasttwo)
break;
while (++idx, --len > 0);
if (len > 0) {
count = (state != STATE_JISX02131 ? 4 : 0) + 2;
if (n < count)
return RET_TOOSMALL;
if (state != STATE_JISX02131) {
r[0] = ESC;
r[1] = '$';
r[2] = '(';
r[3] = 'O';
r += 4;
state = STATE_JISX02131;
}
lasttwo = iso2022_jp3_comp_table_data[idx].composed;
r[0] = (lasttwo >> 8) & 0xff;
r[1] = lasttwo & 0xff;
COMBINE_STATE_NO_LASTTWO;
conv->ostate = state;
return count;
}
not_combining:
count = (prevstate != state ? 3 : 0) + 2;
if (n < count)
return RET_TOOSMALL;
if (prevstate != state) {
if (state != STATE_JISX0208) abort();
r[0] = ESC;
r[1] = '$';
r[2] = 'B';
r += 3;
}
r[0] = (lasttwo >> 8) & 0xff;
r[1] = lasttwo & 0xff;
r += 2;
}
ret = ascii_wctomb(conv,buf,wc,1);
if (ret != RET_ILUNI) {
if (ret != 1) abort();
if (buf[0] < 0x80) {
count += (state == STATE_ASCII ? 1 : 4);
if (n < count)
return RET_TOOSMALL;
if (state != STATE_ASCII) {
r[0] = ESC;
r[1] = '(';
r[2] = 'B';
r += 3;
state = STATE_ASCII;
}
r[0] = buf[0];
COMBINE_STATE_NO_LASTTWO;
conv->ostate = state;
return count;
}
}
ret = jisx0201_wctomb(conv,buf,wc,1);
if (ret != RET_ILUNI) {
if (ret != 1) abort();
if (buf[0] < 0x80) {
count += (state == STATE_JISX0201ROMAN ? 1 : 4);
if (n < count)
return RET_TOOSMALL;
if (state != STATE_JISX0201ROMAN) {
r[0] = ESC;
r[1] = '(';
r[2] = 'J';
r += 3;
state = STATE_JISX0201ROMAN;
}
r[0] = buf[0];
COMBINE_STATE_NO_LASTTWO;
conv->ostate = state;
return count;
}
}
jch = ucs4_to_jisx0213(wc);
ret = jisx0208_wctomb(conv,buf,wc,2);
if (ret != RET_ILUNI) {
if (ret != 2) abort();
if (buf[0] < 0x80 && buf[1] < 0x80) {
if (jch & 0x0080) {
prevstate = state;
lasttwo = jch & 0x7f7f;
state = STATE_JISX0208;
COMBINE_STATE;
conv->ostate = state;
return count;
} else {
count += (state == STATE_JISX0208 ? 2 : 5);
if (n < count)
return RET_TOOSMALL;
if (state != STATE_JISX0208) {
r[0] = ESC;
r[1] = '$';
r[2] = 'B';
r += 3;
state = STATE_JISX0208;
}
r[0] = buf[0];
r[1] = buf[1];
COMBINE_STATE_NO_LASTTWO;
conv->ostate = state;
return count;
}
}
}
if (jch != 0) {
if (jch & 0x8000) {
if (state != STATE_JISX02132) {
count += 4;
if (n < count)
return RET_TOOSMALL;
r[0] = ESC;
r[1] = '$';
r[2] = '(';
r[3] = 'P';
r += 4;
state = STATE_JISX02132;
}
} else {
if (state != STATE_JISX02131) {
count += 4;
if (n < count)
return RET_TOOSMALL;
r[0] = ESC;
r[1] = '$';
r[2] = '(';
r[3] = 'O';
r += 4;
state = STATE_JISX02131;
}
}
if (jch & 0x0080) {
if (jch & 0x8000) abort();
prevstate = state;
lasttwo = jch & 0x7f7f;
COMBINE_STATE;
conv->ostate = state;
return count;
}
count += 2;
if (n < count)
return RET_TOOSMALL;
r[0] = (jch >> 8) & 0x7f;
r[1] = jch & 0x7f;
COMBINE_STATE_NO_LASTTWO;
conv->ostate = state;
return count;
}
ret = jisx0201_wctomb(conv,buf,wc,1);
if (ret != RET_ILUNI) {
if (ret != 1) abort();
if (buf[0] >= 0x80) {
count += (state == STATE_JISX0201KATAKANA ? 1 : 4);
if (n < count)
return RET_TOOSMALL;
if (state != STATE_JISX0201KATAKANA) {
r[0] = ESC;
r[1] = '(';
r[2] = 'I';
r += 3;
state = STATE_JISX0201KATAKANA;
}
r[0] = buf[0]-0x80;
COMBINE_STATE_NO_LASTTWO;
conv->ostate = state;
return count;
}
}
return RET_ILUNI;
}
static int
iso2022_jp3_reset (conv_t conv, unsigned char *r, int n)
{
state_t state = conv->ostate;
SPLIT_STATE;
{
int count =
(lasttwo ? (prevstate != state ? 3 : 0) + 2 : 0)
+ (state != STATE_ASCII ? 3 : 0);
if (n < count)
return RET_TOOSMALL;
if (lasttwo) {
if (prevstate != state) {
if (state != STATE_JISX0208) abort();
r[0] = ESC;
r[1] = '$';
r[2] = 'B';
r += 3;
}
r[0] = (lasttwo >> 8) & 0xff;
r[1] = lasttwo & 0xff;
r += 2;
}
if (state != STATE_ASCII) {
r[0] = ESC;
r[1] = '(';
r[2] = 'B';
}
return count;
}
}
#undef COMBINE_STATE_NO_LASTTWO
#undef COMBINE_STATE
#undef SPLIT_STATE
#undef STATE_JISX02132
#undef STATE_JISX02131
#undef STATE_JISX0208
#undef STATE_JISX0201KATAKANA
#undef STATE_JISX0201ROMAN
#undef STATE_ASCII