update tizen source
[framework/messaging/msg-service.git] / plugin / mms_plugin / LanguagePack / MmsPluginWmLngUTF8Uni.cpp
1 /*
2 *
3 * Copyright (c) 2000-2012 Samsung Electronics Co., Ltd. All Rights Reserved.
4 *
5 * This file is part of msg-service.
6 *
7 * Contact: Jaeyun Jeong <jyjeong@samsung.com>
8 *          Sangkoo Kim <sangkoo.kim@samsung.com>
9 *          Seunghwan Lee <sh.cat.lee@samsung.com>
10 *          SoonMin Jung <sm0415.jung@samsung.com>
11 *          Jae-Young Lee <jy4710.lee@samsung.com>
12 *          KeeBum Kim <keebum.kim@samsung.com>
13 *
14 * PROPRIETARY/CONFIDENTIAL
15 *
16 * This software is the confidential and proprietary information of
17 * SAMSUNG ELECTRONICS ("Confidential Information"). You shall not
18 * disclose such Confidential Information and shall use it only in
19 * accordance with the terms of the license agreement you entered
20 * into with SAMSUNG ELECTRONICS.
21 *
22 * SAMSUNG make no representations or warranties about the suitability
23 * of the software, either express or implied, including but not limited
24 * to the implied warranties of merchantability, fitness for a particular
25 * purpose, or non-infringement. SAMSUNG shall not be liable for any
26 * damages suffered by licensee as a result of using, modifying or
27 * distributing this software or its derivatives.
28 *
29 */
30
31 #include "MsgDebug.h"
32 #include "MmsPluginWmLngPack.h"
33
34
35 /* Local function */
36 int __WmLngUTF2Unicode (unsigned short *des,unsigned char *src, int nChar);
37
38
39 int __WmLngStrlenByByte(UCHAR *szInText, int byteCount)
40 {
41         int nCount = 0;
42         while (byteCount > 0 && (*szInText != '\0')) {
43                 if (*szInText < 0x80) {
44                         szInText++;
45                         byteCount--;
46                 } else if  (((0xC0 <= *szInText) && (*szInText < 0xE0)) && (*(szInText+1) >= 0x80)) {
47                         szInText += 2;
48                         byteCount -= 2;
49                 } else if  ((*szInText >= 0xE0) && (*(szInText+1) >= 0x80) && (*(szInText+2) >= 0x80)) {
50                         szInText += 3;
51                         byteCount -= 3;
52                 } else {
53                         szInText++;
54                         byteCount--;
55                         MSG_DEBUG("__WmLngStrlenByByte: utf8 incorrect range!\n");
56                 }
57                 nCount++;
58         }
59         return (nCount+1)*sizeof(MCHAR);
60 }
61
62 /**
63  * This function convert character n'th byte UTF8 character to ProcessCode(unicode)
64  *
65  * @param       pmszOutText             :       Output Buffer Pointer to ProcessCode(UniCode)
66  * @param       szInText                :       Input  Buffer Pointer to UTF8
67  * @param       byteCount               :       byte number for converting the character
68  * @return                                      :       This function returns true on success, or false
69  *                                                              on failure.
70  *
71  * @see         WmConvertPCode2UTF
72  */
73 bool WmConvertUTF2PCode (MCHAR *pmszOutText, int outBufSize, UCHAR *szInText, int byteCount)
74 {
75         int length;
76         if (byteCount == 0) {
77                 pmszOutText[0] = NULL;
78                 return true;
79         }
80
81         if (__WmConvertCodeBufferSizeCheck((char*)pmszOutText, outBufSize, __WmLngStrlenByByte((UCHAR*)szInText, byteCount)) == false) {
82                 MSG_DEBUG("WmConvertUTF2PCode: Out buffer size seems to be small!\n");
83                 return false;
84         }
85
86         length = __WmLngUTF2Unicode (pmszOutText, szInText, byteCount);
87         if (length == -1) {
88                 MSG_DEBUG("WmConvertUTF2PCode: __WmLngUTF2Unicode returns false!\n");
89                 return false;
90         } else {
91                 return true;
92         }
93 }
94
95
96 /*
97  * change 1byte-encoded-UTF8 character to Unicode
98  * @param       byte1   :       1byte character code
99  * @return                      :       result of bit operation
100  *
101  *
102  * @see
103  */
104 unsigned short __WmLngConvert1ByteChar (unsigned char byte1)
105 {
106         unsigned short result;
107
108         result = 0;
109         result = (byte1 & 0x7F);
110
111         return result;
112 }
113
114 /*
115  * change 2byte-encoded-UTF8 character to Unicode
116  * @param       byte1   :       1'st byte character code
117  * @param       byte2   :       2'st byte character code
118  * @return                      :       result of bit operation
119  *
120  * @see
121  */
122 unsigned short __WmLngConvert2ByteChar (unsigned char byte1, unsigned char byte2)
123 {
124         unsigned short result;
125         unsigned char hi;
126         unsigned char lo;
127
128         result = 0;
129
130         hi = byte1 & 0x1F;
131         lo = byte2 & 0x3F;
132
133         result = (hi << 6) | lo;
134
135         return result;
136 }
137
138 /*
139  * change 3byte-encoded-UTF8 character to Unicode
140  * @param       byte1   :       1'st character code
141  * @param       byte2   :       2'st character code
142  * @param       byte3   :       3'st character code
143  * @return                      :       result of bit operation
144  *
145  * @see
146  */
147 unsigned short __WmLngConvert3ByteChar (unsigned char byte1, unsigned char byte2, unsigned char byte3)
148 {
149         unsigned short result;
150         unsigned char hi;
151         unsigned char mid;
152         unsigned char lo;
153
154         result = 0;
155
156         hi = byte1 & 0x0F;
157         mid = byte2 & 0x3F;
158         lo = byte3 & 0x3F;
159
160         result = (hi << 12) | (mid << 6) | lo;
161
162         return result;
163 }
164
165 /*
166  * This function convert character UTF8 to ProcessCode(unicode)
167  *
168  * @param       des             :       Output Buffer Pointer to ProcessCode(UniCode)
169  * @param       src             :       Input  Buffer Pointer to UTF8
170  * @param       nChar   :       number for convert n'th chararcter
171  * @return                      :       This function returns number for convert n'th chararcter on success, or -1
172  *                                              on failure.
173  *
174  * @see
175  */
176
177 int __WmLngUTF2Unicode (unsigned short *des,unsigned char *src, int nChar)
178 {
179         unsigned short *org;
180
181         org = des;
182
183         while (nChar > 0 && (*src != '\0')) {
184                 if (*src < 0x80) {
185                         *des = __WmLngConvert1ByteChar (*src);
186
187                         des++;
188                         src++;
189                         nChar--;
190                 } else if  (((0xC0 <= *src) && (*src < 0xE0)) && (*(src+1) >= 0x80)) {
191                         *des = __WmLngConvert2ByteChar (*src, *(src+1));
192
193                         des++;
194                         src += 2;
195                         nChar -= 2;
196                 } else if  ((*src >= 0xE0) && (*(src+1) >= 0x80) && (*(src+2) >= 0x80)) {
197                         *des = __WmLngConvert3ByteChar (*src, *(src+1), *(src+2));
198
199                         des++;
200
201                         src += 3;
202                         nChar -= 3;
203                 } else {
204                         *des = __WmLngConvert1ByteChar (*src);
205                         des++;
206                         src++;
207                         nChar--;
208                         MSG_DEBUG("__WmLngUTF2Unicode: utf8 incorrect range!\n");
209                 }
210         }
211         *des = 0;
212         return (des - org);
213 }
214
215