📄 hxinfcod.cpp
字号:
return value;
}
/////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// ToPerplexNibble()
//
// Purpose:
//
// Converts a ULONG32 into the appropriate characters for Perplex encoding.
//
void CHXInfoEncoder::ToPerplex(ULONG32 Input, char* Perplex)
{
ULONG32 value;
UCHAR charValue;
// Convert to net byte order!
value = DwToNet(Input);
for (int n = 0; n < Perplex_PER_ULONG32; n++)
{
charValue = (UCHAR)(value % Perplex_BASE);
Perplex[n] = MapToPerplex(charValue);
value = value / Perplex_BASE;
}
}
/////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// HXClientLicense::SetFromPerplex()
//
// Purpose:
//
// Sets items of the license key from a Perplexidecimal encoding of the
// license key bits.
//
// Parameters:
//
// const char* Perplex
// Pointer to a buffer that contains a Perplexidecimal encoding of the
// license key.
//
// UCHAR* Bits
// Pointer to a buffer that will contain decoded bytes of information
//
// UINT32 ulBitsLen
// Number of bytes in Bits output buffer
//
// int nSize
// Input size in bytes
//
// Return:
//
// int
// Size in bytes of decoded information
//
// Note:
//
// Perplex representations of the License Key consist of the
// representation of the bits in human readable form in a
// Perplex encoding. This basically uses a pointer to a memory
// buffer of charcters encoded as Perplex representing the bits.
// Valid characters are 0-9,A-F. Buffers out will be upper
// case letters, buffers in will be converted to upper case.
//
// These char's are Perplex encoding. They are never DBCS, the only
// valid characters are 0-9,A-F
//
UINT32 CHXInfoDecoder::SetFromPerplex(const char* Perplex, UCHAR* Bits, UINT32 ulBitsLen, UINT32 nSize)
{
UINT32 ndxBits;
UINT32 ndxPerplex = 0;
ULONG32 temp32;
for (ndxBits = 0; ndxPerplex < nSize; )
{
temp32 = FromPerplex(&Perplex[ndxPerplex]);
ndxPerplex+=Perplex_PER_ULONG32;
if (ndxBits+4 <= ulBitsLen) memcpy(&Bits[ndxBits],&temp32,sizeof(temp32)); /* Flawfinder: ignore */
ndxBits+=sizeof(temp32);
}
return ndxBits;
}
/////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// DumpToPerplex()
//
// Purpose:
//
// Formats the license key in human readable Perplexadecimal encoding.
//
// Parameters:
//
// char* Perplex
// Pointer to buffer to be filled with Perplexadecimal encoding of
// license key
//
// Return:
//
// None.
//
void CHXInfoEncoder::DumpToPerplex(char* Perplex, UINT32 ulPerplexSize, UCHAR* Bits, UINT32 nSize)
{
UINT32 ndxBits;
UINT32 ndxPerplex = 0;
ULONG32 temp32;
for (ndxBits = 0; ndxBits < nSize; )
{
if (ndxBits+4 <= nSize) memcpy(&temp32,&Bits[ndxBits],sizeof(temp32)); /* Flawfinder: ignore */
ndxBits+=sizeof(temp32);
if (ndxPerplex+Perplex_PER_ULONG32 <= ulPerplexSize) ToPerplex(temp32,&Perplex[ndxPerplex]);
ndxPerplex+=Perplex_PER_ULONG32;
}
Perplex[ndxPerplex] = '\0';
}
/////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// MapFromMIMEBase64()
//
// Purpose:
//
// Converts appropriate characters for MIME-Base64 encoding into a the
// Base64 ordinal.
//
#ifdef _WIN16
extern char* zMIMEBase64Chars;
#else
// A copy of this is kept in in pnmisc\pub\win\pnmisc16.h. If you change
// this (which is very unlikely), then be sure to change it there.
static const char zMIMEBase64Chars[] =
{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', '+', '/'
};
#endif /* _WIN16 */
#define MIME_BASE64_PADDING '='
#define FROM_MIME_BASE64_BYTES_IN 4
#define FROM_MIME_BASE64_BYTES_OUT 3
#define TO_MIME_BASE64_BYTES_IN FROM_MIME_BASE64_BYTES_OUT
#define TO_MIME_BASE64_BYTES_OUT FROM_MIME_BASE64_BYTES_IN
UCHAR CHXInfoDecoder::MapFromMIMEBase64(char MIMEBase64)
{
for (UCHAR n = 0; n < sizeof(zMIMEBase64Chars)/sizeof(zMIMEBase64Chars[0]); n++)
{
if (MIMEBase64 == zMIMEBase64Chars[n])
{
return n;
}
}
HX_ASSERT(FALSE);
return 0;
}
/////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// MapToMIMEBase64()
//
// Purpose:
//
// Converts a digit ordinal to the MIMEBase64 digit...
//
char CHXInfoEncoder::MapToMIMEBase64(UCHAR MIMEBase64)
{
HX_ASSERT( MIMEBase64 < sizeof(zMIMEBase64Chars)/sizeof(zMIMEBase64Chars[0]));
return zMIMEBase64Chars[MIMEBase64];
}
/////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// SetFromMIMEBase64()
//
// Purpose:
//
// ...
//
// Parameters:
//
// ...
//
// Return:
//
// int
// Size in bytes of decoded information
//
UINT32 CHXInfoDecoder::SetFromMIMEBase64(const char* MIMEBase64, char* Bits, UINT32 nSize)
{
UINT32 ndxBits = 0;
UINT32 ndxMIMEBase64 = 0;
BOOL bDone = FALSE;
UINT32 ndxTempIn = 0;
UCHAR tempBitsIn [FROM_MIME_BASE64_BYTES_IN];
UINT32 padding = 0;
HX_ASSERT(strlen(MIMEBase64) <= nSize);
while (!bDone)
{
HX_ASSERT(ndxMIMEBase64 <= nSize);
HX_ASSERT(ndxBits <= nSize);
// Fill in our "temporary" in buffer with the Base64 characters.
for ( ndxTempIn = 0;
ndxTempIn < FROM_MIME_BASE64_BYTES_IN && (padding == 0);
ndxTempIn++
)
{
HX_ASSERT(ndxMIMEBase64 <= nSize);
UCHAR MIMEBase64char = MIMEBase64[ndxMIMEBase64];
switch (MIMEBase64char)
{
case MIME_BASE64_PADDING:
case '\0':
{
tempBitsIn[ndxTempIn] = 0;
bDone = TRUE;
padding = (FROM_MIME_BASE64_BYTES_IN - ndxTempIn);
}
break;
default:
{
tempBitsIn[ndxTempIn] = MapFromMIMEBase64(MIMEBase64char);
}
break;
}
ndxMIMEBase64++;
}
HX_ASSERT(padding == 2 || padding == 1 || padding == 0);
// Map the Base64 in buffer to the the output buffer...
// This should map the 6 pertinate bits of each IN byte, to the
// the correct bits of the OUT byte.
{
Bits[ndxBits] = (tempBitsIn[0] << 2) + (tempBitsIn[1]>>4);
ndxBits++;
if (padding < 2)
{
Bits[ndxBits] = (tempBitsIn[1] << 4) + (tempBitsIn[2]>>2);
ndxBits++;
}
if (padding < 1)
{
Bits[ndxBits] = (tempBitsIn[2] << 6) + (tempBitsIn[3]);
ndxBits++;
}
}
}
Bits[ndxBits] = '\0';
return ndxBits;
}
/////////////////////////////////////////////////////////////////////////////
//
// Function:
//
// DumpToMIMEBase64()
//
// Purpose:
//
// ...
//
// Parameters:
//
// ...
//
// Return:
//
// None.
//
void CHXInfoEncoder::DumpToMIMEBase64(char* MIMEBase64, const char* Bits, UINT32 nSize)
{
UINT32 ndxBits = 0;
UINT32 ndxMIMEBase64 = 0;
BOOL bDone = FALSE;
UINT32 ndxTempIn = 0;
UINT32 ndxTempOut = 0;
UCHAR tempBitsIn [TO_MIME_BASE64_BYTES_IN];
UINT32 padding = 0;
HX_ASSERT(strlen(Bits) <= nSize);
while (!bDone)
{
HX_ASSERT(ndxMIMEBase64 <= nSize);
HX_ASSERT(ndxBits <= nSize);
// Fill in our "temporary" out buffer with the 6 bit chunks of the input.
for ( ndxTempIn = 0;
ndxTempIn < TO_MIME_BASE64_BYTES_IN && (padding == 0);
ndxTempIn++
)
{
UCHAR rawChar = Bits[ndxBits];
if (rawChar != '\0')
{
switch (ndxTempIn)
{
case 0:
{
tempBitsIn[0] = rawChar >> 2;
tempBitsIn[1] = (rawChar & 0x3) << 4;
}
break;
case 1:
{
tempBitsIn[1] += rawChar >> 4;
tempBitsIn[2] = (rawChar & 0xF) << 2;
}
break;
case 2:
{
tempBitsIn[2] += rawChar >> 6;
tempBitsIn[3] = (rawChar & 0x3F);
}
break;
}
}
else
{
bDone = TRUE;
padding = (TO_MIME_BASE64_BYTES_IN - ndxTempIn);
}
ndxBits++;
}
HX_ASSERT(padding == 2 || padding == 1 || padding == 0);
// Map the Base64 in buffer to the output buffer...
// This should map the 6 pertinate bits of each IN byte, as an
// entire OUT byte
for ( ndxTempOut = 0;
ndxTempOut < TO_MIME_BASE64_BYTES_OUT;
ndxTempOut++
)
{
if (ndxTempOut < (TO_MIME_BASE64_BYTES_OUT-padding))
{
MIMEBase64[ndxMIMEBase64] = MapToMIMEBase64(tempBitsIn[ndxTempOut]);
}
else
{
MIMEBase64[ndxMIMEBase64] = MIME_BASE64_PADDING;
}
ndxMIMEBase64++;
}
}
MIMEBase64[ndxMIMEBase64] = '\0';
}
//*******************************************************************
// CHXSimpleBuffer
//*******************************************************************
/////////////////////////////////////////////////////////////////////
CHXSimpleBuffer::CHXSimpleBuffer()
: m_nSize(0), m_pData(NULL), m_nGrowBy(DEFAULT_GROW_SIZE)
{
}
/////////////////////////////////////////////////////////////////////
CHXSimpleBuffer::CHXSimpleBuffer(UINT32 nSize, UINT32 nGrowBy)
: m_nSize(0), m_pData(NULL), m_nGrowBy(nGrowBy)
{
Resize(nSize);
}
/////////////////////////////////////////////////////////////////////
CHXSimpleBuffer::~CHXSimpleBuffer()
{
Free();
}
/////////////////////////////////////////////////////////////////////
BOOL CHXSimpleBuffer::IsValidOffset(UINT32 n) const
{
if (n<m_nSize)
return(TRUE);
else return(FALSE);
}
/////////////////////////////////////////////////////////////////////
// make sure that pPos is in the buffer.
// If not, the buffer is automatically resized and contents copied.
// pPos must be > pData.
BOOL CHXSimpleBuffer::EnsureValidOffset(UINT32 n)
{
if (IsValidOffset(n)==TRUE)
{
return(TRUE);
}
return (this->Resize(n));
}
/////////////////////////////////////////////////////////////////////
// copy data into the buffer at the given offset, and if the buffer
// is too small we'll resize the buffer first.
BOOL CHXSimpleBuffer::SafeMemCopy(UINT32 nOffset, const void* data, UINT32 len)
{
if ((len > 0) && (EnsureValidOffset(nOffset+len-1)==TRUE))
{
memcpy( m_pData+nOffset, data, (int)len ); /* Flawfinder: ignore */
return(TRUE);
}
return(FALSE);
}
/////////////////////////////////////////////////////////////////////
BOOL CHXSimpleBuffer::Resize(UINT32 nNewSize)
{
if (nNewSize==0)
{
Free();
return(TRUE);
}
nNewSize = RoundUpToGrowSize(nNewSize);
UCHAR* pNewBuffer = new UCHAR[nNewSize];
if (pNewBuffer==NULL) return(FALSE);
if (m_pData!=NULL)
{
// copy MIN(oldSize,newSize) elements
memcpy(pNewBuffer,m_pData, (nNewSize<m_nSize) ? (int)nNewSize : (int)m_nSize); /* Flawfinder: ignore */
delete [] m_pData;
}
m_pData = pNewBuffer;
m_nSize = nNewSize;
return(TRUE);
}
/////////////////////////////////////////////////////////////////////
UINT32 CHXSimpleBuffer::RoundUpToGrowSize(UINT32 nSize)
{
// check for div-by-zero errors (as long as DEFAULT!=0)
if (m_nGrowBy==0)
{
m_nGrowBy = DEFAULT_GROW_SIZE;
}
return ( (int)(nSize/m_nGrowBy)+1 ) * m_nGrowBy;
}
/////////////////////////////////////////////////////////////////////
void CHXSimpleBuffer::Free()
{
if (m_pData!=NULL)
delete [] m_pData;
m_pData=NULL;
m_nSize=0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -