#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unicode_gb2312.h>
#include "common_defs.h"
#define SI 0x0f
#define SO 0x0e
#define ESC 0x1b
#define MSB 0x80
#define NON_ID_CHAR '?'
typedef struct _icv_state {
short _ustate;
short _istate;
short _gstate;
char _cbuf[3];
} _iconv_st;
enum _USTATE { U0, U1, U2, U3, U4, U5, U6 };
enum _ISTATE { IN, OUT };
enum _GSTATE { G0, G1 };
int unicode_to_iso(char in_byte1, char in_byte2, char *buf, int buflen);
void *
_icv_open()
{
_iconv_st *st;
if ((st = (_iconv_st *)malloc(sizeof(_iconv_st))) == NULL) {
errno = ENOMEM;
return ((void *) -1);
}
st->_ustate = U0;
st->_istate = IN;
st->_gstate = G0;
return ((void *)st);
}
void
_icv_close(_iconv_st *st)
{
if (st == NULL)
errno = EBADF;
else
free(st);
}
size_t
_icv_iconv(_iconv_st *st, char **inbuf, size_t*inbytesleft,
char **outbuf, size_t*outbytesleft)
{
char c1, c2;
int n;
if (st == NULL) {
errno = EBADF;
return ((size_t)-1);
}
if (inbuf == NULL || *inbuf == NULL) {
st->_ustate = U0;
st->_istate = IN;
st->_gstate = G0;
return ((size_t)0);
}
errno = 0;
while (*inbytesleft > 0 && *outbytesleft > 0) {
uchar_t first_byte;
switch (st->_ustate) {
case U0:
if ((**inbuf & MSB) == 0) {
if (st->_istate == OUT) {
st->_istate = IN;
**outbuf = SI;
(*outbuf)++, (*outbytesleft)--;
if (*outbytesleft <= 0) {
errno = E2BIG;
return ((size_t)-1);
}
}
**outbuf = **inbuf;
(*outbuf)++, (*outbytesleft)--;
} else {
if ((**inbuf & 0xe0) == 0xc0) {
if ( number_of_bytes_in_utf8_char[((uchar_t)**inbuf)] == ICV_TYPE_ILLEGAL_CHAR )
{
errno = EILSEQ;
break;
}
else
{
st->_ustate = U1;
st->_cbuf[0] = **inbuf;
}
} else if ((**inbuf & 0xf0) == 0xe0) {
st->_ustate = U2;
st->_cbuf[0] = **inbuf;
} else {
if ( number_of_bytes_in_utf8_char[((uchar_t)**inbuf)] == ICV_TYPE_ILLEGAL_CHAR )
{
errno = EILSEQ;
break;
}
else {
st->_ustate = U4;
st->_cbuf[0] = **inbuf;
}
}
if (st->_istate == IN) {
if (st->_gstate == G0) {
if (*outbytesleft < 4) {
errno = E2BIG;
return ((size_t)-1);
}
st->_gstate = G1;
**outbuf = ESC;
*(*outbuf+1) = '$';
*(*outbuf+2) = ')';
*(*outbuf+3) = 'A';
(*outbuf) += 4, (*outbytesleft) -= 4;
if (*outbytesleft <= 0) {
errno = E2BIG;
return ((size_t)-1);
}
}
st->_istate = OUT;
**outbuf = SO;
(*outbuf)++, (*outbytesleft)--;
}
}
break;
case U1:
if ((**inbuf & 0xc0) == MSB) {
c1 = (st->_cbuf[0]&0x1c)>>2;
c2 = ((st->_cbuf[0]&0x03)<<6) | ((**inbuf)&0x3f);
n = unicode_to_iso(c1, c2, *outbuf, *outbytesleft);
if (n > 0) {
(*outbuf) += n, (*outbytesleft) -= n;
} else {
errno = E2BIG;
return ((size_t)-1);
}
st->_ustate = U0;
} else {
errno = EILSEQ;
}
break;
case U2:
first_byte = st->_cbuf[0];
if (((uchar_t)**inbuf) < valid_min_2nd_byte[first_byte] ||
((uchar_t)**inbuf) > valid_max_2nd_byte[first_byte] )
errno = EILSEQ;
else {
st->_ustate = U3;
st->_cbuf[1] = **inbuf;
}
break;
case U3:
if ((**inbuf & 0xc0) == MSB) {
c1 = ((st->_cbuf[0]&0x0f)<<4) | ((st->_cbuf[1]&0x3c)>>2);
c2 = ((st->_cbuf[1]&0x03)<<6) | ((**inbuf)&0x3f);
n = unicode_to_iso(c1, c2, *outbuf, *outbytesleft);
if (n > 0) {
(*outbuf) += n, (*outbytesleft) -= n;
} else if ( n == -1 ) {
errno = EILSEQ;
} else {
errno = E2BIG;
return ((size_t)-1);
}
st->_ustate = U0;
} else {
errno = EILSEQ;
}
break;
case U4:
first_byte = st->_cbuf[0];
if (((uchar_t)**inbuf) < valid_min_2nd_byte[first_byte] ||
((uchar_t)**inbuf) > valid_max_2nd_byte[first_byte] )
errno = EILSEQ;
else {
st->_ustate = U5;
st->_cbuf[1] = **inbuf;
}
break;
case U5:
if ((**inbuf & 0xc0) == MSB)
{
st->_ustate = U6;
st->_cbuf[2] = **inbuf;
}
else
errno = EILSEQ;
break;
case U6:
if ((**inbuf & 0xc0) == MSB)
{
if ( *outbytesleft < 2 )
errno = E2BIG;
else
{
**outbuf = NON_ID_CHAR;
*(*outbuf+1) = NON_ID_CHAR;
(*outbytesleft) -= 2;
st->_ustate = U0;
}
}
else
errno = EILSEQ;
break;
}
if (errno)
return ((size_t)-1);
(*inbuf)++; (*inbytesleft)--;
}
if (*inbytesleft == 0 && st->_ustate != U0) {
errno = EINVAL;
return ((size_t) -1);
}
if (*inbytesleft > 0 && *outbytesleft == 0) {
errno = E2BIG;
return ((size_t)-1);
}
return ((size_t)(*inbytesleft));
}
int unicode_to_iso(in_byte1, in_byte2, buf, buflen)
char in_byte1, in_byte2;
char *buf;
int buflen;
{
int gb, unicode;
int i, l, h;
if (buflen < 2)
return 0;
unicode = ((in_byte1 & 0xff) << 8) + (in_byte2 & 0xff);
if ( unicode == 0xFFFE || unicode == 0xFFFF ) return -1;
for (l = 0, h = UNICODEMAX; l < h; ) {
if (unicode_gb_tab[l].key == unicode) {
i = l;
break;
}
if (unicode_gb_tab[h].key == unicode) {
i = h;
break;
}
i = (l + h) / 2;
if (unicode_gb_tab[i].key == unicode)
break;
if (unicode_gb_tab[i].key < unicode)
l = i + 1;
else h = i - 1;
}
if (unicode == unicode_gb_tab[i].key) {
gb = unicode_gb_tab[i].value;
*buf = (gb & 0xff00) >> 8;
*(buf+1) = gb & 0xff;
} else {
*buf = *(buf+1) = NON_ID_CHAR;
}
return 2;
}