iconv 함수 사용시 errno 두번 발생 후 무한 루프 발생 오류...

seraphino의 이미지

안녕하세요.

도저히 답을 못찾아 여기에 글을 올립니다.

목적:
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 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에 공간이 부족한 오류라네요.

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.