#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <euc.h>
#include "japanese.h"
#include "jfp_iconv_unicode.h"
#ifdef JAVA_CONV_COMPAT
#define JFP_U2E_ICONV_JAVA
#elif JFP_ICONV_MS932
#define JFP_U2E_ICONV_MS932
#else
#define JFP_U2E_ICONV
#endif
#include "jfp_ucs2_to_euc16.h"
#define DEF_SINGLE '?'
static unsigned short lookuptbl(unsigned short);
void *
_icv_open(void)
{
return (_icv_open_unicode((size_t)0));
}
void
_icv_close(void *cd)
{
_icv_close_unicode(cd);
return;
}
size_t
_icv_iconv(void *cd, char **inbuf, size_t *inbytesleft,
char **outbuf, size_t *outbytesleft)
{
unsigned char ic;
size_t rv = (size_t)0;
unsigned int ucs4;
unsigned short euc16;
unsigned short dest;
unsigned char *ip;
size_t ileft;
char *op;
size_t oleft;
if ((inbuf == NULL) || (*inbuf == NULL)) {
_icv_reset_unicode(cd);
return ((size_t)0);
}
ip = (unsigned char *)*inbuf;
ileft = *inbytesleft;
op = *outbuf;
oleft = *outbytesleft;
while (ileft != 0) {
GETU(&ucs4);
if (ucs4 > 0xffff) {
NPUT((unsigned char)DEF_SINGLE, "non-BMP(replaced)");
} else {
euc16 = _jfp_ucs2_to_euc16((unsigned short)ucs4);
switch (euc16 & 0x8080) {
case 0x0000:
if (ISC1CTRL((unsigned char)euc16)) {
NPUT((unsigned char)'?',
"CS0-C1CTRL(replaced)")
} else {
ic = (unsigned char)euc16;
NPUT(ic, "CS0-1");
}
break;
case 0x8080:
ic = (unsigned short)((euc16 >> 8) & 0x7f);
NPUT(jis208tosj1[ic], "CS1-1");
ic = (unsigned char)((euc16 & 0x7f)
+ (((ic % 2) == 0) ? 0x80 : 0x00));
NPUT(jistosj2[ic], "CS1-2");
break;
case 0x0080:
ic = (unsigned char)euc16;
NPUT(ic, "CS2-1");
break;
case 0x8000:
ic = (unsigned short)((euc16 >> 8) & 0x7f);
if (euc16 == 0xa271) {
NPUT(0x87, "CS3-NUMERO-1");
NPUT(0x82, "CS3-NUMERO-2");
} else if (ic < 0x75) {
dest = lookuptbl(euc16 & 0x7f7f);
if (dest == 0xffff) {
NPUT((unsigned char)'?',
"CS3-NoSJIS(replaced)")
} else {
#ifdef JAVA_CONV_COMPAT
NPUT((unsigned char)'?',
"CS3-IBM(replaced)")
#else
if (dest > 0xff) {
NPUT((dest >> 8) & 0xff,
"CS3-IBM-1");
NPUT(dest & 0xff,
"CS3-IBM-2");
} else {
NPUT(dest & 0xff,
"CS3-IBM-1");
}
#endif
}
} else {
NPUT(jis212tosj1[ic], "CS3-1");
ic = (unsigned short)((euc16 & 0x7f)
+ (((ic % 2) == 0) ?
0x80 : 0x00));
NPUT(jistosj2[ic], "CS3-2");
}
break;
}
}
next:
*inbuf = (char *)ip;
*inbytesleft = ileft;
*outbuf = op;
*outbytesleft = oleft;
}
ret:
#if defined(DEBUG)
if (rv == (size_t)-1) {
fprintf(stderr, "DEBUG: errno=%d: %s\n", errno, debugmsg);
}
#endif
return ((rv == (size_t)-1) ? rv : *inbytesleft);
}
static unsigned short
lookuptbl(unsigned short dest)
{
unsigned short tmp;
int i;
int sz = (sizeof (sjtoibmext) / sizeof (sjtoibmext[0]));
for (i = 0; i < sz; i++) {
tmp = (sjtoibmext[i] & 0x7f7f);
if (tmp == dest)
return ((i + 0xfa40 + ((i / 0xc0) * 0x40)));
}
return (0x3f);
}