Email Unicode字符显示不正确

Email Unicode字符显示不正确,email,curl,unicode,wchar,Email,Curl,Unicode,Wchar,我正在制作一个支持多种语言的C程序。该程序使用WCHAR而不是char类型发送电子邮件。问题是,当我收到电子邮件并阅读它时,有些字符显示不正确,甚至有些英文字符如e、m、。。。这是一个例子: <!-- language: lang-c --> curl_easy_setopt(hnd, CURLOPT_READFUNCTION, payload_source); curl_easy_setopt(hnd, CURLOPT_READDATA, &upload_ctx); st

我正在制作一个支持多种语言的C程序。该程序使用WCHAR而不是char类型发送电子邮件。问题是,当我收到电子邮件并阅读它时,有些字符显示不正确,甚至有些英文字符如e、m、。。。这是一个例子:

<!-- language: lang-c -->
curl_easy_setopt(hnd, CURLOPT_READFUNCTION, payload_source);
curl_easy_setopt(hnd, CURLOPT_READDATA, &upload_ctx);

static const WCHAR *payload_text[]={
    L"To: <me@mail.com>\n",
    L"From: <me@mail.com>(Example User)\n",
    L"Subject: Hello!\n",
    L"\n",
    L"Message sent\n",
    NULL
};

struct upload_status {
    int lines_read;
};

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp){
    struct upload_status *upload_ctx = (struct upload_status *)userp;
    const WCHAR *data;

    if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
        return 0;
    }

    data = payload_text[upload_ctx->lines_read];
    if (data) {
        size_t len = wcslen(data);
        memcpy(ptr, data, len);
        upload_ctx->lines_read ++;
        return len;
    }
    return 0;
}

curl_easy_setopt(hnd、CURLOPT_READFUNCTION、payload_source);
curl\u easy\u setopt(hnd、CURLOPT\u READDATA和upload\u ctx);
静态常量WCHAR*有效负载_文本[]={
L“收件人:\n”,
L“发件人:(示例用户)\n”,
L“主题:你好!\n”,
L“\n”,
L“已发送消息\n”,
无效的
};
结构上载\u状态{
整数行(u read);;
};
静态大小有效负载源(void*ptr、size\t size、size\t nmemb、void*userp){
结构上传状态*上传状态=(结构上传状态*)用户p;
常量WCHAR*数据;
如果((size==0)| |(nmemb==0)| |((size*nmemb)<1)){
返回0;
}
数据=有效载荷\文本[上传\ ctx->行\读取];
如果(数据){
尺寸长度=wcslen(数据);
memcpy(ptr、数据、len);
上传ctx->lines\u read++;
回程透镜;
}
返回0;
}
memcpy()
对字节而不是字符进行操作。您没有考虑到
sizeof(wchar\u t)>1
。在某些系统上为2字节,在其他系统上为4字节。这种重复性使得编写可移植代码时,
wchar\u t
成为一个错误的选择。您应该改用Unicode库,如icu或iconv)

调用
memcpy()
时,需要考虑
sizeof(wchar\u t)
。您还需要考虑到目标缓冲区可能小于您试图复制的文本字节的大小。仅跟踪
行\u read
本身是不够的,您还必须跟踪您复制的当前行的字节数,以便处理当前文本行跨多个目标缓冲区的情况

请尝试类似以下内容:

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
{
    struct upload_status *upload_ctx = (struct upload_status *) userp;
    unsigned char *buf = (unsignd char *) ptr;
    size_t available = (size * nmemb);
    size_t total = 0;

    while (available > 0)
    {
        wchar_t *data = payload_text[upload_ctx->lines_read];
        if (!data) break;

        unsigned char *rawdata = (unsigned char *) data;

        size_t remaining = (wcslen(data) * sizeof(wchar_t)) - upload_ctx->line_bytes_read;
        while ((remaining > 0) && (available > 0))
        {
            size_t bytes_to_copy = min(remaining, available);
            memcpy(buf, rawdata, bytes_to_copy);

            buf += bytes_to_copy;
            available -= bytes_to_copy;
            total = bytes_to_copy;

            rawdata += bytes_to_copy;
            remaining -= bytes_to_copy;

            upload_ctx->line_bytes_read += bytes_to_copy;
        }

        if (remaining < 1)
        {
            upload_ctx->lines_read ++;
            upload_ctx->line_bytes_read = 0;
        }
    }

    return total;
}
静态大小负载源(void*ptr、size\t size、size\t nmemb、void*userp)
{
结构上传状态*上传状态=(结构上传状态*)用户p;
无符号字符*buf=(无符号字符*)ptr;
可用大小=(大小*nmemb);
总尺寸=0;
而(可用>0)
{
wchar_t*data=payload_text[上传ctx->行读取];
如果(!数据)中断;
无符号字符*原始数据=(无符号字符*)数据;
剩余大小=(wcslen(数据)*sizeof(wchar\u t))-upload\u ctx->line\u bytes\u read;
而((剩余>0)和&(可用>0))
{
大小\u t字节\u至\u副本=min(剩余,可用);
memcpy(buf、rawdata、字节到字节拷贝);
buf+=字节到字节拷贝;
可用-=字节到副本;
总计=字节到副本;
rawdata+=字节到字节拷贝;
剩余-=字节到副本;
上传\u ctx->line\u bytes\u read+=bytes\u至\u copy;
}
如果(剩余<1)
{
上传ctx->lines\u read++;
上传\u ctx->行\u字节\u读取=0;
}
}
返回总数;
}

您使用了什么环境和编译器?还有,e和m看起来像什么?他们有没有比正常角色稍高的强调标记?