2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FText_Ucs2EncodingCore.cpp
20 * @brief This is the implementation file for _Ucs2EncodingCore class.
24 #include <unique_ptr.h>
25 #include <FBaseSysLog.h>
26 #include "FText_Ucs2EncodingCore.h"
29 using namespace Tizen::Base;
31 #define UCS2_BOM_SIZE 2
32 #define SUPPORTED_ENCODINGS_LENGTH 3
33 static String supportedEncodings[] = {
34 "UCS2", "UCS2BE", "UCS2LE"
37 namespace Tizen { namespace Text
40 _Ucs2EncodingCore::_Ucs2EncodingCore(void)
41 : __encodingType(UCS2_ENCODING_BE)
42 , __bomType(UCS2_BOM_NONE)
48 _Ucs2EncodingCore::~_Ucs2EncodingCore(void)
53 _Ucs2EncodingCore::GetEncodingCoreImplN(const Tizen::Base::String& encodingFrom, const Tizen::Base::String& encodingTo)
55 String encodingStr(encodingFrom);
56 encodingStr.ToUpper();
58 encodingStr.Replace(L"-", "");
59 encodingStr.Replace("_", "");
61 if (!encodingStr.StartsWith(L"UCS2", 0))
63 SetLastResult(E_UNSUPPORTED_TYPE);
67 if ((encodingStr == "UCS2") || (encodingStr == "UCS2BE") || (encodingStr == "UCS2LE"))
69 std::unique_ptr<_Ucs2EncodingCore> pEncodingCore(new (std::nothrow) _Ucs2EncodingCore);
70 SysTryReturn(NID_TEXT, pEncodingCore, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
72 if (encodingStr == "UCS2")
74 pEncodingCore->__encodingType = UCS2_ENCODING_LE; //UCS2_ENCODING;
77 if (encodingStr == "UCS2BE")
79 pEncodingCore->__encodingType = UCS2_ENCODING_BE;
82 if (encodingStr == "UCS2LE")
84 pEncodingCore->__encodingType = UCS2_ENCODING_LE;
87 pEncodingCore->__encodingName = encodingFrom;
89 return pEncodingCore.release();
96 _Ucs2EncodingCore::GetAvailableEncodingsN(Tizen::Base::Collection::IList* pEncodingList)
98 SysTryReturnResult(NID_TEXT, pEncodingList, E_INVALID_ARG,
99 "[%s] Invalid argument is used. Input encoding list is null", GetErrorMessage(E_INVALID_ARG));
101 result r = E_SUCCESS;
102 String* pEncodingStr = null;
103 for (int i = 0; i < SUPPORTED_ENCODINGS_LENGTH; i++)
105 pEncodingStr = new (std::nothrow) String(supportedEncodings[i]);
106 SysTryReturnResult(NID_TEXT, pEncodingStr, E_OUT_OF_MEMORY, "Unable to allocate memory for encoding string.");
108 r = pEncodingList->Add(*pEncodingStr);
120 _Ucs2EncodingCore::GetName(void)
122 return __encodingName;
126 _Ucs2EncodingCore::GetByteCount(const wchar_t* pSrc, int srcLength, int& retLength)
129 wchar_t* pUnicodeStr = (wchar_t*) pSrc;
130 SysTryReturnResult(NID_TEXT, (pUnicodeStr != null), E_INVALID_ARG,
131 "[%s] Invalid argument is used. Input string pSrc is null.", GetErrorMessage(E_INVALID_ARG));
133 int ucs2Size = srcLength * 2;
134 while (srcLength > 0)
136 SysTryReturnResult(NID_TEXT, (*pUnicodeStr <= 0xFFFF), E_INVALID_ENCODING_RANGE, "[%s] Encoding failed. pSrc(%ls) contains code(%x) pointing outside of bounds by UCS2 encdoing.", GetErrorMessage(E_INVALID_ENCODING_RANGE), pSrc, *pUnicodeStr);
137 SysTryReturnResult(NID_TEXT, (*pUnicodeStr < 0xD800 || *pUnicodeStr > 0xDFFF),
138 E_INVALID_ENCODING_RANGE,
139 "[%s] Encoding failed. pSrc(%ls) contains code(%x) pointing outside of bounds by GSM encdoing.", GetErrorMessage(E_INVALID_ENCODING_RANGE), pSrc, *pUnicodeStr);
145 if (__encodingType == UCS2_ENCODING)
147 ucs2Size += UCS2_BOM_SIZE;
150 retLength = ucs2Size;
155 _Ucs2EncodingCore::GetCharCount(const byte* pSrc, int srcLength, int& retLength)
158 byte* pUcs2Byte = (byte*) pSrc;
159 if (pUcs2Byte != null)
161 if (__encodingType == UCS2_ENCODING && srcLength > 2)
163 if ((pUcs2Byte[0] == 0xFE) && (pUcs2Byte[1] == 0xFF))
165 __bomType = UCS2_BOM_BE;
169 else if ((pUcs2Byte[0] == 0xFF) && (pUcs2Byte[1] == 0xFE))
171 __bomType = UCS2_BOM_LE;
177 __bomType = UCS2_BOM_NONE;
181 if ((srcLength == 0x01) && (pUcs2Byte[srcLength - 1] == '\0'))
187 if ((srcLength & 0x01) && (pUcs2Byte[srcLength - 1] != '\0'))
189 return E_INVALID_ENCODING_RANGE;
192 int uniSize = srcLength >> 1;
193 while (srcLength > 0)
195 if (__encodingType == UCS2_ENCODING_BE || __bomType == UCS2_BOM_BE)
197 if (!(*pUcs2Byte < 0xD8 || *pUcs2Byte > 0xDF))
199 return E_INVALID_ENCODING_RANGE;
206 if (!(*pUcs2Byte < 0xD8 || *pUcs2Byte > 0xDF))
208 return E_INVALID_ENCODING_RANGE;
219 return E_INVALID_ARG;
223 _Ucs2EncodingCore::EncodeN(const wchar_t* pSrc, int srcLength, int& retLength)
225 result r = GetByteCount(pSrc, srcLength, retLength);
226 SysTryReturn(NID_TEXT, retLength >= 0, null, r,
227 "[%s] Encoding Failed. retLength(%d) can not be negative", GetErrorMessage(r), retLength);
229 std::unique_ptr<byte[]>pUcs2Byte(new (std::nothrow) byte[retLength + 1]);
230 SysTryReturn(NID_TEXT, pUcs2Byte, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
232 wchar_t* pUnicodeStr = (wchar_t*) pSrc;
233 byte* pCurrPos = pUcs2Byte.get();
234 if (__encodingType == UCS2_ENCODING)
242 if (__encodingType == UCS2_ENCODING_BE)
244 while (srcLength > 0)
246 *pCurrPos = (byte) ((*pUnicodeStr & 0xFF00) >> 8);
247 *(++pCurrPos) = (byte) (*pUnicodeStr & 0x00FF);
256 while (srcLength > 0)
258 *pCurrPos = (byte) (*pUnicodeStr & 0x00FF);
259 *(++pCurrPos) = (byte) ((*pUnicodeStr & 0xFF00) >> 8);
267 SetLastResult(E_SUCCESS);
268 return pUcs2Byte.release();
272 _Ucs2EncodingCore::DecodeN(const byte* pSrc, int srcLength, int& retLength)
274 result r = GetCharCount(pSrc, srcLength, retLength);
275 SysTryReturn(NID_TEXT, retLength >= 0, null, r, "[%s] Encoding Failed", GetErrorMessage(r));
277 byte* pUcs2Byte = (byte*) pSrc;
278 std::unique_ptr<wchar_t[]> pUnicodeStr( new (std::nothrow) wchar_t[retLength + 1]);
280 SysTryReturn(NID_TEXT, pUnicodeStr, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
282 if (__encodingType == UCS2_ENCODING)
284 if (__bomType != UCS2_BOM_NONE)
291 wchar_t* pCurrPos = pUnicodeStr.get();
292 if (__encodingType == UCS2_ENCODING_BE || __bomType == UCS2_BOM_BE)
294 while (srcLength > 0)
296 *pCurrPos = (((wchar_t) *pUcs2Byte) << 8);
297 *pCurrPos = *pCurrPos | ((wchar_t) *(++pUcs2Byte));
306 while (srcLength > 0)
308 *pCurrPos = ((wchar_t) *pUcs2Byte);
309 *pCurrPos = *pCurrPos | (((wchar_t) *(++pUcs2Byte)) << 8);
317 pUnicodeStr[retLength] = '\0';
318 SetLastResult(E_SUCCESS);
319 __bomType = UCS2_BOM_NONE;
320 return pUnicodeStr.release();
324 _Ucs2EncodingCore::GetMaxByteCount(int charCount) const
326 return charCount * 2;
330 _Ucs2EncodingCore::GetMaxCharCount(int byteCount) const