c99.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. * C99
  22. * This is ASCII with \uXXXX and \UXXXXXXXX escape sequences, denoting Unicode
  23. * characters. See ISO/IEC 9899:1999, section 6.4.3.
  24. * The treatment of control characters in the range U+0080..U+009F is not
  25. * specified; we pass them through unmodified.
  26. */
  27. static int
  28. c99_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  29. {
  30. unsigned char c;
  31. ucs4_t wc;
  32. int i;
  33. c = s[0];
  34. if (c < 0xa0) {
  35. if (c != '\\') {
  36. *pwc = c;
  37. return 1;
  38. }
  39. if (n < 2)
  40. return RET_TOOFEW(0);
  41. c = s[1];
  42. if (c == 'u') {
  43. wc = 0;
  44. for (i = 2; i < 6; i++) {
  45. if (n <= i)
  46. return RET_TOOFEW(0);
  47. c = s[i];
  48. if (c >= '0' && c <= '9')
  49. c -= '0';
  50. else if (c >= 'A' && c <= 'Z')
  51. c -= 'A'-10;
  52. else if (c >= 'a' && c <= 'z')
  53. c -= 'a'-10;
  54. else
  55. goto simply_backslash;
  56. wc |= (ucs4_t) c << (4 * (5-i));
  57. }
  58. if ((wc >= 0x00a0 && !(wc >= 0xd800 && wc < 0xe000))
  59. || wc == 0x0024 || wc == 0x0040 || wc == 0x0060) {
  60. *pwc = wc;
  61. return 6;
  62. }
  63. } else if (c == 'U') {
  64. wc = 0;
  65. for (i = 2; i < 10; i++) {
  66. if (n <= i)
  67. return RET_TOOFEW(0);
  68. c = s[i];
  69. if (c >= '0' && c <= '9')
  70. c -= '0';
  71. else if (c >= 'A' && c <= 'Z')
  72. c -= 'A'-10;
  73. else if (c >= 'a' && c <= 'z')
  74. c -= 'a'-10;
  75. else
  76. goto simply_backslash;
  77. wc |= (ucs4_t) c << (4 * (9-i));
  78. }
  79. if ((wc >= 0x00a0 && !(wc >= 0xd800 && wc < 0xe000))
  80. || wc == 0x0024 || wc == 0x0040 || wc == 0x0060) {
  81. *pwc = wc;
  82. return 10;
  83. }
  84. } else
  85. goto simply_backslash;
  86. }
  87. return RET_ILSEQ;
  88. simply_backslash:
  89. *pwc = '\\';
  90. return 1;
  91. }
  92. static int
  93. c99_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n)
  94. {
  95. if (wc < 0xa0) {
  96. *r = wc;
  97. return 1;
  98. } else {
  99. int result;
  100. unsigned char u;
  101. if (wc < 0x10000) {
  102. result = 6;
  103. u = 'u';
  104. } else {
  105. result = 10;
  106. u = 'U';
  107. }
  108. if (n >= result) {
  109. int count;
  110. r[0] = '\\';
  111. r[1] = u;
  112. r += 2;
  113. for (count = result-3; count >= 0; count--) {
  114. unsigned int i = (wc >> (4*count)) & 0x0f;
  115. *r++ = (i < 10 ? '0'+i : 'a'-10+i);
  116. }
  117. return result;
  118. } else
  119. return RET_TOOSMALL;
  120. }
  121. }