Back to index

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

A character set converter from GBK to Unicode. More...

#include <nsHZToUnicode.h>

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

List of all members.

Public Types

enum  { kOnError_Recover, kOnError_Signal }

Public Member Functions

 nsHZToUnicode ()
 Class constructor.
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

nsGBKConvUtil mUtil
char * mBuffer
 Internal buffer for partial conversions.
PRInt32 mBufferCapacity
PRInt32 mBufferLength
PRUint32 mMaxLengthFactor

Private Attributes

PRInt16 mHZState
PRUint32 mRunLength

Detailed Description

A character set converter from GBK to Unicode.

08/Sept/1999

Author:
Yueheng Xu, Yuehe.nosp@m.ng.X.nosp@m.u@int.nosp@m.el.c.nosp@m.om

Definition at line 54 of file nsHZToUnicode.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 79 of file nsHZToUnicode.cpp.

                             : nsBufferDecoderSupport(1)
{
  mHZState = HZ_STATE_ASCII;       // per HZ spec, default to ASCII state 
  mRunLength = 0;
}

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 nsHZToUnicode::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 85 of file nsHZToUnicode.cpp.

{
  PRInt32 i=0;
  PRInt32 iSrcLength = *aSrcLength;
  PRInt32 iDestlen = 0;
  PRUint8 ch1, ch2;
  nsresult res = NS_OK;
  *aSrcLength=0;
  for (i=0;i<iSrcLength;i++)
  {
    if ( iDestlen >= (*aDestLength) )
    {
      res = NS_OK_UDEC_MOREOUTPUT;
      break;
    }
    if ( *aSrc & 0x80 ) // if it is a 8-bit byte
    {
      if (UINT8_IN_RANGE(0x81, aSrc[0], 0xFE) &&
          UINT8_IN_RANGE(0x40, aSrc[1], 0xFE)) {
        // The source is a 8-bit GBCode
        *aDest = mUtil.GBKCharToUnicode(aSrc[0], aSrc[1]);
      } else {
        *aDest = UCS2_NO_MAPPING;
      }
      aSrc += 2;
      i++;
      iDestlen++;
      aDest++;
      *aSrcLength = i+1;
      continue;
    }
    // otherwise, it is a 7-bit byte 
    // The source will be an ASCII or a 7-bit HZ code depending on ch1
    ch1 = *aSrc;
    ch2       = *(aSrc+1);
    if (ch1 == HZLEAD1 )  // if it is lead by '~'
    {
      switch (ch2)
      {
        case HZLEAD2: 
          // we got a '~{'
          // we are switching to HZ state
          mHZState = HZ_STATE_GB;
          mRunLength = 0;
          aSrc += 2;
          i++;
          break;
        case HZLEAD3: 
          // we got a '~}'
          // we are switching to ASCII state
          mHZState = HZ_STATE_ASCII;
          aSrc += 2;
          i++;
          if (mRunLength == 0) {
            *aDest = UCS2_NO_MAPPING;
            iDestlen++;
            aDest++;
          }
          mRunLength = 0;
          break;
        case HZLEAD1: 
          // we got a '~~', process like an ASCII, but no state change
          aSrc++;
          *aDest = CAST_CHAR_TO_UNICHAR(*aSrc);
          aSrc++;
          i++;
          iDestlen++;
          aDest++;
          mRunLength++;
          break;
        case HZLEAD4:       
          // we got a "~\n", it means maintain double byte mode cross lines, ignore the '~' itself
          //  mHZState = HZ_STATE_GB; 
          // I find that "~\n" should interpreted as line continuation without mode change
          // It should not be interpreted as line continuation with double byte mode on
          aSrc++;
          break;
        default:
          // undefined ESC sequence '~X' are ignored since this is a illegal combination 
          aSrc += 2;
          *aDest = UCS2_NO_MAPPING;
          iDestlen++;
          aDest++;
          break;
      };
      continue;// go for next loop
    }
    // ch1 != '~'
    switch (mHZState)
    {
      case HZ_STATE_GB:
        // the following chars are HZ
        *aDest = mUtil.GBKCharToUnicode(aSrc[0]|0x80, aSrc[1]|0x80);
        aSrc += 2;
        i++;
        iDestlen++;
        aDest++;
        mRunLength++;
        break;
      case HZ_STATE_ASCII:
      default:
        // default behavior also like an ASCII
        // when the source is an ASCII
        *aDest = CAST_CHAR_TO_UNICHAR(*aSrc);
        aSrc++;
        iDestlen++;
        aDest++;
        break;
    }
    *aSrcLength = i+1;
  }// for loop
  *aDestLength = iDestlen;
  return NS_OK;
}

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 72 of file nsHZToUnicode.h.

Definition at line 135 of file nsUCSupport.h.

Definition at line 73 of file nsHZToUnicode.h.

Definition at line 69 of file nsHZToUnicode.h.


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