Bug 801402 - Make EncodingUtil methods case sensitive. r=hsivonen
authorMasatoshi Kimura <VYV03354@nifty.ne.jp>
Wed, 07 Nov 2012 18:04:22 -0500
changeset 121273 b2a8c4a7945334effeb5f818176e19e7cb42e0e2
parent 121272 b4b9eb815efa11ae4a3a3aff7b3e4dfc4d8e78fc
child 121274 c0af6d983c0ea12ba4d78a6c408cb75fe38282bb
push id273
push userlsblakk@mozilla.com
push dateThu, 14 Feb 2013 23:19:38 +0000
treeherdermozilla-release@c5e807a3f8b8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershsivonen
bugs801402
milestone19.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 801402 - Make EncodingUtil methods case sensitive. r=hsivonen
content/base/src/nsContentUtils.cpp
dom/encoding/EncodingUtils.cpp
dom/encoding/EncodingUtils.h
dom/encoding/Makefile.in
dom/encoding/TextDecoder.cpp
dom/encoding/TextDecoder.h
dom/encoding/TextEncoder.cpp
dom/encoding/TextEncoder.h
dom/encoding/labelsencodings.properties
--- a/content/base/src/nsContentUtils.cpp
+++ b/content/base/src/nsContentUtils.cpp
@@ -124,17 +124,16 @@ static NS_DEFINE_CID(kXTFServiceCID, NS_
 #include "nsIWebNavigation.h"
 #include "nsILoadContext.h"
 #include "nsTextFragment.h"
 #include "mozilla/Selection.h"
 #include "nsSVGUtils.h"
 #include "nsISVGChildFrame.h"
 #include "nsRenderingContext.h"
 #include "gfxSVGGlyphs.h"
-#include "mozilla/dom/EncodingUtils.h"
 
 #ifdef IBMBIDI
 #include "nsIBidiKeyboard.h"
 #endif
 #include "nsCycleCollectionParticipant.h"
 
 // for ReportToConsole
 #include "nsIStringBundle.h"
@@ -1524,18 +1523,17 @@ nsContentUtils::Shutdown()
   delete sOSText;
   sOSText = nullptr;
   delete sAltText;  
   sAltText = nullptr;
   delete sModifierSeparator;
   sModifierSeparator = nullptr;
 
   NS_IF_RELEASE(sSameOriginChecker);
-  
-  EncodingUtils::Shutdown();
+
   nsTextEditorState::ShutDown();
 }
 
 /**
  * Checks whether two nodes come from the same origin. aTrustedNode is
  * considered 'safe' in that a user can operate on it and that it isn't
  * a js-object that implements nsIDOMNode.
  * Never call this function with the first node provided by script, it
--- a/dom/encoding/EncodingUtils.cpp
+++ b/dom/encoding/EncodingUtils.cpp
@@ -1,337 +1,72 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/dom/EncodingUtils.h"
-#include "nsAutoPtr.h"
-#include "nsContentUtils.h"
+#include "nsUConvPropertySearch.h"
 
 namespace mozilla {
 namespace dom {
 
-EncodingUtils* gEncodings = nullptr;
-
-struct LabelEncoding
-{
-  const char* mLabel;
-  const char* mEncoding;
+static const char* labelsEncodings[][3] = {
+#include "labelsencodings.properties.h"
 };
 
-static const LabelEncoding labelsEncodings[] = {
-  {"unicode-1-1-utf-8", "utf-8"},
-  {"utf-8", "utf-8"},
-  {"utf8", "utf-8"},
-  {"866", "ibm866"},
-  {"cp866", "ibm866"},
-  {"csibm866", "ibm866"},
-  {"ibm866", "ibm866"},
-  {"csisolatin2", "iso-8859-2"},
-  {"iso-8859-2", "iso-8859-2"},
-  {"iso-ir-101", "iso-8859-2"},
-  {"iso8859-2", "iso-8859-2"},
-  {"iso88592", "iso-8859-2"},
-  {"iso_8859-2", "iso-8859-2"},
-  {"iso_8859-2:1987", "iso-8859-2"},
-  {"l2", "iso-8859-2"},
-  {"latin2", "iso-8859-2"},
-  {"csisolatin3", "iso-8859-3"},
-  {"iso-8859-3", "iso-8859-3"},
-  {"iso-ir-109", "iso-8859-3"},
-  {"iso8859-3", "iso-8859-3"},
-  {"iso88593", "iso-8859-3"},
-  {"iso_8859-3", "iso-8859-3"},
-  {"iso_8859-3:1988", "iso-8859-3"},
-  {"l3", "iso-8859-3"},
-  {"latin3", "iso-8859-3"},
-  {"csisolatin4", "iso-8859-4"},
-  {"iso-8859-4", "iso-8859-4"},
-  {"iso-ir-110", "iso-8859-4"},
-  {"iso8859-4", "iso-8859-4"},
-  {"iso88594", "iso-8859-4"},
-  {"iso_8859-4", "iso-8859-4"},
-  {"iso_8859-4:1988", "iso-8859-4"},
-  {"l4", "iso-8859-4"},
-  {"latin4", "iso-8859-4"},
-  {"csisolatincyrillic", "iso-8859-5"},
-  {"cyrillic", "iso-8859-5"},
-  {"iso-8859-5", "iso-8859-5"},
-  {"iso-ir-144", "iso-8859-5"},
-  {"iso8859-5", "iso-8859-5"},
-  {"iso88595", "iso-8859-5"},
-  {"iso_8859-5", "iso-8859-5"},
-  {"iso_8859-5:1988", "iso-8859-5"},
-  {"arabic", "iso-8859-6"},
-  {"asmo-708", "iso-8859-6"},
-  {"csiso88596e", "iso-8859-6"},
-  {"csiso88596i", "iso-8859-6"},
-  {"csisolatinarabic", "iso-8859-6"},
-  {"ecma-114", "iso-8859-6"},
-  {"iso-8859-6", "iso-8859-6"},
-  {"iso-8859-6-e", "iso-8859-6"},
-  {"iso-8859-6-i", "iso-8859-6"},
-  {"iso-ir-127", "iso-8859-6"},
-  {"iso8859-6", "iso-8859-6"},
-  {"iso88596", "iso-8859-6"},
-  {"iso_8859-6", "iso-8859-6"},
-  {"iso_8859-6:1987", "iso-8859-6"},
-  {"csisolatingreek", "iso-8859-7"},
-  {"ecma-118", "iso-8859-7"},
-  {"elot_928", "iso-8859-7"},
-  {"greek", "iso-8859-7"},
-  {"greek8", "iso-8859-7"},
-  {"iso-8859-7", "iso-8859-7"},
-  {"iso-ir-126", "iso-8859-7"},
-  {"iso8859-7", "iso-8859-7"},
-  {"iso88597", "iso-8859-7"},
-  {"iso_8859-7", "iso-8859-7"},
-  {"iso_8859-7:1987", "iso-8859-7"},
-  {"sun_eu_greek", "iso-8859-7"},
-  {"csiso88598e", "iso-8859-8"},
-  {"csisolatinhebrew", "iso-8859-8"},
-  {"hebrew", "iso-8859-8"},
-  {"iso-8859-8", "iso-8859-8"},
-  {"iso-8859-8-e", "iso-8859-8"},
-  {"iso-ir-138", "iso-8859-8"},
-  {"iso8859-8", "iso-8859-8"},
-  {"iso88598", "iso-8859-8"},
-  {"iso_8859-8", "iso-8859-8"},
-  {"iso_8859-8:1988", "iso-8859-8"},
-  {"visual", "iso-8859-8"},
-  {"csiso88598i", "iso-8859-8-i"},
-  {"iso-8859-8-i", "iso-8859-8-i"},
-  {"logical", "iso-8859-8-i"},
-  {"csisolatin6", "iso-8859-10"},
-  {"iso-8859-10", "iso-8859-10"},
-  {"iso-ir-157", "iso-8859-10"},
-  {"iso8859-10", "iso-8859-10"},
-  {"iso885910", "iso-8859-10"},
-  {"l6", "iso-8859-10"},
-  {"latin6", "iso-8859-10"},
-  {"iso-8859-13", "iso-8859-13"},
-  {"iso8859-13", "iso-8859-13"},
-  {"iso885913", "iso-8859-13"},
-  {"iso-8859-14", "iso-8859-14"},
-  {"iso8859-14", "iso-8859-14"},
-  {"iso885914", "iso-8859-14"},
-  {"csisolatin9", "iso-8859-15"},
-  {"iso-8859-15", "iso-8859-15"},
-  {"iso8859-15", "iso-8859-15"},
-  {"iso885915", "iso-8859-15"},
-  {"iso_8859-15", "iso-8859-15"},
-  {"l9", "iso-8859-15"},
-  {"iso-8859-16", "iso-8859-16"},
-  {"cskoi8r", "koi8-r"},
-  {"koi", "koi8-r"},
-  {"koi8", "koi8-r"},
-  {"koi8-r", "koi8-r"},
-  {"koi8_r", "koi8-r"},
-  {"koi8-u", "koi8-u"},
-  {"csmacintosh", "macintosh"},
-  {"mac", "macintosh"},
-  {"macintosh", "macintosh"},
-  {"x-mac-roman", "macintosh"},
-  {"dos-874", "windows-874"},
-  {"iso-8859-11", "windows-874"},
-  {"iso8859-11", "windows-874"},
-  {"iso885911", "windows-874"},
-  {"tis-620", "windows-874"},
-  {"windows-874", "windows-874"},
-  {"cp1250", "windows-1250"},
-  {"windows-1250", "windows-1250"},
-  {"x-cp1250", "windows-1250"},
-  {"cp1251", "windows-1251"},
-  {"windows-1251", "windows-1251"},
-  {"x-cp1251", "windows-1251"},
-  {"ansi_x3.4-1968", "windows-1252"},
-  {"ascii", "windows-1252"},
-  {"cp1252", "windows-1252"},
-  {"cp819", "windows-1252"},
-  {"csisolatin1", "windows-1252"},
-  {"ibm819", "windows-1252"},
-  {"iso-8859-1", "windows-1252"},
-  {"iso-ir-100", "windows-1252"},
-  {"iso8859-1", "windows-1252"},
-  {"iso88591", "windows-1252"},
-  {"iso_8859-1", "windows-1252"},
-  {"iso_8859-1:1987", "windows-1252"},
-  {"l1", "windows-1252"},
-  {"latin1", "windows-1252"},
-  {"us-ascii", "windows-1252"},
-  {"windows-1252", "windows-1252"},
-  {"x-cp1252", "windows-1252"},
-  {"cp1253", "windows-1253"},
-  {"windows-1253", "windows-1253"},
-  {"x-cp1253", "windows-1253"},
-  {"cp1254", "windows-1254"},
-  {"csisolatin5", "windows-1254"},
-  {"iso-8859-9", "windows-1254"},
-  {"iso-ir-148", "windows-1254"},
-  {"iso8859-9", "windows-1254"},
-  {"iso88599", "windows-1254"},
-  {"iso_8859-9", "windows-1254"},
-  {"iso_8859-9:1989", "windows-1254"},
-  {"l5", "windows-1254"},
-  {"latin5", "windows-1254"},
-  {"windows-1254", "windows-1254"},
-  {"x-cp1254", "windows-1254"},
-  {"cp1255", "windows-1255"},
-  {"windows-1255", "windows-1255"},
-  {"x-cp1255", "windows-1255"},
-  {"cp1256", "windows-1256"},
-  {"windows-1256", "windows-1256"},
-  {"x-cp1256", "windows-1256"},
-  {"cp1257", "windows-1257"},
-  {"windows-1257", "windows-1257"},
-  {"x-cp1257", "windows-1257"},
-  {"cp1258", "windows-1258"},
-  {"windows-1258", "windows-1258"},
-  {"x-cp1258", "windows-1258"},
-  {"x-mac-cyrillic", "x-mac-cyrillic"},
-  {"x-mac-ukrainian", "x-mac-cyrillic"},
-  {"chinese", "gbk"},
-  {"csgb2312", "gbk"},
-  {"csiso58gb231280", "gbk"},
-  {"gb2312", "gbk"},
-  {"gb_2312", "gbk"},
-  {"gb_2312-80", "gbk"},
-  {"gbk", "gbk"},
-  {"iso-ir-58", "gbk"},
-  {"x-gbk", "gbk"},
-  {"gb18030", "gb18030"},
-  {"hz-gb-2312", "hz-gb-2312"},
-  {"big5", "big5"},
-  {"big5-hkscs", "big5"},
-  {"cn-big5", "big5"},
-  {"csbig5", "big5"},
-  {"x-x-big5", "big5"},
-  {"cseucpkdfmtjapanese", "euc-jp"},
-  {"euc-jp", "euc-jp"},
-  {"x-euc-jp", "euc-jp"},
-  {"csiso2022jp", "iso-2022-jp"},
-  {"iso-2022-jp", "iso-2022-jp"},
-  {"csshiftjis", "shift_jis"},
-  {"ms_kanji", "shift_jis"},
-  {"shift-jis", "shift_jis"},
-  {"shift_jis", "shift_jis"},
-  {"sjis", "shift_jis"},
-  {"windows-31j", "shift_jis"},
-  {"x-sjis", "shift_jis"},
-  {"cseuckr", "euc-kr"},
-  {"csksc56011987", "euc-kr"},
-  {"euc-kr", "euc-kr"},
-  {"iso-ir-149", "euc-kr"},
-  {"korean", "euc-kr"},
-  {"ks_c_5601-1987", "euc-kr"},
-  {"ks_c_5601-1989", "euc-kr"},
-  {"ksc5601", "euc-kr"},
-  {"ksc_5601", "euc-kr"},
-  {"windows-949", "euc-kr"},
-  {"csiso2022kr", "iso-2022-kr"},
-  {"iso-2022-kr", "iso-2022-kr"},
-  {"utf-16", "utf-16le"},
-  {"utf-16le", "utf-16le"},
-  {"utf-16be", "utf-16be"},
-  {"x-user-defined", "x-user-defined"},
-};
-
-EncodingUtils::EncodingUtils()
-{
-  MOZ_ASSERT(!gEncodings);
-
-  const uint32_t numLabels = ArrayLength(labelsEncodings);
-  mLabelsEncodings.Init(numLabels);
-
-  for (uint32_t i = 0; i < numLabels; i++) {
-    mLabelsEncodings.Put(NS_ConvertASCIItoUTF16(labelsEncodings[i].mLabel),
-                         labelsEncodings[i].mEncoding);
-  }
-}
-
-EncodingUtils::~EncodingUtils()
-{
-  MOZ_ASSERT(gEncodings && gEncodings == this);
-}
-
-void
-EncodingUtils::Shutdown()
-{
-  NS_IF_RELEASE(gEncodings);
-}
-
-already_AddRefed<EncodingUtils>
-EncodingUtils::GetOrCreate()
-{
-  if (!gEncodings) {
-    gEncodings = new EncodingUtils();
-    NS_ADDREF(gEncodings);
-  }
-  NS_ADDREF(gEncodings);
-  return gEncodings;
-}
-
 uint32_t
 EncodingUtils::IdentifyDataOffset(const char* aData,
                                   const uint32_t aLength,
-                                  const char*& aRetval)
+                                  nsACString& aRetval)
 {
   // Truncating to pre-clear return value in case of failure.
-  aRetval = "";
+  aRetval.Truncate();
 
   // Minimum bytes in input stream data that represents
   // the Byte Order Mark is 2. Max is 3.
   if (aLength < 2) {
     return 0;
   }
 
   if (aData[0] == '\xFF' && aData[1] == '\xFE') {
-    aRetval = "utf-16le";
+    aRetval.AssignLiteral("UTF-16LE");
     return 2;
   }
 
   if (aData[0] == '\xFE' && aData[1] == '\xFF') {
-    aRetval = "utf-16be";
+    aRetval.AssignLiteral("UTF-16BE");
     return 2;
   }
 
   // Checking utf-8 byte order mark.
   // Minimum bytes in input stream data that represents
   // the Byte Order Mark for utf-8 is 3.
   if (aLength < 3) {
     return 0;
   }
 
   if (aData[0] == '\xEF' && aData[1] == '\xBB' && aData[2] == '\xBF') {
-    aRetval = "utf-8";
+    aRetval.AssignLiteral("UTF-8");
     return 3;
   }
   return 0;
 }
 
 bool
-EncodingUtils::FindEncodingForLabel(const nsAString& aLabel,
-                                    const char*& aOutEncoding)
+EncodingUtils::FindEncodingForLabel(const nsACString& aLabel,
+                                    nsACString& aOutEncoding)
 {
-  nsRefPtr<EncodingUtils> self = EncodingUtils::GetOrCreate();
-  MOZ_ASSERT(self);
-
-  // Save aLabel first because it may be the same as aOutEncoding.
-  nsString label(aLabel);
-  // Truncating to clear aOutEncoding in case of failure.
-  aOutEncoding = EmptyCString().get();
+  // Save aLabel first because it may refer the same string as aOutEncoding.
+  nsCString label(aLabel);
 
   EncodingUtils::TrimSpaceCharacters(label);
   if (label.IsEmpty()) {
+    aOutEncoding.Truncate();
     return false;
   }
 
-  nsContentUtils::ASCIIToLower(label);
-  const char* encoding = self->mLabelsEncodings.Get(label);
-  if (!encoding) {
-    return false;
-  }
-  aOutEncoding = encoding;
-  return true;
+  ToLowerCase(label);
+  return NS_SUCCEEDED(nsUConvPropertySearch::SearchPropertyValue(
+      labelsEncodings, ArrayLength(labelsEncodings), label, aOutEncoding));
 }
 
 } // namespace dom
 } // namespace mozilla
--- a/dom/encoding/EncodingUtils.h
+++ b/dom/encoding/EncodingUtils.h
@@ -9,68 +9,72 @@
 #include "nsString.h"
 
 namespace mozilla {
 namespace dom {
 
 class EncodingUtils
 {
 public:
-  NS_INLINE_DECL_REFCOUNTING(EncodingUtils)
 
   /**
    * Implements decode algorithm's step 1 & 2 from Encoding spec.
    * http://encoding.spec.whatwg.org/#decode
+   * The returned name may not be lowercased due to compatibility with
+   * our internal implementations.
    *
    * @param     aData, incoming byte stream of data.
    * @param     aLength, incoming byte stream length.
    * @param     aRetval, outgoing encoding corresponding to valid data
    *            byte order mark.
    * @return    offset after the BOM bytes in byte stream
    *            where the actual data starts.
    */
   static uint32_t IdentifyDataOffset(const char* aData,
                                      const uint32_t aLength,
-                                     const char*& aRetval);
+                                     nsACString& aRetval);
 
   /**
    * Implements get an encoding algorithm from Encoding spec.
    * http://encoding.spec.whatwg.org/#concept-encoding-get
    * Given a label, this function returns the corresponding encoding or a
    * false.
+   * The returned name may not be lowercased due to compatibility with
+   * our internal implementations.
    *
    * @param      aLabel, incoming label describing charset to be decoded.
    * @param      aRetEncoding, returning corresponding encoding for label.
    * @return     false if no encoding was found for label.
    *             true if valid encoding found.
    */
+  static bool FindEncodingForLabel(const nsACString& aLabel,
+                                   nsACString& aOutEncoding);
+
   static bool FindEncodingForLabel(const nsAString& aLabel,
-                                   const char*& aOutEncoding);
+                                   nsACString& aOutEncoding)
+  {
+    return FindEncodingForLabel(NS_ConvertUTF16toUTF8(aLabel), aOutEncoding);
+  }
 
   /**
    * Remove any leading and trailing space characters, following the
    * definition of space characters from Encoding spec.
    * http://encoding.spec.whatwg.org/#terminology
    * Note that nsAString::StripWhitespace() doesn't exactly match the
    * definition. It also removes all matching chars in the string,
    * not just leading and trailing.
    *
    * @param      aString, string to be trimmed.
    */
-  static void TrimSpaceCharacters(nsString& aString)
+  template<class T>
+  static void TrimSpaceCharacters(T& aString)
   {
     aString.Trim(" \t\n\f\r");
   }
 
-  /* Called to free up Encoding instance. */
-  static void Shutdown();
-
-protected:
-  nsDataHashtable<nsStringHashKey, const char *> mLabelsEncodings;
-  EncodingUtils();
-  virtual ~EncodingUtils();
-  static already_AddRefed<EncodingUtils> GetOrCreate();
+private:
+  EncodingUtils() MOZ_DELETE;
 };
 
 } // dom
 } // mozilla
 
 #endif // mozilla_dom_encodingutils_h_
--- a/dom/encoding/Makefile.in
+++ b/dom/encoding/Makefile.in
@@ -28,11 +28,25 @@ EXPORTS_mozilla/dom = \
   $(NULL)
 
 CPPSRCS = \
 	EncodingUtils.cpp \
 	TextDecoder.cpp \
 	TextEncoder.cpp \
 	$(NULL)
 
+LOCAL_INCLUDES = \
+	-I$(topsrcdir)/intl/locale/src \
+	$(NULL)
+
 include $(topsrcdir)/dom/dom-config.mk
 include $(topsrcdir)/config/config.mk
 include $(topsrcdir)/config/rules.mk
+
+EncodingUtils.$(OBJ_SUFFIX): labelsencodings.properties.h
+
+PROPS2ARRAYS = $(topsrcdir)/intl/locale/src/props2arrays.py
+labelsencodings.properties.h: $(PROPS2ARRAYS) labelsencodings.properties
+	$(PYTHON) $^ $@
+
+GARBAGE += \
+	charsetalias.properties.h \
+	$(NULL)
--- a/dom/encoding/TextDecoder.cpp
+++ b/dom/encoding/TextDecoder.cpp
@@ -23,33 +23,33 @@ TextDecoder::Init(const nsAString& aEnco
 
   // Let encoding be the result of getting an encoding from label.
   // If encoding is failure, throw a TypeError.
   if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) {
     aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label);
     return;
   }
 
-  mIsUTF16Family = !strcmp(mEncoding, "utf-16le") ||
-                   !strcmp(mEncoding, "utf-16be");
+  mIsUTF16Family = mEncoding.EqualsLiteral("UTF-16LE") ||
+                   mEncoding.EqualsLiteral("UTF-16BE");
 
   // If the constructor is called with an options argument,
   // and the fatal property of the dictionary is set,
   // set the internal fatal flag of the decoder object.
   mFatal = aFatal.fatal;
 
   // Create a decoder object for mEncoding.
   nsCOMPtr<nsICharsetConverterManager> ccm =
     do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID);
   if (!ccm) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return;
   }
 
-  ccm->GetUnicodeDecoder(mEncoding, getter_AddRefs(mDecoder));
+  ccm->GetUnicodeDecoderRaw(mEncoding.get(), getter_AddRefs(mDecoder));
   if (!mDecoder) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return;
   }
 
   if (mFatal) {
     mDecoder->SetInputErrorBehavior(nsIUnicodeDecoder::kOnError_Signal);
   }
@@ -163,22 +163,22 @@ TextDecoder::HandleBOM(const char*& aDat
   }
 
   memcpy(mInitialBytes + mOffset, aData, 2 - mOffset);
   // copied data will be fed later.
   aData += 2 - mOffset;
   aLength -= 2 - mOffset;
   mOffset = 2;
 
-  const char* encoding = "";
+  nsAutoCString encoding;
   if (!EncodingUtils::IdentifyDataOffset(mInitialBytes, 2, encoding) ||
-      strcmp(encoding, mEncoding)) {
+      !encoding.Equals(mEncoding)) {
     // If the stream doesn't start with BOM or the BOM doesn't match the
     // encoding, feed a BOM to workaround decoder's bug (bug 634541).
-    FeedBytes(!strcmp(mEncoding, "utf-16le") ? "\xFF\xFE" : "\xFE\xFF");
+    FeedBytes(mEncoding.EqualsLiteral("UTF-16LE") ? "\xFF\xFE" : "\xFE\xFF");
   }
   FeedBytes(mInitialBytes, &aOutString);
 }
 
 void
 TextDecoder::FeedBytes(const char* aBytes, nsAString* aOutString)
 {
   PRUnichar buf[3];
@@ -196,22 +196,23 @@ TextDecoder::FeedBytes(const char* aByte
 void
 TextDecoder::GetEncoding(nsAString& aEncoding)
 {
   // Our utf-16 converter does not comply with the Encoding Standard.
   // As a result the utf-16le converter is used for the encoding label
   // "utf-16".
   // This workaround should not be exposed to the public API and so "utf-16"
   // is returned by GetEncoding() if the internal encoding name is "utf-16le".
-  if (!strcmp(mEncoding, "utf-16le")) {
+  if (mEncoding.EqualsLiteral("UTF-16LE")) {
     aEncoding.AssignLiteral("utf-16");
     return;
   }
 
-  aEncoding.AssignASCII(mEncoding);
+  CopyASCIItoUTF16(mEncoding, aEncoding);
+  nsContentUtils::ASCIIToLower(aEncoding);
 }
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(TextDecoder)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(TextDecoder)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextDecoder)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
--- a/dom/encoding/TextDecoder.h
+++ b/dom/encoding/TextDecoder.h
@@ -87,17 +87,17 @@ public:
    * @param      aRv, error result.
    */
   void Decode(const ArrayBufferView* aView,
               const TextDecodeOptions& aOptions,
               nsAString& aOutDecodedString,
               ErrorResult& aRv);
 
 private:
-  const char* mEncoding;
+  nsCString mEncoding;
   nsCOMPtr<nsIUnicodeDecoder> mDecoder;
   nsCOMPtr<nsISupports> mGlobal;
   bool mFatal;
   uint8_t mOffset;
   char mInitialBytes[3];
   bool mIsUTF16Family;
 
   /**
--- a/dom/encoding/TextEncoder.cpp
+++ b/dom/encoding/TextEncoder.cpp
@@ -21,32 +21,32 @@ TextEncoder::Init(const nsAString& aEnco
   // Let encoding be the result of getting an encoding from label.
   // If encoding is failure, or is none of utf-8, utf-16, and utf-16be,
   // throw a TypeError.
   if (!EncodingUtils::FindEncodingForLabel(label, mEncoding)) {
     aRv.ThrowTypeError(MSG_ENCODING_NOT_SUPPORTED, &label);
     return;
   }
 
-  if (PL_strcasecmp(mEncoding, "utf-8") &&
-      PL_strcasecmp(mEncoding, "utf-16le") &&
-      PL_strcasecmp(mEncoding, "utf-16be")) {
+  if (!mEncoding.EqualsLiteral("UTF-8") &&
+      !mEncoding.EqualsLiteral("UTF-16LE") &&
+      !mEncoding.EqualsLiteral("UTF-16BE")) {
     aRv.ThrowTypeError(MSG_DOM_ENCODING_NOT_UTF);
     return;
   }
 
   // Create an encoder object for mEncoding.
   nsCOMPtr<nsICharsetConverterManager> ccm =
     do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID);
   if (!ccm) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return;
   }
 
-  ccm->GetUnicodeEncoder(mEncoding, getter_AddRefs(mEncoder));
+  ccm->GetUnicodeEncoderRaw(mEncoding.get(), getter_AddRefs(mEncoder));
   if (!mEncoder) {
     aRv.Throw(NS_ERROR_UNEXPECTED);
     return;
   }
 }
 
 JSObject*
 TextEncoder::Encode(JSContext* aCx,
@@ -101,21 +101,23 @@ TextEncoder::Encode(JSContext* aCx,
 void
 TextEncoder::GetEncoding(nsAString& aEncoding)
 {
   // Our utf-16 converter does not comply with the Encoding Standard.
   // As a result the utf-16le converter is used for the encoding label
   // "utf-16".
   // This workaround should not be exposed to the public API and so "utf-16"
   // is returned by GetEncoding() if the internal encoding name is "utf-16le".
-  if (!strcmp(mEncoding, "utf-16le")) {
+  if (mEncoding.EqualsLiteral("UTF-16LE")) {
     aEncoding.AssignLiteral("utf-16");
     return;
   }
-  aEncoding.AssignASCII(mEncoding);
+
+  CopyASCIItoUTF16(mEncoding, aEncoding);
+  nsContentUtils::ASCIIToLower(aEncoding);
 }
 
 NS_IMPL_CYCLE_COLLECTING_ADDREF(TextEncoder)
 NS_IMPL_CYCLE_COLLECTING_RELEASE(TextEncoder)
 
 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TextEncoder)
   NS_INTERFACE_MAP_ENTRY(nsISupports)
 NS_INTERFACE_MAP_END
--- a/dom/encoding/TextEncoder.h
+++ b/dom/encoding/TextEncoder.h
@@ -80,17 +80,17 @@ public:
    *                   the previous encoding is reused/continued.
    * @return JSObject* The Uint8Array wrapped in a JS object.
    */
   JSObject* Encode(JSContext* aCx,
                    const nsAString& aString,
                    const TextEncodeOptions& aOptions,
                    ErrorResult& aRv);
 private:
-  const char* mEncoding;
+  nsCString mEncoding;
   nsCOMPtr<nsIUnicodeEncoder> mEncoder;
   nsCOMPtr<nsISupports> mGlobal;
 
   /**
    * Validates provided encoding and throws an exception if invalid encoding.
    * If no encoding is provided then mEncoding is default initialised to "utf-8".
    *
    * @param aEncoding    Optional encoding (case insensitive) provided.
new file mode 100644
--- /dev/null
+++ b/dom/encoding/labelsencodings.properties
@@ -0,0 +1,223 @@
+# This Source Code Form is subject to the terms of the Mozilla Public
+# License, v. 2.0. If a copy of the MPL was not distributed with this
+# file, You can obtain one at http://mozilla.org/MPL/2.0/.
+#
+# The list is taken from http://encoding.spec.whatwg.org/#encodings
+# The encoding name may not be lowercased due to compatibility with
+# our internal implementations.
+#
+
+unicode-1-1-utf-8=UTF-8
+utf-8=UTF-8
+utf8=UTF-8
+866=IBM866
+cp866=IBM866
+csibm866=IBM866
+ibm866=IBM866
+csisolatin2=ISO-8859-2
+iso-8859-2=ISO-8859-2
+iso-ir-101=ISO-8859-2
+iso8859-2=ISO-8859-2
+iso88592=ISO-8859-2
+iso_8859-2=ISO-8859-2
+iso_8859-2:1987=ISO-8859-2
+l2=ISO-8859-2
+latin2=ISO-8859-2
+csisolatin3=ISO-8859-3
+iso-8859-3=ISO-8859-3
+iso-ir-109=ISO-8859-3
+iso8859-3=ISO-8859-3
+iso88593=ISO-8859-3
+iso_8859-3=ISO-8859-3
+iso_8859-3:1988=ISO-8859-3
+l3=ISO-8859-3
+latin3=ISO-8859-3
+csisolatin4=ISO-8859-4
+iso-8859-4=ISO-8859-4
+iso-ir-110=ISO-8859-4
+iso8859-4=ISO-8859-4
+iso88594=ISO-8859-4
+iso_8859-4=ISO-8859-4
+iso_8859-4:1988=ISO-8859-4
+l4=ISO-8859-4
+latin4=ISO-8859-4
+csisolatincyrillic=ISO-8859-5
+cyrillic=ISO-8859-5
+iso-8859-5=ISO-8859-5
+iso-ir-144=ISO-8859-5
+iso8859-5=ISO-8859-5
+iso88595=ISO-8859-5
+iso_8859-5=ISO-8859-5
+iso_8859-5:1988=ISO-8859-5
+arabic=ISO-8859-6
+asmo-708=ISO-8859-6
+csiso88596e=ISO-8859-6
+csiso88596i=ISO-8859-6
+csisolatinarabic=ISO-8859-6
+ecma-114=ISO-8859-6
+iso-8859-6=ISO-8859-6
+iso-8859-6-e=ISO-8859-6
+iso-8859-6-i=ISO-8859-6
+iso-ir-127=ISO-8859-6
+iso8859-6=ISO-8859-6
+iso88596=ISO-8859-6
+iso_8859-6=ISO-8859-6
+iso_8859-6:1987=ISO-8859-6
+csisolatingreek=ISO-8859-7
+ecma-118=ISO-8859-7
+elot_928=ISO-8859-7
+greek=ISO-8859-7
+greek8=ISO-8859-7
+iso-8859-7=ISO-8859-7
+iso-ir-126=ISO-8859-7
+iso8859-7=ISO-8859-7
+iso88597=ISO-8859-7
+iso_8859-7=ISO-8859-7
+iso_8859-7:1987=ISO-8859-7
+sun_eu_greek=ISO-8859-7
+csiso88598e=ISO-8859-8
+csisolatinhebrew=ISO-8859-8
+hebrew=ISO-8859-8
+iso-8859-8=ISO-8859-8
+iso-8859-8-e=ISO-8859-8
+iso-ir-138=ISO-8859-8
+iso8859-8=ISO-8859-8
+iso88598=ISO-8859-8
+iso_8859-8=ISO-8859-8
+iso_8859-8:1988=ISO-8859-8
+visual=ISO-8859-8
+csiso88598i=ISO-8859-8-I
+iso-8859-8-i=ISO-8859-8-I
+logical=ISO-8859-8-I
+csisolatin6=ISO-8859-10
+iso-8859-10=ISO-8859-10
+iso-ir-157=ISO-8859-10
+iso8859-10=ISO-8859-10
+iso885910=ISO-8859-10
+l6=ISO-8859-10
+latin6=ISO-8859-10
+iso-8859-13=ISO-8859-13
+iso8859-13=ISO-8859-13
+iso885913=ISO-8859-13
+iso-8859-14=ISO-8859-14
+iso8859-14=ISO-8859-14
+iso885914=ISO-8859-14
+csisolatin9=ISO-8859-15
+iso-8859-15=ISO-8859-15
+iso8859-15=ISO-8859-15
+iso885915=ISO-8859-15
+iso_8859-15=ISO-8859-15
+l9=ISO-8859-15
+iso-8859-16=ISO-8859-16
+cskoi8r=KOI8-R
+koi=KOI8-R
+koi8=KOI8-R
+koi8-r=KOI8-R
+koi8_r=KOI8-R
+koi8-u=KOI8-U
+csmacintosh=macintosh
+mac=macintosh
+macintosh=macintosh
+x-mac-roman=macintosh
+dos-874=windows-874
+iso-8859-11=windows-874
+iso8859-11=windows-874
+iso885911=windows-874
+tis-620=windows-874
+windows-874=windows-874
+cp1250=windows-1250
+windows-1250=windows-1250
+x-cp1250=windows-1250
+cp1251=windows-1251
+windows-1251=windows-1251
+x-cp1251=windows-1251
+ansi_x3.4-1968=windows-1252
+ascii=windows-1252
+cp1252=windows-1252
+cp819=windows-1252
+csisolatin1=windows-1252
+ibm819=windows-1252
+iso-8859-1=windows-1252
+iso-ir-100=windows-1252
+iso8859-1=windows-1252
+iso88591=windows-1252
+iso_8859-1=windows-1252
+iso_8859-1:1987=windows-1252
+l1=windows-1252
+latin1=windows-1252
+us-ascii=windows-1252
+windows-1252=windows-1252
+x-cp1252=windows-1252
+cp1253=windows-1253
+windows-1253=windows-1253
+x-cp1253=windows-1253
+cp1254=windows-1254
+csisolatin5=windows-1254
+iso-8859-9=windows-1254
+iso-ir-148=windows-1254
+iso8859-9=windows-1254
+iso88599=windows-1254
+iso_8859-9=windows-1254
+iso_8859-9:1989=windows-1254
+l5=windows-1254
+latin5=windows-1254
+windows-1254=windows-1254
+x-cp1254=windows-1254
+cp1255=windows-1255
+windows-1255=windows-1255
+x-cp1255=windows-1255
+cp1256=windows-1256
+windows-1256=windows-1256
+x-cp1256=windows-1256
+cp1257=windows-1257
+windows-1257=windows-1257
+x-cp1257=windows-1257
+cp1258=windows-1258
+windows-1258=windows-1258
+x-cp1258=windows-1258
+x-mac-cyrillic=x-mac-cyrillic
+x-mac-ukrainian=x-mac-cyrillic
+chinese=gbk
+csgb2312=gbk
+csiso58gb231280=gbk
+gb2312=gbk
+gb_2312=gbk
+gb_2312-80=gbk
+gbk=gbk
+iso-ir-58=gbk
+x-gbk=gbk
+gb18030=gb18030
+hz-gb-2312=HZ-GB-2312
+big5=Big5
+big5-hkscs=Big5
+cn-big5=Big5
+csbig5=Big5
+x-x-big5=Big5
+cseucpkdfmtjapanese=EUC-JP
+euc-jp=EUC-JP
+x-euc-jp=EUC-JP
+csiso2022jp=ISO-2022-JP
+iso-2022-jp=ISO-2022-JP
+csshiftjis=Shift_JIS
+ms_kanji=Shift_JIS
+shift-jis=Shift_JIS
+shift_jis=Shift_JIS
+sjis=Shift_JIS
+windows-31j=Shift_JIS
+x-sjis=Shift_JIS
+cseuckr=EUC-KR
+csksc56011987=EUC-KR
+euc-kr=EUC-KR
+iso-ir-149=EUC-KR
+korean=EUC-KR
+ks_c_5601-1987=EUC-KR
+ks_c_5601-1989=EUC-KR
+ksc5601=EUC-KR
+ksc_5601=EUC-KR
+windows-949=EUC-KR
+csiso2022kr=ISO-2022-KR
+iso-2022-kr=ISO-2022-KR
+utf-16=UTF-16LE
+utf-16le=UTF-16LE
+utf-16be=UTF-16BE
+x-user-defined=x-user-defined