bug 317216 : UTF16 and UTF32 decoders allow invalid UTF16 into the core : UTF-32 patch, r=smontagu, sr=bzbarsky

This commit is contained in:
jshin%mailaps.org 2005-11-22 07:58:41 +00:00
parent 29c5a75f9a
commit eb97f51003
2 changed files with 10 additions and 13 deletions

View File

@ -48,6 +48,7 @@ FORCE_STATIC_LIB = 1
LIBXUL_LIBRARY = 1
REQUIRES = xpcom \
string \
uconv \
$(NULL)

View File

@ -40,6 +40,7 @@
#include "nsUCSupport.h"
#include "nsUTF32ToUnicode.h"
#include "nsCharTraits.h"
#include <string.h>
//----------------------------------------------------------------------
@ -96,9 +97,7 @@ static nsresult ConvertCommon(const char * aSrc,
ucs4 = aIsLE ? LE_STRING_TO_UCS4(aBuffer) : BE_STRING_TO_UCS4(aBuffer);
if (ucs4 < 0x10000L) // BMP
{
// XXX Do we have to convert surrogate code points to the replacement
// character (0xfffd)?
*dest++= PRUnichar(ucs4);
*dest++= IS_SURROGATE(ucs4) ? UCS2_REPLACEMENT_CHAR : PRUnichar(ucs4);
}
else if (ucs4 < 0x110000L) // plane 1 through plane 16
{
@ -108,9 +107,8 @@ static nsresult ConvertCommon(const char * aSrc,
*aDestLength = 0;
return NS_OK_UDEC_MOREOUTPUT;
}
// ((ucs4 - 0x10000) >> 10) + 0xd800;
*dest++= PRUnichar((ucs4 >> 10) + 0xd7c0); // high surrogate
*dest++= PRUnichar(ucs4 & 0x3ffL | 0xdc00); // low surrogate
*dest++= H_SURROGATE(ucs4);
*dest++= L_SURROGATE(ucs4);
}
// Codepoints in plane 17 and higher (> 0x10ffff)
// are not representable in UTF-16 we use for the internal
@ -119,7 +117,7 @@ static nsresult ConvertCommon(const char * aSrc,
// in plane 17 and higher. Therefore, we convert them
// to Unicode replacement character (0xfffd).
else
*dest++ = 0xfffd;
*dest++ = UCS2_REPLACEMENT_CHAR;
src += *aState;
*aState = 0;
}
@ -141,20 +139,18 @@ static nsresult ConvertCommon(const char * aSrc,
ucs4 = aIsLE ? LE_STRING_TO_UCS4(src) : BE_STRING_TO_UCS4(src);
if (ucs4 < 0x10000L) // BMP
{
// XXX Do we have to convert surrogate code points to the replacement
// character (0xfffd)?
*dest++= PRUnichar(ucs4);
*dest++= IS_SURROGATE(ucs4) ? UCS2_REPLACEMENT_CHAR : PRUnichar(ucs4);
}
else if (ucs4 < 0x110000L) // plane 1 through plane 16
{
if (destEnd - dest < 2)
break;
// ((ucs4 - 0x10000) >> 10) + 0xd800;
*dest++= PRUnichar((ucs4 >> 10) + 0xd7c0);
*dest++= PRUnichar(ucs4 & 0x3ffL | 0xdc00);
*dest++= H_SURROGATE(ucs4);
*dest++= L_SURROGATE(ucs4);
}
else // plane 17 and higher
*dest++ = 0xfffd;
*dest++ = UCS2_REPLACEMENT_CHAR;
}
//output not finished, output buffer too short