2001-09-26 00:47:52 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* 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/. */
|
1999-06-07 18:49:12 +00:00
|
|
|
|
|
|
|
#include "nsUnicodeToUTF7.h"
|
1999-09-06 20:44:26 +00:00
|
|
|
#include <string.h>
|
1999-06-07 18:49:12 +00:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Global functions and data [declaration]
|
|
|
|
|
|
|
|
#define ENC_DIRECT 0
|
|
|
|
#define ENC_BASE64 1
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Class nsBasicUTF7Encoder [implementation]
|
|
|
|
|
|
|
|
nsBasicUTF7Encoder::nsBasicUTF7Encoder(char aLastChar, char aEscChar)
|
2002-08-12 19:16:16 +00:00
|
|
|
: nsEncoderSupport(5)
|
1999-06-07 18:49:12 +00:00
|
|
|
{
|
|
|
|
mLastChar = aLastChar;
|
|
|
|
mEscChar = aEscChar;
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
nsresult nsBasicUTF7Encoder::ShiftEncoding(int32_t aEncoding,
|
1999-06-07 18:49:12 +00:00
|
|
|
char * aDest,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t * aDestLength)
|
1999-06-07 18:49:12 +00:00
|
|
|
{
|
|
|
|
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);
|
1999-06-10 20:06:00 +00:00
|
|
|
mEncStep = 0;
|
|
|
|
mEncBits = 0;
|
1999-06-07 18:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dest >= destEnd) {
|
|
|
|
res = NS_OK_UENC_MOREOUTPUT;
|
|
|
|
} else {
|
|
|
|
switch (aEncoding) {
|
|
|
|
case 0:
|
|
|
|
*(dest++) = '-';
|
1999-06-10 20:06:00 +00:00
|
|
|
mEncStep = 0;
|
|
|
|
mEncBits = 0;
|
1999-06-07 18:49:12 +00:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*(dest++) = mEscChar;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
mEncoding = aEncoding;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aDestLength = dest - aDest;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsBasicUTF7Encoder::EncodeDirect(
|
|
|
|
const PRUnichar * aSrc,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t * aSrcLength,
|
1999-06-07 18:49:12 +00:00
|
|
|
char * aDest,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t * aDestLength)
|
1999-06-07 18:49:12 +00:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
const PRUnichar * src = aSrc;
|
|
|
|
const PRUnichar * srcEnd = aSrc + *aSrcLength;
|
|
|
|
char * dest = aDest;
|
|
|
|
char * destEnd = aDest + *aDestLength;
|
|
|
|
PRUnichar ch;
|
|
|
|
|
|
|
|
while (src < srcEnd) {
|
|
|
|
ch = *src;
|
|
|
|
|
|
|
|
// stop when we reach Unicode chars
|
1999-06-30 20:58:14 +00:00
|
|
|
if (!DirectEncodable(ch)) break;
|
1999-06-07 18:49:12 +00:00
|
|
|
|
|
|
|
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 PRUnichar * aSrc,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t * aSrcLength,
|
1999-06-07 18:49:12 +00:00
|
|
|
char * aDest,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t * aDestLength)
|
1999-06-07 18:49:12 +00:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
const PRUnichar * src = aSrc;
|
|
|
|
const PRUnichar * srcEnd = aSrc + *aSrcLength;
|
|
|
|
char * dest = aDest;
|
|
|
|
char * destEnd = aDest + *aDestLength;
|
|
|
|
PRUnichar ch;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t value;
|
1999-06-07 18:49:12 +00:00
|
|
|
|
|
|
|
while (src < srcEnd) {
|
|
|
|
ch = *src;
|
|
|
|
|
|
|
|
// stop when we reach printable US-ASCII chars
|
1999-06-30 20:58:14 +00:00
|
|
|
if (DirectEncodable(ch)) break;
|
1999-06-07 18:49:12 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
char nsBasicUTF7Encoder::ValueToChar(uint32_t aValue) {
|
1999-06-07 18:49:12 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool nsBasicUTF7Encoder::DirectEncodable(PRUnichar aChar) {
|
1999-06-30 20:58:14 +00:00
|
|
|
// spec says: printable US-ASCII chars
|
2011-10-17 14:59:28 +00:00
|
|
|
if ((aChar >= 0x20) && (aChar <= 0x7e)) return true;
|
|
|
|
else return false;
|
1999-06-30 20:58:14 +00:00
|
|
|
}
|
|
|
|
|
1999-06-07 18:49:12 +00:00
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Subclassing of nsEncoderSupport class [implementation]
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsBasicUTF7Encoder::ConvertNoBuffNoErr(
|
|
|
|
const PRUnichar * aSrc,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t * aSrcLength,
|
1999-06-07 18:49:12 +00:00
|
|
|
char * aDest,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t * aDestLength)
|
1999-06-07 18:49:12 +00:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
const PRUnichar * src = aSrc;
|
|
|
|
const PRUnichar * srcEnd = aSrc + *aSrcLength;
|
|
|
|
char * dest = aDest;
|
|
|
|
char * destEnd = aDest + *aDestLength;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t bcr,bcw;
|
1999-06-07 18:49:12 +00:00
|
|
|
PRUnichar ch;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t enc;
|
1999-06-07 18:49:12 +00:00
|
|
|
|
|
|
|
while (src < srcEnd) {
|
|
|
|
// find the encoding for the next char
|
|
|
|
ch = *src;
|
1999-06-30 20:58:14 +00:00
|
|
|
if (DirectEncodable(ch))
|
1999-06-07 18:49:12 +00:00
|
|
|
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,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t * aDestLength)
|
1999-06-07 18:49:12 +00:00
|
|
|
{
|
|
|
|
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('/', '+')
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-06-30 20:58:14 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool nsUnicodeToUTF7::DirectEncodable(PRUnichar aChar) {
|
2011-10-17 14:59:28 +00:00
|
|
|
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;
|
1999-06-30 20:58:14 +00:00
|
|
|
}
|