Example #1
0
static size_t mac_japanese_char_pull(ucs2_t* out, const uint8_t* in, size_t* size)
{
  uint16_t c = in[0];

  if (c <= 0x7f) {
    *size = 1;
    *out = (c == 0x5c ? 0xa5 : c);
    return 1;
  } else if ((c >= 0x81 && c <= 0x9f) || (c >= 0xe0 && c <= 0xfc)) {
    if (*size >= 2) {
      uint8_t c2 = in[1];

      if ((c2 >= 0x40 && c2 <= 0x7e) || (c2 >= 0x80 && c2 <= 0xfc)) {
	*size = 2;
	if (c >= 0xf0) { /* user defined */
	  *out = 0xe000 + (c - 0xf0) * 188 + c2 - (c2 < 0x80 ? 0x40 : 0x41);
	  return 1;
	}
	c = (c << 8) + c2;
      } else {
	errno = EILSEQ;
	return (size_t)-1;
      }
    } else {
      errno = EINVAL;
      return (size_t)-1;
    }
  } else {
    *size = 1;
  }
  return cjk_char_pull(cjk_lookup(c, mac_japanese_2uni_index,
				  mac_japanese_2uni_charset),
		       out, mac_japanese_compose);
}
static size_t mac_chinese_simp_char_pull(ucs2_t* out, const u_int8_t* in, size_t* size)
{
  u_int16_t c = in[0];

  if (c <= 0x7f) {
    *size = 1;
    *out = c;
    return 1;
  } else if (c >= 0xa1 && c <= 0xfc) {
    if (*size >= 2) {
      u_int8_t c2 = in[1];

      if (c2 >= 0xa1 && c2 <= 0xfe) {
	*size = 2;
	c = (c << 8) + c2;
      } else {
	errno = EILSEQ;
	return (size_t)-1;
      }
    } else {
      errno = EINVAL;
      return (size_t)-1;
    }
  } else {
    *size = 1;
  }
  return cjk_char_pull(cjk_lookup(c, mac_chinese_simp_2uni_index,
				  mac_chinese_simp_2uni_charset),
		       out, mac_chinese_simp_compose);
}