mirror of
https://bitbucket.org/smil3y/katie.git
synced 2025-02-24 02:42:55 +00:00
warn when invalid character is detected in QByteArray::fromHex()
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
parent
3998302179
commit
2fcfd9dff3
1 changed files with 22 additions and 14 deletions
|
@ -3712,7 +3712,7 @@ QByteArray QByteArray::fromBase64(const QByteArray &base64)
|
|||
}
|
||||
|
||||
/*!
|
||||
Returns a decoded copy of the hex encoded array \a hexEncoded. Input is not checked
|
||||
Returns a decoded copy of the hex encoded array \a hexEncoded. Input is checked
|
||||
for validity; invalid characters in the input are skipped, enabling the
|
||||
decoding process to continue with subsequent characters.
|
||||
|
||||
|
@ -3725,31 +3725,39 @@ QByteArray QByteArray::fromBase64(const QByteArray &base64)
|
|||
QByteArray QByteArray::fromHex(const QByteArray &hexEncoded)
|
||||
{
|
||||
QByteArray res((hexEncoded.size() + 1)/ 2, Qt::Uninitialized);
|
||||
uchar *result = (uchar *)res.d->data + res.size();
|
||||
char *result = res.d->data + res.size();
|
||||
|
||||
bool odd_digit = true;
|
||||
int invalidcount = 0;
|
||||
bool oddhex = true;
|
||||
for (int i = hexEncoded.size() - 1; i >= 0; --i) {
|
||||
int ch = hexEncoded.at(i);
|
||||
int tmp;
|
||||
if (ch >= '0' && ch <= '9')
|
||||
tmp = ch - '0';
|
||||
else if (ch >= 'a' && ch <= 'f')
|
||||
tmp = ch - 'a' + 10;
|
||||
else if (ch >= 'A' && ch <= 'F')
|
||||
if (ch >= '0' && ch <= '9') {
|
||||
tmp = (ch - '0');
|
||||
} else if (ch >= 'a' && ch <= 'f') {
|
||||
tmp = (ch - 'a' + 10);
|
||||
} else if (ch >= 'A' && ch <= 'F') {
|
||||
tmp = ch - 'A' + 10;
|
||||
else
|
||||
} else {
|
||||
invalidcount++;
|
||||
continue;
|
||||
if (odd_digit) {
|
||||
}
|
||||
|
||||
if (oddhex) {
|
||||
--result;
|
||||
*result = tmp;
|
||||
odd_digit = false;
|
||||
oddhex = false;
|
||||
} else {
|
||||
*result |= tmp << 4;
|
||||
odd_digit = true;
|
||||
*result |= (tmp << 4);
|
||||
oddhex = true;
|
||||
}
|
||||
}
|
||||
|
||||
res.remove(0, result - (const uchar *)res.constData());
|
||||
if (Q_UNLIKELY(invalidcount > 0)) {
|
||||
qWarning("QByteArray::fromHex: Invalid character in hex");
|
||||
res.remove(0, invalidcount);
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue