2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-26 17:38:20 -08:00
|
|
|
#include "nsUTF16ToUnicode.h"
|
2009-02-22 02:08:27 -08:00
|
|
|
#include "nsCharTraits.h"
|
2014-02-26 17:42:29 -08:00
|
|
|
#include "mozilla/Endian.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-26 17:38:20 -08:00
|
|
|
enum {
|
|
|
|
STATE_NORMAL = 0,
|
|
|
|
STATE_HALF_CODE_POINT = 1,
|
|
|
|
STATE_FIRST_CALL = 2,
|
|
|
|
STATE_SECOND_BYTE = STATE_FIRST_CALL | STATE_HALF_CODE_POINT,
|
|
|
|
STATE_ODD_SURROGATE_PAIR = 4
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-26 17:38:20 -08:00
|
|
|
nsresult
|
|
|
|
nsUTF16ToUnicodeBase::UTF16ConvertToUnicode(const char * aSrc,
|
|
|
|
int32_t * aSrcLength,
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t * aDest,
|
2012-11-26 17:38:20 -08:00
|
|
|
int32_t * aDestLength,
|
|
|
|
bool aSwapBytes)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
const char* src = aSrc;
|
|
|
|
const char* srcEnd = aSrc + *aSrcLength;
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t* dest = aDest;
|
|
|
|
char16_t* destEnd = aDest + *aDestLength;
|
|
|
|
char16_t oddHighSurrogate;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-11-26 17:38:20 -08:00
|
|
|
switch(mState) {
|
2012-11-26 17:38:20 -08:00
|
|
|
case STATE_FIRST_CALL:
|
2011-02-20 01:04:28 -08:00
|
|
|
NS_ASSERTION(*aSrcLength > 1, "buffer too short");
|
2007-03-22 10:30:00 -07:00
|
|
|
src+=2;
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
2010-10-20 09:11:16 -07:00
|
|
|
break;
|
|
|
|
|
2012-11-26 17:38:20 -08:00
|
|
|
case STATE_SECOND_BYTE:
|
|
|
|
NS_ASSERTION(*aSrcLength > 0, "buffer too short");
|
|
|
|
src++;
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
2010-10-20 09:11:16 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_ODD_SURROGATE_PAIR:
|
|
|
|
if (*aDestLength < 2)
|
2012-11-26 17:38:20 -08:00
|
|
|
goto error;
|
2010-10-20 09:11:16 -07:00
|
|
|
else {
|
2012-11-26 17:38:20 -08:00
|
|
|
*dest++ = mOddHighSurrogate;
|
|
|
|
*dest++ = mOddLowSurrogate;
|
|
|
|
mOddHighSurrogate = mOddLowSurrogate = 0;
|
|
|
|
mState = STATE_NORMAL;
|
2010-10-20 09:11:16 -07:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_NORMAL:
|
|
|
|
case STATE_HALF_CODE_POINT:
|
|
|
|
default:
|
|
|
|
break;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-26 17:38:20 -08:00
|
|
|
oddHighSurrogate = mOddHighSurrogate;
|
2012-11-26 17:38:20 -08:00
|
|
|
|
2009-02-22 02:08:27 -08:00
|
|
|
if (src == srcEnd) {
|
2010-10-20 09:11:16 -07:00
|
|
|
*aDestLength = dest - aDest;
|
2012-11-26 17:38:20 -08:00
|
|
|
return (mState != STATE_NORMAL || oddHighSurrogate) ?
|
2012-11-26 17:38:20 -08:00
|
|
|
NS_OK_UDEC_MOREINPUT : NS_OK;
|
2009-02-22 02:08:27 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-02-22 02:08:27 -08:00
|
|
|
const char* srcEvenEnd;
|
|
|
|
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t u;
|
2012-11-26 17:38:20 -08:00
|
|
|
if (mState == STATE_HALF_CODE_POINT) {
|
2012-11-26 17:38:20 -08:00
|
|
|
if (dest == destEnd)
|
|
|
|
goto error;
|
|
|
|
|
2012-11-26 17:38:20 -08:00
|
|
|
// the 1st byte of a 16-bit code unit was stored in |mOddByte| in the
|
2009-02-22 02:08:27 -08:00
|
|
|
// previous run while the 2nd byte has to come from |*src|.
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
2014-02-26 17:42:29 -08:00
|
|
|
#if MOZ_BIG_ENDIAN
|
2013-08-21 06:40:57 -07:00
|
|
|
u = (mOddByte << 8) | uint8_t(*src++); // safe, we know we have at least one byte.
|
2009-02-22 02:08:27 -08:00
|
|
|
#else
|
2012-11-26 17:38:20 -08:00
|
|
|
u = (*src++ << 8) | mOddByte; // safe, we know we have at least one byte.
|
2009-02-22 02:08:27 -08:00
|
|
|
#endif
|
|
|
|
srcEvenEnd = src + ((srcEnd - src) & ~1); // handle even number of bytes in main loop
|
|
|
|
goto have_codepoint;
|
|
|
|
} else {
|
|
|
|
srcEvenEnd = src + ((srcEnd - src) & ~1); // handle even number of bytes in main loop
|
|
|
|
}
|
|
|
|
|
|
|
|
while (src != srcEvenEnd) {
|
|
|
|
if (dest == destEnd)
|
2007-03-22 10:30:00 -07:00
|
|
|
goto error;
|
|
|
|
|
2010-03-06 07:35:11 -08:00
|
|
|
#if !defined(__sparc__) && !defined(__arm__)
|
2014-01-04 07:02:17 -08:00
|
|
|
u = *(const char16_t*)src;
|
2010-03-06 07:35:11 -08:00
|
|
|
#else
|
|
|
|
memcpy(&u, src, 2);
|
|
|
|
#endif
|
2009-02-22 02:08:27 -08:00
|
|
|
src += 2;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2009-02-22 02:08:27 -08:00
|
|
|
have_codepoint:
|
|
|
|
if (aSwapBytes)
|
|
|
|
u = u << 8 | u >> 8;
|
|
|
|
|
|
|
|
if (!IS_SURROGATE(u)) {
|
|
|
|
if (oddHighSurrogate) {
|
2012-11-26 17:38:20 -08:00
|
|
|
if (mErrBehavior == kOnError_Signal) {
|
|
|
|
goto error2;
|
|
|
|
}
|
2009-02-22 02:08:27 -08:00
|
|
|
*dest++ = UCS2_REPLACEMENT_CHAR;
|
|
|
|
if (dest == destEnd)
|
|
|
|
goto error;
|
|
|
|
oddHighSurrogate = 0;
|
|
|
|
}
|
|
|
|
*dest++ = u;
|
|
|
|
} else if (NS_IS_HIGH_SURROGATE(u)) {
|
|
|
|
if (oddHighSurrogate) {
|
2012-11-26 17:38:20 -08:00
|
|
|
if (mErrBehavior == kOnError_Signal) {
|
|
|
|
goto error2;
|
|
|
|
}
|
2009-02-22 02:08:27 -08:00
|
|
|
*dest++ = UCS2_REPLACEMENT_CHAR;
|
|
|
|
if (dest == destEnd)
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
oddHighSurrogate = u;
|
|
|
|
}
|
|
|
|
else /* if (NS_IS_LOW_SURROGATE(u)) */ {
|
2010-10-20 09:11:16 -07:00
|
|
|
if (oddHighSurrogate && *aDestLength > 1) {
|
|
|
|
if (dest + 1 >= destEnd) {
|
2012-11-26 17:38:20 -08:00
|
|
|
mOddLowSurrogate = u;
|
|
|
|
mOddHighSurrogate = oddHighSurrogate;
|
|
|
|
mState = STATE_ODD_SURROGATE_PAIR;
|
2009-02-22 02:08:27 -08:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
*dest++ = oddHighSurrogate;
|
|
|
|
*dest++ = u;
|
|
|
|
} else {
|
2012-11-26 17:38:20 -08:00
|
|
|
if (mErrBehavior == kOnError_Signal) {
|
|
|
|
goto error2;
|
|
|
|
}
|
2009-02-22 02:08:27 -08:00
|
|
|
*dest++ = UCS2_REPLACEMENT_CHAR;
|
|
|
|
}
|
2010-10-20 09:11:16 -07:00
|
|
|
oddHighSurrogate = 0;
|
2009-02-22 02:08:27 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-02-22 02:08:27 -08:00
|
|
|
if (src != srcEnd) {
|
|
|
|
// store the lead byte of a 16-bit unit for the next run.
|
2012-11-26 17:38:20 -08:00
|
|
|
mOddByte = *src++;
|
|
|
|
mState = STATE_HALF_CODE_POINT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2009-02-22 02:08:27 -08:00
|
|
|
|
2012-11-26 17:38:20 -08:00
|
|
|
mOddHighSurrogate = oddHighSurrogate;
|
2009-02-22 02:08:27 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
*aDestLength = dest - aDest;
|
|
|
|
*aSrcLength = src - aSrc;
|
2012-11-26 17:38:20 -08:00
|
|
|
return (mState != STATE_NORMAL || oddHighSurrogate) ?
|
2012-11-26 17:38:20 -08:00
|
|
|
NS_OK_UDEC_MOREINPUT : NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
error:
|
|
|
|
*aDestLength = dest - aDest;
|
|
|
|
*aSrcLength = src - aSrc;
|
|
|
|
return NS_OK_UDEC_MOREOUTPUT;
|
2012-11-26 17:38:20 -08:00
|
|
|
|
|
|
|
error2:
|
|
|
|
*aDestLength = dest - aDest;
|
|
|
|
*aSrcLength = --src - aSrc;
|
|
|
|
return NS_ERROR_ILLEGAL_INPUT;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUTF16ToUnicodeBase::Reset()
|
|
|
|
{
|
|
|
|
mState = STATE_FIRST_CALL;
|
2009-02-22 02:08:27 -08:00
|
|
|
mOddByte = 0;
|
|
|
|
mOddHighSurrogate = 0;
|
2010-10-20 09:11:16 -07:00
|
|
|
mOddLowSurrogate = 0;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsUTF16ToUnicodeBase::GetMaxLength(const char * aSrc, int32_t aSrcLength,
|
|
|
|
int32_t * aDestLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2009-02-22 02:08:27 -08:00
|
|
|
// the left-over data of the previous run have to be taken into account.
|
2012-11-26 17:38:20 -08:00
|
|
|
*aDestLength = (aSrcLength + ((STATE_HALF_CODE_POINT & mState) ? 1 : 0)) / 2;
|
2010-10-20 09:11:16 -07:00
|
|
|
if (mOddHighSurrogate)
|
|
|
|
(*aDestLength)++;
|
|
|
|
if (mOddLowSurrogate)
|
|
|
|
(*aDestLength)++;
|
2007-03-22 10:30:00 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsUTF16BEToUnicode::Convert(const char * aSrc, int32_t * aSrcLength,
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t * aDest, int32_t * aDestLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-26 17:38:20 -08:00
|
|
|
switch (mState) {
|
|
|
|
case STATE_FIRST_CALL:
|
|
|
|
if (*aSrcLength < 2) {
|
|
|
|
if (*aSrcLength < 1) {
|
|
|
|
*aDestLength = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (uint8_t(*aSrc) != 0xFE) {
|
|
|
|
mState = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*aDestLength = 0;
|
|
|
|
mState = STATE_SECOND_BYTE;
|
|
|
|
return NS_OK_UDEC_MOREINPUT;
|
|
|
|
}
|
2014-02-26 17:42:29 -08:00
|
|
|
#if MOZ_LITTLE_ENDIAN
|
2012-11-26 17:38:20 -08:00
|
|
|
// on LE machines, BE BOM is 0xFFFE
|
2014-01-04 07:02:17 -08:00
|
|
|
if (0xFFFE != *((char16_t*)aSrc)) {
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
|
|
|
}
|
|
|
|
#else
|
2014-01-04 07:02:17 -08:00
|
|
|
if (0xFEFF != *((char16_t*)aSrc)) {
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
2012-11-26 17:38:20 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_SECOND_BYTE:
|
|
|
|
if (*aSrcLength < 1) {
|
|
|
|
*aDestLength = 0;
|
|
|
|
return NS_OK_UDEC_MOREINPUT;
|
|
|
|
}
|
|
|
|
if (uint8_t(*aSrc) != 0xFF) {
|
|
|
|
mOddByte = 0xFE;
|
|
|
|
mState = STATE_HALF_CODE_POINT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-02-26 17:42:29 -08:00
|
|
|
return UTF16ConvertToUnicode(aSrc, aSrcLength, aDest, aDestLength,
|
|
|
|
bool(MOZ_LITTLE_ENDIAN));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsUTF16LEToUnicode::Convert(const char * aSrc, int32_t * aSrcLength,
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t * aDest, int32_t * aDestLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-11-26 17:38:20 -08:00
|
|
|
switch (mState) {
|
|
|
|
case STATE_FIRST_CALL:
|
|
|
|
if (*aSrcLength < 2) {
|
|
|
|
if (*aSrcLength < 1) {
|
|
|
|
*aDestLength = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (uint8_t(*aSrc) != 0xFF) {
|
|
|
|
mState = STATE_NORMAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*aDestLength = 0;
|
|
|
|
mState = STATE_SECOND_BYTE;
|
|
|
|
return NS_OK_UDEC_MOREINPUT;
|
|
|
|
}
|
2014-02-26 17:42:29 -08:00
|
|
|
#if MOZ_BIG_ENDIAN
|
2012-11-26 17:38:20 -08:00
|
|
|
// on BE machines, LE BOM is 0xFFFE
|
2014-01-04 07:02:17 -08:00
|
|
|
if (0xFFFE != *((char16_t*)aSrc)) {
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
|
|
|
}
|
|
|
|
#else
|
2014-01-04 07:02:17 -08:00
|
|
|
if (0xFEFF != *((char16_t*)aSrc)) {
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
#endif
|
2012-11-26 17:38:20 -08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case STATE_SECOND_BYTE:
|
|
|
|
if (*aSrcLength < 1) {
|
|
|
|
*aDestLength = 0;
|
|
|
|
return NS_OK_UDEC_MOREINPUT;
|
|
|
|
}
|
|
|
|
if (uint8_t(*aSrc) != 0xFE) {
|
|
|
|
mOddByte = 0xFF;
|
|
|
|
mState = STATE_HALF_CODE_POINT;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-02-26 17:42:29 -08:00
|
|
|
return UTF16ConvertToUnicode(aSrc, aSrcLength, aDest, aDestLength,
|
|
|
|
bool(MOZ_BIG_ENDIAN));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUTF16ToUnicode::Reset()
|
|
|
|
{
|
|
|
|
mEndian = kUnknown;
|
2011-10-17 07:59:28 -07:00
|
|
|
mFoundBOM = false;
|
2007-03-22 10:30:00 -07:00
|
|
|
return nsUTF16ToUnicodeBase::Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 08:56:38 -07:00
|
|
|
nsUTF16ToUnicode::Convert(const char * aSrc, int32_t * aSrcLength,
|
2014-01-04 07:02:17 -08:00
|
|
|
char16_t * aDest, int32_t * aDestLength)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-02-20 01:04:28 -08:00
|
|
|
if(STATE_FIRST_CALL == mState && *aSrcLength < 2)
|
|
|
|
{
|
2011-03-02 23:21:06 -08:00
|
|
|
nsresult res = (*aSrcLength == 0) ? NS_OK : NS_ERROR_ILLEGAL_INPUT;
|
2011-02-20 01:04:28 -08:00
|
|
|
*aSrcLength=0;
|
|
|
|
*aDestLength=0;
|
2011-03-02 23:21:06 -08:00
|
|
|
return res;
|
2011-02-20 01:04:28 -08:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
if(STATE_FIRST_CALL == mState) // first time called
|
|
|
|
{
|
|
|
|
// check if BOM (0xFEFF) is at the beginning, remove it if found, and
|
|
|
|
// set mEndian accordingly.
|
2012-08-22 08:56:38 -07:00
|
|
|
if(0xFF == uint8_t(aSrc[0]) && 0xFE == uint8_t(aSrc[1])) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mEndian = kLittleEndian;
|
2011-10-17 07:59:28 -07:00
|
|
|
mFoundBOM = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
else if(0xFE == uint8_t(aSrc[0]) && 0xFF == uint8_t(aSrc[1])) {
|
2007-03-22 10:30:00 -07:00
|
|
|
mEndian = kBigEndian;
|
2011-10-17 07:59:28 -07:00
|
|
|
mFoundBOM = true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
// BOM is not found, but we can use a simple heuristic to determine
|
|
|
|
// the endianness. Assume the first character is [U+0001, U+00FF].
|
|
|
|
// Not always valid, but it's very likely to hold for html/xml/css.
|
|
|
|
else if(!aSrc[0] && aSrc[1]) { // 0x00 0xhh (hh != 00)
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
2007-03-22 10:30:00 -07:00
|
|
|
mEndian = kBigEndian;
|
|
|
|
}
|
|
|
|
else if(aSrc[0] && !aSrc[1]) { // 0xhh 0x00 (hh != 00)
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
2007-03-22 10:30:00 -07:00
|
|
|
mEndian = kLittleEndian;
|
|
|
|
}
|
|
|
|
else { // Neither BOM nor 'plausible' byte patterns at the beginning.
|
|
|
|
// Just assume it's BE (following Unicode standard)
|
|
|
|
// and let the garbage show up in the browser. (security concern?)
|
|
|
|
// (bug 246194)
|
2012-11-26 17:38:20 -08:00
|
|
|
mState = STATE_NORMAL;
|
2007-03-22 10:30:00 -07:00
|
|
|
mEndian = kBigEndian;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-26 17:38:20 -08:00
|
|
|
nsresult rv = UTF16ConvertToUnicode(aSrc, aSrcLength, aDest, aDestLength,
|
2014-02-26 17:42:29 -08:00
|
|
|
#if MOZ_BIG_ENDIAN
|
2009-02-22 02:08:27 -08:00
|
|
|
(mEndian == kLittleEndian)
|
2007-03-22 10:30:00 -07:00
|
|
|
#else
|
2014-02-26 17:42:29 -08:00
|
|
|
(mEndian == kBigEndian)
|
2007-03-22 10:30:00 -07:00
|
|
|
#endif
|
2009-02-22 02:08:27 -08:00
|
|
|
);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// If BOM is not found and we're to return NS_OK, signal that BOM
|
|
|
|
// is not found. Otherwise, return |rv| from |UTF16ConvertToUnicode|
|
|
|
|
return (rv == NS_OK && !mFoundBOM) ? NS_OK_UDEC_NOBOMFOUND : rv;
|
|
|
|
}
|