intl/uconv/src/nsUTF8ToUnicode.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial