From bc8abb116f3205e398e9a481b84c96e6ed1b0fdf Mon Sep 17 00:00:00 2001 From: iska Date: Fri, 31 Oct 2014 21:46:54 +0100 Subject: [PATCH] Fix UTF32Char to String conversion for invalid unicode characters Invalid, isolated surrogate pairs, UTF32 characters should handled specially and converted to UniChars first --- HTMLKit/HTMLInputStreamReader.m | 4 ++-- HTMLKit/HTMLTokenizerCharacters.h | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/HTMLKit/HTMLInputStreamReader.m b/HTMLKit/HTMLInputStreamReader.m index b801ffe..ff2dc16 100644 --- a/HTMLKit/HTMLInputStreamReader.m +++ b/HTMLKit/HTMLInputStreamReader.m @@ -66,7 +66,7 @@ [HTMLInputStreamReaderErrors emitParseError:HTMLStreamReaderErrorIsolatedLowSurrogate atLocation:_location andCallback:_errorCallback]; - return REPLACEMENT_CHAR; + return nextInputCharacter; } if (CFStringIsSurrogateHighCharacter(nextInputCharacter)) { @@ -75,7 +75,7 @@ [HTMLInputStreamReaderErrors emitParseError:HTMLStreamReaderErrorIsolatedHighSurrogate atLocation:_location andCallback:_errorCallback]; - return REPLACEMENT_CHAR; + return nextInputCharacter; } nextInputCharacter = CFStringGetLongCharacterForSurrogatePair(nextInputCharacter, surrogateLow); diff --git a/HTMLKit/HTMLTokenizerCharacters.h b/HTMLKit/HTMLTokenizerCharacters.h index 5071ead..116c838 100644 --- a/HTMLKit/HTMLTokenizerCharacters.h +++ b/HTMLKit/HTMLTokenizerCharacters.h @@ -175,5 +175,7 @@ NS_INLINE NSString * StringFromUniChar(unichar character) NS_INLINE NSString * StringFromUTF32Char(UTF32Char character) { - return [[NSString alloc] initWithBytes:&character length:sizeof(character) encoding:NSUTF32LittleEndianStringEncoding]; + unichar pair[2]; + Boolean isPair = CFStringGetSurrogatePairForLongCharacter(character, pair); + return [[NSString alloc] initWithCharacters:(const unichar *)&pair length:(isPair ? 2 : 1)]; }