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 FBaseUtilUrlEncoder.cpp
20 * @brief This is the implementation file for UrlEncoder class.
29 #include <unique_ptr.h>
30 #include <unicode/ustring.h>
31 #include <unicode/utypes.h>
32 #include <unicode/ucnv.h>
33 #include <FBaseUtilUrlEncoder.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
45 ///////////////////////////////////////////////////////////////////////////////
46 /// UrlEncoder class lifecycle
48 UrlEncoder::UrlEncoder(void)
53 UrlEncoder::~UrlEncoder(void)
58 ///////////////////////////////////////////////////////////////////////////////
59 /// UrlEncoder class Operations
62 UrlEncoder::Encode(const Tizen::Base::String& str, const Tizen::Base::String& encodingScheme, Tizen::Base::String& encodedStr)
64 SysTryReturnResult(NID_BASE_UTIL, str.GetLength() > 0, E_INVALID_ARG, "Invalid argument is used. String to be encoded is empty.");
65 SysTryReturnResult(NID_BASE_UTIL, encodingScheme.GetLength() > 0, E_INVALID_ARG, "Invalid argument is used. Invalid encoding scheme.");
67 _ICUConverter converter;
68 bool converted = converter.OpenConverter(encodingScheme);
69 result r = GetLastResult();
70 SysTryReturnResult(NID_BASE_UTIL, converted, r, "Propagating.");
73 std::unique_ptr< char[] > pOutBuf(null);
74 const wchar_t* pTmpBytePtr = str.GetPointer();
76 int strLen = str.GetLength();
78 for (int i = 0; i < strLen; i++)
80 // Do not convert unreserved characters (Unreserved Characters for URI in RFC 3986)
81 if ((pTmpBytePtr[i] >= 'a' && pTmpBytePtr[i] <= 'z') || (pTmpBytePtr[i] >= 'A' && pTmpBytePtr[i] <= 'Z') || (pTmpBytePtr[i] >= '0' && pTmpBytePtr[i] <= '9') || (pTmpBytePtr[i] == '_') || (pTmpBytePtr[i] == '~') || (pTmpBytePtr[i] == '-') || (pTmpBytePtr[i] == '.') || (pTmpBytePtr[i] == ' '))
86 pOutBuf.reset(converter.ConvertFromUcharN(pTmpBytePtr + startIndex, (i - startIndex), retLength));
88 SysTryReturnResult(NID_BASE_UTIL, pOutBuf != null, r, "Propagating.");
90 for (int j = 0; j < retLength; j++)
94 memset(&hexChar, 0x00, 4);
95 sprintf(hexChar, "%%%02X", (unsigned char) pOutBuf[j]);
97 encodedStr.Append(hexChar); // Append %ab buffer to encoded string
102 (pTmpBytePtr[i] == ' ') ? encodedStr.Append('+'):encodedStr.Append(pTmpBytePtr[i]);
104 else if (startIndex < 0)
113 pOutBuf.reset(converter.ConvertFromUcharN(pTmpBytePtr + startIndex, (str.GetLength() - startIndex), retLength));
115 SysTryReturnResult(NID_BASE_UTIL, pOutBuf != null, r, GetErrorMessage(r));
117 for (int j = 0; j < retLength; j++)
121 memset(&hexChar, 0x00, 4);
122 sprintf(hexChar, "%%%02X", (unsigned char) pOutBuf[j]);
124 encodedStr.Append(hexChar); // Append %ab buffer to encoded string
129 converter.CloseConverter();
132 } } } // Tizen::Base::Utility