Back to index

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

Support class for a single-table-driven Unicode decoder. More...

#include <nsUCSupport.h>

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

List of all members.

Public Types

enum  { kOnError_Recover, kOnError_Signal }

Public Member Functions

 nsTableDecoderSupport (uShiftTable *aShiftTable, uMappingTable *aMappingTable, PRUint32 aMaxLengthFactor)
 Class constructor.
virtual ~nsTableDecoderSupport ()
 Class destructor.
NS_IMETHOD Convert (const char *aSrc, PRInt32 *aSrcLength, PRUnichar *aDest, PRInt32 *aDestLength)
 Converts the data from one Charset to Unicode.
NS_IMETHOD Reset ()
 Resets the charset converter so it may be recycled for a completely different and urelated buffer of data.
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

NS_IMETHOD ConvertNoBuff (const char *aSrc, PRInt32 *aSrcLength, PRUnichar *aDest, PRInt32 *aDestLength)
 Convert method but without the buffer management stuff.
void FillBuffer (const char **aSrc, PRInt32 aSrcLength)
void DoubleBuffer ()

Protected Attributes

nsIUnicodeDecodeHelpermHelper
uShiftTablemShiftTable
uMappingTablemMappingTable
char * mBuffer
 Internal buffer for partial conversions.
PRInt32 mBufferCapacity
PRInt32 mBufferLength
PRUint32 mMaxLengthFactor

Detailed Description

Support class for a single-table-driven Unicode decoder.

15/Mar/1999

Author:
Catalin Rotaru [CATA]

Definition at line 178 of file nsUCSupport.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

nsTableDecoderSupport::nsTableDecoderSupport ( uShiftTable aShiftTable,
uMappingTable aMappingTable,
PRUint32  aMaxLengthFactor 
)

Class constructor.

Definition at line 215 of file nsUCSupport.cpp.

: nsBufferDecoderSupport(aMaxLengthFactor)
{
  mHelper = NULL;
  mShiftTable = aShiftTable;
  mMappingTable = aMappingTable;
}

Class destructor.

Definition at line 225 of file nsUCSupport.cpp.


Member Function Documentation

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 nsTableDecoderSupport::ConvertNoBuff ( const char *  aSrc,
PRInt32 aSrcLength,
PRUnichar aDest,
PRInt32 aDestLength 
) [protected, virtual]

Convert method but without the buffer management stuff.

Implements nsBufferDecoderSupport.

Definition at line 233 of file nsUCSupport.cpp.

{
  nsresult res;

  if (mHelper == nsnull) {
    res = CallCreateInstance(kUnicodeDecodeHelperCID, &mHelper);
    if (NS_FAILED(res)) return NS_ERROR_UDEC_NOHELPER;
  }

  res = mHelper->ConvertByTable(aSrc, aSrcLength, aDest, aDestLength, 
      mShiftTable, mMappingTable);
  return res;
}

Here is the call 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 nsBufferDecoderSupport::Reset ( ) [virtual, inherited]

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

Implements nsIUnicodeDecoder.

Reimplemented in nsBasicUTF7Decoder.

Definition at line 197 of file nsUCSupport.cpp.

{
  mBufferLength = 0;
  return NS_OK;
}

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.

Definition at line 195 of file nsUCSupport.h.

Definition at line 197 of file nsUCSupport.h.

Definition at line 135 of file nsUCSupport.h.

Definition at line 196 of file nsUCSupport.h.


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