cm_utf8.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. /*
  5. RFC 3629
  6. 07-bit: 0xxxxxxx
  7. 11-bit: 110xxxxx 10xxxxxx
  8. 16-bit: 1110xxxx 10xxxxxx 10xxxxxx
  9. 21-bit: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
  10. Pre-RFC Compatibility
  11. 26-bit: 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  12. 31-bit: 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
  13. */
  14. /* Number of leading ones before a zero in the byte. */
  15. unsigned char const cm_utf8_ones[256] = {
  16. 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,
  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, 1, 1,
  21. 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,
  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, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  24. 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,
  25. 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 7, 8
  26. };
  27. /* Mask away control bits from bytes with n leading ones. */
  28. static unsigned char const cm_utf8_mask[7] = { 0xEF, 0x3F, 0x1F, 0x0F,
  29. 0x07, 0x03, 0x01 };
  30. /* Minimum allowed value when first byte has n leading ones. */
  31. static unsigned int const cm_utf8_min[7] = {
  32. 0, 0, 1u << 7, 1u << 11, 1u << 16, 1u << 21, 1u << 26 /*, 1u<<31 */
  33. };
  34. const char* cm_utf8_decode_character(const char* first, const char* last,
  35. unsigned int* pc)
  36. {
  37. /* Count leading ones in the first byte. */
  38. unsigned char c = (unsigned char)*first++;
  39. unsigned char const ones = cm_utf8_ones[c];
  40. switch (ones) {
  41. case 0:
  42. *pc = c;
  43. return first; /* One-byte character. */
  44. case 1:
  45. case 7:
  46. case 8:
  47. return 0; /* Invalid leading byte. */
  48. default:
  49. break;
  50. }
  51. /* Extract bits from this multi-byte character. */
  52. {
  53. unsigned int uc = c & cm_utf8_mask[ones];
  54. int left;
  55. for (left = ones - 1; left && first != last; --left) {
  56. c = (unsigned char)*first++;
  57. if (cm_utf8_ones[c] != 1) {
  58. return 0;
  59. }
  60. uc = (uc << 6) | (c & cm_utf8_mask[1]);
  61. }
  62. if (left > 0 || uc < cm_utf8_min[ones]) {
  63. return 0;
  64. }
  65. *pc = uc;
  66. return first;
  67. }
  68. }