Skip to content

Commit

Permalink
XML Fix: Surrogate Pairs
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan386 committed Jul 19, 2019
1 parent c21751c commit 79bfc80
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion shareaza/Strings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,29 @@ CString Escape(const CString& strValue)
default:
if ( *pszValue < 32 || *pszValue > 127 )
{
int n = _stprintf_s( pszXML, nXMLLength, _T("&#%u;"), *pszValue );
unsigned long nCode = *pszValue;

if ( nCode >= 0xD800 && nCode <= 0xDFFF ) // Surrogate Pairs
{
if ( nCode <= 0xDBFF )
{
unsigned long nPair = *( pszValue + 1 );

if ( nPair >= 0xDC00 && nPair <= 0xDFFF )
{
nCode = (nCode & 0x3FF) << 10;
nCode |= nPair & 0x3FF;
nCode += 0x10000;
++pszValue;
}
else
nCode = 0xFFFD; // Replacement character
}
else
nCode = 0xFFFD;
}

int n = _stprintf_s( pszXML, nXMLLength, _T("&#%u;"), nCode );
pszXML += n;
nXMLLength -= n;
bChanged = true;
Expand Down

0 comments on commit 79bfc80

Please sign in to comment.