//url.h
class URL
{
public:
static string escape(const string& unes);
static string unEscape(const string& esc);
static string unEscape(const char* esc);
static bool isAcceptable(unsigned char cx);
protected:
static const char hex[];
static const unsigned char acceptMask[];
static const unsigned int XALPHAS;
};
//url.cpp
#include "url.h "
//////////////////////////////////////////////////////////////////////
const unsigned int URL::XALPHAS = 0x1;
const char URL::hex[] = "0123456789ABCDEF ";
const unsigned char URL::acceptMask[] =
{
/* 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF */
0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0xF,0xE,0x0,0xF,0xF,0xC,
/* 2x ! "#$%& '()*+,-./ */
0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0x8,0x0,0x0,0x0,0x0,0x0,
/* 3x 0123456789:<=> ? */
0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,
/* 4x @ABCDEFGHIJKLMNO */
0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0x0,0x0,0x0,0x0,0xF,
/* 5X PQRSTUVWXYZ[\]^_ */
0x0,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,
/* 6x `abcdefghijklmno */
0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0xF,0x0,0x0,0x0,0x0,0x0
/* 7X pqrstuvwxyz{\}~DEL */
};
bool URL::isAcceptable(unsigned char cx)
{
if (cx == '/ ') return true;
return (cx > = 32) && (cx < 128) && ((acceptMask[cx - 32]) & XALPHAS);
}
string URL::escape(const string& str)
{
string result;
if (str.empty()) return result;
for (unsigned int i = 0; i < str.size(); i++)
{
unsigned char a = (unsigned char) (str[i]);
if (!isAcceptable(a))
{
result += "% ";
result += hex[a > > 4];
result += hex[a & 15];
}
else
{
result += str[i];
}
}
return result;
}
string URL::unEscape(const char* lpBuf)
{
int nLen;
if(!lpBuf) nLen=0;
else nLen=::lstrlen(lpBuf);
string s;
int i=0;
while(i <nLen)
{
if(lpBuf[i]== '% ')
{
BYTE c1=lpBuf[i+1];
BYTE c2=lpBuf[i+2];
i+=2;
if(c1> = '0 ' && c1 <= '9 ') c1=(c1- '0 ')*16;
else if(c1> = 'A ' && c1 <= 'Z ') c1=(c1- 'A '+10)*16;
else if(c1> = 'a ' && c1 <= 'a ') c1=(c1- 'a '+10)*16;
if(c2> = '0 ' && c2 <= '9 ') c2=c2- '0 ';
else if(c2> = 'A ' && c2 <= 'Z ') c2=c2- 'A '+10;
else if(c2> = 'a ' && c2 <= 'z ') c2=c2- 'a '+10;
char szStr[2]; szStr[0]=c1+c2; szStr[1]=0;
s+=szStr;
}
else if(lpBuf[i]== '+ ') s+= " ";
else s.append(lpBuf + i, 1);
i++;
}
return s;
}