|
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 "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 "convutils.h" |
|
29 #include "shiftjis.h" |
|
30 #include "JISBASE.H" |
|
31 #include "jisx0201.h" |
|
32 #include "jisx0208.h" |
|
33 #include "jisx0212.h" |
|
34 |
|
35 const TUint KSingleShift2=0x8e; |
|
36 const TUint KSingleShift3=0x8f; |
|
37 |
|
38 |
|
39 #if defined(_DEBUG) |
|
40 |
|
41 _LIT(KLitPanicText, "EUCJP_PACKED"); |
|
42 |
|
43 enum TPanic |
|
44 { |
|
45 EPanicNothingToConvert1=1, |
|
46 EPanicNothingToConvert2, |
|
47 EPanicNothingToConvert3, |
|
48 EPanicNothingToConvert4, |
|
49 EPanicNothingToConvert5, |
|
50 EPanicNothingToConvert6, |
|
51 EPanicOddNumberOfBytes1, |
|
52 EPanicOddNumberOfBytes2, |
|
53 EPanicOddNumberOfBytes3, |
|
54 EPanicOddNumberOfBytes4, |
|
55 EPanicOddNumberOfBytes5, |
|
56 EPanicOddNumberOfBytes6, |
|
57 EPanicBadHighBit1, |
|
58 EPanicBadHighBit2, |
|
59 EPanicBadHighBit3, |
|
60 EPanicBadHighBit4, |
|
61 EPanicBadHighBit5, |
|
62 EPanicBadHighBit6, |
|
63 EPanicBadHighBit7, |
|
64 EPanicBadPointers1, |
|
65 EPanicBadPointers2, |
|
66 EPanicBadPointers3, |
|
67 EPanicBadPointers4, |
|
68 EPanicBadPointers5, |
|
69 EPanicBadPointers6, |
|
70 EPanicBadPointers7, |
|
71 EPanicBadPointers8, |
|
72 EPanicBadPointers9, |
|
73 EPanicBadPointers10, |
|
74 EPanicBadPointers11, |
|
75 EPanicBadPointers12, |
|
76 EPanicBadPointers13, |
|
77 EPanicBadPointers14, |
|
78 EPanicBadPointers15, |
|
79 EPanicBadPointers16, |
|
80 EPanicBadPointers17, |
|
81 EPanicBadPointers18, |
|
82 EPanicBadPointers19, |
|
83 EPanicBadPointers20, |
|
84 EPanicBadPointers21, |
|
85 EPanicBadPointers22, |
|
86 EPanicBadPointers23, |
|
87 EPanicBadPointers24, |
|
88 EPanicBadPointers25, |
|
89 EPanicBadPointers26, |
|
90 EPanicBadPointers27, |
|
91 EPanicBadPointers28, |
|
92 EPanicBadPointers29, |
|
93 EPanicBadPointers30, |
|
94 EPanicBadPointers31, |
|
95 EPanicBadPointers32, |
|
96 EPanicBadPointers33, |
|
97 EPanicBadPointers34, |
|
98 EPanicBadPointers35, |
|
99 EPanicBadPointers36, |
|
100 EPanicBadCalculation1, |
|
101 EPanicBadCalculation2, |
|
102 EPanicNumberOfBytesIsNotMultipleOfThree1, |
|
103 EPanicNumberOfBytesIsNotMultipleOfThree2, |
|
104 EPanicSingleShift2Expected, |
|
105 EPanicSingleShift3Expected |
|
106 }; |
|
107 |
|
108 LOCAL_C void Panic(TPanic aPanic) |
|
109 { |
|
110 User::Panic(KLitPanicText, aPanic); |
|
111 } |
|
112 |
|
113 #endif |
|
114 |
|
115 /** |
|
116 @return The number of bytes that can be converted from aDescriptor to JisRoman |
|
117 @internalTechnology |
|
118 */ |
|
119 TInt NumberOfBytesAbleToConvertToJisRoman(const TDesC8& aDescriptor) |
|
120 { |
|
121 const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1; |
|
122 const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length(); |
|
123 if (pointerToPreviousByte==pointerToLastByte) |
|
124 { |
|
125 return 0; |
|
126 } |
|
127 |
|
128 FOREVER |
|
129 { |
|
130 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers14)); |
|
131 const TUint currentByte=*(pointerToPreviousByte+1); |
|
132 if (currentByte&0x80) |
|
133 { |
|
134 break; |
|
135 } |
|
136 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers15)); |
|
137 ++pointerToPreviousByte; |
|
138 __ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers16)); |
|
139 if (pointerToPreviousByte>=pointerToLastByte) |
|
140 { |
|
141 break; |
|
142 } |
|
143 } |
|
144 return (pointerToPreviousByte+1)-aDescriptor.Ptr(); |
|
145 } |
|
146 |
|
147 /** |
|
148 @return The number of bytes that can be converted from aDescriptor to Jis X208 |
|
149 @internalTechnology |
|
150 */ |
|
151 TInt NumberOfBytesAbleToConvertToJisX0208(const TDesC8& aDescriptor) |
|
152 { |
|
153 const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1; |
|
154 const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length(); |
|
155 if (pointerToPreviousByte==pointerToLastByte) |
|
156 { |
|
157 return 0; |
|
158 } |
|
159 FOREVER |
|
160 { |
|
161 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers17)); |
|
162 TUint currentByte=*(pointerToPreviousByte+1); |
|
163 if (currentByte<0xa0) |
|
164 { |
|
165 break; |
|
166 } |
|
167 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers18)); |
|
168 if (pointerToLastByte-pointerToPreviousByte<2) |
|
169 { |
|
170 break; |
|
171 } |
|
172 ++pointerToPreviousByte; |
|
173 currentByte=*(pointerToPreviousByte+1); |
|
174 if (currentByte<0xa0) |
|
175 { |
|
176 return CCnvCharacterSetConverter::EErrorIllFormedInput; |
|
177 } |
|
178 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers19)); |
|
179 ++pointerToPreviousByte; |
|
180 __ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers20)); |
|
181 if (pointerToPreviousByte>=pointerToLastByte) |
|
182 { |
|
183 break; |
|
184 } |
|
185 } |
|
186 return (pointerToPreviousByte+1)-aDescriptor.Ptr(); |
|
187 } |
|
188 |
|
189 /** |
|
190 @return The number of bytes that can be converted from aDescriptor to HalfWidthKatakana |
|
191 @internalTechnology |
|
192 */ |
|
193 TInt NumberOfBytesAbleToConvertToHalfWidthKatakana8(const TDesC8& aDescriptor) |
|
194 { |
|
195 const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1; |
|
196 const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length(); |
|
197 if (pointerToPreviousByte==pointerToLastByte) |
|
198 { |
|
199 return 0; |
|
200 } |
|
201 FOREVER |
|
202 { |
|
203 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers21)); |
|
204 TUint currentByte=*(pointerToPreviousByte+1); |
|
205 if (currentByte!=KSingleShift2) |
|
206 { |
|
207 break; |
|
208 } |
|
209 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers22)); |
|
210 if (pointerToLastByte-pointerToPreviousByte<2) |
|
211 { |
|
212 break; |
|
213 } |
|
214 ++pointerToPreviousByte; |
|
215 currentByte=*(pointerToPreviousByte+1); |
|
216 if (currentByte<0xa0) |
|
217 { |
|
218 return CCnvCharacterSetConverter::EErrorIllFormedInput; |
|
219 } |
|
220 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers23)); |
|
221 ++pointerToPreviousByte; |
|
222 __ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers24)); |
|
223 if (pointerToPreviousByte>=pointerToLastByte) |
|
224 { |
|
225 break; |
|
226 } |
|
227 } |
|
228 return (pointerToPreviousByte+1)-aDescriptor.Ptr(); |
|
229 } |
|
230 |
|
231 /** |
|
232 @return The number of bytes that can be converted from aDescriptor to JISX0212 |
|
233 @internalTechnology |
|
234 */ |
|
235 TInt NumberOfBytesAbleToConvertToJisX0212(const TDesC8& aDescriptor) |
|
236 { |
|
237 const TUint8* pointerToPreviousByte=aDescriptor.Ptr()-1; |
|
238 const TUint8* const pointerToLastByte=pointerToPreviousByte+aDescriptor.Length(); |
|
239 if (pointerToPreviousByte==pointerToLastByte) |
|
240 { |
|
241 return 0; |
|
242 } |
|
243 |
|
244 FOREVER |
|
245 { |
|
246 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers25)); |
|
247 TUint currentByte=*(pointerToPreviousByte+1); |
|
248 if (currentByte!=KSingleShift3) |
|
249 { |
|
250 break; |
|
251 } |
|
252 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers26)); |
|
253 if (pointerToLastByte-pointerToPreviousByte<3) |
|
254 { |
|
255 break; |
|
256 } |
|
257 ++pointerToPreviousByte; |
|
258 currentByte=*(pointerToPreviousByte+1); |
|
259 if (currentByte<0xa0) |
|
260 { |
|
261 return CCnvCharacterSetConverter::EErrorIllFormedInput; |
|
262 } |
|
263 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers27)); |
|
264 ++pointerToPreviousByte; |
|
265 currentByte=*(pointerToPreviousByte+1); |
|
266 if (currentByte<0xa0) |
|
267 { |
|
268 return CCnvCharacterSetConverter::EErrorIllFormedInput; |
|
269 } |
|
270 __ASSERT_DEBUG(pointerToPreviousByte<pointerToLastByte, Panic(EPanicBadPointers28)); |
|
271 ++pointerToPreviousByte; |
|
272 __ASSERT_DEBUG(pointerToPreviousByte<=pointerToLastByte, Panic(EPanicBadPointers29)); |
|
273 if (pointerToPreviousByte>=pointerToLastByte) |
|
274 { |
|
275 break; |
|
276 } |
|
277 } |
|
278 return (pointerToPreviousByte+1)-aDescriptor.Ptr(); |
|
279 } |
|
280 |
|
281 void DummyConvertToIntermediateBufferInPlace(TDes8&) |
|
282 { |
|
283 } |
|
284 |
|
285 /** |
|
286 Converts to JISX0212 |
|
287 @internalTechnology |
|
288 */ |
|
289 void ConvertToJisX0212FromEucJpPackedInPlace(TDes8& aDescriptor) |
|
290 { |
|
291 const TInt descriptorLength=aDescriptor.Length(); |
|
292 __ASSERT_DEBUG(descriptorLength>0, Panic(EPanicNothingToConvert6)); |
|
293 __ASSERT_DEBUG(descriptorLength%3==0, Panic(EPanicNumberOfBytesIsNotMultipleOfThree2)); |
|
294 TUint8* pointerToTargetByte=CONST_CAST(TUint8*, aDescriptor.Ptr()); |
|
295 const TUint8* pointerToSourceByte=pointerToTargetByte; |
|
296 const TUint8* const pointerToLastByte=pointerToSourceByte+(descriptorLength-1); |
|
297 FOREVER |
|
298 { |
|
299 __ASSERT_DEBUG(*pointerToSourceByte==KSingleShift3, Panic(EPanicSingleShift3Expected)); |
|
300 __ASSERT_DEBUG(pointerToSourceByte<pointerToLastByte, Panic(EPanicBadPointers33)); |
|
301 ++pointerToSourceByte; |
|
302 TUint sourceByte=*pointerToSourceByte; |
|
303 __ASSERT_DEBUG(sourceByte&0x80, Panic(EPanicBadHighBit6)); |
|
304 *pointerToTargetByte=STATIC_CAST(TUint8, sourceByte&~0x80); |
|
305 __ASSERT_DEBUG(pointerToSourceByte<pointerToLastByte, Panic(EPanicBadPointers34)); |
|
306 ++pointerToSourceByte; |
|
307 sourceByte=*pointerToSourceByte; |
|
308 __ASSERT_DEBUG(sourceByte&0x80, Panic(EPanicBadHighBit7)); |
|
309 __ASSERT_DEBUG(pointerToTargetByte<pointerToLastByte, Panic(EPanicBadPointers35)); |
|
310 ++pointerToTargetByte; |
|
311 *pointerToTargetByte=STATIC_CAST(TUint8, sourceByte&~0x80); |
|
312 __ASSERT_DEBUG(pointerToSourceByte<=pointerToLastByte, Panic(EPanicBadPointers36)); |
|
313 if (pointerToSourceByte>=pointerToLastByte) |
|
314 { |
|
315 break; |
|
316 } |
|
317 ++pointerToSourceByte; |
|
318 ++pointerToTargetByte; |
|
319 } |
|
320 aDescriptor.SetLength((descriptorLength/3)*2); |
|
321 } |
|
322 |
|
323 /** |
|
324 Converts to JISX0208 |
|
325 @internalTechnology |
|
326 */ |
|
327 void ConvertToJisX0208FromEucJpPackedInPlace(TDes8& aDescriptor) |
|
328 { |
|
329 const TInt descriptorLength=aDescriptor.Length(); |
|
330 __ASSERT_DEBUG(descriptorLength>0, Panic(EPanicNothingToConvert4)); |
|
331 __ASSERT_DEBUG(descriptorLength%2==0, Panic(EPanicOddNumberOfBytes5)); |
|
332 TUint8* pointerToCurrentByte=CONST_CAST(TUint8*, aDescriptor.Ptr()); |
|
333 const TUint8* const pointerToLastByte=pointerToCurrentByte+(descriptorLength-1); |
|
334 FOREVER |
|
335 { |
|
336 const TUint currentByte=*pointerToCurrentByte; |
|
337 __ASSERT_DEBUG(currentByte&0x80, Panic(EPanicBadHighBit4)); |
|
338 *pointerToCurrentByte=STATIC_CAST(TUint8, currentByte&~0x80); |
|
339 __ASSERT_DEBUG(pointerToCurrentByte<=pointerToLastByte, Panic(EPanicBadPointers30)); |
|
340 if (pointerToCurrentByte>=pointerToLastByte) |
|
341 { |
|
342 break; |
|
343 } |
|
344 ++pointerToCurrentByte; |
|
345 } |
|
346 } |
|
347 |
|
348 /** |
|
349 Converts to Half Width Katakana |
|
350 @internalTechnology |
|
351 */ |
|
352 void ConvertToHalfWidthKatakana8FromEucJpPackedInPlace(TDes8& aDescriptor) |
|
353 { |
|
354 const TInt descriptorLength=aDescriptor.Length(); |
|
355 __ASSERT_DEBUG(descriptorLength>0, Panic(EPanicNothingToConvert5)); |
|
356 __ASSERT_DEBUG(descriptorLength%2==0, Panic(EPanicOddNumberOfBytes6)); |
|
357 TUint8* pointerToTargetByte=CONST_CAST(TUint8*, aDescriptor.Ptr()); |
|
358 const TUint8* pointerToSourceByte=pointerToTargetByte; |
|
359 const TUint8* const pointerToLastByte=pointerToSourceByte+(descriptorLength-1); |
|
360 FOREVER |
|
361 { |
|
362 __ASSERT_DEBUG(*pointerToSourceByte==KSingleShift2, Panic(EPanicSingleShift2Expected)); |
|
363 __ASSERT_DEBUG(pointerToSourceByte<pointerToLastByte, Panic(EPanicBadPointers31)); |
|
364 ++pointerToSourceByte; |
|
365 const TUint sourceByte=*pointerToSourceByte; |
|
366 __ASSERT_DEBUG(sourceByte&0x80, Panic(EPanicBadHighBit5)); |
|
367 *pointerToTargetByte=STATIC_CAST(TUint8, sourceByte); |
|
368 __ASSERT_DEBUG(pointerToSourceByte<=pointerToLastByte, Panic(EPanicBadPointers32)); |
|
369 if (pointerToSourceByte>=pointerToLastByte) |
|
370 { |
|
371 break; |
|
372 } |
|
373 ++pointerToSourceByte; |
|
374 ++pointerToTargetByte; |
|
375 } |
|
376 aDescriptor.SetLength(descriptorLength/2); |
|
377 } |
|
378 |
|
379 |