From ebd5ecb2a2d7f3ae89e70c86b80ad4b79cc2e971 Mon Sep 17 00:00:00 2001 From: "ftang%netscape.com" Date: Mon, 13 Sep 1999 19:27:05 +0000 Subject: [PATCH] check in GBK converter for Xu, Yueheng leftbyte) - 0x81)*0xbf +( (short int)(pGBCode->rightbyte) - 0x40); + + if( (iGBKToUnicodeIndex >= 0 ) && ( iGBKToUnicodeIndex < MAX_GBK_LENGTH) ) + *pUnicode = GBKToUnicodeTable[iGBKToUnicodeIndex]; + +} + + + +NS_IMETHODIMP nsGBKToUnicode::ConvertNoBuff(const char* aSrc, + PRInt32 * aSrcLength, + PRUnichar *aDest, + PRInt32 * aDestLength) +{ + + short int i=0; + short int iSrcLength = (short int)(*aSrcLength); + DByte *pSrcDBCode = (DByte *)aSrc; + PRUnichar *pDestDBCode = (PRUnichar *)aDest; + int iDestlen = 0; + + + for (i=0;i= (*aDestLength) ) + { + break; + } + + if ( *aSrc & 0x80 ) + { + // The source is a GBCode + GBKToUnicode(pSrcDBCode, pDestDBCode); + aSrc += 2; + i++; + } + else + { + // The source is an ASCII + *pDestDBCode = (PRUnichar) ( ((char)(*aSrc) )& 0x00ff); + aSrc++; + } + + iDestlen++; + aDest++; + *aSrcLength = i+1; + } + + *aDestLength = iDestlen; + + return NS_OK; +} + + diff --git a/mozilla/intl/uconv/ucvcn/nsGBKToUnicode.h b/mozilla/intl/uconv/ucvcn/nsGBKToUnicode.h index 68df01bea64..2e3cf8af09e 100644 --- a/mozilla/intl/uconv/ucvcn/nsGBKToUnicode.h +++ b/mozilla/intl/uconv/ucvcn/nsGBKToUnicode.h @@ -15,3 +15,49 @@ * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ +#ifndef nsGBKToUnicode_h___ +#define nsGBKToUnicode_h___ + +#include "nsUCvCnSupport.h" + +//---------------------------------------------------------------------- +// Class nsGBKToUnicode [declaration] + +/** + * A character set converter from GBK to Unicode. + * + * + * @created 07/Sept/1999 + * @author Yueheng Xu, Yueheng.Xu@intel.com + */ +class nsGBKToUnicode : public nsMultiTableDecoderSupport +{ +public: + + /** + * Class constructor. + */ + nsGBKToUnicode(); + + /** + * Static class constructor. + */ + static nsresult CreateInstance(nsISupports **aResult); + + +protected: + + //-------------------------------------------------------------------- + // Subclassing of nsDecoderSupport class [declaration] + NS_IMETHOD ConvertNoBuff(const char* aSrc, + PRInt32 * aSrcLength, + PRUnichar *aDest, + PRInt32 * aDestLength); + + NS_IMETHOD GetMaxLength(const char * aSrc, PRInt32 aSrcLength, + PRInt32 * aDestLength); + + }; + +#endif /* nsGBKToUnicode_h___ */ + diff --git a/mozilla/intl/uconv/ucvcn/nsUnicodeToGBK.cpp b/mozilla/intl/uconv/ucvcn/nsUnicodeToGBK.cpp index 68df01bea64..83beb04c344 100644 --- a/mozilla/intl/uconv/ucvcn/nsUnicodeToGBK.cpp +++ b/mozilla/intl/uconv/ucvcn/nsUnicodeToGBK.cpp @@ -15,3 +15,160 @@ * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ + /** + * A character set converter from Unicode to GBK. + * + * + * @created 08/Sept/1999 + * @author Yueheng Xu, Yueheng.Xu@intel.com + */ + +#include "nsUnicodeToGBK.h" +#include "nsUCvCnDll.h" + + +#define _GBKU_TABLE_ // to use a shared GBKU table +#include "gbku.h" + +//---------------------------------------------------------------------- +// Global functions and data [declaration] + + +static PRInt16 g_ASCIIShiftTable[] = { + 0, u1ByteCharset, + ShiftCell(0,0,0,0,0,0,0,0) +}; + +static PRInt16 g_GB2312ShiftTable[] = { + 0, u2BytesGRCharset, + ShiftCell(0,0,0,0,0,0,0,0) +}; + +static PRInt16 *g_GB2312ShiftTableSet [] = { + g_ASCIIShiftTable, + g_GB2312ShiftTable +}; + +static PRUint16 *g_GB2312MappingTableSet [] ={ + g_AsciiMapping, + g_ufGB2312Mapping +}; +//---------------------------------------------------------------------- +// Class nsUnicodeToGBK [implementation] + +nsUnicodeToGBK::nsUnicodeToGBK() +: nsMultiTableEncoderSupport(2, + (uShiftTable**) &g_GB2312ShiftTableSet, + (uMappingTable**) &g_GB2312MappingTableSet) +{ +} + + +#define TRUE 1 +#define FALSE 0 + +void UnicodeToGBK(PRUnichar SrcUnicode, DByte *pGBCode) +{ + short int iRet = FALSE; + short int i = 0; + short int iGBKToUnicodeIndex = 0; + + + for ( i=0; ileftbyte = (char) ( iGBKToUnicodeIndex / 0x00BF + 0x0081) ; + pGBCode->leftbyte |= 0x80; + pGBCode->rightbyte = (char) ( iGBKToUnicodeIndex % 0x00BF+ 0x0040); + pGBCode->rightbyte |= 0x80; + } + } + + +} + + +NS_IMETHODIMP nsUnicodeToGBK::ConvertNoBuff(const PRUnichar * aSrc, + PRInt32 * aSrcLength, + char * aDest, + PRInt32 * aDestLength) +{ + PRInt32 i=0; + PRInt32 iSrcLength = *aSrcLength; + DByte *pDestDBCode; + DByte *pSrcDBCode; + PRInt32 iDestLength = 0; + + PRUnichar *pSrc = (PRUnichar *)aSrc; + + pDestDBCode = (DByte *)aDest; + + for (i=0;i< iSrcLength;i++) + { + pDestDBCode = (DByte *)aDest; + + if( (*pSrc) & 0xff00 ) + { + // hi byte has something, it is not ASCII, must be a GB + + UnicodeToGBK( *pSrc, pDestDBCode); + aDest += 2; // increment 2 bytes + pDestDBCode = (DByte *)aDest; + iDestLength +=2; + } + else + { + // this is an ASCII + pSrcDBCode = (DByte *)pSrc; + *aDest = pSrcDBCode->leftbyte; + aDest++; // increment 1 byte + iDestLength +=1; + } + pSrc++; // increment 2 bytes + + if ( iDestLength >= (*aDestLength) ) + { + break; + } + } + + *aDestLength = iDestLength; + *aSrcLength = i; + + return NS_OK; +} + + +nsresult nsUnicodeToGBK::CreateInstance(nsISupports ** aResult) +{ + nsIUnicodeEncoder *p = new nsUnicodeToGBK(); + if(p) { + *aResult = p; + return NS_OK; + } + return NS_ERROR_OUT_OF_MEMORY; +} + +//---------------------------------------------------------------------- +// Subclassing of nsTableEncoderSupport class [implementation] + +NS_IMETHODIMP nsUnicodeToGBK::GetMaxLength(const PRUnichar * aSrc, + PRInt32 aSrcLength, + PRInt32 * aDestLength) +{ + *aDestLength = 2 * aSrcLength; + return NS_OK; +} + diff --git a/mozilla/intl/uconv/ucvcn/nsUnicodeToGBK.h b/mozilla/intl/uconv/ucvcn/nsUnicodeToGBK.h index 68df01bea64..f0bc0d6179e 100644 --- a/mozilla/intl/uconv/ucvcn/nsUnicodeToGBK.h +++ b/mozilla/intl/uconv/ucvcn/nsUnicodeToGBK.h @@ -15,3 +15,50 @@ * Copyright (C) 1998 Netscape Communications Corporation. All Rights * Reserved. */ + + /** + * A character set converter from Unicode to GBK. + * + * + * @created 08/Sept/1999 + * @author Yueheng Xu, Yueheng.Xu@intel.com + */ + +#ifndef nsUnicodeToGBK_h___ +#define nsUnicodeToGBK_h___ + +#include "nsUCvCnSupport.h" + +//---------------------------------------------------------------------- +// Class nsUnicodeToGBK [declaration] + +class nsUnicodeToGBK: public nsMultiTableEncoderSupport +{ +public: + + /** + * Class constructor. + */ + nsUnicodeToGBK(); + + /** + * Static class constructor. + */ + static nsresult CreateInstance(nsISupports **aResult); + + +protected: + + //-------------------------------------------------------------------- + // Subclassing of nsEncoderSupport class [declaration] + NS_IMETHOD ConvertNoBuff(const PRUnichar * aSrc, + PRInt32 * aSrcLength, + char * aDest, + PRInt32 * aDestLength); + + NS_IMETHOD GetMaxLength(const PRUnichar * aSrc, PRInt32 aSrcLength, + PRInt32 * aDestLength); +}; + +#endif /* nsUnicodeToGBK_h___ */ +