utf8.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (C) 1999-2001, 2004, 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. * UTF-8
  21. */
  22. /* Specification: RFC 3629 */
  23. static int
  24. utf8_mbtowc (conv_t conv, ucs4_t *pwc, const unsigned char *s, size_t n)
  25. {
  26. unsigned char c = s[0];
  27. if (c < 0x80) {
  28. *pwc = c;
  29. return 1;
  30. } else if (c < 0xc2) {
  31. return RET_ILSEQ;
  32. } else if (c < 0xe0) {
  33. if (n < 2)
  34. return RET_TOOFEW(0);
  35. if (!((s[1] ^ 0x80) < 0x40))
  36. return RET_ILSEQ;
  37. *pwc = ((ucs4_t) (c & 0x1f) << 6)
  38. | (ucs4_t) (s[1] ^ 0x80);
  39. return 2;
  40. } else if (c < 0xf0) {
  41. if (n < 3)
  42. return RET_TOOFEW(0);
  43. if (!((s[1] ^ 0x80) < 0x40 && (s[2] ^ 0x80) < 0x40
  44. && (c >= 0xe1 || s[1] >= 0xa0)
  45. && (c != 0xed || 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. && (c < 0xf4 || (c == 0xf4 && s[1] < 0x90))))
  58. return RET_ILSEQ;
  59. *pwc = ((ucs4_t) (c & 0x07) << 18)
  60. | ((ucs4_t) (s[1] ^ 0x80) << 12)
  61. | ((ucs4_t) (s[2] ^ 0x80) << 6)
  62. | (ucs4_t) (s[3] ^ 0x80);
  63. return 4;
  64. } else
  65. return RET_ILSEQ;
  66. }
  67. static int
  68. utf8_wctomb (conv_t conv, unsigned char *r, ucs4_t wc, size_t n) /* n == 0 is acceptable */
  69. {
  70. int count;
  71. if (wc < 0x80)
  72. count = 1;
  73. else if (wc < 0x800)
  74. count = 2;
  75. else if (wc < 0x10000) {
  76. if (wc < 0xd800 || wc >= 0xe000)
  77. count = 3;
  78. else
  79. return RET_ILUNI;
  80. } else if (wc < 0x110000)
  81. count = 4;
  82. else
  83. return RET_ILUNI;
  84. if (n < count)
  85. return RET_TOOSMALL;
  86. switch (count) { /* note: code falls through cases! */
  87. case 4: r[3] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x10000;
  88. case 3: r[2] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0x800;
  89. case 2: r[1] = 0x80 | (wc & 0x3f); wc = wc >> 6; wc |= 0xc0;
  90. case 1: r[0] = wc;
  91. }
  92. return count;
  93. }