java.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (C) 1999-2002 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. * JAVA
  22. * This is ISO 8859-1 with \uXXXX escape sequences, denoting Unicode BMP
  23. * characters. Consecutive pairs of \uXXXX escape sequences in the surrogate
  24. * range, as in UTF-16, denote Unicode characters outside the BMP.
  25. */
  26. static int
  27. java_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  28. {
  29. unsigned char c;
  30. ucs4_t wc, wc2;
  31. int i;
  32. c = s[0];
  33. if (c != '\\') {
  34. *pwc = c;
  35. return 1;
  36. }
  37. if (n < 2)
  38. return RET_TOOFEW(0);
  39. if (s[1] != 'u')
  40. goto simply_backslash;
  41. wc = 0;
  42. for (i = 2; i < 6; i++) {
  43. if (n <= i)
  44. return RET_TOOFEW(0);
  45. c = s[i];
  46. if (c >= '0' && c <= '9')
  47. c -= '0';
  48. else if (c >= 'A' && c <= 'Z')
  49. c -= 'A'-10;
  50. else if (c >= 'a' && c <= 'z')
  51. c -= 'a'-10;
  52. else
  53. goto simply_backslash;
  54. wc |= (ucs4_t) c << (4 * (5-i));
  55. }
  56. if (!(wc >= 0xd800 && wc < 0xe000)) {
  57. *pwc = wc;
  58. return 6;
  59. }
  60. if (wc >= 0xdc00)
  61. goto simply_backslash;
  62. if (n < 7)
  63. return RET_TOOFEW(0);
  64. if (s[6] != '\\')
  65. goto simply_backslash;
  66. if (n < 8)
  67. return RET_TOOFEW(0);
  68. if (s[7] != 'u')
  69. goto simply_backslash;
  70. wc2 = 0;
  71. for (i = 8; i < 12; i++) {
  72. if (n <= i)
  73. return RET_TOOFEW(0);
  74. c = s[i];
  75. if (c >= '0' && c <= '9')
  76. c -= '0';
  77. else if (c >= 'A' && c <= 'Z')
  78. c -= 'A'-10;
  79. else if (c >= 'a' && c <= 'z')
  80. c -= 'a'-10;
  81. else
  82. goto simply_backslash;
  83. wc2 |= (ucs4_t) c << (4 * (11-i));
  84. }
  85. if (!(wc2 >= 0xdc00 && wc2 < 0xe000))
  86. goto simply_backslash;
  87. *pwc = 0x10000 + ((wc - 0xd800) << 10) + (wc2 - 0xdc00);
  88. return 12;
  89. simply_backslash:
  90. *pwc = '\\';
  91. return 1;
  92. }
  93. static int
  94. java_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  95. {
  96. if (wc < 0x80) {
  97. *r = wc;
  98. return 1;
  99. } else if (wc < 0x10000) {
  100. if (n >= 6) {
  101. unsigned int i;
  102. r[0] = '\\';
  103. r[1] = 'u';
  104. i = (wc >> 12) & 0x0f; r[2] = (i < 10 ? '0'+i : 'a'-10+i);
  105. i = (wc >> 8) & 0x0f; r[3] = (i < 10 ? '0'+i : 'a'-10+i);
  106. i = (wc >> 4) & 0x0f; r[4] = (i < 10 ? '0'+i : 'a'-10+i);
  107. i = wc & 0x0f; r[5] = (i < 10 ? '0'+i : 'a'-10+i);
  108. return 6;
  109. } else
  110. return RET_TOOSMALL;
  111. } else if (wc < 0x110000) {
  112. if (n >= 12) {
  113. ucs4_t wc1 = 0xd800 + ((wc - 0x10000) >> 10);
  114. ucs4_t wc2 = 0xdc00 + ((wc - 0x10000) & 0x3ff);
  115. unsigned int i;
  116. r[0] = '\\';
  117. r[1] = 'u';
  118. i = (wc1 >> 12) & 0x0f; r[2] = (i < 10 ? '0'+i : 'a'-10+i);
  119. i = (wc1 >> 8) & 0x0f; r[3] = (i < 10 ? '0'+i : 'a'-10+i);
  120. i = (wc1 >> 4) & 0x0f; r[4] = (i < 10 ? '0'+i : 'a'-10+i);
  121. i = wc1 & 0x0f; r[5] = (i < 10 ? '0'+i : 'a'-10+i);
  122. r[6] = '\\';
  123. r[7] = 'u';
  124. i = (wc2 >> 12) & 0x0f; r[8] = (i < 10 ? '0'+i : 'a'-10+i);
  125. i = (wc2 >> 8) & 0x0f; r[9] = (i < 10 ? '0'+i : 'a'-10+i);
  126. i = (wc2 >> 4) & 0x0f; r[10] = (i < 10 ? '0'+i : 'a'-10+i);
  127. i = wc2 & 0x0f; r[11] = (i < 10 ? '0'+i : 'a'-10+i);
  128. return 12;
  129. } else
  130. return RET_TOOSMALL;
  131. }
  132. return RET_ILUNI;
  133. }