ICU:ucnv_convertEx – 即时检测编码错误

ICU: ucnv_convertEx – detect encoding error on the fly

是否可以在转换时使用 ICU 检测编码错误,或者是否有必要预先或post检查转换?

鉴于设置了从 UTF8 到 UTF32 的转换的初始化:

#include <stdio.h>
#include "unicode/ucnv.h"     /* C Converter API */

static void eval(UConverter* from, UConverter* to);

int main(int argc, char** argv)
{
    UConverter*  from;
    UConverter*  to;
    UErrorCode   status;

    /* Initialize converter from UTF8 to Unicode ___________________________*/
    status = U_ZERO_ERROR;
    from   = ucnv_open("UTF-8", &status);
    if( ! from || ! U_SUCCESS(status) ) return 1; 
    status = U_ZERO_ERROR;
    to     = ucnv_open("UTF32", &status);
    if( ! to || ! U_SUCCESS(status) ) return 1; 
    /*______________________________________________________________________*/

    eval(from, to);
    return 0;
}

然后,使用 ucnv_convertEx 通过

应用转换
static void eval(UConverter* from, UConverter* to) 
{
    UErrorCode  status = U_ZERO_ERROR;
    uint32_t    drain[1024];
    uint32_t*   drain_p = &drain[0];
    uint32_t*   p       = &drain[0];

    /* UTF8 sequence with error in third byte ______________________________*/
    const char  source[] = { "\xED\x8A\x0A\x0A" }; 
    const char* source_p = &source[0];

    ucnv_convertEx(to, from, (char**)&drain_p, (char*)&drain[1024],
                   &source_p, &source[5],
                   NULL, NULL, NULL, NULL, /* reset = */TRUE, /* flush = */TRUE,
                   &status);

    /* Print conversion result _____________________________________________*/
    printf("source_p: source + %i;\n", (int)(source_p - &source[0]));
    printf("status:   %s;\n", u_errorName(status));
    printf("drain:    (n=%i)[", (int)(drain_p - &drain[0]));
    for(p=&drain[0]; p != drain_p ; ++p) { printf("%06X ", (int)*p); }
    printf("]\n");
}

其中 source 包含不允许的 UTF8 代码单元序列,函数应该以某种方式报告错误。将上述片段存储在“test.c”中,并用

编译上述代码
$ gcc test.c $(icu-config --ldflags) -o test

./test 的输出是(令人惊讶的):

source_p: source + 5;
status:   U_ZERO_ERROR;
drain:    (n=5)[00FEFF 00FFFD 00000A 00000A 000000 ]

因此,没有检测到错误的明显迹象。是否可以比手动检查内容更优雅地进行错误检测?

正如@Eljay 在评论中建议的那样,您可以使用错误回调。你甚至不需要自己写,因为内置的 UCNV_TO_U_CALLBACK_STOP 会做你想做的事(即 return 任何坏字符都会失败)。

int TestIt()
{
  UConverter* utf8conv{};
  UConverter* utf32conv{};
  UErrorCode status{ U_ZERO_ERROR };

  utf8conv = ucnv_open("UTF8", &status);

  if (!U_SUCCESS(status))
  {
    return 1;
  }

  utf32conv = ucnv_open("UTF32", &status);

  if (!U_SUCCESS(status))
  {
    return 2;
  }

  const char source[] =  { "\xED\x8A\x0A\x0A" };
  uint32_t target[10]{ 0 };

  ucnv_setToUCallBack(utf8conv, UCNV_TO_U_CALLBACK_STOP, nullptr, 
    nullptr, nullptr, &status);

  if (!U_SUCCESS(status))
  {
    return 3;
  }

  auto sourcePtr = source;
  auto sourceEnd = source + ARRAYSIZE(source);
  auto targetPtr = target;
  auto targetEnd = reinterpret_cast<const char*>(target + ARRAYSIZE(target));

  ucnv_convertEx(utf32conv, utf8conv, reinterpret_cast<char**>(&targetPtr),
    targetEnd, &sourcePtr, sourceEnd, nullptr, nullptr, nullptr, nullptr, 
    TRUE, TRUE, &status);

  if (!U_SUCCESS(status))
  {
    return 4;
  }

  printf("Converted '%s' to '", source);
  for (auto start = target; start != targetPtr; start++)
  {
    printf("\x%x", *start);
  }
  printf("'\r\n");

  return 0;
}

对于无效的 Unicode 代码点,这应该 return 4,如果成功则打印出 UTF-32 值。我们不太可能从 ucnv_setToUCallBack 收到错误,但我们会检查以防万一。在上面的示例中,我们为之前的操作传递 nullptr,因为我们不关心它是什么,也不需要重置它。