inline unsigned char ToHex(unsigned char x)
{
return x > 9 ? x + 55 : x + 48;
}
inline unsigned char FromHex(unsigned char x)
{
unsigned char y;
if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
else if (x >= '0' && x <= '9') y = x - '0';
else assert(0);
return y;
}
//1个字符会变三个字符,所以nBufferLen应是strlen(cIn)的三倍且再加一个字符串结束符才不会溢出
inline void UrlEncode(char*cOut,int nBufferLen,const char*cIn)
{
if (cOut == NULL || cIn == NULL)return;
size_t length = strlen(cIn);
//char *cDst = new char[length*3+1];
unsigned char*pDst = (unsigned char*)cOut;
unsigned char*pSrc = (unsigned char*)cIn;
int nAt = 0;
size_t i = 0;
while (i<length && pSrc[i] != 0 && nAt < nBufferLen)
{
if (isalnum(pSrc[i]) ||
(pSrc[i] == '-') ||
(pSrc[i] == '_') ||
(pSrc[i] == '.') ||
(pSrc[i] == '~'))
{
pDst[nAt++] = pSrc[i];
}
else if (pSrc[i] == ' ')
{
pDst[nAt++] = '+';
}
else
{
if (nAt > nBufferLen - 3)break;
pDst[nAt++] = '%';
pDst[nAt++] = ToHex(pSrc[i] >> 4);
pDst[nAt++] = ToHex(pSrc[i] % 16);
}
i++;
}
pDst[nAt] = 0;
}
//Decode三个字符会改为一个,所以nBufferLen应是strlen(cIn)的1倍也不会溢出
inline void UrlDecode(char*cOut, int nBufferLen, const char*cIn)
{
size_t length = strlen(cIn);
unsigned char*pDst = (unsigned char*)cOut;
unsigned char*pSrc = (unsigned char*)cIn;
int nAt = 0;
size_t i = 0;
while (i<length && pSrc[i] != 0 && nAt < nBufferLen)
{
if (pSrc[i] == '+')
{
pDst[nAt++] = ' ';
}
else if (pSrc[i] == '%')
{
if (i + 2 < length)
{
unsigned char high = FromHex(pSrc[++i]);
unsigned char low = FromHex(pSrc[++i]);
pDst[nAt++] = high * 16 + low;
}
}
else
{
pDst[nAt++] = pSrc[i];
}
i++;
}
pDst[nAt] = 0;
}
{
return x > 9 ? x + 55 : x + 48;
}
inline unsigned char FromHex(unsigned char x)
{
unsigned char y;
if (x >= 'A' && x <= 'Z') y = x - 'A' + 10;
else if (x >= 'a' && x <= 'z') y = x - 'a' + 10;
else if (x >= '0' && x <= '9') y = x - '0';
else assert(0);
return y;
}
//1个字符会变三个字符,所以nBufferLen应是strlen(cIn)的三倍且再加一个字符串结束符才不会溢出
inline void UrlEncode(char*cOut,int nBufferLen,const char*cIn)
{
if (cOut == NULL || cIn == NULL)return;
size_t length = strlen(cIn);
//char *cDst = new char[length*3+1];
unsigned char*pDst = (unsigned char*)cOut;
unsigned char*pSrc = (unsigned char*)cIn;
int nAt = 0;
size_t i = 0;
while (i<length && pSrc[i] != 0 && nAt < nBufferLen)
{
if (isalnum(pSrc[i]) ||
(pSrc[i] == '-') ||
(pSrc[i] == '_') ||
(pSrc[i] == '.') ||
(pSrc[i] == '~'))
{
pDst[nAt++] = pSrc[i];
}
else if (pSrc[i] == ' ')
{
pDst[nAt++] = '+';
}
else
{
if (nAt > nBufferLen - 3)break;
pDst[nAt++] = '%';
pDst[nAt++] = ToHex(pSrc[i] >> 4);
pDst[nAt++] = ToHex(pSrc[i] % 16);
}
i++;
}
pDst[nAt] = 0;
}
//Decode三个字符会改为一个,所以nBufferLen应是strlen(cIn)的1倍也不会溢出
inline void UrlDecode(char*cOut, int nBufferLen, const char*cIn)
{
size_t length = strlen(cIn);
unsigned char*pDst = (unsigned char*)cOut;
unsigned char*pSrc = (unsigned char*)cIn;
int nAt = 0;
size_t i = 0;
while (i<length && pSrc[i] != 0 && nAt < nBufferLen)
{
if (pSrc[i] == '+')
{
pDst[nAt++] = ' ';
}
else if (pSrc[i] == '%')
{
if (i + 2 < length)
{
unsigned char high = FromHex(pSrc[++i]);
unsigned char low = FromHex(pSrc[++i]);
pDst[nAt++] = high * 16 + low;
}
}
else
{
pDst[nAt++] = pSrc[i];
}
i++;
}
pDst[nAt] = 0;
}