diff options
author | Jay Berkenbilt <jberkenbilt@users.noreply.github.com> | 2023-04-01 15:31:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-01 15:31:28 +0200 |
commit | ef6f5a97f42a6d59abc9ca81ae4e9ab3b7168f55 (patch) | |
tree | 49a9071c15e3947d581cb33f2d5bfc129e1c9593 /libqpdf/QUtil.cc | |
parent | b62ad7ecc625963da9578d47d1ccb2809d6f0452 (diff) | |
parent | 5f4f553c3588f3ef3cb704c9d3c6db6bb78ccfa9 (diff) | |
download | qpdf-ef6f5a97f42a6d59abc9ca81ae4e9ab3b7168f55.tar.zst |
Merge pull request #936 from m-holger/hex
Refactor QUtil::hex_decode
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r-- | libqpdf/QUtil.cc | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index bae067b6..03301d9d 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -783,28 +783,25 @@ std::string QUtil::hex_decode(std::string const& input) { std::string result; - size_t pos = 0; + // We know result.size() <= 0.5 * input.size() + 1. However, reserving + // string space for this upper bound has a negative impact. + bool first = true; + char decoded; for (auto ch: input) { - bool skip = false; - if ((ch >= 'A') && (ch <= 'F')) { - ch = QIntC::to_char(ch - 'A' + 10); - } else if ((ch >= 'a') && (ch <= 'f')) { - ch = QIntC::to_char(ch - 'a' + 10); - } else if ((ch >= '0') && (ch <= '9')) { - ch = QIntC::to_char(ch - '0'); - } else { - skip = true; - } - if (!skip) { - if (pos == 0) { - result.push_back(static_cast<char>(ch << 4)); - pos = 1; + ch = hex_decode_char(ch); + if (ch < '\20') { + if (first) { + decoded = static_cast<char>(ch << 4); + first = false; } else { - result[result.length() - 1] |= ch; - pos = 0; + result.push_back(decoded | ch); + first = true; } } } + if (!first) { + result.push_back(decoded); + } return result; } |