os/textandloc/charconvfw/fatfilenameconversionplugins/src/unicodeconv.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) 2006-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 <e32def.h>
sl@0
    21
#include <e32des8.h> 
sl@0
    22
#include "unicodeconv.h"
sl@0
    23
sl@0
    24
//replacement character to be used when unicode cannot be converted
sl@0
    25
const TUint8 KForeignReplacement = 0x5F;
sl@0
    26
sl@0
    27
//This function converts from Unicoded characters, to foreign characters and adds them into a descriptor
sl@0
    28
EXPORT_C void UnicodeConv::ConvertFromUnicodeL(TDes8& aForeign, const TDesC16& aUnicode)
sl@0
    29
    {
sl@0
    30
    UnicodeConv::ConvertFromUnicodeL(aForeign, aUnicode, ETrue);
sl@0
    31
    }
sl@0
    32
sl@0
    33
//This function converts from Unicoded characters, to foreign characters and adds them into a descriptor
sl@0
    34
EXPORT_C TInt UnicodeConv::ConvertFromUnicodeL(TDes8& aForeign, const TDesC16& aUnicode, TBool leaveWhenOverflow)
sl@0
    35
	{
sl@0
    36
	const TInt unicodeLength = aUnicode.Length();
sl@0
    37
	
sl@0
    38
	//loop going through the character of the unicode descriptor
sl@0
    39
	for(TInt i=0; i<unicodeLength; i++)
sl@0
    40
		{
sl@0
    41
		const TUint16 unicodeChar = aUnicode[i];
sl@0
    42
		
sl@0
    43
		// if the output buffer is already full, leave with KErrOverflow
sl@0
    44
		if ( aForeign.Length() >= aForeign.MaxLength() )
sl@0
    45
		    {
sl@0
    46
		    if (leaveWhenOverflow)
sl@0
    47
		        User::Leave(KErrOverflow);
sl@0
    48
		    else
sl@0
    49
		        return KErrOverflow;
sl@0
    50
		    }			
sl@0
    51
sl@0
    52
		//charcters from 0x0000 to 0x007F, can be mapped directly
sl@0
    53
		if(unicodeChar<0x0080)
sl@0
    54
			{
sl@0
    55
			aForeign.Append(static_cast<TUint8>(unicodeChar));
sl@0
    56
			}
sl@0
    57
		else
sl@0
    58
			{
sl@0
    59
			TInt trailByte = KErrNotFound;
sl@0
    60
			TInt returnValue = TConvDataStruct::ConvertSingleUnicode(unicodeChar,trailByte);
sl@0
    61
			
sl@0
    62
			if(returnValue!=KErrNotFound)
sl@0
    63
				{
sl@0
    64
				if(trailByte!=KErrNotFound)		
sl@0
    65
					{					
sl@0
    66
					// as two bytes are being added check enough space for second
sl@0
    67
					if ( aForeign.Length() + 2 <= aForeign.MaxLength() )
sl@0
    68
					    {
sl@0
    69
					    aForeign.Append(static_cast<TUint8>(returnValue));
sl@0
    70
					    aForeign.Append(static_cast<TUint8>(trailByte));
sl@0
    71
					    }
sl@0
    72
					else
sl@0
    73
					    {
sl@0
    74
			            if (leaveWhenOverflow)
sl@0
    75
			                User::Leave(KErrOverflow);
sl@0
    76
			            else
sl@0
    77
			                return KErrOverflow;
sl@0
    78
					    }					
sl@0
    79
					}
sl@0
    80
				else
sl@0
    81
					aForeign.Append(static_cast<TUint8>(returnValue));
sl@0
    82
				}		
sl@0
    83
			else
sl@0
    84
				aForeign.Append(KForeignReplacement);
sl@0
    85
			}
sl@0
    86
		}
sl@0
    87
	
sl@0
    88
	return KErrNone;
sl@0
    89
	}
sl@0
    90
				
sl@0
    91
//This function converts from foreign characters into unicode and adds them into a descriptor
sl@0
    92
EXPORT_C void UnicodeConv::ConvertToUnicodeL(TDes16& aUnicode, const TDesC8& aForeign)
sl@0
    93
    {
sl@0
    94
    UnicodeConv::ConvertToUnicodeL(aUnicode, aForeign, ETrue);
sl@0
    95
    }
sl@0
    96
sl@0
    97
//This function converts from foreign characters into unicode and adds them into a descriptor
sl@0
    98
EXPORT_C TInt UnicodeConv::ConvertToUnicodeL(TDes16& aUnicode, const TDesC8& aForeign, TBool leaveWhenOverflow)
sl@0
    99
	{
sl@0
   100
	const TInt foreignLength = aForeign.Length();
sl@0
   101
sl@0
   102
	//loop going through the characters of the foreign descriptor
sl@0
   103
	for(TInt i = 0; i<foreignLength; i++)
sl@0
   104
		{
sl@0
   105
		const TUint8 leadForeign = aForeign[i];
sl@0
   106
		TUint8 tailForeign = 0x00;
sl@0
   107
sl@0
   108
		// Check there is enough space in the output buffer, and leave with KErrOverflow if not
sl@0
   109
		if ( aUnicode.Length() == aUnicode.MaxLength() )
sl@0
   110
            {
sl@0
   111
            if (leaveWhenOverflow)
sl@0
   112
                User::Leave(KErrOverflow);
sl@0
   113
            else
sl@0
   114
                return KErrOverflow;
sl@0
   115
            }
sl@0
   116
sl@0
   117
		//charcters from 0x00 to 0x7F, can be mapped directly
sl@0
   118
		if(leadForeign < 0x80)
sl@0
   119
			aUnicode.Append(static_cast<TUint16>(leadForeign));
sl@0
   120
		else
sl@0
   121
			{
sl@0
   122
			if((i+1)<foreignLength)
sl@0
   123
				tailForeign = aForeign[i+1];
sl@0
   124
sl@0
   125
			const TLeadOrSingle* structPtr = TConvDataStruct::KFirstByteConversions + (leadForeign-0x80);
sl@0
   126
			
sl@0
   127
			if(structPtr->iUnicodeIfSingle)
sl@0
   128
				aUnicode.Append(structPtr->iUnicodeIfSingle);
sl@0
   129
			else
sl@0
   130
				{
sl@0
   131
				if(TConvDataStruct::KMinTrailByte<=tailForeign && tailForeign<=TConvDataStruct::KMaxTrailByte)
sl@0
   132
					aUnicode.Append(TConvDataStruct::KDoubleByteConversions[structPtr->iDoubleByteIndex+
sl@0
   133
						(tailForeign - TConvDataStruct::KMinTrailByte)]);
sl@0
   134
				else
sl@0
   135
					aUnicode.Append(0xFFFD);
sl@0
   136
				i++;
sl@0
   137
				}
sl@0
   138
			}
sl@0
   139
		}
sl@0
   140
sl@0
   141
	return KErrNone;
sl@0
   142
	}
sl@0
   143
sl@0
   144
EXPORT_C TBool UnicodeConv::IsLegalShortNameCharacter (TUint aCharacter)
sl@0
   145
	{
sl@0
   146
	//1. aCharacter >= 0x0080 
sl@0
   147
	if (aCharacter>=0x0080)
sl@0
   148
		{
sl@0
   149
		if (aCharacter>0xFFFF)
sl@0
   150
			return EFalse;
sl@0
   151
		
sl@0
   152
		TInt trailByte = KErrNotFound;
sl@0
   153
		TInt returnValue = TConvDataStruct::ConvertSingleUnicode(aCharacter,trailByte);
sl@0
   154
		
sl@0
   155
		if(returnValue!=KErrNotFound)
sl@0
   156
			return ETrue;
sl@0
   157
		else
sl@0
   158
			return EFalse;
sl@0
   159
		}
sl@0
   160
	
sl@0
   161
    // For most common cases: 
sl@0
   162
    // Note: lower case characters are considered legal DOS char here. 
sl@0
   163
	if ((aCharacter>='a' && aCharacter<='z') || 
sl@0
   164
	    (aCharacter>='A' && aCharacter<='Z') || 
sl@0
   165
	    (aCharacter>='0' && aCharacter<='9'))
sl@0
   166
			{
sl@0
   167
			return ETrue;
sl@0
   168
			}
sl@0
   169
    // Checking for illegal chars: 
sl@0
   170
    // 2. aCharacter <= 0x20 
sl@0
   171
    // Note: leading 0x05 byte should be guarded by callers of this function 
sl@0
   172
    //  as the information of the position of the character is required. 
sl@0
   173
	if (aCharacter < 0x20)
sl@0
   174
		return EFalse;
sl@0
   175
	// Space (' ') is not considered as a legal DOS char here.
sl@0
   176
	if (aCharacter == 0x20)
sl@0
   177
		return EFalse;
sl@0
   178
	
sl@0
   179
	// 3. 0x20 < aCharacter < 0x80 
sl@0
   180
    // According to FAT Spec, "following characters are not legal in any bytes of DIR_Name": 
sl@0
   181
    switch (aCharacter) 
sl@0
   182
            { 
sl@0
   183
            case 0x22:        // '"' 
sl@0
   184
            case 0x2A:        // '*' 
sl@0
   185
            case 0x2B:        // '+' 
sl@0
   186
            case 0x2C:        // ',' 
sl@0
   187
            //case 0x2E:        // '.'   // Although '.' is not allowed in any bytes of DIR_Name, it 
sl@0
   188
                                         // is a valid character in short file names. 
sl@0
   189
            case 0x2F:        // '/' 
sl@0
   190
            case 0x3A:        // ':' 
sl@0
   191
            case 0x3B:        // ';' 
sl@0
   192
            case 0x3C:        // '<' 
sl@0
   193
            case 0x3D:        // '=' 
sl@0
   194
            case 0x3E:        // '>' 
sl@0
   195
            case 0x3F:        // '?' 
sl@0
   196
            case 0x5B:        // '[' 
sl@0
   197
            case 0x5C:        // '\' 
sl@0
   198
            case 0x5D:        // ']' 
sl@0
   199
            case 0x7C:        // '|' 
sl@0
   200
            	return EFalse; 
sl@0
   201
            default: 
sl@0
   202
            	return ETrue; 
sl@0
   203
            } 
sl@0
   204
	}		
sl@0
   205