diff options
Diffstat (limited to 'libqpdf')
-rw-r--r-- | libqpdf/QPDF_String.cc | 6 | ||||
-rw-r--r-- | libqpdf/QUtil.cc | 33 |
2 files changed, 24 insertions, 15 deletions
diff --git a/libqpdf/QPDF_String.cc b/libqpdf/QPDF_String.cc index b4d6b630..96736613 100644 --- a/libqpdf/QPDF_String.cc +++ b/libqpdf/QPDF_String.cc @@ -95,7 +95,6 @@ QPDF_String::unparse(bool force_binary) else { result += "("; - char num[5]; for (unsigned int i = 0; i < this->val.length(); ++i) { char ch = this->val[i]; @@ -140,8 +139,9 @@ QPDF_String::unparse(bool force_binary) } else { - sprintf(num, "\\%03o", static_cast<unsigned char>(ch)); // XXXX - result += num; + result += "\\" + QUtil::int_to_string_base( + static_cast<int>(static_cast<unsigned char>(ch)), + 8, 3); } break; } diff --git a/libqpdf/QUtil.cc b/libqpdf/QUtil.cc index 5c92613a..f186389d 100644 --- a/libqpdf/QUtil.cc +++ b/libqpdf/QUtil.cc @@ -24,11 +24,23 @@ std::string QUtil::int_to_string(long long num, int length) { - // Backward compatibility -- this function used to use sprintf - // with %0*d, so we interpret length such that a negative value - // appends spaces and a positive value prepends zeroes. + return int_to_string_base(num, 10, length); +} + +std::string +QUtil::int_to_string_base(long long num, int base, int length) +{ + // Backward compatibility -- int_to_string, which calls this + // function, used to use sprintf with %0*d, so we interpret length + // such that a negative value appends spaces and a positive value + // prepends zeroes. + if (! ((base == 8) || (base == 10) || (base == 16))) + { + throw std::logic_error( + "int_to_string_base called with unsupported base"); + } std::ostringstream buf; - buf << num; + buf << std::setbase(base) << std::nouppercase << num; std::string result; if ((length > 0) && (buf.str().length() < static_cast<size_t>(length))) @@ -152,16 +164,13 @@ QUtil::copy_string(std::string const& str) 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) + std::string result; + for (unsigned int i = 0; i < input.length(); ++i) { - sprintf(buf + i * 2, "%02x", static_cast<unsigned char>(input[i])); // XXXX + result += QUtil::int_to_string_base( + static_cast<int>(static_cast<unsigned char>(input[i])), 16, 2); } - return buf; + return result; } void |