cp936ext.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (C) 1999-2001 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. * CP936 extensions
  22. */
  23. static const unsigned short cp936ext_2uni_pagea6[181-159] = {
  24. /* 0xa6 */
  25. 0xfe35,
  26. 0xfe36, 0xfe39, 0xfe3a, 0xfe3f, 0xfe40, 0xfe3d, 0xfe3e, 0xfe41,
  27. 0xfe42, 0xfe43, 0xfe44, 0xfffd, 0xfffd, 0xfe3b, 0xfe3c, 0xfe37,
  28. 0xfe38, 0xfe31, 0xfffd, 0xfe33, 0xfe34,
  29. };
  30. static const unsigned short cp936ext_2uni_pagea8[128-122] = {
  31. /* 0xa8 */
  32. 0x0251, 0xfffd, 0x0144, 0x0148, 0xfffd, 0x0261,
  33. };
  34. static int
  35. cp936ext_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  36. {
  37. unsigned char c1 = s[0];
  38. if ((c1 == 0xa6) || (c1 == 0xa8)) {
  39. if (n >= 2) {
  40. unsigned char c2 = s[1];
  41. if ((c2 >= 0x40 && c2 < 0x7f) || (c2 >= 0x80 && c2 < 0xff)) {
  42. unsigned int i = 190 * (c1 - 0x81) + (c2 - (c2 >= 0x80 ? 0x41 : 0x40));
  43. unsigned short wc = 0xfffd;
  44. if (i < 7410) {
  45. if (i >= 7189 && i < 7211)
  46. wc = cp936ext_2uni_pagea6[i-7189];
  47. } else {
  48. if (i >= 7532 && i < 7538)
  49. wc = cp936ext_2uni_pagea8[i-7532];
  50. }
  51. if (wc != 0xfffd) {
  52. *pwc = (ucs4_t) wc;
  53. return 2;
  54. }
  55. }
  56. return RET_ILSEQ;
  57. }
  58. return RET_TOOFEW(0);
  59. }
  60. return RET_ILSEQ;
  61. }
  62. static const unsigned short cp936ext_page01[16] = {
  63. 0x0000, 0x0000, 0x0000, 0x0000, 0xa8bd, 0x0000, 0x0000, 0x0000, /*0x40-0x47*/
  64. 0xa8be, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*0x48-0x4f*/
  65. };
  66. static const unsigned short cp936ext_page02[24] = {
  67. 0x0000, 0xa8bb, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*0x50-0x57*/
  68. 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*0x58-0x5f*/
  69. 0x0000, 0xa8c0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, /*0x60-0x67*/
  70. };
  71. static const unsigned short cp936ext_pagefe[24] = {
  72. 0x0000, 0xa6f2, 0x0000, 0xa6f4, 0xa6f5, 0xa6e0, 0xa6e1, 0xa6f0, /*0x30-0x37*/
  73. 0xa6f1, 0xa6e2, 0xa6e3, 0xa6ee, 0xa6ef, 0xa6e6, 0xa6e7, 0xa6e4, /*0x38-0x3f*/
  74. 0xa6e5, 0xa6e8, 0xa6e9, 0xa6ea, 0xa6eb, 0x0000, 0x0000, 0x0000, /*0x40-0x47*/
  75. };
  76. static int
  77. cp936ext_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  78. {
  79. if (n >= 2) {
  80. unsigned short c = 0;
  81. if (wc >= 0x0140 && wc < 0x0150)
  82. c = cp936ext_page01[wc-0x0140];
  83. else if (wc >= 0x0250 && wc < 0x0268)
  84. c = cp936ext_page02[wc-0x0250];
  85. else if (wc >= 0xfe30 && wc < 0xfe48)
  86. c = cp936ext_pagefe[wc-0xfe30];
  87. if (c != 0) {
  88. r[0] = (c >> 8); r[1] = (c & 0xff);
  89. return 2;
  90. }
  91. return RET_ILUNI;
  92. }
  93. return RET_TOOSMALL;
  94. }