diff --git a/libqpdf/ContentNormalizer.cc b/libqpdf/ContentNormalizer.cc index bca8ad6..a092f02 100644 --- a/libqpdf/ContentNormalizer.cc +++ b/libqpdf/ContentNormalizer.cc @@ -1,8 +1,10 @@ #include -#include +#include #include +using namespace qpdf; + ContentNormalizer::ContentNormalizer() : any_bad_tokens(false), last_token_was_bad(false) @@ -55,7 +57,7 @@ ContentNormalizer::handleToken(QPDFTokenizer::Token const& token) break; case QPDFTokenizer::tt_name: - write(QPDF_Name::normalizeName(token.getValue())); + write(Name::normalize(token.getValue())); break; default: diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index d0d9159..beb0279 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -15,7 +15,7 @@ #include #include #include -#include +#include #include #include #include @@ -27,6 +27,7 @@ #include using namespace std::literals; +using namespace qpdf; QPDFWriter::ProgressReporter::~ProgressReporter() // NOLINT (modernize-use-equals-default) { @@ -1176,7 +1177,7 @@ QPDFWriter::writeTrailer( for (auto const& key: trailer.getKeys()) { writeStringQDF(" "); writeStringNoQDF(" "); - writeString(QPDF_Name::normalizeName(key)); + writeString(Name::normalize(key)); writeString(" "); if (key == "/Size") { writeString(std::to_string(size)); @@ -1503,7 +1504,7 @@ QPDFWriter::unparseObject( auto const& key = item.first; writeString(indent); writeStringQDF(" "); - writeString(QPDF_Name::normalizeName(key)); + writeString(Name::normalize(key)); writeString(" "); if (key == "/Contents" && object.isDictionaryOfType("/Sig") && object.hasKey("/ByteRange")) { diff --git a/libqpdf/QPDF_Dictionary.cc b/libqpdf/QPDF_Dictionary.cc index 9b5422a..ae6bce0 100644 --- a/libqpdf/QPDF_Dictionary.cc +++ b/libqpdf/QPDF_Dictionary.cc @@ -63,7 +63,7 @@ QPDF_Dictionary::unparse() std::string result = "<< "; for (auto& iter: this->items) { if (!iter.second.isNull()) { - result += QPDF_Name::normalizeName(iter.first) + " " + iter.second.unparse() + " "; + result += Name::normalize(iter.first) + " " + iter.second.unparse() + " "; } } result += ">>"; @@ -78,17 +78,15 @@ QPDF_Dictionary::writeJSON(int json_version, JSON::Writer& p) if (!iter.second.isNull()) { p.writeNext(); if (json_version == 1) { - p << "\"" << JSON::Writer::encode_string(QPDF_Name::normalizeName(iter.first)) - << "\": "; - } else if (auto res = QPDF_Name::analyzeJSONEncoding(iter.first); res.first) { + p << "\"" << JSON::Writer::encode_string(Name::normalize(iter.first)) << "\": "; + } else if (auto res = Name::analyzeJSONEncoding(iter.first); res.first) { if (res.second) { p << "\"" << iter.first << "\": "; } else { p << "\"" << JSON::Writer::encode_string(iter.first) << "\": "; } } else { - p << "\"n:" << JSON::Writer::encode_string(QPDF_Name::normalizeName(iter.first)) - << "\": "; + p << "\"n:" << JSON::Writer::encode_string(Name::normalize(iter.first)) << "\": "; } iter.second.writeJSON(json_version, p); } diff --git a/libqpdf/QPDF_Name.cc b/libqpdf/QPDF_Name.cc index b66a8a2..c3b36a8 100644 --- a/libqpdf/QPDF_Name.cc +++ b/libqpdf/QPDF_Name.cc @@ -1,8 +1,11 @@ #include #include +#include #include +using namespace qpdf; + QPDF_Name::QPDF_Name(std::string const& name) : QPDFValue(::ot_name), name(name) @@ -22,7 +25,7 @@ QPDF_Name::copy(bool shallow) } std::string -QPDF_Name::normalizeName(std::string const& name) +Name::normalize(std::string const& name) { if (name.empty()) { return name; @@ -49,11 +52,11 @@ QPDF_Name::normalizeName(std::string const& name) std::string QPDF_Name::unparse() { - return normalizeName(this->name); + return Name::normalize(name); } std::pair -QPDF_Name::analyzeJSONEncoding(const std::string& name) +Name::analyzeJSONEncoding(const std::string& name) { int tail = 0; // Number of continuation characters expected. bool tail2 = false; // Potential overlong 3 octet utf-8. @@ -105,16 +108,16 @@ QPDF_Name::writeJSON(int json_version, JSON::Writer& p) // For performance reasons this code is duplicated in QPDF_Dictionary::writeJSON. When updating // this method make sure QPDF_Dictionary is also update. if (json_version == 1) { - p << "\"" << JSON::Writer::encode_string(normalizeName(name)) << "\""; + p << "\"" << JSON::Writer::encode_string(Name::normalize(name)) << "\""; } else { - if (auto res = analyzeJSONEncoding(name); res.first) { + if (auto res = Name::analyzeJSONEncoding(name); res.first) { if (res.second) { p << "\"" << name << "\""; } else { p << "\"" << JSON::Writer::encode_string(name) << "\""; } } else { - p << "\"n:" << JSON::Writer::encode_string(normalizeName(name)) << "\""; + p << "\"n:" << JSON::Writer::encode_string(Name::normalize(name)) << "\""; } } } diff --git a/libqpdf/qpdf/QPDFObjectHandle_private.hh b/libqpdf/qpdf/QPDFObjectHandle_private.hh index 4554dc1..f7820f9 100644 --- a/libqpdf/qpdf/QPDFObjectHandle_private.hh +++ b/libqpdf/qpdf/QPDFObjectHandle_private.hh @@ -81,6 +81,18 @@ namespace qpdf } }; + class Name final: public BaseHandle + { + public: + // Put # into strings with characters unsuitable for name token + static std::string normalize(std::string const& name); + + // Check whether name is valid utf-8 and whether it contains characters that require + // escaping. Return {false, false} if the name is not valid utf-8, otherwise return {true, + // true} if no characters require or {true, false} if escaping is required. + static std::pair analyzeJSONEncoding(std::string const& name); + }; + class Stream final: public BaseHandle { public: diff --git a/libqpdf/qpdf/QPDF_Name.hh b/libqpdf/qpdf/QPDF_Name.hh index b5d3c31..fdc51fb 100644 --- a/libqpdf/qpdf/QPDF_Name.hh +++ b/libqpdf/qpdf/QPDF_Name.hh @@ -11,14 +11,6 @@ class QPDF_Name: public QPDFValue std::shared_ptr copy(bool shallow = false) override; std::string unparse() override; void writeJSON(int json_version, JSON::Writer& p) override; - - // Put # into strings with characters unsuitable for name token - static std::string normalizeName(std::string const& name); - - // Check whether name is valid utf-8 and whether it contains characters that require escaping. - // Return {false, false} if the name is not valid utf-8, otherwise return {true, true} if no - // characters require or {true, false} if escaping is required. - static std::pair analyzeJSONEncoding(std::string const& name); std::string getStringValue() const override {