2001-09-26 04:40:45 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-05-15 16:33:38 +04:00
|
|
|
|
|
|
|
#include "nsGBKConvUtil.h"
|
|
|
|
#include "gbku.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#define MAX_GBK_LENGTH 24066 /* (0xfe-0x80)*(0xfe-0x3f) */
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
// nsGBKConvUtil
|
|
|
|
//--------------------------------------------------------------------
|
|
|
|
|
2003-02-26 04:15:36 +03:00
|
|
|
static const PRUnichar gGBKToUnicodeTable[MAX_GBK_LENGTH] = {
|
2001-05-15 16:33:38 +04:00
|
|
|
#include "cp936map.h"
|
|
|
|
};
|
2012-12-17 04:20:01 +04:00
|
|
|
static const uint16_t gUnicodeToGBKTable[0xA000-0x4e00] = {
|
|
|
|
#include "cp936invmap.h"
|
|
|
|
};
|
2001-05-15 16:33:38 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsGBKConvUtil::UnicodeToGBKChar(
|
|
|
|
PRUnichar aChar, bool aToGL, char*
|
2001-05-15 16:33:38 +04:00
|
|
|
aOutByte1, char* aOutByte2)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found=false;
|
2001-05-15 16:33:38 +04:00
|
|
|
*aOutByte1 = *aOutByte2 = 0;
|
2002-02-13 04:31:54 +03:00
|
|
|
if(UNICHAR_IN_RANGE(0xd800, aChar, 0xdfff))
|
|
|
|
{
|
|
|
|
// surrogate is not in here
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-02-13 04:31:54 +03:00
|
|
|
}
|
2001-05-15 16:33:38 +04:00
|
|
|
if(UNICHAR_IN_RANGE(0x4e00, aChar, 0x9FFF))
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint16_t item = gUnicodeToGBKTable[aChar - 0x4e00];
|
2002-02-13 04:31:54 +03:00
|
|
|
if(item != 0)
|
|
|
|
{
|
|
|
|
*aOutByte1 = item >> 8;
|
|
|
|
*aOutByte2 = item & 0x00FF;
|
2011-10-17 18:59:28 +04:00
|
|
|
found = true;
|
2002-02-13 04:31:54 +03:00
|
|
|
} else {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-02-13 04:31:54 +03:00
|
|
|
}
|
2002-03-06 03:01:35 +03:00
|
|
|
} else {
|
|
|
|
// ugly linear search
|
2012-08-22 19:56:38 +04:00
|
|
|
for( int32_t i = 0; i < MAX_GBK_LENGTH; i++ )
|
2001-05-15 16:33:38 +04:00
|
|
|
{
|
2002-03-06 03:01:35 +03:00
|
|
|
if( aChar == gGBKToUnicodeTable[i])
|
|
|
|
{
|
|
|
|
*aOutByte1 = (i / 0x00BF + 0x0081) ;
|
|
|
|
*aOutByte2 = (i % 0x00BF + 0x0040) ;
|
2011-10-17 18:59:28 +04:00
|
|
|
found = true;
|
2002-03-06 03:01:35 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-05-15 16:33:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(! found)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2001-05-15 16:33:38 +04:00
|
|
|
|
|
|
|
if(aToGL) {
|
|
|
|
// to GL, we only return if it is in the range
|
|
|
|
if(UINT8_IN_RANGE(0xA1, *aOutByte1, 0xFE) &&
|
|
|
|
UINT8_IN_RANGE(0xA1, *aOutByte2, 0xFE))
|
|
|
|
{
|
|
|
|
// mask them to GL
|
|
|
|
*aOutByte1 &= 0x7F;
|
|
|
|
*aOutByte2 &= 0x7F;
|
|
|
|
} else {
|
|
|
|
// if it does not fit into 0xa1-0xfe 0xa1-0xfe range that mean
|
|
|
|
// it is not a GB2312 character, we cannot map to GL
|
|
|
|
*aOutByte1 = 0x00;
|
|
|
|
*aOutByte2 = 0x00;
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2001-05-15 16:33:38 +04:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2001-05-15 16:33:38 +04:00
|
|
|
}
|
|
|
|
PRUnichar nsGBKConvUtil::GBKCharToUnicode(char aByte1, char aByte2)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(UINT8_IN_RANGE(0x81,aByte1, 0xFE), "first byte out of range");
|
|
|
|
NS_ASSERTION(UINT8_IN_RANGE(0x40,aByte2, 0xFE), "second byte out of range");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t i1 = (uint8_t)aByte1;
|
|
|
|
uint8_t i2 = (uint8_t)aByte2;
|
|
|
|
uint16_t idx = (i1 - 0x0081) * 0x00bf + i2 - 0x0040 ;
|
2001-05-15 16:33:38 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(idx < MAX_GBK_LENGTH, "ARB");
|
|
|
|
// play it safe- add if statement here ot protect ARB
|
|
|
|
// probably not necessary
|
|
|
|
if(idx < MAX_GBK_LENGTH)
|
|
|
|
return gGBKToUnicodeTable[ idx ];
|
|
|
|
else
|
|
|
|
return UCS2_NO_MAPPING;
|
|
|
|
}
|