cm_codecvt.cxx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_codecvt.hxx"
  4. #if defined(_WIN32)
  5. # include <cassert>
  6. # include <cstring>
  7. # include <windows.h>
  8. # undef max
  9. # include "cmsys/Encoding.hxx"
  10. #endif
  11. #if defined(_WIN32)
  12. /* Number of leading ones before a zero in the byte (see cm_utf8.c). */
  13. extern "C" unsigned char const cm_utf8_ones[256];
  14. #endif
  15. codecvt::codecvt(Encoding e)
  16. #if defined(_WIN32)
  17. : m_codepage(0)
  18. #endif
  19. {
  20. switch (e) {
  21. case codecvt::ANSI:
  22. #if defined(_WIN32)
  23. m_noconv = false;
  24. m_codepage = CP_ACP;
  25. break;
  26. #endif
  27. // We don't know which ANSI encoding to use for other platforms than
  28. // Windows so we don't do any conversion there
  29. case codecvt::UTF8:
  30. case codecvt::UTF8_WITH_BOM:
  31. // Assume internal encoding is UTF-8
  32. case codecvt::None:
  33. // No encoding
  34. default:
  35. this->m_noconv = true;
  36. }
  37. }
  38. codecvt::~codecvt() = default;
  39. bool codecvt::do_always_noconv() const noexcept
  40. {
  41. return this->m_noconv;
  42. }
  43. std::codecvt_base::result codecvt::do_out(mbstate_t& state, const char* from,
  44. const char* from_end,
  45. const char*& from_next, char* to,
  46. char* to_end, char*& to_next) const
  47. {
  48. from_next = from;
  49. to_next = to;
  50. if (this->m_noconv) {
  51. return std::codecvt_base::noconv;
  52. }
  53. #if defined(_WIN32)
  54. // Use a const view of the state because we should not modify it until we
  55. // have fully processed and consume a byte (with sufficient space in the
  56. // output buffer). We call helpers to re-cast and modify the state
  57. State const& lstate = reinterpret_cast<State&>(state);
  58. while (from_next != from_end) {
  59. // Count leading ones in the bits of the next byte.
  60. unsigned char const ones =
  61. cm_utf8_ones[static_cast<unsigned char>(*from_next)];
  62. if (ones != 1 && lstate.buffered != 0) {
  63. // We have a buffered partial codepoint that we never completed.
  64. return std::codecvt_base::error;
  65. } else if (ones == 1 && lstate.buffered == 0) {
  66. // This is a continuation of a codepoint that never started.
  67. return std::codecvt_base::error;
  68. }
  69. // Compute the number of bytes in the current codepoint.
  70. int need = 0;
  71. switch (ones) {
  72. case 0: // 0xxx xxxx: new codepoint of size 1
  73. need = 1;
  74. break;
  75. case 1: // 10xx xxxx: continues a codepoint
  76. assert(lstate.size != 0);
  77. need = lstate.size;
  78. break;
  79. case 2: // 110x xxxx: new codepoint of size 2
  80. need = 2;
  81. break;
  82. case 3: // 1110 xxxx: new codepoint of size 3
  83. need = 3;
  84. break;
  85. case 4: // 1111 0xxx: new codepoint of size 4
  86. need = 4;
  87. break;
  88. default: // invalid byte
  89. return std::codecvt_base::error;
  90. }
  91. assert(need > 0);
  92. if (lstate.buffered + 1 == need) {
  93. // This byte completes a codepoint.
  94. std::codecvt_base::result decode_result =
  95. this->Decode(state, need, from_next, to_next, to_end);
  96. if (decode_result != std::codecvt_base::ok) {
  97. return decode_result;
  98. }
  99. } else {
  100. // This byte does not complete a codepoint.
  101. this->BufferPartial(state, need, from_next);
  102. }
  103. }
  104. return std::codecvt_base::ok;
  105. #else
  106. static_cast<void>(state);
  107. static_cast<void>(from);
  108. static_cast<void>(from_end);
  109. static_cast<void>(from_next);
  110. static_cast<void>(to);
  111. static_cast<void>(to_end);
  112. static_cast<void>(to_next);
  113. return std::codecvt_base::noconv;
  114. #endif
  115. }
  116. std::codecvt_base::result codecvt::do_unshift(mbstate_t& state, char* to,
  117. char* to_end,
  118. char*& to_next) const
  119. {
  120. to_next = to;
  121. if (this->m_noconv) {
  122. return std::codecvt_base::noconv;
  123. }
  124. #if defined(_WIN32)
  125. State& lstate = reinterpret_cast<State&>(state);
  126. if (lstate.buffered != 0) {
  127. return this->DecodePartial(state, to_next, to_end);
  128. }
  129. return std::codecvt_base::ok;
  130. #else
  131. static_cast<void>(state);
  132. static_cast<void>(to_end);
  133. return std::codecvt_base::ok;
  134. #endif
  135. }
  136. #if defined(_WIN32)
  137. std::codecvt_base::result codecvt::Decode(mbstate_t& state, int size,
  138. const char*& from_next,
  139. char*& to_next, char* to_end) const
  140. {
  141. State& lstate = reinterpret_cast<State&>(state);
  142. // Collect all the bytes for this codepoint.
  143. char buf[4];
  144. memcpy(buf, lstate.partial, lstate.buffered);
  145. buf[lstate.buffered] = *from_next;
  146. // Convert the encoding.
  147. wchar_t wbuf[2];
  148. int wlen =
  149. MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, buf, size, wbuf, 2);
  150. if (wlen <= 0) {
  151. return std::codecvt_base::error;
  152. }
  153. int tlen = WideCharToMultiByte(m_codepage, 0, wbuf, wlen, to_next,
  154. to_end - to_next, NULL, NULL);
  155. if (tlen <= 0) {
  156. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  157. return std::codecvt_base::partial;
  158. }
  159. return std::codecvt_base::error;
  160. }
  161. // Move past the now-consumed byte in the input buffer.
  162. ++from_next;
  163. // Move past the converted codepoint in the output buffer.
  164. to_next += tlen;
  165. // Re-initialize the state for the next codepoint to start.
  166. lstate = State();
  167. return std::codecvt_base::ok;
  168. }
  169. std::codecvt_base::result codecvt::DecodePartial(mbstate_t& state,
  170. char*& to_next,
  171. char* to_end) const
  172. {
  173. State& lstate = reinterpret_cast<State&>(state);
  174. // Try converting the partial codepoint.
  175. wchar_t wbuf[2];
  176. int wlen = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, lstate.partial,
  177. lstate.buffered, wbuf, 2);
  178. if (wlen <= 0) {
  179. return std::codecvt_base::error;
  180. }
  181. int tlen = WideCharToMultiByte(m_codepage, 0, wbuf, wlen, to_next,
  182. to_end - to_next, NULL, NULL);
  183. if (tlen <= 0) {
  184. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  185. return std::codecvt_base::partial;
  186. }
  187. return std::codecvt_base::error;
  188. }
  189. // Move past the converted codepoint in the output buffer.
  190. to_next += tlen;
  191. // Re-initialize the state for the next codepoint to start.
  192. lstate = State();
  193. return std::codecvt_base::ok;
  194. }
  195. void codecvt::BufferPartial(mbstate_t& state, int size,
  196. const char*& from_next) const
  197. {
  198. State& lstate = reinterpret_cast<State&>(state);
  199. // Save the byte in our buffer for later.
  200. lstate.partial[lstate.buffered++] = *from_next;
  201. lstate.size = size;
  202. // Move past the now-consumed byte in the input buffer.
  203. ++from_next;
  204. }
  205. #endif
  206. int codecvt::do_max_length() const noexcept
  207. {
  208. return 4;
  209. }
  210. int codecvt::do_encoding() const noexcept
  211. {
  212. return 0;
  213. }