rendered paste bodyvoid Hex16String2RawBuffer( const char * pszASCIIEncodedRawBuffer, unsigned char * pszByteRawBuffer, size_t nOutputLength )
{
size_t nInputLength = ::strlen( pszASCIIEncodedRawBuffer );
//! If buffer is not the exact multiple of 2, please report it immediatelly
if( nInputLength %2 != 0 )
throw std::runtime_error( "Odd size of buffer. Buffer size must be even." );
if( nInputLength / 2 > nOutputLength )
throw std::runtime_error( "Invalid output buffer size." );
unsigned char * pszOutputBegin = pszByteRawBuffer;
unsigned char * pszOutputEnd = pszByteRawBuffer + nOutputLength;
const size_t nChunkSize = 4;
int nBytes[ nChunkSize ] = { 0 } ;
size_t nNumberOfChunks = nOutputLength / nChunkSize;
size_t nReminder = nOutputLength % nChunkSize;
for( size_t nIndex = 0 ; nIndex < nNumberOfChunks ; ++nIndex )
{
int nFieldsParsed = ::sscanf( pszASCIIEncodedRawBuffer, "%2x%2x%2x%2x", &nBytes[ 0 ], &nBytes[ 1 ], &nBytes[ 2 ], &nBytes[ 3 ] );
if( nFieldsParsed == 4 )
{
*pszOutputBegin++ = ( unsigned char ) nBytes[ 0 ];
*pszOutputBegin++ = ( unsigned char ) nBytes[ 1 ];
*pszOutputBegin++ = ( unsigned char ) nBytes[ 2 ];
*pszOutputBegin++ = ( unsigned char ) nBytes[ 3 ];
pszASCIIEncodedRawBuffer += 8;
}
else
{
throw std::runtime_error( "Failed with conversion at position..." );
}
}
switch( nReminder )
{
case 1:
{
int nFieldsParsed = ::sscanf( pszASCIIEncodedRawBuffer, "%2x",
&nBytes[ 0 ] );
if( nFieldsParsed == 1 )
{
*pszOutputBegin++ = ( unsigned char ) nBytes[ 0 ];
}
else
{
throw std::runtime_error( "Failed with conversion at position..." );
}
}
break;
case 2:
{
int nFieldsParsed = ::sscanf( pszASCIIEncodedRawBuffer, "%2x%2x",
&nBytes[ 0 ],
&nBytes[ 1 ] );
if( nFieldsParsed == 2 )
{
*pszOutputBegin++ = ( unsigned char ) nBytes[ 0 ];
*pszOutputBegin++ = ( unsigned char ) nBytes[ 1 ];
}
else
{
throw std::runtime_error( "Failed with conversion at position..." );
}
}
break;
case 3:
{
int nFieldsParsed = ::sscanf( pszASCIIEncodedRawBuffer, "%2x%2x%2x",
&nBytes[ 0 ],
&nBytes[ 1 ],
&nBytes[ 2 ] );
if( nFieldsParsed == 3 )
{
*pszOutputBegin++ = ( unsigned char ) nBytes[ 0 ];
*pszOutputBegin++ = ( unsigned char ) nBytes[ 1 ];
*pszOutputBegin++ = ( unsigned char ) nBytes[ 2 ];
}
else
{
throw std::runtime_error( "Failed with conversion at position..." );
}
}
break;
case 0:
default:
break;
}
}