utf8.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Copyright (C) 1999-2001, 2004 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. * UTF-8
  22. */
  23. /* Specification: RFC 3629 */
  24. static int
  25. utf8_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, int n)
  26. {
  27. unsigned char c = s[0];
  28. if (c < 0x80) {
  29. *pwc = c;
  30. return 1;
  31. } else if (c < 0xc2) {
  32. return RET_ILSEQ;
  33. } else if (c < 0xe0) {
  34. if (n < 2)
  35. return RET_TOOFEW(0);
  36. if (!((s[1] ^ 0x80) < 0x40))
  37. return RET_ILSEQ;
  38. *pwc = ((ucs4_t) (c & 0x1f) << 6)
  39. | (ucs4_t) (s[1] ^ 0x80);
  40. return 2;
  41. } else if (c < 0xf0) {
  42. if (n < 3)
  43. return RET_TOOFEW(0);
  44. if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
  45. && (c >= 0xe1 || s[1] >= 0xa0)))
  46. return RET_ILSEQ;
  47. *pwc = ((ucs4_t) (c & 0x0f) << 12)
  48. | ((ucs4_t) (s[1] ^ 0x80) << 6)
  49. | (ucs4_t) (s[2] ^ 0x80);
  50. return 3;
  51. } else if (c < 0xf8 && sizeof(ucs4_t)*8 >= 32) {
  52. if (n < 4)
  53. return RET_TOOFEW(0);
  54. if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
  55. && (s[3] ^ 0x80) < 0x40
  56. && (c >= 0xf1 || s[1] >= 0x90)))
  57. return RET_ILSEQ;
  58. *pwc = ((ucs4_t) (c & 0x07) << 18)
  59. | ((ucs4_t) (s[1] ^ 0x80) << 12)
  60. | ((ucs4_t) (s[2] ^ 0x80) << 6)
  61. | (ucs4_t) (s[3] ^ 0x80);
  62. return 4;
  63. } else if (c < 0xfc && sizeof(ucs4_t)*8 >= 32) {
  64. if (n < 5)
  65. return RET_TOOFEW(0);
  66. if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
  67. && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
  68. && (c >= 0xf9 || s[1] >= 0x88)))
  69. return RET_ILSEQ;
  70. *pwc = ((ucs4_t) (c & 0x03) << 24)
  71. | ((ucs4_t) (s[1] ^ 0x80) << 18)
  72. | ((ucs4_t) (s[2] ^ 0x80) << 12)
  73. | ((ucs4_t) (s[3] ^ 0x80) << 6)
  74. | (ucs4_t) (s[4] ^ 0x80);
  75. return 5;
  76. } else if (c < 0xfe && sizeof(ucs4_t)*8 >= 32) {
  77. if (n < 6)
  78. return RET_TOOFEW(0);
  79. if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
  80. && (s[3] ^ 0x80) < 0x40 && (s[4] ^ 0x80) < 0x40
  81. && (s[5] ^ 0x80) < 0x40
  82. && (c >= 0xfd || s[1] >= 0x84)))
  83. return RET_ILSEQ;
  84. *pwc = ((ucs4_t) (c & 0x01) << 30)
  85. | ((ucs4_t) (s[1] ^ 0x80) << 24)
  86. | ((ucs4_t) (s[2] ^ 0x80) << 18)
  87. | ((ucs4_t) (s[3] ^ 0x80) << 12)
  88. | ((ucs4_t) (s[4] ^ 0x80) << 6)
  89. | (ucs4_t) (s[5] ^ 0x80);
  90. return 6;
  91. } else
  92. return RET_ILSEQ;
  93. }
  94. static int
  95. utf8_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, int n) /* n == 0 is acceptable */
  96. {
  97. int count;
  98. if (wc < 0x80)
  99. count = 1;
  100. else if (wc < 0x800)
  101. count = 2;
  102. else if (wc < 0x10000)
  103. count = 3;
  104. else if (wc < 0x200000)
  105. count = 4;
  106. else if (wc < 0x4000000)
  107. count = 5;
  108. else if (wc <= 0x7fffffff)
  109. count = 6;
  110. else
  111. return RET_ILUNI;
  112. if (n < count)
  113. return RET_TOOSMALL;
  114. switch (count) { /* note: code falls through cases! */
  115. case 6: r[5] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x4000000;
  116. case 5: r[4] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x200000;
  117. case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
  118. case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
  119. case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
  120. case 1: r[0] = wc;
  121. }
  122. return count;
  123. }