michael@0: /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: /** michael@0: * A character set converter from Unicode to GBK. michael@0: * michael@0: * michael@0: * @created 08/Sept/1999 michael@0: * @author Yueheng Xu, Yueheng.Xu@intel.com michael@0: * Revision History michael@0: * 04/Oct/1999. Yueheng Xu: used table gUnicodeToGBKTable[0x5200] to make michael@0: * Unicode to GB mapping fast michael@0: */ michael@0: michael@0: #include "nsUnicodeToGBK.h" michael@0: #include "gbku.h" michael@0: #include "uconvutil.h" michael@0: #include "nsCharTraits.h" michael@0: michael@0: //------------------------------------------------------------- michael@0: // Global table initialization function defined in gbku.h michael@0: //------------------------------------------------------------- michael@0: michael@0: //----------------------------------------------------------------------- michael@0: // Private class used by nsUnicodeToGB18030 and nsUnicodeToGB18030Font0 michael@0: // nsUnicodeToGB18030Uniq2Bytes michael@0: //----------------------------------------------------------------------- michael@0: static const uint16_t g_uf_gb18030_2bytes[] = { michael@0: #include "gb18030uniq2b.uf" michael@0: }; michael@0: class nsUnicodeToGB18030Uniq2Bytes : public nsTableEncoderSupport michael@0: { michael@0: public: michael@0: nsUnicodeToGB18030Uniq2Bytes() michael@0: : nsTableEncoderSupport(u2BytesCharset, michael@0: (uMappingTable*) &g_uf_gb18030_2bytes, 2) {} michael@0: protected: michael@0: }; michael@0: //----------------------------------------------------------------------- michael@0: // Private class used by nsUnicodeToGB18030 michael@0: // nsUnicodeTo4BytesGB18030 michael@0: //----------------------------------------------------------------------- michael@0: static const uint16_t g_uf_gb18030_4bytes[] = { michael@0: #include "gb180304bytes.uf" michael@0: }; michael@0: class nsUnicodeTo4BytesGB18030 : public nsTableEncoderSupport michael@0: { michael@0: public: michael@0: nsUnicodeTo4BytesGB18030() michael@0: : nsTableEncoderSupport(u4BytesGB18030Charset, michael@0: (uMappingTable*) &g_uf_gb18030_4bytes, 4) {} michael@0: protected: michael@0: }; michael@0: //----------------------------------------------------------------------- michael@0: // Private class used by nsUnicodeToGBK michael@0: // nsUnicodeToGBKUniq2Bytes michael@0: //----------------------------------------------------------------------- michael@0: static const uint16_t g_uf_gbk_2bytes[] = { michael@0: #include "gbkuniq2b.uf" michael@0: }; michael@0: class nsUnicodeToGBKUniq2Bytes : public nsTableEncoderSupport michael@0: { michael@0: public: michael@0: nsUnicodeToGBKUniq2Bytes() michael@0: : nsTableEncoderSupport(u2BytesCharset, michael@0: (uMappingTable*) &g_uf_gbk_2bytes, 2) {} michael@0: protected: michael@0: }; michael@0: //----------------------------------------------------------------------- michael@0: // nsUnicodeToGB18030 michael@0: //----------------------------------------------------------------------- michael@0: void nsUnicodeToGB18030::CreateExtensionEncoder() michael@0: { michael@0: mExtensionEncoder = new nsUnicodeToGB18030Uniq2Bytes(); michael@0: } michael@0: void nsUnicodeToGB18030::Create4BytesEncoder() michael@0: { michael@0: m4BytesEncoder = new nsUnicodeTo4BytesGB18030(); michael@0: } michael@0: michael@0: bool nsUnicodeToGB18030::EncodeSurrogate( michael@0: char16_t aSurrogateHigh, michael@0: char16_t aSurrogateLow, michael@0: char* aOut) michael@0: { michael@0: if( NS_IS_HIGH_SURROGATE(aSurrogateHigh) && michael@0: NS_IS_LOW_SURROGATE(aSurrogateLow) ) michael@0: { michael@0: // notice that idx does not include the 0x10000 michael@0: uint32_t idx = ((aSurrogateHigh - (char16_t)0xD800) << 10 ) | michael@0: (aSurrogateLow - (char16_t) 0xDC00); michael@0: michael@0: unsigned char *out = (unsigned char*) aOut; michael@0: // notice this is from 0x90 for supplment planes michael@0: out[0] = (idx / (10*126*10)) + 0x90; michael@0: idx %= (10*126*10); michael@0: out[1] = (idx / (10*126)) + 0x30; michael@0: idx %= (10*126); michael@0: out[2] = (idx / (10)) + 0x81; michael@0: out[3] = (idx % 10) + 0x30; michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: // Class nsUnicodeToGBK [implementation] michael@0: michael@0: nsUnicodeToGBK::nsUnicodeToGBK(uint32_t aMaxLength) : michael@0: nsEncoderSupport(aMaxLength) michael@0: { michael@0: mExtensionEncoder = nullptr; michael@0: m4BytesEncoder = nullptr; michael@0: mSurrogateHigh = 0; michael@0: } michael@0: void nsUnicodeToGBK::CreateExtensionEncoder() michael@0: { michael@0: mExtensionEncoder = new nsUnicodeToGBKUniq2Bytes(); michael@0: } michael@0: void nsUnicodeToGBK::Create4BytesEncoder() michael@0: { michael@0: m4BytesEncoder = nullptr; michael@0: } michael@0: bool nsUnicodeToGBK::TryExtensionEncoder( michael@0: char16_t aChar, michael@0: char* aOut, michael@0: int32_t *aOutLen michael@0: ) michael@0: { michael@0: if( NS_IS_HIGH_SURROGATE(aChar) || michael@0: NS_IS_LOW_SURROGATE(aChar) ) michael@0: { michael@0: // performance tune for surrogate characters michael@0: return false; michael@0: } michael@0: if(! mExtensionEncoder ) michael@0: CreateExtensionEncoder(); michael@0: if(mExtensionEncoder) michael@0: { michael@0: int32_t len = 1; michael@0: nsresult res = NS_OK; michael@0: res = mExtensionEncoder->Convert(&aChar, &len, aOut, aOutLen); michael@0: if(NS_SUCCEEDED(res) && (*aOutLen > 0)) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: michael@0: bool nsUnicodeToGBK::Try4BytesEncoder( michael@0: char16_t aChar, michael@0: char* aOut, michael@0: int32_t *aOutLen michael@0: ) michael@0: { michael@0: if( NS_IS_HIGH_SURROGATE(aChar) || michael@0: NS_IS_LOW_SURROGATE(aChar) ) michael@0: { michael@0: // performance tune for surrogate characters michael@0: return false; michael@0: } michael@0: if(! m4BytesEncoder ) michael@0: Create4BytesEncoder(); michael@0: if(m4BytesEncoder) michael@0: { michael@0: int32_t len = 1; michael@0: nsresult res = NS_OK; michael@0: res = m4BytesEncoder->Convert(&aChar, &len, aOut, aOutLen); michael@0: NS_ASSERTION(NS_FAILED(res) || ((1 == len) && (4 == *aOutLen)), michael@0: "unexpect conversion length"); michael@0: if(NS_SUCCEEDED(res) && (*aOutLen > 0)) michael@0: return true; michael@0: } michael@0: return false; michael@0: } michael@0: bool nsUnicodeToGBK::EncodeSurrogate( michael@0: char16_t aSurrogateHigh, michael@0: char16_t aSurrogateLow, michael@0: char* aOut) michael@0: { michael@0: return false; // GBK cannot encode Surrogate, let the subclass encode it. michael@0: } michael@0: michael@0: NS_IMETHODIMP nsUnicodeToGBK::ConvertNoBuff( michael@0: const char16_t * aSrc, michael@0: int32_t * aSrcLength, michael@0: char * aDest, michael@0: int32_t * aDestLength) michael@0: { michael@0: int32_t iSrcLength = 0; michael@0: int32_t iDestLength = 0; michael@0: char16_t unicode; michael@0: nsresult res = NS_OK; michael@0: while (iSrcLength < *aSrcLength ) michael@0: { michael@0: unicode = *aSrc; michael@0: //if unicode's hi byte has something, it is not ASCII, must be a GB michael@0: if(IS_ASCII(unicode)) michael@0: { michael@0: // this is an ASCII michael@0: *aDest = CAST_UNICHAR_TO_CHAR(*aSrc); michael@0: aDest++; // increment 1 byte michael@0: iDestLength +=1; michael@0: } else { michael@0: char byte1, byte2; michael@0: if(mUtil.UnicodeToGBKChar( unicode, false, &byte1, &byte2)) michael@0: { michael@0: // make sure we still have 2 bytes for output first michael@0: if(iDestLength+2 > *aDestLength) michael@0: { michael@0: res = NS_OK_UENC_MOREOUTPUT; michael@0: break; michael@0: } michael@0: aDest[0] = byte1; michael@0: aDest[1] = byte2; michael@0: aDest += 2; // increment 2 bytes michael@0: iDestLength +=2; michael@0: } else { michael@0: int32_t aOutLen = 2; michael@0: // make sure we still have 2 bytes for output first michael@0: if(iDestLength+2 > *aDestLength) michael@0: { michael@0: res = NS_OK_UENC_MOREOUTPUT; michael@0: break; michael@0: } michael@0: // we cannot map in the common mapping. Let's try to michael@0: // call the delegated 2 byte converter for the gbk or gb18030 michael@0: // unique 2 byte mapping michael@0: if(TryExtensionEncoder(unicode, aDest, &aOutLen)) michael@0: { michael@0: iDestLength += aOutLen; michael@0: aDest += aOutLen; michael@0: } else { michael@0: // make sure we still have 4 bytes for output first michael@0: if(iDestLength+4 > *aDestLength) michael@0: { michael@0: res = NS_OK_UENC_MOREOUTPUT; michael@0: break; michael@0: } michael@0: // we still cannot map. Let's try to michael@0: // call the delegated GB18030 4 byte converter michael@0: aOutLen = 4; michael@0: if( NS_IS_HIGH_SURROGATE(unicode) ) michael@0: { michael@0: if((iSrcLength+1) < *aSrcLength ) { michael@0: if(EncodeSurrogate(aSrc[0],aSrc[1], aDest)) { michael@0: // since we got a surrogate pair, we need to increment src. michael@0: iSrcLength++ ; michael@0: aSrc++; michael@0: iDestLength += aOutLen; michael@0: aDest += aOutLen; michael@0: } else { michael@0: // only get a high surrogate, but not a low surrogate michael@0: res = NS_ERROR_UENC_NOMAPPING; michael@0: iSrcLength++; // include length of the unmapped character michael@0: break; michael@0: } michael@0: } else { michael@0: mSurrogateHigh = aSrc[0]; michael@0: break; // this will go to afterwhileloop michael@0: } michael@0: } else { michael@0: if( NS_IS_LOW_SURROGATE(unicode) ) michael@0: { michael@0: if(NS_IS_HIGH_SURROGATE(mSurrogateHigh)) { michael@0: if(EncodeSurrogate(mSurrogateHigh, aSrc[0], aDest)) { michael@0: iDestLength += aOutLen; michael@0: aDest += aOutLen; michael@0: } else { michael@0: // only get a high surrogate, but not a low surrogate michael@0: res = NS_ERROR_UENC_NOMAPPING; michael@0: iSrcLength++; // include length of the unmapped character michael@0: break; michael@0: } michael@0: } else { michael@0: // only get a low surrogate, but not a low surrogate michael@0: res = NS_ERROR_UENC_NOMAPPING; michael@0: iSrcLength++; // include length of the unmapped character michael@0: break; michael@0: } michael@0: } else { michael@0: if(Try4BytesEncoder(unicode, aDest, &aOutLen)) michael@0: { michael@0: NS_ASSERTION((aOutLen == 4), "we should always generate 4 bytes here"); michael@0: iDestLength += aOutLen; michael@0: aDest += aOutLen; michael@0: } else { michael@0: res = NS_ERROR_UENC_NOMAPPING; michael@0: iSrcLength++; // include length of the unmapped character michael@0: break; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: iSrcLength++ ; // Each unicode char just count as one in char16_t string; michael@0: mSurrogateHigh = 0; michael@0: aSrc++; michael@0: if ( iDestLength >= (*aDestLength) && (iSrcLength < *aSrcLength) ) michael@0: { michael@0: res = NS_OK_UENC_MOREOUTPUT; michael@0: break; michael@0: } michael@0: } michael@0: //afterwhileloop: michael@0: *aDestLength = iDestLength; michael@0: *aSrcLength = iSrcLength; michael@0: return res; michael@0: }