sync with tizen_2.0
[platform/framework/native/appfw.git] / src / base / utility / FBaseUtilUrlDecoder.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                FBaseUtilUrlDecoder.cpp
20  * @brief               This is the implementation file for UrlDecoder class.
21  */
22
23 // Includes
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <wchar.h>
27 #include <string.h>
28 #include <new>
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"
39
40
41 namespace Tizen { namespace Base { namespace Utility
42 {
43
44 UrlDecoder::UrlDecoder(void)
45 {
46
47 }
48
49
50 UrlDecoder::~UrlDecoder(void)
51 {
52
53 }
54
55
56 result
57 UrlDecoder::Decode(const Tizen::Base::String& str, const Tizen::Base::String& encodingScheme, Tizen::Base::String& decodedStr)
58 {
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.");
61
62         _ICUConverter converter;
63         bool converted = converter.OpenConverter(encodingScheme);
64         result r = GetLastResult();
65         SysTryReturnResult(NID_BASE_UTIL, converted, r, "Propagating.");
66
67         const wchar_t* pMchar = null;
68         int index = 0;
69         bool found = false;
70         int numChars = 0;
71         int charCount = 0;
72         wchar_t* pIn = null;
73
74         numChars = str.GetLength();
75         pMchar = str.GetPointer();
76         pIn = const_cast<wchar_t*>(&pMchar[0]);
77
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.");
80
81         memset(pUrlDecodedBuf.get(), 0x00, numChars + 1);
82
83         charCount = numChars;
84         decodedStr.Clear();
85
86         //Decode url-encoded string
87         while (charCount > 0 && (*pIn != '\0'))
88         {
89                 if (*pIn == '%')
90                 {
91                         char hexChar[3];
92                         int decimal = 0;
93
94                         hexChar[0] = *(pIn + 1);
95                         hexChar[1] = *(pIn + 2);
96                         hexChar[2] = '\0';
97                         pIn += 3;
98
99                         //Converting %ab type substring to corresponing decimal value
100                         sscanf(hexChar, "%X", &decimal);
101                         pUrlDecodedBuf[index] = decimal;
102                         index++;
103                         charCount -= 3;
104                         found = true;
105                 }
106                 else
107                 {
108                         if (found)
109                         {
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);
114                                 index = 0;
115                                 found = false;
116                         }
117
118                         (*pIn == '+') ? decodedStr.Append(' '):decodedStr.Append(*pIn);
119                         pIn++;
120                         charCount--;
121                 }
122         }
123
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)
125         if (found)
126         {
127                 std::unique_ptr< wchar_t[] > pDst(converter.ConvertToUcharN(reinterpret_cast< const char* >(pUrlDecodedBuf.get()), index));
128                 r = GetLastResult();
129                 SysTryReturnResult(NID_BASE_UTIL, pDst != null, r, "Propagating.");
130                 decodedStr.Append(pDst.get());
131         }
132
133         converter.CloseConverter();
134         return r;
135 }
136 } } } // Tizen::Base::Utility