1
0

cm_utf8.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cm_utf8.h"
  4. #include <string.h>
  5. /*
  6. RFC 3629
  7. 07-bit: 0xxxxxxx
  8. 11-bit: 110xxxxx 10xxxxxx
  9. 16-bit: 1110xxxx 10xxxxxx 10xxxxxx
  10. 21-bit: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  11. Pre-RFC Compatibility
  12. 26-bit: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  13. 31-bit: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  14. */
  15. /* Number of leading ones before a zero in the byte. */
  16. unsigned char const cm_utf8_ones[256] = {
  17. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  18. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  19. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  20. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  21. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
  22. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  23. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  24. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  25. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  26. 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8
  27. };
  28. /* Mask away control bits from bytes with n leading ones. */
  29. static unsigned char const cm_utf8_mask[7] = { 0xEF, 0x3F, 0x1F, 0x0F,
  30. 0x07, 0x03, 0x01 };
  31. /* Minimum allowed value when first byte has n leading ones. */
  32. static unsigned int const cm_utf8_min[7] = {
  33. 0, 0, 1u << 7, 1u << 11, 1u << 16, 1u << 21, 1u << 26 /*, 1u<<31 */
  34. };
  35. const char* cm_utf8_decode_character(const char* first, const char* last,
  36. unsigned int* pc)
  37. {
  38. /* We need at least one byte. */
  39. if (first == last) {
  40. return 0;
  41. }
  42. /* Count leading ones in the first byte. */
  43. unsigned char c = (unsigned char)*first++;
  44. unsigned char const ones = cm_utf8_ones[c];
  45. switch (ones) {
  46. case 0:
  47. *pc = c;
  48. return first; /* One-byte character. */
  49. case 1:
  50. case 7:
  51. case 8:
  52. return 0; /* Invalid leading byte. */
  53. default:
  54. break;
  55. }
  56. /* Extract bits from this multi-byte character. */
  57. {
  58. unsigned int uc = c & cm_utf8_mask[ones];
  59. int left;
  60. for (left = ones - 1; left && first != last; --left) {
  61. c = (unsigned char)*first++;
  62. if (cm_utf8_ones[c] != 1) {
  63. return 0;
  64. }
  65. uc = (uc << 6) | (c & cm_utf8_mask[1]);
  66. }
  67. if (left > 0 || uc < cm_utf8_min[ones]) {
  68. return 0;
  69. }
  70. /* UTF-16 surrogate halves. */
  71. if (0xD800 <= uc && uc <= 0xDFFF) {
  72. return 0;
  73. }
  74. /* Invalid codepoints. */
  75. if (0x10FFFF < uc) {
  76. return 0;
  77. }
  78. *pc = uc;
  79. return first;
  80. }
  81. }
  82. int cm_utf8_is_valid(const char* s)
  83. {
  84. if (!s) {
  85. return 0;
  86. }
  87. const char* last = s + strlen(s);
  88. const char* pos = s;
  89. unsigned int pc;
  90. while (pos != last && (pos = cm_utf8_decode_character(pos, last, &pc))) {
  91. /* Nothing to do. */
  92. }
  93. return pos == last;
  94. }