nsUTF8ToUnicode.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsUCSupport.h"
  6. #include "nsUTF8ToUnicode.h"
  7. #include "mozilla/CheckedInt.h"
  8. #include "mozilla/SSE.h"
  9. #include "nsCharTraits.h"
  10. #include <algorithm>
  11. #define UNICODE_BYTE_ORDER_MARK 0xFEFF
  12. static char16_t* EmitSurrogatePair(uint32_t ucs4, char16_t* aDest)
  13. {
  14. NS_ASSERTION(ucs4 > 0xFFFF, "Should be a supplementary character");
  15. ucs4 -= 0x00010000;
  16. *aDest++ = 0xD800 | (0x000003FF & (ucs4 >> 10));
  17. *aDest++ = 0xDC00 | (0x000003FF & ucs4);
  18. return aDest;
  19. }
  20. //----------------------------------------------------------------------
  21. // Class nsUTF8ToUnicode [implementation]
  22. nsUTF8ToUnicode::nsUTF8ToUnicode()
  23. : nsBasicDecoderSupport()
  24. {
  25. Reset();
  26. }
  27. //----------------------------------------------------------------------
  28. // Subclassing of nsTableDecoderSupport class [implementation]
  29. /**
  30. * Normally the maximum length of the output of the UTF8 decoder in UTF16
  31. * code units is the same as the length of the input in UTF8 code units,
  32. * since 1-byte, 2-byte and 3-byte UTF-8 sequences decode to a single
  33. * UTF-16 character, and 4-byte UTF-8 sequences decode to a surrogate pair.
  34. *
  35. * However, there is an edge case where the output can be longer than the
  36. * input: if the previous buffer ended with an incomplete multi-byte
  37. * sequence and this buffer does not begin with a valid continuation
  38. * byte, we will return NS_ERROR_ILLEGAL_INPUT and the caller may insert a
  39. * replacement character in the output buffer which corresponds to no
  40. * character in the input buffer. So in the worst case the destination
  41. * will need to be one code unit longer than the source.
  42. * See bug 301797.
  43. */
  44. NS_IMETHODIMP nsUTF8ToUnicode::GetMaxLength(const char * aSrc,
  45. int32_t aSrcLength,
  46. int32_t * aDestLength)
  47. {
  48. mozilla::CheckedInt32 length = aSrcLength;
  49. length += 1;
  50. if (!length.isValid()) {
  51. return NS_ERROR_OUT_OF_MEMORY;
  52. }
  53. *aDestLength = length.value();
  54. return NS_OK;
  55. }
  56. //----------------------------------------------------------------------
  57. // Subclassing of nsBasicDecoderSupport class [implementation]
  58. NS_IMETHODIMP nsUTF8ToUnicode::Reset()
  59. {
  60. mUcs4 = 0; // cached Unicode character
  61. mState = 0; // cached expected number of octets after the current octet
  62. // until the beginning of the next UTF8 character sequence
  63. mBytes = 1; // cached expected number of octets in the current sequence
  64. mFirst = true;
  65. return NS_OK;
  66. }
  67. //----------------------------------------------------------------------
  68. // Subclassing of nsBasicDecoderSupport class [implementation]
  69. // Fast ASCII -> UTF16 inner loop implementations
  70. //
  71. // Convert_ascii_run will update src and dst to the new values, and
  72. // len must be the maximum number ascii chars that it would be valid
  73. // to take from src and place into dst. (That is, the minimum of the
  74. // number of bytes left in src and the number of unichars available in
  75. // dst.)
  76. #if defined(__arm__) || defined(_M_ARM)
  77. // on ARM, do extra work to avoid byte/halfword reads/writes by
  78. // reading/writing a word at a time for as long as we can
  79. static inline void
  80. Convert_ascii_run (const char *&src,
  81. char16_t *&dst,
  82. int32_t len)
  83. {
  84. const uint32_t *src32;
  85. uint32_t *dst32;
  86. // with some alignments, we'd never actually break out of the slow loop, so
  87. // check and do the faster slow loop
  88. if ((((NS_PTR_TO_UINT32(dst) & 3) == 0) && ((NS_PTR_TO_UINT32(src) & 1) == 0)) ||
  89. (((NS_PTR_TO_UINT32(dst) & 3) == 2) && ((NS_PTR_TO_UINT32(src) & 1) == 1)))
  90. {
  91. while (((NS_PTR_TO_UINT32(src) & 3) ||
  92. (NS_PTR_TO_UINT32(dst) & 3)) &&
  93. len > 0)
  94. {
  95. if (*src & 0x80U)
  96. return;
  97. *dst++ = (char16_t) *src++;
  98. len--;
  99. }
  100. } else {
  101. goto finish;
  102. }
  103. // then go 4 bytes at a time
  104. src32 = (const uint32_t*) src;
  105. dst32 = (uint32_t*) dst;
  106. while (len > 4) {
  107. uint32_t in = *src32++;
  108. if (in & 0x80808080U) {
  109. src32--;
  110. break;
  111. }
  112. *dst32++ = ((in & 0x000000ff) >> 0) | ((in & 0x0000ff00) << 8);
  113. *dst32++ = ((in & 0x00ff0000) >> 16) | ((in & 0xff000000) >> 8);
  114. len -= 4;
  115. }
  116. src = (const char *) src32;
  117. dst = (char16_t *) dst32;
  118. finish:
  119. while (len-- > 0 && (*src & 0x80U) == 0) {
  120. *dst++ = (char16_t) *src++;
  121. }
  122. }
  123. #else
  124. #ifdef MOZILLA_MAY_SUPPORT_SSE2
  125. namespace mozilla {
  126. namespace SSE2 {
  127. void Convert_ascii_run(const char *&src, char16_t *&dst, int32_t len);
  128. } // namespace SSE2
  129. } // namespace mozilla
  130. #endif
  131. static inline void
  132. Convert_ascii_run (const char *&src,
  133. char16_t *&dst,
  134. int32_t len)
  135. {
  136. #ifdef MOZILLA_MAY_SUPPORT_SSE2
  137. if (mozilla::supports_sse2()) {
  138. mozilla::SSE2::Convert_ascii_run(src, dst, len);
  139. return;
  140. }
  141. #endif
  142. while (len-- > 0 && (*src & 0x80U) == 0) {
  143. *dst++ = (char16_t) *src++;
  144. }
  145. }
  146. #endif
  147. NS_IMETHODIMP nsUTF8ToUnicode::Convert(const char * aSrc,
  148. int32_t * aSrcLength,
  149. char16_t * aDest,
  150. int32_t * aDestLength)
  151. {
  152. uint32_t aSrcLen = (uint32_t) (*aSrcLength);
  153. uint32_t aDestLen = (uint32_t) (*aDestLength);
  154. const char *in, *inend;
  155. inend = aSrc + aSrcLen;
  156. char16_t *out, *outend;
  157. outend = aDest + aDestLen;
  158. nsresult res = NS_OK; // conversion result
  159. out = aDest;
  160. if (mState == 0xFF) {
  161. // Emit supplementary character left over from previous iteration. It is
  162. // caller's responsibility to keep a sufficient buffer.
  163. if (aDestLen < 2) {
  164. *aSrcLength = *aDestLength = 0;
  165. return NS_OK_UDEC_MOREOUTPUT;
  166. }
  167. out = EmitSurrogatePair(mUcs4, out);
  168. mUcs4 = 0;
  169. mState = 0;
  170. mBytes = 1;
  171. mFirst = false;
  172. }
  173. // alias these locally for speed
  174. int32_t mUcs4 = this->mUcs4;
  175. uint8_t mState = this->mState;
  176. uint8_t mBytes = this->mBytes;
  177. bool mFirst = this->mFirst;
  178. // Set mFirst to false now so we don't have to every time through the ASCII
  179. // branch within the loop.
  180. if (mFirst && aSrcLen && (0 == (0x80 & (*aSrc))))
  181. mFirst = false;
  182. for (in = aSrc; ((in < inend) && (out < outend)); ++in) {
  183. uint8_t c = *in;
  184. if (0 == mState) {
  185. // When mState is zero we expect either a US-ASCII character or a
  186. // multi-octet sequence.
  187. if (c < 0x80) { // 00..7F
  188. int32_t max_loops = std::min(inend - in, outend - out);
  189. Convert_ascii_run(in, out, max_loops);
  190. --in; // match the rest of the cases
  191. mBytes = 1;
  192. } else if (c < 0xC2) { // C0/C1
  193. // Overlong 2 octet sequence
  194. if (mErrBehavior == kOnError_Signal) {
  195. res = NS_ERROR_ILLEGAL_INPUT;
  196. break;
  197. }
  198. *out++ = UCS2_REPLACEMENT_CHAR;
  199. mFirst = false;
  200. } else if (c < 0xE0) { // C2..DF
  201. // First octet of 2 octet sequence
  202. mUcs4 = c;
  203. mUcs4 = (mUcs4 & 0x1F) << 6;
  204. mState = 1;
  205. mBytes = 2;
  206. } else if (c < 0xF0) { // E0..EF
  207. // First octet of 3 octet sequence
  208. mUcs4 = c;
  209. mUcs4 = (mUcs4 & 0x0F) << 12;
  210. mState = 2;
  211. mBytes = 3;
  212. } else if (c < 0xF5) { // F0..F4
  213. // First octet of 4 octet sequence
  214. mUcs4 = c;
  215. mUcs4 = (mUcs4 & 0x07) << 18;
  216. mState = 3;
  217. mBytes = 4;
  218. } else { // F5..FF
  219. /* Current octet is neither in the US-ASCII range nor a legal first
  220. * octet of a multi-octet sequence.
  221. */
  222. if (mErrBehavior == kOnError_Signal) {
  223. /* Return an error condition. Caller is responsible for flushing and
  224. * refilling the buffer and resetting state.
  225. */
  226. res = NS_ERROR_ILLEGAL_INPUT;
  227. break;
  228. }
  229. *out++ = UCS2_REPLACEMENT_CHAR;
  230. mFirst = false;
  231. }
  232. } else {
  233. // When mState is non-zero, we expect a continuation of the multi-octet
  234. // sequence
  235. if (0x80 == (0xC0 & c)) {
  236. if (mState > 1) {
  237. // If we are here, all possibilities are:
  238. // mState == 2 && mBytes == 3 ||
  239. // mState == 2 && mBytes == 4 ||
  240. // mState == 3 && mBytes == 4
  241. if ((mBytes == 3 && ((!mUcs4 && c < 0xA0) || // E0 80..9F
  242. (mUcs4 == 0xD000 && c > 0x9F))) || // ED A0..BF
  243. (mState == 3 && ((!mUcs4 && c < 0x90) || // F0 80..8F
  244. (mUcs4 == 0x100000 && c > 0x8F)))) {// F4 90..BF
  245. // illegal sequences or sequences converted into illegal ranges.
  246. in--;
  247. if (mErrBehavior == kOnError_Signal) {
  248. res = NS_ERROR_ILLEGAL_INPUT;
  249. break;
  250. }
  251. *out++ = UCS2_REPLACEMENT_CHAR;
  252. mState = 0;
  253. mFirst = false;
  254. continue;
  255. }
  256. }
  257. // Legal continuation.
  258. uint32_t shift = (mState - 1) * 6;
  259. uint32_t tmp = c;
  260. tmp = (tmp & 0x0000003FL) << shift;
  261. mUcs4 |= tmp;
  262. if (0 == --mState) {
  263. /* End of the multi-octet sequence. mUcs4 now contains the final
  264. * Unicode codepoint to be output
  265. */
  266. if (mUcs4 > 0xFFFF) {
  267. // mUcs4 is in the range 0x10000 - 0x10FFFF. Output a UTF-16 pair
  268. if (out + 2 > outend) {
  269. // insufficient space left in the buffer. Keep mUcs4 for the
  270. // next iteration.
  271. mState = 0xFF;
  272. ++in;
  273. res = NS_OK_UDEC_MOREOUTPUT;
  274. break;
  275. }
  276. out = EmitSurrogatePair(mUcs4, out);
  277. } else if (UNICODE_BYTE_ORDER_MARK != mUcs4 || !mFirst) {
  278. // Don't output the BOM only if it is the first character
  279. *out++ = mUcs4;
  280. }
  281. //initialize UTF8 cache
  282. mUcs4 = 0;
  283. mState = 0;
  284. mBytes = 1;
  285. mFirst = false;
  286. }
  287. } else {
  288. /* ((0xC0 & c != 0x80) && (mState != 0))
  289. *
  290. * Incomplete multi-octet sequence. Unconsume this
  291. * octet and return an error condition. Caller is responsible
  292. * for flushing and refilling the buffer and resetting state.
  293. */
  294. in--;
  295. if (mErrBehavior == kOnError_Signal) {
  296. res = NS_ERROR_ILLEGAL_INPUT;
  297. break;
  298. }
  299. *out++ = UCS2_REPLACEMENT_CHAR;
  300. mState = 0;
  301. mFirst = false;
  302. }
  303. }
  304. }
  305. // output not finished, output buffer too short
  306. if ((NS_OK == res) && (in < inend) && (out >= outend))
  307. res = NS_OK_UDEC_MOREOUTPUT;
  308. // last UCS4 is incomplete, make sure the caller
  309. // returns with properly aligned continuation of the buffer
  310. if ((NS_OK == res) && (mState != 0))
  311. res = NS_OK_UDEC_MOREINPUT;
  312. *aSrcLength = in - aSrc;
  313. *aDestLength = out - aDest;
  314. this->mUcs4 = mUcs4;
  315. this->mState = mState;
  316. this->mBytes = mBytes;
  317. this->mFirst = mFirst;
  318. return(res);
  319. }