From 95e705dbed9cc22552bcb96e011c3c1247577b30 Mon Sep 17 00:00:00 2001 From: "ftang%netscape.com" Date: Tue, 15 May 2001 12:33:38 +0000 Subject: [PATCH] add new file for bug 80772 r=bstell a=ftang --- intl/uconv/ucvcn/nsGBKConvUtil.cpp | 188 +++++++++++++++++++++++++++++ intl/uconv/ucvcn/nsGBKConvUtil.h | 38 ++++++ 2 files changed, 226 insertions(+) create mode 100644 intl/uconv/ucvcn/nsGBKConvUtil.cpp create mode 100644 intl/uconv/ucvcn/nsGBKConvUtil.h diff --git a/intl/uconv/ucvcn/nsGBKConvUtil.cpp b/intl/uconv/ucvcn/nsGBKConvUtil.cpp new file mode 100644 index 000000000000..41509e447dd2 --- /dev/null +++ b/intl/uconv/ucvcn/nsGBKConvUtil.cpp @@ -0,0 +1,188 @@ +/* -*- 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.1 (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. + * + * Contributor(s): + */ + +#include "nsGBKConvUtil.h" +#include "gbku.h" +#include "nsCRT.h" +#include "nsICharRepresentable.h" +#define MAX_GBK_LENGTH 24066 /* (0xfe-0x80)*(0xfe-0x3f) */ +//-------------------------------------------------------------------- +// nsGBKConvUtil +//-------------------------------------------------------------------- + +static PRBool gInitToGBKTable = PR_FALSE; +static PRUnichar gGBKToUnicodeTable[MAX_GBK_LENGTH] = { +#include "cp936map.h" +}; +static PRUint16 gUnicodeToGBKTable[0xA000-0x4e00]; + +PRBool nsGBKConvUtil::UnicodeToGBKChar( + PRUnichar aChar, PRBool aToGL, char* + aOutByte1, char* aOutByte2) +{ + NS_ASSERTION(gInitToGBKTable, "gGBKToUnicodeTable is not init yet. need to call InitToGBKTable first"); + PRBool found=PR_FALSE; + *aOutByte1 = *aOutByte2 = 0; + if(UNICHAR_IN_RANGE(0x4e00, aChar, 0x9FFF)) + { + PRUint16 item = gUnicodeToGBKTable[aChar - 0x4e00]; + *aOutByte1 = item >> 8; + *aOutByte2 = item & 0x00FF; + found = PR_TRUE; + } + for( PRInt32 i = 0; i < MAX_GBK_LENGTH; i++ ) + { + if( aChar == gGBKToUnicodeTable[i]) + { + *aOutByte1 = (i / 0x00BF + 0x0081) ; + *aOutByte2 = (i % 0x00BF + 0x0040) ; + found = PR_TRUE; + break; + } + } + if(! found) + return PR_FALSE; + + 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; + return PR_FALSE; + } + } + return PR_TRUE; +} +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"); + + PRUint8 i1 = (PRUint8)aByte1; + PRUint8 i2 = (PRUint8)aByte2; + PRUint16 idx = (i1 - 0x0081) * 0x00bf + i2 - 0x0040 ; + + 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; +} +void nsGBKConvUtil::InitToGBKTable() +{ + if ( gInitToGBKTable ) + return; + + PRUnichar unicode; + PRUnichar i; + // zap it to zero first + nsCRT::memset(gUnicodeToGBKTable,0, sizeof(gUnicodeToGBKTable)); + + for ( i=0; i