#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <euc.h>
#define JFP_ICONV_STATELESS
#include "japanese.h"
void *
_icv_open(void)
{
return (_icv_open_stateless());
}
void
_icv_close(void *cd)
{
_icv_close_stateless(cd);
return;
}
size_t
_icv_iconv(void *cd, char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
int stat;
unsigned char *ip, ic;
char *op;
size_t ileft, oleft;
size_t retval;
stat = ST_INIT;
if ((inbuf == NULL) || (*inbuf == NULL)) {
return ((size_t)0);
}
ip = (unsigned char *)*inbuf;
op = *outbuf;
ileft = *inbytesleft;
oleft = *outbytesleft;
while ((int)ileft > 0) {
GET(ic);
if (stat == ST_INCS1) {
PUT(((ic & CMASK) - 0x20));
stat = ST_INIT;
continue;
} else if (stat == ST_INCS3) {
PUT(((ic & CMASK) - 0x20));
GET(ic);
PUT(((ic & CMASK) - 0x20));
stat = ST_INIT;
continue;
}
if (ISASC((int)ic)) {
errno = EILSEQ;
retval = (size_t)ERR_RETURN;
goto ret;
} else if (ISCS1(ic)) {
if ((int)ileft > 0) {
if (ISCS1(ic) && ISCS1(*ip)) {
if (oleft < JISW1) {
UNGET();
errno = E2BIG;
retval = (size_t)ERR_RETURN;
goto ret;
}
stat = ST_INCS1;
PUT(((ic & CMASK) - 0x20));
continue;
} else {
errno = EILSEQ;
retval = (size_t)ERR_RETURN;
goto ret;
}
} else {
UNGET();
errno = EINVAL;
retval = (size_t)ERR_RETURN;
goto ret;
}
} else if (ic == SS2) {
errno = EILSEQ;
retval = (size_t)ERR_RETURN;
goto ret;
} else if (ic == SS3) {
if (ileft >= EUCW3) {
if (ISCS3(*ip) && ISCS3(*(ip + 1))) {
if (oleft < JISW3) {
UNGET();
errno = E2BIG;
retval = (size_t)ERR_RETURN;
goto ret;
}
stat = ST_INCS3;
continue;
} else {
errno = EILSEQ;
retval = (size_t)ERR_RETURN;
goto ret;
}
} else {
UNGET();
errno = EINVAL;
retval = (size_t)ERR_RETURN;
goto ret;
}
} else {
UNGET();
errno = EILSEQ;
retval = (size_t)ERR_RETURN;
goto ret;
}
}
retval = ileft;
ret:
*inbuf = (char *)ip;
*inbytesleft = ileft;
ret2:
*outbuf = op;
*outbytesleft = oleft;
return (retval);
}