Skip to content

Commit

Permalink
Merge pull request #4448 from Karlson2k/xml_warn_charset
Browse files Browse the repository at this point in the history
XBMCTinyXML: put warnings to log if suspicious charset is used for XML processing
  • Loading branch information
jmarshallnz authored and t-nelson committed Mar 24, 2014
1 parent 107eae5 commit 6481552
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions xbmc/utils/XBMCTinyXML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,52 @@ bool CXBMCTinyXML::Parse(const std::string& data, TiXmlEncoding encoding /*= TIX

std::string detectedCharset;
if (CCharsetDetection::DetectXmlEncoding(data, detectedCharset) && TryParse(data, detectedCharset))
{
if (!m_SuggestedCharset.empty())
CLog::Log(LOGWARNING, "%s: \"%s\" charset was used instead of suggested charset \"%s\" for %s", __FUNCTION__, m_UsedCharset.c_str(), m_SuggestedCharset.c_str(),
(value.empty() ? "XML data" : ("file \"" + value + "\"").c_str()));

return true;
}

// check for valid UTF-8
if (m_SuggestedCharset != "UTF-8" && detectedCharset != "UTF-8" && CUtf8Utils::isValidUtf8(data) &&
TryParse(data, "UTF-8"))
return true;
{
if (!m_SuggestedCharset.empty())
CLog::Log(LOGWARNING, "%s: \"%s\" charset was used instead of suggested charset \"%s\" for %s", __FUNCTION__, m_UsedCharset.c_str(), m_SuggestedCharset.c_str(),
(value.empty() ? "XML data" : ("file \"" + value + "\"").c_str()));
else if (!detectedCharset.empty())
CLog::Log(LOGWARNING, "%s: \"%s\" charset was used instead of detected charset \"%s\" for %s", __FUNCTION__, m_UsedCharset.c_str(), detectedCharset.c_str(),
(value.empty() ? "XML data" : ("file \"" + value + "\"").c_str()));
return true;
}

// fallback: try user GUI charset
if (TryParse(data, g_langInfo.GetGuiCharSet()))
{
if (!m_SuggestedCharset.empty())
CLog::Log(LOGWARNING, "%s: \"%s\" charset was used instead of suggested charset \"%s\" for %s", __FUNCTION__, m_UsedCharset.c_str(), m_SuggestedCharset.c_str(),
(value.empty() ? "XML data" : ("file \"" + value + "\"").c_str()));
else if (!detectedCharset.empty())
CLog::Log(LOGWARNING, "%s: \"%s\" charset was used instead of detected charset \"%s\" for %s", __FUNCTION__, m_UsedCharset.c_str(), detectedCharset.c_str(),
(value.empty() ? "XML data" : ("file \"" + value + "\"").c_str()));
return true;
}

// can't detect correct data charset, try to process data as is
return InternalParse(data, TIXML_ENCODING_UNKNOWN);
if (InternalParse(data, TIXML_ENCODING_UNKNOWN))
{
if (!m_SuggestedCharset.empty())
CLog::Log(LOGWARNING, "%s: Processed %s as unknown encoding instead of suggested \"%s\"", __FUNCTION__,
(value.empty() ? "XML data" : ("file \"" + value + "\"").c_str()), m_SuggestedCharset.c_str());
else if (!detectedCharset.empty())
CLog::Log(LOGWARNING, "%s: Processed %s as unknown encoding instead of detected \"%s\"", __FUNCTION__,
(value.empty() ? "XML data" : ("file \"" + value + "\"").c_str()), detectedCharset.c_str());
return true;
}

return false;
}

bool CXBMCTinyXML::TryParse(const std::string& data, const std::string& tryDataCharset)
Expand Down Expand Up @@ -199,10 +232,6 @@ bool CXBMCTinyXML::TryParse(const std::string& data, const std::string& tryDataC
}

m_UsedCharset = tryDataCharset;
if (!m_SuggestedCharset.empty() && m_UsedCharset != m_SuggestedCharset)
CLog::Log(LOGWARNING, "%s: Using \"%s\" charset instead of \"%s\" charset%s", __FUNCTION__, m_UsedCharset.c_str(), m_SuggestedCharset.c_str(),
(value.empty() ? "" : (" for file " + value).c_str()));

return true;
}

Expand Down

0 comments on commit 6481552

Please sign in to comment.