2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-12-22 10:58:58 +00:00

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.
This commit is contained in:
Jay Berkenbilt 2013-01-25 08:59:55 -05:00
parent 9f1594656c
commit 32b62035ce
9 changed files with 34 additions and 50 deletions

View File

@ -16,6 +16,9 @@
2013-01-25 Jay Berkenbilt <ejb@ql.org> 2013-01-25 Jay Berkenbilt <ejb@ql.org>
* New method QUtil::hex_encode to encode binary data as a
hexadecimal string
* qpdf --check was exiting with status 0 in some rare cases even * qpdf --check was exiting with status 0 in some rare cases even
when errors were found. It now always exits with one of the when errors were found. It now always exits with one of the
document error codes (0 for success, 2 for errors, 3 or warnings). document error codes (0 for success, 2 for errors, 3 or warnings).

View File

@ -33,15 +33,7 @@ ParserCallbacks::handleObject(QPDFObjectHandle obj)
std::cout << obj.getTypeName() << ": "; std::cout << obj.getTypeName() << ": ";
if (obj.isInlineImage()) if (obj.isInlineImage())
{ {
std::string val = obj.getInlineImageValue(); std::cout << QUtil::hex_encode(obj.getInlineImageValue()) << std::endl;
char buf[3];
buf[2] = '\0';
for (size_t i = 0; i < val.length(); ++i)
{
sprintf(buf, "%02x", (unsigned char)(val[i]));
std::cout << buf;
}
std::cout << std::endl;
} }
else else
{ {

View File

@ -56,6 +56,12 @@ namespace QUtil
QPDF_DLL QPDF_DLL
char* copy_string(std::string const&); char* copy_string(std::string const&);
// Returns lower-case hex-encoded version of the string, treating
// each character in the input string as unsigned. The output
// string will be twice as long as the input string.
QPDF_DLL
std::string hex_encode(std::string const&);
// Set stdin, stdout to binary mode // Set stdin, stdout to binary mode
QPDF_DLL QPDF_DLL
void binary_stdout(); void binary_stdout();

View File

@ -386,16 +386,7 @@ void MD5::print()
std::string MD5::unparse() std::string MD5::unparse()
{ {
final(); final();
return QUtil::hex_encode(std::string((char*)digest_val, 16));
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;
} }
std::string std::string

View File

@ -2,6 +2,7 @@
#include <stdexcept> #include <stdexcept>
#include <cstdio> #include <cstdio>
#include <qpdf/PointerHolder.hh> #include <qpdf/PointerHolder.hh>
#include <qpdf/QUtil.hh>
Pl_SHA2::Pl_SHA2(int bits, Pipeline* next) : Pl_SHA2::Pl_SHA2(int bits, Pipeline* next) :
Pipeline("sha2", next), Pipeline("sha2", next),
@ -150,15 +151,5 @@ Pl_SHA2::getHexDigest()
throw std::logic_error( throw std::logic_error(
"digest requested for in-progress SHA2 Pipeline"); "digest requested for in-progress SHA2 Pipeline");
} }
std::string raw = getRawDigest(); return QUtil::hex_encode(getRawDigest());
size_t raw_size = raw.length();
size_t hex_size = 1 + (2 * raw_size);
PointerHolder<char> 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;
} }

View File

@ -2,6 +2,7 @@
#include <string.h> #include <string.h>
#include <stdio.h> #include <stdio.h>
#include <qpdf/QUtil.hh>
QPDF_Name::QPDF_Name(std::string const& name) : QPDF_Name::QPDF_Name(std::string const& name) :
name(name) name(name)
@ -16,7 +17,6 @@ std::string
QPDF_Name::normalizeName(std::string const& name) QPDF_Name::normalizeName(std::string const& name)
{ {
std::string result; std::string result;
char num[4];
result += name[0]; result += name[0];
for (unsigned int i = 1; i < name.length(); ++i) 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. // Don't use locale/ctype here; follow PDF spec guidelines.
if (strchr("#()<>[]{}/%", ch) || (ch < 33) || (ch > 126)) if (strchr("#()<>[]{}/%", ch) || (ch < 33) || (ch > 126))
{ {
sprintf(num, "#%02x", (unsigned char) ch); result += "#" + QUtil::hex_encode(std::string(&ch, 1));
result += num;
} }
else else
{ {

View File

@ -90,14 +90,7 @@ QPDF_String::unparse(bool force_binary)
std::string result; std::string result;
if (use_hexstring) if (use_hexstring)
{ {
result += "<"; result += "<" + QUtil::hex_encode(this->val) + ">";
char num[3];
for (unsigned int i = 0; i < this->val.length(); ++i)
{
sprintf(num, "%02x", (unsigned char) this->val[i]);
result += num;
}
result += ">";
} }
else else
{ {

View File

@ -2,6 +2,7 @@
#include <qpdf/qpdf-config.h> #include <qpdf/qpdf-config.h>
#include <qpdf/QUtil.hh> #include <qpdf/QUtil.hh>
#include <qpdf/PointerHolder.hh>
#include <stdio.h> #include <stdio.h>
#include <errno.h> #include <errno.h>
@ -163,6 +164,21 @@ QUtil::copy_string(std::string const& str)
return result; 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 void
QUtil::binary_stdout() QUtil::binary_stdout()
{ {

View File

@ -75,16 +75,9 @@ ParserCallbacks::handleObject(QPDFObjectHandle obj)
std::cout << obj.getTypeName() << ": "; std::cout << obj.getTypeName() << ": ";
if (obj.isInlineImage()) if (obj.isInlineImage())
{ {
// Exercise getTypeCode
assert(obj.getTypeCode() == QPDFObject::ot_inlineimage); assert(obj.getTypeCode() == QPDFObject::ot_inlineimage);
std::string val = obj.getInlineImageValue(); std::cout << QUtil::hex_encode(obj.getInlineImageValue()) << std::endl;
char buf[3];
buf[2] = '\0';
for (size_t i = 0; i < val.length(); ++i)
{
sprintf(buf, "%02x", (unsigned char)(val[i]));
std::cout << buf;
}
std::cout << std::endl;
} }
else else
{ {