Bug 1363281 - add UTF-7 support for IMAP. r=jcranmer

This commit is contained in:
Jorg K 2017-06-14 08:01:23 +02:00
Родитель ccbd7f65b4
Коммит a354c87eb0
6 изменённых файлов: 52 добавлений и 21 удалений

Просмотреть файл

@ -26,6 +26,9 @@
#include "nsComponentManagerUtils.h"
#include "nsUnicharUtils.h"
#include "nsIFileStreams.h"
#include "../../intl/nsMUTF7ToUnicode.h"
#include "../../intl/nsUnicodeToMUTF7.h"
//
// International functions necessary for composition
//
@ -79,6 +82,43 @@ nsresult nsMsgI18NConvertToUnicode(const char* aCharset,
outString);
}
nsresult CopyUTF16toMUTF7(const nsString &aSrc, nsACString& aDest)
{
#define IMAP_UTF7_BUF_LENGTH 100
nsUnicodeToMUTF7 converter;
static char buffer[IMAP_UTF7_BUF_LENGTH];
char16_t *in = (char16_t *)aSrc.get();
int32_t inLen = aSrc.Length();
int32_t outLen;
aDest.Truncate();
while (inLen > 0) {
outLen = IMAP_UTF7_BUF_LENGTH;
int32_t remaining = inLen;
converter.ConvertNoBuffNoErr(in, &remaining, buffer, &outLen);
aDest.Append(buffer, outLen);
in += remaining;
inLen -= remaining;
}
outLen = IMAP_UTF7_BUF_LENGTH;
converter.FinishNoBuff(buffer, &outLen);
if (outLen > 0)
aDest.Append(buffer, outLen);
return NS_OK;
}
nsresult CopyMUTF7toUTF16(const nsCString& aSrc, nsAString& aDest)
{
// UTF-7 encoding size cannot be larger than the size in UTF-16.
nsMUTF7ToUnicode converter;
int32_t inLen = aSrc.Length();
int32_t outLen = inLen;
aDest.SetCapacity(outLen);
converter.ConvertNoBuff(aSrc.get(), &inLen, aDest.BeginWriting(), &outLen);
MOZ_ASSERT(inLen == aSrc.Length(), "UTF-7 should not produce a longer output");
aDest.SetLength(outLen);
return NS_OK;
}
// Charset used by the file system.
const char * nsMsgI18NFileSystemCharset()
{

Просмотреть файл

@ -138,16 +138,9 @@ NS_MSG_BASE void nsMsgI18NConvertRawBytesToUTF8(const nsCString& inString,
const char* charset,
nsACString& outString);
// inline forwarders to avoid littering with 'x-imap4-.....'
inline nsresult CopyUTF16toMUTF7(const nsString &aSrc, nsACString& aDest)
{
return nsMsgI18NConvertFromUnicode("x-imap4-modified-utf7", aSrc, aDest);
}
inline nsresult CopyMUTF7toUTF16(const nsCString& aSrc, nsAString& aDest)
{
return nsMsgI18NConvertToUnicode("x-imap4-modified-utf7", aSrc, aDest);
}
// Convert between UTF-16 and modified UTF-7 used for IMAP.
NS_MSG_BASE nsresult CopyUTF16toMUTF7(const nsString &aSrc, nsACString& aDest);
NS_MSG_BASE nsresult CopyMUTF7toUTF16(const nsCString& aSrc, nsAString& aDest);
inline nsresult ConvertToUnicode(const char* charset,
const nsCString &aSrc, nsAString& aDest)

Просмотреть файл

@ -12,7 +12,6 @@
// Class nsBasicUTF7Decoder [implementation]
nsBasicUTF7Decoder::nsBasicUTF7Decoder(char aLastChar, char aEscChar)
: nsBufferDecoderSupport(1)
{
mLastChar = aLastChar;
mEscChar = aEscChar;
@ -216,7 +215,7 @@ NS_IMETHODIMP nsBasicUTF7Decoder::Reset()
mEncoding = ENC_DIRECT;
mEncBits = 0;
mEncStep = 0;
return nsBufferDecoderSupport::Reset();
return NS_OK;
}
//----------------------------------------------------------------------

Просмотреть файл

@ -16,7 +16,7 @@
* @created 03/Jun/1999
* @author Catalin Rotaru [CATA]
*/
class nsBasicUTF7Decoder : public nsBufferDecoderSupport
class nsBasicUTF7Decoder
{
public:
@ -24,6 +24,8 @@ public:
* Class constructor.
*/
nsBasicUTF7Decoder(char aLastChar, char aEscChar);
NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength,
char16_t * aDest, int32_t * aDestLength);
protected:
@ -43,8 +45,6 @@ protected:
//--------------------------------------------------------------------
// Subclassing of nsBufferDecoderSupport class [declaration]
NS_IMETHOD ConvertNoBuff(const char * aSrc, int32_t * aSrcLength,
char16_t * aDest, int32_t * aDestLength);
NS_IMETHOD Reset();
};

Просмотреть файл

@ -16,7 +16,6 @@
// Class nsBasicUTF7Encoder [implementation]
nsBasicUTF7Encoder::nsBasicUTF7Encoder(char aLastChar, char aEscChar)
: nsEncoderSupport(5)
{
mLastChar = aLastChar;
mEscChar = aEscChar;
@ -262,7 +261,7 @@ NS_IMETHODIMP nsBasicUTF7Encoder::Reset()
mEncoding = ENC_DIRECT;
mEncBits = 0;
mEncStep = 0;
return nsEncoderSupport::Reset();
return NS_OK;
}
//----------------------------------------------------------------------

Просмотреть файл

@ -15,7 +15,7 @@
* @created 03/Jun/1999
* @author Catalin Rotaru [CATA]
*/
class nsBasicUTF7Encoder : public nsEncoderSupport
class nsBasicUTF7Encoder
{
public:
@ -23,6 +23,9 @@ public:
* Class constructor.
*/
nsBasicUTF7Encoder(char aLastChar, char aEscChar);
NS_IMETHOD ConvertNoBuffNoErr(const char16_t * aSrc, int32_t * aSrcLength,
char * aDest, int32_t * aDestLength);
NS_IMETHOD FinishNoBuff(char * aDest, int32_t * aDestLength);
protected:
@ -44,9 +47,6 @@ protected:
//--------------------------------------------------------------------
// 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();
};