Back to index

lightning-sunbird  0.9+nobinonly
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes
nsMUTF7ToUnicode Class Reference

A character set converter from Modified UTF7 to Unicode. More...

#include <nsMUTF7ToUnicode.h>

Inheritance diagram for nsMUTF7ToUnicode:
Inheritance graph
[legend]
Collaboration diagram for nsMUTF7ToUnicode:
Collaboration graph
[legend]

List of all members.

Public Types

enum  { kOnError_Recover, kOnError_Signal }

Public Member Functions

 nsMUTF7ToUnicode ()
 Class constructor.
NS_IMETHOD Convert (const char *aSrc, PRInt32 *aSrcLength, PRUnichar *aDest, PRInt32 *aDestLength)
 Converts the data from one Charset to Unicode.
NS_IMETHOD GetMaxLength (const char *aSrc, PRInt32 aSrcLength, PRInt32 *aDestLength)
 Returns a quick estimation of the size of the buffer needed to hold the converted data.

Protected Member Functions

nsresult DecodeDirect (const char *aSrc, PRInt32 *aSrcLength, PRUnichar *aDest, PRInt32 *aDestLength)
nsresult DecodeBase64 (const char *aSrc, PRInt32 *aSrcLength, PRUnichar *aDest, PRInt32 *aDestLength)
PRUint32 CharToValue (char aChar)
NS_IMETHOD ConvertNoBuff (const char *aSrc, PRInt32 *aSrcLength, PRUnichar *aDest, PRInt32 *aDestLength)
 Convert method but without the buffer management stuff.
NS_IMETHOD Reset ()
 Resets the charset converter so it may be recycled for a completely different and urelated buffer of data.
void FillBuffer (const char **aSrc, PRInt32 aSrcLength)
void DoubleBuffer ()

Protected Attributes

PRInt32 mEncoding
PRUint32 mEncBits
PRInt32 mEncStep
char mLastChar
char mEscChar
PRBool mFreshBase64
char * mBuffer
 Internal buffer for partial conversions.
PRInt32 mBufferCapacity
PRInt32 mBufferLength
PRUint32 mMaxLengthFactor

Detailed Description

A character set converter from Modified UTF7 to Unicode.

18/May/1999

Author:
Catalin Rotaru [CATA]

Definition at line 52 of file nsMUTF7ToUnicode.h.


Member Enumeration Documentation

anonymous enum [inherited]
Enumerator:
kOnError_Recover 
kOnError_Signal 

Definition at line 98 of file nsIUnicodeDecoder.h.

       {
    kOnError_Recover,       // on an error, recover and continue
    kOnError_Signal         // on an error, stop and signal
  };

Constructor & Destructor Documentation

Class constructor.

Definition at line 43 of file nsMUTF7ToUnicode.cpp.

: nsBasicUTF7Decoder(',', '&')
{
}

Member Function Documentation

PRUint32 nsBasicUTF7Decoder::CharToValue ( char  aChar) [protected, inherited]

Definition at line 172 of file nsUTF7ToUnicode.cpp.

                                                   {
  if ((aChar>='A')&&(aChar<='Z'))
    return (PRUint8)(aChar-'A');
  else if ((aChar>='a')&&(aChar<='z'))
    return (PRUint8)(26+aChar-'a');
  else if ((aChar>='0')&&(aChar<='9'))
    return (PRUint8)(26+26+aChar-'0');
  else if (aChar=='+')
    return (PRUint8)(26+26+10);
  else if (aChar==mLastChar)
    return (PRUint8)(26+26+10+1);
  else
    return 0xffff;
}

Here is the caller graph for this function:

NS_IMETHODIMP nsBufferDecoderSupport::Convert ( const char *  aSrc,
PRInt32 aSrcLength,
PRUnichar aDest,
PRInt32 aDestLength 
) [virtual, inherited]

Converts the data from one Charset to Unicode.

About the byte ordering:

  • For input, if the converter cares (that depends of the charset, for example a singlebyte will ignore the byte ordering) it should assume network order. If necessary and requested, we can add a method SetInputByteOrder() so that the reverse order can be used, too. That method would have as default the assumed network order.
  • The output stream is Unicode, having the byte order which is internal for the machine on which the converter is running on.

Unless there is not enough output space, this method must consume all the available input data! The eventual incomplete final character data will be stored internally in the converter and used when the method is called again for continuing the conversion. This way, the caller will not have to worry about managing incomplete input data by mergeing it with the next buffer.

Error conditions: If the read value does not belong to this character set, one should replace it with the Unicode special 0xFFFD. When an actual input error is encountered, like a format error, the converter stop and return error. Hoever, we should keep in mind that we need to be lax in decoding.

Converter required behavior: In this order: when output space is full - return right away. When input data is wrong, return input pointer right after the wrong byte. When partial input, it will be consumed and cached. All the time input pointer will show how much was actually consumed and how much was actually written.

Parameters:
aSrc[IN] the source data buffer
aSrcLength[IN/OUT] the length of source data buffer; after conversion will contain the number of bytes read
aDest[OUT] the destination data buffer
aDestLength[IN/OUT] the length of the destination data buffer; after conversion will contain the number of Unicode characters written
Returns:
NS_PARTIAL_MORE_INPUT if only a partial conversion was done; more input is needed to continue NS_PARTIAL_MORE_OUTPUT if only a partial conversion was done; more output space is needed to continue NS_ERROR_ILLEGAL_INPUT if an illegal input sequence was encountered and the behavior was set to "signal"

Implements nsIUnicodeDecoder.

Definition at line 114 of file nsUCSupport.cpp.

{
  // we do all operations using pointers internally
  const char * src = aSrc;
  const char * srcEnd = aSrc + *aSrcLength;
  PRUnichar * dest = aDest;
  PRUnichar * destEnd = aDest + *aDestLength;

  PRInt32 bcr, bcw; // byte counts for read & write;
  nsresult res = NS_OK;

  // do we have some residual data from the last conversion?
  if (mBufferLength > 0) if (dest == destEnd) {
    res = NS_OK_UDEC_MOREOUTPUT;
  } else for (;;) {
    // we need new data to add to the buffer
    if (src == srcEnd) {
      res = NS_OK_UDEC_MOREINPUT;
      break;
    }

    // fill that buffer
    PRInt32 buffLen = mBufferLength;  // initial buffer length
    FillBuffer(&src, srcEnd - src);

    // convert that buffer
    bcr = mBufferLength;
    bcw = destEnd - dest;
    res = ConvertNoBuff(mBuffer, &bcr, dest, &bcw);
    dest += bcw;

    if ((res == NS_OK_UDEC_MOREINPUT) && (bcw == 0)) {
        res = NS_ERROR_UNEXPECTED;
#if defined(DEBUG_yokoyama) || defined(DEBUG_ftang)
        NS_ASSERTION(0, "This should not happen. Internal buffer may be corrupted.");
#endif
        break;
    } else {
      if (bcr < buffLen) {
        // we didn't convert that residual data - unfill the buffer
        src -= mBufferLength - buffLen;
        mBufferLength = buffLen;
#if defined(DEBUG_yokoyama) || defined(DEBUG_ftang)
        NS_ASSERTION(0, "This should not happen. Internal buffer may be corrupted.");
#endif
      } else {
        // the buffer and some extra data was converted - unget the rest
        src -= mBufferLength - bcr;
        mBufferLength = 0;
        res = NS_OK;
      }
      break;
    }
  }

  if (res == NS_OK) {
    bcr = srcEnd - src;
    bcw = destEnd - dest;
    res = ConvertNoBuff(src, &bcr, dest, &bcw);
    src += bcr;
    dest += bcw;

    // if we have partial input, store it in our internal buffer.
    if (res == NS_OK_UDEC_MOREINPUT) {
      bcr = srcEnd - src;
      // make sure buffer is large enough
      if (bcr > mBufferCapacity) {
          // somehow we got into an error state and the buffer is growing out of control
          res = NS_ERROR_UNEXPECTED;
      } else {
          FillBuffer(&src, bcr);
      }
    }
  }

  *aSrcLength   -= srcEnd - src;
  *aDestLength  -= destEnd - dest;
  return res;
}

Here is the call graph for this function:

NS_IMETHODIMP nsBasicUTF7Decoder::ConvertNoBuff ( const char *  aSrc,
PRInt32 aSrcLength,
PRUnichar aDest,
PRInt32 aDestLength 
) [protected, virtual, inherited]

Convert method but without the buffer management stuff.

Implements nsBufferDecoderSupport.

Definition at line 190 of file nsUTF7ToUnicode.cpp.

{
  const char * srcEnd = aSrc + *aSrcLength;
  const char * src = aSrc;
  PRUnichar * destEnd = aDest + *aDestLength;
  PRUnichar * dest = aDest;
  PRInt32 bcr,bcw;
  nsresult res = NS_OK;
  char ch;

  while (src < srcEnd) {
    ch = *src;

    // 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 = PR_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 = PR_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;
}

Here is the call graph for this function:

nsresult nsBasicUTF7Decoder::DecodeBase64 ( const char *  aSrc,
PRInt32 aSrcLength,
PRUnichar aDest,
PRInt32 aDestLength 
) [protected, inherited]

Definition at line 92 of file nsUTF7ToUnicode.cpp.

{
  const char * srcEnd = aSrc + *aSrcLength;
  const char * src = aSrc;
  PRUnichar * destEnd = aDest + *aDestLength;
  PRUnichar * dest = aDest;
  nsresult res = NS_OK;
  char ch;
  PRUint32 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++) = (PRUnichar) 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++) = (PRUnichar) 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++) = (PRUnichar) mEncBits;
        mEncBits = 0;
        break;
    }

    if (res != NS_OK) break;

    src++;
    (++mEncStep)%=8;
  }

  *aSrcLength = src - aSrc;
  *aDestLength = dest - aDest;
  return res;
}

Here is the call graph for this function:

Here is the caller graph for this function:

nsresult nsBasicUTF7Decoder::DecodeDirect ( const char *  aSrc,
PRInt32 aSrcLength,
PRUnichar aDest,
PRInt32 aDestLength 
) [protected, inherited]

Definition at line 54 of file nsUTF7ToUnicode.cpp.

{
  const char * srcEnd = aSrc + *aSrcLength;
  const char * src = aSrc;
  PRUnichar * destEnd = aDest + *aDestLength;
  PRUnichar * 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;
}

Here is the caller graph for this function:

void nsBufferDecoderSupport::DoubleBuffer ( ) [protected, inherited]

Definition at line 102 of file nsUCSupport.cpp.

{
  mBufferCapacity *= 2;
  char * newBuffer = new char [mBufferCapacity];
  if (mBufferLength > 0) memcpy(newBuffer, mBuffer, mBufferLength);
  delete [] mBuffer;
  mBuffer = newBuffer;
}

Here is the call graph for this function:

void nsBufferDecoderSupport::FillBuffer ( const char **  aSrc,
PRInt32  aSrcLength 
) [protected, inherited]

Definition at line 94 of file nsUCSupport.cpp.

{
  PRInt32 bcr = PR_MIN(mBufferCapacity - mBufferLength, aSrcLength);
  memcpy(mBuffer + mBufferLength, *aSrc, bcr);
  mBufferLength += bcr;
  (*aSrc) += bcr;
}

Here is the call graph for this function:

Here is the caller graph for this function:

NS_IMETHODIMP nsBufferDecoderSupport::GetMaxLength ( const char *  aSrc,
PRInt32  aSrcLength,
PRInt32 aDestLength 
) [virtual, inherited]

Returns a quick estimation of the size of the buffer needed to hold the converted data.

Remember: this estimation is >= with the actual size of the buffer needed. It will be computed for the "worst case"

Parameters:
aSrc[IN] the source data buffer
aSrcLength[IN] the length of source data buffer
aDestLength[OUT] the needed size of the destination buffer
Returns:
NS_EXACT_LENGTH if an exact length was computed NS_OK is all we have is an approximation

Implements nsIUnicodeDecoder.

Definition at line 203 of file nsUCSupport.cpp.

{
  NS_ASSERTION(mMaxLengthFactor != 0, "Must override GetMaxLength!");
  *aDestLength = aSrcLength * mMaxLengthFactor;
  return NS_OK;
}
NS_IMETHODIMP nsBasicUTF7Decoder::Reset ( ) [protected, virtual, inherited]

Resets the charset converter so it may be recycled for a completely different and urelated buffer of data.

Reimplemented from nsBufferDecoderSupport.

Definition at line 248 of file nsUTF7ToUnicode.cpp.

Here is the caller graph for this function:


Member Data Documentation

char* nsBufferDecoderSupport::mBuffer [protected, inherited]

Internal buffer for partial conversions.

Definition at line 131 of file nsUCSupport.h.

Definition at line 132 of file nsUCSupport.h.

Definition at line 133 of file nsUCSupport.h.

PRUint32 nsBasicUTF7Decoder::mEncBits [protected, inherited]

Definition at line 64 of file nsUTF7ToUnicode.h.

PRInt32 nsBasicUTF7Decoder::mEncoding [protected, inherited]

Definition at line 63 of file nsUTF7ToUnicode.h.

PRInt32 nsBasicUTF7Decoder::mEncStep [protected, inherited]

Definition at line 65 of file nsUTF7ToUnicode.h.

char nsBasicUTF7Decoder::mEscChar [protected, inherited]

Definition at line 67 of file nsUTF7ToUnicode.h.

PRBool nsBasicUTF7Decoder::mFreshBase64 [protected, inherited]

Definition at line 68 of file nsUTF7ToUnicode.h.

char nsBasicUTF7Decoder::mLastChar [protected, inherited]

Definition at line 66 of file nsUTF7ToUnicode.h.

Definition at line 135 of file nsUCSupport.h.


The documentation for this class was generated from the following files: