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_Iso885916EncodingCore.cpp
19 * @brief This is the implementation file for _Iso885916EncodingCore class.
23 #include <unique_ptr.h>
24 #include <FBaseSysLog.h>
25 #include "FText_Iso885916EncodingCore.h"
28 using namespace Tizen::Base;
30 namespace Tizen { namespace Text
33 static const wchar_t ISO_8859_16_UNICODE_TABLE[] =
35 0x00A0, 0x0104, 0x0105, 0x0141, 0x20AC, 0x201E, 0x0160, 0x00A7,
36 0x0161, 0x00A9, 0x0218, 0x00AB, 0x0179, 0x00AD, 0x017A, 0x017B,
37 0x00B0, 0x00B1, 0x010C, 0x0142, 0x017D, 0x201D, 0x00B6, 0x00B7,
38 0x017E, 0x010D, 0x0219, 0x00BB, 0x0152, 0x0153, 0x0178, 0x017C,
39 0x00C0, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0106, 0x00C6, 0x00C7,
40 0x00C8, 0x00C9, 0x00CA, 0x00CB, 0x00CC, 0x00CD, 0x00CE, 0x00CF,
41 0x0110, 0x0143, 0x00D2, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x015A,
42 0x0170, 0x00D9, 0x00DA, 0x00DB, 0x00DC, 0x0118, 0x021A, 0x00DF,
43 0x00E0, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x0107, 0x00E6, 0x00E7,
44 0x00E8, 0x00E9, 0x00EA, 0x00EB, 0x00EC, 0x00ED, 0x00EE, 0x00EF,
45 0x0111, 0x0144, 0x00F2, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x015B,
46 0x0171, 0x00F9, 0x00FA, 0x00FB, 0x00FC, 0x0119, 0x021B, 0x00FF
50 _Iso885916EncodingCore::_Iso885916EncodingCore(void)
55 _Iso885916EncodingCore::~_Iso885916EncodingCore(void)
60 _Iso885916EncodingCore::GetEncodingCoreImplN(const Tizen::Base::String& encodingFrom, const Tizen::Base::String& encodingTo)
62 SysTryReturn(NID_TEXT, encodingFrom == L"ISO-8859-16", null, E_UNSUPPORTED_TYPE, "[E_UNSUPPORTED_TYPE] It is the unsupported type.");
63 _Iso885916EncodingCore* pEncodingCore = new (std::nothrow) _Iso885916EncodingCore;
64 SysTryReturn(NID_TEXT, pEncodingCore, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
70 _Iso885916EncodingCore::GetName(void)
72 return String(L"ISO-8859-16");
76 _Iso885916EncodingCore::GetByteCount(const wchar_t* pSrc, int srcLength, int& retLength)
79 wchar_t* pUnicodeStr = (wchar_t*) pSrc;
82 int iso8859_16Size = srcLength;
85 if ((*pUnicodeStr) > 0x00A0)
87 if (((*pUnicodeStr) > 0xFFFF))
89 return E_INVALID_ENCODING_RANGE;
92 if (!(*pUnicodeStr & 0xFF00))
94 if (ISO_8859_16_UNICODE_TABLE[*pUnicodeStr - 0xA0] != *pUnicodeStr)
96 return E_INVALID_ENCODING_RANGE;
99 else if (!(*pUnicodeStr & 0xFE80))
102 for (int i = 0; i <= 0x5F; i++)
104 if (ISO_8859_16_UNICODE_TABLE[i] == *pUnicodeStr)
110 // check for invalid values
113 return E_INVALID_ENCODING_RANGE;
116 else if (!(*pUnicodeStr & 0xFDE0))
118 if ((*pUnicodeStr == 0x0218) || (*pUnicodeStr == 0x0219) || (*pUnicodeStr == 0x021A) ||
119 (*pUnicodeStr == 0x021B))
125 return E_INVALID_ENCODING_RANGE;
128 else if (!(*pUnicodeStr & 0xDF40))
130 if ((*pUnicodeStr == 0x201D) || (*pUnicodeStr == 0x201E) || (*pUnicodeStr == 0x20AC))
136 return E_INVALID_ENCODING_RANGE;
141 return E_INVALID_ENCODING_RANGE;
147 retLength = iso8859_16Size;
151 return E_INVALID_ARG;
155 _Iso885916EncodingCore::GetCharCount(const byte* pSrc, int srcLength, int& retLength)
158 SysTryReturnResult(NID_TEXT, pSrc, E_INVALID_ARG,
159 "[%s] Invalid argument is used. Input buffer pSrc is null.", GetErrorMessage(E_INVALID_ARG));
160 retLength = srcLength;
165 _Iso885916EncodingCore::EncodeN(const wchar_t* pSrc, int srcLength, int& retLength)
167 wchar_t* pUnicodeStr = (wchar_t*) pSrc;
168 result r = GetByteCount(pUnicodeStr, srcLength, retLength);
169 SysTryReturn(NID_TEXT, retLength > 0, null, r, "[%s] Encoding Failed", GetErrorMessage(r));
171 std::unique_ptr<byte[]> pIso8859_16Byte(new (std::nothrow) byte[retLength + 1]);
172 SysTryReturn(NID_TEXT, pIso8859_16Byte, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
174 byte* pCurrPos = pIso8859_16Byte.get();
175 while (srcLength > 0)
177 if ((*pUnicodeStr) <= 0x00FF)
179 *pCurrPos = (byte) * pUnicodeStr;
181 else if (!(*pUnicodeStr & 0xFE80))
183 for (int i = 0; i <= 0x5F; i++)
185 if (ISO_8859_16_UNICODE_TABLE[i] == *pUnicodeStr)
187 *pCurrPos = (byte) (i + 0xA0);
192 else if (!(*pUnicodeStr & 0xFDE0))
194 if (*pUnicodeStr == 0x0218)
198 else if (*pUnicodeStr == 0x0219)
202 else if (*pUnicodeStr == 0x021A)
206 else if (*pUnicodeStr == 0x021B)
215 else if (!(*pUnicodeStr & 0xDF40))
217 if (*pUnicodeStr == 0x201D)
221 else if (*pUnicodeStr == 0x201E)
225 else if (*pUnicodeStr == 0x20AC)
244 SetLastResult(E_SUCCESS);
245 return pIso8859_16Byte.release();
249 _Iso885916EncodingCore::DecodeN(const byte* pSrc, int srcLength, int& retLength)
251 byte* pIso8859_16Byte = (byte*) pSrc;
252 result r = GetCharCount(pIso8859_16Byte, srcLength, retLength);
253 SysTryReturn(NID_TEXT, retLength > 0, null, r, "[%s] Encoding Failed", GetErrorMessage(r));
255 std::unique_ptr<wchar_t[]> pUnicodeStr( new (std::nothrow) wchar_t[retLength + 1]);
256 SysTryReturn(NID_TEXT, pUnicodeStr, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
258 wchar_t* pCurrPos = pUnicodeStr.get();
259 while (srcLength > 0)
261 if ((*pIso8859_16Byte) <= 0xA0)
263 *pCurrPos = (unsigned short) *pIso8859_16Byte;
265 // added following check to resolved c+=test issue, this check will always be true if the above condition is false
266 else if ((*pIso8859_16Byte) <= 0xFF)
268 *pCurrPos = ISO_8859_16_UNICODE_TABLE[*pIso8859_16Byte - 0xA0];
275 pUnicodeStr[retLength] = '\0';
276 SetLastResult(E_SUCCESS);
277 return pUnicodeStr.release();
281 _Iso885916EncodingCore::GetMaxByteCount(int charCount) const
287 _Iso885916EncodingCore::GetMaxCharCount(int byteCount) const