diff options
author | m-holger <m-holger@kubitscheck.org> | 2023-03-23 19:51:28 +0100 |
---|---|---|
committer | m-holger <m-holger@kubitscheck.org> | 2023-03-23 21:25:42 +0100 |
commit | 5f4f553c3588f3ef3cb704c9d3c6db6bb78ccfa9 (patch) | |
tree | acc7ee648f983502a38198138996228dc8c9c814 | |
parent | 0795b6952bdaf5b7f8ce7842ae26676d80286dab (diff) | |
download | qpdf-5f4f553c3588f3ef3cb704c9d3c6db6bb78ccfa9.tar.zst |
Refactor QUtil::hex_decode
-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; } |