2
1
mirror of https://github.com/qpdf/qpdf.git synced 2024-06-22 12:04:53 +00:00

Add private methods QPDF::isCached and QPDF::isUnresolved

This commit is contained in:
m-holger 2022-08-10 13:16:06 +01:00
parent 23d50a2f14
commit c0cd72a3ee
4 changed files with 25 additions and 8 deletions

View File

@ -1175,6 +1175,8 @@ class QPDF
QPDFObjectHandle reserveStream(QPDFObjGen const& og); QPDFObjectHandle reserveStream(QPDFObjGen const& og);
QPDFObjectHandle QPDFObjectHandle
newIndirect(QPDFObjGen const&, std::shared_ptr<QPDFObject> const&); newIndirect(QPDFObjGen const&, std::shared_ptr<QPDFObject> const&);
bool isCached(QPDFObjGen const& og);
bool isUnresolved(QPDFObjGen const& og);
// Calls finish() on the pipeline when done but does not delete it // Calls finish() on the pipeline when done but does not delete it
bool pipeStreamData( bool pipeStreamData(

View File

@ -134,7 +134,11 @@ class QPDFObject
value = o->value; value = o->value;
o->value = v; o->value = v;
} }
bool
isUnresolved()
{
return value->type_code == ::ot_unresolved;
}
template <typename T> template <typename T>
T* T*
as() as()

View File

@ -1425,8 +1425,7 @@ QPDF::fixDanglingReferences(bool force)
for (auto sub: to_check) { for (auto sub: to_check) {
if (sub.isIndirect()) { if (sub.isIndirect()) {
if (sub.getOwningQPDF() == this) { if (sub.getOwningQPDF() == this) {
QPDFObjGen og(sub.getObjGen()); if (!isCached(sub.getObjGen())) {
if (this->m->obj_cache.count(og) == 0) {
QTC::TC("qpdf", "QPDF detected dangling ref"); QTC::TC("qpdf", "QPDF detected dangling ref");
queue.push_back(sub); queue.push_back(sub);
} }
@ -1886,7 +1885,7 @@ QPDF::readObjectAtOffset(
"expected endobj"); "expected endobj");
} }
if (!this->m->obj_cache.count(og)) { if (!isCached(og)) {
// Store the object in the cache here so it gets cached // Store the object in the cache here so it gets cached
// whether we first know the offset or whether we first know // whether we first know the offset or whether we first know
// the object ID and generation (in which we case we would get // the object ID and generation (in which we case we would get
@ -1947,7 +1946,7 @@ QPDF::resolve(QPDFObjGen const& og)
} }
ResolveRecorder rr(this, og); ResolveRecorder rr(this, og);
if ((!this->m->obj_cache.count(og)) && this->m->xref_table.count(og)) { if ((!isCached(og)) && this->m->xref_table.count(og)) {
QPDFXRefEntry const& entry = this->m->xref_table[og]; QPDFXRefEntry const& entry = this->m->xref_table[og];
try { try {
switch (entry.getType()) { switch (entry.getType()) {
@ -1985,7 +1984,7 @@ QPDF::resolve(QPDFObjGen const& og)
": error reading object: " + e.what())); ": error reading object: " + e.what()));
} }
} }
if (this->m->obj_cache.count(og) == 0) { if (!isCached(og)) {
// PDF spec says unknown objects resolve to the null object. // PDF spec says unknown objects resolve to the null object.
QTC::TC("qpdf", "QPDF resolve failure to null"); QTC::TC("qpdf", "QPDF resolve failure to null");
QPDFObjectHandle oh = QPDFObjectHandle::newNull(); QPDFObjectHandle oh = QPDFObjectHandle::newNull();
@ -2112,6 +2111,18 @@ QPDF::newIndirect(QPDFObjGen const& og, std::shared_ptr<QPDFObject> const& obj)
return QPDFObjectHandle::Factory::newIndirect(this, og, obj); return QPDFObjectHandle::Factory::newIndirect(this, og, obj);
} }
bool
QPDF::isCached(QPDFObjGen const& og)
{
return m->obj_cache.count(og) != 0;
}
bool
QPDF::isUnresolved(QPDFObjGen const& og)
{
return !isCached(og) || m->obj_cache[og].object->isUnresolved();
}
QPDFObjectHandle QPDFObjectHandle
QPDF::makeIndirectObject(QPDFObjectHandle oh) QPDF::makeIndirectObject(QPDFObjectHandle oh)
{ {
@ -2129,7 +2140,7 @@ QPDF::makeIndirectObject(QPDFObjectHandle oh)
QPDFObjectHandle QPDFObjectHandle
QPDF::reserveObjectIfNotExists(QPDFObjGen const& og) QPDF::reserveObjectIfNotExists(QPDFObjGen const& og)
{ {
if ((!m->obj_cache.count(og)) && (!m->xref_table.count(og))) { if (!isCached(og) && !m->xref_table.count(og)) {
resolve(og); resolve(og);
m->obj_cache[og].object = QPDF_Reserved::create(); m->obj_cache[og].object = QPDF_Reserved::create();
return newIndirect(og, m->obj_cache[og].object); return newIndirect(og, m->obj_cache[og].object);

View File

@ -2609,7 +2609,7 @@ QPDFObjectHandle::dereference()
if (!isInitialized()) { if (!isInitialized()) {
return false; return false;
} }
if (this->obj->getTypeCode() == QPDFObject::ot_unresolved) { if (this->obj->isUnresolved()) {
this->obj = QPDF::Resolver::resolve(this->qpdf, getObjGen()); this->obj = QPDF::Resolver::resolve(this->qpdf, getObjGen());
} }
return true; return true;