• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Copyright (C) 2014 Samsung Electronics Co., Ltd. All rights reserved
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 "msg_text_convert.h"
18 
19 #include <cstdio>
20 #include <cstring>
21 #include <memory>
22 
23 #include "glib.h"
24 #include "mms_charset.h"
25 #include "securec.h"
26 #include "telephony_log_wrapper.h"
27 
28 namespace OHOS {
29 namespace Telephony {
30 using msg_encode_type_t = unsigned char;
31 using namespace std;
32 std::shared_ptr<MsgTextConvert> MsgTextConvert::instance_ = nullptr;
33 static constexpr uint8_t GSM7_DEFLIST_LEN = 128;
34 
35 template<typename T>
UniquePtrDeleterOneDimension(T ** (& ptr))36 inline void UniquePtrDeleterOneDimension(T **(&ptr))
37 {
38     if (ptr && *ptr) {
39         delete[] *ptr;
40         *ptr = nullptr;
41     }
42 }
43 
44 const WCHAR GSM7_BIT_TO_UC_S2[] = {
45     /* @ */
46     0x0040, 0x00A3, 0x0024, 0x00A5, 0x00E8, 0x00E9, 0x00F9, 0x00EC, 0x00F2, 0x00C7, 0x000A, 0x00D8, 0x00F8, 0x000D,
47     0x00C5, 0x00E5, 0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8, 0x03A3, 0x0398, 0x039E, 0x001B,
48     0x00C6, 0x00E6, 0x00DF, 0x00C9,
49     /* SP */
50     0x0020, 0x0021, 0x0022, 0x0023, 0x00A4, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D,
51     0x002E, 0x002F,
52     /* 0 */
53     0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D,
54     0x003E, 0x003F, 0x00A1, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x004B,
55     0x004C, 0x004D, 0x004E, 0x004F,
56     /* P */
57     0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x00C4, 0x00D6, 0x00D1,
58     0x00DC, 0x00A7, 0x00BF, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B,
59     0x006C, 0x006D, 0x006E, 0x006F,
60     /* p */
61     0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x00E4, 0x00F6, 0x00F1,
62     0x00FC, 0x00E0
63 };
64 
MsgTextConvert()65 MsgTextConvert::MsgTextConvert()
66 {
67     InitExtCharMap();
68     InitUCS2ToGSM7DefMap();
69     InitUCS2ToExtMap();
70     InitUCS2ToTurkishMap();
71     InitUCS2ToSpanishMap();
72     InitUCS2ToPortuMap();
73     InitUCS2ToReplaceCharMap();
74 }
75 
~MsgTextConvert()76 MsgTextConvert::~MsgTextConvert()
77 {
78     extCharMap_.clear();
79     ucs2toGSM7DefMap_.clear();
80     ucs2toGSM7ExtMap_.clear();
81     ucs2toTurkishMap_.clear();
82     ucs2toSpanishMap_.clear();
83     ucs2toPortuMap_.clear();
84     replaceCharMap_.clear();
85 }
86 
Instance()87 MsgTextConvert *MsgTextConvert::Instance()
88 {
89     if (!instance_) {
90         TELEPHONY_LOGI("pInstance is nullptr. Now creating instance.\r\n");
91         struct make_shared_enabler : public MsgTextConvert {};
92         instance_ = std::make_shared<make_shared_enabler>();
93     }
94     return instance_.get();
95 }
96 
InitExtCharMap()97 void MsgTextConvert::InitExtCharMap()
98 {
99     extCharMap_.clear();
100     extCharMap_ = { { 0x000C, MSG_GSM7EXT_CHAR }, { 0x005B, MSG_GSM7EXT_CHAR }, { 0x005C, MSG_GSM7EXT_CHAR },
101         { 0x005D, MSG_GSM7EXT_CHAR }, { 0x005E, MSG_GSM7EXT_CHAR }, { 0x007B, MSG_GSM7EXT_CHAR },
102         { 0x007C, MSG_GSM7EXT_CHAR }, { 0x007D, MSG_GSM7EXT_CHAR }, { 0x007E, MSG_GSM7EXT_CHAR },
103         { 0x20AC, MSG_GSM7EXT_CHAR }, { 0x00E7, MSG_TURKISH_CHAR }, { 0x011E, MSG_TURKISH_CHAR },
104         { 0x011F, MSG_TURKISH_CHAR }, { 0x01E6, MSG_TURKISH_CHAR }, { 0x01E7, MSG_TURKISH_CHAR },
105         { 0x0130, MSG_TURKISH_CHAR }, { 0x0131, MSG_TURKISH_CHAR }, { 0x015E, MSG_TURKISH_CHAR },
106         { 0x015F, MSG_TURKISH_CHAR }, { 0x00C1, MSG_SPANISH_CHAR }, { 0x00E1, MSG_SPANISH_CHAR },
107         { 0x00CD, MSG_SPANISH_CHAR }, { 0x00ED, MSG_SPANISH_CHAR }, { 0x00D3, MSG_SPANISH_CHAR },
108         { 0x00F3, MSG_SPANISH_CHAR }, { 0x00DA, MSG_SPANISH_CHAR }, { 0x00FA, MSG_SPANISH_CHAR },
109         { 0x00D4, MSG_PORTUGUESE_CHAR }, { 0x00F4, MSG_PORTUGUESE_CHAR }, { 0x00CA, MSG_PORTUGUESE_CHAR },
110         { 0x00EA, MSG_PORTUGUESE_CHAR }, { 0x00C0, MSG_PORTUGUESE_CHAR }, { 0x00E7, MSG_PORTUGUESE_CHAR },
111         { 0x00C3, MSG_PORTUGUESE_CHAR }, { 0x00E3, MSG_PORTUGUESE_CHAR }, { 0x00D5, MSG_PORTUGUESE_CHAR },
112         { 0x00F5, MSG_PORTUGUESE_CHAR }, { 0x00C2, MSG_PORTUGUESE_CHAR }, { 0x00E2, MSG_PORTUGUESE_CHAR } };
113 }
114 
InitUCS2ToGSM7DefMap()115 void MsgTextConvert::InitUCS2ToGSM7DefMap()
116 {
117     ucs2toGSM7DefMap_.clear();
118     for (unsigned char i = 0; i < GSM7_DEFLIST_LEN; i++) {
119         ucs2toGSM7DefMap_[GSM7_BIT_TO_UC_S2[i]] = i;
120     }
121 }
122 
InitUCS2ToExtMap()123 void MsgTextConvert::InitUCS2ToExtMap()
124 {
125     ucs2toGSM7ExtMap_.clear();
126     ucs2toGSM7ExtMap_ = { { 0x005B, 0x3C }, { 0x005D, 0x3E }, { 0x007B, 0x28 }, { 0x007D, 0x29 }, { 0x000C, 0x0A },
127         { 0x005C, 0x2F }, { 0x005E, 0x14 }, { 0x007C, 0x40 }, { 0x007E, 0x3D }, { 0x20AC, 0x65 } };
128 }
129 
InitUCS2ToTurkishMap()130 void MsgTextConvert::InitUCS2ToTurkishMap()
131 {
132     /* Turkish */
133     ucs2toTurkishMap_.clear();
134     ucs2toTurkishMap_ = { { 0x005B, 0x3C }, { 0x005D, 0x3E }, { 0x007B, 0x28 }, { 0x007D, 0x29 }, { 0x000C, 0x0A },
135         { 0x005C, 0x2F }, { 0x005E, 0x14 }, { 0x007C, 0x40 }, { 0x007E, 0x3D }, { 0x20AC, 0x65 }, { 0x00E7, 0x63 },
136         { 0x011E, 0x47 }, { 0x011F, 0x67 }, { 0x01E6, 0x47 }, { 0x01E7, 0x67 }, { 0x0130, 0x49 }, { 0x0131, 0x69 },
137         { 0x015E, 0x53 }, { 0x015F, 0x73 } };
138 }
139 
InitUCS2ToSpanishMap()140 void MsgTextConvert::InitUCS2ToSpanishMap()
141 {
142     /* Spanish */
143     ucs2toSpanishMap_.clear();
144     ucs2toSpanishMap_ = { { 0x005B, 0x3C }, { 0x005D, 0x3E }, { 0x007B, 0x28 }, { 0x007D, 0x29 }, { 0x000C, 0x0A },
145         { 0x005C, 0x2F }, { 0x005E, 0x14 }, { 0x007C, 0x40 }, { 0x007E, 0x3D }, { 0x20AC, 0x65 }, { 0x00C1, 0x41 },
146         { 0x00E1, 0x61 }, { 0x00CD, 0x49 }, { 0x00ED, 0x69 }, { 0x00D3, 0x4F }, { 0x00F3, 0x6F }, { 0x00DA, 0x55 },
147         { 0x00FA, 0x75 } };
148 }
149 
InitUCS2ToPortuMap()150 void MsgTextConvert::InitUCS2ToPortuMap()
151 {
152     /* Portuguese */
153     ucs2toPortuMap_.clear();
154     ucs2toPortuMap_ = { { 0x005B, 0x3C }, { 0x005D, 0x3E }, { 0x007B, 0x28 }, { 0x007D, 0x29 }, { 0x000C, 0x0A },
155         { 0x005C, 0x2F }, { 0x005E, 0x14 }, { 0x007C, 0x40 }, { 0x007E, 0x3D }, { 0x20AC, 0x65 }, { 0x00D4, 0x0B },
156         { 0x00F4, 0x0C }, { 0x00C1, 0x0E }, { 0x00E1, 0x0F }, { 0x00CA, 0x1F }, { 0x00EA, 0x05 }, { 0x00C0, 0x41 },
157         { 0x00E7, 0x09 }, { 0x00CD, 0x49 }, { 0x00ED, 0x69 }, { 0x00D3, 0x4F }, { 0x00F3, 0x6F }, { 0x00DA, 0x55 },
158         { 0x00FA, 0x75 }, { 0x00C3, 0x61 }, { 0x00E3, 0x7B }, { 0x00D5, 0x5C }, { 0x00F5, 0x7C }, { 0x00C2, 0x61 },
159         { 0x00E2, 0x7F }, { 0x03A6, 0x12 }, { 0x0393, 0x13 }, { 0x03A9, 0x15 }, { 0x03A0, 0x16 }, { 0x03A8, 0x17 },
160         { 0x03A3, 0x18 }, { 0x0398, 0x19 } };
161 }
162 
InitUCS2ToReplaceCharMap()163 void MsgTextConvert::InitUCS2ToReplaceCharMap()
164 {
165     /* character replacement table */
166     replaceCharMap_.clear();
167     replaceCharMap_ = { { 0x00E0, 0x61 }, { 0x00E1, 0x61 }, { 0x00E2, 0x61 }, { 0x00E3, 0x61 }, { 0x00E4, 0x61 },
168         { 0x00E5, 0x61 }, { 0x00E6, 0x61 }, { 0x0101, 0x61 }, { 0x0103, 0x61 }, { 0x0105, 0x61 }, { 0x01CE, 0x61 },
169         { 0x00C0, 0x41 }, { 0x00C1, 0x41 }, { 0x00C2, 0x41 }, { 0x00C3, 0x41 }, { 0x00C4, 0x41 }, { 0x00C5, 0x41 },
170         { 0x00C6, 0x41 }, { 0x0100, 0x41 }, { 0x0102, 0x41 }, { 0x0104, 0x41 }, { 0x01CD, 0x41 }, { 0x00E7, 0x63 },
171         { 0x0107, 0x63 }, { 0x0109, 0x63 }, { 0x010B, 0x63 }, { 0x010D, 0x63 }, { 0x00C7, 0x43 }, { 0x0106, 0x43 },
172         { 0x0108, 0x43 }, { 0x010A, 0x43 }, { 0x010C, 0x43 }, { 0x010F, 0x64 }, { 0x0111, 0x64 }, { 0x010E, 0x44 },
173         { 0x0110, 0x44 }, { 0x00E8, 0x65 }, { 0x00E9, 0x65 }, { 0x00EA, 0x65 }, { 0x00EB, 0x65 }, { 0x0113, 0x65 },
174         { 0x0115, 0x65 }, { 0x0117, 0x65 }, { 0x0119, 0x65 }, { 0x011B, 0x65 }, { 0x0259, 0x65 }, { 0x00C8, 0x45 },
175         { 0x00C9, 0x45 }, { 0x00CA, 0x45 }, { 0x00CB, 0x45 }, { 0x0112, 0x45 }, { 0x0114, 0x45 }, { 0x0116, 0x45 },
176         { 0x0118, 0x45 }, { 0x011A, 0x45 }, { 0x018F, 0x45 }, { 0x011D, 0x67 }, { 0x011F, 0x67 }, { 0x0121, 0x67 },
177         { 0x0123, 0x67 }, { 0x01E7, 0x67 }, { 0x01F5, 0x67 }, { 0x1E21, 0x67 }, { 0x011C, 0x47 }, { 0x011E, 0x47 },
178         { 0x0120, 0x47 }, { 0x0122, 0x47 }, { 0x01E6, 0x47 }, { 0x01F4, 0x47 }, { 0x1E20, 0x47 }, { 0x00EC, 0x69 },
179         { 0x00ED, 0x69 }, { 0x00EE, 0x69 }, { 0x00EF, 0x69 }, { 0x0129, 0x69 }, { 0x012B, 0x69 }, { 0x012D, 0x69 },
180         { 0x012F, 0x69 }, { 0x01D0, 0x69 }, { 0x0131, 0x69 }, { 0x00CC, 0x49 }, { 0x00CD, 0x49 }, { 0x00CE, 0x49 },
181         { 0x00CF, 0x49 }, { 0x0128, 0x49 }, { 0x012A, 0x49 }, { 0x012C, 0x49 }, { 0x012E, 0x49 }, { 0x0130, 0x49 },
182         { 0x0137, 0x6B }, { 0x0136, 0x4B }, { 0x013A, 0x6C }, { 0x013C, 0x6C }, { 0x013E, 0x6C }, { 0x0140, 0x6C },
183         { 0x0142, 0x6C }, { 0x0139, 0x4C }, { 0x013B, 0x4C }, { 0x013D, 0x4C }, { 0x013F, 0x4C }, { 0x0141, 0x4C },
184         { 0x00F1, 0x6E }, { 0x0144, 0x6E }, { 0x0146, 0x6E }, { 0x0148, 0x6E }, { 0x00D1, 0x4E }, { 0x0143, 0x4E },
185         { 0x0145, 0x4E }, { 0x0147, 0x4E }, { 0x00F2, 0x6F }, { 0x00F3, 0x6F }, { 0x00F4, 0x6F }, { 0x00F5, 0x6F },
186         { 0x00F6, 0x6F }, { 0x00F8, 0x6F }, { 0x014D, 0x6F }, { 0x014F, 0x6F }, { 0x01D2, 0x6F }, { 0x01EB, 0x6F },
187         { 0x0151, 0x6F }, { 0x0153, 0x6F }, { 0x00D2, 0x4F }, { 0x00D3, 0x4F }, { 0x00D4, 0x4F }, { 0x00D5, 0x4F },
188         { 0x00D6, 0x4F }, { 0x00D8, 0x4F }, { 0x014C, 0x4F }, { 0x014E, 0x4F }, { 0x01D1, 0x4F }, { 0x01EA, 0x4F },
189         { 0x0150, 0x4F }, { 0x0152, 0x4F }, { 0x0155, 0x72 }, { 0x0157, 0x72 }, { 0x0159, 0x72 }, { 0x0154, 0x52 },
190         { 0x0156, 0x52 }, { 0x0158, 0x52 }, { 0x015B, 0x73 }, { 0x015D, 0x73 }, { 0x015F, 0x73 }, { 0x0161, 0x73 },
191         { 0x015A, 0x53 }, { 0x015C, 0x53 }, { 0x015E, 0x53 }, { 0x0160, 0x53 }, { 0x00FE, 0x74 }, { 0x0163, 0x74 },
192         { 0x0165, 0x74 }, { 0x0167, 0x74 }, { 0x021B, 0x74 }, { 0x00DE, 0x54 }, { 0x0162, 0x54 }, { 0x0164, 0x54 },
193         { 0x0166, 0x54 }, { 0x00F9, 0x75 }, { 0x00FA, 0x75 }, { 0x00FB, 0x75 }, { 0x00FC, 0x75 }, { 0x0169, 0x75 },
194         { 0x016B, 0x75 }, { 0x016D, 0x75 }, { 0x016F, 0x75 }, { 0x0171, 0x75 }, { 0x0173, 0x75 }, { 0x01D4, 0x75 },
195         { 0x00D9, 0x55 }, { 0x00DA, 0x55 }, { 0x00DB, 0x55 }, { 0x00DC, 0x55 }, { 0x0168, 0x55 }, { 0x016A, 0x55 },
196         { 0x016C, 0x55 }, { 0x016E, 0x55 }, { 0x0170, 0x55 }, { 0x0172, 0x55 }, { 0x01D3, 0x55 }, { 0x00FD, 0x79 },
197         { 0x00FF, 0x79 }, { 0x0177, 0x79 }, { 0x0233, 0x79 }, { 0x1EF3, 0x79 }, { 0x1EF9, 0x79 }, { 0x00DD, 0x59 },
198         { 0x0176, 0x59 }, { 0x0178, 0x59 }, { 0x0232, 0x59 }, { 0x1EF2, 0x59 }, { 0x1EF8, 0x59 }, { 0x017A, 0x7A },
199         { 0x017C, 0x7A }, { 0x017E, 0x7A }, { 0x0179, 0x5A }, { 0x017B, 0x5A }, { 0x017D, 0x5A } };
200 }
201 
202 /**
203  * @brief ConvertUTF8ToGSM7bit
204  * max # of UCS2 chars, NOT bytes. when all utf8 chars are only one byte,
205  * UCS2Length is maxUCS2 Length. otherwise (ex: 2 bytes of UTF8 is one char)
206  * UCS2Length must be  less than utf8Length
207  */
ConvertUTF8ToGSM7bit(std::tuple<unsigned char *,int,unsigned char *,int,MSG_LANGUAGE_ID_T *,bool * > & parameters)208 int MsgTextConvert::ConvertUTF8ToGSM7bit(std::tuple<unsigned char *, int,
209     unsigned char *, int, MSG_LANGUAGE_ID_T *, bool *> &parameters)
210 {
211     unsigned char *pDestText;
212     int maxLength;
213     unsigned char *pSrcText;
214     int srcTextLen;
215     MSG_LANGUAGE_ID_T *pLangId;
216     bool *bIncludeAbnormalChar;
217     std::tie(pDestText, maxLength, pSrcText, srcTextLen, pLangId, bIncludeAbnormalChar) = parameters;
218     int utf8Length = 0;
219     int gsm7bitLength = 0;
220     int ucs2Length = 0;
221     if (srcTextLen <= 0 && pSrcText) {
222         utf8Length = strlen(reinterpret_cast<const char *>(pSrcText));
223         srcTextLen = utf8Length;
224     } else {
225         utf8Length = srcTextLen;
226     }
227 
228     int maxUCS2Length = utf8Length;
229     WCHAR *pUCS2Text = nullptr;
230     unique_ptr<WCHAR *, void (*)(WCHAR * *(&))> buf(&pUCS2Text, UniquePtrDeleterOneDimension);
231     pUCS2Text = reinterpret_cast<WCHAR *>(new (std::nothrow) char[maxUCS2Length * sizeof(WCHAR)]);
232     if (pUCS2Text == nullptr) {
233         return gsm7bitLength;
234     }
235     (void)memset_s(pUCS2Text, maxUCS2Length * sizeof(WCHAR), 0x00, maxUCS2Length * sizeof(WCHAR));
236     TELEPHONY_LOGI("srcTextLen = %{public}d", srcTextLen);
237 
238     ucs2Length = ConvertUTF8ToUCS2(
239         reinterpret_cast<unsigned char *>(pUCS2Text), maxUCS2Length * sizeof(WCHAR), pSrcText, srcTextLen);
240     gsm7bitLength = ConvertUCS2ToGSM7bit(pDestText, maxLength, reinterpret_cast<unsigned char *>(pUCS2Text),
241         ucs2Length, pLangId, bIncludeAbnormalChar);
242     return gsm7bitLength;
243 }
244 
ConvertUTF8ToUCS2(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen)245 int MsgTextConvert::ConvertUTF8ToUCS2(
246     OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen)
247 {
248     gsize textLen;
249     auto unicodeTemp = reinterpret_cast<unsigned char *>(pDestText);
250     gsize ucs2Length = 0;
251     gsize remainedBuffer = maxLength;
252 #ifdef CONVERT_DUMP
253     int srcLen = srcTextLen;
254     const unsigned char *pTempSrcText = pSrcText;
255     const unsigned char *pTempDestText = pDestText;
256 #endif
257     if (maxLength == 0 || pSrcText == nullptr || pDestText == nullptr) {
258         TELEPHONY_LOGE("UTF8 to UCS2 Failed as text length is 0");
259         return -1;
260     }
261     /* nullptr terminated string */
262     if (srcTextLen == -1) {
263         textLen = strlen(reinterpret_cast<char *>(const_cast<unsigned char *>(pSrcText)));
264     } else {
265         textLen = srcTextLen;
266     }
267 
268     GIConv cd;
269     int err = 0;
270     cd = g_iconv_open("UTF16BE", "UTF8");
271     if (cd != nullptr) {
272         err = g_iconv(cd, reinterpret_cast<char **>(const_cast<unsigned char **>(&pSrcText)),
273             reinterpret_cast<gsize *>(&textLen), reinterpret_cast<char **>(&unicodeTemp),
274             reinterpret_cast<gsize *>(&remainedBuffer));
275     }
276     ucs2Length = (err < 0) ? -1 : (maxLength - remainedBuffer);
277 #ifdef CONVERT_DUMP
278     TELEPHONY_LOGI("########## Dump UTF8 -> UCS2");
279     ConvertDumpTextToHex((unsigned char *)pTempSrcText, srcLen);
280     ConvertDumpTextToHex((unsigned char *)pTempDestText, ucs2Length);
281 #endif
282     g_iconv_close(cd);
283     return ucs2Length;
284 }
285 
ConvertGsmUTF8ToAuto(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen,OUT SmsCodingScheme * pCharType)286 int MsgTextConvert::ConvertGsmUTF8ToAuto(OUT unsigned char *pDestText, IN int maxLength,
287     IN const unsigned char *pSrcText, IN int srcTextLen, OUT SmsCodingScheme *pCharType)
288 {
289     int ret = 0;
290     int tempTextLen = 0;
291     int utf8Length = 0;
292     int gsm7bitLength = 0;
293     int ucs2Length = 0;
294     bool bUnknown = false;
295     utf8Length = srcTextLen;
296 
297     int maxUCS2Length = utf8Length;
298     WCHAR pUCS2Text[maxUCS2Length];
299     ret = memset_s(pUCS2Text, sizeof(pUCS2Text), 0x00, maxUCS2Length * sizeof(WCHAR));
300     if (ret != EOK) {
301         TELEPHONY_LOGE("memset_s err = %{public}d", ret);
302     }
303     ucs2Length = ConvertUTF8ToUCS2(
304         reinterpret_cast<unsigned char *>(pUCS2Text), maxUCS2Length * sizeof(WCHAR), pSrcText, srcTextLen);
305     if (ucs2Length < 0) {
306         *pCharType = SMS_CODING_8BIT;
307         tempTextLen = (srcTextLen > maxLength) ? maxLength : srcTextLen;
308         ret = memcpy_s(pDestText, tempTextLen, pSrcText, tempTextLen);
309         if (ret != EOK) {
310             TELEPHONY_LOGE("memcpy_s err = %{public}d", ret);
311         }
312         return tempTextLen;
313     } else {
314         gsm7bitLength = ConvertUCS2ToGSM7bitAuto(
315             pDestText, maxLength, reinterpret_cast<unsigned char *>(pUCS2Text), ucs2Length, &bUnknown);
316         if (bUnknown) {
317             *pCharType = SMS_CODING_UCS2;
318             if (ucs2Length <= 0) {
319                 return gsm7bitLength;
320             }
321             tempTextLen = (ucs2Length > maxLength) ? maxLength : ucs2Length;
322             ret = memcpy_s(pDestText, tempTextLen, pUCS2Text, tempTextLen);
323             if (ret != EOK) {
324                 TELEPHONY_LOGE("memcpy_s err = %{public}d", ret);
325             }
326             return tempTextLen;
327         } else {
328             *pCharType = SMS_CODING_7BIT;
329         }
330         return gsm7bitLength;
331     }
332 }
333 
ConvertCdmaUTF8ToAuto(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen,OUT SmsCodingScheme * pCharType)334 int MsgTextConvert::ConvertCdmaUTF8ToAuto(OUT unsigned char *pDestText, IN int maxLength,
335     IN const unsigned char *pSrcText, IN int srcTextLen, OUT SmsCodingScheme *pCharType)
336 {
337     int ret = 0;
338     int tempTextLen = 0;
339     int utf8Length = 0;
340     int gsm7bitLength = 0;
341     int ucs2Length = 0;
342     bool bUnknown = false;
343     utf8Length = srcTextLen;
344 
345     int maxUCS2Length = utf8Length;
346     WCHAR pUCS2Text[maxUCS2Length];
347     ret = memset_s(pUCS2Text, sizeof(pUCS2Text), 0x00, maxUCS2Length * sizeof(WCHAR));
348     if (ret != EOK) {
349         TELEPHONY_LOGE("memset_s err = %{public}d", ret);
350     }
351     ucs2Length = ConvertUTF8ToUCS2(
352         reinterpret_cast<unsigned char *>(pUCS2Text), maxUCS2Length * sizeof(WCHAR), pSrcText, srcTextLen);
353     if (ucs2Length < 0) {
354         *pCharType = SMS_CODING_8BIT;
355         tempTextLen = (srcTextLen > maxLength) ? maxLength : srcTextLen;
356         ret = memcpy_s(pDestText, tempTextLen, pSrcText, tempTextLen);
357         if (ret != EOK) {
358             TELEPHONY_LOGE("memcpy_s err = %{public}d", ret);
359         }
360         return tempTextLen;
361     } else {
362         gsm7bitLength = ConvertUCS2ToASCII(
363             pDestText, maxLength, reinterpret_cast<unsigned char *>(pUCS2Text), ucs2Length, &bUnknown);
364         if (bUnknown) {
365             *pCharType = SMS_CODING_UCS2;
366             if (ucs2Length <= 0) {
367                 return gsm7bitLength;
368             }
369             tempTextLen = (ucs2Length > maxLength) ? maxLength : ucs2Length;
370             ret = memcpy_s(pDestText, tempTextLen, pUCS2Text, tempTextLen);
371             if (ret != EOK) {
372                 TELEPHONY_LOGE("memcpy_s err = %{public}d", ret);
373             }
374             return tempTextLen;
375         } else {
376             *pCharType = SMS_CODING_ASCII7BIT;
377         }
378         return gsm7bitLength;
379     }
380 }
381 
382 /* max # of UCS2 chars, NOT bytes. when all gsm7 chars are only one byte(-there
383    is no extension), UCS2Length is maxUCS2 Length. otherwise(ex: gsm7 char
384    starts with 0x1b) UCS2Length must be less than gsm7 length */
ConvertGSM7bitToUTF8(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen,const IN MsgLangInfo * pLangInfo)385 int MsgTextConvert::ConvertGSM7bitToUTF8(OUT unsigned char *pDestText, IN int maxLength,
386     IN const unsigned char *pSrcText, IN int srcTextLen, const IN MsgLangInfo *pLangInfo)
387 {
388     int utf8Length = 0;
389     int ucs2Length = 0;
390     int maxUCS2Length = srcTextLen;
391 
392     WCHAR pUCS2Text[maxUCS2Length];
393     if (memset_s(pUCS2Text, sizeof(pUCS2Text), 0x00, sizeof(pUCS2Text)) != EOK) {
394         TELEPHONY_LOGE("ConvertGSM7bitToUTF8 memset_sfail");
395         return utf8Length;
396     }
397     TELEPHONY_LOGI("srcTextLen = %{public}d", srcTextLen);
398     TELEPHONY_LOGI("max dest Length = %{public}d", maxLength);
399     ucs2Length = ConvertGSM7bitToUCS2(reinterpret_cast<unsigned char *>(pUCS2Text), maxUCS2Length * sizeof(WCHAR),
400         pSrcText, srcTextLen, pLangInfo);
401     utf8Length = ConvertUCS2ToUTF8(pDestText, maxLength, reinterpret_cast<unsigned char *>(pUCS2Text), ucs2Length);
402     return utf8Length;
403 }
404 
ConvertUCS2ToUTF8(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen)405 int MsgTextConvert::ConvertUCS2ToUTF8(
406     OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen)
407 {
408     gsize textLen;
409     gsize remainedBuffer = maxLength;
410     int utf8Length;
411 
412 #ifdef CONVERT_DUMP
413     int srcLen = srcTextLen;
414     const unsigned char *pTempSrcText = pSrcText;
415 #endif
416     unsigned char *pTempDestText = pDestText;
417     if (srcTextLen == 0 || pSrcText == nullptr || pDestText == nullptr || maxLength == 0) {
418         TELEPHONY_LOGE("UCS2 to UTF8 Failed as text length is 0");
419         return false;
420     }
421 
422     if (srcTextLen == -1) {
423         textLen = strlen(reinterpret_cast<char *>(const_cast<unsigned char *>(pSrcText)));
424     } else {
425         textLen = srcTextLen;
426     }
427 
428     GIConv cd;
429     int err = 0;
430     cd = g_iconv_open("UTF8", "UTF16BE");
431     if (cd != nullptr) {
432         err = g_iconv(cd, reinterpret_cast<char **>(const_cast<unsigned char **>(&pSrcText)),
433             reinterpret_cast<gsize *>(&textLen), reinterpret_cast<char **>(&pDestText),
434             reinterpret_cast<gsize *>(&remainedBuffer));
435     }
436     if (err != 0) {
437         TELEPHONY_LOGI("g_iconv() return value = %{public}d", err);
438     }
439     utf8Length = maxLength - remainedBuffer;
440     pTempDestText[utf8Length] = 0x00;
441 #ifdef CONVERT_DUMP
442     TELEPHONY_LOGI("\n########## Dump UCS2 -> UTF8");
443     ConvertDumpTextToHex((unsigned char *)pTempSrcText, srcLen);
444     ConvertDumpTextToHex((unsigned char *)pTempDestText, utf8Length);
445 #endif
446     g_iconv_close(cd);
447     return utf8Length;
448 }
449 
ConvertEUCKRToUTF8(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen)450 int MsgTextConvert::ConvertEUCKRToUTF8(
451     OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen)
452 {
453     gsize textLen;
454     gsize remainedBuffer = maxLength;
455     int utf8Length;
456 
457 #ifdef CONVERT_DUMP
458     int srcLen = srcTextLen;
459     const unsigned char *pTempSrcText = pSrcText;
460 #endif
461     unsigned char *pTempDestText = pDestText;
462     if (srcTextLen == 0 || pSrcText == nullptr || pDestText == nullptr || maxLength == 0) {
463         TELEPHONY_LOGE("EUCKR to UTF8 Failed as text length is 0");
464         return false;
465     }
466     if (srcTextLen == -1) {
467         textLen = strlen(reinterpret_cast<char *>(const_cast<unsigned char *>(pSrcText)));
468     } else {
469         textLen = srcTextLen;
470     }
471 
472     GIConv cd;
473     int err = 0;
474     cd = g_iconv_open("UTF8", "EUCKR");
475     if (cd != nullptr) {
476         err = g_iconv(cd, reinterpret_cast<char **>(const_cast<unsigned char **>(&pSrcText)),
477             reinterpret_cast<gsize *>(&textLen), reinterpret_cast<char **>(&pDestText),
478             reinterpret_cast<gsize *>(&remainedBuffer));
479     }
480     if (err != 0) {
481         TELEPHONY_LOGI("g_iconv() return value = %{public}d", err);
482     }
483     utf8Length = maxLength - remainedBuffer;
484     pTempDestText[utf8Length] = 0x00;
485 #ifdef CONVERT_DUMP
486     TELEPHONY_LOGI("\n########## Dump EUCKR -> UTF8\n");
487     ConvertDumpTextToHex((unsigned char *)pTempSrcText, srcLen);
488     ConvertDumpTextToHex((unsigned char *)pTempDestText, utf8Length);
489 #endif
490     g_iconv_close(cd);
491     return utf8Length;
492 }
493 
ConvertSHIFTJISToUTF8(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen) const494 int MsgTextConvert::ConvertSHIFTJISToUTF8(
495     OUT unsigned char *pDestText, IN int maxLength, IN const unsigned char *pSrcText, IN int srcTextLen) const
496 {
497     gsize textLen;
498     gsize remainedBuffer = maxLength;
499     int utf8Length;
500 
501 #ifdef CONVERT_DUMP
502     int srcLen = srcTextLen;
503     const unsigned char *pTempSrcText = pSrcText;
504 #endif
505     unsigned char *pTempDestText = pDestText;
506     if (srcTextLen == 0 || pSrcText == nullptr || pDestText == nullptr || maxLength == 0) {
507         TELEPHONY_LOGE("EUCKR to UTF8 Failed as text length is 0");
508         return false;
509     }
510     if (srcTextLen == -1) {
511         textLen = strlen(reinterpret_cast<char *>(const_cast<unsigned char *>(pSrcText)));
512     } else {
513         textLen = srcTextLen;
514     }
515 
516     GIConv cd;
517     int err = 0;
518     cd = g_iconv_open("UTF8", "SHIFT-JIS");
519     if (cd != nullptr) {
520         err = g_iconv(cd, reinterpret_cast<char **>(const_cast<unsigned char **>(&pSrcText)),
521             reinterpret_cast<gsize *>(&textLen), reinterpret_cast<char **>(&pDestText),
522             reinterpret_cast<gsize *>(&remainedBuffer));
523     }
524 
525     TELEPHONY_LOGI("g_iconv() return value = %{public}d", err);
526     utf8Length = maxLength - remainedBuffer;
527     pTempDestText[utf8Length] = 0x00;
528 #ifdef CONVERT_DUMP
529     TELEPHONY_LOGI("\n########## Dump SHIFT-JIS -> UTF8");
530     ConvertDumpTextToHex((unsigned char *)pTempSrcText, srcLen);
531     ConvertDumpTextToHex((unsigned char *)pTempDestText, utf8Length);
532 #endif
533     g_iconv_close(cd);
534     return utf8Length;
535 }
536 
ConvertUCS2ToGSM7bit(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen,OUT MSG_LANGUAGE_ID_T * pLangId,OUT bool * abnormalChar)537 int MsgTextConvert::ConvertUCS2ToGSM7bit(OUT unsigned char *pDestText, IN int maxLength,
538     IN const unsigned char *pSrcText, IN int srcTextLen, OUT MSG_LANGUAGE_ID_T *pLangId, OUT bool *abnormalChar)
539 {
540     /* for UNICODE */
541     int outTextLen = 0;
542     int remainLen = 0;
543     if (srcTextLen == 0 || pSrcText == nullptr || pDestText == nullptr || maxLength <= 0) {
544         TELEPHONY_LOGE("Invalid parameter.");
545         return -1;
546     }
547     unsigned short inText;
548     unsigned char lowerByte;
549     unsigned char upperByte;
550     unsigned char currType = GetLangType(pSrcText, srcTextLen);
551     std::map<unsigned short, unsigned char>::iterator itChar;
552     for (int index = 0; index < (srcTextLen - 1); index++) {
553         upperByte = pSrcText[index++];
554         lowerByte = pSrcText[index];
555         inText = ((upperByte << 0x08) & 0xFF00) | lowerByte;
556         itChar = ucs2toGSM7DefMap_.find(inText); /* Check Default Char */
557         if (itChar != ucs2toGSM7DefMap_.end()) {
558             pDestText[outTextLen++] = static_cast<unsigned char>(itChar->second);
559         } else {
560             switch (currType) {
561                 case MSG_GSM7EXT_CHAR:
562                     remainLen = maxLength - outTextLen;
563                     outTextLen += FindUCS2toGSM7Ext(&pDestText[outTextLen], remainLen, inText, *abnormalChar);
564                     outTextLen++;
565                     break;
566                 case MSG_TURKISH_CHAR:
567                     *pLangId = MSG_ID_TURKISH_LANG;
568                     remainLen = maxLength - outTextLen;
569                     outTextLen += FindUCS2toTurkish(&pDestText[outTextLen], remainLen, inText, *abnormalChar);
570                     outTextLen++;
571                     break;
572                 case MSG_SPANISH_CHAR:
573                     *pLangId = MSG_ID_SPANISH_LANG;
574                     remainLen = maxLength - outTextLen;
575                     outTextLen += FindUCS2toSpanish(&pDestText[outTextLen], remainLen, inText, *abnormalChar);
576                     outTextLen++;
577                     break;
578                 case MSG_PORTUGUESE_CHAR:
579                     *pLangId = MSG_ID_PORTUGUESE_LANG;
580                     remainLen = maxLength - outTextLen;
581                     outTextLen += FindUCS2toPortu(&pDestText[outTextLen], remainLen, inText, *abnormalChar);
582                     outTextLen++;
583                     break;
584                 default:
585                     pDestText[outTextLen++] = FindUCS2ReplaceChar(inText);
586                     *abnormalChar = true;
587                     break;
588             }
589         }
590         /* prevent buffer overflow */
591         if (maxLength <= outTextLen) {
592             TELEPHONY_LOGE("Buffer full.");
593             break;
594         }
595     }
596     return outTextLen;
597 }
598 
ConvertUCS2ToGSM7bitAuto(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen,OUT bool * pUnknown)599 int MsgTextConvert::ConvertUCS2ToGSM7bitAuto(OUT unsigned char *pDestText, IN int maxLength,
600     IN const unsigned char *pSrcText, IN int srcTextLen, OUT bool *pUnknown)
601 {
602     /* for UNICODE */
603     int outTextLen = 0;
604     unsigned char lowerByte;
605     unsigned char upperByte;
606     if (srcTextLen == 0 || pSrcText == nullptr || pDestText == nullptr || maxLength == 0) {
607         TELEPHONY_LOGE("UCS2 to GSM7bit Failed as text length is 0");
608         return -1;
609     }
610     std::map<unsigned short, unsigned char>::iterator itChar;
611     std::map<unsigned short, unsigned char>::iterator itExt;
612     unsigned short inText;
613     for (int index = 0; index < srcTextLen; index++) {
614         upperByte = pSrcText[index++];
615         lowerByte = pSrcText[index];
616         inText = (upperByte << 0x08) & 0xFF00;
617         inText = inText | lowerByte;
618         /* Check Default Char */
619         itChar = ucs2toGSM7DefMap_.find(inText);
620         if (itChar != ucs2toGSM7DefMap_.end()) {
621             pDestText[outTextLen++] = static_cast<unsigned char>(itChar->second);
622         } else {
623             itExt = ucs2toGSM7ExtMap_.find(inText);
624             if (itExt == ucs2toGSM7ExtMap_.end()) {
625                 TELEPHONY_LOGI("Abnormal character is included. inText : [%{public}04x]", inText);
626                 *pUnknown = true;
627                 return 0;
628             } else if (maxLength <= outTextLen + 1) {
629                 TELEPHONY_LOGE("Buffer Full.");
630                 break;
631             } else {
632                 pDestText[outTextLen++] = 0x1B;
633                 pDestText[outTextLen++] = static_cast<unsigned char>(itExt->second);
634             }
635         }
636         /* prevent buffer overflow */
637         if (maxLength <= outTextLen) {
638             TELEPHONY_LOGE("Buffer full");
639             break;
640         }
641     }
642 #ifdef CONVERT_DUMP
643     TELEPHONY_LOGI("\n########## Dump UCS2 -> GSM7bit");
644     ConvertDumpTextToHex((unsigned char *)pSrcText, srcTextLen);
645     ConvertDumpTextToHex((unsigned char *)pDestText, outTextLen);
646 #endif
647     return outTextLen;
648 }
649 
ConvertUCS2ToASCII(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen,OUT bool * pUnknown)650 int MsgTextConvert::ConvertUCS2ToASCII(OUT unsigned char *pDestText, IN int maxLength,
651     IN const unsigned char *pSrcText, IN int srcTextLen, OUT bool *pUnknown)
652 {
653     /* for UNICODE */
654     int outTextLen = 0;
655     unsigned char lowerByte;
656     unsigned char upperByte;
657     if (srcTextLen == 0 || pSrcText == nullptr || pDestText == nullptr || maxLength == 0) {
658         TELEPHONY_LOGI("UCS2 to GSM7bit Failed as text length is 0");
659         return -1;
660     }
661     std::map<unsigned short, unsigned char>::iterator itChar;
662     std::map<unsigned short, unsigned char>::iterator itExt;
663     unsigned short inText;
664     for (int index = 0; index < srcTextLen; index++) {
665         upperByte = pSrcText[index++];
666         lowerByte = pSrcText[index];
667         inText = (upperByte << 0x08) & 0xFF00;
668         inText = inText | lowerByte;
669         /* Check Default Char */
670         if (inText > 0x007f) {
671             TELEPHONY_LOGI("Abnormal character is included. inText : [%{public}04x]", inText);
672             *pUnknown = true;
673             return 0;
674         }
675         pDestText[outTextLen++] = static_cast<unsigned char>(inText);
676         /* prevent buffer overflow */
677         if (maxLength <= outTextLen) {
678             TELEPHONY_LOGI("Buffer full\n");
679             break;
680         }
681     }
682 
683 #ifdef CONVERT_DUMP
684     TELEPHONY_LOGI("\n########## Dump UCS2 -> GSM7bit");
685     ConvertDumpTextToHex((unsigned char *)pSrcText, srcTextLen);
686     ConvertDumpTextToHex((unsigned char *)pDestText, outTextLen);
687 #endif
688     return outTextLen;
689 }
690 
GetLangType(const unsigned char * pSrcText,int srcTextLen)691 unsigned char MsgTextConvert::GetLangType(const unsigned char *pSrcText, int srcTextLen)
692 {
693     /* for UNICODE */
694     if (srcTextLen <= 0 || pSrcText == nullptr) {
695         TELEPHONY_LOGE("Invalid parameter.");
696         return MSG_DEFAULT_CHAR;
697     }
698 
699     unsigned char lowerByte;
700     unsigned char upperByte;
701     std::map<unsigned short, unsigned char>::iterator itExt;
702     unsigned char currType = MSG_DEFAULT_CHAR;
703     unsigned char newType = MSG_DEFAULT_CHAR;
704     unsigned short inText;
705     /* Get Language Type by checking each character */
706     for (int index = 0; index < (srcTextLen - 1); index++) {
707         upperByte = pSrcText[index++];
708         lowerByte = pSrcText[index];
709         inText = (upperByte << 0x08) & 0xFF00;
710         inText = inText | lowerByte;
711         itExt = extCharMap_.find(inText);
712         if (itExt == extCharMap_.end()) {
713             continue;
714         }
715         newType = static_cast<unsigned char>(itExt->second);
716         if (newType >= currType) {
717             bool isTurkisk = (inText == 0x00e7 && currType <= MSG_TURKISH_CHAR);
718             currType = isTurkisk ? MSG_TURKISH_CHAR : newType;
719         }
720     }
721     TELEPHONY_LOGI("charType : [%{public}hhu]", currType);
722     return currType;
723 }
724 
FindUCS2toGSM7Ext(unsigned char * pDestText,int maxLength,const unsigned short inText,bool & abnormalChar)725 int MsgTextConvert::FindUCS2toGSM7Ext(
726     unsigned char *pDestText, int maxLength, const unsigned short inText, bool &abnormalChar)
727 {
728     int outTextLen = 0;
729     if (pDestText == nullptr || maxLength <= 0) {
730         TELEPHONY_LOGE("Invalid parameter.");
731         return outTextLen;
732     }
733 
734     auto itExt = ucs2toGSM7ExtMap_.find(inText);
735     if (itExt == ucs2toGSM7ExtMap_.end()) {
736         pDestText[outTextLen++] = FindUCS2ReplaceChar(inText);
737         abnormalChar = true;
738 #ifdef CONVERT_DUMP
739         TELEPHONY_LOGI("Abnormal character is included. inText : [%{public}04x]", inText);
740 #endif
741         return outTextLen;
742     }
743     /* prevent buffer overflow */
744     if (maxLength <= outTextLen + 1) {
745         TELEPHONY_LOGE("Buffer Full.");
746         return outTextLen;
747     }
748     pDestText[outTextLen++] = 0x1B;
749     pDestText[outTextLen++] = static_cast<unsigned char>(itExt->second);
750     return outTextLen;
751 }
752 
FindUCS2toTurkish(unsigned char * pDestText,int maxLength,const unsigned short inText,bool & abnormalChar)753 int MsgTextConvert::FindUCS2toTurkish(
754     unsigned char *pDestText, int maxLength, const unsigned short inText, bool &abnormalChar)
755 {
756     int outTextLen = 0;
757     if (pDestText == nullptr || maxLength <= 0) {
758         TELEPHONY_LOGE("Invalid parameter.");
759         return outTextLen;
760     }
761 
762     auto itExt = ucs2toTurkishMap_.find(inText);
763     if (itExt == ucs2toTurkishMap_.end()) {
764         pDestText[outTextLen++] = FindUCS2ReplaceChar(inText);
765         abnormalChar = true;
766 #ifdef CONVERT_DUMP
767         TELEPHONY_LOGI("Abnormal character is included. inText : [%{public}04x]", inText);
768 #endif
769         return outTextLen;
770     }
771     /* prevent buffer overflow */
772     if (maxLength <= outTextLen + 1) {
773         TELEPHONY_LOGE("Buffer Full.");
774         return outTextLen;
775     }
776     pDestText[outTextLen++] = 0x1B;
777     pDestText[outTextLen++] = static_cast<unsigned char>(itExt->second);
778     return outTextLen;
779 }
780 
FindUCS2toSpanish(unsigned char * pDestText,int maxLength,const unsigned short inText,bool & abnormalChar)781 int MsgTextConvert::FindUCS2toSpanish(
782     unsigned char *pDestText, int maxLength, const unsigned short inText, bool &abnormalChar)
783 {
784     int outTextLen = 0;
785     if (pDestText == nullptr || maxLength <= 0) {
786         TELEPHONY_LOGE("Invalid parameter.");
787         return outTextLen;
788     }
789 
790     auto itExt = ucs2toSpanishMap_.find(inText);
791     if (itExt == ucs2toSpanishMap_.end()) {
792         pDestText[outTextLen++] = FindUCS2ReplaceChar(inText);
793         abnormalChar = true;
794 #ifdef CONVERT_DUMP
795         TELEPHONY_LOGI("Abnormal character is included. inText : [%{public}04x]", inText);
796 #endif
797         return outTextLen;
798     }
799     /* prevent buffer overflow */
800     if (maxLength <= outTextLen + 1) {
801         TELEPHONY_LOGE("Buffer Full.");
802         return outTextLen;
803     }
804     pDestText[outTextLen++] = 0x1B;
805     pDestText[outTextLen++] = static_cast<unsigned char>(itExt->second);
806     return outTextLen;
807 }
808 
FindUCS2toPortu(unsigned char * pDestText,int maxLength,const unsigned short inText,bool & abnormalChar)809 int MsgTextConvert::FindUCS2toPortu(
810     unsigned char *pDestText, int maxLength, const unsigned short inText, bool &abnormalChar)
811 {
812     int outTextLen = 0;
813     if (pDestText == nullptr || maxLength <= 0) {
814         TELEPHONY_LOGE("Invalid parameter.");
815         return outTextLen;
816     }
817 
818     auto itExt = ucs2toPortuMap_.find(inText);
819     if (itExt == ucs2toPortuMap_.end()) {
820         pDestText[outTextLen++] = FindUCS2ReplaceChar(inText);
821         abnormalChar = true;
822 #ifdef CONVERT_DUMP
823         TELEPHONY_LOGI("Abnormal character is included. inText : [%{public}04x]", inText);
824 #endif
825         return outTextLen;
826     }
827     /* prevent buffer overflow */
828     if (maxLength <= outTextLen + 1) {
829         TELEPHONY_LOGE("Buffer Full.");
830         return outTextLen;
831     }
832     pDestText[outTextLen++] = 0x1B;
833     pDestText[outTextLen++] = static_cast<unsigned char>(itExt->second);
834     return outTextLen;
835 }
836 
FindUCS2ReplaceChar(const unsigned short inText)837 unsigned char MsgTextConvert::FindUCS2ReplaceChar(const unsigned short inText)
838 {
839     unsigned char result = 0;
840     auto itReplace = replaceCharMap_.find(inText);
841     if (itReplace != replaceCharMap_.end()) {
842         result = static_cast<unsigned char>(itReplace->second);
843     } else {
844         result = 0x3F;
845     }
846     return result;
847 }
848 
ConvertGSM7bitToUCS2(OUT unsigned char * pDestText,IN int maxLength,IN const unsigned char * pSrcText,IN int srcTextLen,const IN MsgLangInfo * pLangInfo)849 int MsgTextConvert::ConvertGSM7bitToUCS2(OUT unsigned char *pDestText, IN int maxLength,
850     IN const unsigned char *pSrcText, IN int srcTextLen, const IN MsgLangInfo *pLangInfo)
851 {
852     int outTextLen = 0;
853     unsigned char lowerByte = 0;
854     unsigned char upperByte = 0;
855     unsigned short result;
856     if (srcTextLen == 0 || pSrcText == nullptr || pDestText == nullptr || maxLength <= 0) {
857         TELEPHONY_LOGE("UCS2 to GSM7bit Failed as text length is 0");
858         return -1;
859     }
860     for (int i = 0; i < srcTextLen && maxLength > 0; i++) {
861         if (pSrcText[i] >= 0x80) {
862             TELEPHONY_LOGE("a_pTextString[i]=%{public}x, The alpha isn't the gsm 7bit code", pSrcText[i]);
863             return -1;
864         }
865         if (pLangInfo->bLockingShift) { /* National Language Locking Shift */
866             TELEPHONY_LOGI("Locking Shift [%{public}d]", pLangInfo->lockingLang);
867             if (pLangInfo->lockingLang == MSG_ID_TURKISH_LANG) {
868                 /* Check Escape */
869                 i += EscapeTurkishLockingToUCS2(&pSrcText[i], (srcTextLen - i), *pLangInfo, result);
870                 lowerByte = static_cast<unsigned char>(result & 0x00FF);
871                 upperByte = static_cast<unsigned char>(result >> 0x08);
872             } else if (pLangInfo->lockingLang == MSG_ID_PORTUGUESE_LANG) {
873                 /* Check Escape */
874                 i += EscapePortuLockingToUCS2(&pSrcText[i], (srcTextLen - i), *pLangInfo, result);
875                 lowerByte = static_cast<unsigned char>(result & 0x00FF);
876                 upperByte = static_cast<unsigned char>(result >> 0x08);
877             }
878         } else {
879             /* Check Escape */
880             i += EscapeGSM7BitToUCS2(&pSrcText[i], (srcTextLen - i), *pLangInfo, result);
881             lowerByte = static_cast<unsigned char>(result & 0x00FF);
882             upperByte = static_cast<unsigned char>(result >> 0x08);
883         }
884         pDestText[outTextLen++] = upperByte;
885         pDestText[outTextLen++] = lowerByte;
886         maxLength -= 0x02;
887     }
888 
889 #ifdef CONVERT_DUMP
890     TELEPHONY_LOGI("\n########## Dump GSM7bit -> UCS2");
891     ConvertDumpTextToHex((unsigned char *)pSrcText, srcTextLen);
892     ConvertDumpTextToHex((unsigned char *)pDestText, outTextLen);
893 #endif
894     return outTextLen;
895 }
896 
EscapeTurkishLockingToUCS2(const unsigned char * pSrcText,int srcLen,const MsgLangInfo & pLangInfo,unsigned short & result)897 int MsgTextConvert::EscapeTurkishLockingToUCS2(
898     const unsigned char *pSrcText, int srcLen, const MsgLangInfo &pLangInfo, unsigned short &result)
899 {
900     int index = 0;
901     if (pSrcText == nullptr || srcLen <= 0) {
902         return index;
903     }
904     /* Turkish National Language Locking Shift Table -> UCS2 */
905     const WCHAR g_turkishLockingToUCS2[] = { /* @ */
906         0x0040, 0x00A3, 0x0024, 0x00A5, 0x20AC, 0x00E9, 0x00F9, 0x00EC, 0x00F2, 0x00C7, 0x000A, 0x011E, 0x011F, 0x000D,
907         0x00C5, 0x00E5, 0x0394, 0x005F, 0x03A6, 0x0393, 0x039B, 0x03A9, 0x03A0, 0x03A8, 0x03A3, 0x0398, 0x039E, 0x001B,
908         0x015E, 0x015F, 0x00DF, 0x00C9,
909         /* SP */
910         0x0020, 0x0021, 0x0022, 0x0023, 0x00A4, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D,
911         0x002E, 0x002F,
912         /* 0 */
913         0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D,
914         0x003E, 0x003F, 0x0130, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x004B,
915         0x004C, 0x004D, 0x004E, 0x004F,
916         /* P */
917         0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x00C4, 0x00D6, 0x00D1,
918         0x00DC, 0x00A7,
919         /* c */
920         0x00E7, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B, 0x006C, 0x006D,
921         0x006E, 0x006F,
922         /* p */
923         0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x00E4, 0x00F6, 0x00F1,
924         0x00FC, 0x00E0
925     };
926     /* Check Escape */
927     if (g_turkishLockingToUCS2[pSrcText[index]] == 0x001B) {
928         index++;
929         if (index > srcLen) {
930             return index;
931         }
932         result = EscapeToUCS2(pSrcText[index], pLangInfo);
933     } else { /* TURKISH - National Language Locking Shift */
934         result = g_turkishLockingToUCS2[pSrcText[index]];
935     }
936     return index;
937 }
938 
EscapePortuLockingToUCS2(const unsigned char * pSrcText,int srcLen,const MsgLangInfo & pLangInfo,unsigned short & result)939 int MsgTextConvert::EscapePortuLockingToUCS2(
940     const unsigned char *pSrcText, int srcLen, const MsgLangInfo &pLangInfo, unsigned short &result)
941 {
942     int index = 0;
943     if (pSrcText == nullptr || srcLen <= 0) {
944         return index;
945     }
946     /* Portuguese National Language Locking Shift Table -> UCS2 */
947     const WCHAR g_portuLockingToUCS2[] = { /* @ */
948         0x0040, 0x00A3, 0x0024, 0x00A5, 0x00EA, 0x00E9, 0x00FA, 0x00ED, 0x00F3, 0x00E7, 0x000A, 0x00D4, 0x00F4, 0x000D,
949         0x00C1, 0x00E1, 0x0394, 0x005F, 0x0020, 0x00C7, 0x00C0, 0x0020, 0x005E, 0x005C, 0x20AC, 0x00D3, 0x007C, 0x001B,
950         0x00C2, 0x00E2, 0x00CA, 0x00C9,
951         /* SP */
952         0x0020, 0x0021, 0x0022, 0x0023, 0x00A4, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002A, 0x002B, 0x002C, 0x002D,
953         0x002E, 0x002F,
954         /* 0 */
955         0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037, 0x0038, 0x0039, 0x003A, 0x003B, 0x003C, 0x003D,
956         0x003E, 0x003F, 0x00CD, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047, 0x0048, 0x0049, 0x004A, 0x004B,
957         0x004C, 0x004D, 0x004E, 0x004F,
958         /* P */
959         0x0050, 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059, 0x005A, 0x00C3, 0x00D5, 0x00DA,
960         0x00DC, 0x00A7, 0x00BF, 0x0061, 0x0062, 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006A, 0x006B,
961         0x006C, 0x006D, 0x006E, 0x006F,
962         /* p */
963         0x0070, 0x0071, 0x0072, 0x0073, 0x0074, 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007A, 0x00E3, 0x00F5, 0x0020,
964         0x00FC, 0x00E0
965     };
966     if (g_portuLockingToUCS2[pSrcText[index]] == 0x001B) {
967         index++;
968         if (index > srcLen) {
969             return index;
970         }
971         result = EscapeToUCS2(pSrcText[index], pLangInfo);
972     } else { /* PORTUGUESE - National Language Locking Shift */
973         result = g_portuLockingToUCS2[pSrcText[index]];
974     }
975     return index;
976 }
977 
EscapeGSM7BitToUCS2(const unsigned char * pSrcText,int srcLen,const MsgLangInfo & pLangInfo,unsigned short & result)978 int MsgTextConvert::EscapeGSM7BitToUCS2(
979     const unsigned char *pSrcText, int srcLen, const MsgLangInfo &pLangInfo, unsigned short &result)
980 {
981     int index = 0;
982     if (pSrcText == nullptr || srcLen <= 0) {
983         return index;
984     }
985     if (GSM7_BIT_TO_UC_S2[pSrcText[index]] == 0x001B) {
986         index++;
987         if (index > srcLen) {
988             return index;
989         }
990         result = EscapeToUCS2(pSrcText[index], pLangInfo);
991     } else {
992         result = GSM7_BIT_TO_UC_S2[pSrcText[index]];
993     }
994     return index;
995 }
996 
EscapeToUCS2(const unsigned char srcText,const MsgLangInfo & pLangInfo)997 unsigned short MsgTextConvert::EscapeToUCS2(const unsigned char srcText, const MsgLangInfo &pLangInfo)
998 {
999     unsigned short result = 0;
1000     if (pLangInfo.bSingleShift) { /* National Language Single Shift */
1001         TELEPHONY_LOGI("Single Shift [%{public}d]", pLangInfo.singleLang);
1002         switch (pLangInfo.singleLang) {
1003             case MSG_ID_TURKISH_LANG:
1004                 GetTurkishSingleToUCS2(srcText, result);
1005                 break;
1006             case MSG_ID_SPANISH_LANG:
1007                 GetSpanishSingleToUCS2(srcText, result);
1008                 break;
1009             case MSG_ID_PORTUGUESE_LANG:
1010                 GetPortuSingleToUCS2(srcText, result);
1011                 break;
1012             default:
1013                 GetGsm7BitExtToUCS2(srcText, result);
1014                 break;
1015         }
1016     } else { /* GSM 7 bit Default Alphabet Extension Table */
1017         GetGsm7BitExtToUCS2(srcText, result);
1018     }
1019     return result;
1020 }
1021 
GetTurkishSingleToUCS2(const unsigned char & srcText,unsigned short & result)1022 void MsgTextConvert::GetTurkishSingleToUCS2(const unsigned char &srcText, unsigned short &result)
1023 {
1024     /* Turkish National Language Single Shift Table -> UCS2 */
1025     const WCHAR g_turkishSingleToUCS2[] = { /* 0x0020 -> (SP) for invalid code */
1026         /* Page Break */
1027         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x000C, 0x0020, 0x0020, 0x0020,
1028         0x0020, 0x0020,
1029         /* ^ */
1030         0x0020, 0x0020, 0x0020, 0x0020, 0x005E, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x001B, 0x0020, 0x0020,
1031         0x0020, 0x0020,
1032         /* { */ /* } */
1033         /* \ */
1034         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x007B, 0x007D, 0x0020, 0x0020, 0x0020, 0x0020,
1035         0x0020, 0x005C,
1036         /* [ */ /* ~ */ /* ] */
1037         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x005B, 0x007E,
1038         0x005D, 0x0020,
1039         /* | */
1040         0x007C, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x011E, 0x0020, 0x0130, 0x0020, 0x0020, 0x0020, 0x0020,
1041         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x015E, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
1042         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00E7, 0x0020, 0x20AC, 0x0020, 0x011F, 0x0020, 0x0131,
1043         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x015F, 0x0020, 0x0020, 0x0020, 0x0020,
1044         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020
1045     };
1046     result = g_turkishSingleToUCS2[srcText];
1047 }
1048 
GetSpanishSingleToUCS2(const unsigned char & srcText,unsigned short & result)1049 void MsgTextConvert::GetSpanishSingleToUCS2(const unsigned char &srcText, unsigned short &result)
1050 {
1051     /* Spanish National Language Single Shift Table -> UCS2 */
1052     const WCHAR g_spanishSingleToUCS2[] = { /* 0x0020 -> (SP) for invalid code */
1053         /* Page Break */
1054         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00E7, 0x000C, 0x0020, 0x0020, 0x0020,
1055         0x0020, 0x0020,
1056         /* ^ */
1057         0x0020, 0x0020, 0x0020, 0x0020, 0x005E, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x001B, 0x0020, 0x0020,
1058         0x0020, 0x0020,
1059         /* { */ /* } */
1060         /* \ */
1061         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x007B, 0x007D, 0x0020, 0x0020, 0x0020, 0x0020,
1062         0x0020, 0x005C,
1063         /* [ */ /* ~ */ /* ] */
1064         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x005B, 0x007E,
1065         0x005D, 0x0020,
1066         /* | */
1067         0x007C, 0x00C1, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00CD, 0x0020, 0x0020, 0x0020, 0x0020,
1068         0x0020, 0x00D3, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00DA, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
1069         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00E1, 0x0020, 0x0020, 0x0020, 0x20AC, 0x0020, 0x0020, 0x0020, 0x00ED,
1070         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00F3, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00FA, 0x0020, 0x0020,
1071         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020
1072     };
1073     result = g_spanishSingleToUCS2[srcText];
1074 }
1075 
GetGsm7BitExtToUCS2(const unsigned char & srcText,unsigned short & result)1076 void MsgTextConvert::GetGsm7BitExtToUCS2(const unsigned char &srcText, unsigned short &result)
1077 {
1078     /* GSM 7 bit Default Alphabet Extension Table -> UCS2 */
1079     const WCHAR g_gsm7BitExtToUCS2[] = { /* 0x0020 -> (SP) for invalid code */
1080         /* Page Break */
1081         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x000C, 0x0020, 0x0020, 0x0020,
1082         0x0020, 0x0020,
1083         /* ^ */
1084         0x0020, 0x0020, 0x0020, 0x0020, 0x005E, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x001B, 0x0020, 0x0020,
1085         0x0020, 0x0020,
1086         /* { */ /* } */
1087         /* \ */
1088         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x007B, 0x007D, 0x0020, 0x0020, 0x0020, 0x0020,
1089         0x0020, 0x005C,
1090         /* [ */ /* ~ */ /* ] */
1091         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x005B, 0x007E,
1092         0x005D, 0x0020,
1093         /* | */
1094         0x007C, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
1095         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
1096         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x20AC, 0x0020, 0x0020, 0x0020, 0x0020,
1097         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020,
1098         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020
1099     };
1100     result = g_gsm7BitExtToUCS2[srcText];
1101 }
1102 
GetPortuSingleToUCS2(const unsigned char & srcText,unsigned short & result)1103 void MsgTextConvert::GetPortuSingleToUCS2(const unsigned char &srcText, unsigned short &result)
1104 {
1105     /* Portuguese National Language Single Shift Table -> UCS2 */
1106     const WCHAR g_portuSingleToUCS2[] = { /* 0x0020 -> (SP) for invalid code */
1107         /* Page Break */
1108         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00EA, 0x0020, 0x0020, 0x0020, 0x00E7, 0x000C, 0x00D4, 0x00F4, 0x0020,
1109         0x00C1, 0x00E1,
1110         /* ^ */
1111         0x0020, 0x0020, 0x03A6, 0x0393, 0x005E, 0x03A9, 0x03A0, 0x03A8, 0x03A3, 0x0398, 0x0020, 0x001B, 0x0020, 0x0020,
1112         0x0020, 0x00CA,
1113         /* { */ /* } */
1114         /* \ */
1115         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x007B, 0x007D, 0x0020, 0x0020, 0x0020, 0x0020,
1116         0x0020, 0x005C,
1117         /* [ */ /* ~ */ /* ] */
1118         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x005B, 0x007E,
1119         0x005D, 0x0020,
1120         /* | */
1121         0x007C, 0x00C0, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00CD, 0x0020, 0x0020, 0x0020, 0x0020,
1122         0x0020, 0x00D3, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00DA, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00C3,
1123         0x00D5, 0x0020, 0x0020, 0x0020, 0x0020, 0x00C2, 0x0020, 0x0020, 0x0020, 0x20AC, 0x0020, 0x0020, 0x0020, 0x00ED,
1124         0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00F3, 0x0020, 0x0020, 0x0020, 0x0020, 0x0020, 0x00FA, 0x0020, 0x0020,
1125         0x0020, 0x0020, 0x0020, 0x00E3, 0x00F5, 0x0020, 0x0020, 0x00E2
1126     };
1127     result = g_portuSingleToUCS2[srcText];
1128 }
1129 
ConvertDumpTextToHex(const unsigned char * pText,int length)1130 void MsgTextConvert::ConvertDumpTextToHex(const unsigned char *pText, int length)
1131 {
1132     if (pText == nullptr) {
1133         return;
1134     }
1135     TELEPHONY_LOGI("=======================================");
1136     TELEPHONY_LOGI("   Dump Text To Hex - Length :%{public}d", length);
1137     TELEPHONY_LOGI("=======================================");
1138     for (int i = 0; i < length; i++) {
1139         TELEPHONY_LOGI("[%{public}02x]", pText[i]);
1140     }
1141     TELEPHONY_LOGI("=======================================");
1142 }
1143 
Base64Encode(const std::string & src,std::string & dest)1144 void MsgTextConvert::Base64Encode(const std::string &src, std::string &dest)
1145 {
1146     gchar *encode_data = g_base64_encode((guchar *)src.data(), src.length());
1147     if (encode_data == nullptr) {
1148         return;
1149     }
1150     gsize out_len = 0;
1151     out_len = strlen(encode_data);
1152     std::string temp(static_cast<char *>(encode_data), out_len);
1153     dest = temp;
1154 
1155     if (encode_data != nullptr) {
1156         g_free(encode_data);
1157     }
1158 }
1159 
Base64Decode(const std::string & src,std::string & dest)1160 void MsgTextConvert::Base64Decode(const std::string &src, std::string &dest)
1161 {
1162     gsize out_len = 0;
1163     char *decodeData = reinterpret_cast<char *>(g_base64_decode(src.data(), &out_len));
1164     if (decodeData == nullptr) {
1165         return;
1166     }
1167     std::string temp(decodeData, out_len);
1168     dest = temp;
1169 
1170     if (decodeData != nullptr) {
1171         g_free(decodeData);
1172     }
1173 }
1174 
GetEncodeString(std::string & encodeString,uint32_t charset,uint32_t valLength,const std::string & strEncodeString)1175 bool MsgTextConvert::GetEncodeString(
1176     std::string &encodeString, uint32_t charset, uint32_t valLength, const std::string &strEncodeString)
1177 {
1178     bool ret = false;
1179     char *pDest = nullptr;
1180     gsize bytes_read = 0;
1181     gsize bytes_written = 0;
1182     GError *error = nullptr;
1183     std::string strToCodeset("UTF-8");
1184     std::string strFromCodeset;
1185     auto charSetInstance = DelayedSingleton<MmsCharSet>::GetInstance();
1186     if (charSetInstance == nullptr || (!charSetInstance->GetCharSetStrFromInt(strFromCodeset, charset))) {
1187         strFromCodeset = "UTF-8";
1188     }
1189     pDest = g_convert(strEncodeString.c_str(), valLength, strToCodeset.c_str(), strFromCodeset.c_str(), &bytes_read,
1190         &bytes_written, &error);
1191     if (!error) {
1192         encodeString = std::string(pDest, bytes_written);
1193         ret = true;
1194     } else {
1195         TELEPHONY_LOGE("EncodeString charset convert fail.");
1196         ret = false;
1197     }
1198     if (pDest != nullptr) {
1199         g_free(pDest);
1200     }
1201     return ret;
1202 }
1203 } // namespace Telephony
1204 } // namespace OHOS