1999-09-08 00:04:53 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an "AS IS"
|
|
|
|
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
|
|
|
|
* the License for the specific language governing rights and limitations
|
|
|
|
* under the License.
|
|
|
|
*
|
|
|
|
* The Original Code is Mozilla Communicator client code.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape Communications
|
|
|
|
* Corporation. Portions created by Netscape are Copyright (C) 1998
|
|
|
|
* Netscape Communications Corporation. All Rights Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "nsGB2312ToUnicodeV2.h"
|
|
|
|
#include "nsUCvCnDll.h"
|
|
|
|
|
1999-09-22 07:35:31 +04:00
|
|
|
|
|
|
|
#define _GBKU_TABLE_ // to use a shared GBKU table
|
|
|
|
#include "gbku.h"
|
1999-09-08 00:04:53 +04:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Global functions and data [declaration]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Class nsGB2312ToUnicodeV2 [implementation]
|
|
|
|
|
|
|
|
nsresult nsGB2312ToUnicodeV2::CreateInstance(nsISupports ** aResult)
|
|
|
|
{
|
|
|
|
*aResult = new nsGB2312ToUnicodeV2();
|
|
|
|
return (*aResult == NULL)? NS_ERROR_OUT_OF_MEMORY : NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// Subclassing of nsTablesDecoderSupport class [implementation]
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsGB2312ToUnicodeV2::GetMaxLength(const char * aSrc,
|
|
|
|
PRInt32 aSrcLength,
|
|
|
|
PRInt32 * aDestLength)
|
|
|
|
{
|
|
|
|
*aDestLength = aSrcLength;
|
1999-09-22 07:35:31 +04:00
|
|
|
return NS_OK;
|
1999-09-08 00:04:53 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//Overwriting the ConvertNoBuff() in nsUCvCnSupport.cpp.
|
|
|
|
|
1999-09-22 07:35:31 +04:00
|
|
|
void nsGB2312ToUnicodeV2::GBKToUnicode(DByte *pGBCode, PRUnichar * pUnicode)
|
1999-09-08 00:04:53 +04:00
|
|
|
{
|
1999-09-22 07:35:31 +04:00
|
|
|
short int iGBKToUnicodeIndex;
|
1999-09-08 00:04:53 +04:00
|
|
|
|
|
|
|
if(pGBCode)
|
1999-09-22 07:35:31 +04:00
|
|
|
iGBKToUnicodeIndex = ( (short int)(pGBCode->leftbyte) - 0x81)*0xbf +( (short int)(pGBCode->rightbyte) - 0x40);
|
1999-09-08 00:04:53 +04:00
|
|
|
|
1999-09-22 07:35:31 +04:00
|
|
|
if( (iGBKToUnicodeIndex >= 0 ) && ( iGBKToUnicodeIndex < MAX_GBK_LENGTH) )
|
|
|
|
*pUnicode = GBKToUnicodeTable[iGBKToUnicodeIndex];
|
1999-09-08 00:04:53 +04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-09-22 07:35:31 +04:00
|
|
|
|
|
|
|
|
1999-09-08 00:04:53 +04:00
|
|
|
NS_IMETHODIMP nsGB2312ToUnicodeV2::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<iSrcLength;i++)
|
|
|
|
{
|
|
|
|
pSrcDBCode = (DByte *)aSrc;
|
|
|
|
pDestDBCode = aDest;
|
|
|
|
|
|
|
|
if ( iDestlen >= (*aDestLength) )
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( *aSrc & 0x80 )
|
|
|
|
{
|
|
|
|
// The source is a GBCode
|
1999-09-22 07:35:31 +04:00
|
|
|
GBKToUnicode(pSrcDBCode, pDestDBCode);
|
1999-09-08 00:04:53 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|