32
|
1 |
/*
|
|
2 |
* Copyright (c) 2005 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 the License "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 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
#include <e32std.h>
|
|
26 |
#include <charconv.h>
|
|
27 |
#include <ecom/implementationproxy.h>
|
|
28 |
#include <utf.h>
|
|
29 |
#include "charactersetconverter.h"
|
|
30 |
#include "convutils.h"
|
|
31 |
#include "shiftjis.h"
|
|
32 |
#include "JISBASE.H"
|
|
33 |
#include "J5.H"
|
|
34 |
|
|
35 |
#include "jisx0201.h"
|
|
36 |
#include "jisx0208.h"
|
|
37 |
#include "jisx0212.h"
|
|
38 |
|
|
39 |
/**
|
|
40 |
J5 will use up to KMaxSizeAutoDetectSample to try to deterine the format of data.
|
|
41 |
*/
|
|
42 |
const TInt KMaxSizeAutoDetectSample = 1000;
|
|
43 |
|
|
44 |
const TUint8 KEscape = 0x1b;
|
|
45 |
const TInt KByteOrderMark = 0xfeff;
|
|
46 |
|
|
47 |
const TDesC8& CJ5Converter::ReplacementForUnconvertibleUnicodeCharacters()
|
|
48 |
{
|
|
49 |
return CnvShiftJis::ReplacementForUnconvertibleUnicodeCharacters();
|
|
50 |
}
|
|
51 |
|
|
52 |
/**
|
|
53 |
This API should not be used as it is ambiguous as to what encoding is required.
|
|
54 |
The user should instead call the specific plug-in for the appropriate conversion.
|
|
55 |
J5 ConvertFromUnicode() will convert to UTF8 as default.
|
|
56 |
@internalTechnology
|
|
57 |
*/
|
|
58 |
TInt CJ5Converter::ConvertFromUnicode(
|
|
59 |
CCnvCharacterSetConverter::TEndianness /* aDefaultEndiannessOfForeignCharacters */,
|
|
60 |
const TDesC8& /* aReplacementForUnconvertibleUnicodeCharacters */,
|
|
61 |
TDes8& aForeign,
|
|
62 |
const TDesC16& aUnicode,
|
|
63 |
CCnvCharacterSetConverter::TArrayOfAscendingIndices& /* aIndicesOfUnconvertibleCharacters */)
|
|
64 |
{
|
|
65 |
return CnvUtfConverter::ConvertFromUnicodeToUtf8(aForeign, aUnicode);
|
|
66 |
}
|
|
67 |
|
|
68 |
/**
|
|
69 |
This will automatically determine one of the five supported encodings
|
|
70 |
to use and convert accordingly. This plugin method is available to the
|
|
71 |
user though the CCnvCharacterSetConverter::ConvertToUnicode() method.
|
|
72 |
There is no way for the caller to determine which encoding has been used.
|
|
73 |
|
|
74 |
NOTE: For debugging the selected character set is returned in the state.
|
|
75 |
|
|
76 |
@released 9.1
|
|
77 |
@param aDefaultEndiannessOfForeignCharacters The default endian-ness to use when reading characters
|
|
78 |
in the foreign character set.
|
|
79 |
@param aUnicode On return, contains the text converted into Unicode.
|
|
80 |
@param aForeign The non-Unicode source text to be converted.
|
|
81 |
@param aState Used to save state information across multiple calls
|
|
82 |
to <code>ConvertToUnicode()</code>.
|
|
83 |
@param aNumberOfUnconvertibleCharacters On return, contains the number of bytes which were not
|
|
84 |
converted.
|
|
85 |
@param aIndexOfFirstByteOfFirstUnconvertibleCharacter On return, contains the index of the first bytein the
|
|
86 |
input text that could not be converted. A negative
|
|
87 |
value indicates that all the characters were
|
|
88 |
converted.
|
|
89 |
@return The number of unconverted bytes left at the end of the input descriptor
|
|
90 |
(e.g. because the output descriptor is not long enough to hold all the text),
|
|
91 |
or one of the error values defined in TError.
|
|
92 |
@internalTechnology
|
|
93 |
*/
|
|
94 |
TInt CJ5Converter::ConvertToUnicode(
|
|
95 |
CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters,
|
|
96 |
TDes16& aUnicode,
|
|
97 |
const TDesC8& aForeign,
|
|
98 |
TInt& aState,
|
|
99 |
TInt& aNumberOfUnconvertibleCharacters,
|
|
100 |
TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter)
|
|
101 |
{
|
|
102 |
// As the aState parameter is used to pass back the detected value
|
|
103 |
// use a "hidden" internal state variable.
|
|
104 |
TInt internalState = CCnvCharacterSetConverter::KStateDefault;
|
|
105 |
|
|
106 |
// determine the encoding type and then decode appropriatly
|
|
107 |
switch ( DetectEncoding(aDefaultEndiannessOfForeignCharacters, aForeign))
|
|
108 |
{
|
|
109 |
case EShiftjis:
|
|
110 |
aState = EShiftjis;
|
|
111 |
return CnvShiftJis::ConvertToUnicode(aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign,
|
|
112 |
aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter);
|
|
113 |
|
|
114 |
case EIso2022jp1:
|
|
115 |
aState = EIso2022jp1;
|
|
116 |
return CnvJisBase::ConvertToUnicode(aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign, internalState,
|
|
117 |
aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter);
|
|
118 |
|
|
119 |
case EEucjp:
|
|
120 |
aState = EEucjp;
|
|
121 |
return ConvertEEucjpToUnicode(
|
|
122 |
aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign, internalState,
|
|
123 |
aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter);
|
|
124 |
|
|
125 |
case EUcs2:
|
|
126 |
aState = EUcs2;
|
|
127 |
return ConvertUcs2ToUnicode( aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign,
|
|
128 |
aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter);
|
|
129 |
|
|
130 |
case EUtf8:
|
|
131 |
aState = EUtf8;
|
|
132 |
return CnvUtfConverter::ConvertToUnicodeFromUtf8(aUnicode, aForeign);
|
|
133 |
|
|
134 |
default:
|
|
135 |
// fall though to the default, which is decode as UTF8
|
|
136 |
aState = EUnknown;
|
|
137 |
break;
|
|
138 |
}
|
|
139 |
|
|
140 |
// decode as UTF8
|
|
141 |
return CnvUtfConverter::ConvertToUnicodeFromUtf8(aUnicode, aForeign);
|
|
142 |
}
|
|
143 |
|
|
144 |
/**
|
|
145 |
This API is used by CCnvCharacterSetConverter::AutoDetectCharacterSetL().
|
|
146 |
This method returns a value between 0 and 100, indicating how likely it
|
|
147 |
is that this is the correct converter, for the text supplied. As J5 is
|
|
148 |
NOT intended to be used with the existing auto-detect mechanism, it will
|
|
149 |
always return 0
|
|
150 |
@internalTechnology
|
|
151 |
*/
|
|
152 |
TBool CJ5Converter::IsInThisCharacterSetL(
|
|
153 |
TBool& aSetToTrue,
|
|
154 |
TInt& aConfidenceLevel,
|
|
155 |
const TDesC8& /* aSample */)
|
|
156 |
{
|
|
157 |
/*
|
|
158 |
aSetToTrue - This value should be set to ETrue. It is used to indicate to
|
|
159 |
CCnvCharacterSetConverter::AutoDetectCharacterSetL() that the plug-in DLL
|
|
160 |
is implementing a function of this signature and is therefore not the empty
|
|
161 |
*/
|
|
162 |
aSetToTrue=ETrue;
|
|
163 |
|
|
164 |
/* no need to look at the sample as this always returns 0
|
|
165 |
as the autodetect feature is not supported by the J5 plug-in
|
|
166 |
*/
|
|
167 |
aConfidenceLevel=0;
|
|
168 |
return ETrue;
|
|
169 |
}
|
|
170 |
|
|
171 |
CJ5Converter* CJ5Converter::NewL()
|
|
172 |
{
|
|
173 |
CJ5Converter* self = new(ELeave) CJ5Converter();
|
|
174 |
CleanupStack::PushL(self);
|
|
175 |
self->ConstructL();
|
|
176 |
CleanupStack::Pop(self);
|
|
177 |
return self;
|
|
178 |
}
|
|
179 |
|
|
180 |
CJ5Converter::~CJ5Converter()
|
|
181 |
{
|
|
182 |
}
|
|
183 |
|
|
184 |
CJ5Converter::CJ5Converter()
|
|
185 |
{
|
|
186 |
}
|
|
187 |
|
|
188 |
void CJ5Converter::ConstructL()
|
|
189 |
{
|
|
190 |
}
|
|
191 |
|
|
192 |
const TImplementationProxy ImplementationTable[] =
|
|
193 |
{
|
|
194 |
#ifdef DOCOMO_TEST
|
|
195 |
// for the test build use a special test UID
|
|
196 |
IMPLEMENTATION_PROXY_ENTRY(0x01000002, CJ5Converter::NewL)
|
|
197 |
#else
|
|
198 |
IMPLEMENTATION_PROXY_ENTRY(KCharacterSetIdentifierJ5, CJ5Converter::NewL)
|
|
199 |
#endif
|
|
200 |
};
|
|
201 |
|
|
202 |
EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
|
|
203 |
{
|
|
204 |
aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
|
|
205 |
|
|
206 |
return ImplementationTable;
|
|
207 |
}
|
|
208 |
|
|
209 |
/**
|
|
210 |
DetectEncoding determine the characterset encoding.
|
|
211 |
The logic for this detection is based on the information in CJKV by Ken Lunde.
|
|
212 |
A detailed diagram of this logic is in the J5 how to document section 2.4
|
|
213 |
@return The detected character set as a enum CJ5Converter.
|
|
214 |
@internalTechnology
|
|
215 |
*/
|
|
216 |
enum CJ5Converter::TJ5Encoding CJ5Converter::DetectEncoding(
|
|
217 |
CCnvCharacterSetConverter::TEndianness& aDefaultEndiannessOfForeignCharacters ,
|
|
218 |
const TDesC8& aForeign)
|
|
219 |
{
|
|
220 |
|
|
221 |
// first check for UCS2
|
|
222 |
CCnvCharacterSetConverter::TEndianness ucs2Endianness = CCnvCharacterSetConverter::ELittleEndian;
|
|
223 |
if ( DetectUcs2(aForeign, ucs2Endianness ))
|
|
224 |
{
|
|
225 |
// if ucs2 is detected pass back the detected endianess
|
|
226 |
aDefaultEndiannessOfForeignCharacters = ucs2Endianness;
|
|
227 |
return EUcs2;
|
|
228 |
}
|
|
229 |
|
|
230 |
// next try EUC_JP
|
|
231 |
TInt eucJpValidBytes = 0;
|
|
232 |
CJ5Converter::TDectectCharacterSet result = DetectEucJp( aForeign, eucJpValidBytes );
|
|
233 |
if ( result == EIsCharacterSet )
|
|
234 |
{
|
|
235 |
return EEucjp;
|
|
236 |
}
|
|
237 |
|
|
238 |
// next try Iso 2020JP
|
|
239 |
if ( DetectIso2022( aForeign ) == EIsCharacterSet )
|
|
240 |
{
|
|
241 |
return EIso2022jp1;
|
|
242 |
}
|
|
243 |
|
|
244 |
// next try Utf8
|
|
245 |
if ( DetectUtf8( aForeign ) == EIsCharacterSet )
|
|
246 |
{
|
|
247 |
return EUtf8;
|
|
248 |
}
|
|
249 |
|
|
250 |
// shiftjis
|
|
251 |
TInt shiftjisValidBytes = 0;
|
|
252 |
result = DetectShiftJis( aForeign, shiftjisValidBytes );
|
|
253 |
if ( result == EIsCharacterSet )
|
|
254 |
{
|
|
255 |
return EShiftjis;
|
|
256 |
}
|
|
257 |
|
|
258 |
// no clear winner so go for the best
|
|
259 |
TInt sampleLength = Min(aForeign.Length(), KMaxSizeAutoDetectSample);
|
|
260 |
|
|
261 |
// if more than half is shiftjis and more shiftjis than EUC_JP,
|
|
262 |
if ((shiftjisValidBytes > eucJpValidBytes ) && (shiftjisValidBytes * 2> sampleLength))
|
|
263 |
return EShiftjis;
|
|
264 |
|
|
265 |
// if more than half is EUC_JP and more EUC_JP than shiftjis,
|
|
266 |
if ((eucJpValidBytes > shiftjisValidBytes ) && (eucJpValidBytes * 2> sampleLength))
|
|
267 |
return EEucjp;
|
|
268 |
|
|
269 |
// return the default
|
|
270 |
return EUcs2;
|
|
271 |
}
|
|
272 |
|
|
273 |
|
|
274 |
/**
|
|
275 |
Check if UCS2.
|
|
276 |
If the first two bytes are the Unicode Endian Specifiers (0xfffe or 0xfeff)
|
|
277 |
then this must be UCS2. Otherwise try lookiing for 0x**00 or 0x00**
|
|
278 |
@param A sample of data to be checked
|
|
279 |
@param The Endianness if USC2 is detected
|
|
280 |
@return ETrue if UCS2 else EFalse
|
|
281 |
@internalTechnology
|
|
282 |
*/
|
|
283 |
TBool CJ5Converter::DetectUcs2( const TDesC8& aForeign,
|
|
284 |
CCnvCharacterSetConverter::TEndianness& aTEndianness )
|
|
285 |
{
|
|
286 |
// if the sample is not big enough
|
|
287 |
if (aForeign.Length() < 2)
|
|
288 |
{
|
|
289 |
return EFalse;
|
|
290 |
}
|
|
291 |
else if (aForeign[0]==0xff && aForeign[1]==0xfe )
|
|
292 |
{
|
|
293 |
// we have found a Little Endian Byte order mark
|
|
294 |
aTEndianness = CCnvCharacterSetConverter::ELittleEndian;
|
|
295 |
return ETrue;
|
|
296 |
}
|
|
297 |
else if (aForeign[0]==0xfe && aForeign[1]==0xff )
|
|
298 |
{
|
|
299 |
// we have found a Big Endian Byte order mark
|
|
300 |
aTEndianness = CCnvCharacterSetConverter::EBigEndian;
|
|
301 |
return ETrue;
|
|
302 |
}
|
|
303 |
|
|
304 |
// Next check for sequences of 0x**00 or 0x00** as UCS-2 is the only charset that
|
|
305 |
// specifies 0x**00 or 0x00** (according to endianness) for the ASCII range of characters.
|
|
306 |
// NB: This will fail if there are no ASCII characters in the text.
|
|
307 |
TInt sampleLength = aForeign.Length();
|
|
308 |
sampleLength = Min(aForeign.Length(), KMaxSizeAutoDetectSample);;
|
|
309 |
|
|
310 |
// check the sample for sequences of 0x**00 or 0x00**
|
|
311 |
TInt bigEndianConfidence = 0;
|
|
312 |
TInt littleEndianConfidence = 0;
|
|
313 |
TInt i=0;
|
|
314 |
for(;i< (sampleLength-1); i+=2)
|
|
315 |
{
|
|
316 |
if( aForeign[i] == 0x00)
|
|
317 |
{
|
|
318 |
bigEndianConfidence +=2;
|
|
319 |
}
|
|
320 |
else if ( aForeign[i+1] == 0x00)
|
|
321 |
{
|
|
322 |
littleEndianConfidence +=2;
|
|
323 |
}
|
|
324 |
}
|
|
325 |
|
|
326 |
// which occurs most BE or LE
|
|
327 |
TInt confidenceLevel = 0;
|
|
328 |
if (bigEndianConfidence > littleEndianConfidence)
|
|
329 |
{
|
|
330 |
aTEndianness = CCnvCharacterSetConverter::EBigEndian;
|
|
331 |
confidenceLevel = bigEndianConfidence;
|
|
332 |
}
|
|
333 |
else
|
|
334 |
{
|
|
335 |
aTEndianness = CCnvCharacterSetConverter::ELittleEndian;
|
|
336 |
confidenceLevel = littleEndianConfidence;
|
|
337 |
}
|
|
338 |
|
|
339 |
// if more than 97% count as UCS2
|
|
340 |
if ( confidenceLevel * 100/sampleLength > 97)
|
|
341 |
return ETrue;
|
|
342 |
|
|
343 |
return EFalse;
|
|
344 |
}
|
|
345 |
|
|
346 |
/**
|
|
347 |
Check if ShiftJis (reference CJKV by Ken Lunde page 175)
|
|
348 |
@param A sample of data to be checked
|
|
349 |
@param The number of input bytes that can be converted
|
|
350 |
@return The result of the check as either EIsCharacterSet, EIsNotCharacterSet or EMaybeCharacterSet
|
|
351 |
@internalTechnology
|
|
352 |
*/
|
|
353 |
enum CJ5Converter::TDectectCharacterSet CJ5Converter::DetectShiftJis( const TDesC8& aForeign,TInt &aNumberOfBytesConverted )
|
|
354 |
{
|
|
355 |
// Get the sample length
|
|
356 |
TInt sampleLength = Min(aForeign.Length(), KMaxSizeAutoDetectSample);;
|
|
357 |
|
|
358 |
TInt i=0;
|
|
359 |
aNumberOfBytesConverted = 0;
|
|
360 |
|
|
361 |
TText8 character;
|
|
362 |
TText8 characterPlus1;
|
|
363 |
TText8 characterPlus2;
|
|
364 |
|
|
365 |
// scan the sample text looking for valid shiftjis data
|
|
366 |
while ( i < sampleLength )
|
|
367 |
{
|
|
368 |
// get the next few characters, use 0 if there is no more sample
|
|
369 |
// as this will not match any test.
|
|
370 |
character = aForeign[i];
|
|
371 |
characterPlus1 = ( i < (sampleLength-1) ? aForeign[i+1]:0);
|
|
372 |
characterPlus2 = ( i < (sampleLength-2) ? aForeign[i+2]:0);
|
|
373 |
|
|
374 |
// SHIFTJIS - 0x8e to 0x9f followed by 0x40 to 0xfc
|
|
375 |
if ((character >= 0x81) && (character <= 0x9f) &&
|
|
376 |
(characterPlus1 >= 0x40) && (characterPlus1 <= 0xfc) )
|
|
377 |
{
|
|
378 |
// this is SHIFTJIS unless it is EUC JP code set 2 or 3
|
|
379 |
if ((character == 0x8E) && (characterPlus1 >= 0xA1) && (characterPlus1 <= 0xDF))
|
|
380 |
{
|
|
381 |
// this could be EUC JP code set 2 (or shiftjis)
|
|
382 |
aNumberOfBytesConverted+=2;
|
|
383 |
i++;
|
|
384 |
}
|
|
385 |
else if ((character == 0x8F) &&
|
|
386 |
(characterPlus1 >= 0xA1) && (characterPlus1 <= 0xDF) &&
|
|
387 |
(characterPlus2 >= 0xA1) && (characterPlus2 <= 0xDF))
|
|
388 |
{
|
|
389 |
// this could be EUC JP code set 3 (or shiftjis)
|
|
390 |
aNumberOfBytesConverted+=3;
|
|
391 |
i+=2;
|
|
392 |
}
|
|
393 |
else
|
|
394 |
{
|
|
395 |
// this can only be shift jis
|
|
396 |
return EIsCharacterSet;
|
|
397 |
}
|
|
398 |
}
|
|
399 |
|
|
400 |
// SHIFTJIS - 0xE0 to 0xEF followed by .....
|
|
401 |
else if ((character >= 0xE0) && (character <= 0xEF))
|
|
402 |
{
|
|
403 |
// 0x40 to 0xFC which overlaps UTF8 between 0x80 and 0xBF
|
|
404 |
// including Mopera extension to shiftjis from 0xEF80 to 0xEFFC
|
|
405 |
|
|
406 |
if ( (characterPlus1 >= 0x40) && (characterPlus1 <= 0x7E) )
|
|
407 |
{
|
|
408 |
// this can only be shift jis
|
|
409 |
return EIsCharacterSet;
|
|
410 |
}
|
|
411 |
else if ( (characterPlus1 >= 0xC0) && (characterPlus1 <= 0xFC) )
|
|
412 |
{
|
|
413 |
// this could be EUC JP code set 1
|
|
414 |
aNumberOfBytesConverted+=2;
|
|
415 |
i++;
|
|
416 |
}
|
|
417 |
|
|
418 |
// problem here is the overlap between the UTF8 and shiftjis
|
|
419 |
else if ( (characterPlus1 >= 0x80) && (characterPlus1 <= 0xBF) )
|
|
420 |
{
|
|
421 |
// this could be shiftjis or utf8
|
|
422 |
aNumberOfBytesConverted+=2;
|
|
423 |
i++;
|
|
424 |
}
|
|
425 |
}
|
|
426 |
// half width katakana A1-DF
|
|
427 |
else if ((character >= 0xA1) && (character <= 0xDF))
|
|
428 |
{
|
|
429 |
aNumberOfBytesConverted+=1;
|
|
430 |
}
|
|
431 |
// ASCII or JIS-Roman 20-7e
|
|
432 |
else if ( ((character >= 0x20) && (character <= 0x7E)) || (character == 0x0A) || (character == 0x0D))
|
|
433 |
{
|
|
434 |
aNumberOfBytesConverted+=1;
|
|
435 |
}
|
|
436 |
else
|
|
437 |
{
|
|
438 |
// This is not decoding as shiftjis, so reject
|
|
439 |
aNumberOfBytesConverted =0;
|
|
440 |
return EIsNotCharacterSet;
|
|
441 |
}
|
|
442 |
i++;
|
|
443 |
}
|
|
444 |
|
|
445 |
// if all the characters could be converted
|
|
446 |
if (aNumberOfBytesConverted == sampleLength)
|
|
447 |
{
|
|
448 |
return EIsCharacterSet;
|
|
449 |
}
|
|
450 |
else if (aNumberOfBytesConverted == 0)
|
|
451 |
{
|
|
452 |
return EIsNotCharacterSet;
|
|
453 |
}
|
|
454 |
else
|
|
455 |
{
|
|
456 |
return EMaybeCharacterSet;
|
|
457 |
}
|
|
458 |
}
|
|
459 |
|
|
460 |
/**
|
|
461 |
Check if UTF8 (reference CJKV by Ken Lunde page 189)
|
|
462 |
@param A sample of data to be checked
|
|
463 |
@param The number of input bytes that can be converted
|
|
464 |
@return The result of the check as either EIsCharacterSet, EIsNotCharacterSet or EMaybeCharacterSet
|
|
465 |
@internalTechnology
|
|
466 |
*/
|
|
467 |
enum CJ5Converter::TDectectCharacterSet CJ5Converter::DetectUtf8( const TDesC8& aForeign )
|
|
468 |
{
|
|
469 |
// Get the sample length
|
|
470 |
TInt sampleLength = Min(aForeign.Length(), KMaxSizeAutoDetectSample);;
|
|
471 |
|
|
472 |
TInt i=0;
|
|
473 |
TText8 character;
|
|
474 |
TText8 characterPlus1;
|
|
475 |
TText8 characterPlus2;
|
|
476 |
TText8 characterPlus3;
|
|
477 |
|
|
478 |
// scan the sample text looking for valid UTF8
|
|
479 |
while ( i < sampleLength )
|
|
480 |
{
|
|
481 |
// get the next few characters, use 0 if there is no more sample
|
|
482 |
// as this will not match any test.
|
|
483 |
character = aForeign[i];
|
|
484 |
characterPlus1 = ( i < (sampleLength-1) ? aForeign[i+1]:0);
|
|
485 |
characterPlus2 = ( i < (sampleLength-2) ? aForeign[i+2]:0);
|
|
486 |
characterPlus3 = ( i < (sampleLength-3) ? aForeign[i+3]:0);
|
|
487 |
|
|
488 |
// UTF8 range 110xxxxx followed by one valid UTF8 bytes
|
|
489 |
if(((character & 0xe0)==0xc0) && (( characterPlus1 & 0xc0)==0x80) )
|
|
490 |
{
|
|
491 |
// two bytes of valid UTF8 found
|
|
492 |
i+=2;
|
|
493 |
}
|
|
494 |
// UTF8 range 1110xxxx followed by two valid UTF8 bytes
|
|
495 |
else if(((character & 0xf0)==0xe0) && (( characterPlus1 & 0xc0)==0x80) && (( characterPlus2 & 0xc0)==0x80))
|
|
496 |
{
|
|
497 |
// three bytes of valid UTF8 found
|
|
498 |
i+=3;
|
|
499 |
}
|
|
500 |
// UTF8 range 11110xxx followed by three valid UTF8 bytes
|
|
501 |
else if(((character & 0xf8)==0xf0) && (( characterPlus1 & 0xc0)==0x80)
|
|
502 |
&& (( characterPlus2 & 0xc0)==0x80) && (( characterPlus3 & 0xc0)==0x80) )
|
|
503 |
{
|
|
504 |
// four bytes of valid UTF8 found
|
|
505 |
i+=4;
|
|
506 |
}
|
|
507 |
|
|
508 |
// ascii range 0 to 0x7F
|
|
509 |
else if((character & 0x80)==0x00)
|
|
510 |
{
|
|
511 |
// The value of character is in the range 0x00-0x7f
|
|
512 |
// UTF8 maintains ASCII transparency. So it's a valid UTF8.
|
|
513 |
i++;
|
|
514 |
}
|
|
515 |
// if the sample data is longer than KMaxSizeAutoDetectSample then except anything
|
|
516 |
// for the last two bytes as they may not appear valid without more data
|
|
517 |
else if( i >= (KMaxSizeAutoDetectSample -2) )
|
|
518 |
{
|
|
519 |
i++;
|
|
520 |
}
|
|
521 |
else
|
|
522 |
{
|
|
523 |
// This is not decoding as UTF8 so reject
|
|
524 |
return EIsNotCharacterSet;
|
|
525 |
}
|
|
526 |
}
|
|
527 |
|
|
528 |
// All the characters could be converted
|
|
529 |
return EIsCharacterSet;
|
|
530 |
|
|
531 |
}
|
|
532 |
|
|
533 |
|
|
534 |
/**
|
|
535 |
Check if ISO2022JP by lookiing for the escape sequences.
|
|
536 |
@param A sample of data to be checked
|
|
537 |
@param The number of input bytes that can be converted
|
|
538 |
@return The result of the check as either EIsCharacterSet, EIsNotCharacterSet or EMaybeCharacterSet
|
|
539 |
@internalTechnology
|
|
540 |
*/
|
|
541 |
enum CJ5Converter::TDectectCharacterSet CJ5Converter::DetectIso2022( const TDesC8& aForeign )
|
|
542 |
{
|
|
543 |
// Get the sample length
|
|
544 |
TInt sampleLength = Min(aForeign.Length(), KMaxSizeAutoDetectSample);;
|
|
545 |
|
|
546 |
TInt i=0;
|
|
547 |
TText8 character;
|
|
548 |
TText8 characterPlus1;
|
|
549 |
TText8 characterPlus2;
|
|
550 |
TText8 characterPlus3;
|
|
551 |
TText8 characterPlus4;
|
|
552 |
TText8 characterPlus5;
|
|
553 |
|
|
554 |
// scan the sample text looking for valid UTF8
|
|
555 |
while ( i < sampleLength )
|
|
556 |
{
|
|
557 |
// get the next few characters, use 0 if there is no more sample
|
|
558 |
// as this will not match any test.
|
|
559 |
character = aForeign[i];
|
|
560 |
characterPlus1 = ( i < (sampleLength-1) ? aForeign[i+1]:0);
|
|
561 |
characterPlus2 = ( i < (sampleLength-2) ? aForeign[i+2]:0);
|
|
562 |
characterPlus3 = ( i < (sampleLength-3) ? aForeign[i+3]:0);
|
|
563 |
|
|
564 |
|
|
565 |
// check for the JIS escape sequences of ISO 2022Jp
|
|
566 |
// These values have been taken from JISBASE_SHARED
|
|
567 |
if (character == KEscape)
|
|
568 |
{
|
|
569 |
// Escape Sequence For Jis C6226_1978 \x1b\x24\x40
|
|
570 |
if ((characterPlus1 == 0x24) && (characterPlus2 == 0x40))
|
|
571 |
{
|
|
572 |
return EIsCharacterSet;
|
|
573 |
}
|
|
574 |
|
|
575 |
// Escape Sequence For Jis X0208_1983 \x1b\x24\x42
|
|
576 |
else if ((characterPlus1 == 0x24) && (characterPlus2 == 0x42))
|
|
577 |
{
|
|
578 |
return EIsCharacterSet;
|
|
579 |
}
|
|
580 |
|
|
581 |
// Escape Sequence For Jis Roman \x1b\x28\x4a
|
|
582 |
else if ((characterPlus1 == 0x28) && (characterPlus2 == 0x4A))
|
|
583 |
{
|
|
584 |
return EIsCharacterSet;
|
|
585 |
}
|
|
586 |
|
|
587 |
// Escape Sequence For Jis RomanIncorrect \x1b\x28\x48
|
|
588 |
else if ((characterPlus1 == 0x28) && (characterPlus2 == 0x48))
|
|
589 |
{
|
|
590 |
return EIsCharacterSet;
|
|
591 |
}
|
|
592 |
|
|
593 |
// Escape Sequence For Ascii \x1b\x28\x42
|
|
594 |
else if ((characterPlus1 == 0x28) && (characterPlus2 == 0x42))
|
|
595 |
{
|
|
596 |
return EIsCharacterSet;
|
|
597 |
}
|
|
598 |
|
|
599 |
// Escape Sequence For EscapeSequenceForHalfWidthKatakana \x1b\x28\x49
|
|
600 |
else if ((characterPlus1 == 0x28) && (characterPlus2 == 0x49))
|
|
601 |
{
|
|
602 |
return EIsCharacterSet;
|
|
603 |
}
|
|
604 |
|
|
605 |
// Escape Sequence For Jis X0208_199x \x1b\x26\x40\x1b\x24\x42
|
|
606 |
else if ((characterPlus1 == 0x26) && (characterPlus2 == 0x40))
|
|
607 |
{
|
|
608 |
characterPlus4 = ( i < (sampleLength-4) ? aForeign[i+4]:0);
|
|
609 |
characterPlus5 = ( i < (sampleLength-5) ? aForeign[i+5]:0);
|
|
610 |
|
|
611 |
if ((characterPlus3 == 0x1b) && (characterPlus4 == 0x24) && (characterPlus5 == 0x42))
|
|
612 |
{
|
|
613 |
return EIsCharacterSet;
|
|
614 |
}
|
|
615 |
}
|
|
616 |
// Escape Sequence For Jis X0212_1990 \x1b\x24\x28\x44
|
|
617 |
else if ((characterPlus1 == 0x24) && (characterPlus2 == 0x28))
|
|
618 |
{
|
|
619 |
if (characterPlus3 == 0x44)
|
|
620 |
{
|
|
621 |
return EIsCharacterSet;
|
|
622 |
}
|
|
623 |
}
|
|
624 |
|
|
625 |
// check for the JIS escape sequences of ISO 2022Jp "B@" x42 x40
|
|
626 |
else if ((characterPlus1 == 'B') || (characterPlus1 == '@'))
|
|
627 |
{
|
|
628 |
return EIsCharacterSet;
|
|
629 |
}
|
|
630 |
|
|
631 |
} // end of if ( character == KEscape )
|
|
632 |
|
|
633 |
i++;
|
|
634 |
}
|
|
635 |
|
|
636 |
// if escape sequences have been found then this is not ISO2022
|
|
637 |
return EIsNotCharacterSet;
|
|
638 |
|
|
639 |
}
|
|
640 |
|
|
641 |
|
|
642 |
/**
|
|
643 |
Check if EUC JP (reference CJKV by Ken Lunde page 164)
|
|
644 |
@param A sample of data to be checked
|
|
645 |
@param The number of input bytes that can be converted
|
|
646 |
@return The result of the check as either EIsCharacterSet, EIsNotCharacterSet or EMaybeCharacterSet
|
|
647 |
@internalTechnology
|
|
648 |
*/
|
|
649 |
CJ5Converter::TDectectCharacterSet CJ5Converter::DetectEucJp( const TDesC8& aForeign,TInt &aNumberOfBytesConverted )
|
|
650 |
{
|
|
651 |
// Get the sample length
|
|
652 |
TInt sampleLength = Min(aForeign.Length(), KMaxSizeAutoDetectSample);;
|
|
653 |
|
|
654 |
TInt i=0;
|
|
655 |
aNumberOfBytesConverted = 0;
|
|
656 |
|
|
657 |
TText8 character;
|
|
658 |
TText8 characterPlus1;
|
|
659 |
TText8 characterPlus2;
|
|
660 |
|
|
661 |
// scan the sample text looking for valid shiftjis data
|
|
662 |
while ( i < sampleLength )
|
|
663 |
{
|
|
664 |
// get the next few characters, use 0 if there is no more sample
|
|
665 |
// as this will not match any test.
|
|
666 |
character = aForeign[i];
|
|
667 |
characterPlus1 = ( i < (sampleLength-1) ? aForeign[i+1]:0);
|
|
668 |
characterPlus2 = ( i < (sampleLength-2) ? aForeign[i+2]:0);
|
|
669 |
|
|
670 |
// EUCJP code set 0 0x21-0x7e
|
|
671 |
if ( (character >= 0x21) && (character <= 0x7e))
|
|
672 |
{
|
|
673 |
aNumberOfBytesConverted++;
|
|
674 |
}
|
|
675 |
else if ( (character == 0x0a) || (character == 0x0d))
|
|
676 |
{
|
|
677 |
aNumberOfBytesConverted++;
|
|
678 |
}
|
|
679 |
// EUCJP code set 1
|
|
680 |
else if ( (character >= 0xa1) && (character <= 0xff)
|
|
681 |
&& (characterPlus1 >= 0xa1) && (characterPlus1 <= 0xff) )
|
|
682 |
{
|
|
683 |
aNumberOfBytesConverted+=2;
|
|
684 |
i++;
|
|
685 |
}
|
|
686 |
|
|
687 |
// EUC JP code set 2, starts with the EUC JP SS2 character (0x8E)
|
|
688 |
// and is followed by character in range 0xA1- 0xDF
|
|
689 |
else if ((character == 0x8E) && (characterPlus1 >= 0xA1) && (characterPlus1 <= 0xDF) )
|
|
690 |
{
|
|
691 |
// this could be 2 bytes of EUC JP code set 2
|
|
692 |
aNumberOfBytesConverted += 2;
|
|
693 |
i++;
|
|
694 |
}
|
|
695 |
// EUC JP code set 3, starts with the EUC JP SS3 character (0x8F)
|
|
696 |
// and is followed by two characters in range A1- DF A1 -FE
|
|
697 |
else if ((character == 0x8F) && (characterPlus1 >= 0xA1) && (characterPlus1 <= 0xDF)
|
|
698 |
&& (characterPlus2 >= 0xA1) && (characterPlus2 <= 0xDF))
|
|
699 |
{
|
|
700 |
// this could be 3 bytes of EUC JP code set 3
|
|
701 |
aNumberOfBytesConverted += 3;
|
|
702 |
i+=2;
|
|
703 |
}
|
|
704 |
else
|
|
705 |
{
|
|
706 |
// This is not a valid decoding as EUC JP so reject
|
|
707 |
return EIsNotCharacterSet;
|
|
708 |
}
|
|
709 |
i++;
|
|
710 |
}
|
|
711 |
|
|
712 |
|
|
713 |
// if all the characters could be converted
|
|
714 |
if (aNumberOfBytesConverted == sampleLength)
|
|
715 |
{
|
|
716 |
return EIsCharacterSet;
|
|
717 |
}
|
|
718 |
else if (aNumberOfBytesConverted == 0)
|
|
719 |
{
|
|
720 |
return EIsNotCharacterSet;
|
|
721 |
}
|
|
722 |
else
|
|
723 |
{
|
|
724 |
return EMaybeCharacterSet;
|
|
725 |
}
|
|
726 |
}
|
|
727 |
|
|
728 |
|
|
729 |
/**
|
|
730 |
Convert from UCS2 (Universal Character Set containing two bytes) to unicode
|
|
731 |
Remove any byte order marks in the UCSs.
|
|
732 |
@param aUnicode Contains the converted text in the Unicode character set.
|
|
733 |
@param aForeign The non-Unicode source text to be converted
|
|
734 |
@param aNumberOfUnconvertibleCharacters Contains the number of bytes which were not converted.
|
|
735 |
@param aIndexOfFirstByteOfFirstUnconvertibleCharacter The index of the first byte of the first unconvertible character.
|
|
736 |
@return the number of bytes converted
|
|
737 |
@internalTechnology
|
|
738 |
*/
|
|
739 |
TInt CJ5Converter::ConvertUcs2ToUnicode(CCnvCharacterSetConverter::TEndianness& aDefaultEndiannessOfForeignCharacters,
|
|
740 |
TDes16& aUnicode,
|
|
741 |
const TDesC8& aForeign,
|
|
742 |
TInt& aNumberOfUnconvertibleCharacters,
|
|
743 |
TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter)
|
|
744 |
|
|
745 |
{
|
|
746 |
TInt numberOfBytesConverted = 0;
|
|
747 |
TInt numberOfUnicodeCharacters =0;
|
|
748 |
TChar nextChar;
|
|
749 |
|
|
750 |
// start at begining of the output buffer provided
|
|
751 |
aUnicode.Zero();
|
|
752 |
|
|
753 |
// while there is at least 2 bytes of data to convert and space in the output buffer
|
|
754 |
while ( (numberOfBytesConverted+1 < aForeign.Size()) && (numberOfUnicodeCharacters < aUnicode.MaxLength()) )
|
|
755 |
{
|
|
756 |
if (aDefaultEndiannessOfForeignCharacters == CCnvCharacterSetConverter::ELittleEndian )
|
|
757 |
{
|
|
758 |
// ELittleEndian 0x??00
|
|
759 |
nextChar = aForeign[numberOfBytesConverted] + ( aForeign[numberOfBytesConverted+1] << 8);
|
|
760 |
}
|
|
761 |
else
|
|
762 |
{
|
|
763 |
// EBigEndian 0x00??
|
|
764 |
nextChar = ( aForeign[numberOfBytesConverted] <<8 ) + aForeign[numberOfBytesConverted+1];
|
|
765 |
}
|
|
766 |
|
|
767 |
// save the unicode character extracted unless it's a BOM
|
|
768 |
if ( nextChar != KByteOrderMark )
|
|
769 |
{
|
|
770 |
aUnicode.Append( nextChar );
|
|
771 |
numberOfUnicodeCharacters++;
|
|
772 |
}
|
|
773 |
|
|
774 |
numberOfBytesConverted+=2;
|
|
775 |
}
|
|
776 |
|
|
777 |
// there are no uncovertable characters with UCS2, but there could be
|
|
778 |
aNumberOfUnconvertibleCharacters = 0;
|
|
779 |
// a negative value indicates that all characters converted
|
|
780 |
aIndexOfFirstByteOfFirstUnconvertibleCharacter = -1;
|
|
781 |
|
|
782 |
// returns the number of unconverted bytes left at the end of the input descriptor
|
|
783 |
// Note there could be 1 byte left over if an odd number of bytes provided for conversion
|
|
784 |
return aForeign.Size() - numberOfBytesConverted;
|
|
785 |
}
|
|
786 |
|
|
787 |
/**
|
|
788 |
Convert from EUC_JP (Extended Unix Code encoding for Japanese)
|
|
789 |
Using the standard Charconv method of an array of methods
|
|
790 |
@return the number of bytes converted
|
|
791 |
@internalTechnology
|
|
792 |
*/
|
|
793 |
TInt CJ5Converter::ConvertEEucjpToUnicode(
|
|
794 |
CCnvCharacterSetConverter::TEndianness aDefaultEndiannessOfForeignCharacters,
|
|
795 |
TDes16& aUnicode,
|
|
796 |
const TDesC8& aForeign,
|
|
797 |
TInt& /*aState*/,
|
|
798 |
TInt& aNumberOfUnconvertibleCharacters,
|
|
799 |
TInt& aIndexOfFirstByteOfFirstUnconvertibleCharacter)
|
|
800 |
{
|
|
801 |
TFixedArray<CnvUtilities::SMethod, 4> methods;
|
|
802 |
methods[0].iNumberOfBytesAbleToConvert=NumberOfBytesAbleToConvertToJisRoman;
|
|
803 |
methods[0].iConvertToIntermediateBufferInPlace=DummyConvertToIntermediateBufferInPlace;
|
|
804 |
methods[0].iConversionData=&CnvJisRoman::ConversionData();
|
|
805 |
methods[0].iNumberOfBytesPerCharacter=1;
|
|
806 |
methods[0].iNumberOfCoreBytesPerCharacter=1;
|
|
807 |
methods[1].iNumberOfBytesAbleToConvert=NumberOfBytesAbleToConvertToJisX0208;
|
|
808 |
methods[1].iConvertToIntermediateBufferInPlace=ConvertToJisX0208FromEucJpPackedInPlace;
|
|
809 |
methods[1].iConversionData=&CnvJisX0208::ConversionData();
|
|
810 |
methods[1].iNumberOfBytesPerCharacter=2;
|
|
811 |
methods[1].iNumberOfCoreBytesPerCharacter=2;
|
|
812 |
methods[2].iNumberOfBytesAbleToConvert=NumberOfBytesAbleToConvertToHalfWidthKatakana8;
|
|
813 |
methods[2].iConvertToIntermediateBufferInPlace=ConvertToHalfWidthKatakana8FromEucJpPackedInPlace;
|
|
814 |
methods[2].iConversionData=&CnvHalfWidthKatakana8::ConversionData();
|
|
815 |
methods[2].iNumberOfBytesPerCharacter=2;
|
|
816 |
methods[2].iNumberOfCoreBytesPerCharacter=1;
|
|
817 |
methods[3].iNumberOfBytesAbleToConvert=NumberOfBytesAbleToConvertToJisX0212;
|
|
818 |
methods[3].iConvertToIntermediateBufferInPlace=ConvertToJisX0212FromEucJpPackedInPlace;
|
|
819 |
methods[3].iConversionData=&CnvJisX0212::ConversionData();
|
|
820 |
methods[3].iNumberOfBytesPerCharacter=3;
|
|
821 |
methods[3].iNumberOfCoreBytesPerCharacter=2;
|
|
822 |
return CnvUtilities::ConvertToUnicodeFromHeterogeneousForeign(aDefaultEndiannessOfForeignCharacters, aUnicode, aForeign, aNumberOfUnconvertibleCharacters, aIndexOfFirstByteOfFirstUnconvertibleCharacter, methods.Array());
|
|
823 |
}
|
|
824 |
|