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