Bug 1303397. nsScriptLoader should just use nsContentUtils::CheckForBOM instead of rolling its own copy. r=bkelly
authorBoris Zbarsky <bzbarsky@mit.edu>
Tue, 20 Sep 2016 13:24:06 +0100
changeset 314621 2daffd62a19b2a060e573bddc3fb0536607faf5e
parent 314620 aa252c2f85b5919410685eebc6d8c1c34c86e7ea
child 314622 3c60fe237a35defea33b60a4a0ab15b700cdc110
push id30732
push usercbook@mozilla.com
push dateWed, 21 Sep 2016 10:04:03 +0000
treeherdermozilla-central@560b2c805bf7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbkelly
bugs1303397
milestone52.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1303397. nsScriptLoader should just use nsContentUtils::CheckForBOM instead of rolling its own copy. r=bkelly
dom/base/nsScriptLoader.cpp
--- a/dom/base/nsScriptLoader.cpp
+++ b/dom/base/nsScriptLoader.cpp
@@ -2355,49 +2355,16 @@ nsScriptLoader::ReadyToExecuteParserBloc
       loader->SetBlockingPredecessor(lastPred);
       return false;
     }
   }
 
   return true;
 }
 
-// This function was copied from nsParser.cpp. It was simplified a bit.
-static bool
-DetectByteOrderMark(const unsigned char* aBytes, int32_t aLen, nsCString& oCharset)
-{
-  if (aLen < 2)
-    return false;
-
-  switch(aBytes[0]) {
-  case 0xEF:
-    if (aLen >= 3 && 0xBB == aBytes[1] && 0xBF == aBytes[2]) {
-      // EF BB BF
-      // Win2K UTF-8 BOM
-      oCharset.AssignLiteral("UTF-8");
-    }
-    break;
-  case 0xFE:
-    if (0xFF == aBytes[1]) {
-      // FE FF
-      // UTF-16, big-endian
-      oCharset.AssignLiteral("UTF-16BE");
-    }
-    break;
-  case 0xFF:
-    if (0xFE == aBytes[1]) {
-      // FF FE
-      // UTF-16, little-endian
-      oCharset.AssignLiteral("UTF-16LE");
-    }
-    break;
-  }
-  return !oCharset.IsEmpty();
-}
-
 /* static */ nsresult
 nsScriptLoader::ConvertToUTF16(nsIChannel* aChannel, const uint8_t* aData,
                                uint32_t aLength, const nsAString& aHintCharset,
                                nsIDocument* aDocument,
                                char16_t*& aBufOut, size_t& aLengthOut)
 {
   if (!aLength) {
     aBufOut = nullptr;
@@ -2410,20 +2377,19 @@ nsScriptLoader::ConvertToUTF16(nsIChanne
   // HTTP Content-Type (if name recognized)
   // charset attribute (if name recognized)
   // The encoding of the document
 
   nsAutoCString charset;
 
   nsCOMPtr<nsIUnicodeDecoder> unicodeDecoder;
 
-  if (DetectByteOrderMark(aData, aLength, charset)) {
-    // charset is now "UTF-8" or "UTF-16". The UTF-16 decoder will re-sniff
-    // the BOM for endianness. Both the UTF-16 and the UTF-8 decoder will
-    // take care of swallowing the BOM.
+  if (nsContentUtils::CheckForBOM(aData, aLength, charset)) {
+    // charset is now one of "UTF-16BE", "UTF-16BE" or "UTF-8".  Those decoder
+    // will take care of swallowing the BOM.
     unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
   }
 
   if (!unicodeDecoder &&
       aChannel &&
       NS_SUCCEEDED(aChannel->GetContentCharset(charset)) &&
       EncodingUtils::FindEncodingForLabel(charset, charset)) {
     unicodeDecoder = EncodingUtils::DecoderForEncoding(charset);
@@ -2969,17 +2935,17 @@ nsScriptLoadHandler::EnsureDecoder(nsIIn
   // Determine if BOM check should be done.  This occurs either
   // if end-of-stream has been reached, or at least 3 bytes have
   // been read from input.
   if (!aEndOfStream && (aDataLength < 3)) {
     return false;
   }
 
   // Do BOM detection.
-  if (DetectByteOrderMark(aData, aDataLength, charset)) {
+  if (nsContentUtils::CheckForBOM(aData, aDataLength, charset)) {
     mDecoder = EncodingUtils::DecoderForEncoding(charset);
     return true;
   }
 
   // BOM detection failed, check content stream for charset.
   nsCOMPtr<nsIRequest> req;
   nsresult rv = aLoader->GetRequest(getter_AddRefs(req));
   NS_ASSERTION(req, "StreamLoader's request went away prematurely");