Bug 638318 - Require all zero bytes to be in either odd or even position when sniffing for UTF-16BE/LE-encoded Basic Latin. r=bz, a=beltzner
authorHenri Sivonen <hsivonen@iki.fi>
Thu, 03 Mar 2011 08:52:47 +0200
changeset 63321 ed9c526f93b5
parent 63320 a6eb991e7f05
child 63322 770e2709cff5
push idunknown
push userunknown
push dateunknown
reviewersbz, beltzner
bugs638318
milestone2.0b13pre
Bug 638318 - Require all zero bytes to be in either odd or even position when sniffing for UTF-16BE/LE-encoded Basic Latin. r=bz, a=beltzner
parser/html/nsHtml5StreamParser.cpp
--- a/parser/html/nsHtml5StreamParser.cpp
+++ b/parser/html/nsHtml5StreamParser.cpp
@@ -338,35 +338,46 @@ void
 nsHtml5StreamParser::SniffBOMlessUTF16BasicLatin(const PRUint8* aFromSegment,
                                                  PRUint32 aCountToSniffingLimit)
 {
   // Make sure there's enough data. Require room for "<title></title>"
   if (mSniffingLength + aCountToSniffingLimit < 30) {
     return;
   }
   // even-numbered bytes tracked at 0, odd-numbered bytes tracked at 1
+  PRBool byteZero[2] = { PR_FALSE, PR_FALSE };
   PRBool byteNonZero[2] = { PR_FALSE, PR_FALSE };
   PRUint32 i = 0;
   if (mSniffingBuffer) {
     for (; i < mSniffingLength; ++i) {
       if (mSniffingBuffer[i]) {
         if (byteNonZero[1 - (i % 2)]) {
           return;
         }
         byteNonZero[i % 2] = PR_TRUE;
+      } else {
+        if (byteZero[1 - (i % 2)]) {
+          return;
+        }
+        byteZero[i % 2] = PR_TRUE;
       }
     }
   }
   if (aFromSegment) {
     for (PRUint32 j = 0; j < aCountToSniffingLimit; ++j) {
       if (aFromSegment[j]) {
         if (byteNonZero[1 - ((i + j) % 2)]) {
           return;
         }
         byteNonZero[(i + j) % 2] = PR_TRUE;
+      } else {
+        if (byteZero[1 - ((i + j) % 2)]) {
+          return;
+        }
+        byteZero[(i + j) % 2] = PR_TRUE;
       }
     }
   }
 
   if (byteNonZero[0]) {
     mCharset.Assign("UTF-16LE");
   } else {
     mCharset.Assign("UTF-16BE");