os/textandloc/charconvfw/charconvplugins/src/plugins/eucjp_packed.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies).
sl@0
     3
* All rights reserved.
sl@0
     4
* This component and the accompanying materials are made available
sl@0
     5
* under the terms of "Eclipse Public License v1.0"
sl@0
     6
* which accompanies this distribution, and is available
sl@0
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
sl@0
     8
*
sl@0
     9
* Initial Contributors:
sl@0
    10
* Nokia Corporation - initial contribution.
sl@0
    11
*
sl@0
    12
* Contributors:
sl@0
    13
*
sl@0
    14
* Description: 
sl@0
    15
*
sl@0
    16
*/
sl@0
    17
sl@0
    18
sl@0
    19
#include <e32std.h>
sl@0
    20
#include <charconv.h>
sl@0
    21
#include <convutils.h>
sl@0
    22
#include "jisx0201.h"
sl@0
    23
#include "jisx0208.h"
sl@0
    24
#include "jisx0212.h"
sl@0
    25
#include <ecom/implementationproxy.h>
sl@0
    26
#include <charactersetconverter.h>
sl@0
    27
sl@0
    28
const TUint KSingleShift2=0x8e;
sl@0
    29
const TUint KSingleShift3=0x8f;
sl@0
    30
sl@0
    31
_LIT8(KLit8EucJpPackedReplacementForUnconvertibleUnicodeCharacters, "\xa1\xa9"); // fullwidth question mark
sl@0
    32
sl@0
    33
#if defined(_DEBUG)
sl@0
    34
sl@0
    35
_LIT(KLitPanicText, "EUCJP_PACKED");
sl@0
    36
sl@0
    37
enum TPanic
sl@0
    38
	{
sl@0
    39
	EPanicNothingToConvert1=1,
sl@0
    40
	EPanicNothingToConvert2,
sl@0
    41
	EPanicNothingToConvert3,
sl@0
    42
	EPanicNothingToConvert4,
sl@0
    43
	EPanicNothingToConvert5,
sl@0
    44
	EPanicNothingToConvert6,
sl@0
    45
	EPanicOddNumberOfBytes1,
sl@0
    46
	EPanicOddNumberOfBytes2,
sl@0
    47
	EPanicOddNumberOfBytes3,
sl@0
    48
	EPanicOddNumberOfBytes4,
sl@0
    49
	EPanicOddNumberOfBytes5,
sl@0
    50
	EPanicOddNumberOfBytes6,
sl@0
    51
	EPanicBadHighBit1,
sl@0
    52
	EPanicBadHighBit2,
sl@0
    53
	EPanicBadHighBit3,
sl@0
    54
	EPanicBadHighBit4,
sl@0
    55
	EPanicBadHighBit5,
sl@0
    56
	EPanicBadHighBit6,
sl@0
    57
	EPanicBadHighBit7,
sl@0
    58
	EPanicBadPointers1,
sl@0
    59
	EPanicBadPointers2,
sl@0
    60
	EPanicBadPointers3,
sl@0
    61
	EPanicBadPointers4,
sl@0
    62
	EPanicBadPointers5,
sl@0
    63
	EPanicBadPointers6,
sl@0
    64
	EPanicBadPointers7,
sl@0
    65
	EPanicBadPointers8,
sl@0
    66
	EPanicBadPointers9,
sl@0
    67
	EPanicBadPointers10,
sl@0
    68
	EPanicBadPointers11,
sl@0
    69
	EPanicBadPointers12,
sl@0
    70
	EPanicBadPointers13,
sl@0
    71
	EPanicBadPointers14,
sl@0
    72
	EPanicBadPointers15,
sl@0
    73
	EPanicBadPointers16,
sl@0
    74
	EPanicBadPointers17,
sl@0
    75
	EPanicBadPointers18,
sl@0
    76
	EPanicBadPointers19,
sl@0
    77
	EPanicBadPointers20,
sl@0
    78
	EPanicBadPointers21,
sl@0
    79
	EPanicBadPointers22,
sl@0
    80
	EPanicBadPointers23,
sl@0
    81
	EPanicBadPointers24,
sl@0
    82
	EPanicBadPointers25,
sl@0
    83
	EPanicBadPointers26,
sl@0
    84
	EPanicBadPointers27,
sl@0
    85
	EPanicBadPointers28,
sl@0
    86
	EPanicBadPointers29,
sl@0
    87
	EPanicBadPointers30,
sl@0
    88
	EPanicBadPointers31,
sl@0
    89
	EPanicBadPointers32,
sl@0
    90
	EPanicBadPointers33,
sl@0
    91
	EPanicBadPointers34,
sl@0
    92
	EPanicBadPointers35,
sl@0
    93
	EPanicBadPointers36,
sl@0
    94
	EPanicBadCalculation1,
sl@0
    95
	EPanicBadCalculation2,
sl@0
    96
	EPanicNumberOfBytesIsNotMultipleOfThree1,
sl@0
    97
	EPanicNumberOfBytesIsNotMultipleOfThree2,
sl@0
    98
	EPanicSingleShift2Expected,
sl@0
    99
	EPanicSingleShift3Expected
sl@0
   100
	};
sl@0
   101
sl@0
   102
LOCAL_C void Panic(TPanic aPanic)
sl@0
   103
	{
sl@0
   104
	User::Panic(KLitPanicText, aPanic);
sl@0
   105
	}
sl@0
   106
sl@0
   107
#endif
sl@0
   108
sl@0
   109
sl@0
   110
class CEucjpPackedConverterImpl : public CCharacterSetConverterPluginInterface
sl@0
   111
	{
sl@0
   112
sl@0
   113
public:
sl@0
   114
	virtual const TDesC8& ReplacementForUnconvertibleUnicodeCharacters();
sl@0
   115
sl@0
   116
	virtual TInt ConvertFromUnicode(
sl@0
   117
		CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, 
sl@0
   118
		const TDesC8& aReplacementForUnconvertibleUnicodeCharacters, 
sl@0
   119
		TDes8& aForeign, 
sl@0
   120
		const TDesC16& aUnicode, 
sl@0
   121
		CCnvCharacterSetConverter::TArrayOfAscendingIndices& aIndicesOfUnconvertibleCharacters);
sl@0
   122
sl@0
   123
	virtual TInt ConvertToUnicode(
sl@0
   124
		CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, 
sl@0
   125
		TDes16& aUnicode, 
sl@0
   126
		const TDesC8& aForeign, 
sl@0
   127
		TInt& aState, 
sl@0
   128
		TInt& aNumberOfUnconvertibleCharacters, 
sl@0
   129
		TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter);
sl@0
   130
sl@0
   131
	virtual TBool IsInThisCharacterSetL(
sl@0
   132
		TBool& aSetToTrue, 
sl@0
   133
		TInt& aConfidenceLevel, 
sl@0
   134
		const TDesC8& aSample);
sl@0
   135
sl@0
   136
	static CEucjpPackedConverterImpl* NewL();
sl@0
   137
	virtual ~CEucjpPackedConverterImpl();
sl@0
   138
sl@0
   139
private:
sl@0
   140
	CEucjpPackedConverterImpl();
sl@0
   141
sl@0
   142
	};
sl@0
   143
sl@0
   144
sl@0
   145
sl@0
   146
sl@0
   147
sl@0
   148
const TDesC8& CEucjpPackedConverterImpl::ReplacementForUnconvertibleUnicodeCharacters()
sl@0
   149
	{
sl@0
   150
	return KLit8EucJpPackedReplacementForUnconvertibleUnicodeCharacters;
sl@0
   151
	}
sl@0
   152
sl@0
   153
LOCAL_C void DummyConvertFromIntermediateBufferInPlace(TInt, TDes8&, TInt& aNumberOfCharactersThatDroppedOut)
sl@0
   154
	{
sl@0
   155
	aNumberOfCharactersThatDroppedOut=0;
sl@0
   156
	}
sl@0
   157
sl@0
   158
LOCAL_C void ConvertFromJisX0208ToEucJpPackedInPlace(TInt aStartPositionInDescriptor, TDes8& aDescriptor, TInt& aNumberOfCharactersThatDroppedOut)
sl@0
   159
	{
sl@0
   160
	aNumberOfCharactersThatDroppedOut=0;
sl@0
   161
	const TInt descriptorLength=aDescriptor.Length();
sl@0
   162
	__ASSERT_DEBUG(descriptorLength>aStartPositionInDescriptor, Panic(EPanicNothingToConvert1));
sl@0
   163
	__ASSERT_DEBUG((descriptorLength-aStartPositionInDescriptor)%2==0, Panic(EPanicOddNumberOfBytes1));
sl@0
   164
	TUint8* pointerToCurrentByte=CONST_CAST(TUint8*, aDescriptor.Ptr());
sl@0
   165
	const TUint8* const pointerToLastByte=pointerToCurrentByte+(descriptorLength-1);
sl@0
   166
	pointerToCurrentByte+=aStartPositionInDescriptor;
sl@0
   167
	FOREVER
sl@0
   168
		{
sl@0
   169
		const TUint currentByte=*pointerToCurrentByte;
sl@0
   170
		__ASSERT_DEBUG((currentByte&0x80)==0, Panic(EPanicBadHighBit1));
sl@0
   171
		*pointerToCurrentByte=STATIC_CAST(TUint8, currentByte|0x80);
sl@0
   172
		__ASSERT_DEBUG(pointerToCurrentByte<=pointerToLastByte, Panic(EPanicBadPointers1));
sl@0
   173
		if (pointerToCurrentByte>=pointerToLastByte)
sl@0
   174
			{
sl@0
   175
			break;
sl@0
   176
			}
sl@0
   177
		++pointerToCurrentByte;
sl@0
   178
		}
sl@0
   179
	}
sl@0
   180
sl@0
   181
LOCAL_C void ConvertFromHalfWidthKatakana8ToEucJpPackedInPlace(TInt aStartPositionInDescriptor, TDes8& aDescriptor, TInt& aNumberOfCharactersThatDroppedOut)
sl@0
   182
	{
sl@0
   183
	TInt descriptorLength=aDescriptor.Length();
sl@0
   184
	__ASSERT_DEBUG(descriptorLength>aStartPositionInDescriptor, Panic(EPanicNothingToConvert2));
sl@0
   185
	aNumberOfCharactersThatDroppedOut=Max(0, (descriptorLength-aStartPositionInDescriptor)-((aDescriptor.MaxLength()-aStartPositionInDescriptor)/2));
sl@0
   186
	descriptorLength-=aNumberOfCharactersThatDroppedOut;
sl@0
   187
	__ASSERT_DEBUG(descriptorLength>=aStartPositionInDescriptor, Panic(EPanicBadCalculation1));
sl@0
   188
	if (descriptorLength<=aStartPositionInDescriptor)
sl@0
   189
		{
sl@0
   190
		aDescriptor.SetLength(descriptorLength);
sl@0
   191
		}
sl@0
   192
	else
sl@0
   193
		{
sl@0
   194
		TUint8* pointerToTargetByte=CONST_CAST(TUint8*, aDescriptor.Ptr()); // pointerToTargetByte is initialized properly when descriptorLength has been offset to the actual final length of aDescriptor
sl@0
   195
		const TUint8* const pointerToFirstByte=pointerToTargetByte+aStartPositionInDescriptor;
sl@0
   196
		const TUint8* pointerToSourceByte=pointerToTargetByte+(descriptorLength-1);
sl@0
   197
		descriptorLength=((descriptorLength-aStartPositionInDescriptor)*2)+aStartPositionInDescriptor;
sl@0
   198
		__ASSERT_DEBUG((descriptorLength-aStartPositionInDescriptor)%2==0, Panic(EPanicOddNumberOfBytes2));
sl@0
   199
		aDescriptor.SetLength(descriptorLength);
sl@0
   200
		pointerToTargetByte+=descriptorLength-1; // pointerToTargetByte is is initialized properly here
sl@0
   201
		FOREVER
sl@0
   202
			{
sl@0
   203
			*pointerToTargetByte=*pointerToSourceByte;
sl@0
   204
			__ASSERT_DEBUG(pointerToTargetByte>pointerToFirstByte, Panic(EPanicBadPointers2));
sl@0
   205
			--pointerToTargetByte;
sl@0
   206
			*pointerToTargetByte=KSingleShift2;
sl@0
   207
			__ASSERT_DEBUG(pointerToTargetByte>=pointerToFirstByte, Panic(EPanicBadPointers3));
sl@0
   208
			if (pointerToTargetByte<=pointerToFirstByte)
sl@0
   209
				{
sl@0
   210
				break;
sl@0
   211
				}
sl@0
   212
			--pointerToTargetByte;
sl@0
   213
			__ASSERT_DEBUG(pointerToSourceByte>pointerToFirstByte, Panic(EPanicBadPointers4));
sl@0
   214
			--pointerToSourceByte;
sl@0
   215
			}
sl@0
   216
		__ASSERT_DEBUG(pointerToTargetByte==pointerToFirstByte, Panic(EPanicBadPointers5));
sl@0
   217
		__ASSERT_DEBUG(pointerToSourceByte==pointerToFirstByte, Panic(EPanicBadPointers6));
sl@0
   218
		}
sl@0
   219
	}
sl@0
   220
sl@0
   221
LOCAL_C void ConvertFromJisX0212ToEucJpPackedInPlace(TInt aStartPositionInDescriptor, TDes8& aDescriptor, TInt& aNumberOfCharactersThatDroppedOut)
sl@0
   222
	{
sl@0
   223
	TInt descriptorLength=aDescriptor.Length();
sl@0
   224
	__ASSERT_DEBUG(descriptorLength>aStartPositionInDescriptor, Panic(EPanicNothingToConvert3));
sl@0
   225
	__ASSERT_DEBUG((descriptorLength-aStartPositionInDescriptor)%2==0, Panic(EPanicOddNumberOfBytes3));
sl@0
   226
	aNumberOfCharactersThatDroppedOut=Max(0, ((descriptorLength-aStartPositionInDescriptor)/2)-((aDescriptor.MaxLength()-aStartPositionInDescriptor)/3));
sl@0
   227
	descriptorLength-=aNumberOfCharactersThatDroppedOut*2;
sl@0
   228
	__ASSERT_DEBUG(descriptorLength>=aStartPositionInDescriptor, Panic(EPanicBadCalculation2));
sl@0
   229
	if (descriptorLength<=aStartPositionInDescriptor)
sl@0
   230
		{
sl@0
   231
		aDescriptor.SetLength(descriptorLength);
sl@0
   232
		}
sl@0
   233
	else
sl@0
   234
		{
sl@0
   235
		__ASSERT_DEBUG((descriptorLength-aStartPositionInDescriptor)%2==0, Panic(EPanicOddNumberOfBytes4));
sl@0
   236
		TUint8* pointerToTargetByte=CONST_CAST(TUint8*, aDescriptor.Ptr()); // pointerToTargetByte is initialized properly when descriptorLength has been offset to the actual final length of aDescriptor
sl@0
   237
		const TUint8* const pointerToFirstByte=pointerToTargetByte+aStartPositionInDescriptor;
sl@0
   238
		const TUint8* pointerToSourceByte=pointerToTargetByte+(descriptorLength-1);
sl@0
   239
		descriptorLength=(((descriptorLength-aStartPositionInDescriptor)*3)/2)+aStartPositionInDescriptor;
sl@0
   240
		__ASSERT_DEBUG((descriptorLength-aStartPositionInDescriptor)%3==0, Panic(EPanicNumberOfBytesIsNotMultipleOfThree1));
sl@0
   241
		aDescriptor.SetLength(descriptorLength);
sl@0
   242
		pointerToTargetByte+=descriptorLength-1; // pointerToTargetByte is is initialized properly here
sl@0
   243
		FOREVER
sl@0
   244
			{
sl@0
   245
			__ASSERT_DEBUG((*pointerToSourceByte&0x80)==0, Panic(EPanicBadHighBit2));
sl@0
   246
			*pointerToTargetByte=STATIC_CAST(TUint8, *pointerToSourceByte|0x80);
sl@0
   247
			__ASSERT_DEBUG(pointerToTargetByte>pointerToFirstByte, Panic(EPanicBadPointers7));
sl@0
   248
			--pointerToTargetByte;
sl@0
   249
			__ASSERT_DEBUG(pointerToSourceByte>pointerToFirstByte, Panic(EPanicBadPointers8));
sl@0
   250
			--pointerToSourceByte;
sl@0
   251
			__ASSERT_DEBUG((*pointerToSourceByte&0x80)==0, Panic(EPanicBadHighBit3));
sl@0
   252
			*pointerToTargetByte=STATIC_CAST(TUint8, *pointerToSourceByte|0x80);
sl@0
   253
			__ASSERT_DEBUG(pointerToTargetByte>pointerToFirstByte, Panic(EPanicBadPointers9));
sl@0
   254
			--pointerToTargetByte;
sl@0
   255
			*pointerToTargetByte=KSingleShift3;
sl@0
   256
			__ASSERT_DEBUG(pointerToTargetByte>=pointerToFirstByte, Panic(EPanicBadPointers10));
sl@0
   257
			if (pointerToTargetByte<=pointerToFirstByte)
sl@0
   258
				{
sl@0
   259
				break;
sl@0
   260
				}
sl@0
   261
			--pointerToTargetByte;
sl@0
   262
			__ASSERT_DEBUG(pointerToSourceByte>pointerToFirstByte, Panic(EPanicBadPointers11));
sl@0
   263
			--pointerToSourceByte;
sl@0
   264
			}
sl@0
   265
		__ASSERT_DEBUG(pointerToTargetByte==pointerToFirstByte, Panic(EPanicBadPointers12));
sl@0
   266
		__ASSERT_DEBUG(pointerToSourceByte==pointerToFirstByte, Panic(EPanicBadPointers13));
sl@0
   267
		}
sl@0
   268
	}
sl@0
   269
sl@0
   270
TInt CEucjpPackedConverterImpl::ConvertFromUnicode(
sl@0
   271
		CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, 
sl@0
   272
		const TDesC8& aReplacementForUnconvertibleUnicodeCharacters, 
sl@0
   273
		TDes8& aForeign, 
sl@0
   274
		const TDesC16& aUnicode, 
sl@0
   275
		CCnvCharacterSetConverter::TArrayOfAscendingIndices& aIndicesOfUnconvertibleCharacters)
sl@0
   276
	{
sl@0
   277
	TFixedArray<CnvUtilities::SCharacterSet, 4> characterSets;
sl@0
   278
	characterSets[0].iConversionData=&CnvJisRoman::ConversionData();
sl@0
   279
	characterSets[0].iConvertFromIntermediateBufferInPlace=DummyConvertFromIntermediateBufferInPlace;
sl@0
   280
	characterSets[0].iEscapeSequence=&KNullDesC8;
sl@0
   281
	characterSets[1].iConversionData=&CnvJisX0208::ConversionData();
sl@0
   282
	characterSets[1].iConvertFromIntermediateBufferInPlace=ConvertFromJisX0208ToEucJpPackedInPlace;
sl@0
   283
	characterSets[1].iEscapeSequence=&KNullDesC8;
sl@0
   284
	characterSets[2].iConversionData=&CnvHalfWidthKatakana8::ConversionData();
sl@0
   285
	characterSets[2].iConvertFromIntermediateBufferInPlace=ConvertFromHalfWidthKatakana8ToEucJpPackedInPlace;
sl@0
   286
	characterSets[2].iEscapeSequence=&KNullDesC8;
sl@0
   287
	characterSets[3].iConversionData=&CnvJisX0212::ConversionData();
sl@0
   288
	characterSets[3].iConvertFromIntermediateBufferInPlace=ConvertFromJisX0212ToEucJpPackedInPlace;
sl@0
   289
	characterSets[3].iEscapeSequence=&KNullDesC8;
sl@0
   290
	return CnvUtilities::ConvertFromUnicode(aDefaultEndiannessOfForeignCharacters, aReplacementForUnconvertibleUnicodeCharacters, aForeign, aUnicode, aIndicesOfUnconvertibleCharacters, characterSets.Array());
sl@0
   291
	}
sl@0
   292
sl@0
   293
LOCAL_C TInt NumberOfBytesAbleToConvertToJisRoman(const TDesC8& aDescriptor)
sl@0
   294
	{
sl@0
   295
	const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1;
sl@0
   296
	const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length();
sl@0
   297
	if (pointerToPreviousByte==pointerToLastByte)
sl@0
   298
		{
sl@0
   299
		return 0;
sl@0
   300
		}
sl@0
   301
	FOREVER
sl@0
   302
		{
sl@0
   303
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers14));
sl@0
   304
		const TUint currentByte=*(pointerToPreviousByte+1);
sl@0
   305
		if (currentByte&0x80)
sl@0
   306
			{
sl@0
   307
			break;
sl@0
   308
			}
sl@0
   309
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers15));
sl@0
   310
		++pointerToPreviousByte;
sl@0
   311
		__ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers16));
sl@0
   312
		if (pointerToPreviousByte>=pointerToLastByte)
sl@0
   313
			{
sl@0
   314
			break;
sl@0
   315
			}
sl@0
   316
		}
sl@0
   317
	return (pointerToPreviousByte+1)-aDescriptor.Ptr();
sl@0
   318
	}
sl@0
   319
sl@0
   320
LOCAL_C TInt NumberOfBytesAbleToConvertToJisX0208(const TDesC8& aDescriptor)
sl@0
   321
	{
sl@0
   322
	const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1;
sl@0
   323
	const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length();
sl@0
   324
	if (pointerToPreviousByte==pointerToLastByte)
sl@0
   325
		{
sl@0
   326
		return 0;
sl@0
   327
		}
sl@0
   328
	FOREVER
sl@0
   329
		{
sl@0
   330
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers17));
sl@0
   331
		TUint currentByte=*(pointerToPreviousByte+1);
sl@0
   332
		if (currentByte<0xa0)
sl@0
   333
			{
sl@0
   334
			break;
sl@0
   335
			}
sl@0
   336
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers18));
sl@0
   337
		if (pointerToLastByte-pointerToPreviousByte<2)
sl@0
   338
			{
sl@0
   339
			break;
sl@0
   340
			}
sl@0
   341
		++pointerToPreviousByte;
sl@0
   342
		currentByte=*(pointerToPreviousByte+1);
sl@0
   343
		if (currentByte<0xa0)
sl@0
   344
			{
sl@0
   345
			return CCnvCharacterSetConverter::EErrorIllFormedInput;
sl@0
   346
			}
sl@0
   347
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers19));
sl@0
   348
		++pointerToPreviousByte;
sl@0
   349
		__ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers20));
sl@0
   350
		if (pointerToPreviousByte>=pointerToLastByte)
sl@0
   351
			{
sl@0
   352
			break;
sl@0
   353
			}
sl@0
   354
		}
sl@0
   355
	return (pointerToPreviousByte+1)-aDescriptor.Ptr();
sl@0
   356
	}
sl@0
   357
sl@0
   358
LOCAL_C TInt NumberOfBytesAbleToConvertToHalfWidthKatakana8(const TDesC8& aDescriptor)
sl@0
   359
	{
sl@0
   360
	const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1;
sl@0
   361
	const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length();
sl@0
   362
	if (pointerToPreviousByte==pointerToLastByte)
sl@0
   363
		{
sl@0
   364
		return 0;
sl@0
   365
		}
sl@0
   366
	FOREVER
sl@0
   367
		{
sl@0
   368
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers21));
sl@0
   369
		TUint currentByte=*(pointerToPreviousByte+1);
sl@0
   370
		if (currentByte!=KSingleShift2)
sl@0
   371
			{
sl@0
   372
			break;
sl@0
   373
			}
sl@0
   374
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers22));
sl@0
   375
		if (pointerToLastByte-pointerToPreviousByte<2)
sl@0
   376
			{
sl@0
   377
			break;
sl@0
   378
			}
sl@0
   379
		++pointerToPreviousByte;
sl@0
   380
		currentByte=*(pointerToPreviousByte+1);
sl@0
   381
		if (currentByte<0xa0)
sl@0
   382
			{
sl@0
   383
			return CCnvCharacterSetConverter::EErrorIllFormedInput;
sl@0
   384
			}
sl@0
   385
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers23));
sl@0
   386
		++pointerToPreviousByte;
sl@0
   387
		__ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers24));
sl@0
   388
		if (pointerToPreviousByte>=pointerToLastByte)
sl@0
   389
			{
sl@0
   390
			break;
sl@0
   391
			}
sl@0
   392
		}
sl@0
   393
	return (pointerToPreviousByte+1)-aDescriptor.Ptr();
sl@0
   394
	}
sl@0
   395
sl@0
   396
LOCAL_C TInt NumberOfBytesAbleToConvertToJisX0212(const TDesC8& aDescriptor)
sl@0
   397
	{
sl@0
   398
	const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1;
sl@0
   399
	const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length();
sl@0
   400
	if (pointerToPreviousByte==pointerToLastByte)
sl@0
   401
		{
sl@0
   402
		return 0;
sl@0
   403
		}
sl@0
   404
	FOREVER
sl@0
   405
		{
sl@0
   406
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers25));
sl@0
   407
		TUint currentByte=*(pointerToPreviousByte+1);
sl@0
   408
		if (currentByte!=KSingleShift3)
sl@0
   409
			{
sl@0
   410
			break;
sl@0
   411
			}
sl@0
   412
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers26));
sl@0
   413
		if (pointerToLastByte-pointerToPreviousByte<3)
sl@0
   414
			{
sl@0
   415
			break;
sl@0
   416
			}
sl@0
   417
		++pointerToPreviousByte;
sl@0
   418
		currentByte=*(pointerToPreviousByte+1);
sl@0
   419
		if (currentByte<0xa0)
sl@0
   420
			{
sl@0
   421
			return CCnvCharacterSetConverter::EErrorIllFormedInput;
sl@0
   422
			}
sl@0
   423
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers27));
sl@0
   424
		++pointerToPreviousByte;
sl@0
   425
		currentByte=*(pointerToPreviousByte+1);
sl@0
   426
		if (currentByte<0xa0)
sl@0
   427
			{
sl@0
   428
			return CCnvCharacterSetConverter::EErrorIllFormedInput;
sl@0
   429
			}
sl@0
   430
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers28));
sl@0
   431
		++pointerToPreviousByte;
sl@0
   432
		__ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers29));
sl@0
   433
		if (pointerToPreviousByte>=pointerToLastByte)
sl@0
   434
			{
sl@0
   435
			break;
sl@0
   436
			}
sl@0
   437
		}
sl@0
   438
	return (pointerToPreviousByte+1)-aDescriptor.Ptr();
sl@0
   439
	}
sl@0
   440
sl@0
   441
LOCAL_C void DummyConvertToIntermediateBufferInPlace(TDes8&)
sl@0
   442
	{
sl@0
   443
	}
sl@0
   444
sl@0
   445
LOCAL_C void ConvertToJisX0208FromEucJpPackedInPlace(TDes8& aDescriptor)
sl@0
   446
	{
sl@0
   447
	const TInt descriptorLength=aDescriptor.Length();
sl@0
   448
	__ASSERT_DEBUG(descriptorLength>0, Panic(EPanicNothingToConvert4));
sl@0
   449
	__ASSERT_DEBUG(descriptorLength%2==0, Panic(EPanicOddNumberOfBytes5));
sl@0
   450
	TUint8* pointerToCurrentByte=CONST_CAST(TUint8*, aDescriptor.Ptr());
sl@0
   451
	const TUint8* const pointerToLastByte=pointerToCurrentByte+(descriptorLength-1);
sl@0
   452
	FOREVER
sl@0
   453
		{
sl@0
   454
		const TUint currentByte=*pointerToCurrentByte;
sl@0
   455
		__ASSERT_DEBUG(currentByte&0x80, Panic(EPanicBadHighBit4));
sl@0
   456
		*pointerToCurrentByte=STATIC_CAST(TUint8, currentByte&~0x80);
sl@0
   457
		__ASSERT_DEBUG(pointerToCurrentByte<=pointerToLastByte, Panic(EPanicBadPointers30));
sl@0
   458
		if (pointerToCurrentByte>=pointerToLastByte)
sl@0
   459
			{
sl@0
   460
			break;
sl@0
   461
			}
sl@0
   462
		++pointerToCurrentByte;
sl@0
   463
		}
sl@0
   464
	}
sl@0
   465
sl@0
   466
LOCAL_C void ConvertToHalfWidthKatakana8FromEucJpPackedInPlace(TDes8& aDescriptor)
sl@0
   467
	{
sl@0
   468
	const TInt descriptorLength=aDescriptor.Length();
sl@0
   469
	__ASSERT_DEBUG(descriptorLength>0, Panic(EPanicNothingToConvert5));
sl@0
   470
	__ASSERT_DEBUG(descriptorLength%2==0, Panic(EPanicOddNumberOfBytes6));
sl@0
   471
	TUint8* pointerToTargetByte=CONST_CAST(TUint8*, aDescriptor.Ptr());
sl@0
   472
	const TUint8* pointerToSourceByte=pointerToTargetByte;
sl@0
   473
	const TUint8* const pointerToLastByte=pointerToSourceByte+(descriptorLength-1);
sl@0
   474
	FOREVER
sl@0
   475
		{
sl@0
   476
		__ASSERT_DEBUG(*pointerToSourceByte==KSingleShift2, Panic(EPanicSingleShift2Expected));
sl@0
   477
		__ASSERT_DEBUG(pointerToSourceByte<pointerToLastByte, Panic(EPanicBadPointers31));
sl@0
   478
		++pointerToSourceByte;
sl@0
   479
		const TUint sourceByte=*pointerToSourceByte;
sl@0
   480
		__ASSERT_DEBUG(sourceByte&0x80, Panic(EPanicBadHighBit5));
sl@0
   481
		*pointerToTargetByte=STATIC_CAST(TUint8, sourceByte);
sl@0
   482
		__ASSERT_DEBUG(pointerToSourceByte<=pointerToLastByte, Panic(EPanicBadPointers32));
sl@0
   483
		if (pointerToSourceByte>=pointerToLastByte)
sl@0
   484
			{
sl@0
   485
			break;
sl@0
   486
			}
sl@0
   487
		++pointerToSourceByte;
sl@0
   488
		++pointerToTargetByte;
sl@0
   489
		}
sl@0
   490
	aDescriptor.SetLength(descriptorLength/2);
sl@0
   491
	}
sl@0
   492
sl@0
   493
LOCAL_C void ConvertToJisX0212FromEucJpPackedInPlace(TDes8& aDescriptor)
sl@0
   494
	{
sl@0
   495
	const TInt descriptorLength=aDescriptor.Length();
sl@0
   496
	__ASSERT_DEBUG(descriptorLength>0, Panic(EPanicNothingToConvert6));
sl@0
   497
	__ASSERT_DEBUG(descriptorLength%3==0, Panic(EPanicNumberOfBytesIsNotMultipleOfThree2));
sl@0
   498
	TUint8* pointerToTargetByte=CONST_CAST(TUint8*, aDescriptor.Ptr());
sl@0
   499
	const TUint8* pointerToSourceByte=pointerToTargetByte;
sl@0
   500
	const TUint8* const pointerToLastByte=pointerToSourceByte+(descriptorLength-1);
sl@0
   501
	FOREVER
sl@0
   502
		{
sl@0
   503
		__ASSERT_DEBUG(*pointerToSourceByte==KSingleShift3, Panic(EPanicSingleShift3Expected));
sl@0
   504
		__ASSERT_DEBUG(pointerToSourceByte<pointerToLastByte, Panic(EPanicBadPointers33));
sl@0
   505
		++pointerToSourceByte;
sl@0
   506
		TUint sourceByte=*pointerToSourceByte;
sl@0
   507
		__ASSERT_DEBUG(sourceByte&0x80, Panic(EPanicBadHighBit6));
sl@0
   508
		*pointerToTargetByte=STATIC_CAST(TUint8, sourceByte&~0x80);
sl@0
   509
		__ASSERT_DEBUG(pointerToSourceByte<pointerToLastByte, Panic(EPanicBadPointers34));
sl@0
   510
		++pointerToSourceByte;
sl@0
   511
		sourceByte=*pointerToSourceByte;
sl@0
   512
		__ASSERT_DEBUG(sourceByte&0x80, Panic(EPanicBadHighBit7));
sl@0
   513
		__ASSERT_DEBUG(pointerToTargetByte<pointerToLastByte, Panic(EPanicBadPointers35));
sl@0
   514
		++pointerToTargetByte;
sl@0
   515
		*pointerToTargetByte=STATIC_CAST(TUint8, sourceByte&~0x80);
sl@0
   516
		__ASSERT_DEBUG(pointerToSourceByte<=pointerToLastByte, Panic(EPanicBadPointers36));
sl@0
   517
		if (pointerToSourceByte>=pointerToLastByte)
sl@0
   518
			{
sl@0
   519
			break;
sl@0
   520
			}
sl@0
   521
		++pointerToSourceByte;
sl@0
   522
		++pointerToTargetByte;
sl@0
   523
		}
sl@0
   524
	aDescriptor.SetLength((descriptorLength/3)*2);
sl@0
   525
	}
sl@0
   526
sl@0
   527
TInt CEucjpPackedConverterImpl::ConvertToUnicode(
sl@0
   528
		CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters, 
sl@0
   529
		TDes16& aUnicode, 
sl@0
   530
		const TDesC8& aForeign, 
sl@0
   531
		TInt& /*aState*/, 
sl@0
   532
		TInt& aNumberOfUnconvertibleCharacters, 
sl@0
   533
		TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter)
sl@0
   534
	{
sl@0
   535
	TFixedArray<CnvUtilities::SMethod, 4> methods;
sl@0
   536
	methods[0].iNumberOfBytesAbleToConvert=NumberOfBytesAbleToConvertToJisRoman;
sl@0
   537
	methods[0].iConvertToIntermediateBufferInPlace=DummyConvertToIntermediateBufferInPlace;
sl@0
   538
	methods[0].iConversionData=&CnvJisRoman::ConversionData();
sl@0
   539
	methods[0].iNumberOfBytesPerCharacter=1;
sl@0
   540
	methods[0].iNumberOfCoreBytesPerCharacter=1;
sl@0
   541
	methods[1].iNumberOfBytesAbleToConvert=NumberOfBytesAbleToConvertToJisX0208;
sl@0
   542
	methods[1].iConvertToIntermediateBufferInPlace=ConvertToJisX0208FromEucJpPackedInPlace;
sl@0
   543
	methods[1].iConversionData=&CnvJisX0208::ConversionData();
sl@0
   544
	methods[1].iNumberOfBytesPerCharacter=2;
sl@0
   545
	methods[1].iNumberOfCoreBytesPerCharacter=2;
sl@0
   546
	methods[2].iNumberOfBytesAbleToConvert=NumberOfBytesAbleToConvertToHalfWidthKatakana8;
sl@0
   547
	methods[2].iConvertToIntermediateBufferInPlace=ConvertToHalfWidthKatakana8FromEucJpPackedInPlace;
sl@0
   548
	methods[2].iConversionData=&CnvHalfWidthKatakana8::ConversionData();
sl@0
   549
	methods[2].iNumberOfBytesPerCharacter=2;
sl@0
   550
	methods[2].iNumberOfCoreBytesPerCharacter=1;
sl@0
   551
	methods[3].iNumberOfBytesAbleToConvert=NumberOfBytesAbleToConvertToJisX0212;
sl@0
   552
	methods[3].iConvertToIntermediateBufferInPlace=ConvertToJisX0212FromEucJpPackedInPlace;
sl@0
   553
	methods[3].iConversionData=&CnvJisX0212::ConversionData();
sl@0
   554
	methods[3].iNumberOfBytesPerCharacter=3;
sl@0
   555
	methods[3].iNumberOfCoreBytesPerCharacter=2;
sl@0
   556
	return CnvUtilities::ConvertToUnicodeFromHeterogeneousForeign(aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign, aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter, methods.Array());
sl@0
   557
	}
sl@0
   558
sl@0
   559
TBool CEucjpPackedConverterImpl::IsInThisCharacterSetL(
sl@0
   560
		TBool& aSetToTrue, 
sl@0
   561
		TInt& aConfidenceLevel, 
sl@0
   562
		const TDesC8& aSample)
sl@0
   563
	{
sl@0
   564
	aSetToTrue=ETrue;
sl@0
   565
	// check for the SS2 and SS3 which specifies Code Set 2 & 3 respectively from the Code space
sl@0
   566
	// between 00-7f only... then ambiguous 
sl@0
   567
	aSetToTrue = ETrue; 
sl@0
   568
	TInt sampleLength = aSample.Length();
sl@0
   569
	TInt eucjpPacked = 0;
sl@0
   570
	aConfidenceLevel = 0;
sl@0
   571
sl@0
   572
	for (TInt i = 0; i < sampleLength; ++i)
sl@0
   573
		{
sl@0
   574
		// Code set 1 JISX 0208 support
sl@0
   575
		TInt increment1 = i+1;
sl@0
   576
		if (increment1 >= sampleLength)
sl@0
   577
			break;
sl@0
   578
		if (((aSample[i] >= 0xa1) && (aSample[i] <= 0xfe)) && 
sl@0
   579
			((aSample[increment1] >= 0xa1) && (aSample[increment1] <= 0xfe)))
sl@0
   580
			{
sl@0
   581
			eucjpPacked = eucjpPacked +2;
sl@0
   582
			i++;
sl@0
   583
			}
sl@0
   584
		// Single Shift 2 (SS2) sequence - Code Set 2
sl@0
   585
		if (aSample[i]==0x8e)
sl@0
   586
			{
sl@0
   587
			TInt increment1 = i+1;
sl@0
   588
			if (increment1 >= sampleLength)
sl@0
   589
				break;
sl@0
   590
			if ((aSample[increment1] >= 0xa1) && (aSample[increment1] <= 0xdf))
sl@0
   591
				{
sl@0
   592
				eucjpPacked = eucjpPacked+2;
sl@0
   593
				i++;
sl@0
   594
				}
sl@0
   595
			else
sl@0
   596
				{
sl@0
   597
				eucjpPacked = 0;
sl@0
   598
				break;
sl@0
   599
				}
sl@0
   600
			}
sl@0
   601
		// Single Shift 3 (SS3) sequence - Code Set 3
sl@0
   602
		if (aSample[i]==0x8f)
sl@0
   603
			{
sl@0
   604
			TInt increment1 = i+1;
sl@0
   605
			TInt increment2 = i+2;
sl@0
   606
			if ((increment1 >= sampleLength) || (increment2 >= sampleLength))
sl@0
   607
				break;
sl@0
   608
			if (((aSample[increment1] >= 0xa1) && (aSample[increment1] <= 0xfe)) && 
sl@0
   609
				((aSample[increment2] >= 0xa1) && (aSample[increment2] <= 0xfe)))
sl@0
   610
				{
sl@0
   611
				eucjpPacked = eucjpPacked +3;
sl@0
   612
				i+=2;
sl@0
   613
				}
sl@0
   614
			else
sl@0
   615
				{
sl@0
   616
				eucjpPacked =0;
sl@0
   617
				break;
sl@0
   618
				}
sl@0
   619
			}
sl@0
   620
		}
sl@0
   621
	if (eucjpPacked)
sl@0
   622
		aConfidenceLevel = (eucjpPacked*100)/sampleLength;
sl@0
   623
	else
sl@0
   624
		aConfidenceLevel = 0;
sl@0
   625
	aConfidenceLevel=(aConfidenceLevel >100)?100:aConfidenceLevel; 
sl@0
   626
	return ETrue;
sl@0
   627
	}
sl@0
   628
sl@0
   629
CEucjpPackedConverterImpl* CEucjpPackedConverterImpl::NewL()
sl@0
   630
	{
sl@0
   631
	CEucjpPackedConverterImpl* self = new(ELeave) CEucjpPackedConverterImpl();
sl@0
   632
	return self;
sl@0
   633
	}
sl@0
   634
sl@0
   635
CEucjpPackedConverterImpl::~CEucjpPackedConverterImpl()
sl@0
   636
	{
sl@0
   637
	}
sl@0
   638
sl@0
   639
CEucjpPackedConverterImpl::CEucjpPackedConverterImpl()
sl@0
   640
	{
sl@0
   641
	}
sl@0
   642
sl@0
   643
const TImplementationProxy ImplementationTable[] = 
sl@0
   644
	{
sl@0
   645
		IMPLEMENTATION_PROXY_ENTRY(0x10006067,CEucjpPackedConverterImpl::NewL)
sl@0
   646
	};
sl@0
   647
sl@0
   648
EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
sl@0
   649
	{
sl@0
   650
	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
sl@0
   651
sl@0
   652
	return ImplementationTable;
sl@0
   653
	}
sl@0
   654