nsresult UTF16ConvertToUnicode(PRUint8& aState, PRUint8& aData, const char * aSrc, PRInt32 * aSrcLength, PRUnichar * aDest, PRInt32 * aDestLength)
static nsresult UTF16ConvertToUnicode(PRUint8& aState, PRUint8& aData, const char * aSrc, PRInt32 * aSrcLength, PRUnichar * aDest, PRInt32 * aDestLength)
nsresult UTF16ConvertToUnicode(PRUint8& aState, PRUint8& aData, const char * aSrc, PRInt32 * aSrcLength, PRUnichar * aDest, PRInt32 * aDestLength){  const char* src = aSrc;  const char* srcEnd = aSrc + *aSrcLength;  PRUnichar* dest = aDest;  PRUnichar* destEnd = aDest + *aDestLength;  PRInt32 copybytes;  if(2 == aState) // first time called  {    // eleminate BOM    if(0xFEFF == *((PRUnichar*)src)) {      src+=2;    } else if(0xFFFE == *((PRUnichar*)src)) {      *aSrcLength=0;      *aDestLength=0;      return NS_ERROR_ILLEGAL_INPUT;    }      aState=0;  }  if((1 == aState) && (src < srcEnd))  {    if(dest >= destEnd)      goto error;    if(src>=srcEnd)      goto done;    char tmpbuf[2];    PRUnichar * up = (PRUnichar*) &tmpbuf[0];    tmpbuf[0]= aData;    tmpbuf[1]= *src++;    *dest++ = *up;  }    copybytes = (destEnd-dest)*2;  if(copybytes > (0xFFFE & (srcEnd-src)))      copybytes = 0xFFFE & (srcEnd-src);  memcpy(dest,src,copybytes);  src +=copybytes;  dest +=(copybytes/2);  if(srcEnd==src)  {     aState = 0;  } else if(1 == (srcEnd-src) ) {     aState = 1;     aData  = *src++;  } else  {     goto error;  }  done:  *aDestLength = dest - aDest;  *aSrcLength =  src  - aSrc;   return NS_OK;error:  *aDestLength = dest - aDest;  *aSrcLength =  src  - aSrc;   return  NS_OK_UDEC_MOREOUTPUT;}