Esempio n. 1
0
static int CountBinaryBytes( CUtlBuffer &buf, int *pEndGet )
{
	// This counts the number of bytes in the uuencoded text
	int nStartGet = buf.TellGet();
	buf.EatWhiteSpace();
	*pEndGet = buf.TellGet();
	int nByteCount = 0;
	while ( buf.IsValid() )
	{
		char c1 = buf.GetChar();
		char c2 = buf.GetChar();

		bool bIsNum1 = ( c1 >= '0' ) && ( c1 <= '9' );
		bool bIsNum2 = ( c2 >= '0' ) && ( c2 <= '9' );

		bool bIsAlpha1 = (( c1 >= 'A' ) && ( c1 <= 'F' )) || (( c1 >= 'a' ) && ( c1 <= 'f' ));
		bool bIsAlpha2 = (( c2 >= 'A' ) && ( c2 <= 'F' )) || (( c2 >= 'a' ) && ( c2 <= 'f' ));

		if ( !(bIsNum1 || bIsAlpha1) || !(bIsNum2 || bIsAlpha2) )
			break;

		buf.EatWhiteSpace();
		*pEndGet = buf.TellGet();
		++nByteCount;
	}
	buf.SeekGet( CUtlBuffer::SEEK_HEAD, nStartGet );
	return nByteCount;
}
Esempio n. 2
0
bool Unserialize( CUtlBuffer &buf, CUtlBinaryBlock &dest )
{
	if ( !buf.IsText() )
	{
		int nLen = buf.GetInt( );
		dest.SetLength( nLen );
		if ( dest.Length() != 0 )
		{
			buf.Get( dest.Get(), dest.Length() );
		}

		if ( nLen != dest.Length() )
		{
			buf.SeekGet( CUtlBuffer::SEEK_CURRENT, nLen - dest.Length() );
			return false;
		}

		return buf.IsValid();
	}

	int nEndGet;
	int nByteCount = CountBinaryBytes( buf, &nEndGet );
	if ( nByteCount < 0 )
		return false;

	buf.EatWhiteSpace();
	int nDest = 0;
	dest.SetLength( nByteCount );
	while( buf.TellGet() < nEndGet )
	{
		char c1 = buf.GetChar();
		char c2 = buf.GetChar();

		unsigned char b1 = HexCharToInt( c1 );
		unsigned char b2 = HexCharToInt( c2 );
		if ( b1 == 0xFF || b2 == 0xFF )
			return false;

		dest[ nDest++ ] = b2 | ( b1 << 4 );
		buf.EatWhiteSpace();
	}

	return true;
}