2
0

unicode.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Dealing with Unicode
  3. *
  4. * Copyright (C) 2013 Red Hat, Inc.
  5. *
  6. * Authors:
  7. * Markus Armbruster <armbru@redhat.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2 or
  10. * later. See the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "qemu/unicode.h"
  14. static bool is_valid_codepoint(int codepoint)
  15. {
  16. if (codepoint > 0x10FFFFu) {
  17. return false; /* beyond Unicode range */
  18. }
  19. if ((codepoint >= 0xFDD0 && codepoint <= 0xFDEF)
  20. || (codepoint & 0xFFFE) == 0xFFFE) {
  21. return false; /* noncharacter */
  22. }
  23. if (codepoint >= 0xD800 && codepoint <= 0xDFFF) {
  24. return false; /* surrogate code point */
  25. }
  26. return true;
  27. }
  28. /**
  29. * mod_utf8_codepoint:
  30. * @s: string encoded in modified UTF-8
  31. * @n: maximum number of bytes to read from @s, if less than 6
  32. * @end: set to end of sequence on return
  33. *
  34. * Convert the modified UTF-8 sequence at the start of @s. Modified
  35. * UTF-8 is exactly like UTF-8, except U+0000 is encoded as
  36. * "\xC0\x80".
  37. *
  38. * If @n is zero or @s points to a zero byte, the sequence is invalid,
  39. * and @end is set to @s.
  40. *
  41. * If @s points to an impossible byte (0xFE or 0xFF) or a continuation
  42. * byte, the sequence is invalid, and @end is set to @s + 1
  43. *
  44. * Else, the first byte determines how many continuation bytes are
  45. * expected. If there are fewer, the sequence is invalid, and @end is
  46. * set to @s + 1 + actual number of continuation bytes. Else, the
  47. * sequence is well-formed, and @end is set to @s + 1 + expected
  48. * number of continuation bytes.
  49. *
  50. * A well-formed sequence is valid unless it encodes a codepoint
  51. * outside the Unicode range U+0000..U+10FFFF, one of Unicode's 66
  52. * noncharacters, a surrogate codepoint, or is overlong. Except the
  53. * overlong sequence "\xC0\x80" is valid.
  54. *
  55. * Conversion succeeds if and only if the sequence is valid.
  56. *
  57. * Returns: the Unicode codepoint on success, -1 on failure.
  58. */
  59. int mod_utf8_codepoint(const char *s, size_t n, char **end)
  60. {
  61. static int min_cp[5] = { 0x80, 0x800, 0x10000, 0x200000, 0x4000000 };
  62. const unsigned char *p;
  63. unsigned byte, mask, len, i;
  64. int cp;
  65. if (n == 0 || *s == 0) {
  66. /* empty sequence */
  67. *end = (char *)s;
  68. return -1;
  69. }
  70. p = (const unsigned char *)s;
  71. byte = *p++;
  72. if (byte < 0x80) {
  73. cp = byte; /* one byte sequence */
  74. } else if (byte >= 0xFE) {
  75. cp = -1; /* impossible bytes 0xFE, 0xFF */
  76. } else if ((byte & 0x40) == 0) {
  77. cp = -1; /* unexpected continuation byte */
  78. } else {
  79. /* multi-byte sequence */
  80. len = 0;
  81. for (mask = 0x80; byte & mask; mask >>= 1) {
  82. len++;
  83. }
  84. assert(len > 1 && len < 7);
  85. cp = byte & (mask - 1);
  86. for (i = 1; i < len; i++) {
  87. byte = i < n ? *p : 0;
  88. if ((byte & 0xC0) != 0x80) {
  89. cp = -1; /* continuation byte missing */
  90. goto out;
  91. }
  92. p++;
  93. cp <<= 6;
  94. cp |= byte & 0x3F;
  95. }
  96. if (!is_valid_codepoint(cp)) {
  97. cp = -1;
  98. } else if (cp < min_cp[len - 2] && !(cp == 0 && len == 2)) {
  99. cp = -1; /* overlong, not \xC0\x80 */
  100. }
  101. }
  102. out:
  103. *end = (char *)p;
  104. return cp;
  105. }
  106. /**
  107. * mod_utf8_encode:
  108. * @buf: Destination buffer
  109. * @bufsz: size of @buf, at least 5.
  110. * @codepoint: Unicode codepoint to encode
  111. *
  112. * Convert Unicode codepoint @codepoint to modified UTF-8.
  113. *
  114. * Returns: the length of the UTF-8 sequence on success, -1 when
  115. * @codepoint is invalid.
  116. */
  117. ssize_t mod_utf8_encode(char buf[], size_t bufsz, int codepoint)
  118. {
  119. assert(bufsz >= 5);
  120. if (!is_valid_codepoint(codepoint)) {
  121. return -1;
  122. }
  123. if (codepoint > 0 && codepoint <= 0x7F) {
  124. buf[0] = codepoint & 0x7F;
  125. buf[1] = 0;
  126. return 1;
  127. }
  128. if (codepoint <= 0x7FF) {
  129. buf[0] = 0xC0 | ((codepoint >> 6) & 0x1F);
  130. buf[1] = 0x80 | (codepoint & 0x3F);
  131. buf[2] = 0;
  132. return 2;
  133. }
  134. if (codepoint <= 0xFFFF) {
  135. buf[0] = 0xE0 | ((codepoint >> 12) & 0x0F);
  136. buf[1] = 0x80 | ((codepoint >> 6) & 0x3F);
  137. buf[2] = 0x80 | (codepoint & 0x3F);
  138. buf[3] = 0;
  139. return 3;
  140. }
  141. buf[0] = 0xF0 | ((codepoint >> 18) & 0x07);
  142. buf[1] = 0x80 | ((codepoint >> 12) & 0x3F);
  143. buf[2] = 0x80 | ((codepoint >> 6) & 0x3F);
  144. buf[3] = 0x80 | (codepoint & 0x3F);
  145. buf[4] = 0;
  146. return 4;
  147. }