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