os/textandloc/charconvfw/charconvplugins/src/shared/gbk_shared.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include <e32std.h>
    20 #include <charconv.h>
    21 #include <convdata.h>
    22 #include <convgeneratedcpp.h>
    23 #include "gb2312.h"
    24 #include "gbk.h"
    25 
    26 #define ARRAY_LENGTH(aArray) (sizeof(aArray)/sizeof((aArray)[0]))
    27 
    28 const TInt KNumberOfBytesOfWorkingMemory=(21+21+2+1)*sizeof(SCnvConversionData::SOneDirectionData::SRange); // GB2312.CTL defines 21 foreign-to-Unicode ranges and 21 Unicode-to-foreign ranges, and GBK.CTL defines 2 foreign-to-Unicode ranges and 1 Unicode-to-foreign range
    29 
    30 _LIT(KLitPanicText, "GBK_SHARED");
    31 
    32 enum TPanic
    33 	{
    34 	EPanicTooManyBytesOfWorkingMemoryUsed1=1,
    35 	EPanicTooManyBytesOfWorkingMemoryUsed2
    36 	};
    37 
    38 LOCAL_D const SCnvConversionData::SVariableByteData::SRange foreignVariableByteDataRanges[]=
    39 	{
    40 		{
    41 		0x00,
    42 		0x7f,
    43 		0,
    44 		0
    45 		},
    46 		{
    47 		0x80,
    48 		0xff,
    49 		1,
    50 		0
    51 		}
    52 	};
    53 
    54 LOCAL_C void Panic(TPanic aPanic)
    55 	{
    56 	User::Panic(KLitPanicText, aPanic);
    57 	}
    58 
    59 LOCAL_C void SetUpCompleteGbkConversionData(SCnvConversionData& aCompleteGbkConversionData, TUint8* aWorkingMemory)
    60 	{
    61 	const SCnvConversionData& gb2312ConversionData=CnvGb2312::ConversionData();
    62 	const SCnvConversionData& supplementaryGbkConversionData=conversionData;
    63 	// create a SCnvConversionData that is the combination of gb2312ConversionData and supplementaryGbkConversionData
    64 	aCompleteGbkConversionData.iEndiannessOfForeignCharacters=SCnvConversionData::EFixedBigEndian;
    65 	aCompleteGbkConversionData.iForeignVariableByteData.iNumberOfRanges=ARRAY_LENGTH(foreignVariableByteDataRanges);
    66 	aCompleteGbkConversionData.iForeignVariableByteData.iRangeArray=foreignVariableByteDataRanges;
    67 	TInt numberOfBytesOfWorkingMemoryUsed=0;
    68 	// set up the foreign-to-Unicode data
    69 	const TInt numberOfForeignToUnicodeDataRanges=gb2312ConversionData.iForeignToUnicodeData.iNumberOfRanges+supplementaryGbkConversionData.iForeignToUnicodeData.iNumberOfRanges;
    70 	aCompleteGbkConversionData.iForeignToUnicodeData.iNumberOfRanges=numberOfForeignToUnicodeDataRanges;
    71 	SCnvConversionData::SOneDirectionData::SRange* foreignToUnicodeDataRangeArray=REINTERPRET_CAST(SCnvConversionData::SOneDirectionData::SRange*, aWorkingMemory+numberOfBytesOfWorkingMemoryUsed);
    72 	numberOfBytesOfWorkingMemoryUsed+=(numberOfForeignToUnicodeDataRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    73 	__ASSERT_ALWAYS(numberOfBytesOfWorkingMemoryUsed<=KNumberOfBytesOfWorkingMemory, Panic(EPanicTooManyBytesOfWorkingMemoryUsed1));
    74 	aCompleteGbkConversionData.iForeignToUnicodeData.iRangeArray=foreignToUnicodeDataRangeArray;
    75 	Mem::Copy(foreignToUnicodeDataRangeArray, gb2312ConversionData.iForeignToUnicodeData.iRangeArray, gb2312ConversionData.iForeignToUnicodeData.iNumberOfRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    76 	Mem::Copy(foreignToUnicodeDataRangeArray+gb2312ConversionData.iForeignToUnicodeData.iNumberOfRanges, supplementaryGbkConversionData.iForeignToUnicodeData.iRangeArray, supplementaryGbkConversionData.iForeignToUnicodeData.iNumberOfRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    77 	// set up the Unicode-to-foreign data
    78 	const TInt numberOfUnicodeToForeignDataRanges=gb2312ConversionData.iUnicodeToForeignData.iNumberOfRanges+supplementaryGbkConversionData.iUnicodeToForeignData.iNumberOfRanges;
    79 	aCompleteGbkConversionData.iUnicodeToForeignData.iNumberOfRanges=numberOfUnicodeToForeignDataRanges;
    80 	SCnvConversionData::SOneDirectionData::SRange* unicodeToForeignDataRangeArray=REINTERPRET_CAST(SCnvConversionData::SOneDirectionData::SRange*, aWorkingMemory+numberOfBytesOfWorkingMemoryUsed);
    81 	numberOfBytesOfWorkingMemoryUsed+=(numberOfUnicodeToForeignDataRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    82 	__ASSERT_ALWAYS(numberOfBytesOfWorkingMemoryUsed<=KNumberOfBytesOfWorkingMemory, Panic(EPanicTooManyBytesOfWorkingMemoryUsed2));
    83 	aCompleteGbkConversionData.iUnicodeToForeignData.iRangeArray=unicodeToForeignDataRangeArray;
    84 	Mem::Copy(unicodeToForeignDataRangeArray, gb2312ConversionData.iUnicodeToForeignData.iRangeArray, gb2312ConversionData.iUnicodeToForeignData.iNumberOfRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    85 	Mem::Copy(unicodeToForeignDataRangeArray+gb2312ConversionData.iUnicodeToForeignData.iNumberOfRanges, supplementaryGbkConversionData.iUnicodeToForeignData.iRangeArray, supplementaryGbkConversionData.iUnicodeToForeignData.iNumberOfRanges*sizeof(SCnvConversionData::SOneDirectionData::SRange));
    86 	}
    87 
    88 EXPORT_C const TDesC8& CnvGbk::ReplacementForUnconvertibleUnicodeCharacters()
    89 	{
    90 	return ReplacementForUnconvertibleUnicodeCharacters_internal();
    91 	}
    92 
    93 EXPORT_C const SCnvConversionData& CnvGbk::ConversionData()
    94 	{
    95 	return conversionData;
    96 	}
    97 
    98 EXPORT_C TInt CnvGbk::ConvertFromUnicode(CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, const TDesC8& aReplacementForUnconvertibleUnicodeCharacters, TDes8& aForeign, const TDesC16& aUnicode, CCnvCharacterSetConverter::TArrayOfAscendingIndices& aIndicesOfUnconvertibleCharacters)
    99 	{
   100 	TUint notUsed;
   101 	return ConvertFromUnicode(aDefaultEndiannessOfForeignCharacters, aReplacementForUnconvertibleUnicodeCharacters, aForeign, aUnicode, aIndicesOfUnconvertibleCharacters, notUsed, 0);
   102 	}
   103 
   104 EXPORT_C TInt CnvGbk::ConvertFromUnicode(CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, const TDesC8& aReplacementForUnconvertibleUnicodeCharacters, TDes8& aForeign, const TDesC16& aUnicode, CCnvCharacterSetConverter::TArrayOfAscendingIndices& aIndicesOfUnconvertibleCharacters, TUint& aOutputConversionFlags, TUint aInputConversionFlags)
   105 	{
   106 	SCnvConversionData completeGbkConversionData;
   107 	TUint8 workingMemory[KNumberOfBytesOfWorkingMemory]; // 900 bytes on the stack is just about tolerable (no other functions called from here will make any significant use of the stack)
   108 	SetUpCompleteGbkConversionData(completeGbkConversionData, workingMemory);
   109 	return CCnvCharacterSetConverter::DoConvertFromUnicode(completeGbkConversionData, aDefaultEndiannessOfForeignCharacters, aReplacementForUnconvertibleUnicodeCharacters, aForeign, aUnicode, aIndicesOfUnconvertibleCharacters, aOutputConversionFlags, aInputConversionFlags);
   110 	}
   111 
   112 EXPORT_C TInt CnvGbk::ConvertToUnicode(CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, TDes16& aUnicode, const TDesC8& aForeign, TInt& aNumberOfUnconvertibleCharacters, TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter)
   113 	{
   114 	TUint notUsed;
   115 	return ConvertToUnicode(aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign, aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter, notUsed, 0);
   116 	}
   117 
   118 EXPORT_C TInt CnvGbk::ConvertToUnicode(CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, TDes16& aUnicode, const TDesC8& aForeign, TInt& aNumberOfUnconvertibleCharacters, TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter, TUint& aOutputConversionFlags, TUint aInputConversionFlags)
   119 	{
   120 	SCnvConversionData completeGbkConversionData;
   121 	TUint8 workingMemory[KNumberOfBytesOfWorkingMemory]; // 900 bytes on the stack is just about tolerable (no other functions called from here will make any significant use of the stack)
   122 	SetUpCompleteGbkConversionData(completeGbkConversionData, workingMemory);
   123 	return CCnvCharacterSetConverter::DoConvertToUnicode(completeGbkConversionData, aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign, aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter, aOutputConversionFlags, aInputConversionFlags);
   124 	}
   125 
   126 EXPORT_C TBool CnvGbk::IsCharGBBased(TInt& aConfidenceLevel, const TDesC8& aSample)
   127 	{
   128 	return CnvGb2312::IsCharGBBased(aConfidenceLevel, aSample);
   129 	}