iconv_open()在Solaris 8上返回EINVAL

时间:2015-11-18 11:20:15

标签: c solaris iconv wchar-t libiconv

Solaris 8 中,看起来iconv*()系列函数已损坏,仅支持单字节字符集和UTF-8之间的转换,可以使用此代码进行验证例如:

#include <stdio.h>
#include <errno.h>
#include <iconv.h>

#if defined(__sun) && defined(__SVR4)
#define CP1251 "ansi-1251"
#define ISO_8859_5 "ISO8859-5"
#else
#define CP1251 "CP1251"
#define ISO_8859_5 "ISO-8859-5"
#endif

void iconv_open_debug(const char *, const char *);

int main() {
    iconv_open_debug(CP1251, CP1251);
    iconv_open_debug(CP1251, ISO_8859_5);
    iconv_open_debug(CP1251, "KOI8-R");
    iconv_open_debug(CP1251, "UTF-8");
    iconv_open_debug(CP1251, "WCHAR_T");

    iconv_open_debug(ISO_8859_5, CP1251);
    iconv_open_debug(ISO_8859_5, ISO_8859_5);
    iconv_open_debug(ISO_8859_5, "KOI8-R");
    iconv_open_debug(ISO_8859_5, "UTF-8");
    iconv_open_debug(ISO_8859_5, "WCHAR_T");

    iconv_open_debug("KOI8-R", CP1251);
    iconv_open_debug("KOI8-R", ISO_8859_5);
    iconv_open_debug("KOI8-R", "KOI8-R");
    iconv_open_debug("KOI8-R", "UTF-8");
    iconv_open_debug("KOI8-R", "WCHAR_T");

    iconv_open_debug("UTF-8", CP1251);
    iconv_open_debug("UTF-8", ISO_8859_5);
    iconv_open_debug("UTF-8", "KOI8-R");
    iconv_open_debug("UTF-8", "UTF-8");
    iconv_open_debug("UTF-8", "WCHAR_T");

    iconv_open_debug("WCHAR_T", CP1251);
    iconv_open_debug("WCHAR_T", ISO_8859_5);
    iconv_open_debug("WCHAR_T", "KOI8-R");
    iconv_open_debug("WCHAR_T", "UTF-8");
    iconv_open_debug("WCHAR_T", "WCHAR_T");

    return 0;
}

void iconv_open_debug(const char *from, const char *to) {
    errno = 0;
    if (iconv_open(to, from) == (iconv_t) -1) {
        fprintf(stderr, "iconv_open(\"%s\", \"%s\") FAIL: errno = %d\n", to, from, errno);
        perror("iconv_open()");
    } else {
        fprintf(stdout, "iconv_open(\"%s\", \"%s\") PASS\n", to, from);
    }
}

仅打印

iconv_open("UTF-8", "ansi-1251") PASS
iconv_open("UTF-8", "ISO8859-5") PASS
iconv_open("UTF-8", "KOI8-R") PASS
iconv_open("ansi-1251", "UTF-8") PASS
iconv_open("ISO8859-5", "UTF-8") PASS
iconv_open("KOI8-R", "UTF-8") PASS

到stdout并返回其他对的EINVAL。请注意,甚至不支持转换为相同的字符集(例如UTF-8 - > UTF-8)。

问题

  1. 任何人都可以参考描述 Solaris 版本iconv.h的限制的文档吗?
  2. 如何将wchar_t*转换为单字节或多字节字符串,而不依赖于 GNU libiconvwcstombs() 正常,除非它依赖于当前语言环境的字符集,而我希望使用特定字符集将宽字符串转换为常规字符串,可能与默认字符串不同。

1 个答案:

答案 0 :(得分:0)

正在运行sdtconvtool表示支持大多数旧版代码页。

在使用truss -u libc::iconv_open重新运行相同的实用程序后,我了解到从一个单字节编码到另一个单字节编码的转换分两步完成,中间转换为UTF-8。< / p>

说到从"WCHAR_T"转换,iconv(3)也支持它,但"UCS-4"应该用作源字符集名称,因为sizeof(wchar_t)在Solaris上是4(对于这两者都是如此) x86和SPARC)。