charset.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. Copyright (C) 2010 Joseph A. Adams (joeyadams3.14159@gmail.com)
  3. All rights reserved.
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include "charset.h"
  21. bool utf8_allow_surrogates = false;
  22. bool utf8_validate(const char *str, size_t length)
  23. {
  24. const unsigned char *s = (const unsigned char*)str;
  25. const unsigned char *e = s + length;
  26. while (s < e) {
  27. unsigned char c = *s++;
  28. unsigned int len; /* number of bytes in sequence - 2 */
  29. /* If character is ASCII, move on. */
  30. if (c < 0x80)
  31. continue;
  32. if (s >= e)
  33. return false; /* Missing bytes in sequence. */
  34. if (c < 0xE0) {
  35. /* 2-byte sequence, U+0080 to U+07FF
  36. c must be 11000010 or higher
  37. s[0] must be 10xxxxxx */
  38. len = 0;
  39. if (c < 0xC2)
  40. return false;
  41. } else if (c < 0xF0) {
  42. /* 3-byte sequence, U+0800 to U+FFFF
  43. Note that the surrogate range is U+D800 to U+DFFF,
  44. and that U+FFFE and U+FFFF are illegal characters.
  45. c must be >= 11100000 (which it is)
  46. If c is 11100000, then s[0] must be >= 10100000
  47. If the global parameter utf8_allow_surrogates is false:
  48. If c is 11101101 and s[0] is >= 10100000,
  49. then this is a surrogate and we should fail.
  50. If c is 11101111, s[0] is 10111111, and s[1] >= 10111110,
  51. then this is an illegal character and we should fail.
  52. s[0] and s[1] must be 10xxxxxx */
  53. len = 1;
  54. if (c == 0xE0 && *s < 0xA0)
  55. return false;
  56. if (!utf8_allow_surrogates && c == 0xED && *s >= 0xA0)
  57. return false;
  58. if (c == 0xEF && s[0] == 0xBF && (s+1 >= e || s[1] >= 0xBE))
  59. return false;
  60. } else {
  61. /* 4-byte sequence, U+010000 to U+10FFFF
  62. c must be >= 11110000 (which it is) and <= 11110100
  63. If c is 11110000, then s[0] must be >= 10010000
  64. If c is 11110100, then s[0] must be < 10010000
  65. s[0], s[1], and s[2] must be 10xxxxxx */
  66. len = 2;
  67. if (c > 0xF4)
  68. return false;
  69. if (c == 0xF0 && *s < 0x90)
  70. return false;
  71. if (c == 0xF4 && *s >= 0x90)
  72. return false;
  73. }
  74. if (s + len >= e)
  75. return false; /* Missing bytes in sequence. */
  76. do {
  77. if ((*s++ & 0xC0) != 0x80)
  78. return false;
  79. } while (len--);
  80. }
  81. return true;
  82. }
  83. /*
  84. Note to future contributors: These routines are currently all under the
  85. MIT license. It would be nice to keep it that way :)
  86. */