1 #include "stdafx.h"
2 #include "UrlEncode.h"
3 
4 #include <stdlib.h>
5 #include <cmath>
6 #include <string>
7 
8 #include <algorithm>
9 
10 // HEX Values array
11 char hexVals[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
12 // UNSAFE String
13 std::string CURLEncode::csUnsafeString= "\"<>%\\^[]`+$,@:;/!#?=&";
14 
15 // PURPOSE OF THIS FUNCTION IS TO CONVERT A GIVEN CHAR TO URL HEX FORM
convert(char val)16 std::string CURLEncode::convert(char val)
17 {
18 	std::string csRet;
19 	csRet += "%";
20 	csRet += decToHex(val, 16);
21 	return  csRet;
22 }
23 
24 // THIS IS A HELPER FUNCTION.
25 // PURPOSE OF THIS FUNCTION IS TO GENERATE A HEX REPRESENTATION OF GIVEN CHARACTER
decToHex(char num,int radix)26 std::string CURLEncode::decToHex(char num, int radix)
27 {
28 	std::string csTmp;
29 	int num_char;
30 	num_char = (int) num;
31 
32 	// ISO-8859-1
33 	// IF THE IF LOOP IS COMMENTED, THE CODE WILL FAIL TO GENERATE A
34 	// PROPER URL ENCODE FOR THE CHARACTERS WHOSE RANGE IN 127-255(DECIMAL)
35 	if (num_char < 0)
36 		num_char = 256 + num_char;
37 
38 	while (num_char >= radix)
39 	{
40 		int temp = num_char % radix;
41 		num_char = (int)floor((float)num_char / radix);
42 		csTmp = hexVals[temp];
43 	}
44 	csTmp += hexVals[num_char%16];
45 
46 	if(csTmp.size() < 2)
47 	{
48 		csTmp += '0';
49 	}
50 
51 	std::string strdecToHex(csTmp);
52 	// Reverse the String
53 	std::reverse(strdecToHex.begin(),strdecToHex.end());
54 
55 	return strdecToHex;
56 }
57 
58 // PURPOSE OF THIS FUNCTION IS TO CHECK TO SEE IF A CHAR IS URL UNSAFE.
59 // TRUE = UNSAFE, FALSE = SAFE
isUnsafe(char compareChar)60 bool CURLEncode::isUnsafe(char compareChar)
61 {
62 	bool bcharfound = false;
63 	int m_strLen = 0;
64 
65 	m_strLen = csUnsafeString.size();
66 	for(int ichar_pos = 0; ichar_pos < m_strLen ;ichar_pos++)
67 	{
68 		char tmpsafeChar = csUnsafeString[ichar_pos];
69 		if(tmpsafeChar == compareChar)
70 		{
71 			bcharfound = true;
72 			break;
73 		}
74 	}
75 	int char_ascii_value = 0;
76 	//char_ascii_value = __toascii(compareChar);
77 	char_ascii_value = (int) compareChar;
78 
79 	if(bcharfound == false &&  char_ascii_value > 32 && char_ascii_value < 123)
80 	{
81 		return false;
82 	}
83 	// found no unsafe chars, return false
84 	else
85 	{
86 		return true;
87 	}
88 
89 	return true;
90 }
91 // PURPOSE OF THIS FUNCTION IS TO CONVERT A STRING
92 // TO URL ENCODE FORM.
URLEncode(const std::string & pcsEncode)93 std::string CURLEncode::URLEncode(const std::string &pcsEncode)
94 {
95 	int ichar_pos;
96 	std::string csEncode;
97 	std::string csEncoded;
98 	int m_length;
99 
100 	csEncode = pcsEncode;
101 	m_length = csEncode.size();
102 
103 	for(ichar_pos = 0; ichar_pos < m_length; ichar_pos++)
104 	{
105 		char ch = csEncode[ichar_pos];
106 		//if (ch < ' ')
107 		//{
108 		//	ch = ch;
109 		//}
110 		if(!isUnsafe(ch))
111 		{
112 			// Safe Character
113 			csEncoded += ch;
114 		}
115 		else
116 		{
117 			// get Hex Value of the Character
118 			csEncoded += convert(ch);
119 		}
120 	}
121 
122 	return csEncoded;
123 }
124 
URLDecode(const std::string & SRC)125 std::string CURLEncode::URLDecode(const std::string &SRC)
126 {
127 	std::string ret;
128 	char ch;
129 	int ii;
130 	size_t len=SRC.length();
131 	for (size_t i=0; i<len; i++) {
132 		if (int(SRC[i])==37) {
133 			if ( i+2 >= len )
134 				return SRC;
135 			int iret=sscanf(SRC.substr(i+1,2).c_str(), "%x", &ii);
136 			if (iret < 1)
137 				return "";
138 			ch=static_cast<char>(ii);
139 			ret+=ch;
140 			i=i+2;
141 		} else {
142 			ret+=SRC[i];
143 		}
144 	}
145 	return (ret);
146 }
147 
148