iconv 함수 사용시 errno 두번 발생 후 무한 루프 발생 오류...
안녕하세요.
도저히 답을 못찾아 여기에 글을 올립니다.
목적:
utf8을 euckr로 변환하려고 합니다.
iconv 변환 시 변환이 안되는 글자는 .으로 표기 하려 합니다.
시험:
가나봎가사샆
UTF8로 위의 글자를 인코딩하고 이를 EUCkr로 변환
결과:
start iconv srclen(21)
====================================
inleftsize(21) res(-1) outptr(bfffef70) outleftsize(43)
iconv_str: not defined char in the table [가나봎가사샆]
inleftsize(12) res(-1) outptr(bfffef75) outleftsize(38)
iconv_str: not defined char in the table [가나봎가사샆]
inleftsize(3) res(-1) outptr(bfffef7a) outleftsize(33)
iconv_str: not defined char in the table [가나봎가사샆]
inleftsize(2) res(-1) outptr(bfffef7b) outleftsize(32)
iconv_str: not defined char in the table [가나봎가사샆]
inleftsize(-32) res(-1) outptr(bfffef9b) outleftsize(0)
iconv unknown errno(7)
inleftsize(-35) res(-1) outptr(bfffef9c) outleftsize(-1)
....
....
iconv unknown errno(7)
inleftsize(-629) res(-1) outptr(bffff062) outleftsize(-199)
iconv_str: not defined char in the table [가나봎가사샆]
Segmentation fault (core dumped)
while(inleftsize>1) {
if (inleftsize < 2 || outleftsize <2 ) break;
소스 상에 위의 루프 탈출을 넣어 두었으나,,,, 탈출이 안되고 무한 루프에 빠져 죽음.
소스:
int IconvString (char *to, char *from, char *src, unsigned short srclen, char *dst)
{
//size_t srclen;
printf("start iconv srclen(%d)\n",srclen);
printf("====================================\n");
size_t dstlen;
size_t inleftsize, outleftsize;
size_t res; /* report of iconv */
iconv_t cd = (iconv_t)-1;
int index = 0;
if( strstr(from, "EUCKR") && strstr(to, "UTF8") ){
outleftsize = inleftsize*2+1;
index = 2;
}else if ( strstr(from, "UTF8") && strstr(to, "EUCKR") ){
outleftsize = inleftsize;
index = 3;
}else {
printf("[r245IconvString] cannot convert from %s to %s\n\n", from, to);
return 0;
}
char *inptr;
char *outptr;
/* open iconv */
cd = iconv_open(to, from);
if (cd==(iconv_t)(-1)) {
printf("error iconv open\n");
return (int)cd;
}
if (!strcasecmp(from, "UCS-2")) inleftsize=2;
else {
//srclen = (size_t)strlen(src);
inleftsize = (size_t)srclen;
}
outleftsize = inleftsize*2+1;
dstlen = outleftsize;
inptr = (char*)src;
outptr = dst;
while(inleftsize>1) {
if (inleftsize < 2 || outleftsize <2 ) break;
res = iconv(cd,&inptr,&inleftsize,&outptr,&outleftsize);
printf("inleftsize(%d) res(%d) outptr(%x) outleftsize(%d)\n\n", inleftsize, res,
outptr, outleftsize);
if (res == (size_t)(-1)) {
if( (inleftsize - index) > 1) {
inleftsize = inleftsize - index;
inptr = inptr + index;
} else {
inptr++;
inleftsize--;
}
if (errno == EILSEQ) { /* not defined char in the table ? */
fprintf(stderr, "iconv_str: not defined char in the table [%s]\n", src);
}
else if (errno == EINVAL) { /* incomplete char, need readin more codes */
fprintf(stderr, "iconv_str: incomplete char or shift sequence\n");
memmove(inptr, inptr, inleftsize+index);
}
else {
fprintf(stderr, "iconv unknown errno(%d)\n",errno);
}
*outptr='.';
outptr++;
outleftsize--;
}
else break;
}
dst[dstlen-outleftsize] = '\0';
/* close
* iconv
* */
iconv_close(cd);
printf("=============================== \n");
printf("end iconv \n");
return(dstlen-outleftsize);
}
소스 상에서 어디가 잘못된 걸까요? 제발 알려 주세요~ TT
잘 모르겠지만 iconv unknown
잘 모르겠지만 iconv unknown errno(7)가 나오면 break해야하지 않을까요?
7이 뭘까 살펴보니..
http://www.redwiki.net/wiki/wiki.php/iconv
http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/include/asm-generic/errno-base.h#L10
E2BIG 오류네요. *outbuf에 공간이 부족한 오류라네요.
댓글 달기