mirror of
https://github.com/qpdf/qpdf.git
synced 2024-12-23 03:18:59 +00:00
Merge pull request #901 from m-holger/jrrr
Refactor removal of reserved objects in QPDF::JSONReactor
This commit is contained in:
commit
7b64f219a6
@ -54,6 +54,8 @@ class JSON
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static int constexpr LATEST = 2;
|
static int constexpr LATEST = 2;
|
||||||
|
|
||||||
|
QPDF_DLL
|
||||||
JSON() = default;
|
JSON() = default;
|
||||||
|
|
||||||
QPDF_DLL
|
QPDF_DLL
|
||||||
@ -369,7 +371,7 @@ class JSON
|
|||||||
}
|
}
|
||||||
virtual ~JSON_dictionary() = default;
|
virtual ~JSON_dictionary() = default;
|
||||||
virtual void write(Pipeline*, size_t depth) const;
|
virtual void write(Pipeline*, size_t depth) const;
|
||||||
std::map<std::string, std::shared_ptr<JSON_value>> members;
|
std::map<std::string, JSON> members;
|
||||||
std::set<std::string> parsed_keys;
|
std::set<std::string> parsed_keys;
|
||||||
};
|
};
|
||||||
struct JSON_array: public JSON_value
|
struct JSON_array: public JSON_value
|
||||||
@ -380,7 +382,7 @@ class JSON
|
|||||||
}
|
}
|
||||||
virtual ~JSON_array() = default;
|
virtual ~JSON_array() = default;
|
||||||
virtual void write(Pipeline*, size_t depth) const;
|
virtual void write(Pipeline*, size_t depth) const;
|
||||||
std::vector<std::shared_ptr<JSON_value>> elements;
|
std::vector<JSON> elements;
|
||||||
};
|
};
|
||||||
struct JSON_string: public JSON_value
|
struct JSON_string: public JSON_value
|
||||||
{
|
{
|
||||||
@ -423,7 +425,7 @@ class JSON
|
|||||||
std::function<void(Pipeline*)> fn;
|
std::function<void(Pipeline*)> fn;
|
||||||
};
|
};
|
||||||
|
|
||||||
JSON(std::shared_ptr<JSON_value>);
|
JSON(std::unique_ptr<JSON_value>);
|
||||||
|
|
||||||
static bool checkSchemaInternal(
|
static bool checkSchemaInternal(
|
||||||
JSON_value* this_v,
|
JSON_value* this_v,
|
||||||
@ -441,13 +443,13 @@ class JSON
|
|||||||
~Members() = default;
|
~Members() = default;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Members(std::shared_ptr<JSON_value>);
|
Members(std::unique_ptr<JSON_value>);
|
||||||
Members(Members const&) = delete;
|
Members(Members const&) = delete;
|
||||||
|
|
||||||
std::shared_ptr<JSON_value> value;
|
std::unique_ptr<JSON_value> value;
|
||||||
// start and end are only populated for objects created by parse
|
// start and end are only populated for objects created by parse
|
||||||
qpdf_offset_t start;
|
qpdf_offset_t start{0};
|
||||||
qpdf_offset_t end;
|
qpdf_offset_t end{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
std::shared_ptr<Members> m;
|
std::shared_ptr<Members> m;
|
||||||
|
@ -9,15 +9,13 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
JSON::Members::Members(std::shared_ptr<JSON_value> value) :
|
JSON::Members::Members(std::unique_ptr<JSON_value> value) :
|
||||||
value(value),
|
value(std::move(value))
|
||||||
start(0),
|
|
||||||
end(0)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON::JSON(std::shared_ptr<JSON_value> value) :
|
JSON::JSON(std::unique_ptr<JSON_value> value) :
|
||||||
m(new Members(value))
|
m(new Members(std::move(value)))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -278,7 +276,7 @@ JSON::encode_string(std::string const& str)
|
|||||||
JSON
|
JSON
|
||||||
JSON::makeDictionary()
|
JSON::makeDictionary()
|
||||||
{
|
{
|
||||||
return JSON(std::make_shared<JSON_dictionary>());
|
return JSON(std::make_unique<JSON_dictionary>());
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON
|
JSON
|
||||||
@ -286,7 +284,7 @@ JSON::addDictionaryMember(std::string const& key, JSON const& val)
|
|||||||
{
|
{
|
||||||
if (auto* obj = dynamic_cast<JSON_dictionary*>(this->m->value.get())) {
|
if (auto* obj = dynamic_cast<JSON_dictionary*>(this->m->value.get())) {
|
||||||
return obj->members[encode_string(key)] =
|
return obj->members[encode_string(key)] =
|
||||||
val.m->value ? val.m->value : std::make_shared<JSON_null>();
|
val.m->value ? val : makeNull();
|
||||||
} else {
|
} else {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"JSON::addDictionaryMember called on non-dictionary");
|
"JSON::addDictionaryMember called on non-dictionary");
|
||||||
@ -311,7 +309,7 @@ JSON::checkDictionaryKeySeen(std::string const& key)
|
|||||||
JSON
|
JSON
|
||||||
JSON::makeArray()
|
JSON::makeArray()
|
||||||
{
|
{
|
||||||
return JSON(std::make_shared<JSON_array>());
|
return JSON(std::make_unique<JSON_array>());
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON
|
JSON
|
||||||
@ -322,9 +320,9 @@ JSON::addArrayElement(JSON const& val)
|
|||||||
throw std::runtime_error("JSON::addArrayElement called on non-array");
|
throw std::runtime_error("JSON::addArrayElement called on non-array");
|
||||||
}
|
}
|
||||||
if (val.m->value.get()) {
|
if (val.m->value.get()) {
|
||||||
arr->elements.push_back(val.m->value);
|
arr->elements.push_back(val);
|
||||||
} else {
|
} else {
|
||||||
arr->elements.push_back(std::make_shared<JSON_null>());
|
arr->elements.push_back(makeNull());
|
||||||
}
|
}
|
||||||
return arr->elements.back();
|
return arr->elements.back();
|
||||||
}
|
}
|
||||||
@ -332,43 +330,43 @@ JSON::addArrayElement(JSON const& val)
|
|||||||
JSON
|
JSON
|
||||||
JSON::makeString(std::string const& utf8)
|
JSON::makeString(std::string const& utf8)
|
||||||
{
|
{
|
||||||
return JSON(std::make_shared<JSON_string>(utf8));
|
return JSON(std::make_unique<JSON_string>(utf8));
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON
|
JSON
|
||||||
JSON::makeInt(long long int value)
|
JSON::makeInt(long long int value)
|
||||||
{
|
{
|
||||||
return JSON(std::make_shared<JSON_number>(value));
|
return JSON(std::make_unique<JSON_number>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON
|
JSON
|
||||||
JSON::makeReal(double value)
|
JSON::makeReal(double value)
|
||||||
{
|
{
|
||||||
return JSON(std::make_shared<JSON_number>(value));
|
return JSON(std::make_unique<JSON_number>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON
|
JSON
|
||||||
JSON::makeNumber(std::string const& encoded)
|
JSON::makeNumber(std::string const& encoded)
|
||||||
{
|
{
|
||||||
return JSON(std::make_shared<JSON_number>(encoded));
|
return JSON(std::make_unique<JSON_number>(encoded));
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON
|
JSON
|
||||||
JSON::makeBool(bool value)
|
JSON::makeBool(bool value)
|
||||||
{
|
{
|
||||||
return JSON(std::make_shared<JSON_bool>(value));
|
return JSON(std::make_unique<JSON_bool>(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON
|
JSON
|
||||||
JSON::makeNull()
|
JSON::makeNull()
|
||||||
{
|
{
|
||||||
return JSON(std::make_shared<JSON_null>());
|
return JSON(std::make_unique<JSON_null>());
|
||||||
}
|
}
|
||||||
|
|
||||||
JSON
|
JSON
|
||||||
JSON::makeBlob(std::function<void(Pipeline*)> fn)
|
JSON::makeBlob(std::function<void(Pipeline*)> fn)
|
||||||
{
|
{
|
||||||
return JSON(std::make_shared<JSON_blob>(fn));
|
return JSON(std::make_unique<JSON_blob>(fn));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -504,11 +502,11 @@ JSON::checkSchemaInternal(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (sch_dict && (!pattern_key.empty())) {
|
if (sch_dict && (!pattern_key.empty())) {
|
||||||
auto pattern_schema = sch_dict->members[pattern_key].get();
|
auto pattern_schema = sch_dict->members[pattern_key].m->value.get();
|
||||||
for (auto const& iter: this_dict->members) {
|
for (auto const& iter: this_dict->members) {
|
||||||
std::string const& key = iter.first;
|
std::string const& key = iter.first;
|
||||||
checkSchemaInternal(
|
checkSchemaInternal(
|
||||||
this_dict->members[key].get(),
|
this_dict->members[key].m->value.get(),
|
||||||
pattern_schema,
|
pattern_schema,
|
||||||
flags,
|
flags,
|
||||||
errors,
|
errors,
|
||||||
@ -519,8 +517,8 @@ JSON::checkSchemaInternal(
|
|||||||
std::string const& key = iter.first;
|
std::string const& key = iter.first;
|
||||||
if (this_dict->members.count(key)) {
|
if (this_dict->members.count(key)) {
|
||||||
checkSchemaInternal(
|
checkSchemaInternal(
|
||||||
this_dict->members[key].get(),
|
this_dict->members[key].m->value.get(),
|
||||||
iter.second.get(),
|
iter.second.m->value.get(),
|
||||||
flags,
|
flags,
|
||||||
errors,
|
errors,
|
||||||
prefix + "." + key);
|
prefix + "." + key);
|
||||||
@ -557,8 +555,8 @@ JSON::checkSchemaInternal(
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
for (auto const& element: this_arr->elements) {
|
for (auto const& element: this_arr->elements) {
|
||||||
checkSchemaInternal(
|
checkSchemaInternal(
|
||||||
element.get(),
|
element.m->value.get(),
|
||||||
sch_arr->elements.at(0).get(),
|
sch_arr->elements.at(0).m->value.get(),
|
||||||
flags,
|
flags,
|
||||||
errors,
|
errors,
|
||||||
prefix + "." + std::to_string(i));
|
prefix + "." + std::to_string(i));
|
||||||
@ -568,7 +566,7 @@ JSON::checkSchemaInternal(
|
|||||||
QTC::TC("libtests", "JSON schema array for single item");
|
QTC::TC("libtests", "JSON schema array for single item");
|
||||||
checkSchemaInternal(
|
checkSchemaInternal(
|
||||||
this_v,
|
this_v,
|
||||||
sch_arr->elements.at(0).get(),
|
sch_arr->elements.at(0).m->value.get(),
|
||||||
flags,
|
flags,
|
||||||
errors,
|
errors,
|
||||||
prefix);
|
prefix);
|
||||||
@ -587,8 +585,8 @@ JSON::checkSchemaInternal(
|
|||||||
size_t i = 0;
|
size_t i = 0;
|
||||||
for (auto const& element: this_arr->elements) {
|
for (auto const& element: this_arr->elements) {
|
||||||
checkSchemaInternal(
|
checkSchemaInternal(
|
||||||
element.get(),
|
element.m->value.get(),
|
||||||
sch_arr->elements.at(i).get(),
|
sch_arr->elements.at(i).m->value.get(),
|
||||||
flags,
|
flags,
|
||||||
errors,
|
errors,
|
||||||
prefix + "." + std::to_string(i));
|
prefix + "." + std::to_string(i));
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
#include <qpdf/QIntC.hh>
|
#include <qpdf/QIntC.hh>
|
||||||
#include <qpdf/QPDFObject_private.hh>
|
#include <qpdf/QPDFObject_private.hh>
|
||||||
#include <qpdf/QPDFValue.hh>
|
#include <qpdf/QPDFValue.hh>
|
||||||
|
#include <qpdf/QPDF_Null.hh>
|
||||||
#include <qpdf/QTC.hh>
|
#include <qpdf/QTC.hh>
|
||||||
#include <qpdf/QUtil.hh>
|
#include <qpdf/QUtil.hh>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -234,6 +235,11 @@ class QPDF::JSONReactor: public JSON::Reactor
|
|||||||
descr(std::make_shared<QPDFValue::Description>(QPDFValue::JSON_Descr(
|
descr(std::make_shared<QPDFValue::Description>(QPDFValue::JSON_Descr(
|
||||||
std::make_shared<std::string>(is->getName()), "")))
|
std::make_shared<std::string>(is->getName()), "")))
|
||||||
{
|
{
|
||||||
|
for (auto& oc: pdf.m->obj_cache) {
|
||||||
|
if (oc.second.object->getTypeCode() == ::ot_reserved) {
|
||||||
|
reserved.insert(oc.first);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
virtual ~JSONReactor() = default;
|
virtual ~JSONReactor() = default;
|
||||||
virtual void dictionaryStart() override;
|
virtual void dictionaryStart() override;
|
||||||
@ -265,7 +271,6 @@ class QPDF::JSONReactor: public JSON::Reactor
|
|||||||
void setObjectDescription(QPDFObjectHandle& oh, JSON const& value);
|
void setObjectDescription(QPDFObjectHandle& oh, JSON const& value);
|
||||||
QPDFObjectHandle makeObject(JSON const& value);
|
QPDFObjectHandle makeObject(JSON const& value);
|
||||||
void error(qpdf_offset_t offset, std::string const& message);
|
void error(qpdf_offset_t offset, std::string const& message);
|
||||||
QPDFObjectHandle reserveObject(int obj, int gen);
|
|
||||||
void replaceObject(
|
void replaceObject(
|
||||||
QPDFObjectHandle to_replace,
|
QPDFObjectHandle to_replace,
|
||||||
QPDFObjectHandle replacement,
|
QPDFObjectHandle replacement,
|
||||||
@ -416,29 +421,19 @@ QPDF::JSONReactor::containerEnd(JSON const& value)
|
|||||||
object_stack.pop_back();
|
object_stack.pop_back();
|
||||||
}
|
}
|
||||||
} else if ((state == st_top) && (from_state == st_qpdf)) {
|
} else if ((state == st_top) && (from_state == st_qpdf)) {
|
||||||
for (auto const& og: this->reserved) {
|
// Handle dangling indirect object references which the PDF spec says to
|
||||||
// Handle dangling indirect object references which the
|
// treat as nulls. It's tempting to make this an error, but that would
|
||||||
// PDF spec says to treat as nulls. It's tempting to make
|
// be wrong since valid input files may have these.
|
||||||
// this an error, but that would be wrong since valid
|
for (auto& oc: pdf.m->obj_cache) {
|
||||||
// input files may have these.
|
if (oc.second.object->getTypeCode() == ::ot_reserved &&
|
||||||
QTC::TC("qpdf", "QPDF_json non-trivial null reserved");
|
reserved.count(oc.first) == 0) {
|
||||||
this->pdf.replaceObject(og, QPDFObjectHandle::newNull());
|
QTC::TC("qpdf", "QPDF_json non-trivial null reserved");
|
||||||
|
pdf.updateCache(oc.first, QPDF_Null::create(), -1, -1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this->reserved.clear();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QPDFObjectHandle
|
|
||||||
QPDF::JSONReactor::reserveObject(int obj, int gen)
|
|
||||||
{
|
|
||||||
QPDFObjGen og(obj, gen);
|
|
||||||
auto oh = pdf.reserveObjectIfNotExists(og);
|
|
||||||
if (oh.isReserved()) {
|
|
||||||
this->reserved.insert(og);
|
|
||||||
}
|
|
||||||
return oh;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
QPDF::JSONReactor::replaceObject(
|
QPDF::JSONReactor::replaceObject(
|
||||||
QPDFObjectHandle to_replace,
|
QPDFObjectHandle to_replace,
|
||||||
@ -446,7 +441,6 @@ QPDF::JSONReactor::replaceObject(
|
|||||||
JSON const& value)
|
JSON const& value)
|
||||||
{
|
{
|
||||||
auto og = to_replace.getObjGen();
|
auto og = to_replace.getObjGen();
|
||||||
this->reserved.erase(og);
|
|
||||||
this->pdf.replaceObject(og, replacement);
|
this->pdf.replaceObject(og, replacement);
|
||||||
auto oh = pdf.getObject(og);
|
auto oh = pdf.getObject(og);
|
||||||
setObjectDescription(oh, value);
|
setObjectDescription(oh, value);
|
||||||
@ -564,7 +558,7 @@ QPDF::JSONReactor::dictionaryItem(std::string const& key, JSON const& value)
|
|||||||
this->cur_object = "trailer";
|
this->cur_object = "trailer";
|
||||||
} else if (is_obj_key(key, obj, gen)) {
|
} else if (is_obj_key(key, obj, gen)) {
|
||||||
this->cur_object = key;
|
this->cur_object = key;
|
||||||
auto oh = reserveObject(obj, gen);
|
auto oh = pdf.reserveObjectIfNotExists(QPDFObjGen(obj, gen));
|
||||||
object_stack.push_back(oh);
|
object_stack.push_back(oh);
|
||||||
nestedState(key, value, st_object_top);
|
nestedState(key, value, st_object_top);
|
||||||
} else {
|
} else {
|
||||||
@ -763,7 +757,7 @@ QPDF::JSONReactor::makeObject(JSON const& value)
|
|||||||
int gen = 0;
|
int gen = 0;
|
||||||
std::string str;
|
std::string str;
|
||||||
if (is_indirect_object(str_v, obj, gen)) {
|
if (is_indirect_object(str_v, obj, gen)) {
|
||||||
result = reserveObject(obj, gen);
|
result = pdf.reserveObjectIfNotExists(QPDFObjGen(obj, gen));
|
||||||
} else if (is_unicode_string(str_v, str)) {
|
} else if (is_unicode_string(str_v, str)) {
|
||||||
result = QPDFObjectHandle::newUnicodeString(str);
|
result = QPDFObjectHandle::newUnicodeString(str);
|
||||||
} else if (is_binary_string(str_v, str)) {
|
} else if (is_binary_string(str_v, str)) {
|
||||||
|
Loading…
Reference in New Issue
Block a user