syncmlfw/common/syncagent/src/nsmlunicodeconverter.cpp
changeset 0 b497e44ab2fc
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002 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:  UNICODE conversion
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <utf.h>
       
    20 #include <nsmlunicodeconverter.h>
       
    21 #include "nsmlcliagconstants.h"
       
    22 // ---------------------------------------------------------
       
    23 // NSmlUnicodeConverter::HBufC8InUTF8LC()
       
    24 // Converts Unicode data to UTF-8
       
    25 // No size limitations, this function re-allocates memory
       
    26 // when needed. Note that a pointer given as a parameter is updated.
       
    27 // ---------------------------------------------------------
       
    28 EXPORT_C TInt NSmlUnicodeConverter::HBufC8InUTF8LC( const TDesC& aUnicodeData, HBufC8*& aUTF8Data )
       
    29 	{
       
    30 	TPtr8 outputBuffer = HBufC8::NewLC( KNSmlConvBufferSize )->Des(); 
       
    31 	TPtrC16 remainderOfUnicodeText( aUnicodeData );
       
    32 	TInt convertedDataLength( 0 );
       
    33 	HBufC8* convertedData = HBufC8::NewL( aUnicodeData.Length() );
       
    34 	HBufC8* oldPointer = NULL;
       
    35 	TInt returnValue( 0 );
       
    36 	TInt pushed( 0 );
       
    37 	do
       
    38 		{
       
    39 		returnValue = CnvUtfConverter::ConvertFromUnicodeToUtf8( outputBuffer, remainderOfUnicodeText );
       
    40 		if ( returnValue==CnvUtfConverter::EErrorIllFormedInput )
       
    41 			{
       
    42 			delete convertedData;
       
    43             User::Leave( KErrCorrupt );
       
    44 			}
       
    45         else if ( returnValue < 0 ) 
       
    46 			{
       
    47             delete convertedData;
       
    48 			User::Leave( KErrGeneral );
       
    49 			}
       
    50 		if ( outputBuffer.Size() > 0 )
       
    51 			{
       
    52 			convertedDataLength += outputBuffer.Length();
       
    53 			if ( convertedDataLength > convertedData->Des().MaxLength() )
       
    54 				{
       
    55 				oldPointer = convertedData;
       
    56 				convertedData = convertedData->ReAlloc( convertedDataLength );
       
    57 				if ( convertedData == NULL )
       
    58 					{
       
    59 					delete oldPointer;
       
    60 					User::Leave( KErrNoMemory );
       
    61 					}
       
    62 				}
       
    63 			convertedData->Des().Append( outputBuffer );
       
    64 			}
       
    65 		if ( returnValue > 0 )
       
    66 			{
       
    67 			remainderOfUnicodeText.Set( remainderOfUnicodeText.Right( returnValue ) );
       
    68 			}
       
    69 		}
       
    70 		while ( returnValue > 0 ); 
       
    71 	CleanupStack::PopAndDestroy(); //outputBuffer
       
    72 	aUTF8Data = convertedData;
       
    73 	CleanupStack::PushL( aUTF8Data );
       
    74 	pushed++;
       
    75 	return pushed;
       
    76 	}
       
    77 // ---------------------------------------------------------
       
    78 // NSmlUnicodeConverter::HBufC16InUnicodeL()
       
    79 // 
       
    80 // ---------------------------------------------------------
       
    81 EXPORT_C TInt NSmlUnicodeConverter::HBufC16InUnicodeL( const TDesC8& aUtf8, HBufC*& aUnicodeData )
       
    82 	{
       
    83 	TInt pushed;
       
    84 	pushed = HBufC16InUnicodeLC( aUtf8, aUnicodeData );
       
    85 	CleanupStack::Pop();
       
    86 	return pushed;
       
    87 	}
       
    88 // ---------------------------------------------------------
       
    89 // TNSmlUnicodeConverter::HBufC16InUnicodeLC()
       
    90 // Converts UTF-8 data to Unicode.
       
    91 // No size limitations, this function re-allocates memory
       
    92 // when needed. Note that a pointer given as a parameter is updated.
       
    93 // ---------------------------------------------------------
       
    94 EXPORT_C TInt NSmlUnicodeConverter::HBufC16InUnicodeLC( const TDesC8& aUtf8, HBufC*& aUnicodeData )
       
    95 	{
       
    96 	TPtr outputBuffer = HBufC::NewLC( KNSmlConvBufferSize )->Des(); 
       
    97 	TPtrC8 remainderOfUtf8 (aUtf8 );
       
    98 	TInt convertedDataLength( 0 );
       
    99 	HBufC16* convertedData = HBufC16::NewL( aUtf8.Length() / 2 );
       
   100 	HBufC16* oldPointer = NULL;
       
   101 	TInt returnValue( 0 );
       
   102 	TInt pushed( 0 );
       
   103 	do
       
   104 		{
       
   105 		returnValue = CnvUtfConverter::ConvertToUnicodeFromUtf8( outputBuffer, remainderOfUtf8 );
       
   106 		if (returnValue == CnvUtfConverter::EErrorIllFormedInput )
       
   107 			{
       
   108             delete convertedData;
       
   109 			User::Leave( KErrCorrupt );
       
   110 			}
       
   111 		else if ( returnValue < 0 ) // future-proof against "TError" expanding
       
   112 			{
       
   113             delete convertedData;
       
   114 			User::Leave( KErrGeneral );
       
   115 			}   
       
   116 		if ( outputBuffer.Size() > 0 )
       
   117 			{
       
   118 			convertedDataLength += outputBuffer.Length();
       
   119 			if ( convertedDataLength > convertedData->Des().MaxLength() )
       
   120 				{
       
   121 				oldPointer = convertedData;
       
   122 				convertedData = convertedData->ReAlloc( convertedDataLength );
       
   123 				if ( convertedData == NULL )
       
   124 					{
       
   125 					delete oldPointer;
       
   126 					User::Leave( KErrNoMemory );
       
   127 					}
       
   128 				}
       
   129 			convertedData->Des().Append( outputBuffer );
       
   130 			}
       
   131 		if ( returnValue > 0 )
       
   132 			{
       
   133 			remainderOfUtf8.Set( remainderOfUtf8.Right( returnValue ) );
       
   134 			}
       
   135 		}
       
   136 	while ( returnValue > 0 );
       
   137 	CleanupStack::PopAndDestroy(); //outputBuffer
       
   138 	aUnicodeData = convertedData;
       
   139 	CleanupStack::PushL( aUnicodeData );
       
   140 	pushed++;
       
   141 	return pushed;
       
   142 	}