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