dec_hanyu.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (C) 2001, 2005, 2016 Free Software Foundation, Inc.
  3. * This file is part of the GNU LIBICONV Library.
  4. *
  5. * The GNU LIBICONV Library is free software; you can redistribute it
  6. * and/or modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either version 2.1
  8. * of the License, or (at your option) any later version.
  9. *
  10. * The GNU LIBICONV Library is distributed in the hope that it will be
  11. * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * DEC-HANYU
  21. */
  22. static int
  23. dec_hanyu_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  24. {
  25. unsigned char c = *s;
  26. /* Code set 0 (ASCII) */
  27. if (c < 0x80)
  28. return ascii_mbtowc(conv,pwc,s,n);
  29. /* Code set 1 (CNS 11643-1992 Plane 1),
  30. Code set 2 (CNS 11643-1992 Plane 2),
  31. Code set 3 (CNS 11643-1992 Plane 3) */
  32. if (c >= 0xa1 && c < 0xff) {
  33. if (n < 2)
  34. return RET_TOOFEW(0);
  35. {
  36. unsigned char c2 = s[1];
  37. if (c == 0xc2 && c2 == 0xcb) {
  38. if (n < 4)
  39. return RET_TOOFEW(0);
  40. if (s[2] >= 0xa1 && s[2] < 0xff && s[3] >= 0xa1 && s[3] < 0xff) {
  41. unsigned char buf[2];
  42. int ret;
  43. buf[0] = s[2]-0x80; buf[1] = s[3]-0x80;
  44. ret = cns11643_3_mbtowc(conv,pwc,buf,2);
  45. if (ret != RET_ILSEQ) {
  46. if (ret != 2) abort();
  47. return 4;
  48. }
  49. }
  50. } else if (c2 >= 0xa1 && c2 < 0xff) {
  51. if (c != 0xc2 || c2 < 0xc2) {
  52. unsigned char buf[2];
  53. buf[0] = c-0x80; buf[1] = c2-0x80;
  54. return cns11643_1_mbtowc(conv,pwc,buf,2);
  55. }
  56. } else if (c2 >= 0x21 && c2 < 0x7f) {
  57. unsigned char buf[2];
  58. buf[0] = c-0x80; buf[1] = c2;
  59. return cns11643_2_mbtowc(conv,pwc,buf,2);
  60. }
  61. }
  62. }
  63. return RET_ILSEQ;
  64. }
  65. static int
  66. dec_hanyu_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n)
  67. {
  68. unsigned char buf[3];
  69. int ret;
  70. /* Code set 0 (ASCII) */
  71. ret = ascii_wctomb(conv,r,wc,n);
  72. if (ret != RET_ILUNI)
  73. return ret;
  74. ret = cns11643_wctomb(conv,buf,wc,3);
  75. if (ret != RET_ILUNI) {
  76. if (ret != 3) abort();
  77. /* Code set 1 (CNS 11643-1992 Plane 1) */
  78. if (buf[0] == 1 && (buf[1] != 0x42 || buf[2] < 0x42)) {
  79. if (n < 2)
  80. return RET_TOOSMALL;
  81. r[0] = buf[1]+0x80;
  82. r[1] = buf[2]+0x80;
  83. return 2;
  84. }
  85. /* Code set 2 (CNS 11643-1992 Plane 2) */
  86. if (buf[0] == 2) {
  87. if (n < 2)
  88. return RET_TOOSMALL;
  89. r[0] = buf[1]+0x80;
  90. r[1] = buf[2];
  91. return 2;
  92. }
  93. /* Code set 3 (CNS 11643-1992 Plane 3) */
  94. if (buf[0] == 3) {
  95. if (n < 4)
  96. return RET_TOOSMALL;
  97. r[0] = 0xc2;
  98. r[1] = 0xcb;
  99. r[2] = buf[1]+0x80;
  100. r[3] = buf[2]+0x80;
  101. return 4;
  102. }
  103. }
  104. return RET_ILUNI;
  105. }