📄 rtsputil.cpp
字号:
/* ***** BEGIN LICENSE BLOCK *****
* Version: RCSL 1.0/RPSL 1.0
*
* Portions Copyright (c) 1995-2002 RealNetworks, Inc. All Rights Reserved.
*
* The contents of this file, and the files included with this file, are
* subject to the current version of the RealNetworks Public Source License
* Version 1.0 (the "RPSL") available at
* http://www.helixcommunity.org/content/rpsl unless you have licensed
* the file under the RealNetworks Community Source License Version 1.0
* (the "RCSL") available at http://www.helixcommunity.org/content/rcsl,
* in which case the RCSL will apply. You may also obtain the license terms
* directly from RealNetworks. You may not use this file except in
* compliance with the RPSL or, if you have a valid RCSL with RealNetworks
* applicable to this file, the RCSL. Please see the applicable RPSL or
* RCSL for the rights, obligations and limitations governing use of the
* contents of the file.
*
* This file is part of the Helix DNA Technology. RealNetworks is the
* developer of the Original Code and owns the copyrights in the portions
* it created.
*
* This file, and the files included with this file, is distributed and made
* available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
*
* Technology Compatibility Kit Test Suite(s) Location:
* http://www.helixcommunity.org/content/tck
*
* Contributor(s):
*
* ***** END LICENSE BLOCK ***** */
#include "hxtypes.h"
#include "hxstring.h"
#include "dbcs.h"
#include "rtsputil.h"
#include "safestring.h"
#include "hxheap.h"
#ifdef _DEBUG
#undef HX_THIS_FILE
static const char HX_THIS_FILE[] = __FILE__;
#endif
/*
* Table for encoding base64
*/
static const char Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
/*
* Table for encoding URL-safe base64
*/
static const char URLBase64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!*";
#define XX 127
/*
* Table for decoding base64
*/
static const char Index64[256] = { /* Flawfinder: ignore */
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,62,XX,XX, XX,XX,XX,XX, XX,XX,63,62, XX,XX,XX,63,
52,53,54,55, 56,57,58,59, 60,61,XX,XX, XX,XX,XX,XX,
XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
15,16,17,18, 19,20,21,22, 23,24,25,XX, XX,XX,XX,XX,
XX,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
41,42,43,44, 45,46,47,48, 49,50,51,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX
};
#define CHAR64(c) (Index64[(unsigned char)(c)])
static void
Output64Chunk(int c1, int c2, int c3, int pads, char* pBuf,
INT32 bufOffset)
{
pBuf[bufOffset++] = Base64[c1>>2];
pBuf[bufOffset++] = Base64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)];
if (pads == 2)
{
pBuf[bufOffset++] = '=';
pBuf[bufOffset++] = '=';
}
else if (pads)
{
pBuf[bufOffset++] = Base64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
pBuf[bufOffset++] = '=';
}
else
{
pBuf[bufOffset++] = Base64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
pBuf[bufOffset++] = Base64[c3 & 0x3F];
}
}
static void
OutputURL64Chunk(int c1, int c2, int c3, int pads, char* pBuf,
INT32 bufOffset)
{
pBuf[bufOffset++] = URLBase64[c1>>2];
pBuf[bufOffset++] = URLBase64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)];
if (pads == 2)
{
pBuf[bufOffset++] = '=';
pBuf[bufOffset++] = '=';
}
else if (pads)
{
pBuf[bufOffset++] = URLBase64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
pBuf[bufOffset++] = '=';
}
else
{
pBuf[bufOffset++] = URLBase64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
pBuf[bufOffset++] = URLBase64[c3 & 0x3F];
}
}
INT32
BinTo64(const BYTE* pInBuf, INT32 len, char* pOutBuf)
{
int c1, c2, c3;
//INT32 ct = 0;
//INT32 written = 0;
INT32 inOffset = 0;
INT32 outOffset = 0;
while(inOffset < len)
{
c1 = pInBuf[inOffset++];
if(inOffset == len)
{
Output64Chunk(c1, 0, 0, 2, pOutBuf, outOffset);
outOffset += 4;
}
else
{
c2 = pInBuf[inOffset++];
if(inOffset == len)
{
Output64Chunk(c1, c2, 0, 1, pOutBuf, outOffset);
outOffset += 4;
}
else
{
c3 = pInBuf[inOffset++];
Output64Chunk(c1, c2, c3, 0, pOutBuf, outOffset);
outOffset += 4;
}
}
#if XXXBAB
ct += 4;
if(ct > 44) // EOL after 45 chars
{
pOutBuf[outOffset++] = '\n';
written += 46;
ct = 0;
}
#endif
}
#if XXXBAB
if(ct)
{
pOutBuf[outOffset++] = '\n';
ct++;
}
#endif
pOutBuf[outOffset++] = '\0';
return outOffset;
}
INT32
BinToURL64(const BYTE* pInBuf, INT32 len, char* pOutBuf)
{
int c1, c2, c3;
INT32 inOffset = 0;
INT32 outOffset = 0;
while(inOffset < len)
{
c1 = pInBuf[inOffset++];
if(inOffset == len)
{
OutputURL64Chunk(c1, 0, 0, 2, pOutBuf, outOffset);
outOffset += 4;
}
else
{
c2 = pInBuf[inOffset++];
if(inOffset == len)
{
OutputURL64Chunk(c1, c2, 0, 1, pOutBuf, outOffset);
outOffset += 4;
}
else
{
c3 = pInBuf[inOffset++];
OutputURL64Chunk(c1, c2, c3, 0, pOutBuf, outOffset);
outOffset += 4;
}
}
}
pOutBuf[outOffset++] = '\0';
return outOffset;
}
INT32
BinFrom64(const char* pInBuf, INT32 len, BYTE* pOutBuf)
{
int c1, c2, c3, c4;
INT32 offset = 0;
INT32 outOffset = 0;
while(offset < len)
{
c1 = pInBuf[offset++];
if(c1 != '=' && CHAR64(c1) == XX)
{
continue;
}
else if (offset == len)
{
/* BAD data */
return -1;
}
do
{
c2 = pInBuf[offset++];
}
while(offset < len && c2 != '=' && CHAR64(c2) == XX);
if (offset == len)
{
/* BAD data */
return -1;
}
do
{
c3 = pInBuf[offset++];
}
while(offset < len && c3 != '=' && CHAR64(c3) == XX);
if (offset == len)
{
/* BAD data */
return -1;
}
do
{
c4 = pInBuf[offset++];
}
while(offset < len && c4 != '=' && CHAR64(c4) == XX);
if (offset == len && c4 != '=' && CHAR64(c4) == XX)
{
/* BAD data */
return -1;
}
if(c1 == '=' || c2 == '=')
{
continue;
}
c1 = CHAR64(c1);
c2 = CHAR64(c2);
pOutBuf[outOffset++] = ((c1 << 2) | ((c2 & 0x30) >> 4));
if(c3 == '=')
{
continue;
}
else
{
c3 = CHAR64(c3);
pOutBuf[outOffset++] = (((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2));
if(c4 == '=')
{
continue;
}
else
{
c4 = CHAR64(c4);
pOutBuf[outOffset++] = (((c3 & 0x03) << 6) | c4);
}
}
}
return outOffset;
}
const char*
EncodeCString(const char* pStr)
{
CHXString tmp;
for(size_t i=0;i<strlen(pStr);++i)
{
switch(pStr[i])
{
case '\n':
{
tmp += "\\n";
}
break;
case '\t':
{
tmp += "\\t";
}
break;
case '\r':
{
tmp += "\\r";
}
break;
case '\"':
{
tmp += "\\\"";
}
break;
case '\\':
{
tmp += "\\\\";
}
break;
default:
{
tmp += pStr[i];
}
}
}
return tmp;
}
#ifdef XXXBAB
void
printbuf(unsigned char* pBuf, int len)
{
for(int i=0; i<len; i++)
{
printf("%02x ", pBuf[i]);
}
printf("\n");
}
static unsigned char testBytes[] =
{ 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
0x4f, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x00, 0x00, 0x00, 0x00, 0x75, 0x76, 0x77, 0x78,
0x45, 0x44, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xb3, 0x5f, 0x78,
0x4f, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x00, 0x00, 0x00, 0x00, 0x75, 0x76, 0x77, 0x78,
0x45, 0x44, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f,
0x45, 0x44, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xb3, 0x5f, 0x78,
0x4f, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x00, 0x00, 0x00, 0x00, 0x75, 0x76, 0x77, 0x78,
0x45, 0x44, 0x4a, 0x5b, 0x6c, 0x7d, 0x8e, 0x9f,
0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xb3, 0x5f, 0x78,
0x4f, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10,
0x00, 0x00, 0x00, 0x00, 0x75, 0x76, 0x77, 0x78,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -