NS_IMETHODIMP nsUnicodeToGB2312V2::ConvertNoBuff(const PRUnichar * aSrc, PRInt32 * aSrcLength, char * aDest, PRInt32 * aDestLength) { PRInt32 iSrcLength = 0; PRInt32 iDestLength = 0; nsresult res = NS_OK; while (iSrcLength < *aSrcLength) { //if unicode's hi byte has something, it is not ASCII, must be a GB if(IS_ASCII(*aSrc)) { // this is an ASCII *aDest = CAST_UNICHAR_TO_CHAR(*aSrc); aDest++; // increment 1 byte iDestLength +=1; } else { char byte1, byte2; if(mUtil.UnicodeToGBKChar(*aSrc, false, &byte1, &byte2)) { if(iDestLength+2 > *aDestLength) { res = NS_OK_UENC_MOREOUTPUT; break; } aDest[0]=byte1; aDest[1]=byte2; aDest += 2; // increment 2 bytes iDestLength +=2; // each GB char count as two in char* string } else { // cannot convert res= NS_ERROR_UENC_NOMAPPING; iSrcLength++; // include length of the unmapped character break; } } iSrcLength++ ; // each unicode char just count as one in PRUnichar* string aSrc++; if ( iDestLength >= (*aDestLength) && (iSrcLength < *aSrcLength )) { res = NS_OK_UENC_MOREOUTPUT; break; } } *aDestLength = iDestLength; *aSrcLength = iSrcLength; return res; }
NS_IMETHODIMP nsUnicodeToGBK::ConvertNoBuff( const PRUnichar * aSrc, PRInt32 * aSrcLength, char * aDest, PRInt32 * aDestLength) { PRInt32 iSrcLength = 0; PRInt32 iDestLength = 0; PRUnichar unicode; nsresult res = NS_OK; while (iSrcLength < *aSrcLength ) { unicode = *aSrc; //if unicode's hi byte has something, it is not ASCII, must be a GB if(IS_ASCII(unicode)) { // this is an ASCII *aDest = CAST_UNICHAR_TO_CHAR(*aSrc); aDest++; // increment 1 byte iDestLength +=1; } else { char byte1, byte2; if(mUtil.UnicodeToGBKChar( unicode, false, &byte1, &byte2)) { // make sure we still have 2 bytes for output first if(iDestLength+2 > *aDestLength) { res = NS_OK_UENC_MOREOUTPUT; break; } aDest[0] = byte1; aDest[1] = byte2; aDest += 2; // increment 2 bytes iDestLength +=2; } else { PRInt32 aOutLen = 2; // make sure we still have 2 bytes for output first if(iDestLength+2 > *aDestLength) { res = NS_OK_UENC_MOREOUTPUT; break; } // we cannot map in the common mapping. Let's try to // call the delegated 2 byte converter for the gbk or gb18030 // unique 2 byte mapping if(TryExtensionEncoder(unicode, aDest, &aOutLen)) { iDestLength += aOutLen; aDest += aOutLen; } else { // make sure we still have 4 bytes for output first if(iDestLength+4 > *aDestLength) { res = NS_OK_UENC_MOREOUTPUT; break; } // we still cannot map. Let's try to // call the delegated GB18030 4 byte converter aOutLen = 4; if( NS_IS_HIGH_SURROGATE(unicode) ) { if((iSrcLength+1) < *aSrcLength ) { if(EncodeSurrogate(aSrc[0],aSrc[1], aDest)) { // since we got a surrogate pair, we need to increment src. iSrcLength++ ; aSrc++; iDestLength += aOutLen; aDest += aOutLen; } else { // only get a high surrogate, but not a low surrogate res = NS_ERROR_UENC_NOMAPPING; iSrcLength++; // include length of the unmapped character break; } } else { mSurrogateHigh = aSrc[0]; break; // this will go to afterwhileloop } } else { if( NS_IS_LOW_SURROGATE(unicode) ) { if(NS_IS_HIGH_SURROGATE(mSurrogateHigh)) { if(EncodeSurrogate(mSurrogateHigh, aSrc[0], aDest)) { iDestLength += aOutLen; aDest += aOutLen; } else { // only get a high surrogate, but not a low surrogate res = NS_ERROR_UENC_NOMAPPING; iSrcLength++; // include length of the unmapped character break; } } else { // only get a low surrogate, but not a low surrogate res = NS_ERROR_UENC_NOMAPPING; iSrcLength++; // include length of the unmapped character break; } } else { if(Try4BytesEncoder(unicode, aDest, &aOutLen)) { NS_ASSERTION((aOutLen == 4), "we should always generate 4 bytes here"); iDestLength += aOutLen; aDest += aOutLen; } else { res = NS_ERROR_UENC_NOMAPPING; iSrcLength++; // include length of the unmapped character break; } } } } } } iSrcLength++ ; // Each unicode char just count as one in PRUnichar string; mSurrogateHigh = 0; aSrc++; if ( iDestLength >= (*aDestLength) && (iSrcLength < *aSrcLength) ) { res = NS_OK_UENC_MOREOUTPUT; break; } } //afterwhileloop: *aDestLength = iDestLength; *aSrcLength = iSrcLength; return res; }