From 3e306ae64cc3d160034f27d72ad27bee03a65aa5 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sun, 14 Jan 2018 09:04:13 -0500 Subject: Add QUtil::hex_decode --- libqpdf/QUtil.cc | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'libqpdf/QUtil.cc') diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 93374568..1cc5449a 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -290,6 +290,50 @@ QUtil::hex_encode(std::string const& input) return result; } +std::string +QUtil::hex_decode(std::string const& input) +{ + std::string result; + size_t pos = 0; + for (std::string::const_iterator p = input.begin(); p != input.end(); ++p) + { + char ch = *p; + bool skip = false; + if ((*p >= 'A') && (*p <= 'F')) + { + ch -= 'A'; + ch += 10; + } + else if ((*p >= 'a') && (*p <= 'f')) + { + ch -= 'a'; + ch += 10; + } + else if ((*p >= '0') && (*p <= '9')) + { + ch -= '0'; + } + else + { + skip = true; + } + if (! skip) + { + if (pos == 0) + { + result.push_back(ch << 4); + pos = 1; + } + else + { + result.back() += ch; + pos = 0; + } + } + } + return result; +} + void QUtil::binary_stdout() { -- cgit v1.2.3-54-g00ecf