Back to index

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

#include <nsUCS2BEToUnicode.h>

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

List of all members.

Public Types

enum  { kOnError_Recover, kOnError_Signal }

Public Member Functions

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 Attributes

PRUint8 mState
PRUint8 mData

Private Types

enum  Endian { kUnknown, kBigEndian, kLittleEndian }

Private Attributes

Endian mEndian
PRBool mFoundBOM

Detailed Description

Definition at line 83 of file nsUCS2BEToUnicode.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
  };
enum nsUTF16ToUnicode::Endian [private]
Enumerator:
kUnknown 
kBigEndian 
kLittleEndian 

Definition at line 94 of file nsUCS2BEToUnicode.h.


Member Function Documentation

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

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 216 of file nsUCS2BEToUnicode.cpp.

{
    if(2 == mState) // first time called
    {
      NS_ASSERTION(*aSrcLength >= 2, "Too few bytes in input");

      // check if BOM (0xFEFF) is at the beginning, remove it if found, and
      // set mEndian accordingly.
      if(0xFF == PRUint8(aSrc[0]) && 0xFE == PRUint8(aSrc[1])) {
        aSrc += 2;
        *aSrcLength -= 2;
        mState = 0;
        mEndian = kLittleEndian;
        mFoundBOM = PR_TRUE;
      }
      else if(0xFE == PRUint8(aSrc[0]) && 0xFF == PRUint8(aSrc[1])) {
        aSrc += 2;
        *aSrcLength -= 2;
        mState = 0;
        mEndian = kBigEndian;
        mFoundBOM = PR_TRUE;
      }
      // BOM is not found, but we can use a simple heuristic to determine
      // the endianness. Assume the first character is [U+0001, U+00FF].
      // Not always valid, but it's very likely to hold for html/xml/css. 
      else if(!aSrc[0] && aSrc[1]) {  // 0x00 0xhh (hh != 00)
        mState = 0;                   
        mEndian = kBigEndian;
      }
      else if(aSrc[0] && !aSrc[1]) {  // 0xhh 0x00 (hh != 00)
        mState = 0;
        mEndian = kLittleEndian;
      }
      else { // Neither BOM nor 'plausible' byte patterns at the beginning.
             // Just assume it's BE (following Unicode standard)
             // and let the garbage show up in the browser. (security concern?)
             // (bug 246194)
        mState = 0;   
        mEndian = kBigEndian;
      }
    }
    
    nsresult rv = UTF16ConvertToUnicode(mState, mData, aSrc, aSrcLength, aDest,
                                        aDestLength);

#ifdef IS_BIG_ENDIAN
    if (mEndian == kLittleEndian)
#elif defined(IS_LITTLE_ENDIAN)
    if (mEndian == kBigEndian)
#else
    #error "Unknown endianness"
#endif
      SwapBytes(aDest, *aDestLength);

    // If BOM is not found and we're to return NS_OK, signal that BOM
    // is not found. Otherwise, return |rv| from |UTF16ConvertToUnicode|
    return (rv == NS_OK && !mFoundBOM) ? NS_OK_UDEC_NOBOMFOUND : rv;
}

Here is the call graph for this function:

NS_IMETHODIMP nsUTF16ToUnicodeBase::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 134 of file nsUCS2BEToUnicode.cpp.

{
  // the left-over byte of the previous run has to be taken into account.
  *aDestLength = (aSrcLength + ((1 == mState) ? 1 : 0)) / 2;
  return NS_OK;
}

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

Reimplemented from nsUTF16ToUnicodeBase.

Definition at line 208 of file nsUCS2BEToUnicode.cpp.


Member Data Documentation

PRUint8 nsUTF16ToUnicodeBase::mData [protected, inherited]

Definition at line 61 of file nsUCS2BEToUnicode.h.

Definition at line 95 of file nsUCS2BEToUnicode.h.

Definition at line 96 of file nsUCS2BEToUnicode.h.

PRUint8 nsUTF16ToUnicodeBase::mState [protected, inherited]

Definition at line 60 of file nsUCS2BEToUnicode.h.


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