Bug 1511950 - Add missing calls to GetCharsetAlias() to take Thunderbird's aliases into account. r=mkmelin
authorJorg K <jorgk@jorgk.com>
Wed, 05 Dec 2018 11:27:59 +0100
changeset 33852 7fd582467790edf2957a5433b818828c71c6c368
parent 33851 c665681e7f02fa6bf60236e918576fc039b6f060
child 33853 654b68255a76f5204502f8ad10a2b9febad5a860
push id388
push userclokep@gmail.com
push dateMon, 28 Jan 2019 20:54:56 +0000
reviewersmkmelin
bugs1511950
Bug 1511950 - Add missing calls to GetCharsetAlias() to take Thunderbird's aliases into account. r=mkmelin
mailnews/base/util/nsMsgI18N.cpp
mailnews/mime/src/mimemoz2.cpp
--- a/mailnews/base/util/nsMsgI18N.cpp
+++ b/mailnews/base/util/nsMsgI18N.cpp
@@ -67,30 +67,42 @@ nsresult nsMsgI18NConvertFromUnicode(con
 nsresult nsMsgI18NConvertToUnicode(const nsACString& aCharset,
                                    const nsACString& inString,
                                    nsAString& outString)
 {
   if (inString.IsEmpty()) {
     outString.Truncate();
     return NS_OK;
   }
-  else if (aCharset.IsEmpty()) {
+  if (aCharset.IsEmpty()) {
     // Despite its name, it also works for Latin-1.
     CopyASCIItoUTF16(inString, outString);
     return NS_OK;
   }
-  else if (aCharset.Equals("UTF-7", nsCaseInsensitiveCStringComparator())) {
+
+  if (aCharset.Equals("UTF-8", nsCaseInsensitiveCStringComparator())) {
+    return UTF_8_ENCODING->DecodeWithBOMRemoval(inString, outString);
+  }
+
+  // Look up Thunderbird's special aliases from charsetalias.properties.
+  nsresult rv;
+  nsCOMPtr<nsICharsetConverterManager> ccm =
+    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsCString newCharset;
+  rv = ccm->GetCharsetAlias(PromiseFlatCString(aCharset).get(), newCharset);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (newCharset.Equals("UTF-7", nsCaseInsensitiveCStringComparator())) {
     // Special treatment for decoding UTF-7 since it's not handled by encoding_rs.
     return CopyUTF7toUTF16(inString, outString);
   }
-  else if (aCharset.Equals("UTF-8", nsCaseInsensitiveCStringComparator())) {
-    return UTF_8_ENCODING->DecodeWithBOMRemoval(inString, outString);
-  }
 
-  auto encoding = mozilla::Encoding::ForLabelNoReplacement(aCharset);
+  auto encoding = mozilla::Encoding::ForLabelNoReplacement(newCharset);
   if (!encoding)
     return NS_ERROR_UCONV_NOCONV;
   return encoding->DecodeWithoutBOMHandlingAndWithoutReplacement(inString,
                                                                  outString);
 }
 
 // This is used to decode UTF-7. No support for encoding in UTF-7.
 nsresult CopyUTF7toUTF16(const nsACString& aSrc, nsAString& aDest)
--- a/mailnews/mime/src/mimemoz2.cpp
+++ b/mailnews/mime/src/mimemoz2.cpp
@@ -806,26 +806,35 @@ mime_file_type (const char *filename, vo
 }
 
 int ConvertToUTF8(const char *stringToUse, int32_t inLength,
                   const char *input_charset,
                   nsACString& outString)
 {
   nsresult rv = NS_OK;
 
-  if (PL_strcasecmp(input_charset, "UTF-7") == 0) {
+  // Look up Thunderbird's special aliases from charsetalias.properties.
+  nsCOMPtr<nsICharsetConverterManager> ccm =
+    do_GetService(NS_CHARSETCONVERTERMANAGER_CONTRACTID, &rv);
+  NS_ENSURE_SUCCESS(rv, -1);
+
+  nsCString newCharset;
+  rv = ccm->GetCharsetAlias(input_charset, newCharset);
+  NS_ENSURE_SUCCESS(rv, -1);
+
+  if (newCharset.Equals("UTF-7", nsCaseInsensitiveCStringComparator())) {
     nsAutoString utf16;
     rv = CopyUTF7toUTF16(nsDependentCString(stringToUse, inLength), utf16);
     if (NS_FAILED(rv))
       return -1;
     CopyUTF16toUTF8(utf16, outString);
     return 0;
   }
 
-  auto encoding = mozilla::Encoding::ForLabel(nsDependentCString(input_charset));
+  auto encoding = mozilla::Encoding::ForLabel(newCharset);
   if (!encoding) {
     // Assume input is UTF-8.
     encoding = UTF_8_ENCODING;
   }
 
   rv = encoding->DecodeWithoutBOMHandling(nsDependentCSubstring(stringToUse, inLength), outString);
   return NS_SUCCEEDED(rv) ? 0 : -1;
 }