Commit 32b62035ce9d5d07f7396cc50a0c38215f19c906

Authored by Jay Berkenbilt
1 parent 9f159465

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.
ChangeLog
... ... @@ -16,6 +16,9 @@
16 16  
17 17 2013-01-25 Jay Berkenbilt <ejb@ql.org>
18 18  
  19 + * New method QUtil::hex_encode to encode binary data as a
  20 + hexadecimal string
  21 +
19 22 * qpdf --check was exiting with status 0 in some rare cases even
20 23 when errors were found. It now always exits with one of the
21 24 document error codes (0 for success, 2 for errors, 3 or warnings).
... ...
examples/pdf-parse-content.cc
... ... @@ -33,15 +33,7 @@ ParserCallbacks::handleObject(QPDFObjectHandle obj)
33 33 std::cout << obj.getTypeName() << ": ";
34 34 if (obj.isInlineImage())
35 35 {
36   - std::string val = obj.getInlineImageValue();
37   - char buf[3];
38   - buf[2] = '\0';
39   - for (size_t i = 0; i < val.length(); ++i)
40   - {
41   - sprintf(buf, "%02x", (unsigned char)(val[i]));
42   - std::cout << buf;
43   - }
44   - std::cout << std::endl;
  36 + std::cout << QUtil::hex_encode(obj.getInlineImageValue()) << std::endl;
45 37 }
46 38 else
47 39 {
... ...
include/qpdf/QUtil.hh
... ... @@ -56,6 +56,12 @@ namespace QUtil
56 56 QPDF_DLL
57 57 char* copy_string(std::string const&);
58 58  
  59 + // Returns lower-case hex-encoded version of the string, treating
  60 + // each character in the input string as unsigned. The output
  61 + // string will be twice as long as the input string.
  62 + QPDF_DLL
  63 + std::string hex_encode(std::string const&);
  64 +
59 65 // Set stdin, stdout to binary mode
60 66 QPDF_DLL
61 67 void binary_stdout();
... ...
libqpdf/MD5.cc
... ... @@ -386,16 +386,7 @@ void MD5::print()
386 386 std::string MD5::unparse()
387 387 {
388 388 final();
389   -
390   - char result[33];
391   - char* p = result;
392   - unsigned int i;
393   - for (i = 0; i < 16; ++i)
394   - {
395   - sprintf(p, "%02x", digest_val[i]);
396   - p += 2;
397   - }
398   - return result;
  389 + return QUtil::hex_encode(std::string((char*)digest_val, 16));
399 390 }
400 391  
401 392 std::string
... ...
libqpdf/Pl_SHA2.cc
... ... @@ -2,6 +2,7 @@
2 2 #include <stdexcept>
3 3 #include <cstdio>
4 4 #include <qpdf/PointerHolder.hh>
  5 +#include <qpdf/QUtil.hh>
5 6  
6 7 Pl_SHA2::Pl_SHA2(int bits, Pipeline* next) :
7 8 Pipeline("sha2", next),
... ... @@ -150,15 +151,5 @@ Pl_SHA2::getHexDigest()
150 151 throw std::logic_error(
151 152 "digest requested for in-progress SHA2 Pipeline");
152 153 }
153   - std::string raw = getRawDigest();
154   - size_t raw_size = raw.length();
155   - size_t hex_size = 1 + (2 * raw_size);
156   - PointerHolder<char> bufp(true, new char[hex_size]);
157   - char* buf = bufp.getPointer();
158   - buf[hex_size - 1] = '\0';
159   - for (unsigned int i = 0; i < raw_size; ++i)
160   - {
161   - std::sprintf(buf + i * 2, "%02x", (unsigned char)raw[i]);
162   - }
163   - return buf;
  154 + return QUtil::hex_encode(getRawDigest());
164 155 }
... ...
libqpdf/QPDF_Name.cc
... ... @@ -2,6 +2,7 @@
2 2  
3 3 #include <string.h>
4 4 #include <stdio.h>
  5 +#include <qpdf/QUtil.hh>
5 6  
6 7 QPDF_Name::QPDF_Name(std::string const& name) :
7 8 name(name)
... ... @@ -16,7 +17,6 @@ std::string
16 17 QPDF_Name::normalizeName(std::string const& name)
17 18 {
18 19 std::string result;
19   - char num[4];
20 20 result += name[0];
21 21 for (unsigned int i = 1; i < name.length(); ++i)
22 22 {
... ... @@ -24,8 +24,7 @@ QPDF_Name::normalizeName(std::string const&amp; name)
24 24 // Don't use locale/ctype here; follow PDF spec guidelines.
25 25 if (strchr("#()<>[]{}/%", ch) || (ch < 33) || (ch > 126))
26 26 {
27   - sprintf(num, "#%02x", (unsigned char) ch);
28   - result += num;
  27 + result += "#" + QUtil::hex_encode(std::string(&ch, 1));
29 28 }
30 29 else
31 30 {
... ...
libqpdf/QPDF_String.cc
... ... @@ -90,14 +90,7 @@ QPDF_String::unparse(bool force_binary)
90 90 std::string result;
91 91 if (use_hexstring)
92 92 {
93   - result += "<";
94   - char num[3];
95   - for (unsigned int i = 0; i < this->val.length(); ++i)
96   - {
97   - sprintf(num, "%02x", (unsigned char) this->val[i]);
98   - result += num;
99   - }
100   - result += ">";
  93 + result += "<" + QUtil::hex_encode(this->val) + ">";
101 94 }
102 95 else
103 96 {
... ...
libqpdf/QUtil.cc
... ... @@ -2,6 +2,7 @@
2 2 #include <qpdf/qpdf-config.h>
3 3  
4 4 #include <qpdf/QUtil.hh>
  5 +#include <qpdf/PointerHolder.hh>
5 6  
6 7 #include <stdio.h>
7 8 #include <errno.h>
... ... @@ -163,6 +164,21 @@ QUtil::copy_string(std::string const&amp; str)
163 164 return result;
164 165 }
165 166  
  167 +std::string
  168 +QUtil::hex_encode(std::string const& input)
  169 +{
  170 + size_t input_size = input.length();
  171 + size_t hex_size = 1 + (2 * input_size);
  172 + PointerHolder<char> bufp(true, new char[hex_size]);
  173 + char* buf = bufp.getPointer();
  174 + buf[hex_size - 1] = '\0';
  175 + for (unsigned int i = 0; i < input_size; ++i)
  176 + {
  177 + sprintf(buf + i * 2, "%02x", (unsigned char)input[i]);
  178 + }
  179 + return buf;
  180 +}
  181 +
166 182 void
167 183 QUtil::binary_stdout()
168 184 {
... ...
qpdf/test_driver.cc
... ... @@ -75,16 +75,9 @@ ParserCallbacks::handleObject(QPDFObjectHandle obj)
75 75 std::cout << obj.getTypeName() << ": ";
76 76 if (obj.isInlineImage())
77 77 {
  78 + // Exercise getTypeCode
78 79 assert(obj.getTypeCode() == QPDFObject::ot_inlineimage);
79   - std::string val = obj.getInlineImageValue();
80   - char buf[3];
81   - buf[2] = '\0';
82   - for (size_t i = 0; i < val.length(); ++i)
83   - {
84   - sprintf(buf, "%02x", (unsigned char)(val[i]));
85   - std::cout << buf;
86   - }
87   - std::cout << std::endl;
  80 + std::cout << QUtil::hex_encode(obj.getInlineImageValue()) << std::endl;
88 81 }
89 82 else
90 83 {
... ...