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