os/security/cryptoservices/certificateandkeymgmt/asn1/utf8strdec.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/asn1/utf8strdec.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,221 @@
     1.4 +/*
     1.5 +* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +* UTF8STR.CPP
    1.19 +* This file contains the implementation of the UTF8 String ASN1 class.
    1.20 +* The IA5 string can contain the following characters:
    1.21 +* \<character set unknown pending arrival of ITU spec\>
    1.22 +*
    1.23 +*/
    1.24 +
    1.25 +
    1.26 +/**
    1.27 + @file
    1.28 +*/
    1.29 +
    1.30 +#include <asn1dec.h>
    1.31 +
    1.32 +TInt ConvertToUnicodeFromUtf8(TDes16& aUnicode, const TDesC8& aUtf8);
    1.33 +
    1.34 +EXPORT_C TASN1DecUTF8String::TASN1DecUTF8String(void)
    1.35 +	{
    1.36 +	}
    1.37 +
    1.38 +EXPORT_C HBufC* TASN1DecUTF8String::DecodeDERL(const TASN1DecGeneric& aSource)
    1.39 +	{
    1.40 +	return DecodeContentsL(aSource.GetContentDER());
    1.41 +	}
    1.42 +
    1.43 +EXPORT_C HBufC* TASN1DecUTF8String::DecodeDERL(const TDesC8& aSource,TInt& aPos)
    1.44 +
    1.45 +	{
    1.46 +	TPtrC8 Source=aSource.Mid(aPos);
    1.47 +	TASN1DecGeneric gen(Source);
    1.48 +	gen.InitL();
    1.49 +	HBufC* res = DecodeContentsL(gen.GetContentDER());
    1.50 +	aPos+=gen.LengthDER();
    1.51 +	return res;
    1.52 +	}
    1.53 +
    1.54 +HBufC* TASN1DecUTF8String::DecodeContentsL(const TDesC8& aSource)
    1.55 +	{
    1.56 +	HBufC* res = HBufC::NewLC(aSource.Length());
    1.57 +	TPtr pRes = res->Des();
    1.58 +	User::LeaveIfError(ConvertToUnicodeFromUtf8(pRes, aSource));
    1.59 +	CleanupStack::Pop(res);
    1.60 +	return res;
    1.61 +	}
    1.62 +
    1.63 +/**
    1.64 + * Converts text encoded using the Unicode transformation format UTF-8
    1.65 + * into the Unicode UCS-2 character set.
    1.66 + *
    1.67 + * @param aUnicode	On return, contains the Unicode encoded output string.
    1.68 + * @param aUtf8		The UTF-8 encoded input string
    1.69 + * @return			The number of unconverted bytes left at the end of the
    1.70 + *					input descriptor, or one of the error values defined
    1.71 + *					in <code>TError</code>.
    1.72 + */
    1.73 +TInt ConvertToUnicodeFromUtf8(TDes16& aUnicode, const TDesC8& aUtf8)
    1.74 +	{
    1.75 +	if (aUtf8.Length()==0)
    1.76 +		{
    1.77 +		aUnicode.SetLength(0);
    1.78 +		return 0;
    1.79 +		}
    1.80 +	if (aUnicode.MaxLength()==0)
    1.81 +		{
    1.82 +		return aUtf8.Length();
    1.83 +		}
    1.84 +	TUint16* pointerToCurrentUnicodeCharacter=CONST_CAST(TUint16*, aUnicode.Ptr());
    1.85 +	const TUint16* pointerToLastUnicodeCharacter=pointerToCurrentUnicodeCharacter+(aUnicode.MaxLength()-1);
    1.86 +	const TUint8* pointerToCurrentUtf8Byte=aUtf8.Ptr();
    1.87 +	const TUint8* pointerToLastUtf8Byte=pointerToCurrentUtf8Byte+(aUtf8.Length()-1);
    1.88 +	TBool inputIsTruncated=EFalse;
    1.89 +	TUint16 replacementcharacter = 0xFFFD;
    1.90 +	FOREVER
    1.91 +		{
    1.92 +		//__ASSERT_DEBUG(pointerToCurrentUnicodeCharacter<=pointerToLastUnicodeCharacter, Panic(EPanicBadUnicodePointers8));
    1.93 +		//__ASSERT_DEBUG(pointerToCurrentUtf8Byte<=pointerToLastUtf8Byte, Panic(EPanicBadUtf8Pointers3));
    1.94 +		TUint currentUtf8Byte=*pointerToCurrentUtf8Byte;
    1.95 +		if ((currentUtf8Byte&0x80)==0x00)
    1.96 +			{
    1.97 +			*pointerToCurrentUnicodeCharacter=STATIC_CAST(TUint16, currentUtf8Byte);
    1.98 +			}
    1.99 +		else if ((currentUtf8Byte&0xe0)==0xc0)
   1.100 +			{
   1.101 +			//__ASSERT_DEBUG(pointerToCurrentUtf8Byte<=pointerToLastUtf8Byte, Panic(EPanicBadUtf8Pointers4));
   1.102 +			if (pointerToCurrentUtf8Byte>=pointerToLastUtf8Byte)
   1.103 +				{
   1.104 +				--pointerToCurrentUnicodeCharacter;
   1.105 +				--pointerToCurrentUtf8Byte;
   1.106 +				inputIsTruncated=ETrue;
   1.107 +				break;
   1.108 +				}
   1.109 +			TUint currentUnicodeCharacter=((currentUtf8Byte&0x1f)<<6);
   1.110 +			++pointerToCurrentUtf8Byte;
   1.111 +			currentUtf8Byte=*pointerToCurrentUtf8Byte;
   1.112 +			if ((currentUtf8Byte&0xc0)==0x80)
   1.113 +				{
   1.114 +				currentUnicodeCharacter|=(currentUtf8Byte&0x3f);
   1.115 +				*pointerToCurrentUnicodeCharacter=STATIC_CAST(TUint16, currentUnicodeCharacter);
   1.116 +				}
   1.117 +			else
   1.118 +				{
   1.119 +				*pointerToCurrentUnicodeCharacter=replacementcharacter;
   1.120 +				}
   1.121 +			}
   1.122 +		else if ((currentUtf8Byte&0xf0)==0xe0)
   1.123 +			{
   1.124 +			//__ASSERT_DEBUG(pointerToCurrentUtf8Byte<=pointerToLastUtf8Byte, Panic(EPanicBadUtf8Pointers5));
   1.125 +			if (pointerToLastUtf8Byte-pointerToCurrentUtf8Byte<2)
   1.126 +				{
   1.127 +				--pointerToCurrentUnicodeCharacter;
   1.128 +				--pointerToCurrentUtf8Byte;
   1.129 +				inputIsTruncated=ETrue;
   1.130 +				break;
   1.131 +				}
   1.132 +			TUint currentUnicodeCharacter=((currentUtf8Byte&0x0f)<<12);
   1.133 +			++pointerToCurrentUtf8Byte;
   1.134 +			currentUtf8Byte=*pointerToCurrentUtf8Byte;
   1.135 +			if ((currentUtf8Byte&0xc0)==0x80)
   1.136 +				{
   1.137 +				currentUnicodeCharacter|=((currentUtf8Byte&0x3f)<<6);
   1.138 +				++pointerToCurrentUtf8Byte;
   1.139 +				currentUtf8Byte=*pointerToCurrentUtf8Byte;
   1.140 +				if ((currentUtf8Byte&0xc0)==0x80)
   1.141 +					{
   1.142 +					currentUnicodeCharacter|=(currentUtf8Byte&0x3f);
   1.143 +					*pointerToCurrentUnicodeCharacter=STATIC_CAST(TUint16, currentUnicodeCharacter);
   1.144 +					}
   1.145 +				else
   1.146 +					{
   1.147 +					*pointerToCurrentUnicodeCharacter=STATIC_CAST(TUint16, currentUnicodeCharacter);
   1.148 +					}
   1.149 +				}
   1.150 +			else
   1.151 +				{
   1.152 +				*pointerToCurrentUnicodeCharacter=replacementcharacter;
   1.153 +				}
   1.154 +			
   1.155 +			}
   1.156 +		else if ((currentUtf8Byte&0xf8)==0xf0)
   1.157 +			{
   1.158 +			//__ASSERT_DEBUG(pointerToCurrentUnicodeCharacter<=pointerToLastUnicodeCharacter, Panic(EPanicBadUnicodePointers9));
   1.159 +			if (pointerToCurrentUnicodeCharacter>=pointerToLastUnicodeCharacter)
   1.160 +				{
   1.161 +				--pointerToCurrentUnicodeCharacter;
   1.162 +				--pointerToCurrentUtf8Byte;
   1.163 +				break;
   1.164 +				}
   1.165 +			//__ASSERT_DEBUG(pointerToCurrentUtf8Byte<=pointerToLastUtf8Byte, Panic(EPanicBadUtf8Pointers6));
   1.166 +			if (pointerToLastUtf8Byte-pointerToCurrentUtf8Byte<3)
   1.167 +				{
   1.168 +				--pointerToCurrentUnicodeCharacter;
   1.169 +				--pointerToCurrentUtf8Byte;
   1.170 +				inputIsTruncated=ETrue;
   1.171 +				break;
   1.172 +				}
   1.173 +			TUint currentUnicodeCharacter=((currentUtf8Byte&0x07)<<8);
   1.174 +			++pointerToCurrentUtf8Byte;
   1.175 +			currentUtf8Byte=*pointerToCurrentUtf8Byte;
   1.176 +			if ((currentUtf8Byte&0xc0)==0x80)
   1.177 +				{
   1.178 +				currentUnicodeCharacter|=((currentUtf8Byte&0x3f)<<2);
   1.179 +				if (currentUnicodeCharacter>=0x0040)
   1.180 +					{
   1.181 +					currentUnicodeCharacter-=0x0040;
   1.182 +					if (currentUnicodeCharacter<0x0400)
   1.183 +						{
   1.184 +						++pointerToCurrentUtf8Byte;
   1.185 +						currentUtf8Byte=*pointerToCurrentUtf8Byte;
   1.186 +						if ((currentUtf8Byte&0xc0)==0x80)
   1.187 +							{
   1.188 +							currentUnicodeCharacter|=((currentUtf8Byte&0x30)>>4);
   1.189 +							*pointerToCurrentUnicodeCharacter=STATIC_CAST(TUint16, 0xd800|currentUnicodeCharacter);
   1.190 +							currentUnicodeCharacter=((currentUtf8Byte&0x0f)<<6);
   1.191 +							++pointerToCurrentUtf8Byte;
   1.192 +							currentUtf8Byte=*pointerToCurrentUtf8Byte;
   1.193 +							if ((currentUtf8Byte&0xc0)==0x80)
   1.194 +								{
   1.195 +								currentUnicodeCharacter|=(currentUtf8Byte&0x3f);
   1.196 +								++pointerToCurrentUnicodeCharacter;
   1.197 +								*pointerToCurrentUnicodeCharacter=STATIC_CAST(TUint16, 0xdc00|currentUnicodeCharacter);
   1.198 +								}
   1.199 +							}
   1.200 +						}
   1.201 +					}
   1.202 +				}
   1.203 +			}
   1.204 +		else
   1.205 +			{
   1.206 +			*pointerToCurrentUnicodeCharacter=replacementcharacter;
   1.207 +			}
   1.208 +		//__ASSERT_DEBUG(pointerToCurrentUtf8Byte<=pointerToLastUtf8Byte, Panic(EPanicBadUtf8Pointers7));
   1.209 +		//__ASSERT_DEBUG(pointerToCurrentUnicodeCharacter<=pointerToLastUnicodeCharacter, Panic(EPanicBadUnicodePointers10));
   1.210 +		if ((pointerToCurrentUtf8Byte>=pointerToLastUtf8Byte) || (pointerToCurrentUnicodeCharacter>=pointerToLastUnicodeCharacter))
   1.211 +			{
   1.212 +			break;
   1.213 +			}
   1.214 +		++pointerToCurrentUnicodeCharacter;
   1.215 +		++pointerToCurrentUtf8Byte;
   1.216 +		}
   1.217 +	if ((pointerToCurrentUtf8Byte<aUtf8.Ptr()) && inputIsTruncated)
   1.218 +		{
   1.219 +		return KErrArgument;
   1.220 +		}
   1.221 +	aUnicode.SetLength((pointerToCurrentUnicodeCharacter-aUnicode.Ptr())+1);
   1.222 +	return pointerToLastUtf8Byte-pointerToCurrentUtf8Byte;
   1.223 +	}
   1.224 +