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_DecoderImpl.cpp
19 * @brief This is the implementation file for _DecoderImpl class.
22 #include <unique_ptr.h>
23 #include "FText_EncodingCore.h"
24 #include <FBaseSysLog.h>
25 #include "FText_DecoderImpl.h"
27 using namespace Tizen::Base;
28 using namespace Tizen::Base::Collection;
29 using namespace Tizen::Base::Utility;
31 namespace Tizen { namespace Text
34 _DecoderImpl::_DecoderImpl(void)
40 _DecoderImpl::~_DecoderImpl(void)
45 _DecoderImpl::GetDecoderN(const String& encodingType)
47 SysTryReturn(NID_TEXT, (encodingType == L"UTF-8"), null, E_INVALID_ARG,
48 "[%s] Invalid argument is used.encdoingType(%ls) is not UTF-8.Failed to open decoder.", GetErrorMessage(E_INVALID_ARG), encodingType.GetPointer());
50 std::unique_ptr<_DecoderImpl> pDecoderImpl(new (std::nothrow) _DecoderImpl);
51 SysTryReturn(NID_TEXT, pDecoderImpl, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
53 pDecoderImpl->__pDecoder.reset(_EncodingCore::GetEncodingCoreN(encodingType, L"WCHAR_T"));
55 result r = GetLastResult();
56 SysTryReturn(NID_TEXT, (pDecoderImpl->__pDecoder != null), null, r,
57 "[%s] Failed to open decoder.", GetErrorMessage(r));
59 pDecoderImpl->__encodingType = encodingType;
60 pDecoderImpl->_pDecoderImpl = pDecoderImpl.get();
62 SetLastResult(E_SUCCESS);
63 return pDecoderImpl.release();
67 _DecoderImpl::Decode(const byte* pSrc, int srcLength, WcharBuffer*& pWcharBuffer, int index, bool flush) const
69 SysAssertf(__pDecoder != null, "Not yet constructed! Construct() should be called before use.");
71 SysTryReturnResult(NID_TEXT, (pSrc != null), E_INVALID_ARG, "[%s] Invalid argument is used. pSrc can not be null.", GetErrorMessage(E_INVALID_ARG));
72 SysTryReturnResult(NID_TEXT, (srcLength >= 0), E_INVALID_ARG,
73 "[%s] Invalid argument is used. srcLength(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), srcLength);
74 SysTryReturnResult(NID_TEXT, (index >= 0), E_INVALID_ARG,
75 "[%s] Invalid argument is used. index(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), index);
80 bool isMemAllocReq = false;
82 std::unique_ptr<wchar_t[]> pDst(__pDecoder->DecodeExN(pSrc, srcLength, retLength, flush));
84 SysTryReturnResult(NID_TEXT, (pDst != null), r, "[%s] Decoding failed", GetErrorMessage(r));
86 WcharBuffer* pOutBuffer = pWcharBuffer;
89 if (pOutBuffer == null)
92 pOutBuffer = new (std::nothrow) WcharBuffer;
93 SysTryCatch(NID_TEXT, (pOutBuffer != null), ,
94 E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Byte Buffer allocation failed");
96 r = pOutBuffer->Construct(retLength);
97 SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to Construct Byte buffer", GetErrorMessage(r));
99 pWcharBuffer = pOutBuffer;
104 currPosition = pOutBuffer->GetPosition();
106 r = pOutBuffer->SetPosition(index);
107 SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to set Byte buffer position", GetErrorMessage(r));
110 r = pOutBuffer->SetArray(pDst.get(), 0, retLength);
111 SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to fill Byte buffer", GetErrorMessage(r));
113 r = pOutBuffer->SetPosition(currPosition);
114 SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to set Byte buffer position", GetErrorMessage(r));
129 _DecoderImpl::GetCharCount(const ByteBuffer& bytes, int byteIndex, int byteCount, int& charCount, bool flush) const
131 SysAssertf(__pDecoder != null, "Not yet constructed! Construct() should be called before use.");
134 int inBufSize = bytes.GetLimit();
135 result r = CheckBufferInput(inBufSize, byteIndex, byteCount);
136 SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
139 r = bytes.GetByte(byteIndex + byteCount - 1, lastByte);
142 if (lastByte == '\0')
148 r = __pDecoder->GetCharCount((bytes.GetPointer() + byteIndex), byteCount, charCount);
159 _DecoderImpl::GetCharsN(const ByteBuffer& bytes, bool flush) const
161 SysAssertf(__pDecoder != null, "Not yet constructed! Construct() should be called before use.");
163 WcharBuffer* pBuffer = null;
165 result r = GetBufferSize(bytes, srcLength);
166 SysTryReturn(NID_TEXT, (!IsFailed(r)), null, r, "[%s] Input validation failed", GetErrorMessage(r));
168 r = Decode(bytes.GetPointer(), srcLength, pBuffer, 0, flush);
175 _DecoderImpl::GetCharsN(const ByteBuffer& bytes, int byteIndex, int byteCount, bool flush) const
177 SysAssertf(__pDecoder != null, "Not yet constructed! Construct() should be called before use.");
179 WcharBuffer* pBuffer = null;
180 int srcLength = bytes.GetLimit();
181 result r = CheckBufferInput(srcLength, byteIndex, byteCount);
182 SysTryReturn(NID_TEXT, (!IsFailed(r)), null, r, "[%s] Input validation failed", GetErrorMessage(r));
184 r = Decode(bytes.GetPointer() + byteIndex, byteCount, pBuffer, 0, flush);
191 _DecoderImpl::GetBufferSize(const ByteBuffer& bytes, int& byteBufSize) const
193 // It is assumed that user has set limit properly
194 byteBufSize = bytes.GetLimit();
196 // Adjusting byteBufSize if bytes has null char at the end
198 result r = bytes.GetByte(byteBufSize - 1, lastByte);
199 SysTryReturnResult(NID_TEXT, (!IsFailed(r)), E_INVALID_ARG, "[%s] Last byte check failed", GetErrorMessage(r));
201 if ((lastByte == '\0')and(byteBufSize > 1))
205 SysTryReturnResult(NID_TEXT, (byteBufSize > 0), E_INVALID_ARG,
206 "[%s] Invalid argument is used. byteBufSize [%d] is invalid.", GetErrorMessage(E_INVALID_ARG), byteBufSize);
212 _DecoderImpl::CheckBufferInput(int inBufSize, int inIndex, int inCount) const
214 SysTryReturnResult(NID_TEXT, (inIndex >= 0), E_INVALID_ARG,
215 "[%s] Invalid argument is used. inIndex(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inIndex);
216 SysTryReturnResult(NID_TEXT, (inCount >= 0), E_INVALID_ARG,
217 "[%s] Invalid argument is used. inCount(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inCount);
218 SysTryReturnResult(NID_TEXT, (inBufSize > 0), E_INVALID_ARG,
219 "[%s] Invalid argument is used. inBufSize(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inBufSize);
221 SysTryReturnResult(NID_TEXT, (inBufSize > inIndex), E_OUT_OF_RANGE,
222 "[%s] inIndex(%d) is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE), inIndex);
223 SysTryReturnResult(NID_TEXT, (inBufSize >= inCount), E_OUT_OF_RANGE,
224 "[%s] inCount(%d) is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE), inCount);
225 SysTryReturnResult(NID_TEXT, (inBufSize >= (inIndex + inCount)), E_UNDERFLOW,
226 "[%s] sum of the length of the inIndex(%d) and inCount(%d) is greater than inBufSize(%d).",
227 GetErrorMessage(E_UNDERFLOW), inIndex, inCount, inBufSize);