2 * Copyright 2012-2013 Samsung Electronics Co., Ltd
4 * Licensed under the Flora License, Version 1.1 (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
8 * http://floralicense.org/license/
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.
20 #include "MsgTextConvert.h"
23 /*==================================================================================================
24 IMPLEMENTATION OF MsgConvertText - Member Functions
25 ==================================================================================================*/
27 MsgTextConvert::MsgTextConvert()
30 ucs2toGSM7DefList.clear();
31 ucs2toGSM7ExtList.clear();
32 ucs2toTurkishList.clear();
33 ucs2toSpanishList.clear();
34 ucs2toPortuList.clear();
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; // ��
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;
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;
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;
79 for (unsigned char i = 0; i < 128; i++)
81 ucs2toGSM7DefList[g_GSM7BitToUCS2[i]] = i;
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; // ��
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 *
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
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; // ��
178 MsgTextConvert::~MsgTextConvert()
181 ucs2toGSM7DefList.clear();
182 ucs2toGSM7ExtList.clear();
183 ucs2toTurkishList.clear();
184 ucs2toSpanishList.clear();
185 ucs2toPortuList.clear();
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)
193 int gsm7bitLength = 0;
198 utf8Length = strlen((char*)pSrcText);
199 srcTextLen = utf8Length;
203 utf8Length = srcTextLen;
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));
210 MSG_DEBUG("srcTextLen = %d", srcTextLen);
211 MSG_DEBUG("temp buffer size = %d", maxUCS2Length * sizeof(WCHAR));
212 MSG_DEBUG("max dest Length = %d", maxLength);
214 ucs2Length = convertUTF8ToUCS2((unsigned char*)pUCS2Text, maxUCS2Length * sizeof(WCHAR), pSrcText, srcTextLen);
215 gsm7bitLength = convertUCS2ToGSM7bit(pDestText, maxLength, (unsigned char*)pUCS2Text, ucs2Length, pLangId, bIncludeAbnormalChar);
217 return gsm7bitLength;
222 if srcTextLen ispSrcText should be null terminated
224 byte length of converted UCS2 characters
225 -1 : converting error
227 int MsgTextConvert::convertUTF8ToUCS2(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen)
231 unsigned char *unicodeTemp = (unsigned char*)pDestText;
233 int remainedBuffer = maxLength;
236 int srcLen = srcTextLen;
237 const unsigned char * pTempSrcText = pSrcText;
238 const unsigned char * pTempDestText = pDestText;
243 if(maxLength == 0 || pSrcText == NULL || pDestText == NULL)
245 MSG_DEBUG("UTF8 to UCS2 Failed as text length is 0\n");
249 // null terminated string
250 if ( srcTextLen == -1 )
252 textLen = strlen((char*)pSrcText);
253 srcTextLen = textLen;
257 textLen = srcTextLen;
263 cd = g_iconv_open("UCS-2BE", "UTF8");
267 err = g_iconv(cd, (char**)&pSrcText, (gsize*)&textLen, (char**)&unicodeTemp, (gsize*)&remainedBuffer);
272 MSG_DEBUG("Error in g_iconv.");
277 ucs2Length = maxLength - remainedBuffer;
281 MSG_DEBUG("\n########## Dump UTF8 -> UCS2\n");
282 convertDumpTextToHex((unsigned char*)pTempSrcText, srcLen);
283 convertDumpTextToHex((unsigned char*)pTempDestText, ucs2Length);
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)
295 int gsm7bitLength = 0;
298 bool bUnknown = false;
300 utf8Length = srcTextLen;
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));
306 MSG_DEBUG("srcTextLen = %d", srcTextLen);
307 MSG_DEBUG("temp buffer size = %d", maxUCS2Length * sizeof(WCHAR));
308 MSG_DEBUG("max dest Length = %d", maxLength);
310 ucs2Length = convertUTF8ToUCS2((unsigned char*)pUCS2Text, maxUCS2Length * sizeof(WCHAR), pSrcText, srcTextLen);
314 *pCharType = MSG_ENCODE_8BIT;
316 memcpy(pDestText, pSrcText, srcTextLen);
321 gsm7bitLength = convertUCS2ToGSM7bitAuto(pDestText, maxLength, (unsigned char*)pUCS2Text, ucs2Length, &bUnknown);
323 if (bUnknown == true)
325 *pCharType = MSG_ENCODE_UCS2;
328 memcpy(pDestText, pUCS2Text, ucs2Length);
334 *pCharType = MSG_ENCODE_GSM7BIT;
337 return gsm7bitLength;
344 bytelength of UTF8 text
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)
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
352 WCHAR pUCS2Text[maxUCS2Length];
353 memset(pUCS2Text, 0x00, maxUCS2Length * sizeof(WCHAR));
355 MSG_DEBUG("srcTextLen = %d\n", srcTextLen);
356 MSG_DEBUG("max dest Length = %d\n", maxLength);
358 ucs2Length = convertGSM7bitToUCS2((unsigned char*)pUCS2Text, maxUCS2Length * sizeof(WCHAR), pSrcText, srcTextLen, pLangInfo);
359 utf8Length = convertUCS2ToUTF8(pDestText, maxLength, (unsigned char*)pUCS2Text, ucs2Length);
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
372 byte length of converted UTF8 characters
373 -1 : The alpha isn't the gsm 7bit code
375 int MsgTextConvert::convertUCS2ToUTF8(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen)
377 int remainedBuffer = maxLength;
381 int srcLen = srcTextLen;
382 const unsigned char * pTempSrcText = pSrcText;
384 unsigned char * pTempDestText = pDestText;
386 if (srcTextLen == 0 || pSrcText == NULL || pDestText == NULL || maxLength == 0)
388 MSG_DEBUG("UCS2 to UTF8 Failed as text length is 0\n");
395 cd = g_iconv_open( "UTF8", "UCS-2BE" );
399 err = g_iconv(cd, (char**)&pSrcText, (gsize*)&srcTextLen, (char**)&pDestText, (gsize*)&remainedBuffer);
402 utf8Length = maxLength - remainedBuffer;
403 pTempDestText[utf8Length] = 0x00;
406 MSG_DEBUG("\n########## Dump UCS2 -> UTF8\n");
407 convertDumpTextToHex((unsigned char*)pTempSrcText, srcLen);
408 convertDumpTextToHex((unsigned char*)pTempDestText, utf8Length);
417 int MsgTextConvert::convertEUCKRToUTF8(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen)
419 int remainedBuffer = maxLength;
423 int srcLen = srcTextLen;
424 const unsigned char * pTempSrcText = pSrcText;
426 unsigned char * pTempDestText = pDestText;
428 if(srcTextLen == 0 || pSrcText == NULL || pDestText == NULL || maxLength == 0)
430 MSG_DEBUG("EUCKR to UTF8 Failed as text length is 0\n");
437 cd = g_iconv_open( "UTF8", "EUCKR" );
441 err = g_iconv(cd, (char**)&pSrcText, (gsize*)&srcTextLen, (char**)&pDestText, (gsize*)&remainedBuffer);
444 utf8Length = maxLength - remainedBuffer;
445 pTempDestText[utf8Length] = 0x00;
448 MSG_DEBUG("\n########## Dump EUCKR -> UTF8\n");
449 convertDumpTextToHex((unsigned char*)pTempSrcText, srcLen);
450 convertDumpTextToHex((unsigned char*)pTempDestText, utf8Length);
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)
467 bytelength of gsm7bit text
468 -1 : converting error
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)
474 unsigned char lowerByte, upperByte;
476 if (srcTextLen == 0 || pSrcText == NULL || pDestText == NULL || maxLength == 0)
478 MSG_DEBUG("UCS2 to GSM7bit Failed as text length is 0\n");
482 std::map<unsigned short, unsigned char>::iterator itChar;
483 std::map<unsigned short, unsigned char>::iterator itExt;
485 MSG_CHAR_TYPE_T currType = MSG_CHAR_DEFAULT;
486 MSG_CHAR_TYPE_T newType = MSG_CHAR_DEFAULT;
488 unsigned short inText;
490 // Get Language Type by checking each character
491 for (int index = 0; index < srcTextLen; index++)
493 upperByte = pSrcText[index++];
494 lowerByte = pSrcText[index];
496 inText = (upperByte << 8) & 0xFF00;
498 inText = inText | lowerByte;
500 //MSG_DEBUG("inText : [%04x]", inText);
502 itExt = extCharList.find(inText);
504 if (itExt != extCharList.end())
506 newType = (MSG_CHAR_TYPE_T)itExt->second;
508 if (newType >= currType)
515 MSG_DEBUG("charType : [%d]", currType);
517 for (int index = 0; index < srcTextLen; index++)
519 upperByte = pSrcText[index++];
520 lowerByte = pSrcText[index];
522 inText = (upperByte << 8) & 0xFF00;
523 inText = inText | lowerByte;
525 MSG_DEBUG("inText : [%04x]", inText);
527 // Check Default Char
528 itChar = ucs2toGSM7DefList.find(inText);
530 if (itChar != ucs2toGSM7DefList.end())
532 MSG_DEBUG("default char");
533 pDestText[outTextLen++] = (unsigned char)itChar->second;
537 if (currType == MSG_CHAR_GSM7EXT)
539 itExt = ucs2toGSM7ExtList.find(inText);
541 if (itExt != ucs2toGSM7ExtList.end())
543 // prevent buffer overflow
544 if (maxLength <= outTextLen + 1)
546 MSG_DEBUG("Buffer Full");
550 pDestText[outTextLen++] = 0x1B;
551 pDestText[outTextLen++] = (unsigned char)itExt->second;
555 pDestText[outTextLen++] = 0x20;
556 *bIncludeAbnormalChar = true;
559 else if (currType == MSG_CHAR_TURKISH)
561 *pLangId = MSG_LANG_ID_TURKISH;
563 itExt = ucs2toTurkishList.find(inText);
565 if (itExt != ucs2toTurkishList.end())
567 // prevent buffer overflow
568 if (maxLength <= outTextLen + 1)
570 MSG_DEBUG("Buffer Full");
574 pDestText[outTextLen++] = 0x1B;
575 pDestText[outTextLen++] = (unsigned char)itExt->second;
579 pDestText[outTextLen++] = 0x20;
580 *bIncludeAbnormalChar = true;
583 else if (currType == MSG_CHAR_SPANISH)
585 *pLangId = MSG_LANG_ID_SPANISH;
587 itExt = ucs2toSpanishList.find(inText);
589 if (itExt != ucs2toSpanishList.end())
591 // prevent buffer overflow
592 if (maxLength <= outTextLen + 1)
594 MSG_DEBUG("Buffer Full");
598 pDestText[outTextLen++] = 0x1B;
599 pDestText[outTextLen++] = (unsigned char)itExt->second;
603 pDestText[outTextLen++] = 0x20;
604 *bIncludeAbnormalChar = true;
607 else if (currType == MSG_CHAR_PORTUGUESE)
609 *pLangId = MSG_LANG_ID_PORTUGUESE;
611 itExt = ucs2toPortuList.find(inText);
613 if (itExt != ucs2toPortuList.end())
615 // prevent buffer overflow
616 if (maxLength <= outTextLen + 1)
618 MSG_DEBUG("Buffer Full");
622 MSG_DEBUG("ucs2toPortuList : [%02x]", (unsigned char)itExt->second);
624 pDestText[outTextLen++] = 0x1B;
625 pDestText[outTextLen++] = (unsigned char)itExt->second;
629 MSG_DEBUG("no char");
630 pDestText[outTextLen++] = 0x20;
631 *bIncludeAbnormalChar = true;
636 pDestText[outTextLen++] = 0x20;
637 *bIncludeAbnormalChar = true;
641 // prevent buffer overflow
642 if (maxLength <= outTextLen)
644 MSG_DEBUG("Buffer full\n");
650 MSG_DEBUG("\n########## Dump UCS2 -> GSM7bit\n");
651 convertDumpTextToHex((unsigned char*)pSrcText, srcTextLen);
652 convertDumpTextToHex((unsigned char*)pDestText, outTextLen);
659 int MsgTextConvert::convertUCS2ToGSM7bitAuto(OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen, OUT bool *pUnknown)
663 unsigned char lowerByte, upperByte;
665 if (srcTextLen == 0 || pSrcText == NULL || pDestText == NULL || maxLength == 0)
667 MSG_DEBUG("UCS2 to GSM7bit Failed as text length is 0\n");
671 std::map<unsigned short, unsigned char>::iterator itChar;
672 std::map<unsigned short, unsigned char>::iterator itExt;
674 unsigned short inText;
676 for (int index = 0; index < srcTextLen; index++)
678 upperByte = pSrcText[index++];
679 lowerByte = pSrcText[index];
681 inText = (upperByte << 8) & 0xFF00;
682 inText = inText | lowerByte;
684 //MSG_DEBUG("inText : [%04x]", inText);
686 // Check Default Char
687 itChar = ucs2toGSM7DefList.find(inText);
689 if (itChar != ucs2toGSM7DefList.end())
691 //MSG_DEBUG("default char");
692 pDestText[outTextLen++] = (unsigned char)itChar->second;
696 itExt = ucs2toGSM7ExtList.find(inText);
698 if (itExt != ucs2toGSM7ExtList.end())
700 // prevent buffer overflow
701 if (maxLength <= outTextLen + 1)
703 MSG_DEBUG("Buffer Full");
707 pDestText[outTextLen++] = 0x1B;
708 pDestText[outTextLen++] = (unsigned char)itExt->second;
717 // prevent buffer overflow
718 if (maxLength <= outTextLen)
720 MSG_DEBUG("Buffer full\n");
726 MSG_DEBUG("\n########## Dump UCS2 -> GSM7bit\n");
727 convertDumpTextToHex((unsigned char*)pSrcText, srcTextLen);
728 convertDumpTextToHex((unsigned char*)pDestText, outTextLen);
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)
742 byte length of converted UCS2 characters
743 -1 : The alpha isn't the gsm 7bit code
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)
748 unsigned char lowerByte = 0, upperByte = 0;
750 if (srcTextLen == 0 || pSrcText == NULL || pDestText == NULL || maxLength == 0)
752 MSG_DEBUG("UCS2 to GSM7bit Failed as text length is 0\n");
756 for (int i = 0; i<srcTextLen; i++)
763 if (pSrcText[i] >= 0x80)
766 MSG_DEBUG(">>>>>>>a_pTextString[i]=%x, The alpha isn't the gsm 7bit code, Never Come here!!!\n", pSrcText[i]);
770 if (pLangInfo->bLockingShift == true) // National Language Locking Shift
772 MSG_DEBUG("Locking Shift [%d]", pLangInfo->lockingLang);
774 if (pLangInfo->lockingLang == MSG_LANG_ID_TURKISH)
777 if (g_TurkishLockingToUCS2[pSrcText[i]] == 0x001B)
781 if (pLangInfo->bSingleShift == true) // National Language Single Shift
783 MSG_DEBUG("Single Shift [%d]", pLangInfo->singleLang);
785 if (pLangInfo->singleLang == MSG_LANG_ID_TURKISH)
787 lowerByte = g_TurkishSingleToUCS2[pSrcText[i]] & 0x00FF;
788 upperByte = (g_TurkishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
790 else if (pLangInfo->singleLang == MSG_LANG_ID_SPANISH)
792 lowerByte = g_SpanishSingleToUCS2[pSrcText[i]] & 0x00FF;
793 upperByte = (g_SpanishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
795 else if (pLangInfo->singleLang == MSG_LANG_ID_PORTUGUESE)
797 lowerByte = g_PortuSingleToUCS2[pSrcText[i]] & 0x00FF;
798 upperByte = (g_PortuSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
802 lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
803 upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
806 else // GSM 7 bit Default Alphabet Extension Table
808 lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
809 upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
812 else // TURKISH - National Language Locking Shift
814 lowerByte = g_TurkishLockingToUCS2[pSrcText[i]] & 0x00FF;
815 upperByte = (g_TurkishLockingToUCS2[pSrcText[i]] & 0xFF00) >> 8;
818 else if (pLangInfo->lockingLang == MSG_LANG_ID_PORTUGUESE)
821 if (g_PortuLockingToUCS2[pSrcText[i]] == 0x001B)
825 if (pLangInfo->bSingleShift == true) // National Language Single Shift
827 MSG_DEBUG("Single Shift [%d]", pLangInfo->singleLang);
829 if (pLangInfo->singleLang == MSG_LANG_ID_TURKISH)
831 lowerByte = g_TurkishSingleToUCS2[pSrcText[i]] & 0x00FF;
832 upperByte = (g_TurkishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
834 else if (pLangInfo->singleLang == MSG_LANG_ID_SPANISH)
836 lowerByte = g_SpanishSingleToUCS2[pSrcText[i]] & 0x00FF;
837 upperByte = (g_SpanishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
839 else if (pLangInfo->singleLang == MSG_LANG_ID_PORTUGUESE)
841 lowerByte = g_PortuSingleToUCS2[pSrcText[i]] & 0x00FF;
842 upperByte = (g_PortuSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
846 lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
847 upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
850 else // GSM 7 bit Default Alphabet Extension Table
852 lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
853 upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
856 else // PORTUGUESE - National Language Locking Shift
858 lowerByte = g_PortuLockingToUCS2[pSrcText[i]] & 0x00FF;
859 upperByte = (g_PortuLockingToUCS2[pSrcText[i]] & 0xFF00) >> 8;
866 if (g_GSM7BitToUCS2[pSrcText[i]] == 0x001B)
870 if (pLangInfo->bSingleShift == true) // National Language Single Shift
872 MSG_DEBUG("Single Shift [%d]", pLangInfo->singleLang);
874 if (pLangInfo->singleLang == MSG_LANG_ID_TURKISH)
876 lowerByte = g_TurkishSingleToUCS2[pSrcText[i]] & 0x00FF;
877 upperByte = (g_TurkishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
879 else if (pLangInfo->singleLang == MSG_LANG_ID_SPANISH)
881 lowerByte = g_SpanishSingleToUCS2[pSrcText[i]] & 0x00FF;
882 upperByte = (g_SpanishSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
884 else if (pLangInfo->singleLang == MSG_LANG_ID_PORTUGUESE)
886 lowerByte = g_PortuSingleToUCS2[pSrcText[i]] & 0x00FF;
887 upperByte = (g_PortuSingleToUCS2[pSrcText[i]] & 0xFF00) >> 8;
891 lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
892 upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
895 else // GSM 7 bit Default Alphabet Extension Table
897 lowerByte = g_GSM7BitExtToUCS2[pSrcText[i]] & 0x00FF;
898 upperByte = (g_GSM7BitExtToUCS2[pSrcText[i]] & 0xFF00) >> 8;
903 lowerByte = g_GSM7BitToUCS2[pSrcText[i]] & 0x00FF;
904 upperByte = (g_GSM7BitToUCS2[pSrcText[i]] & 0xFF00) >> 8;
908 pDestText[outTextLen++] = upperByte;
909 pDestText[outTextLen++] = lowerByte;
914 MSG_DEBUG("\n########## Dump GSM7bit -> UCS2\n");
915 convertDumpTextToHex((unsigned char*)pSrcText, srcTextLen);
916 convertDumpTextToHex((unsigned char*)pDestText, outTextLen);
923 void MsgTextConvert::convertDumpTextToHex(const unsigned char *pText, int length)
925 printf("\n=======================================\n");
926 printf(" Dump Text To Hex - Length :%d\n", length);
927 printf("=======================================");
929 for (int i = 0; i < length; i++ )
931 if ( i % MAX_DUMP_COLUMN == 0 )
935 printf("%02x ", pText[i]);
938 printf("\n=======================================\n\n");