Back to index

lightning-sunbird  0.9+nobinonly
Functions
nsUCS2BEToUnicode.cpp File Reference
#include "nsUCConstructors.h"
#include "nsUCS2BEToUnicode.h"
#include "nsUCvLatinDll.h"
#include <string.h>
#include "prtypes.h"

Go to the source code of this file.

Functions

static nsresult UTF16ConvertToUnicode (PRUint8 &aState, PRUint8 &aData, const char *aSrc, PRInt32 *aSrcLength, PRUnichar *aDest, PRInt32 *aDestLength)
static void SwapBytes (PRUnichar *aDest, PRInt32 aLen)

Function Documentation

static void SwapBytes ( PRUnichar aDest,
PRInt32  aLen 
) [static]

Definition at line 119 of file nsUCS2BEToUnicode.cpp.

{
  for (PRUnichar *p = aDest; aLen > 0; ++p, --aLen)
     *p = ((*p & 0xff) << 8) | ((*p >> 8) & 0xff);
}

Here is the caller graph for this function:

static nsresult UTF16ConvertToUnicode ( PRUint8 aState,
PRUint8 aData,
const char *  aSrc,
PRInt32 aSrcLength,
PRUnichar aDest,
PRInt32 aDestLength 
) [static]

Definition at line 46 of file nsUCS2BEToUnicode.cpp.

{
  const char* src = aSrc;
  const char* srcEnd = aSrc + *aSrcLength;
  PRUnichar* dest = aDest;
  PRUnichar* destEnd = aDest + *aDestLength;

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

    // Eliminate BOM (0xFEFF). Note that different endian case is taken care of
    // in |Convert| of LE and BE converters. Here, we only have to
    // deal with the same endian case. That is, 0xFFFE (byte-swapped BOM) is
    // illegal.
    if(0xFEFF == *((PRUnichar*)src)) {
      src+=2;
    } else if(0xFFFE == *((PRUnichar*)src)) {
      *aSrcLength=0;
      *aDestLength=0;
      return NS_ERROR_ILLEGAL_INPUT;
    }  
    aState=0;
  }

  PRInt32 copybytes;

  if((1 == aState) && (src < srcEnd))
  {
    if(dest >= destEnd)
      goto error;

    char tmpbuf[2];

    // the 1st byte of a 16-bit code unit was stored in |aData| in the previous
    // run while the 2nd byte has to come from |*src|. We just have to copy
    // 'byte-by-byte'. Byte-swapping, if necessary, will be done in |Convert| of
    // LE and BE converters.
    PRUnichar * up = (PRUnichar*) &tmpbuf[0];
    tmpbuf[0]= aData;
    tmpbuf[1]= *src++;
    *dest++ = *up;
  }
  
  copybytes = (destEnd-dest)*2;
  // if |srcEnd-src| is odd, we copy one fewer bytes.
  if(copybytes > (~1 & (srcEnd - src)))
      copybytes = ~1 & (srcEnd - src);
  memcpy(dest,src,copybytes);
  src +=copybytes;
  dest +=(copybytes/2);
  if(srcEnd == src)  { // srcLength was even.
     aState = 0;
  } else if(1 == (srcEnd - src) ) { // srcLength was odd. 
     aState = 1;
     aData  = *src++;  // store the lead byte of a 16-bit unit for the next run.
  } else  {
     goto error;
  }
  
  *aDestLength = dest - aDest;
  *aSrcLength =  src  - aSrc; 
  return NS_OK;

error:
  *aDestLength = dest - aDest;
  *aSrcLength =  src  - aSrc; 
  return  NS_OK_UDEC_MOREOUTPUT;
}

Here is the call graph for this function:

Here is the caller graph for this function: