java.h 3.8 KB

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