RSA sync with private
[platform/core/messaging/msg-service.git] / utils / MsgTextConvert.cpp
1 /*
2 * Copyright 2012  Samsung Electronics Co., Ltd
3 *
4 * Licensed under the Flora License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *    http://www.tizenopensource.org/license
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <glib.h>
18
19 #include "MsgDebug.h"
20 #include "MsgTextConvert.h"
21
22
23 /*==================================================================================================
24                                      IMPLEMENTATION OF MsgConvertText - Member Functions
25 ==================================================================================================*/
26
27 MsgTextConvert::MsgTextConvert()
28 {
29         extCharList.clear();
30         ucs2toGSM7DefList.clear();
31         ucs2toGSM7ExtList.clear();
32         ucs2toTurkishList.clear();
33         ucs2toSpanishList.clear();
34         ucs2toPortuList.clear();
35
36         extCharList[0x000C] = MSG_CHAR_GSM7EXT;
37         extCharList[0x005B] = MSG_CHAR_GSM7EXT;
38         extCharList[0x005C] = MSG_CHAR_GSM7EXT;
39         extCharList[0x005D] = MSG_CHAR_GSM7EXT;
40         extCharList[0x005E] = MSG_CHAR_GSM7EXT;
41         extCharList[0x007B] = MSG_CHAR_GSM7EXT;
42         extCharList[0x007C] = MSG_CHAR_GSM7EXT;
43         extCharList[0x007D] = MSG_CHAR_GSM7EXT;
44         extCharList[0x007E] = MSG_CHAR_GSM7EXT;
45         extCharList[0x20AC] = MSG_CHAR_GSM7EXT; // ��
46
47         extCharList[0x00E7] = MSG_CHAR_TURKISH;
48         extCharList[0x011E] = MSG_CHAR_TURKISH;
49         extCharList[0x011F] = MSG_CHAR_TURKISH;
50         extCharList[0x01E6] = MSG_CHAR_TURKISH;
51         extCharList[0x01E7] = MSG_CHAR_TURKISH;
52         extCharList[0x0130] = MSG_CHAR_TURKISH;
53         extCharList[0x0131] = MSG_CHAR_TURKISH;
54         extCharList[0x015E] = MSG_CHAR_TURKISH;
55         extCharList[0x015F] = MSG_CHAR_TURKISH;
56
57         extCharList[0x00C1] = MSG_CHAR_SPANISH;
58         extCharList[0x00E1] = MSG_CHAR_SPANISH;
59         extCharList[0x00CD] = MSG_CHAR_SPANISH;
60         extCharList[0x00ED] = MSG_CHAR_SPANISH;
61         extCharList[0x00D3] = MSG_CHAR_SPANISH;
62         extCharList[0x00F3] = MSG_CHAR_SPANISH;
63         extCharList[0x00DA] = MSG_CHAR_SPANISH;
64         extCharList[0x00FA] = MSG_CHAR_SPANISH;
65
66         extCharList[0x00D4] = MSG_CHAR_PORTUGUESE;
67         extCharList[0x00F4] = MSG_CHAR_PORTUGUESE;
68         extCharList[0x00CA] = MSG_CHAR_PORTUGUESE;
69         extCharList[0x00EA] = MSG_CHAR_PORTUGUESE;
70         extCharList[0x00C0] = MSG_CHAR_PORTUGUESE;
71         extCharList[0x00E7] = MSG_CHAR_PORTUGUESE;
72         extCharList[0x00C3] = MSG_CHAR_PORTUGUESE;
73         extCharList[0x00E3] = MSG_CHAR_PORTUGUESE;
74         extCharList[0x00D5] = MSG_CHAR_PORTUGUESE;
75         extCharList[0x00F5] = MSG_CHAR_PORTUGUESE;
76         extCharList[0x00C2] = MSG_CHAR_PORTUGUESE;
77         extCharList[0x00E2] = MSG_CHAR_PORTUGUESE;
78
79         for (unsigned char i = 0; i < 128; i++)
80         {
81                 ucs2toGSM7DefList[g_GSM7BitToUCS2[i]] = i;
82         }
83
84         // GSM 7 bit Extension
85         ucs2toGSM7ExtList[0x005B] = 0x3C; // [
86         ucs2toGSM7ExtList[0x005D] = 0x3E; // ]
87         ucs2toGSM7ExtList[0x007B] = 0x28; // {
88         ucs2toGSM7ExtList[0x007D] = 0x29; // }
89         ucs2toGSM7ExtList[0x000C] = 0x0A; // Page Break
90         ucs2toGSM7ExtList[0x005C] = 0x2F; /* \ */
91         ucs2toGSM7ExtList[0x005E] = 0x14; // ^
92         ucs2toGSM7ExtList[0x007C] = 0x40; // |
93         ucs2toGSM7ExtList[0x007E] = 0x3D; // ~
94         ucs2toGSM7ExtList[0x20AC] = 0x65; // ��
95
96         // Turkish
97         ucs2toTurkishList[0x005B] = 0x3C; // [
98         ucs2toTurkishList[0x005D] = 0x3E; // ]
99         ucs2toTurkishList[0x007B] = 0x28; // {
100         ucs2toTurkishList[0x007D] = 0x29; // }
101         ucs2toTurkishList[0x000C] = 0x0A; // Page Break
102         ucs2toTurkishList[0x005C] = 0x2F; /* \ */
103         ucs2toTurkishList[0x005E] = 0x14; // ^
104         ucs2toTurkishList[0x007C] = 0x40; // |
105         ucs2toTurkishList[0x007E] = 0x3D; // ~
106         ucs2toTurkishList[0x20AC] = 0x65; // ��
107         ucs2toTurkishList[0x00E7] = 0x63; // c LATIN SMALL LETTER S WITH CEDILLA *
108         ucs2toTurkishList[0x011E] = 0x47; // G LATIN CAPITAL LETTER G WITH BREVE
109         ucs2toTurkishList[0x011F] = 0x67; // g LATIN SMALL LETTER G WITH BREVE
110         ucs2toTurkishList[0x01E6] = 0x47; // G LATIN CAPITAL LETTER G WITH CARON
111         ucs2toTurkishList[0x01E7] = 0x67; // g LATIN SMALL LETTER G WITH CARON
112         ucs2toTurkishList[0x0130] = 0x49; // I LATIN CAPITAL LETTER I WITH DOT ABOVE
113         ucs2toTurkishList[0x0131] = 0x69; // i LATIN SMALL LETTER DOTLESS
114         ucs2toTurkishList[0x015E] = 0x53; // S LATIN CAPITAL LETTER S WITH CEDILLA *
115         ucs2toTurkishList[0x015F] = 0x73; // s LATIN SMALL LETTER S WITH CEDILLA *
116
117         // Spanish
118         ucs2toSpanishList[0x005B] = 0x3C; // [
119         ucs2toSpanishList[0x005D] = 0x3E; // ]
120         ucs2toSpanishList[0x007B] = 0x28; // {
121         ucs2toSpanishList[0x007D] = 0x29; // }
122         ucs2toSpanishList[0x000C] = 0x0A; // Page Break
123         ucs2toSpanishList[0x005C] = 0x2F; /* \ */
124         ucs2toSpanishList[0x005E] = 0x14; // ^
125         ucs2toSpanishList[0x007C] = 0x40; // |
126         ucs2toSpanishList[0x007E] = 0x3D; // ~
127         ucs2toSpanishList[0x20AC] = 0x65; // ��
128         ucs2toSpanishList[0x00C1] = 0x41; // A
129         ucs2toSpanishList[0x00E1] = 0x61; // a
130         ucs2toSpanishList[0x00CD] = 0x49; // I
131         ucs2toSpanishList[0x00ED] = 0x69; // i
132         ucs2toSpanishList[0x00D3] = 0x4F; // O
133         ucs2toSpanishList[0x00F3] = 0x6F; // o
134         ucs2toSpanishList[0x00DA] = 0x55; // U
135         ucs2toSpanishList[0x00FA] = 0x75; // u
136
137         // Portuguese
138         ucs2toPortuList[0x005B] = 0x3C; // [
139         ucs2toPortuList[0x005D] = 0x3E; // ]
140         ucs2toPortuList[0x007B] = 0x28; // {
141         ucs2toPortuList[0x007D] = 0x29; // }
142         ucs2toPortuList[0x000C] = 0x0A; // Page Break
143         ucs2toPortuList[0x005C] = 0x2F; /* \ */
144         ucs2toPortuList[0x005E] = 0x14; // ^
145         ucs2toPortuList[0x007C] = 0x40; // |
146         ucs2toPortuList[0x007E] = 0x3D; // ~
147         ucs2toPortuList[0x20AC] = 0x65; // ��
148         ucs2toPortuList[0x00D4] = 0x0B; // O
149         ucs2toPortuList[0x00F4] = 0x0C; // o
150         ucs2toPortuList[0x00C1] = 0x0E; // A
151         ucs2toPortuList[0x00E1] = 0x0F; // a
152         ucs2toPortuList[0x00CA] = 0x1F; // E
153         ucs2toPortuList[0x00EA] = 0x05; // e
154         ucs2toPortuList[0x00C0] = 0x41; // A
155         ucs2toPortuList[0x00E7] = 0x09; // c
156         ucs2toPortuList[0x00CD] = 0x49; // I
157         ucs2toPortuList[0x00ED] = 0x69; // i
158         ucs2toPortuList[0x00D3] = 0x4F; // O
159         ucs2toPortuList[0x00F3] = 0x6F; // o
160         ucs2toPortuList[0x00DA] = 0x55; // U
161         ucs2toPortuList[0x00FA] = 0x75; // u
162         ucs2toPortuList[0x00C3] = 0x61; // A
163         ucs2toPortuList[0x00E3] = 0x7B; // a
164         ucs2toPortuList[0x00D5] = 0x5C; // O
165         ucs2toPortuList[0x00F5] = 0x7C; // o
166         ucs2toPortuList[0x00C2] = 0x61; // A
167         ucs2toPortuList[0x00E2] = 0x7F; // a
168         ucs2toPortuList[0x03A6] = 0x12; // ��
169         ucs2toPortuList[0x0393] = 0x13; // ��
170         ucs2toPortuList[0x03A9] = 0x15; // ��
171         ucs2toPortuList[0x03A0] = 0x16; // ��
172         ucs2toPortuList[0x03A8] = 0x17; // ��
173         ucs2toPortuList[0x03A3] = 0x18; // ��
174         ucs2toPortuList[0x0398] = 0x19; // ��
175 }
176
177
178 MsgTextConvert::~MsgTextConvert()
179 {
180         extCharList.clear();
181         ucs2toGSM7DefList.clear();
182         ucs2toGSM7ExtList.clear();
183         ucs2toTurkishList.clear();
184         ucs2toSpanishList.clear();
185         ucs2toPortuList.clear();
186 }
187
188
189
190 int MsgTextConvert::convertUTF8ToGSM7bit(OUT unsigned char *pDestText, IN int maxLength,  IN const unsigned char *pSrcText, IN int srcTextLen, OUT MSG_LANGUAGE_ID_T *pLangId, OUT bool *bIncludeAbnormalChar)
191 {
192         int utf8Length = 0;
193         int gsm7bitLength = 0;
194         int ucs2Length = 0;
195
196         if (srcTextLen <= 0)
197         {
198                 utf8Length = strlen((char*)pSrcText);
199                 srcTextLen = utf8Length;
200         }
201         else
202         {
203                 utf8Length = srcTextLen;
204         }
205
206         int maxUCS2Length = utf8Length;         // max # of UCS2 chars, NOT bytes. when all utf8 chars are only one byte, UCS2Length is maxUCS2 Length. otherwise (ex: 2 bytes of UTF8 is one char) UCS2Length must be  less than utf8Length
207         WCHAR pUCS2Text[maxUCS2Length];
208         memset(pUCS2Text, 0x00, maxUCS2Length * sizeof(WCHAR));
209
210         MSG_DEBUG("srcTextLen = %d", srcTextLen);
211         MSG_DEBUG("temp buffer size = %d", maxUCS2Length * sizeof(WCHAR));
212         MSG_DEBUG("max dest Length = %d", maxLength);
213
214         ucs2Length = convertUTF8ToUCS2((unsigned char*)pUCS2Text, maxUCS2Length * sizeof(WCHAR), pSrcText, srcTextLen);
215         gsm7bitLength = convertUCS2ToGSM7bit(pDestText, maxLength, (unsigned char*)pUCS2Text, ucs2Length, pLangId, bIncludeAbnormalChar);
216
217         return gsm7bitLength;
218 }
219
220
221 /**
222         if srcTextLen ispSrcText should be null terminated
223 return :
224                 byte length of converted UCS2 characters
225                         -1 : converting error
226 */
227 int MsgTextConvert::convertUTF8ToUCS2(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen)
228 {
229         int i, j;
230         int textLen;
231         unsigned char *unicodeTemp = (unsigned char*)pDestText;
232         int ucs2Length = 0;
233         int remainedBuffer = maxLength;
234
235 #ifdef CONVERT_DUMP
236         int srcLen = srcTextLen;
237         const unsigned char * pTempSrcText = pSrcText;
238         const unsigned char * pTempDestText = pDestText;
239 #endif
240
241         i = j = 0;
242
243         if(maxLength == 0 || pSrcText == NULL || pDestText ==  NULL)
244         {
245                 MSG_DEBUG("UTF8 to UCS2 Failed as text length is 0\n");
246                 return -1;
247         }
248
249         // null terminated string
250         if ( srcTextLen == -1 )
251         {
252                 textLen = strlen((char*)pSrcText);
253                 srcTextLen = textLen;
254         }
255         else
256         {
257                 textLen = srcTextLen;
258         }
259
260         GIConv cd;
261         int err=0;
262
263         cd = g_iconv_open("UCS-2BE", "UTF8");
264
265         if (cd > 0)
266         {
267                 err = g_iconv(cd, (char**)&pSrcText, (gsize*)&textLen, (char**)&unicodeTemp, (gsize*)&remainedBuffer);
268         }
269
270         if(err < 0)
271         {
272                 MSG_DEBUG("Error in g_iconv.");
273                 ucs2Length = -1;
274         }
275         else
276         {
277                 ucs2Length = maxLength - remainedBuffer;
278         }
279
280 #ifdef CONVERT_DUMP
281         MSG_DEBUG("\n########## Dump UTF8 -> UCS2\n");
282         convertDumpTextToHex((unsigned char*)pTempSrcText, srcLen);
283         convertDumpTextToHex((unsigned char*)pTempDestText, ucs2Length);
284 #endif
285
286         g_iconv_close(cd);
287
288         return ucs2Length;
289 }
290
291
292 int MsgTextConvert::convertUTF8ToAuto(OUT unsigned char *pDestText, IN int maxLength,  IN const unsigned char *pSrcText, IN int srcTextLen, OUT msg_encode_type_t *pCharType)
293 {
294         int utf8Length = 0;
295         int gsm7bitLength = 0;
296         int ucs2Length = 0;
297
298         bool bUnknown = false;
299
300         utf8Length = srcTextLen;
301
302         int maxUCS2Length = utf8Length;         // max # of UCS2 chars, NOT bytes. when all utf8 chars are only one byte, UCS2Length is maxUCS2 Length. otherwise (ex: 2 bytes of UTF8 is one char) UCS2Length must be  less than utf8Length
303         WCHAR pUCS2Text[maxUCS2Length];
304         memset(pUCS2Text, 0x00, maxUCS2Length * sizeof(WCHAR));
305
306         MSG_DEBUG("srcTextLen = %d", srcTextLen);
307         MSG_DEBUG("temp buffer size = %d", maxUCS2Length * sizeof(WCHAR));
308         MSG_DEBUG("max dest Length = %d", maxLength);
309
310         ucs2Length = convertUTF8ToUCS2((unsigned char*)pUCS2Text, maxUCS2Length * sizeof(WCHAR), pSrcText, srcTextLen);
311
312         if(ucs2Length < 0)
313         {
314                 *pCharType = MSG_ENCODE_8BIT;
315
316                 memcpy(pDestText, pSrcText, srcTextLen);
317                 return srcTextLen;
318         }
319         else
320         {
321                 gsm7bitLength = convertUCS2ToGSM7bitAuto(pDestText, maxLength, (unsigned char*)pUCS2Text, ucs2Length, &bUnknown);
322
323                 if (bUnknown == true)
324                 {
325                         *pCharType = MSG_ENCODE_UCS2;
326
327                         if (ucs2Length > 0)
328                                 memcpy(pDestText, pUCS2Text, ucs2Length);
329
330                         return ucs2Length;
331                 }
332                 else
333                 {
334                         *pCharType = MSG_ENCODE_GSM7BIT;
335                 }
336
337                 return gsm7bitLength;
338         }
339 }
340
341
342 /**
343 return:
344                 bytelength of UTF8 text
345 */
346 int MsgTextConvert::convertGSM7bitToUTF8(OUT unsigned char *pDestText, IN int maxLength,  IN const unsigned char *pSrcText, IN int srcTextLen, IN MSG_LANG_INFO_S *pLangInfo)
347 {
348         int utf8Length = 0;
349         int ucs2Length = 0;
350         int maxUCS2Length = srcTextLen;         // max # of UCS2 chars, NOT bytes. when all gsm7 chars are only one byte(-there is no extenstion), UCS2Length is maxUCS2 Length. otherwise(ex: gsm7 char starts with 0x1b) UCS2Length must be less than gsm7 legnth
351
352         WCHAR pUCS2Text[maxUCS2Length];
353         memset(pUCS2Text, 0x00, maxUCS2Length * sizeof(WCHAR));
354
355         MSG_DEBUG("srcTextLen = %d\n", srcTextLen);
356         MSG_DEBUG("max dest Length = %d\n", maxLength);
357
358         ucs2Length = convertGSM7bitToUCS2((unsigned char*)pUCS2Text, maxUCS2Length * sizeof(WCHAR), pSrcText, srcTextLen, pLangInfo);
359         utf8Length = convertUCS2ToUTF8(pDestText, maxLength, (unsigned char*)pUCS2Text, ucs2Length);
360
361         return utf8Length;
362 }
363
364
365 /**
366 args:
367         OUT unsigned char *pDestText
368         IN int maxLength                : max byte length of destination text
369         IN const unsigned char *pSrcText
370         IN  int srcTextLen              : byte length of UCS2 source text
371 return :
372                 byte length of converted UTF8 characters
373                         -1 : The alpha isn't the gsm 7bit code
374 */
375 int MsgTextConvert::convertUCS2ToUTF8(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN  int srcTextLen)
376 {
377         int remainedBuffer = maxLength;
378         int utf8Length;
379
380 #ifdef CONVERT_DUMP
381         int srcLen = srcTextLen;
382         const unsigned char * pTempSrcText = pSrcText;
383 #endif
384         unsigned char * pTempDestText = pDestText;
385
386         if (srcTextLen == 0 || pSrcText == NULL || pDestText ==  NULL || maxLength == 0)
387         {
388                 MSG_DEBUG("UCS2 to UTF8 Failed as text length is 0\n");
389                 return false;
390         }
391
392         GIConv cd;
393         int err=0;
394
395         cd = g_iconv_open( "UTF8", "UCS-2BE" );
396
397         if (cd > 0)
398         {
399                 err = g_iconv(cd, (char**)&pSrcText, (gsize*)&srcTextLen, (char**)&pDestText, (gsize*)&remainedBuffer);
400         }
401
402         utf8Length = maxLength - remainedBuffer;
403         pTempDestText[utf8Length] = 0x00;
404
405 #ifdef CONVERT_DUMP
406         MSG_DEBUG("\n########## Dump UCS2 -> UTF8\n");
407         convertDumpTextToHex((unsigned char*)pTempSrcText, srcLen);
408         convertDumpTextToHex((unsigned char*)pTempDestText, utf8Length);
409 #endif
410
411         g_iconv_close(cd);
412
413         return utf8Length;
414 }
415
416
417 int MsgTextConvert::convertEUCKRToUTF8(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN  int srcTextLen)
418 {
419         int remainedBuffer = maxLength;
420         int utf8Length;
421
422 #ifdef CONVERT_DUMP
423         int srcLen = srcTextLen;
424         const unsigned char * pTempSrcText = pSrcText;
425 #endif
426         unsigned char * pTempDestText = pDestText;
427
428         if(srcTextLen == 0 || pSrcText == NULL || pDestText ==  NULL || maxLength == 0)
429         {
430                 MSG_DEBUG("EUCKR to UTF8 Failed as text length is 0\n");
431                 return false;
432         }
433
434         GIConv cd;
435         int err=0;
436
437         cd = g_iconv_open( "UTF8", "EUCKR" );
438
439         if (cd > 0)
440         {
441                 err = g_iconv(cd, (char**)&pSrcText, (gsize*)&srcTextLen, (char**)&pDestText, (gsize*)&remainedBuffer);
442         }
443
444         utf8Length = maxLength - remainedBuffer;
445         pTempDestText[utf8Length] = 0x00;
446
447 #ifdef CONVERT_DUMP
448         MSG_DEBUG("\n########## Dump EUCKR -> UTF8\n");
449         convertDumpTextToHex((unsigned char*)pTempSrcText, srcLen);
450         convertDumpTextToHex((unsigned char*)pTempDestText, utf8Length);
451 #endif
452
453         g_iconv_close(cd);
454
455         return utf8Length;
456 }
457
458
459 /**
460
461 args:
462                 unsigned char *pDestText
463                 int maxLength                           : max destination buffer size
464                 const unsigned char *pSrcText
465                 int srcTextLen                          : BYTE length of src text (UCS2)
466 return:
467                 bytelength of gsm7bit text
468                 -1 : converting error
469 */
470 int MsgTextConvert::convertUCS2ToGSM7bit(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen, OUT MSG_LANGUAGE_ID_T *pLangId, OUT bool *bIncludeAbnormalChar)
471 {
472         // for UNICODE
473         int outTextLen = 0;
474         unsigned char lowerByte, upperByte;
475
476         if (srcTextLen == 0 || pSrcText == NULL || pDestText ==  NULL || maxLength == 0)
477         {
478                 MSG_DEBUG("UCS2 to GSM7bit Failed as text length is 0\n");
479                 return -1;
480         }
481
482         std::map<unsigned short, unsigned char>::iterator itChar;
483         std::map<unsigned short, unsigned char>::iterator itExt;
484
485         MSG_CHAR_TYPE_T currType = MSG_CHAR_DEFAULT;
486         MSG_CHAR_TYPE_T newType = MSG_CHAR_DEFAULT;
487
488         unsigned short inText;
489
490         // Get Language Type by checking each character
491         for (int index = 0; index < srcTextLen; index++)
492         {
493                 upperByte = pSrcText[index++];
494                 lowerByte = pSrcText[index];
495
496                 inText = (upperByte << 8) & 0xFF00;
497
498                 inText = inText | lowerByte;
499
500 //MSG_DEBUG("inText : [%04x]", inText);
501
502                 itExt = extCharList.find(inText);
503
504                 if (itExt != extCharList.end())
505                 {
506                         newType = (MSG_CHAR_TYPE_T)itExt->second;
507
508                         if (newType >= currType)
509                         {
510                                 currType = newType;
511                         }
512                 }
513         }
514
515 MSG_DEBUG("charType : [%d]", currType);
516
517         for (int index = 0; index < srcTextLen; index++)
518         {
519                 upperByte = pSrcText[index++];
520                 lowerByte = pSrcText[index];
521
522                 inText = (upperByte << 8) & 0xFF00;
523                 inText = inText | lowerByte;
524
525 MSG_DEBUG("inText : [%04x]", inText);
526
527                 // Check Default Char
528                 itChar = ucs2toGSM7DefList.find(inText);
529
530                 if (itChar != ucs2toGSM7DefList.end())
531                 {
532 MSG_DEBUG("default char");
533                         pDestText[outTextLen++] = (unsigned char)itChar->second;
534                 }
535                 else
536                 {
537                         if (currType == MSG_CHAR_GSM7EXT)
538                         {
539                                 itExt = ucs2toGSM7ExtList.find(inText);
540
541                                 if (itExt != ucs2toGSM7ExtList.end())
542                                 {
543                                         // prevent buffer overflow
544                                         if (maxLength <= outTextLen + 1)
545                                         {
546                                                 MSG_DEBUG("Buffer Full");
547                                                 break;
548                                         }
549
550                                         pDestText[outTextLen++] = 0x1B;
551                                         pDestText[outTextLen++] = (unsigned char)itExt->second;
552                                 }
553                                 else
554                                 {
555                                         pDestText[outTextLen++] = 0x20;
556                                         *bIncludeAbnormalChar = true;
557                                 }
558                         }
559                         else if (currType == MSG_CHAR_TURKISH)
560                         {
561                                 *pLangId = MSG_LANG_ID_TURKISH;
562
563                                 itExt = ucs2toTurkishList.find(inText);
564
565                                 if (itExt != ucs2toTurkishList.end())
566                                 {
567                                         // prevent buffer overflow
568                                         if (maxLength <= outTextLen + 1)
569                                         {
570                                                 MSG_DEBUG("Buffer Full");
571                                                 break;
572                                         }
573
574                                         pDestText[outTextLen++] = 0x1B;
575                                         pDestText[outTextLen++] = (unsigned char)itExt->second;
576                                 }
577                                 else
578                                 {
579                                         pDestText[outTextLen++] = 0x20;
580                                         *bIncludeAbnormalChar = true;
581                                 }
582                         }
583                         else if (currType == MSG_CHAR_SPANISH)
584                         {
585                                 *pLangId = MSG_LANG_ID_SPANISH;
586
587                                 itExt = ucs2toSpanishList.find(inText);
588
589                                 if (itExt != ucs2toSpanishList.end())
590                                 {
591                                         // prevent buffer overflow
592                                         if (maxLength <= outTextLen + 1)
593                                         {
594                                                 MSG_DEBUG("Buffer Full");
595                                                 break;
596                                         }
597
598                                         pDestText[outTextLen++] = 0x1B;
599                                         pDestText[outTextLen++] = (unsigned char)itExt->second;
600                                 }
601                                 else
602                                 {
603                                         pDestText[outTextLen++] = 0x20;
604                                         *bIncludeAbnormalChar = true;
605                                 }
606                         }
607                         else if (currType == MSG_CHAR_PORTUGUESE)
608                         {
609                                 *pLangId = MSG_LANG_ID_PORTUGUESE;
610
611                                 itExt = ucs2toPortuList.find(inText);
612
613                                 if (itExt != ucs2toPortuList.end())
614                                 {
615                                         // prevent buffer overflow
616                                         if (maxLength <= outTextLen + 1)
617                                         {
618                                                 MSG_DEBUG("Buffer Full");
619                                                 break;
620                                         }
621
622 MSG_DEBUG("ucs2toPortuList : [%02x]", (unsigned char)itExt->second);
623
624                                         pDestText[outTextLen++] = 0x1B;
625                                         pDestText[outTextLen++] = (unsigned char)itExt->second;
626                                 }
627                                 else
628                                 {
629 MSG_DEBUG("no char");
630                                         pDestText[outTextLen++] = 0x20;
631                                         *bIncludeAbnormalChar = true;
632                                 }
633                         }
634                         else
635                         {
636                                 pDestText[outTextLen++] = 0x20;
637                                 *bIncludeAbnormalChar = true;
638                         }
639                 }
640
641                 // prevent buffer overflow
642                 if (maxLength <= outTextLen)
643                 {
644                         MSG_DEBUG("Buffer full\n");
645                         break;
646                 }
647         }
648
649 #ifdef CONVERT_DUMP
650         MSG_DEBUG("\n########## Dump UCS2 -> GSM7bit\n");
651         convertDumpTextToHex((unsigned char*)pSrcText, srcTextLen);
652         convertDumpTextToHex((unsigned char*)pDestText, outTextLen);
653 #endif
654
655         return outTextLen;
656 }
657
658
659 int MsgTextConvert::convertUCS2ToGSM7bitAuto(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen, OUT bool *pUnknown)
660 {
661         // for UNICODE
662         int outTextLen = 0;
663         unsigned char lowerByte, upperByte;
664
665         if (srcTextLen == 0 || pSrcText == NULL || pDestText ==  NULL || maxLength == 0)
666         {
667                 MSG_DEBUG("UCS2 to GSM7bit Failed as text length is 0\n");
668                 return -1;
669         }
670
671         std::map<unsigned short, unsigned char>::iterator itChar;
672         std::map<unsigned short, unsigned char>::iterator itExt;
673
674         unsigned short inText;
675
676         for (int index = 0; index < srcTextLen; index++)
677         {
678                 upperByte = pSrcText[index++];
679                 lowerByte = pSrcText[index];
680
681                 inText = (upperByte << 8) & 0xFF00;
682                 inText = inText | lowerByte;
683
684 //MSG_DEBUG("inText : [%04x]", inText);
685
686                 // Check Default Char
687                 itChar = ucs2toGSM7DefList.find(inText);
688
689                 if (itChar != ucs2toGSM7DefList.end())
690                 {
691 //MSG_DEBUG("default char");
692                         pDestText[outTextLen++] = (unsigned char)itChar->second;
693                 }
694                 else
695                 {
696                         itExt = ucs2toGSM7ExtList.find(inText);
697
698                         if (itExt != ucs2toGSM7ExtList.end())
699                         {
700                                 // prevent buffer overflow
701                                 if (maxLength <= outTextLen + 1)
702                                 {
703                                         MSG_DEBUG("Buffer Full");
704                                         break;
705                                 }
706
707                                 pDestText[outTextLen++] = 0x1B;
708                                 pDestText[outTextLen++] = (unsigned char)itExt->second;
709                         }
710                         else
711                         {
712                                 *pUnknown = true;
713                                 return 0;
714                         }
715                 }
716
717                 // prevent buffer overflow
718                 if (maxLength <= outTextLen)
719                 {
720                         MSG_DEBUG("Buffer full\n");
721                         break;
722                 }
723         }
724
725 #ifdef CONVERT_DUMP
726         MSG_DEBUG("\n########## Dump UCS2 -> GSM7bit\n");
727         convertDumpTextToHex((unsigned char*)pSrcText, srcTextLen);
728         convertDumpTextToHex((unsigned char*)pDestText, outTextLen);
729 #endif
730
731         return outTextLen;
732 }
733
734
735 /**
736  args :
737                 unsigned char *pDestText                                : destination text (UCS2) - byte order depends on local byte order
738                 const unsigned char *pSrcText           : source text (gsm7bit)
739                 int maxLength                   : max destination buffer size
740                 int srcTextLen                  : byte length of source text (gsm7bit)
741  return :
742                 byte length of converted UCS2 characters
743                         -1 : The alpha isn't the gsm 7bit code
744 */
745 int MsgTextConvert::convertGSM7bitToUCS2(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen, IN MSG_LANG_INFO_S *pLangInfo)
746 {
747         int outTextLen = 0;
748         unsigned char lowerByte = 0, upperByte = 0;
749
750         if (srcTextLen == 0 || pSrcText == NULL || pDestText ==  NULL || maxLength == 0)
751         {
752                 MSG_DEBUG("UCS2 to GSM7bit Failed as text length is 0\n");
753                 return -1;
754         }
755
756         for (int i = 0; i<srcTextLen; i++)
757         {
758                 if (maxLength == 0)
759                 {
760                         break;
761                 }
762
763                 if (pSrcText[i] >= 0x80)
764                 {
765                         //error
766                         MSG_DEBUG(">>>>>>>a_pTextString[i]=%x, The alpha isn't the gsm 7bit code, Never Come here!!!\n", pSrcText[i]);
767                         return -1;
768                 }
769
770                 if (pLangInfo->bLockingShift == true) // National Language Locking Shift
771                 {
772                         MSG_DEBUG("Locking Shift [%d]", pLangInfo->lockingLang);
773
774                         if (pLangInfo->lockingLang == MSG_LANG_ID_TURKISH)
775                         {
776                                 // Check Escape
777                                 if (g_TurkishLockingToUCS2[pSrcText[i]] == 0x001B)
778                                 {
779                                         i++;
780
781                                         if (pLangInfo->bSingleShift == true) // National Language Single Shift
782                                         {
783                                                 MSG_DEBUG("Single Shift [%d]", pLangInfo->singleLang);
784
785                                                 if (pLangInfo->singleLang == MSG_LANG_ID_TURKISH)
786                                                 {
787                                                         lowerByte = g_TurkishSingleToUCS2[pSrcText[i]] & 0x00FF;
788                                                         upperByte = (g_TurkishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
789                                                 }
790                                                 else if (pLangInfo->singleLang == MSG_LANG_ID_SPANISH)
791                                                 {
792                                                         lowerByte = g_SpanishSingleToUCS2[pSrcText[i]] & 0x00FF;
793                                                         upperByte = (g_SpanishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
794                                                 }
795                                                 else if (pLangInfo->singleLang == MSG_LANG_ID_PORTUGUESE)
796                                                 {
797                                                         lowerByte = g_PortuSingleToUCS2[pSrcText[i]] & 0x00FF;
798                                                         upperByte = (g_PortuSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
799                                                 }
800                                                 else
801                                                 {
802                                                         lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
803                                                         upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
804                                                 }
805                                         }
806                                         else // GSM 7 bit Default Alphabet Extension Table
807                                         {
808                                                 lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
809                                                 upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
810                                         }
811                                 }
812                                 else // TURKISH - National Language Locking Shift
813                                 {
814                                         lowerByte = g_TurkishLockingToUCS2[pSrcText[i]] & 0x00FF;
815                                         upperByte = (g_TurkishLockingToUCS2[pSrcText[i]] & 0xFF00) >> 8;
816                                 }
817                         }
818                         else if (pLangInfo->lockingLang == MSG_LANG_ID_PORTUGUESE)
819                         {
820                                 // Check Escape
821                                 if (g_PortuLockingToUCS2[pSrcText[i]] == 0x001B)
822                                 {
823                                         i++;
824
825                                         if (pLangInfo->bSingleShift == true) // National Language Single Shift
826                                         {
827                                                 MSG_DEBUG("Single Shift [%d]", pLangInfo->singleLang);
828
829                                                 if (pLangInfo->singleLang == MSG_LANG_ID_TURKISH)
830                                                 {
831                                                         lowerByte = g_TurkishSingleToUCS2[pSrcText[i]] & 0x00FF;
832                                                         upperByte = (g_TurkishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
833                                                 }
834                                                 else if (pLangInfo->singleLang == MSG_LANG_ID_SPANISH)
835                                                 {
836                                                         lowerByte = g_SpanishSingleToUCS2[pSrcText[i]] & 0x00FF;
837                                                         upperByte = (g_SpanishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
838                                                 }
839                                                 else if (pLangInfo->singleLang == MSG_LANG_ID_PORTUGUESE)
840                                                 {
841                                                         lowerByte = g_PortuSingleToUCS2[pSrcText[i]] & 0x00FF;
842                                                         upperByte = (g_PortuSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
843                                                 }
844                                                 else
845                                                 {
846                                                         lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
847                                                         upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
848                                                 }
849                                         }
850                                         else // GSM 7 bit Default Alphabet Extension Table
851                                         {
852                                                 lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
853                                                 upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
854                                         }
855                                 }
856                                 else // PORTUGUESE - National Language Locking Shift
857                                 {
858                                         lowerByte = g_PortuLockingToUCS2[pSrcText[i]] & 0x00FF;
859                                         upperByte = (g_PortuLockingToUCS2[pSrcText[i]] & 0xFF00) >> 8;
860                                 }
861                         }
862                 }
863                 else
864                 {
865                         // Check Escape
866                         if (g_GSM7BitToUCS2[pSrcText[i]] == 0x001B)
867                         {
868                                 i++;
869
870                                 if (pLangInfo->bSingleShift == true) // National Language Single Shift
871                                 {
872                                         MSG_DEBUG("Single Shift [%d]", pLangInfo->singleLang);
873
874                                         if (pLangInfo->singleLang == MSG_LANG_ID_TURKISH)
875                                         {
876                                                 lowerByte = g_TurkishSingleToUCS2[pSrcText[i]] & 0x00FF;
877                                                 upperByte = (g_TurkishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
878                                         }
879                                         else if (pLangInfo->singleLang == MSG_LANG_ID_SPANISH)
880                                         {
881                                                 lowerByte = g_SpanishSingleToUCS2[pSrcText[i]] & 0x00FF;
882                                                 upperByte = (g_SpanishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
883                                         }
884                                         else if (pLangInfo->singleLang == MSG_LANG_ID_PORTUGUESE)
885                                         {
886                                                 lowerByte = g_PortuSingleToUCS2[pSrcText[i]] & 0x00FF;
887                                                 upperByte = (g_PortuSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
888                                         }
889                                         else
890                                         {
891                                                 lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
892                                                 upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
893                                         }
894                                 }
895                                 else // GSM 7 bit Default Alphabet Extension Table
896                                 {
897                                         lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
898                                         upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
899                                 }
900                         }
901                         else
902                         {
903                                 lowerByte = g_GSM7BitToUCS2[pSrcText[i]] & 0x00FF;
904                                 upperByte = (g_GSM7BitToUCS2[pSrcText[i]] & 0xFF00) >> 8;
905                         }
906                 }
907
908                 pDestText[outTextLen++] = upperByte;
909                 pDestText[outTextLen++] = lowerByte;
910                 maxLength -= 2;
911         }
912
913 #ifdef CONVERT_DUMP
914         MSG_DEBUG("\n########## Dump GSM7bit -> UCS2\n");
915         convertDumpTextToHex((unsigned char*)pSrcText, srcTextLen);
916         convertDumpTextToHex((unsigned char*)pDestText, outTextLen);
917 #endif
918
919         return outTextLen;
920 }
921
922
923 void MsgTextConvert::convertDumpTextToHex(const unsigned char *pText, int length)
924 {
925         printf("\n=======================================\n");
926         printf("   Dump Text To Hex - Length :%d\n", length);
927         printf("=======================================");
928
929         for (int i = 0; i < length; i++ )
930         {
931                 if ( i % MAX_DUMP_COLUMN == 0 )
932                 {
933                         printf("\n\t");
934                 }
935                 printf("%02x ", pText[i]);
936         }
937
938         printf("\n=======================================\n\n");
939 }