sync with tizen_2.0
[platform/framework/native/appfw.git] / src / text / FText_EncodingImpl.cpp
1 //
2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 //
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
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 /**
19  * @file                FText_EncodingImpl.cpp
20  * @brief               This is the implementation file for _EncodingImpl class.
21  */
22
23 #include <FBaseResult.h>
24 #include "FText_EncodingCore.h"
25 #include <FBaseSysLog.h>
26 #include "FText_EncodingImpl.h"
27
28 using namespace Tizen::Base;
29 using namespace Tizen::Base::Collection;
30 using namespace Tizen::Base::Utility;
31
32 #define AVAILABLE_ENCODING_COUNT 45
33 static String availableEncodings[] =
34 {
35 L"ASCII",
36 L"GSM",
37 L"KSC5601",
38 L"Big5",
39 L"GB2312",
40 L"UTF-8",
41 L"UTF-16",
42 L"UTF-16BE",
43 L"UTF-16LE",
44 L"UTF-32",
45 L"UTF-32BE",
46 L"UTF-32LE",
47 L"UCS-2",
48 L"UCS-2BE",
49 L"UCS-2LE",
50 L"UCS-4",
51 L"UCS-4BE",
52 L"UCS-4LE",
53 L"ISO-8859-1",
54 L"ISO-8859-2",
55 L"ISO-8859-3",
56 L"ISO-8859-4",
57 L"ISO-8859-5",
58 L"ISO-8859-6",
59 L"ISO-8859-7",
60 L"ISO-8859-8",
61 L"ISO-8859-9",
62 L"ISO-8859-10",
63 L"ISO-8859-11",
64 L"ISO-8859-13",
65 L"ISO-8859-14",
66 L"ISO-8859-15",
67 L"ISO-8859-16",
68 L"Windows-874",
69 L"Windows-1250",
70 L"Windows-1251",
71 L"Windows-1252",
72 L"Windows-1253",
73 L"Windows-1254",
74 L"Windows-1255",
75 L"Windows-1256",
76 L"Windows-1257",
77 L"Windows-1258",
78 L"Shift-JIS",
79 L"ISO-2022-JP",
80 };
81
82 namespace Tizen { namespace Text
83 {
84
85 _EncodingImpl::_EncodingImpl(void)
86         : Encoding()
87         , __encodingType()
88         , __pEncodingCore(null)
89 {
90 }
91
92 _EncodingImpl::~_EncodingImpl(void)
93 {
94
95 }
96
97 result
98 _EncodingImpl::Construct(const String& encodingType)
99 {
100         // Object is not allowed to construct twice
101         SysAssertf(__pEncodingCore == null,
102                 "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
103
104         SysTryReturnResult(NID_TEXT, IsEncodingSupported(encodingType),
105                           E_UNSUPPORTED_TYPE, "[%s] encodingType[%ls] is not supported", GetErrorMessage(E_UNSUPPORTED_TYPE), encodingType.GetPointer());
106
107         __pEncodingCore.reset(_EncodingCore::GetEncodingCoreN(encodingType, L"WCHAR_T"));
108
109         result r = GetLastResult();
110         SysTryReturnResult(NID_TEXT, (__pEncodingCore != null), r, "[%s] Failed to open encoder/decoder.", GetErrorMessage(r));
111
112         __encodingType = encodingType;
113         _pEncodingImpl = this;
114
115         return E_SUCCESS;
116 }
117
118 result
119 _EncodingImpl::Encode(const wchar_t* pSrc, int srcLength, ByteBuffer*& pByteBuffer, int index) const
120 {
121         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
122
123         SysTryReturnResult(NID_TEXT, (pSrc != null), E_INVALID_ARG, "[%s] Invalid argument is used. pSrc can not be null.", GetErrorMessage(E_INVALID_ARG));
124         SysTryReturnResult(NID_TEXT, (srcLength >= 0), E_INVALID_ARG,
125                                 "[%s] Invalid argument is used. srcLength(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), srcLength);
126         SysTryReturnResult(NID_TEXT, (index >= 0), E_INVALID_ARG,
127                                 "[%s] Invalid argument is used. index(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), index);
128
129         result r = E_SUCCESS;
130         int currPosition = 0;
131         int retLength = 0;
132         bool isMemAllocReq = false;
133
134         std::unique_ptr<byte[]> pDst(__pEncodingCore->EncodeN(pSrc, srcLength, retLength));
135         r = GetLastResult();
136         SysTryReturnResult(NID_TEXT, (pDst != null), r, "[%s] Decoding failed", GetErrorMessage(r));
137
138         ByteBuffer* pOutBuffer = pByteBuffer;
139         if (retLength > 0)
140         {
141                 if (pOutBuffer == null)
142                 {
143                         isMemAllocReq = true;
144                         pOutBuffer = new (std::nothrow) ByteBuffer;
145                         SysTryCatch(NID_TEXT, (pOutBuffer != null), , E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
146
147                         r = pOutBuffer->Construct(retLength + 1);
148                         SysTryCatch(NID_TEXT, !IsFailed(r), , r, "[%s] Unable to Construct Byte buffer", GetErrorMessage(r));
149
150                         pByteBuffer = pOutBuffer;
151                 }
152                 else
153                 {
154                         currPosition = pOutBuffer->GetPosition();
155
156                         r = pOutBuffer->SetPosition(index);
157                         SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to set Byte buffer position", GetErrorMessage(r));
158                 }
159                 r = pOutBuffer->SetArray(pDst.get(), 0, retLength);
160                 SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to fill Byte buffer", GetErrorMessage(r));
161
162                 r = pOutBuffer->SetPosition(currPosition);
163                 SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to set Byte buffer position", GetErrorMessage(r));
164
165                 return r;
166         }
167
168 CATCH:
169         if (isMemAllocReq)
170         {
171                 delete pByteBuffer;
172                 pByteBuffer = null;
173         }
174         return r;
175 }
176
177 result
178 _EncodingImpl::Decode(const byte* pSrc, int srcLength, WcharBuffer*& pWcharBuffer, int index) const
179 {
180         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
181
182         SysTryReturnResult(NID_TEXT, (pSrc != null), E_INVALID_ARG, "[%s] Invalid argument is used. pSrc can not be null.", GetErrorMessage(E_INVALID_ARG));
183         SysTryReturnResult(NID_TEXT, (srcLength >= 0), E_INVALID_ARG,
184                                 "[%s] Invalid argument is used. srcLength(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), srcLength);
185         SysTryReturnResult(NID_TEXT, (index >= 0), E_INVALID_ARG,
186                                 "[%s] Invalid argument is used. index(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), index);
187
188         result r = E_SUCCESS;
189         int currPosition = 0;
190         int retLength = 0;
191         bool isMemAllocReq = false;
192
193         std::unique_ptr<wchar_t[]> pDst(__pEncodingCore->DecodeN(pSrc, srcLength, retLength));
194         r = GetLastResult();
195         SysTryReturnResult(NID_TEXT, (pDst != null), r, "[%s] Decoding failed", GetErrorMessage(r));
196
197         WcharBuffer* pOutBuffer = pWcharBuffer;
198         if (retLength > 0)
199         {
200                 if (pOutBuffer == null)
201                 {
202                         isMemAllocReq = true;
203                         pOutBuffer = new (std::nothrow) WcharBuffer;
204                         SysTryCatch(NID_TEXT, (pOutBuffer != null), , E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
205
206                         r = pOutBuffer->Construct(retLength + 1);
207                         SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to Construct Byte buffer", GetErrorMessage(r));
208
209                         r = pOutBuffer->SetArray(pDst.get(), 0, retLength);
210                         SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to fill Byte buffer", GetErrorMessage(r));
211
212                         r = pOutBuffer->Set(L'\0');
213                         SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to set null Byte", GetErrorMessage(r));
214
215                         pOutBuffer->Rewind();
216                         pWcharBuffer = pOutBuffer;
217                 }
218                 else
219                 {
220                         currPosition = pOutBuffer->GetPosition();
221
222                         r = pOutBuffer->SetPosition(index);
223                         SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to set Byte buffer position", GetErrorMessage(r));
224
225                         r = pOutBuffer->SetArray(pDst.get(), 0, retLength);
226                         SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to fill Byte buffer", GetErrorMessage(r));
227
228                         r = pOutBuffer->SetPosition(currPosition);
229                         SysTryCatch(NID_TEXT, (!IsFailed(r)), , r, "[%s] Unable to set Byte buffer position", GetErrorMessage(r));
230                 }
231                 return r;
232         }
233
234 CATCH:
235         if (isMemAllocReq)
236         {
237                 delete pWcharBuffer;
238                 pWcharBuffer = null;
239         }
240         return r;
241 }
242
243 result
244 _EncodingImpl::Decode(const byte* pSrc, int srcLength, String& outStr, int index) const
245 {
246         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
247
248         SysTryReturnResult(NID_TEXT, (pSrc != null), E_INVALID_ARG, "[%s] Invalid argument is used. pSrc can not be null.", GetErrorMessage(E_INVALID_ARG));
249         SysTryReturnResult(NID_TEXT, (srcLength >= 0), E_INVALID_ARG,
250                                 "[%s] Invalid argument is used. srcLength(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), srcLength);
251         SysTryReturnResult(NID_TEXT, (index >= 0), E_INVALID_ARG,
252                                 "[%s] Invalid argument is used. index(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), index);
253
254         result r = E_SUCCESS;
255         int retLength = 0;
256
257         std::unique_ptr<wchar_t[]> pDst(__pEncodingCore->DecodeN(pSrc, srcLength, retLength));
258         r = GetLastResult();
259         SysTryReturnResult(NID_TEXT, (pDst != null), r, "[%s] Decoding failed", GetErrorMessage(r));
260
261         outStr = pDst.get();
262
263         return E_SUCCESS;
264 }
265
266 result
267 _EncodingImpl::GetByteCount(const String& str, int& byteCount) const
268 {
269         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
270
271         byteCount = -1;
272         int srcLength = str.GetLength();
273         SysTryReturnResult(NID_TEXT, (srcLength > 0), E_INVALID_ARG,
274                         "[%s] Invalid argument is used. Input string is empty.", GetErrorMessage(E_INVALID_ARG));
275
276         return __pEncodingCore->GetByteCount(str.GetPointer(), srcLength, byteCount);
277 }
278
279 result
280 _EncodingImpl::GetByteCount(const WcharBuffer& chars, int& byteCount) const
281 {
282         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
283
284         byteCount = -1;
285         int srcLength = 0;
286         result r = GetBufferSize(chars, srcLength);
287         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
288
289         return __pEncodingCore->GetByteCount(chars.GetPointer(), srcLength, byteCount);
290 }
291
292 result
293 _EncodingImpl::GetByteCount(const WcharBuffer& chars, int charIndex, int charCount, int& byteCount) const
294 {
295         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
296
297         byteCount = -1;
298         int inBufSize = chars.GetLimit();
299         result r = CheckBufferInput(inBufSize, charIndex, charCount);
300         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
301
302         return __pEncodingCore->GetByteCount((chars.GetPointer() + charIndex), charCount, byteCount);
303 }
304
305 result
306 _EncodingImpl::GetCharCount(const ByteBuffer& bytes, int& charCount) const
307 {
308         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
309
310         charCount = -1;
311         int srcLength = 0;
312         result r = GetBufferSize(bytes, srcLength);
313         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
314
315         return __pEncodingCore->GetCharCount(bytes.GetPointer(), srcLength, charCount);
316 }
317
318 result
319 _EncodingImpl::GetCharCount(const ByteBuffer& bytes, int byteIndex, int byteCount, int& charCount) const
320 {
321         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
322
323         charCount = -1;
324         int inBufSize = bytes.GetLimit();
325         result r = CheckBufferInput(inBufSize, byteIndex, byteCount);
326         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
327
328         return __pEncodingCore->GetCharCount((bytes.GetPointer() + byteIndex), byteCount, charCount);
329 }
330
331 ByteBuffer*
332 _EncodingImpl::GetBytesN(const WcharBuffer& chars) const
333 {
334         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
335
336         ByteBuffer* pBuffer = null;
337         int srcLength = 0;
338         result r = GetBufferSize(chars, srcLength);
339         SysTryReturn(NID_TEXT, (!IsFailed(r)), null, r, "[%s] Input validation failed", GetErrorMessage(r));
340
341         r = Encode(chars.GetPointer(), srcLength, pBuffer, 0);
342
343         SetLastResult(r);
344         return pBuffer;
345 }
346
347 ByteBuffer*
348 _EncodingImpl::GetBytesN(const String& str) const
349 {
350         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
351
352         ByteBuffer* pBuffer = null;
353         int srcLength = str.GetLength();
354         SysTryReturn(NID_TEXT, (srcLength > 0), null, E_INVALID_ARG,
355                         "[%s] Invalid argument is used. Input string is empty.", GetErrorMessage(E_INVALID_ARG));
356
357         result r = Encode(str.GetPointer(), srcLength, pBuffer, 0);
358
359         SetLastResult(r);
360         return pBuffer;
361 }
362
363 result
364 _EncodingImpl::GetBytes(const WcharBuffer& chars, int charIndex, int charCount, ByteBuffer& bytes, int byteIndex) const
365 {
366         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
367
368         ByteBuffer* pBuffer = &bytes;
369         int srcLen = chars.GetLimit();
370         int destLen = bytes.GetLimit();
371
372         result r = CheckBufferInput(srcLen, charIndex, charCount, destLen, byteIndex);
373         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
374
375         return Encode((chars.GetPointer() + charIndex), charCount, pBuffer, byteIndex);
376 }
377
378 result
379 _EncodingImpl::GetBytes(const String& str, int charIndex, int charCount, ByteBuffer& bytes, int byteIndex) const
380 {
381         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
382
383         ByteBuffer* pBuffer = &bytes;
384         int srcLen = str.GetLength();
385         int destLen = bytes.GetLimit();
386
387         result r = CheckBufferInput(srcLen, charIndex, charCount, destLen, byteIndex);
388         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
389
390         return Encode((str.GetPointer() + charIndex), charCount, pBuffer, byteIndex);
391 }
392
393 WcharBuffer*
394 _EncodingImpl::GetCharsN(const ByteBuffer& bytes) const
395 {
396         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
397
398         WcharBuffer* pBuffer = null;
399         int srcLength = 0;
400         result r = GetBufferSize(bytes, srcLength);
401         SysTryReturn(NID_TEXT, (!IsFailed(r)), null, r, "[%s] Input validation failed", GetErrorMessage(r));
402
403         r = Decode(bytes.GetPointer(), srcLength, pBuffer, 0);
404
405         SetLastResult(r);
406         return pBuffer;
407 }
408
409 result
410 _EncodingImpl::GetChars(const ByteBuffer& bytes, int byteIndex, int byteCount, WcharBuffer& chars, int charIndex) const
411 {
412         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
413
414         WcharBuffer* pBuffer = &chars;
415         int srcLen = bytes.GetLimit();
416         int destLen = chars.GetLimit();
417
418         result r = CheckBufferInput(srcLen, byteIndex, byteCount, destLen, charIndex);
419         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
420
421         return Decode((bytes.GetPointer() + byteIndex), byteCount, pBuffer, charIndex);
422 }
423
424 result
425 _EncodingImpl::GetString(const ByteBuffer& bytes, Tizen::Base::String& str) const
426 {
427         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
428
429         int srcLength = 0;
430         result r = GetBufferSize(bytes, srcLength);
431         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
432
433         return Decode(bytes.GetPointer(), srcLength, str, 0);
434 }
435
436 result
437 _EncodingImpl::GetString(const ByteBuffer& bytes, int index, int count, String& str) const
438 {
439         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
440
441         int srcLen = bytes.GetLimit();
442         result r = CheckBufferInput(srcLen, index, count);
443         SysTryReturn(NID_TEXT, (!IsFailed(r)), r, r, "[%s] Input validation failed", GetErrorMessage(r));
444
445         if (count == 0)
446         {
447                 str = "";
448                 return E_SUCCESS;
449         }
450
451         return Decode((bytes.GetPointer() + index), count, str, 0);
452 }
453
454 ByteBuffer*
455 _EncodingImpl::ConvertImplN(const Encoding& src, const Encoding& dst, const ByteBuffer& srcBytes)
456 {
457         const _EncodingImpl* pSrcEncodingImpl = src._pEncodingImpl;
458         SysTryReturn(NID_TEXT, (pSrcEncodingImpl != null), null, E_INVALID_ARG, "[E_INVALID_ARG] Unable to get encoder object");
459
460         int inBufSize = -1;
461         result r = pSrcEncodingImpl->GetBufferSize(srcBytes, inBufSize);
462         SysTryReturn(NID_TEXT, !IsFailed(r), null, r, "[%s] Unable to get byte buffer size", GetErrorMessage(r));
463
464         return ConvertN(src, dst, srcBytes, 0, inBufSize);
465 }
466
467 ByteBuffer*
468 _EncodingImpl::ConvertImplN(const Encoding& src, const Encoding& dst, const ByteBuffer& srcBytes, int index, int count)
469 {
470         const _EncodingImpl* pSrcEncodingImpl = src._pEncodingImpl;
471         SysTryReturn(NID_TEXT, (pSrcEncodingImpl != null), null, E_INVALID_ARG, "[%s] Unable to get encoder object", GetErrorMessage(E_INVALID_ARG));
472
473         _EncodingCore* pSrcConverter = pSrcEncodingImpl->__pEncodingCore.get();
474         SysTryReturn(NID_TEXT, (pSrcConverter != null), null, E_INVALID_ARG, "[%s] Unable to get encoder decoder object", GetErrorMessage(E_INVALID_ARG));
475
476         const _EncodingImpl* pDstEncodingImpl = dst._pEncodingImpl;
477         SysTryReturn(NID_TEXT, (pDstEncodingImpl != null), null, E_INVALID_ARG, "[%s] Unable to get encoder object", GetErrorMessage(E_INVALID_ARG));
478
479         _EncodingCore* pDstConverter = pDstEncodingImpl->__pEncodingCore.get();
480         SysTryReturn(NID_TEXT, (pDstConverter != null), null, E_INVALID_ARG, "[%s] Unable to get encoder decoder object", GetErrorMessage(E_INVALID_ARG));
481
482         int inBufSize = srcBytes.GetLimit();
483
484         result r = pSrcEncodingImpl->CheckBufferInput(inBufSize, index, count);
485         SysTryReturn(NID_TEXT, !IsFailed(r), null, r, "[%s] Unable to get byte buffer size", GetErrorMessage(r));
486
487         int retLength = -1;
488
489         std::unique_ptr<byte[]> pByte(pSrcConverter->ConvertN(pDstConverter, srcBytes.GetPointer() + index, count, retLength));
490         r = GetLastResult();
491         SysTryReturn(NID_TEXT, (pByte != null), null, r, "[%s] Unable to get encoder object", GetErrorMessage(r));
492
493         std::unique_ptr<ByteBuffer> pOutBuffer(new (std::nothrow) ByteBuffer);
494         SysTryReturn(NID_TEXT, pOutBuffer != null, null,
495                            E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
496
497         r = pOutBuffer->Construct(retLength + 1);
498         SysTryReturn(NID_TEXT, !IsFailed(r), null, r, "[%s] Unable to Construct Byte buffer", GetErrorMessage(r));
499
500         r = pOutBuffer->SetArray(pByte.get(), 0, retLength);
501         SysTryReturn(NID_TEXT, (!IsFailed(r)), null, r, "[%s] Unable to fill Byte buffer", GetErrorMessage(r));
502
503         pOutBuffer->Rewind();
504
505         SetLastResult(E_SUCCESS);
506         return pOutBuffer.release();
507 }
508
509 int
510 _EncodingImpl::GetMaxByteCount(int charCount) const
511 {
512         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
513         return __pEncodingCore->GetMaxByteCount(charCount);
514 }
515
516 int
517 _EncodingImpl::GetMaxCharCount(int byteCount) const
518 {
519         SysAssertf(__pEncodingCore != null, "Not yet constructed! Construct() should be called before use.");
520         return __pEncodingCore->GetMaxCharCount(byteCount);
521 }
522
523 Encoder*
524 _EncodingImpl::GetEncoderN(void) const
525 {
526         return null;
527 }
528
529 Decoder*
530 _EncodingImpl::GetDecoderN(void) const
531 {
532         return null;
533 }
534
535 IList*
536 _EncodingImpl::GetAvailableEncodingsImplN(void)
537 {
538         ClearLastResult();
539         result r = E_SUCCESS;
540
541         std::unique_ptr<LinkedList, AllElementsDeleter> pList(new (std::nothrow) LinkedList());
542
543         SysTryReturn(NID_TEXT, pList != null, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
544
545         for (int i = 0; i < AVAILABLE_ENCODING_COUNT; i++)
546         {
547                 String* pEncodingStr = new (std::nothrow) String(availableEncodings[i]);
548                 SysTryReturn(NID_TEXT, pEncodingStr, null, E_OUT_OF_MEMORY, "[%s] Memory allocation failed", GetErrorMessage(E_OUT_OF_MEMORY));
549
550                 r = pList->Add(*pEncodingStr);
551                 if(IsFailed(r))
552                 {
553                         delete pEncodingStr;
554                         SysTryReturn(NID_TEXT, !IsFailed(r), null, r, "[%s] Unable to add encoding to list", GetErrorMessage(r));
555                 }
556         }
557
558         return pList.release();
559 }
560
561 result
562 _EncodingImpl::GetBufferSize(const WcharBuffer& chars, int& charBufSize) const
563 {
564         charBufSize = StringUtil::GetStringLengthInMb(chars);
565
566         // This check is to correct tmpSize if chars does not contain null char
567         if (charBufSize == -1)
568         {
569                 charBufSize = chars.GetLimit();
570         }
571
572         SysTryReturnResult(NID_TEXT, (charBufSize > 0), E_INVALID_ARG,
573                         "[%s] Invalid argument is used. charBufSize(%d) is less than 0.", GetErrorMessage(E_INVALID_ARG), charBufSize);
574         return E_SUCCESS;
575 }
576
577 result
578 _EncodingImpl::GetBufferSize(const ByteBuffer& bytes, int& byteBufSize) const
579 {
580         // It is assumed that user has set limit properly
581         byteBufSize = bytes.GetLimit();
582
583         int minByteCountForEncoding = 1;
584         if (__pEncodingCore)
585         {
586                 minByteCountForEncoding = 4 / __pEncodingCore->GetMaxCharCount(4);
587         }
588
589         byte lastByte = '\0';
590         bool nullAtTheEnd = false;
591         result r = bytes.GetByte(byteBufSize - 1, lastByte);
592         SysTryReturnResult(NID_TEXT, (!IsFailed(r)), E_INVALID_ARG, "[%s] Last byte check failed", GetErrorMessage(r));
593
594         if ((lastByte == '\0')and(byteBufSize > 1))
595         {
596                 nullAtTheEnd = true;
597                 if ((byteBufSize % minByteCountForEncoding) == 0)
598                 {
599                         for (int i = 0; i < minByteCountForEncoding; i++)
600                         {
601                                 byte lastByte = 0;
602                                 result r = bytes.GetByte(byteBufSize - i - 1, lastByte);
603                                 SysTryReturnResult(NID_TEXT, (!IsFailed(r)), E_INVALID_ARG, "[%s] Last byte check failed", GetErrorMessage(r));
604
605                                 if (lastByte != '\0')
606                                 {
607                                         nullAtTheEnd = false;
608                                 }
609                         }
610                 }
611
612                 if (nullAtTheEnd)
613                 {
614                         byteBufSize -= 1;
615                 }
616         }
617
618         SysTryReturnResult(NID_TEXT, (byteBufSize > 0), E_INVALID_ARG, "byteBufSize [%d] is invalid.", byteBufSize);
619         return E_SUCCESS;
620 }
621
622 result
623 _EncodingImpl::CheckBufferInput(int inBufSize, int inIndex, int inCount) const
624 {
625         SysTryReturnResult(NID_TEXT, (inIndex >= 0), E_INVALID_ARG,
626                                 "[%s] Invalid argument is used. inIndex(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inIndex);
627         SysTryReturnResult(NID_TEXT, (inCount >= 0), E_INVALID_ARG,
628                                 "[%s] Invalid argument is used. inCount(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inCount);
629         SysTryReturnResult(NID_TEXT, (inBufSize > 0), E_INVALID_ARG,
630                                 "[%s] Invalid argument is used. inBufSize(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inBufSize);
631
632         SysTryReturnResult(NID_TEXT, (inBufSize > inIndex), E_OUT_OF_RANGE,
633                                 "[%s] inIndex(%d) is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE), inIndex);
634         SysTryReturnResult(NID_TEXT, (inBufSize >= inCount), E_OUT_OF_RANGE,
635                                 "[%s] inCount(%d) is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE), inCount);
636         SysTryReturnResult(NID_TEXT, (inBufSize >= (inIndex + inCount)), E_UNDERFLOW,
637                         "[%s] sum of the length of the inIndex(%d) and inCount(%d) is greater than inBufSize(%d).",
638                         GetErrorMessage(E_UNDERFLOW), inIndex, inCount, inBufSize);
639
640         return E_SUCCESS;
641 }
642
643 result
644 _EncodingImpl::CheckBufferInput(int inBufSize, int inIndex, int inCount, int outBufSize, int outIndex) const
645 {
646         SysTryReturnResult(NID_TEXT, (inIndex >= 0), E_INVALID_ARG,
647                                 "[%s] Invalid argument is used. inIndex(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inIndex);
648         SysTryReturnResult(NID_TEXT, (inCount >= 0), E_INVALID_ARG,
649                                 "[%s] Invalid argument is used. inCount(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inCount);
650         SysTryReturnResult(NID_TEXT, (outIndex >= 0), E_INVALID_ARG,
651                                 "[%s] Invalid argument is used. outIndex(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), outIndex);
652         SysTryReturnResult(NID_TEXT, (inBufSize > 0), E_INVALID_ARG,
653                                 "[%s] Invalid argument is used. inBufSize(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), inBufSize);
654
655         SysTryReturnResult(NID_TEXT, (inBufSize > inIndex), E_OUT_OF_RANGE,
656                                 "[%s] inIndex(%d) is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE), inIndex);
657         SysTryReturnResult(NID_TEXT, (inBufSize >= inCount), E_OUT_OF_RANGE,
658                                 "[%s] inCount(%d) is outside the valid range.", GetErrorMessage(E_OUT_OF_RANGE), inCount);
659         SysTryReturnResult(NID_TEXT, (inBufSize >= (inIndex + inCount)), E_UNDERFLOW,
660                         "[%s] sum of the length of the inIndex(%d) and inCount(%d) is greater than inBufSize(%d).",
661                         GetErrorMessage(E_UNDERFLOW), inIndex, inCount, inBufSize);
662
663         SysTryReturnResult(NID_TEXT, (outBufSize > 0), E_INVALID_ARG,
664                                 "[%s] Invalid argument is used. outBufSize(%d) cannot be negative.", GetErrorMessage(E_INVALID_ARG), outBufSize);
665         SysTryReturnResult(NID_TEXT, (outBufSize > outIndex), E_OVERFLOW, "[%s] outIndex [%d] is outside the valid range.", GetErrorMessage(E_OVERFLOW), outIndex);
666
667         return E_SUCCESS;
668 }
669
670 String
671 _EncodingImpl::GetEncodingType(void) const
672 {
673         return __encodingType;
674 }
675
676 bool
677 _EncodingImpl::IsEncodingSupported(const Tizen::Base::String& encodingStr) const
678 {
679         SysTryReturn(NID_TEXT, !encodingStr.IsEmpty(), false, E_INVALID_ARG, "[%s] Invalid argument is used. Input string is empty", GetErrorMessage(E_INVALID_ARG));
680
681         bool retValue = false;
682         for (int i = 0; i < AVAILABLE_ENCODING_COUNT; i++)
683         {
684                 if (encodingStr.Equals(availableEncodings[i], false))
685                 {
686                         retValue = true;
687                         break;
688                 }
689         }
690         return retValue;
691 }
692
693 } } // Tizen::Text