/****************************************************************
* *
* Written bu Stanic Igor 2012/2013 *
* Implementation of helper fuctions *
* *
* *
***************************************************************/
#include "Helpers.h"
typedef unsigned int UINT;
std::string Helpers::URIEnc(const std::string& source )
{
static const std::map<char,std::string> reservedURIMappings =
{PAIR('!', "%21"), PAIR('#', "%23"), PAIR('$', "%24"), PAIR('&', "%26"), /*PAIR('\'', "%27"),*/ PAIR('(', "%28"), PAIR(')', "%29"), PAIR('*', "%2A"), PAIR('+', "%2B"),
PAIR(',', "%2C"), /*PAIR('/', "%2F"),*/ PAIR(':', "%3A"), PAIR(';', "%3B"), PAIR('=', "%3D"), PAIR('?', "%3F"), PAIR('@', "%40"), PAIR('[', "%5B"), PAIR(']', "%5D")};
static const std::map<char, std::string> commonCharURIMappings =
{PAIR(' ', "%20"), PAIR('\"', "%22"),PAIR('%', "%25"),/*PAIR('-', "%2D"),PAIR('.', "%2E"),*/PAIR('<', "%3C"),PAIR('>', "%3E"),
PAIR('\\', "%5C"),PAIR('^', "%5E"),/*PAIR('_', "%5F"),*/
PAIR('`', "%60"),PAIR('{', "%7B"),PAIR('|', "%7C"),PAIR('}', "%7D"),PAIR('~', "%7E")};
std::string result;
int len = source.length();
std::stringstream ss;
for(int i = 0; i < len; ++i)
{
//char c = source[i];
int j = (int)source[i];
//char seems to be negative number for unstandard chars
if(j >= 0 && j <= 127)
{
char c = (char)j;
std::map<char,std::string>::const_iterator reservedMapFind = reservedURIMappings.find(c);
if(reservedMapFind != reservedURIMappings.end())
{
ss << (*reservedMapFind).second;
continue;
}
std::map<char,std::string>::const_iterator commonMapFind = commonCharURIMappings.find(c);
if(commonMapFind != commonCharURIMappings.end())
{
ss << (*commonMapFind).second;
continue;
}
ss << c;
}
else
{
int ii = j;
if (ii < 0)
ii = 256 + ii;
ss << "%";
ss << hex << ii;
}
}
result = ss.str();
return result;
}
std::string Helpers::URIDec(const std::string source)
{
static const std::map<std::string, char> reservedURIMappings =
{PAIR("%21",'!'), PAIR("%23",'#'), PAIR("%24",'$'), PAIR("%26",'&'), PAIR("%27",'\''), PAIR("%28",'('), PAIR("%29",')'), PAIR("%2A",'*'), PAIR("%2B",'+'),
PAIR("%2C",','), PAIR("%2F",'/'), PAIR("%3A",':'), PAIR("%3B",';'), PAIR("%3D",'='), PAIR("%3F",'?'), PAIR("%40",'@'), PAIR("%5B",'['), PAIR("%5D",']')};
static const std::map<std::string, char> commonCharURIMappings =
{PAIR("%20",' '), PAIR("%22",'\"'),PAIR("%25",'%'),/*PAIR("%2D",'-'),PAIR("%2E",'.'),*/PAIR("%3C",'<'),PAIR("%3E",'>'),
PAIR("%5C",'\\'),PAIR("%5E",'^'),/*PAIR("%5F",'_'),*/
PAIR("%60",'`'),PAIR("%7B",'{'),PAIR("%7C",'|'),PAIR("%7D",'}'),PAIR("%7E",'~')};
std::stringstream ss;
for(std::string::const_iterator it = source.cbegin(), end = source.cend(); it != end; ++it)
{
if(*it == '%')
{
std::string temp;
std::string::const_iterator current = it;
std::string::const_iterator limit = it + 2;
do
{
temp += *current++;
}while(current <= limit && limit < source.cend());
auto iter_map = commonCharURIMappings.find(temp);
if(iter_map != commonCharURIMappings.end())
{
ss << iter_map->second;
it = limit;
continue;
}
auto iter_map2 = reservedURIMappings.find(temp);
if(iter_map2 != reservedURIMappings.end())
{
ss << iter_map2->second;
it = limit;
}
}
else
ss << *it;
}
return ss.str();
}