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 HZ. 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: Fixed line continuation problem when line michael@0: * ended by '~'; michael@0: * Used table UnicodeToGBK[] to speed up the mapping. michael@0: */ michael@0: #include "nsUnicodeToHZ.h" michael@0: #include "gbku.h" michael@0: //---------------------------------------------------------------------- michael@0: // Class nsUnicodeToGBK [implementation] michael@0: #define HZ_STATE_GB 1 michael@0: #define HZ_STATE_ASCII 2 michael@0: #define HZ_STATE_TILD 3 michael@0: #define HZLEAD1 '~' michael@0: #define HZLEAD2 '{' michael@0: #define HZLEAD3 '}' michael@0: #define UNICODE_TILD 0x007E michael@0: nsUnicodeToHZ::nsUnicodeToHZ() : nsEncoderSupport(6) michael@0: { michael@0: mHZState = HZ_STATE_ASCII; // per HZ spec, default to HZ mode michael@0: } michael@0: NS_IMETHODIMP nsUnicodeToHZ::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 i=0; michael@0: int32_t iSrcLength = *aSrcLength; michael@0: int32_t iDestLength = 0; michael@0: michael@0: for (i=0;i< iSrcLength;i++) michael@0: { michael@0: if(! IS_ASCII(*aSrc)) michael@0: { michael@0: // hi byte has something, it is not ASCII, process as a GB michael@0: if ( mHZState != HZ_STATE_GB ) michael@0: { michael@0: // we are adding a '~{' ESC sequence to star a HZ string michael@0: mHZState = HZ_STATE_GB; michael@0: aDest[0] = '~'; michael@0: aDest[1] = '{'; michael@0: aDest += 2; // increment 2 bytes michael@0: iDestLength +=2; michael@0: } michael@0: if(mUtil.UnicodeToGBKChar(*aSrc, true, &aDest[0], &aDest[1])) { michael@0: aDest += 2; // increment 2 bytes michael@0: iDestLength +=2; michael@0: } else { michael@0: // some thing that we cannot convert michael@0: // xxx fix me ftang michael@0: // error handling here michael@0: } michael@0: } else { michael@0: // this is an ASCII michael@0: michael@0: // if we are in HZ mode, end it by adding a '~}' ESC sequence michael@0: if ( mHZState == HZ_STATE_GB ) michael@0: { michael@0: mHZState = HZ_STATE_ASCII; michael@0: aDest[0] = '~'; michael@0: aDest[1] = '}'; michael@0: aDest += 2; // increment 2 bytes michael@0: iDestLength +=2; michael@0: } michael@0: michael@0: // if this is a regular char '~' , convert it to two '~' michael@0: if ( *aSrc == UNICODE_TILD ) michael@0: { michael@0: aDest[0] = '~'; michael@0: aDest[1] = '~'; michael@0: aDest += 2; // increment 2 bytes michael@0: iDestLength +=2; michael@0: } else { michael@0: // other regular ASCII chars convert by normal ways michael@0: michael@0: // Is this works for both little endian and big endian machines ? michael@0: *aDest = (char) ( (char16_t)(*aSrc) ); michael@0: aDest++; // increment 1 byte michael@0: iDestLength +=1; michael@0: } michael@0: } michael@0: aSrc++; // increment 2 bytes michael@0: if ( iDestLength >= (*aDestLength) ) michael@0: { michael@0: break; michael@0: } michael@0: } michael@0: *aDestLength = iDestLength; michael@0: *aSrcLength = i; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsUnicodeToHZ::FinishNoBuff(char * aDest, int32_t * aDestLength) michael@0: { michael@0: if ( mHZState == HZ_STATE_GB ) michael@0: { michael@0: // if we are in HZ mode, end it by adding a '~}' ESC sequence michael@0: mHZState = HZ_STATE_ASCII; michael@0: aDest[0] = '~'; michael@0: aDest[1] = '}'; michael@0: *aDestLength = 2; michael@0: } else { michael@0: *aDestLength = 0; michael@0: } michael@0: return NS_OK; michael@0: }