Bug 937056 - Add UTF-7 converters to comm-central. r=Standard8
authorHenri Sivonen <hsivonen@hsivonen.fi>
Mon, 14 Apr 2014 17:04:02 +0300
changeset 19908 c36c9c4b65e510b00e933235b2305db093fbfa7b
parent 19907 3bf3e1ea970280b5a9fee3bdae5a5494e28f7640
child 19909 e3b89706dfb6c58b0e31671024e432b867f5c786
push idunknown
push userunknown
push dateunknown
reviewersStandard8
bugs937056
Bug 937056 - Add UTF-7 converters to comm-central. r=Standard8
mailnews/build/Makefile.in
mailnews/build/nsMailModule.cpp
mailnews/intl/moz.build
mailnews/intl/nsCommUConvCID.h
mailnews/intl/nsMUTF7ToUnicode.cpp
mailnews/intl/nsMUTF7ToUnicode.h
mailnews/intl/nsUTF7ToUnicode.cpp
mailnews/intl/nsUTF7ToUnicode.h
mailnews/intl/nsUnicodeToMUTF7.cpp
mailnews/intl/nsUnicodeToMUTF7.h
mailnews/intl/nsUnicodeToUTF7.cpp
mailnews/intl/nsUnicodeToUTF7.h
mailnews/intl/test/Makefile
mailnews/intl/test/moz.build
mailnews/intl/test/unit/test_decode_utf-7.js
mailnews/intl/test/unit/test_decode_utf-7_internal.js
mailnews/intl/test/unit/test_encode_utf-7.js
mailnews/intl/test/unit/test_encode_utf-7_internal.js
mailnews/intl/test/unit/xpcshell.ini
mailnews/moz.build
--- a/mailnews/build/Makefile.in
+++ b/mailnews/build/Makefile.in
@@ -37,16 +37,17 @@ EXTRA_DSO_LDOPTS += \
 endif 
 
 LOCAL_INCLUDES = -I$(srcdir) \
                  -I$(srcdir)/../base/src \
                  -I$(srcdir)/../base/util \
                  -I$(srcdir)/../local/src \
                  -I$(srcdir)/../addrbook/src \
                  -I$(srcdir)/../imap/src \
+                 -I$(srcdir)/../intl \
                  -I$(srcdir)/../compose/src \
                  -I$(srcdir)/../base/search/src \
                  -I$(srcdir)/../mime/src \
                  -I$(srcdir)/../mime/emitters \
                  -I$(srcdir)/../news/src \
                  -I$(srcdir)/../extensions/bayesian-spam-filter/src \
                  -I$(srcdir)/../extensions/fts3/src \
                  -I$(srcdir)/../extensions/mailviews/src \
--- a/mailnews/build/nsMailModule.cpp
+++ b/mailnews/build/nsMailModule.cpp
@@ -295,16 +295,27 @@
 #include "nsFts3Tokenizer.h"
 
 ////////////////////////////////////////////////////////////////////////////////
 // PGP/MIME includes
 ////////////////////////////////////////////////////////////////////////////////
 #include "nsPgpMimeProxy.h"
 
 ////////////////////////////////////////////////////////////////////////////////
+// i18n includes
+////////////////////////////////////////////////////////////////////////////////
+#include "nsEncoderDecoderUtils.h"
+#include "nsCommUConvCID.h"
+
+#include "nsUTF7ToUnicode.h"
+#include "nsMUTF7ToUnicode.h"
+#include "nsUnicodeToUTF7.h"
+#include "nsUnicodeToMUTF7.h"
+
+////////////////////////////////////////////////////////////////////////////////
 // mailnews base factories
 ////////////////////////////////////////////////////////////////////////////////
 using namespace mozilla::mailnews;
 
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMessengerBootstrap)
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsMsgMailSession, Init)
 NS_GENERIC_FACTORY_CONSTRUCTOR(nsMessenger)
 NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(nsMsgAccountManager, Init)
@@ -800,16 +811,30 @@ nsPgpMimeMimeContentTypeHandlerConstruct
     new nsMimeContentTypeHandler("mulitpart/encrypted",
                                  &MIME_PgpMimeCreateContentTypeHandlerClass));
 
   NS_ENSURE_TRUE(inst, NS_ERROR_OUT_OF_MEMORY);
 
   return inst->QueryInterface(aIID, aResult);
 }
 
+////////////////////////////////////////////////////////////////////////////////
+// i18n factories
+////////////////////////////////////////////////////////////////////////////////
+
+NS_GENERIC_FACTORY_CONSTRUCTOR(nsUTF7ToUnicode)
+NS_GENERIC_FACTORY_CONSTRUCTOR(nsMUTF7ToUnicode)
+NS_GENERIC_FACTORY_CONSTRUCTOR(nsUnicodeToUTF7)
+NS_GENERIC_FACTORY_CONSTRUCTOR(nsUnicodeToMUTF7)
+
+NS_DEFINE_NAMED_CID(NS_UTF7TOUNICODE_CID);
+NS_DEFINE_NAMED_CID(NS_MUTF7TOUNICODE_CID);
+NS_DEFINE_NAMED_CID(NS_UNICODETOUTF7_CID);
+NS_DEFINE_NAMED_CID(NS_UNICODETOMUTF7_CID);
+
 const mozilla::Module::CIDEntry kMailNewsCIDs[] = {
   // MailNews Base Entries
   { &kNS_MESSENGERBOOTSTRAP_CID, false, NULL, nsMessengerBootstrapConstructor },
   { &kNS_MESSENGERWINDOWSERVICE_CID, false, NULL, nsMessengerBootstrapConstructor},
   { &kNS_MSGMAILSESSION_CID, false, NULL, nsMsgMailSessionConstructor},
   { &kNS_MESSENGER_CID, false, NULL,nsMessengerConstructor},
   { &kNS_MSGACCOUNTMANAGER_CID, false, NULL, nsMsgAccountManagerConstructor},
   { &kNS_MSGACCOUNT_CID, false, NULL, nsMsgAccountConstructor},
@@ -999,16 +1024,21 @@ const mozilla::Module::CIDEntry kMailNew
   { &kNS_MSGSMIMECOMPFIELDS_CID, false, NULL, nsMsgSMIMEComposeFieldsConstructor },
   { &kNS_SMIMEJSJELPER_CID, false, NULL, nsSMimeJSHelperConstructor },
   { &kNS_SMIMEENCRYPTURISERVICE_CID, false, NULL, nsEncryptedSMIMEURIsServiceConstructor },
   // Vcard Entries
   { &kNS_VCARD_CONTENT_TYPE_HANDLER_CID, false, NULL, nsVCardMimeContentTypeHandlerConstructor},
   // PGP/MIME Entries
   { &kNS_PGPMIME_CONTENT_TYPE_HANDLER_CID, false, NULL, nsPgpMimeMimeContentTypeHandlerConstructor },
   { &kNS_PGPMIMEPROXY_CID, false, NULL, nsPgpMimeProxyConstructor },
+  // i18n Entries
+  { &kNS_UTF7TOUNICODE_CID, false, nullptr, nsUTF7ToUnicodeConstructor },
+  { &kNS_MUTF7TOUNICODE_CID, false, nullptr, nsMUTF7ToUnicodeConstructor },
+  { &kNS_UNICODETOUTF7_CID, false, nullptr, nsUnicodeToUTF7Constructor },
+  { &kNS_UNICODETOMUTF7_CID, false, nullptr, nsUnicodeToMUTF7Constructor },
   // Tokenizer Entries
   { NULL }
 };
 
 const mozilla::Module::ContractIDEntry kMailNewsContracts[] = {
   // MailNews Base Entries
   { NS_MESSENGERBOOTSTRAP_CONTRACTID, &kNS_MESSENGERBOOTSTRAP_CID },
   { NS_MESSENGERWINDOWSERVICE_CONTRACTID, &kNS_MESSENGERWINDOWSERVICE_CID },
@@ -1229,16 +1259,21 @@ const mozilla::Module::ContractIDEntry k
   { NS_MSGSMIMECOMPFIELDS_CONTRACTID, &kNS_MSGSMIMECOMPFIELDS_CID },
   { NS_SMIMEJSHELPER_CONTRACTID, &kNS_SMIMEJSJELPER_CID },
   { NS_SMIMEENCRYPTURISERVICE_CONTRACTID, &kNS_SMIMEENCRYPTURISERVICE_CID },
   // Vcard Entries
   { "@mozilla.org/mimecth;1?type=text/x-vcard", &kNS_VCARD_CONTENT_TYPE_HANDLER_CID },
   // PGP/MIME Entries
   { "@mozilla.org/mimecth;1?type=multipart/encrypted", &kNS_PGPMIME_CONTENT_TYPE_HANDLER_CID },
   { NS_PGPMIMEPROXY_CONTRACTID, &kNS_PGPMIMEPROXY_CID },
+  // i18n Entries
+  { NS_UNICODEDECODER_CONTRACTID_BASE "UTF-7", &kNS_UTF7TOUNICODE_CID },
+  { NS_UNICODEDECODER_CONTRACTID_BASE "x-imap4-modified-utf7", &kNS_MUTF7TOUNICODE_CID },
+  { NS_UNICODEENCODER_CONTRACTID_BASE "UTF-7", &kNS_UNICODETOUTF7_CID },
+  { NS_UNICODEENCODER_CONTRACTID_BASE "x-imap4-modified-utf7", &kNS_UNICODETOMUTF7_CID },
   // Tokenizer Entries
   { NULL }
 };
 
 static const mozilla::Module::CategoryEntry kMailNewsCategories[] = {
   // MailNews Base Entries
   { XPCOM_DIRECTORY_PROVIDER_CATEGORY, "mail-directory-provider", NS_MAILDIRPROVIDER_CONTRACTID },
   { "content-policy", NS_MSGCONTENTPOLICY_CONTRACTID, NS_MSGCONTENTPOLICY_CONTRACTID },
@@ -1259,16 +1294,19 @@ static const mozilla::Module::CategoryEn
   { "mime-emitter", NS_HTML_MIME_EMITTER_CONTRACTID, NS_HTML_MIME_EMITTER_CONTRACTID },
   { "mime-emitter", NS_XML_MIME_EMITTER_CONTRACTID, NS_XML_MIME_EMITTER_CONTRACTID },
   { "mime-emitter", NS_PLAIN_MIME_EMITTER_CONTRACTID, NS_PLAIN_MIME_EMITTER_CONTRACTID },
   { "mime-emitter", NS_RAW_MIME_EMITTER_CONTRACTID, NS_RAW_MIME_EMITTER_CONTRACTID },
   // News Entries
   { "command-line-handler", "m-news", NS_NEWSSTARTUPHANDLER_CONTRACTID },
   // Mail View Entries
   // mdn Entries
+  // i18n Entries
+  NS_UCONV_REG_UNREG("UTF-7", NS_UTF7TOUNICODE_CID, NS_UNICODETOUTF7_CID)
+  NS_UCONV_REG_UNREG("x-imap4-modified-utf7", NS_MUTF7TOUNICODE_CID, NS_UNICODETOMUTF7_CID)
   // Tokenizer Entries
   { NULL }
 };
 
 static void
 msgMailNewsModuleDtor()
 {
   nsAddrDatabase::CleanupCache();
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/moz.build
@@ -0,0 +1,19 @@
+# -*- Mode: python; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 40 -*-
+# vim: set filetype=python:
+# 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/.
+
+UNIFIED_SOURCES += [
+    'nsMUTF7ToUnicode.cpp',
+    'nsUnicodeToMUTF7.cpp',
+    'nsUnicodeToUTF7.cpp',
+    'nsUTF7ToUnicode.cpp',
+]
+
+MSVC_ENABLE_PGO = True
+
+FINAL_LIBRARY = 'mail'
+
+# Tests need more attention before they can be enabled.
+# TEST_DIRS += ['test']
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/nsCommUConvCID.h
@@ -0,0 +1,26 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+// Class ID for our UTF7ToUnicode charset converter
+// {77CFAAF1-1CF4-11d3-8AAF-00600811A836}
+#define NS_UTF7TOUNICODE_CID \
+  { 0x77cfaaf1, 0x1cf4, 0x11d3, {0x8a, 0xaf, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36}}
+
+// Class ID for our MUTF7ToUnicode charset converter
+// {B57F97C1-0D70-11d3-8AAE-00600811A836}
+#define NS_MUTF7TOUNICODE_CID \
+  { 0xb57f97c1, 0xd70, 0x11d3, {0x8a, 0xae, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36}}
+
+// Class ID for our UnicodeToUTF7 charset converter
+// {77CFAAF2-1CF4-11d3-8AAF-00600811A836}
+#define NS_UNICODETOUTF7_CID \
+  { 0x77cfaaf2, 0x1cf4, 0x11d3, {0x8a, 0xaf, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36}}
+
+// Class ID for our UnicodeToMUTF7 charset converter
+// {B57F97C2-0D70-11d3-8AAE-00600811A836}
+#define NS_UNICODETOMUTF7_CID \
+  { 0xb57f97c2, 0xd70, 0x11d3, {0x8a, 0xae, 0x0, 0x60, 0x8, 0x11, 0xa8, 0x36}}
+
+
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/nsMUTF7ToUnicode.cpp
@@ -0,0 +1,14 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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 "nsMUTF7ToUnicode.h"
+
+//----------------------------------------------------------------------
+// Class nsMUTF7ToUnicode [implementation]
+
+nsMUTF7ToUnicode::nsMUTF7ToUnicode()
+: nsBasicUTF7Decoder(',', '&')
+{
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/nsMUTF7ToUnicode.h
@@ -0,0 +1,31 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+#ifndef nsMUTF7ToUnicode_h___
+#define nsMUTF7ToUnicode_h___
+
+#include "nsUTF7ToUnicode.h"
+
+//----------------------------------------------------------------------
+// Class nsMUTF7ToUnicode [declaration]
+
+/**
+ * A character set converter from Modified UTF7 to Unicode.
+ *
+ * @created         18/May/1999
+ * @author  Catalin Rotaru [CATA]
+ */
+class nsMUTF7ToUnicode : public nsBasicUTF7Decoder
+{
+public:
+
+  /**
+   * Class constructor.
+   */
+  nsMUTF7ToUnicode();
+
+};
+
+#endif /* nsMUTF7ToUnicode_h___ */
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/nsUTF7ToUnicode.cpp
@@ -0,0 +1,228 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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 "nsUTF7ToUnicode.h"
+
+#define ENC_DIRECT      0
+#define ENC_BASE64      1
+
+//----------------------------------------------------------------------
+// Class nsBasicUTF7Decoder [implementation]
+
+nsBasicUTF7Decoder::nsBasicUTF7Decoder(char aLastChar, char aEscChar)
+: nsBufferDecoderSupport(1)
+{
+  mLastChar = aLastChar;
+  mEscChar = aEscChar;
+  Reset();
+}
+
+nsresult nsBasicUTF7Decoder::DecodeDirect(
+                             const char * aSrc,
+                             int32_t * aSrcLength,
+                             char16_t * aDest,
+                             int32_t * aDestLength)
+{
+  const char * srcEnd = aSrc + *aSrcLength;
+  const char * src = aSrc;
+  char16_t * destEnd = aDest + *aDestLength;
+  char16_t * dest = aDest;
+  nsresult res = NS_OK;
+  char ch;
+
+  while (src < srcEnd) {
+    ch = *src;
+
+    // stop when we meet other chars or end of direct encoded seq.
+    // if (!(DirectEncodable(ch)) || (ch == mEscChar)) {
+    // but we are decoding; so we should be lax; pass everything until escchar
+    if (ch == mEscChar) {
+      res = NS_ERROR_UDEC_ILLEGALINPUT;
+      break;
+    }
+
+    if (dest >= destEnd) {
+      res = NS_OK_UDEC_MOREOUTPUT;
+      break;
+    } else {
+      *dest++ = ch;
+      src++;
+    }
+  }
+
+  *aSrcLength = src - aSrc;
+  *aDestLength = dest - aDest;
+  return res;
+}
+
+nsresult nsBasicUTF7Decoder::DecodeBase64(
+                             const char * aSrc,
+                             int32_t * aSrcLength,
+                             char16_t * aDest,
+                             int32_t * aDestLength)
+{
+  const char * srcEnd = aSrc + *aSrcLength;
+  const char * src = aSrc;
+  char16_t * destEnd = aDest + *aDestLength;
+  char16_t * dest = aDest;
+  nsresult res = NS_OK;
+  char ch;
+  uint32_t value;
+
+  while (src < srcEnd) {
+    ch = *src;
+
+    // stop when we meet other chars or end of direct encoded seq.
+    value = CharToValue(ch);
+    if (value > 0xff) {
+      res = NS_ERROR_UDEC_ILLEGALINPUT;
+      break;
+    }
+
+    switch (mEncStep) {
+      case 0:
+        mEncBits = value << 10;
+        break;
+      case 1:
+        mEncBits += value << 4;
+        break;
+      case 2:
+        if (dest >= destEnd) {
+          res = NS_OK_UDEC_MOREOUTPUT;
+          break;
+        }
+        mEncBits += value >> 2;
+        *(dest++) = (char16_t) mEncBits;
+        mEncBits = (value & 0x03) << 14;
+        break;
+      case 3:
+        mEncBits += value << 8;
+        break;
+      case 4:
+        mEncBits += value << 2;
+        break;
+      case 5:
+        if (dest >= destEnd) {
+          res = NS_OK_UDEC_MOREOUTPUT;
+          break;
+        }
+        mEncBits += value >> 4;
+        *(dest++) = (char16_t) mEncBits;
+        mEncBits = (value & 0x0f) << 12;
+        break;
+      case 6:
+        mEncBits += value << 6;
+        break;
+      case 7:
+        if (dest >= destEnd) {
+          res = NS_OK_UDEC_MOREOUTPUT;
+          break;
+        }
+        mEncBits += value;
+        *(dest++) = (char16_t) mEncBits;
+        mEncBits = 0;
+        break;
+    }
+
+    if (res != NS_OK) break;
+
+    src++;
+    (++mEncStep)%=8;
+  }
+
+  *aSrcLength = src - aSrc;
+  *aDestLength = dest - aDest;
+  return res;
+}
+
+uint32_t nsBasicUTF7Decoder::CharToValue(char aChar) {
+  if ((aChar>='A')&&(aChar<='Z'))
+    return (uint8_t)(aChar-'A');
+  else if ((aChar>='a')&&(aChar<='z'))
+    return (uint8_t)(26+aChar-'a');
+  else if ((aChar>='0')&&(aChar<='9'))
+    return (uint8_t)(26+26+aChar-'0');
+  else if (aChar=='+')
+    return (uint8_t)(26+26+10);
+  else if (aChar==mLastChar)
+    return (uint8_t)(26+26+10+1);
+  else
+    return 0xffff;
+}
+
+//----------------------------------------------------------------------
+// Subclassing of nsBufferDecoderSupport class [implementation]
+
+NS_IMETHODIMP nsBasicUTF7Decoder::ConvertNoBuff(const char * aSrc,
+                                                int32_t * aSrcLength,
+                                                char16_t * aDest,
+                                                int32_t * aDestLength)
+{
+  const char * srcEnd = aSrc + *aSrcLength;
+  const char * src = aSrc;
+  char16_t * destEnd = aDest + *aDestLength;
+  char16_t * dest = aDest;
+  int32_t bcr,bcw;
+  nsresult res = NS_OK;
+
+  while (src < srcEnd) {
+
+    // fist, attept to decode in the current mode
+    bcr = srcEnd - src;
+    bcw = destEnd - dest;
+    if (mEncoding == ENC_DIRECT)
+      res = DecodeDirect(src, &bcr, dest, &bcw);
+    else if ((mFreshBase64) && (*src == '-')) {
+      *dest = mEscChar;
+      bcr = 0;
+      bcw = 1;
+      res = NS_ERROR_UDEC_ILLEGALINPUT;
+    } else {
+      mFreshBase64 = false;
+      res = DecodeBase64(src, &bcr, dest, &bcw);
+    }
+    src += bcr;
+    dest += bcw;
+
+    // if an illegal char was encountered, test if it is an escape seq.
+    if (res == NS_ERROR_UDEC_ILLEGALINPUT) {
+      if (mEncoding == ENC_DIRECT) {
+        if (*src == mEscChar) {
+          mEncoding = ENC_BASE64;
+          mFreshBase64 = true;
+          mEncBits = 0;
+          mEncStep = 0;
+          src++;
+          res = NS_OK;
+        } else break;
+      } else {
+        mEncoding = ENC_DIRECT;
+        res = NS_OK;
+        // absorbe end of escape sequence
+        if (*src == '-') src++;
+      }
+    } else if (res != NS_OK) break;
+  }
+
+  *aSrcLength = src - aSrc;
+  *aDestLength = dest - aDest;
+  return res;
+}
+
+NS_IMETHODIMP nsBasicUTF7Decoder::Reset()
+{
+  mEncoding = ENC_DIRECT;
+  mEncBits = 0;
+  mEncStep = 0;
+  return nsBufferDecoderSupport::Reset();
+}
+
+//----------------------------------------------------------------------
+// Class nsUTF7ToUnicode [implementation]
+
+nsUTF7ToUnicode::nsUTF7ToUnicode()
+: nsBasicUTF7Decoder('/', '+')
+{
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/nsUTF7ToUnicode.h
@@ -0,0 +1,72 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+#ifndef nsUTF7ToUnicode_h___
+#define nsUTF7ToUnicode_h___
+
+#include "nsUCSupport.h"
+
+//----------------------------------------------------------------------
+// Class nsBasicUTF7Decoder [declaration]
+
+/**
+ * Basic class for a character set converter from UTF-7 to Unicode.
+ *
+ * @created         03/Jun/1999
+ * @author  Catalin Rotaru [CATA]
+ */
+class nsBasicUTF7Decoder : public nsBufferDecoderSupport
+{
+public:
+
+  /**
+   * Class constructor.
+   */
+  nsBasicUTF7Decoder(char aLastChar, char aEscChar);
+
+protected:
+
+  int32_t                   mEncoding;      // current encoding
+  uint32_t                  mEncBits;
+  int32_t                   mEncStep;
+  char                      mLastChar;
+  char                      mEscChar;
+  bool                      mFreshBase64;
+
+  nsresult DecodeDirect(const char * aSrc, int32_t * aSrcLength,
+      char16_t * aDest, int32_t * aDestLength);
+  nsresult DecodeBase64(const char * aSrc, int32_t * aSrcLength,
+      char16_t * aDest, int32_t * aDestLength);
+  uint32_t CharToValue(char aChar);
+
+  //--------------------------------------------------------------------
+  // Subclassing of nsBufferDecoderSupport class [declaration]
+
+  NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength,
+      char16_t * aDest, int32_t * aDestLength);
+  NS_IMETHOD Reset();
+};
+
+//----------------------------------------------------------------------
+// Class nsUTF7ToUnicode [declaration]
+
+/**
+ * A character set converter from Modified UTF7 to Unicode.
+ *
+ * @created         18/May/1999
+ * @author  Catalin Rotaru [CATA]
+ */
+class nsUTF7ToUnicode : public nsBasicUTF7Decoder
+{
+public:
+
+  /**
+   * Class constructor.
+   */
+  nsUTF7ToUnicode();
+
+};
+
+#endif /* nsUTF7ToUnicode_h___ */
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/nsUnicodeToMUTF7.cpp
@@ -0,0 +1,14 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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 "nsUnicodeToMUTF7.h"
+
+//----------------------------------------------------------------------
+// Class nsUnicodeToMUTF7 [implementation]
+
+nsUnicodeToMUTF7::nsUnicodeToMUTF7()
+: nsBasicUTF7Encoder(',', '&')
+{
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/nsUnicodeToMUTF7.h
@@ -0,0 +1,31 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+#ifndef nsUnicodeToMUTF7_h___
+#define nsUnicodeToMUTF7_h___
+
+#include "nsUnicodeToUTF7.h"
+
+//----------------------------------------------------------------------
+// Class nsUnicodeToMUTF7 [declaration]
+
+/**
+ * A character set converter from Unicode to Modified UTF-7.
+ *
+ * @created         18/May/1999
+ * @author  Catalin Rotaru [CATA]
+ */
+class nsUnicodeToMUTF7 : public nsBasicUTF7Encoder
+{
+public:
+
+  /**
+   * Class constructor.
+   */
+  nsUnicodeToMUTF7();
+
+};
+
+#endif /* nsUnicodeToMUTF7_h___ */
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/nsUnicodeToUTF7.cpp
@@ -0,0 +1,298 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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 "nsUnicodeToUTF7.h"
+#include <string.h>
+
+//----------------------------------------------------------------------
+// Global functions and data [declaration]
+
+#define ENC_DIRECT      0
+#define ENC_BASE64      1
+
+//----------------------------------------------------------------------
+// Class nsBasicUTF7Encoder [implementation]
+
+nsBasicUTF7Encoder::nsBasicUTF7Encoder(char aLastChar, char aEscChar)
+: nsEncoderSupport(5)
+{
+  mLastChar = aLastChar;
+  mEscChar = aEscChar;
+  Reset();
+}
+
+nsresult nsBasicUTF7Encoder::ShiftEncoding(int32_t aEncoding,
+                                          char * aDest,
+                                          int32_t * aDestLength)
+{
+  if (aEncoding == mEncoding) {
+    *aDestLength = 0;
+    return NS_OK;
+  }
+
+  nsresult res = NS_OK;
+  char * dest = aDest;
+  char * destEnd = aDest + *aDestLength;
+
+  if (mEncStep != 0) {
+    if (dest >= destEnd) return NS_OK_UENC_MOREOUTPUT;
+    *(dest++)=ValueToChar(mEncBits);
+    mEncStep = 0;
+    mEncBits = 0;
+  }
+
+  if (dest >= destEnd) {
+    res = NS_OK_UENC_MOREOUTPUT;
+  } else {
+    switch (aEncoding) {
+      case 0:
+        *(dest++) = '-';
+        mEncStep = 0;
+        mEncBits = 0;
+        break;
+      case 1:
+        *(dest++) = mEscChar;
+        break;
+    }
+    mEncoding = aEncoding;
+  }
+
+  *aDestLength  = dest - aDest;
+  return res;
+}
+
+nsresult nsBasicUTF7Encoder::EncodeDirect(
+                            const char16_t * aSrc,
+                            int32_t * aSrcLength,
+                            char * aDest,
+                            int32_t * aDestLength)
+{
+  nsresult res = NS_OK;
+  const char16_t * src = aSrc;
+  const char16_t * srcEnd = aSrc + *aSrcLength;
+  char * dest = aDest;
+  char * destEnd = aDest + *aDestLength;
+  char16_t ch;
+
+  while (src < srcEnd) {
+    ch = *src;
+
+    // stop when we reach Unicode chars
+    if (!DirectEncodable(ch)) break;
+
+    if (ch == mEscChar) {
+      // special case for the escape char
+      if (destEnd - dest < 1) {
+        res = NS_OK_UENC_MOREOUTPUT;
+        break;
+      } else {
+        *dest++ = (char)ch;
+        *dest++ = (char)'-';
+        src++;
+      }
+    } else {
+      //classic direct encoding
+      if (dest >= destEnd) {
+        res = NS_OK_UENC_MOREOUTPUT;
+        break;
+      } else {
+        *dest++ = (char)ch;
+        src++;
+      }
+    }
+  }
+
+  *aSrcLength = src - aSrc;
+  *aDestLength  = dest - aDest;
+  return res;
+}
+
+nsresult nsBasicUTF7Encoder::EncodeBase64(
+                             const char16_t * aSrc,
+                             int32_t * aSrcLength,
+                             char * aDest,
+                             int32_t * aDestLength)
+{
+  nsresult res = NS_OK;
+  const char16_t * src = aSrc;
+  const char16_t * srcEnd = aSrc + *aSrcLength;
+  char * dest = aDest;
+  char * destEnd = aDest + *aDestLength;
+  char16_t ch;
+  uint32_t value;
+
+  while (src < srcEnd) {
+    ch = *src;
+
+    // stop when we reach printable US-ASCII chars
+    if (DirectEncodable(ch)) break;
+
+    switch (mEncStep) {
+      case 0:
+        if (destEnd - dest < 2) {
+          res = NS_OK_UENC_MOREOUTPUT;
+          break;
+        }
+        value=ch>>10;
+        *(dest++)=ValueToChar(value);
+        value=(ch>>4)&0x3f;
+        *(dest++)=ValueToChar(value);
+        mEncBits=(ch&0x0f)<<2;
+        break;
+      case 1:
+        if (destEnd - dest < 3) {
+          res = NS_OK_UENC_MOREOUTPUT;
+          break;
+        }
+        value=mEncBits+(ch>>14);
+        *(dest++)=ValueToChar(value);
+        value=(ch>>8)&0x3f;
+        *(dest++)=ValueToChar(value);
+        value=(ch>>2)&0x3f;
+        *(dest++)=ValueToChar(value);
+        mEncBits=(ch&0x03)<<4;
+        break;
+      case 2:
+        if (destEnd - dest < 3) {
+          res = NS_OK_UENC_MOREOUTPUT;
+          break;
+        }
+        value=mEncBits+(ch>>12);
+        *(dest++)=ValueToChar(value);
+        value=(ch>>6)&0x3f;
+        *(dest++)=ValueToChar(value);
+        value=ch&0x3f;
+        *(dest++)=ValueToChar(value);
+        mEncBits=0;
+        break;
+    }
+
+    if (res != NS_OK) break;
+
+    src++;
+    (++mEncStep)%=3;
+  }
+
+  *aSrcLength = src - aSrc;
+  *aDestLength  = dest - aDest;
+  return res;
+}
+
+char nsBasicUTF7Encoder::ValueToChar(uint32_t aValue) {
+  if (aValue < 26)
+    return (char)('A'+aValue);
+  else if (aValue < 26 + 26)
+    return (char)('a' + aValue - 26);
+  else if (aValue < 26 + 26 + 10)
+    return (char)('0' + aValue - 26 - 26);
+  else if (aValue == 26 + 26 + 10)
+    return '+';
+  else if (aValue == 26 + 26 + 10 + 1)
+    return mLastChar;
+  else
+    return -1;
+}
+
+bool nsBasicUTF7Encoder::DirectEncodable(char16_t aChar) {
+  // spec says: printable US-ASCII chars
+  if ((aChar >= 0x20) && (aChar <= 0x7e)) return true;
+  else return false;
+}
+
+//----------------------------------------------------------------------
+// Subclassing of nsEncoderSupport class [implementation]
+
+NS_IMETHODIMP nsBasicUTF7Encoder::ConvertNoBuffNoErr(
+                                  const char16_t * aSrc,
+                                  int32_t * aSrcLength,
+                                  char * aDest,
+                                  int32_t * aDestLength)
+{
+  nsresult res = NS_OK;
+  const char16_t * src = aSrc;
+  const char16_t * srcEnd = aSrc + *aSrcLength;
+  char * dest = aDest;
+  char * destEnd = aDest + *aDestLength;
+  int32_t bcr,bcw;
+  char16_t ch;
+  int32_t enc;
+
+  while (src < srcEnd) {
+    // find the encoding for the next char
+    ch = *src;
+    if (DirectEncodable(ch))
+      enc = ENC_DIRECT;
+    else
+      enc = ENC_BASE64;
+
+    // if necessary, shift into the required encoding
+    bcw = destEnd - dest;
+    res = ShiftEncoding(enc, dest, &bcw);
+    dest += bcw;
+    if (res != NS_OK) break;
+
+    // now encode (as much as you can)
+    bcr = srcEnd - src;
+    bcw = destEnd - dest;
+    if (enc == ENC_DIRECT)
+      res = EncodeDirect(src, &bcr, dest, &bcw);
+    else
+      res = EncodeBase64(src, &bcr, dest, &bcw);
+    src += bcr;
+    dest += bcw;
+
+    if (res != NS_OK) break;
+  }
+
+  *aSrcLength = src - aSrc;
+  *aDestLength  = dest - aDest;
+  return res;
+}
+
+NS_IMETHODIMP nsBasicUTF7Encoder::FinishNoBuff(char * aDest,
+                                               int32_t * aDestLength)
+{
+  return ShiftEncoding(ENC_DIRECT, aDest, aDestLength);
+}
+
+NS_IMETHODIMP nsBasicUTF7Encoder::Reset()
+{
+  mEncoding = ENC_DIRECT;
+  mEncBits = 0;
+  mEncStep = 0;
+  return nsEncoderSupport::Reset();
+}
+
+//----------------------------------------------------------------------
+// Class nsUnicodeToUTF7 [implementation]
+
+nsUnicodeToUTF7::nsUnicodeToUTF7()
+: nsBasicUTF7Encoder('/', '+')
+{
+}
+
+
+bool nsUnicodeToUTF7::DirectEncodable(char16_t aChar) {
+  if ((aChar >= 'A') && (aChar <= 'Z')) return true;
+  else if ((aChar >= 'a') && (aChar <= 'z')) return true;
+  else if ((aChar >= '0') && (aChar <= '9')) return true;
+  else if ((aChar >= 39) && (aChar <= 41)) return true;
+  else if ((aChar >= 44) && (aChar <= 47)) return true;
+  else if (aChar == 58) return true;
+  else if (aChar == 63) return true;
+  else if (aChar == ' ') return true;
+  else if (aChar == 9) return true;
+  else if (aChar == 13) return true;
+  else if (aChar == 10) return true;
+  else if (aChar == 60) return true;  // '<'
+  else if (aChar == 33) return true;  // '!'
+  else if (aChar == 34) return true;  // '"'
+  else if (aChar == 62) return true;  // '>'
+  else if (aChar == 61) return true;  // '='
+  else if (aChar == 59) return true;  // ';'
+  else if (aChar == 91) return true;  // '['
+  else if (aChar == 93) return true;  // ']'
+  else return false;
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/nsUnicodeToUTF7.h
@@ -0,0 +1,78 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* 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/. */
+
+#ifndef nsUnicodeToUTF7_h___
+#define nsUnicodeToUTF7_h___
+
+#include "nsUCSupport.h"
+
+//----------------------------------------------------------------------
+// Class nsBasicUTF7Encoder [declaration]
+
+/**
+ * Basic class for a character set converter from Unicode to UTF-7.
+ *
+ * @created         03/Jun/1999
+ * @author  Catalin Rotaru [CATA]
+ */
+class nsBasicUTF7Encoder : public nsEncoderSupport
+{
+public:
+
+  /**
+   * Class constructor.
+   */
+  nsBasicUTF7Encoder(char aLastChar, char aEscChar);
+
+protected:
+
+  int32_t                   mEncoding;      // current encoding
+  uint32_t                  mEncBits;
+  int32_t                   mEncStep;
+  char                      mLastChar;
+  char                      mEscChar;
+
+  nsresult ShiftEncoding(int32_t aEncoding, char * aDest,
+      int32_t * aDestLength);
+  nsresult EncodeDirect(const char16_t * aSrc, int32_t * aSrcLength,
+      char * aDest, int32_t * aDestLength);
+  nsresult EncodeBase64(const char16_t * aSrc, int32_t * aSrcLength,
+      char * aDest, int32_t * aDestLength);
+  char ValueToChar(uint32_t aValue);
+  virtual bool DirectEncodable(char16_t aChar);
+
+  //--------------------------------------------------------------------
+  // Subclassing of nsEncoderSupport class [declaration]
+
+  NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength,
+      char * aDest, int32_t * aDestLength);
+  NS_IMETHOD FinishNoBuff(char * aDest, int32_t * aDestLength);
+  NS_IMETHOD Reset();
+};
+
+//----------------------------------------------------------------------
+// Class nsUnicodeToUTF7 [declaration]
+
+/**
+ * A character set converter from Unicode to UTF-7.
+ *
+ * @created         03/Jun/1999
+ * @author  Catalin Rotaru [CATA]
+ */
+class nsUnicodeToUTF7 : public nsBasicUTF7Encoder
+{
+public:
+
+  /**
+   * Class constructor.
+   */
+  nsUnicodeToUTF7();
+
+protected:
+
+  virtual bool DirectEncodable(char16_t aChar);
+};
+
+#endif /* nsUnicodeToUTF7_h___ */
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/test/Makefile
@@ -0,0 +1,16 @@
+# 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/.
+
+DEPTH		= ../../..
+topsrcdir	= ../../..
+srcdir		= .
+VPATH		= .
+relativesrcdir = mailnews/intl/test
+
+include $(DEPTH)/config/autoconf.mk
+
+XPCSHELL_TESTS = unit
+
+include $(topsrcdir)/config/rules.mk
+
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/test/moz.build
@@ -0,0 +1,7 @@
+# vim: set filetype=python:
+# 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/.
+
+XPCSHELL_TESTS_MANIFESTS += ['unit/xpcshell.ini']
+
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/test/unit/test_decode_utf-7.js
@@ -0,0 +1,14 @@
+// Tests conversion from UTF-7 to Unicode. The conversion should fail!
+
+load('CharsetConversionTests.js');
+
+const inString = "+LGI--+ITIipSIp- +AocCeQ-oddns +Ad0CjQ- s+ATECZQKH- p+AlAB3QJ5- u+AlACVA- no+Ao4- +Al8-I";
+
+const expectedString = "+LGI--+ITIipSIp- +AocCeQ-oddns +Ad0CjQ- s+ATECZQKH- p+AlAB3QJ5- u+AlACVA- no+Ao4- +Al8-I";
+
+const aliases = [ "UTF-7", "utf-7", "x-unicode-2-0-utf-7", "unicode-2-0-utf-7",
+		  "unicode-1-1-utf-7", "csunicode11utf7" ];
+
+function run_test() {
+  testDecodeAliases();
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/test/unit/test_decode_utf-7_internal.js
@@ -0,0 +1,14 @@
+// Tests conversion from UTF-7 to Unicode.
+
+load('CharsetConversionTests.js');
+
+const inString = "+LGI--+ITIipSIp- +AocCeQ-oddns +Ad0CjQ- s+ATECZQKH- p+AlAB3QJ5- u+AlACVA- no+Ao4- +Al8-I";
+
+const expectedString = "\u2C62-\u2132\u22A5\u2229 \u0287\u0279oddns \u01DD\u028D s\u0131\u0265\u0287 p\u0250\u01DD\u0279 u\u0250\u0254 no\u028E \u025FI";
+
+const aliases = [ "UTF-7", "utf-7", "x-unicode-2-0-utf-7", "unicode-2-0-utf-7",
+		  "unicode-1-1-utf-7", "csunicode11utf7" ];
+
+function run_test() {
+  testDecodeAliasesInternal();
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/test/unit/test_encode_utf-7.js
@@ -0,0 +1,14 @@
+// Tests conversion from Unicode to UTF-7. The conversion should fail!
+
+load('CharsetConversionTests.js');
+
+const inString = "\u2C62-\u2132\u22A5\u2229 \u0287\u0279oddns \u01DD\u028D s\u0131\u0265\u0287 p\u0250\u01DD\u0279 u\u0250\u0254 no\u028E \u025FI";
+
+const expectedString = "?-??? ??oddns ?? s??? p??? u?? no? ?I";
+
+const aliases = [ "UTF-7", "utf-7", "x-unicode-2-0-utf-7", "unicode-2-0-utf-7",
+		  "unicode-1-1-utf-7", "csunicode11utf7" ];
+
+function run_test() {
+  testEncodeAliases();
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/test/unit/test_encode_utf-7_internal.js
@@ -0,0 +1,14 @@
+// Tests conversion from Unicode to UTF-7.
+
+load('CharsetConversionTests.js');
+
+const inString = "\u2C62-\u2132\u22A5\u2229 \u0287\u0279oddns \u01DD\u028D s\u0131\u0265\u0287 p\u0250\u01DD\u0279 u\u0250\u0254 no\u028E \u025FI";
+
+const expectedString = "+LGI--+ITIipSIp- +AocCeQ-oddns +Ad0CjQ- s+ATECZQKH- p+AlAB3QJ5- u+AlACVA- no+Ao4- +Al8-I";
+
+const aliases = [ "UTF-7", "utf-7", "x-unicode-2-0-utf-7", "unicode-2-0-utf-7",
+		  "unicode-1-1-utf-7", "csunicode11utf7" ];
+
+function run_test() {
+  testEncodeAliasesInternal();
+}
new file mode 100644
--- /dev/null
+++ b/mailnews/intl/test/unit/xpcshell.ini
@@ -0,0 +1,6 @@
+[DEFAULT]
+
+[test_decode_utf-7.js]
+[test_decode_utf-7_internal.js]
+[test_encode_utf-7.js]
+[test_encode_utf-7_internal.js]
--- a/mailnews/moz.build
+++ b/mailnews/moz.build
@@ -11,16 +11,17 @@ PARALLEL_DIRS += [
     'db/msgdb',
     'extensions',
     'imap/public',
     'imap/src',
     'import/public',
     'import/src',
     'import/text/src',
     'import/vcard/src',
+    'intl',
     'local/public',
     'local/src',
     'mime',
     'news',
 ]
 
 if CONFIG['MOZ_WIDGET_TOOLKIT'] == 'cocoa':
     PARALLEL_DIRS += [