dec_hanyu.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2001, 2005 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 Library General Public
  7. * License as published by the Free Software Foundation; either version 2
  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. * Library General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Library General Public
  16. * License along with the GNU LIBICONV Library; see the file COPYING.LIB.
  17. * If not, write to the Free Software Foundation, Inc., 51 Franklin Street,
  18. * Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /*
  21. * DEC-HANYU
  22. */
  23. static int
  24. dec_hanyu_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  25. {
  26. unsigned char c = *s;
  27. /* Code set 0 (ASCII) */
  28. if (c < 0x80)
  29. return ascii_mbtowc(conv,pwc,s,n);
  30. /* Code set 1 (CNS 11643-1992 Plane 1),
  31. Code set 2 (CNS 11643-1992 Plane 2),
  32. Code set 3 (CNS 11643-1992 Plane 3) */
  33. if (c >= 0xa1 && c < 0xff) {
  34. if (n < 2)
  35. return RET_TOOFEW(0);
  36. {
  37. unsigned char c2 = s[1];
  38. if (c == 0xc2 && c2 == 0xcb) {
  39. if (n < 4)
  40. return RET_TOOFEW(0);
  41. if (s[2] >= 0xa1 && s[2] < 0xff && s[3] >= 0xa1 && s[3] < 0xff) {
  42. unsigned char buf[2];
  43. int ret;
  44. buf[0] = s[2]-0x80; buf[1] = s[3]-0x80;
  45. ret = cns11643_3_mbtowc(conv,pwc,buf,2);
  46. if (ret != RET_ILSEQ) {
  47. if (ret != 2) abort();
  48. return 4;
  49. }
  50. }
  51. } else if (c2 >= 0xa1 && c2 < 0xff) {
  52. if (c != 0xc2 || c2 < 0xc2) {
  53. unsigned char buf[2];
  54. buf[0] = c-0x80; buf[1] = c2-0x80;
  55. return cns11643_1_mbtowc(conv,pwc,buf,2);
  56. }
  57. } else if (c2 >= 0x21 && c2 < 0x7f) {
  58. unsigned char buf[2];
  59. buf[0] = c-0x80; buf[1] = c2;
  60. return cns11643_2_mbtowc(conv,pwc,buf,2);
  61. }
  62. }
  63. }
  64. return RET_ILSEQ;
  65. }
  66. static int
  67. dec_hanyu_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  68. {
  69. unsigned char buf[3];
  70. int ret;
  71. /* Code set 0 (ASCII) */
  72. ret = ascii_wctomb(conv,r,wc,n);
  73. if (ret != RET_ILUNI)
  74. return ret;
  75. ret = cns11643_wctomb(conv,buf,wc,3);
  76. if (ret != RET_ILUNI) {
  77. if (ret != 3) abort();
  78. /* Code set 1 (CNS 11643-1992 Plane 1) */
  79. if (buf[0] == 1 && (buf[1] != 0x42 || buf[2] < 0x42)) {
  80. if (n < 2)
  81. return RET_TOOSMALL;
  82. r[0] = buf[1]+0x80;
  83. r[1] = buf[2]+0x80;
  84. return 2;
  85. }
  86. /* Code set 2 (CNS 11643-1992 Plane 2) */
  87. if (buf[0] == 2) {
  88. if (n < 2)
  89. return RET_TOOSMALL;
  90. r[0] = buf[1]+0x80;
  91. r[1] = buf[2];
  92. return 2;
  93. }
  94. /* Code set 3 (CNS 11643-1992 Plane 3) */
  95. if (buf[0] == 3) {
  96. if (n < 4)
  97. return RET_TOOSMALL;
  98. r[0] = 0xc2;
  99. r[1] = 0xcb;
  100. r[2] = buf[1]+0x80;
  101. r[3] = buf[2]+0x80;
  102. return 4;
  103. }
  104. }
  105. return RET_ILUNI;
  106. }