2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @file FText_Ucs2EncodingCore.cpp
19 * @brief This is the implementation file for _Ucs2EncodingCore class.
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include "FText_Ucs2EncodingCore.h"
28 using namespace Tizen::Base;
30 #define UCS2_BOM_SIZE 2
32 namespace Tizen { namespace Text
35 _Ucs2EncodingCore::_Ucs2EncodingCore(void)
36 : __encodingType(UCS2_ENCODING_BE)
37 , __bomType(UCS2_BOM_NONE)
43 _Ucs2EncodingCore::~_Ucs2EncodingCore(void)
48 _Ucs2EncodingCore::GetEncodingCoreImplN(const Tizen::Base::String& encodingFrom, const Tizen::Base::String& encodingTo)
50 String encodingStr(encodingFrom);
51 encodingStr.ToUpper();
53 encodingStr.Replace(L"-", "");
54 encodingStr.Replace("_", "");
56 SysTryReturn(NID_TEXT, encodingStr.StartsWith(L"UCS2", 0), null, E_UNSUPPORTED_TYPE, "[E_UNSUPPORTED_TYPE] It is not the UCS2 type.");
58 if ((encodingStr == "UCS2") || (encodingStr == "UCS2BE") || (encodingStr == "UCS2LE"))
60 std::unique_ptr<_Ucs2EncodingCore> pEncodingCore(new (std::nothrow) _Ucs2EncodingCore);
61 SysTryReturn(NID_TEXT, pEncodingCore, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
63 if (encodingStr == "UCS2")
65 pEncodingCore->__encodingType = UCS2_ENCODING_LE; //UCS2_ENCODING;
68 if (encodingStr == "UCS2BE")
70 pEncodingCore->__encodingType = UCS2_ENCODING_BE;
73 if (encodingStr == "UCS2LE")
75 pEncodingCore->__encodingType = UCS2_ENCODING_LE;
78 pEncodingCore->__encodingName = encodingFrom;
80 return pEncodingCore.release();
88 _Ucs2EncodingCore::GetName(void)
90 return __encodingName;
94 _Ucs2EncodingCore::GetByteCount(const wchar_t* pSrc, int srcLength, int& retLength)
97 wchar_t* pUnicodeStr = (wchar_t*) pSrc;
98 SysTryReturnResult(NID_TEXT, (pUnicodeStr != null), E_INVALID_ARG,
99 "[%s] Invalid argument is used. Input string pSrc is null.", GetErrorMessage(E_INVALID_ARG));
101 int ucs2Size = srcLength * 2;
102 while (srcLength > 0)
104 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);
105 SysTryReturnResult(NID_TEXT, (*pUnicodeStr < 0xD800 || *pUnicodeStr > 0xDFFF),
106 E_INVALID_ENCODING_RANGE,
107 "[%s] Encoding failed. pSrc(%ls) contains code(%x) pointing outside of bounds by GSM encdoing.", GetErrorMessage(E_INVALID_ENCODING_RANGE), pSrc, *pUnicodeStr);
113 if (__encodingType == UCS2_ENCODING)
115 ucs2Size += UCS2_BOM_SIZE;
118 retLength = ucs2Size;
123 _Ucs2EncodingCore::GetCharCount(const byte* pSrc, int srcLength, int& retLength)
126 byte* pUcs2Byte = (byte*) pSrc;
127 if (pUcs2Byte != null)
129 if (__encodingType == UCS2_ENCODING && srcLength > 2)
131 if ((pUcs2Byte[0] == 0xFE) && (pUcs2Byte[1] == 0xFF))
133 __bomType = UCS2_BOM_BE;
137 else if ((pUcs2Byte[0] == 0xFF) && (pUcs2Byte[1] == 0xFE))
139 __bomType = UCS2_BOM_LE;
145 __bomType = UCS2_BOM_NONE;
149 if ((srcLength == 0x01) && (pUcs2Byte[srcLength - 1] == '\0'))
155 if ((srcLength & 0x01) && (pUcs2Byte[srcLength - 1] != '\0'))
157 return E_INVALID_ENCODING_RANGE;
160 int uniSize = srcLength >> 1;
161 while (srcLength > 0)
163 if (__encodingType == UCS2_ENCODING_BE || __bomType == UCS2_BOM_BE)
165 if (!(*pUcs2Byte < 0xD8 || *pUcs2Byte > 0xDF))
167 return E_INVALID_ENCODING_RANGE;
174 if (!(*pUcs2Byte < 0xD8 || *pUcs2Byte > 0xDF))
176 return E_INVALID_ENCODING_RANGE;
187 return E_INVALID_ARG;
191 _Ucs2EncodingCore::EncodeN(const wchar_t* pSrc, int srcLength, int& retLength)
193 result r = GetByteCount(pSrc, srcLength, retLength);
194 SysTryReturn(NID_TEXT, retLength >= 0, null, r,
195 "[%s] Encoding Failed. retLength(%d) can not be negative", GetErrorMessage(r), retLength);
197 std::unique_ptr<byte[]>pUcs2Byte(new (std::nothrow) byte[retLength + 1]);
198 SysTryReturn(NID_TEXT, pUcs2Byte, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
200 wchar_t* pUnicodeStr = (wchar_t*) pSrc;
201 byte* pCurrPos = pUcs2Byte.get();
202 if (__encodingType == UCS2_ENCODING)
210 if (__encodingType == UCS2_ENCODING_BE)
212 while (srcLength > 0)
214 *pCurrPos = (byte) ((*pUnicodeStr & 0xFF00) >> 8);
215 *(++pCurrPos) = (byte) (*pUnicodeStr & 0x00FF);
224 while (srcLength > 0)
226 *pCurrPos = (byte) (*pUnicodeStr & 0x00FF);
227 *(++pCurrPos) = (byte) ((*pUnicodeStr & 0xFF00) >> 8);
235 SetLastResult(E_SUCCESS);
236 return pUcs2Byte.release();
240 _Ucs2EncodingCore::DecodeN(const byte* pSrc, int srcLength, int& retLength)
242 result r = GetCharCount(pSrc, srcLength, retLength);
243 SysTryReturn(NID_TEXT, retLength >= 0, null, r, "[%s] Encoding Failed", GetErrorMessage(r));
245 byte* pUcs2Byte = (byte*) pSrc;
246 std::unique_ptr<wchar_t[]> pUnicodeStr( new (std::nothrow) wchar_t[retLength + 1]);
248 SysTryReturn(NID_TEXT, pUnicodeStr, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
250 if (__encodingType == UCS2_ENCODING)
252 if (__bomType != UCS2_BOM_NONE)
259 wchar_t* pCurrPos = pUnicodeStr.get();
260 if (__encodingType == UCS2_ENCODING_BE || __bomType == UCS2_BOM_BE)
262 while (srcLength > 0)
264 *pCurrPos = (((wchar_t) *pUcs2Byte) << 8);
265 *pCurrPos = *pCurrPos | ((wchar_t) *(++pUcs2Byte));
274 while (srcLength > 0)
276 *pCurrPos = ((wchar_t) *pUcs2Byte);
277 *pCurrPos = *pCurrPos | (((wchar_t) *(++pUcs2Byte)) << 8);
285 pUnicodeStr[retLength] = '\0';
286 SetLastResult(E_SUCCESS);
287 __bomType = UCS2_BOM_NONE;
288 return pUnicodeStr.release();
292 _Ucs2EncodingCore::GetMaxByteCount(int charCount) const
294 return charCount * 2;
298 _Ucs2EncodingCore::GetMaxCharCount(int byteCount) const