#include "jisx0213.h"
#include "flushwc.h"
static int
shift_jisx0213_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
{
ucs4_t last_wc = conv->istate;
if (last_wc) {
conv->istate = 0;
*pwc = last_wc;
return 0;
} else {
unsigned char c = *s;
if (c < 0x80) {
if (c == 0x5c)
*pwc = (ucs4_t) 0x00a5;
else if (c == 0x7e)
*pwc = (ucs4_t) 0x203e;
else
*pwc = (ucs4_t) c;
return 1;
} else if (c >= 0xa1 && c <= 0xdf) {
*pwc = c + 0xfec0;
return 1;
} else {
if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) {
if (n >= 2) {
unsigned char c2 = s[1];
if ((c2 >= 0x40 && c2 <= 0x7e) || (c2 >= 0x80 && c2 <= 0xfc)) {
unsigned int c1;
ucs4_t wc;
if (c < 0xe0)
c -= 0x81;
else
c -= 0xc1;
if (c2 < 0x80)
c2 -= 0x40;
else
c2 -= 0x41;
c1 = 2 * c;
if (c2 >= 0x5e)
c2 -= 0x5e, c1++;
c2 += 0x21;
if (c1 >= 0x5e) {
if (c1 >= 0x67)
c1 += 230;
else if (c1 >= 0x63 || c1 == 0x5f)
c1 += 168;
else
c1 += 162;
}
wc = jisx0213_to_ucs4(0x121+c1,c2);
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;
} else
*pwc = wc;
return 2;
}
}
} else
return RET_TOOFEW(0);
}
return RET_ILSEQ;
}
}
}
#define shift_jisx0213_flushwc normal_flushwc
static const struct { unsigned short base; unsigned short composed; } shift_jisx0213_comp_table_data[] = {
#define shift_jisx0213_comp_table02e5_idx 0
#define shift_jisx0213_comp_table02e5_len 1
{ 0x8684, 0x8685 },
#define shift_jisx0213_comp_table02e9_idx (shift_jisx0213_comp_table02e5_idx+shift_jisx0213_comp_table02e5_len)
#define shift_jisx0213_comp_table02e9_len 1
{ 0x8680, 0x8686 },
#define shift_jisx0213_comp_table0300_idx (shift_jisx0213_comp_table02e9_idx+shift_jisx0213_comp_table02e9_len)
#define shift_jisx0213_comp_table0300_len 5
{ 0x857b, 0x8663 },
{ 0x8657, 0x8667 },
{ 0x8656, 0x8669 },
{ 0x864f, 0x866b },
{ 0x8662, 0x866d },
#define shift_jisx0213_comp_table0301_idx (shift_jisx0213_comp_table0300_idx+shift_jisx0213_comp_table0300_len)
#define shift_jisx0213_comp_table0301_len 4
{ 0x8657, 0x8668 },
{ 0x8656, 0x866a },
{ 0x864f, 0x866c },
{ 0x8662, 0x866e },
#define shift_jisx0213_comp_table309a_idx (shift_jisx0213_comp_table0301_idx+shift_jisx0213_comp_table0301_len)
#define shift_jisx0213_comp_table309a_len 14
{ 0x82a9, 0x82f5 },
{ 0x82ab, 0x82f6 },
{ 0x82ad, 0x82f7 },
{ 0x82af, 0x82f8 },
{ 0x82b1, 0x82f9 },
{ 0x834a, 0x8397 },
{ 0x834c, 0x8398 },
{ 0x834e, 0x8399 },
{ 0x8350, 0x839a },
{ 0x8352, 0x839b },
{ 0x835a, 0x839c },
{ 0x8363, 0x839d },
{ 0x8367, 0x839e },
{ 0x83f3, 0x83f6 },
};
static int
shift_jisx0213_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
{
int count = 0;
unsigned short lasttwo = conv->ostate;
if (lasttwo) {
unsigned int idx;
unsigned int len;
if (wc == 0x02e5)
idx = shift_jisx0213_comp_table02e5_idx,
len = shift_jisx0213_comp_table02e5_len;
else if (wc == 0x02e9)
idx = shift_jisx0213_comp_table02e9_idx,
len = shift_jisx0213_comp_table02e9_len;
else if (wc == 0x0300)
idx = shift_jisx0213_comp_table0300_idx,
len = shift_jisx0213_comp_table0300_len;
else if (wc == 0x0301)
idx = shift_jisx0213_comp_table0301_idx,
len = shift_jisx0213_comp_table0301_len;
else if (wc == 0x309a)
idx = shift_jisx0213_comp_table309a_idx,
len = shift_jisx0213_comp_table309a_len;
else
goto not_combining;
do
if (shift_jisx0213_comp_table_data[idx].base == lasttwo)
break;
while (++idx, --len > 0);
if (len > 0) {
if (n >= 2) {
lasttwo = shift_jisx0213_comp_table_data[idx].composed;
r[0] = (lasttwo >> 8) & 0xff;
r[1] = lasttwo & 0xff;
conv->ostate = 0;
return 2;
} else
return RET_TOOSMALL;
}
not_combining:
if (n < 2)
return RET_TOOSMALL;
r[0] = (lasttwo >> 8) & 0xff;
r[1] = lasttwo & 0xff;
r += 2;
count = 2;
}
if (wc < 0x80 && wc != 0x5c && wc != 0x7e) {
if (n > count) {
r[0] = (unsigned char) wc;
conv->ostate = 0;
return count+1;
} else
return RET_TOOSMALL;
} else if (wc == 0x00a5) {
if (n > count) {
r[0] = 0x5c;
conv->ostate = 0;
return count+1;
} else
return RET_TOOSMALL;
} else if (wc == 0x203e) {
if (n > count) {
r[0] = 0x7e;
conv->ostate = 0;
return count+1;
} else
return RET_TOOSMALL;
} else if (wc >= 0xff61 && wc <= 0xff9f) {
if (n > count) {
r[0] = wc - 0xfec0;
conv->ostate = 0;
return count+1;
} else
return RET_TOOSMALL;
} else {
unsigned int s1, s2;
unsigned short jch = ucs4_to_jisx0213(wc);
if (jch != 0) {
s1 = jch >> 8;
s2 = jch & 0x7f;
s1 -= 0x21;
s2 -= 0x21;
if (s1 >= 0x5e) {
if (s1 >= 0xcd)
s1 -= 102;
else if (s1 >= 0x8b || s1 == 0x87)
s1 -= 40;
else
s1 -= 34;
}
if (s1 & 1)
s2 += 0x5e;
s1 = s1 >> 1;
if (s1 < 0x1f)
s1 += 0x81;
else
s1 += 0xc1;
if (s2 < 0x3f)
s2 += 0x40;
else
s2 += 0x41;
if (jch & 0x0080) {
if (jch & 0x8000) abort();
conv->ostate = (s1 << 8) | s2;
return count+0;
}
if (n >= count+2) {
r[0] = s1;
r[1] = s2;
conv->ostate = 0;
return count+2;
} else
return RET_TOOSMALL;
}
return RET_ILUNI;
}
}
static int
shift_jisx0213_reset (conv_t conv, unsigned char *r, int n)
{
state_t lasttwo = conv->ostate;
if (lasttwo) {
if (n < 2)
return RET_TOOSMALL;
r[0] = (lasttwo >> 8) & 0xff;
r[1] = lasttwo & 0xff;
return 2;
} else
return 0;
}