os/textandloc/charconvfw/charconvplugins/src/plugins/j5eucjp.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) 2005-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
* J5 charconv character converter
sl@0
    16
* converts from EUC_JP to unicode
sl@0
    17
* based on code in EUCJP_PACKED
sl@0
    18
*
sl@0
    19
*/
sl@0
    20
sl@0
    21
sl@0
    22
#include <e32std.h>
sl@0
    23
#include <charconv.h>
sl@0
    24
#include <ecom/implementationproxy.h>
sl@0
    25
#include <convutils.h>
sl@0
    26
#include "shiftjis.h"
sl@0
    27
#include "jisbase.h"
sl@0
    28
#include "jisx0201.h"
sl@0
    29
#include "jisx0208.h"
sl@0
    30
#include "jisx0212.h"
sl@0
    31
sl@0
    32
const TUint KSingleShift2=0x8e;
sl@0
    33
const TUint KSingleShift3=0x8f;
sl@0
    34
sl@0
    35
		
sl@0
    36
#if defined(_DEBUG)
sl@0
    37
sl@0
    38
_LIT(KLitPanicText, "EUCJP_PACKED");
sl@0
    39
sl@0
    40
enum TPanic
sl@0
    41
	{
sl@0
    42
	EPanicNothingToConvert1=1,
sl@0
    43
	EPanicNothingToConvert2,
sl@0
    44
	EPanicNothingToConvert3,
sl@0
    45
	EPanicNothingToConvert4,
sl@0
    46
	EPanicNothingToConvert5,
sl@0
    47
	EPanicNothingToConvert6,
sl@0
    48
	EPanicOddNumberOfBytes1,
sl@0
    49
	EPanicOddNumberOfBytes2,
sl@0
    50
	EPanicOddNumberOfBytes3,
sl@0
    51
	EPanicOddNumberOfBytes4,
sl@0
    52
	EPanicOddNumberOfBytes5,
sl@0
    53
	EPanicOddNumberOfBytes6,
sl@0
    54
	EPanicBadHighBit1,
sl@0
    55
	EPanicBadHighBit2,
sl@0
    56
	EPanicBadHighBit3,
sl@0
    57
	EPanicBadHighBit4,
sl@0
    58
	EPanicBadHighBit5,
sl@0
    59
	EPanicBadHighBit6,
sl@0
    60
	EPanicBadHighBit7,
sl@0
    61
	EPanicBadPointers1,
sl@0
    62
	EPanicBadPointers2,
sl@0
    63
	EPanicBadPointers3,
sl@0
    64
	EPanicBadPointers4,
sl@0
    65
	EPanicBadPointers5,
sl@0
    66
	EPanicBadPointers6,
sl@0
    67
	EPanicBadPointers7,
sl@0
    68
	EPanicBadPointers8,
sl@0
    69
	EPanicBadPointers9,
sl@0
    70
	EPanicBadPointers10,
sl@0
    71
	EPanicBadPointers11,
sl@0
    72
	EPanicBadPointers12,
sl@0
    73
	EPanicBadPointers13,
sl@0
    74
	EPanicBadPointers14,
sl@0
    75
	EPanicBadPointers15,
sl@0
    76
	EPanicBadPointers16,
sl@0
    77
	EPanicBadPointers17,
sl@0
    78
	EPanicBadPointers18,
sl@0
    79
	EPanicBadPointers19,
sl@0
    80
	EPanicBadPointers20,
sl@0
    81
	EPanicBadPointers21,
sl@0
    82
	EPanicBadPointers22,
sl@0
    83
	EPanicBadPointers23,
sl@0
    84
	EPanicBadPointers24,
sl@0
    85
	EPanicBadPointers25,
sl@0
    86
	EPanicBadPointers26,
sl@0
    87
	EPanicBadPointers27,
sl@0
    88
	EPanicBadPointers28,
sl@0
    89
	EPanicBadPointers29,
sl@0
    90
	EPanicBadPointers30,
sl@0
    91
	EPanicBadPointers31,
sl@0
    92
	EPanicBadPointers32,
sl@0
    93
	EPanicBadPointers33,
sl@0
    94
	EPanicBadPointers34,
sl@0
    95
	EPanicBadPointers35,
sl@0
    96
	EPanicBadPointers36,
sl@0
    97
	EPanicBadCalculation1,
sl@0
    98
	EPanicBadCalculation2,
sl@0
    99
	EPanicNumberOfBytesIsNotMultipleOfThree1,
sl@0
   100
	EPanicNumberOfBytesIsNotMultipleOfThree2,
sl@0
   101
	EPanicSingleShift2Expected,
sl@0
   102
	EPanicSingleShift3Expected
sl@0
   103
	};
sl@0
   104
sl@0
   105
LOCAL_C void Panic(TPanic aPanic)
sl@0
   106
	{
sl@0
   107
	User::Panic(KLitPanicText, aPanic);
sl@0
   108
	}
sl@0
   109
sl@0
   110
#endif
sl@0
   111
	
sl@0
   112
/**
sl@0
   113
 @return The number of bytes that can be converted from aDescriptor to JisRoman
sl@0
   114
 @internalTechnology 
sl@0
   115
 */
sl@0
   116
TInt NumberOfBytesAbleToConvertToJisRoman(const TDesC8& aDescriptor)
sl@0
   117
	{
sl@0
   118
	const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1;
sl@0
   119
	const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length();
sl@0
   120
	if (pointerToPreviousByte==pointerToLastByte)
sl@0
   121
		{
sl@0
   122
		return 0;
sl@0
   123
		}
sl@0
   124
		
sl@0
   125
	FOREVER
sl@0
   126
		{
sl@0
   127
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers14));
sl@0
   128
		const TUint currentByte=*(pointerToPreviousByte+1);
sl@0
   129
		if (currentByte&0x80)
sl@0
   130
			{
sl@0
   131
			break;
sl@0
   132
			}
sl@0
   133
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers15));
sl@0
   134
		++pointerToPreviousByte;
sl@0
   135
		__ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers16));
sl@0
   136
		if (pointerToPreviousByte>=pointerToLastByte)
sl@0
   137
			{
sl@0
   138
			break;
sl@0
   139
			}
sl@0
   140
		}
sl@0
   141
	return (pointerToPreviousByte+1)-aDescriptor.Ptr();
sl@0
   142
	}
sl@0
   143
sl@0
   144
/**
sl@0
   145
 @return The number of bytes that can be converted from aDescriptor to Jis X208
sl@0
   146
 @internalTechnology 
sl@0
   147
 */
sl@0
   148
 TInt NumberOfBytesAbleToConvertToJisX0208(const TDesC8& aDescriptor)
sl@0
   149
	{
sl@0
   150
	const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1;
sl@0
   151
	const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length();
sl@0
   152
	if (pointerToPreviousByte==pointerToLastByte)
sl@0
   153
		{
sl@0
   154
		return 0;
sl@0
   155
		}
sl@0
   156
	FOREVER
sl@0
   157
		{
sl@0
   158
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers17));
sl@0
   159
		TUint currentByte=*(pointerToPreviousByte+1);
sl@0
   160
		if (currentByte<0xa0)
sl@0
   161
			{
sl@0
   162
			break;
sl@0
   163
			}
sl@0
   164
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers18));
sl@0
   165
		if (pointerToLastByte-pointerToPreviousByte<2)
sl@0
   166
			{
sl@0
   167
			break;
sl@0
   168
			}
sl@0
   169
		++pointerToPreviousByte;
sl@0
   170
		currentByte=*(pointerToPreviousByte+1);
sl@0
   171
		if (currentByte<0xa0)
sl@0
   172
			{
sl@0
   173
			return CCnvCharacterSetConverter::EErrorIllFormedInput;
sl@0
   174
			}
sl@0
   175
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers19));
sl@0
   176
		++pointerToPreviousByte;
sl@0
   177
		__ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers20));
sl@0
   178
		if (pointerToPreviousByte>=pointerToLastByte)
sl@0
   179
			{
sl@0
   180
			break;
sl@0
   181
			}
sl@0
   182
		}
sl@0
   183
	return (pointerToPreviousByte+1)-aDescriptor.Ptr();
sl@0
   184
	}
sl@0
   185
	
sl@0
   186
/**
sl@0
   187
 @return The number of bytes that can be converted from aDescriptor to HalfWidthKatakana
sl@0
   188
 @internalTechnology 
sl@0
   189
 */
sl@0
   190
 TInt NumberOfBytesAbleToConvertToHalfWidthKatakana8(const TDesC8& aDescriptor)
sl@0
   191
	{
sl@0
   192
	const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1;
sl@0
   193
	const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length();
sl@0
   194
	if (pointerToPreviousByte==pointerToLastByte)
sl@0
   195
		{
sl@0
   196
		return 0;
sl@0
   197
		}
sl@0
   198
	FOREVER
sl@0
   199
		{
sl@0
   200
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers21));
sl@0
   201
		TUint currentByte=*(pointerToPreviousByte+1);
sl@0
   202
		if (currentByte!=KSingleShift2)
sl@0
   203
			{
sl@0
   204
			break;
sl@0
   205
			}
sl@0
   206
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers22));
sl@0
   207
		if (pointerToLastByte-pointerToPreviousByte<2)
sl@0
   208
			{
sl@0
   209
			break;
sl@0
   210
			}
sl@0
   211
		++pointerToPreviousByte;
sl@0
   212
		currentByte=*(pointerToPreviousByte+1);
sl@0
   213
		if (currentByte<0xa0)
sl@0
   214
			{
sl@0
   215
			return CCnvCharacterSetConverter::EErrorIllFormedInput;
sl@0
   216
			}
sl@0
   217
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers23));
sl@0
   218
		++pointerToPreviousByte;
sl@0
   219
		__ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers24));
sl@0
   220
		if (pointerToPreviousByte>=pointerToLastByte)
sl@0
   221
			{
sl@0
   222
			break;
sl@0
   223
			}
sl@0
   224
		}
sl@0
   225
	return (pointerToPreviousByte+1)-aDescriptor.Ptr();
sl@0
   226
	}
sl@0
   227
sl@0
   228
/**
sl@0
   229
 @return The number of bytes that can be converted from aDescriptor to JISX0212
sl@0
   230
 @internalTechnology 
sl@0
   231
 */
sl@0
   232
TInt NumberOfBytesAbleToConvertToJisX0212(const TDesC8& aDescriptor)
sl@0
   233
	{
sl@0
   234
	const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1;
sl@0
   235
	const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length();
sl@0
   236
	if (pointerToPreviousByte==pointerToLastByte)
sl@0
   237
		{
sl@0
   238
		return 0;
sl@0
   239
		}
sl@0
   240
		
sl@0
   241
	FOREVER
sl@0
   242
		{
sl@0
   243
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers25));
sl@0
   244
		TUint currentByte=*(pointerToPreviousByte+1);
sl@0
   245
		if (currentByte!=KSingleShift3)
sl@0
   246
			{
sl@0
   247
			break;
sl@0
   248
			}
sl@0
   249
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers26));
sl@0
   250
		if (pointerToLastByte-pointerToPreviousByte<3)
sl@0
   251
			{
sl@0
   252
			break;
sl@0
   253
			}
sl@0
   254
		++pointerToPreviousByte;
sl@0
   255
		currentByte=*(pointerToPreviousByte+1);
sl@0
   256
		if (currentByte<0xa0)
sl@0
   257
			{
sl@0
   258
			return CCnvCharacterSetConverter::EErrorIllFormedInput;
sl@0
   259
			}
sl@0
   260
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers27));
sl@0
   261
		++pointerToPreviousByte;
sl@0
   262
		currentByte=*(pointerToPreviousByte+1);
sl@0
   263
		if (currentByte<0xa0)
sl@0
   264
			{
sl@0
   265
			return CCnvCharacterSetConverter::EErrorIllFormedInput;
sl@0
   266
			}
sl@0
   267
		__ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers28));
sl@0
   268
		++pointerToPreviousByte;
sl@0
   269
		__ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers29));
sl@0
   270
		if (pointerToPreviousByte>=pointerToLastByte)
sl@0
   271
			{
sl@0
   272
			break;
sl@0
   273
			}
sl@0
   274
		}
sl@0
   275
	return (pointerToPreviousByte+1)-aDescriptor.Ptr();
sl@0
   276
	}
sl@0
   277
sl@0
   278
void DummyConvertToIntermediateBufferInPlace(TDes8&)
sl@0
   279
	{
sl@0
   280
	}
sl@0
   281
	
sl@0
   282
/**
sl@0
   283
 Converts to JISX0212
sl@0
   284
 @internalTechnology 
sl@0
   285
 */
sl@0
   286
void ConvertToJisX0212FromEucJpPackedInPlace(TDes8& aDescriptor)
sl@0
   287
	{
sl@0
   288
	const TInt descriptorLength=aDescriptor.Length();
sl@0
   289
	__ASSERT_DEBUG(descriptorLength>0, Panic(EPanicNothingToConvert6));
sl@0
   290
	__ASSERT_DEBUG(descriptorLength%3==0, Panic(EPanicNumberOfBytesIsNotMultipleOfThree2));
sl@0
   291
	TUint8* pointerToTargetByte=CONST_CAST(TUint8*, aDescriptor.Ptr());
sl@0
   292
	const TUint8* pointerToSourceByte=pointerToTargetByte;
sl@0
   293
	const TUint8* const pointerToLastByte=pointerToSourceByte+(descriptorLength-1);
sl@0
   294
	FOREVER
sl@0
   295
		{
sl@0
   296
		__ASSERT_DEBUG(*pointerToSourceByte==KSingleShift3, Panic(EPanicSingleShift3Expected));
sl@0
   297
		__ASSERT_DEBUG(pointerToSourceByte<pointerToLastByte, Panic(EPanicBadPointers33));
sl@0
   298
		++pointerToSourceByte;
sl@0
   299
		TUint sourceByte=*pointerToSourceByte;
sl@0
   300
		__ASSERT_DEBUG(sourceByte&0x80, Panic(EPanicBadHighBit6));
sl@0
   301
		*pointerToTargetByte=STATIC_CAST(TUint8, sourceByte&~0x80);
sl@0
   302
		__ASSERT_DEBUG(pointerToSourceByte<pointerToLastByte, Panic(EPanicBadPointers34));
sl@0
   303
		++pointerToSourceByte;
sl@0
   304
		sourceByte=*pointerToSourceByte;
sl@0
   305
		__ASSERT_DEBUG(sourceByte&0x80, Panic(EPanicBadHighBit7));
sl@0
   306
		__ASSERT_DEBUG(pointerToTargetByte<pointerToLastByte, Panic(EPanicBadPointers35));
sl@0
   307
		++pointerToTargetByte;
sl@0
   308
		*pointerToTargetByte=STATIC_CAST(TUint8, sourceByte&~0x80);
sl@0
   309
		__ASSERT_DEBUG(pointerToSourceByte<=pointerToLastByte, Panic(EPanicBadPointers36));
sl@0
   310
		if (pointerToSourceByte>=pointerToLastByte)
sl@0
   311
			{
sl@0
   312
			break;
sl@0
   313
			}
sl@0
   314
		++pointerToSourceByte;
sl@0
   315
		++pointerToTargetByte;
sl@0
   316
		}
sl@0
   317
	aDescriptor.SetLength((descriptorLength/3)*2);
sl@0
   318
	}
sl@0
   319
	
sl@0
   320
/**
sl@0
   321
 Converts to JISX0208
sl@0
   322
 @internalTechnology 
sl@0
   323
 */
sl@0
   324
void ConvertToJisX0208FromEucJpPackedInPlace(TDes8& aDescriptor)
sl@0
   325
	{
sl@0
   326
	const TInt descriptorLength=aDescriptor.Length();
sl@0
   327
	__ASSERT_DEBUG(descriptorLength>0, Panic(EPanicNothingToConvert4));
sl@0
   328
	__ASSERT_DEBUG(descriptorLength%2==0, Panic(EPanicOddNumberOfBytes5));
sl@0
   329
	TUint8* pointerToCurrentByte=CONST_CAST(TUint8*, aDescriptor.Ptr());
sl@0
   330
	const TUint8* const pointerToLastByte=pointerToCurrentByte+(descriptorLength-1);
sl@0
   331
	FOREVER
sl@0
   332
		{
sl@0
   333
		const TUint currentByte=*pointerToCurrentByte;
sl@0
   334
		__ASSERT_DEBUG(currentByte&0x80, Panic(EPanicBadHighBit4));
sl@0
   335
		*pointerToCurrentByte=STATIC_CAST(TUint8, currentByte&~0x80);
sl@0
   336
		__ASSERT_DEBUG(pointerToCurrentByte<=pointerToLastByte, Panic(EPanicBadPointers30));
sl@0
   337
		if (pointerToCurrentByte>=pointerToLastByte)
sl@0
   338
			{
sl@0
   339
			break;
sl@0
   340
			}
sl@0
   341
		++pointerToCurrentByte;
sl@0
   342
		}
sl@0
   343
	}
sl@0
   344
	
sl@0
   345
/**
sl@0
   346
 Converts to Half Width Katakana
sl@0
   347
 @internalTechnology 
sl@0
   348
 */
sl@0
   349
void ConvertToHalfWidthKatakana8FromEucJpPackedInPlace(TDes8& aDescriptor)
sl@0
   350
	{
sl@0
   351
	const TInt descriptorLength=aDescriptor.Length();
sl@0
   352
	__ASSERT_DEBUG(descriptorLength>0, Panic(EPanicNothingToConvert5));
sl@0
   353
	__ASSERT_DEBUG(descriptorLength%2==0, Panic(EPanicOddNumberOfBytes6));
sl@0
   354
	TUint8* pointerToTargetByte=CONST_CAST(TUint8*, aDescriptor.Ptr());
sl@0
   355
	const TUint8* pointerToSourceByte=pointerToTargetByte;
sl@0
   356
	const TUint8* const pointerToLastByte=pointerToSourceByte+(descriptorLength-1);
sl@0
   357
	FOREVER
sl@0
   358
		{
sl@0
   359
		__ASSERT_DEBUG(*pointerToSourceByte==KSingleShift2, Panic(EPanicSingleShift2Expected));
sl@0
   360
		__ASSERT_DEBUG(pointerToSourceByte<pointerToLastByte, Panic(EPanicBadPointers31));
sl@0
   361
		++pointerToSourceByte;
sl@0
   362
		const TUint sourceByte=*pointerToSourceByte;
sl@0
   363
		__ASSERT_DEBUG(sourceByte&0x80, Panic(EPanicBadHighBit5));
sl@0
   364
		*pointerToTargetByte=STATIC_CAST(TUint8, sourceByte);
sl@0
   365
		__ASSERT_DEBUG(pointerToSourceByte<=pointerToLastByte, Panic(EPanicBadPointers32));
sl@0
   366
		if (pointerToSourceByte>=pointerToLastByte)
sl@0
   367
			{
sl@0
   368
			break;
sl@0
   369
			}
sl@0
   370
		++pointerToSourceByte;
sl@0
   371
		++pointerToTargetByte;
sl@0
   372
		}
sl@0
   373
	aDescriptor.SetLength(descriptorLength/2);
sl@0
   374
	}
sl@0
   375
	
sl@0
   376