Have strUnhex report malformed hex.

This commit is contained in:
Arthur Britto
2013-01-14 16:58:14 -08:00
parent 2b70d3f750
commit f2337b81db
2 changed files with 39 additions and 5 deletions

View File

@@ -75,14 +75,48 @@ int charUnHex(char cDigit)
: -1;
}
void strUnHex(std::string& strDst, const std::string& strSrc)
int strUnHex(std::string& strDst, const std::string& strSrc)
{
int iBytes = strSrc.size()/2;
int iBytes = (strSrc.size()+1)/2;
strDst.resize(iBytes);
for (int i=0; i != iBytes; i++)
strDst[i] = (charUnHex(strSrc[i*2]) << 4) | charUnHex(strSrc[i*2+1]);
const char* pSrc = &strSrc[0];
char* pDst = &strDst[0];
if (strSrc.size() & 1)
{
int c = charUnHex(*pSrc++);
if (c < 0)
{
iBytes = -1;
}
else
{
*pDst++ = c;
}
}
for (int i=0; iBytes >= 0 && i != iBytes; i++)
{
int cHigh = charUnHex(*pSrc++);
int cLow = charUnHex(*pSrc++);
if (cHigh < 0 || cLow < 0)
{
iBytes = -1;
}
else
{
strDst[i] = (cHigh << 4) | cLow;
}
}
if (iBytes < 0)
strDst.clear();
return iBytes;
}
std::vector<unsigned char> strUnHex(const std::string& strSrc)

View File

@@ -198,7 +198,7 @@ bool isZero(Iterator first, int iSize)
}
int charUnHex(char cDigit);
void strUnHex(std::string& strDst, const std::string& strSrc);
int strUnHex(std::string& strDst, const std::string& strSrc);
uint64_t uintFromHex(const std::string& strSrc);