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