cm_codecvt.cxx 6.7 KB

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