diff --git a/content/base/public/nsContentUtils.h b/content/base/public/nsContentUtils.h index 06edf242199..9818a9828e6 100644 --- a/content/base/public/nsContentUtils.h +++ b/content/base/public/nsContentUtils.h @@ -472,6 +472,19 @@ public: const nsACString& aInput, nsAString& aOutput); + /** + * Determine whether a buffer begins with a BOM for UTF-8, UTF-16LE, + * UTF-16BE, UTF-32LE, UTF-32BE. + * + * @param aBuffer the buffer to check + * @param aLength the length of the buffer + * @param aCharset empty if not found + * @return boolean indicating whether a BOM was detected. + */ + static PRBool CheckForBOM(const unsigned char* aBuffer, PRUint32 aLength, + nsACString& aCharset); + + /** * Determine whether aContent is in some way associated with aForm. If the * form is a container the only elements that are considered to be associated diff --git a/content/base/src/nsContentUtils.cpp b/content/base/src/nsContentUtils.cpp index 55917c8f739..4420fe7fb2e 100644 --- a/content/base/src/nsContentUtils.cpp +++ b/content/base/src/nsContentUtils.cpp @@ -2874,6 +2874,47 @@ nsContentUtils::ConvertStringFromCharset(const nsACString& aCharset, return rv; } +/* static */ +PRBool +nsContentUtils::CheckForBOM(const unsigned char* aBuffer, PRUint32 aLength, + nsACString& aCharset) +{ + PRBool found = PR_TRUE; + aCharset.Truncate(); + if (aLength >= 3 && + aBuffer[0] == 0xEF && + aBuffer[1] == 0xBB && + aBuffer[2] == 0xBF) { + aCharset = "UTF-8"; + } + else if (aLength >= 4 && + aBuffer[0] == 0x00 && + aBuffer[1] == 0x00 && + aBuffer[2] == 0xFE && + aBuffer[3] == 0xFF) { + aCharset = "UTF-32BE"; + } + else if (aLength >= 4 && + aBuffer[0] == 0xFF && + aBuffer[1] == 0xFE && + aBuffer[2] == 0x00 && + aBuffer[3] == 0x00) { + aCharset = "UTF-32LE"; + } + else if (aLength >= 2 && + aBuffer[0] == 0xFE && aBuffer[1] == 0xFF) { + aCharset = "UTF-16BE"; + } + else if (aLength >= 2 && + aBuffer[0] == 0xFF && aBuffer[1] == 0xFE) { + aCharset = "UTF-16LE"; + } else { + found = PR_FALSE; + } + + return found; +} + static PRBool EqualExceptRef(nsIURL* aURL1, nsIURL* aURL2) { nsCOMPtr u1;