From 32b62035ce9d5d07f7396cc50a0c38215f19c906 Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Fri, 25 Jan 2013 08:59:55 -0500 Subject: 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. --- libqpdf/MD5.cc | 11 +---------- libqpdf/Pl_SHA2.cc | 13 ++----------- libqpdf/QPDF_Name.cc | 5 ++--- libqpdf/QPDF_String.cc | 9 +-------- libqpdf/QUtil.cc | 16 ++++++++++++++++ 5 files changed, 22 insertions(+), 32 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/MD5.cc b/libqpdf/MD5.cc index 70be696d..57bcc45f 100644 --- a/libqpdf/MD5.cc +++ b/libqpdf/MD5.cc @@ -386,16 +386,7 @@ void MD5::print() std::string MD5::unparse() { final(); - - char result[33]; - char* p = result; - unsigned int i; - for (i = 0; i < 16; ++i) - { - sprintf(p, "%02x", digest_val[i]); - p += 2; - } - return result; + return QUtil::hex_encode(std::string((char*)digest_val, 16)); } std::string diff --git a/libqpdf/Pl_SHA2.cc b/libqpdf/Pl_SHA2.cc index 018f411f..84f24d6e 100644 --- a/libqpdf/Pl_SHA2.cc +++ b/libqpdf/Pl_SHA2.cc @@ -2,6 +2,7 @@ #include #include #include +#include Pl_SHA2::Pl_SHA2(int bits, Pipeline* next) : Pipeline("sha2", next), @@ -150,15 +151,5 @@ Pl_SHA2::getHexDigest() throw std::logic_error( "digest requested for in-progress SHA2 Pipeline"); } - std::string raw = getRawDigest(); - size_t raw_size = raw.length(); - size_t hex_size = 1 + (2 * raw_size); - PointerHolder bufp(true, new char[hex_size]); - char* buf = bufp.getPointer(); - buf[hex_size - 1] = '\0'; - for (unsigned int i = 0; i < raw_size; ++i) - { - std::sprintf(buf + i * 2, "%02x", (unsigned char)raw[i]); - } - return buf; + return QUtil::hex_encode(getRawDigest()); } diff --git a/libqpdf/QPDF_Name.cc b/libqpdf/QPDF_Name.cc index 19610bff..9adb25b5 100644 --- a/libqpdf/QPDF_Name.cc +++ b/libqpdf/QPDF_Name.cc @@ -2,6 +2,7 @@ #include #include +#include QPDF_Name::QPDF_Name(std::string const& name) : name(name) @@ -16,7 +17,6 @@ std::string QPDF_Name::normalizeName(std::string const& name) { std::string result; - char num[4]; result += name[0]; for (unsigned int i = 1; i < name.length(); ++i) { @@ -24,8 +24,7 @@ QPDF_Name::normalizeName(std::string const& name) // Don't use locale/ctype here; follow PDF spec guidelines. if (strchr("#()<>[]{}/%", ch) || (ch < 33) || (ch > 126)) { - sprintf(num, "#%02x", (unsigned char) ch); - result += num; + result += "#" + QUtil::hex_encode(std::string(&ch, 1)); } else { diff --git a/libqpdf/QPDF_String.cc b/libqpdf/QPDF_String.cc index 3a8e7074..619adef6 100644 --- a/libqpdf/QPDF_String.cc +++ b/libqpdf/QPDF_String.cc @@ -90,14 +90,7 @@ QPDF_String::unparse(bool force_binary) std::string result; if (use_hexstring) { - result += "<"; - char num[3]; - for (unsigned int i = 0; i < this->val.length(); ++i) - { - sprintf(num, "%02x", (unsigned char) this->val[i]); - result += num; - } - result += ">"; + result += "<" + QUtil::hex_encode(this->val) + ">"; } else { 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 #include +#include #include #include @@ -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 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() { -- cgit v1.2.3-54-g00ecf