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 FBaseUtilUrlDecoder.cpp
20 * @brief This is the implementation file for UrlDecoder class.
29 #include <unique_ptr.h>
30 #include <unicode/ustring.h>
31 #include <unicode/utypes.h>
32 #include <unicode/ucnv.h>
33 #include <FBaseUtilUrlDecoder.h>
34 #include <FBaseResult.h>
35 #include <FBaseSysLog.h>
36 #include "FBase_StringConverter.h"
37 #include "FBase_NativeError.h"
38 #include "FBaseUtil_IcuConverter.h"
41 namespace Tizen { namespace Base { namespace Utility
44 UrlDecoder::UrlDecoder(void)
50 UrlDecoder::~UrlDecoder(void)
57 UrlDecoder::Decode(const Tizen::Base::String& str, const Tizen::Base::String& encodingScheme, Tizen::Base::String& decodedStr)
59 SysTryReturnResult(NID_BASE_UTIL, str.GetLength() > 0, E_INVALID_ARG, "String to be encoded is empty.");
60 SysTryReturnResult(NID_BASE_UTIL, encodingScheme.GetLength() > 0, E_INVALID_ARG, "Invalid encoding scheme.");
62 _ICUConverter converter;
63 bool converted = converter.OpenConverter(encodingScheme);
64 result r = GetLastResult();
65 SysTryReturnResult(NID_BASE_UTIL, converted, r, "Propagating.");
67 const wchar_t* pMchar = null;
74 numChars = str.GetLength();
75 pMchar = str.GetPointer();
76 pIn = const_cast<wchar_t*>(&pMchar[0]);
78 std::unique_ptr< char[] > pUrlDecodedBuf(new (std::nothrow) char[numChars + 1]);
79 SysTryReturnResult(NID_BASE_UTIL, pUrlDecodedBuf != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
81 memset(pUrlDecodedBuf.get(), 0x00, numChars + 1);
86 //Decode url-encoded string
87 while (charCount > 0 && (*pIn != '\0'))
94 hexChar[0] = *(pIn + 1);
95 hexChar[1] = *(pIn + 2);
99 //Converting %ab type substring to corresponing decimal value
100 sscanf(hexChar, "%X", &decimal);
101 pUrlDecodedBuf[index] = decimal;
110 std::unique_ptr< wchar_t[] > pDst(converter.ConvertToUcharN(reinterpret_cast< const char* >(pUrlDecodedBuf.get()), index));
111 SysTryReturnResult(NID_BASE_UTIL, pDst != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
112 decodedStr.Append(pDst.get());
113 memset(pUrlDecodedBuf.get(), 0x00, numChars + 1);
118 (*pIn == '+') ? decodedStr.Append(' '):decodedStr.Append(*pIn);
124 // if %XX is the last substring or all the substrings in input string are in form of %XX (e.g. %D7%91%D7%93%D7)
127 std::unique_ptr< wchar_t[] > pDst(converter.ConvertToUcharN(reinterpret_cast< const char* >(pUrlDecodedBuf.get()), index));
129 SysTryReturnResult(NID_BASE_UTIL, pDst != null, r, "Propagating.");
130 decodedStr.Append(pDst.get());
133 converter.CloseConverter();
136 } } } // Tizen::Base::Utility