📄 rtsputil.cpp
字号:
0x45, 0x44, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f,
0x77, 0x88, 0x00 };
static unsigned char testBytes2[] =
{ 0x7F, 0x63, 0x31, 0xBB, 0x90, 0x21, 0x32, 0x11,
0xC4 };
void main()
{
unsigned char encodeBuf[300]; /* Flawfinder: ignore */
unsigned char decodeBuf[300]; /* Flawfinder: ignore */
memset(encodeBuf, '\0', sizeof(encodeBuf));
memset(decodeBuf, '\0', sizeof(decodeBuf));
int rc = BinTo64(testBytes, sizeof(testBytes), encodeBuf);
printf("original size: %d, encoded size: %d\n", sizeof(testBytes), rc);
printf("%s\n", encodeBuf);
rc = BinFrom64(encodeBuf, rc, decodeBuf);
printf("decode size: %d\n", rc);
printbuf(decodeBuf, rc);
}
#endif /* XXXBAB */
const char escapeChars[] =
{
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,0,0,1,1,0,1,0,0,
0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};
/*
* Function: URLEscapeBuffer
*/
INT32
URLEscapeBuffer(const char* pInBuf, INT32 len, char* pOutBuf)
{
const char* pInputPos = pInBuf;
char* pOutputPos = pOutBuf;
char pTemp[3]; /* Flawfinder: ignore */
if (!pInBuf || !len || !pOutBuf)
{
return -1;
}
// Iterate through the input buffer
while (pInputPos < pInBuf + len)
{
BOOL bIsIllegal = FALSE;
if (HXIsDBCSEnabled())
{
// If an illegal URL character is found, translate it
bIsIllegal =
HXIsEqual(pInputPos, 0x7f) ||
HXIsEqual(pInputPos, '%') ||
HXIsEqual(pInputPos, '"') ||
HXIsEqual(pInputPos, '+') ||
HXIsEqual(pInputPos, '<') ||
HXIsEqual(pInputPos, '>') ||
HXIsEqual(pInputPos, ',') ||
HXIsEqual(pInputPos, '#') ||
HXIsEqual(pInputPos, 0x01) ||
HXIsEqual(pInputPos, 0x02) ||
HXIsEqual(pInputPos, 0x03) ||
HXIsEqual(pInputPos, 0x04) ||
HXIsEqual(pInputPos, 0x05) ||
HXIsEqual(pInputPos, 0x06) ||
HXIsEqual(pInputPos, 0x07) ||
HXIsEqual(pInputPos, 0x08) ||
HXIsEqual(pInputPos, 0x09) ||
HXIsEqual(pInputPos, 0x0a) ||
HXIsEqual(pInputPos, 0x0b) ||
HXIsEqual(pInputPos, 0x0c) ||
HXIsEqual(pInputPos, 0x0d) ||
HXIsEqual(pInputPos, 0x0e) ||
HXIsEqual(pInputPos, 0x0f) ||
HXIsEqual(pInputPos, 0x10) ||
HXIsEqual(pInputPos, 0x11) ||
HXIsEqual(pInputPos, 0x12) ||
HXIsEqual(pInputPos, 0x13) ||
HXIsEqual(pInputPos, 0x14) ||
HXIsEqual(pInputPos, 0x15) ||
HXIsEqual(pInputPos, 0x16) ||
HXIsEqual(pInputPos, 0x17) ||
HXIsEqual(pInputPos, 0x18) ||
HXIsEqual(pInputPos, 0x19) ||
HXIsEqual(pInputPos, 0x1a) ||
HXIsEqual(pInputPos, 0x1b) ||
HXIsEqual(pInputPos, 0x1c) ||
HXIsEqual(pInputPos, 0x1d) ||
HXIsEqual(pInputPos, 0x1e) ||
HXIsEqual(pInputPos, 0x1f);
}
else
{
// Use a lookup table for improved performance
bIsIllegal = (BOOL)escapeChars[(UCHAR)*pInputPos];
}
if (bIsIllegal)
{
SafeSprintf(pTemp, sizeof(pTemp), "%02x", *pInputPos);
*pOutputPos++ = '%';
*pOutputPos++ = pTemp[0];
*pOutputPos++ = pTemp[1];
}
else if (HXIsEqual(pInputPos, ' '))
{
*pOutputPos++ = '+';
}
else
{
*pOutputPos++ = *pInputPos;
if (HXIsLeadByte(*pInputPos))
{
*pOutputPos++ = *(pInputPos + 1);
}
}
pInputPos = HXGetNextChar(pInputPos);
}
// Return the length of the escaped content
return (pOutputPos - pOutBuf);
}
const char escapeCharsNoReserved[] =
{ //8,9,A,B,C,D,E,F
0,1,1,1,1,1,1,1, // 0x00
1,1,1,1,1,1,1,1, // 0x08
1,1,1,1,1,1,1,1, // 0x10
1,1,1,1,1,1,1,1, // 0x18
1,0,1,1,0,1,0,0, // 0x20
0,0,0,0,0,0,0,0, // 0x28
0,0,0,0,0,0,0,0, // 0x30
0,0,0,0,1,0,1,0, // 0x38
0,0,0,0,0,0,0,0, // 0x40
0,0,0,0,0,0,0,0, // 0x48
0,0,0,0,0,0,0,0, // 0x50
0,0,0,1,1,1,1,0, // 0x58
1,0,0,0,0,0,0,0, // 0x60
0,0,0,0,0,0,0,0, // 0x68
0,0,0,0,0,0,0,0, // 0x70
0,0,0,1,1,1,1,1, // 0x78
1,1,1,1,1,1,1,1, // 0x80
1,1,1,1,1,1,1,1, // 0x88
1,1,1,1,1,1,1,1, // 0x90
1,1,1,1,1,1,1,1, // 0x98
1,1,1,1,1,1,1,1, // 0xA0
1,1,1,1,1,1,1,1, // 0xA8
1,1,1,1,1,1,1,1, // 0xB0
1,1,1,1,1,1,1,1, // 0xB8
1,1,1,1,1,1,1,1, // 0xC0
1,1,1,1,1,1,1,1, // 0xC8
1,1,1,1,1,1,1,1, // 0xD0
1,1,1,1,1,1,1,1, // 0xD8
1,1,1,1,1,1,1,1, // 0xE0
1,1,1,1,1,1,1,1, // 0xE8
1,1,1,1,1,1,1,1, // 0xF0
1,1,1,1,1,1,1,1 // 0xF8
};
const char escapeCharsReserved[] =
{ //8,9,A,B,C,D,E,F
0,1,1,1,1,1,1,1, // 0x00
1,1,1,1,1,1,1,1, // 0x08
1,1,1,1,1,1,1,1, // 0x10
1,1,1,1,1,1,1,1, // 0x18
1,0,1,1,0,1,1,0, // 0x20
0,0,0,0,0,0,0,1, // 0x28
0,0,0,0,0,0,0,0, // 0x30
0,0,1,1,1,1,1,1, // 0x38
1,0,0,0,0,0,0,0, // 0x40
0,0,0,0,0,0,0,0, // 0x48
0,0,0,0,0,0,0,0, // 0x50
0,0,0,1,1,1,1,0, // 0x58
1,0,0,0,0,0,0,0, // 0x60
0,0,0,0,0,0,0,0, // 0x68
0,0,0,0,0,0,0,0, // 0x70
0,0,0,1,1,1,1,1, // 0x78
1,1,1,1,1,1,1,1, // 0x80
1,1,1,1,1,1,1,1, // 0x88
1,1,1,1,1,1,1,1, // 0x90
1,1,1,1,1,1,1,1, // 0x98
1,1,1,1,1,1,1,1, // 0xA0
1,1,1,1,1,1,1,1, // 0xA8
1,1,1,1,1,1,1,1, // 0xB0
1,1,1,1,1,1,1,1, // 0xB8
1,1,1,1,1,1,1,1, // 0xC0
1,1,1,1,1,1,1,1, // 0xC8
1,1,1,1,1,1,1,1, // 0xD0
1,1,1,1,1,1,1,1, // 0xD8
1,1,1,1,1,1,1,1, // 0xE0
1,1,1,1,1,1,1,1, // 0xE8
1,1,1,1,1,1,1,1, // 0xF0
1,1,1,1,1,1,1,1 // 0xF8
};
/*
* Function: URLEscapeBufferReserved this function will escape all potentially unsafe charactors.
*/
INT32
URLEscapeBuffer2(const char* pInBuf, INT32 len, char* pOutBuf, BOOL bReserved)
{
const char* pInputPos = pInBuf;
char* pOutputPos = pOutBuf;
char pTemp[3];
if (!pInBuf || !len || !pOutBuf)
{
return -1;
}
const char* lookupTable = NULL;
if (bReserved)
{
lookupTable = (const char*)escapeCharsReserved;
}
else
{
lookupTable = (const char*)escapeCharsNoReserved;
}
// Iterate through the input buffer
while (pInputPos < pInBuf + len)
{
// Use a lookup table for improved performance
BOOL bIsIllegal = (BOOL)lookupTable[(UCHAR)*pInputPos];
if (bIsIllegal)
{
SafeSprintf(pTemp, sizeof(pTemp), "%02x", *pInputPos);
*pOutputPos++ = '%';
*pOutputPos++ = pTemp[0];
*pOutputPos++ = pTemp[1];
}
else
{
*pOutputPos++ = *pInputPos;
}
++pInputPos;
}
// Return the length of the escaped content
return (pOutputPos - pOutBuf);
}
/*
* Function: URLUnescapeBuffer
*/
INT32
URLUnescapeBuffer(const char* pInBuf, INT32 len, char* pOutBuf)
{
char* pOutputPos = pOutBuf;
char pTemp[3];
if (!pInBuf || !len || !pOutBuf)
{
return -1;
}
// Iterate through the input buffer
for (INT32 i = 0; i < len; i++)
{
// Ignore whitespace
if ((UCHAR)pInBuf[i] < 21)
{
continue;
}
// If an escaped character is found, translate it
else if (pInBuf[i] == '%')
{
if (len < i + 3)
{
// Incomplete %xx representation
return -1;
}
// Ignore whitespace
while (pInBuf[i + 1] < 21)
{
i++;
if (len < i + 3)
{
return -1;
}
}
pTemp[0] = pInBuf[i + 1];
// Ignore whitespace
while (pInBuf[i + 2] < 21)
{
i++;
if (len < i + 3)
{
return -1;
}
}
pTemp[1] = pInBuf[i + 2];
pTemp[2] = '\0';
*pOutputPos++ = (char)strtol(pTemp, NULL, 16);
i += 2;
}
else if (pInBuf[i] == '+')
{
*pOutputPos++ = ' ';
}
else
{
*pOutputPos++ = pInBuf[i];
}
}
// Return the length of the escaped content
return (pOutputPos - pOutBuf);
}
#ifdef XXXDPS
static const char* pTestTable[] =
{
"this is a test, this is only a test",
"abcdefghijklmnopqrstuvwxyz0123456789/NoneOfThisShouldGetEscaped...",
"\r\n\t,#><+\"%All Of The First 10 Chars Should Get Escaped",
"Insert a DBCS test string here."
};
void main()
{
unsigned char encodeBuf[300]; /* Flawfinder: ignore */
unsigned char decodeBuf[300]; /* Flawfinder: ignore */
for (int i = 0; i < 4; i++)
{
memset(encodeBuf, '\0', sizeof(encodeBuf));
memset(decodeBuf, '\0', sizeof(decodeBuf));
printf("Escaping >>>%s<<<\n", pTestTable[i]);
int rc = URLEscapeBuffer((char*)pTestTable[i], strlen(pTestTable[i]), (char*)encodeBuf);
printf("original size: %d, encoded size: %d\n", strlen(pTestTable[i]), rc);
printf("%s\n", encodeBuf);
rc = URLUnescapeBuffer((char*)encodeBuf, rc, (char*)decodeBuf);
printf("decode size: %d\n", rc);
printf("%s\n", decodeBuf);
printf("\n");
}
}
#endif /* XXXDPS */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -