diff options
author | Jay Berkenbilt <ejb@ql.org> | 2013-01-25 14:59:55 +0100 |
---|---|---|
committer | Jay Berkenbilt <ejb@ql.org> | 2013-03-04 22:45:15 +0100 |
commit | 32b62035ce9d5d07f7396cc50a0c38215f19c906 (patch) | |
tree | c7ade8bcf294574d731dc2bc7cdf42524ee023d2 /libqpdf/QUtil.cc | |
parent | 9f1594656cc4702b4b0e99a2787e18e4b00d54d3 (diff) | |
download | qpdf-32b62035ce9d5d07f7396cc50a0c38215f19c906.tar.zst |
Replace many calls to sprintf with QUtil::hex_encode
Add QUtil::hex_encode to encode binary data has a hexadecimal string,
and use it in place of sprintf where possible.
Diffstat (limited to 'libqpdf/QUtil.cc')
-rw-r--r-- | libqpdf/QUtil.cc | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 3cdfdc49..faccaee7 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -2,6 +2,7 @@ #include <qpdf/qpdf-config.h> #include <qpdf/QUtil.hh> +#include <qpdf/PointerHolder.hh> #include <stdio.h> #include <errno.h> @@ -163,6 +164,21 @@ QUtil::copy_string(std::string const& str) return result; } +std::string +QUtil::hex_encode(std::string const& input) +{ + size_t input_size = input.length(); + size_t hex_size = 1 + (2 * input_size); + PointerHolder<char> bufp(true, new char[hex_size]); + char* buf = bufp.getPointer(); + buf[hex_size - 1] = '\0'; + for (unsigned int i = 0; i < input_size; ++i) + { + sprintf(buf + i * 2, "%02x", (unsigned char)input[i]); + } + return buf; +} + void QUtil::binary_stdout() { |