[英]iconv() 32 bit vs 64 bit on Linux
I have a bit of code that uses iconv() on Linux to validate a string as being UTF-8 encoded. 我有一些代码在Linux上使用iconv()来验证字符串是否为UTF-8编码。 The conversion I create like:
我创建的转换如下:
iconv_t c = iconv_open("UTF-8","UTF-8");
I run iconv() like: 我像这样运行iconv():
int status = iconv(c, &fromArray, (size_t*)&inSize, &toArray, (size_t*)&outSize);
I then take the string to have valid UTF-8 if status
is not -1. 然后,如果
status
不是-1,则我将字符串具有有效的UTF-8。
This compiles and works fine in a 32 bit environment (where it was initially developed and tested). 它可以在32位环境(最初开发和测试)中编译并正常工作。 However I now have a requirement to get this working in a 64 bit environment (to be specific the 64 bit flavor of Fedora 14 I believe).
但是我现在有一个要求在64位环境中工作(我相信是Fedora 14的64位风格)。 When I compile and run a test on this there
status
is always -1
and I always get an EILSEQ
error in errno
, even for the same string which the 32 bit compile says is fine. 当我对此进行编译并进行测试时,
status
始终为-1
并且在errno
始终会收到EILSEQ
错误,即使对于32位编译器所说的相同字符串也可以。
Does anyone have any ideas as to why this might be happening? 有谁知道为什么会发生这种情况吗?
Recently had experienced same issue. 最近遇到了同样的问题。 Casting to (size_t*) is more (very) likely the root cause.
转换为(size_t *)更有可能是根本原因。 The problem even could be easy simulated with next code:
该问题甚至可以通过以下代码轻松模拟:
cat >Makefile <<EOF
all: build test clean
clean:
rm -f *.o core* t32 t64
test: build
@echo ; echo "run_32bit version:" ; ./t32
@echo ; echo "run_64bit version:" ; ./t64
build:
g++ -m32 t.cpp -o t32 -Wall -O0 -g
g++ t.cpp -o t64 -Wall -O0 -g
EOF
cat >t.cpp <<EOF
#include <errno.h>
#include <stdio.h>
#include <iconv.h>
char buff_toArray [BUFSIZ];
char buff_fromArray [] = \
"<TESTS_STRINGS>\
<T_VERIFICATION_STRINGS/>\
</TESTS_STRINGS>";
void iconv_test ( const char* desc, size_t* size )
{
printf ("%s = size[%zu]\n",desc, (*size) );
}
int main (int argc, char* argv[])
{
char* toArray = &buff_toArray[0];
char* fromArray = &buff_fromArray[0];
const int inSize_const = 61;
short inSize_short = (short) sizeof(buff_fromArray);
int inSize_int = (int) sizeof(buff_fromArray);
unsigned int inSize_uint = (unsigned int) sizeof(buff_fromArray);
long inSize_long = (long) sizeof(buff_fromArray);
long long inSize_llong = (long long) sizeof(buff_fromArray);
size_t inSize_size_t = sizeof(buff_fromArray);
printf ("fake iconv usage:\n");
iconv_test((const char*) "inSize_const", (size_t*)&inSize_const);
iconv_test((const char*) "inSize_short", (size_t*)&inSize_short);
iconv_test((const char*) "inSize_int", (size_t*)&inSize_int);
iconv_test((const char*) "inSize_uint", (size_t*)&inSize_uint);
iconv_test((const char*) "inSize_long", (size_t*)&inSize_long);
iconv_test((const char*) "inSize_llong", (size_t*)&inSize_llong);
iconv_test((const char*) "inSize_size_t", &inSize_size_t);
printf ("real iconv usage:\n");
int inSize = sizeof(buff_fromArray);
int outSize = sizeof(buff_toArray);
iconv_t c = iconv_open("UTF-8","UTF-8");
int status = iconv(c, &fromArray, (size_t*)&inSize, &toArray, (size_t*)&outSize);
printf ("status=[%d], errno=[%d] \n", status, errno );
printf ("result string:\n");
for(size_t i = 0; i <= sizeof(buff_toArray); i++) { printf ("%c", buff_toArray[i]); }
printf ("\n");
int close_status = iconv_close(c);
printf ("close status=[%d], errno=[%d] \n", close_status, errno );
return 0;
}
EOF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.