#include <errno.h>
#include "ktable.h"
#include "hangulcode.h"
static unsigned short _johap92_to_wansung(unsigned short code);
void* _icv_open()
{
return((void*)MAGIC_NUMBER);
}
void _icv_close(int* cd)
{
if (!cd || cd != (int*)MAGIC_NUMBER)
errno = EBADF;
}
size_t _icv_iconv(int* cd, char** inbuf, size_t* inbufleft,
char** outbuf, size_t* outbufleft)
{
size_t ret_val = 0;
unsigned char* ib;
unsigned char* ob;
unsigned char* ibtail;
unsigned char* obtail;
if (!cd || cd != (int*)MAGIC_NUMBER)
{
errno = EBADF;
return((size_t)-1);
}
if (!inbuf || !(*inbuf))
return((size_t)0);
ib = (unsigned char*)*inbuf;
ob = (unsigned char*)*outbuf;
ibtail = ib + *inbufleft;
obtail = ob + *outbufleft;
while (ib < ibtail)
{
if (!(*ib & 0x80))
{
if (ob >= obtail)
{
errno = E2BIG;
ret_val = (size_t)-1;
break;
}
*ob++ = *ib++;
}
else
{
unsigned short result;
if ((ibtail - ib) < 2)
{
errno = EINVAL;
ret_val = (size_t)-1;
break;
}
result = _johap92_to_wansung((unsigned short)(*ib)<<8 |
(unsigned short)(*(ib + 1)));
if (result != FAILED && result != ILLEGAL_SEQ)
{
if ((obtail - ob) < 2)
{
errno = E2BIG;
ret_val = (size_t)-1;
break;
}
*ob++ = (unsigned char)(result >> 8);
*ob++ = (unsigned char)(result & 0xFF);
}
else
{
errno = EILSEQ;
ret_val = (size_t)-1;
break;
}
ib += 2;
}
}
*inbuf = (char*)ib;
*inbufleft = ibtail - ib;
*outbuf = (char*)ob;
*outbufleft = obtail - ob;
return(ret_val);
}
static unsigned short _johap92_to_wansung(unsigned short code)
{
short ci, v, cf;
short mask;
int disp, i;
int ch1, ch2;
long cfbit;
ch1 = code >> 8;
ch2 = code & 0xff;
if ((ch1 >= 0x84 && ch1 <= 0xd3) && ((ch2 >= 0x41 && ch2 <= 0x7e) ||
(ch2 >= 0x81 && ch2 <= 0xfe)))
{
ci = CHOSUNG(code) - 0x02;
v = JOONGSUNG(code) - ((unsigned short)(JOONGSUNG(code) - 2) /
8 * 2 + 3);
cf = JONGSUNG(code) - (unsigned short)JONGSUNG(code) / 18;
if (v < 0)
return(0xA4A0 + Y19_32[CHOSUNG(code) - 1]);
if (ci < 0)
{
if (cf <= 1)
return(0xA4BF + v);
return(ILLEGAL_SEQ);
}
for (cfbit = cmp_bitmap[ci][v], disp = 0, i = 0; i < cf; i++)
{
if (cfbit & BIT_MASK)
disp++;
cfbit >>= 1;
}
if (!(cfbit & BIT_MASK))
return(ILLEGAL_SEQ);
code = cmp_srchtbl[ci][v] + disp;
mask = cmp_srchtbl[ci][v] & 0xff;
if ((mask + disp) > 0xfe)
code += SKIP;
return(code);
}
else if ((ch2 >= 0x31 && ch2 <= 0x7e) || (ch2 >= 0x91 && ch2 <= 0xfe))
{
if (ch1 >= 0xe0 && ch1 <= 0xf9)
{
code = (0xca + (ch1 - 0xe0) * 2 + ch2 / 0xa1) << 8;
return(code | ((ch2 > 0xa0) ? ch2
: ch2 + 0x70 - ((ch2 / 0x91) * 0x12)));
}
else if (ch1 >= 0xd9 && ch1 <= 0xde)
{
code = (0xa1 + (ch1 - 0xd9) * 2 + ch2 / 0xa1) << 8;
return(code | ((ch2 > 0xa0) ? ch2
: ch2 + 0x70 - ((ch2 / 0x91) * 0x12)));
}
else if (ch1 == 0xd8)
return((ch2 > 0xa0) ? 0xfe00 | ch2 : 0xc900
| (ch2 + 0x70 - ((ch2 / 0x91) * 0x12)));
}
return(FAILED);
}